[FIX] website_event_sale: multi tickets

bzr revid: chm@openerp.com-20140502140152-hx7ktaf0cg2m7ija
This commit is contained in:
chm@openerp.com 2014-05-02 16:01:52 +02:00
parent ee16b014b1
commit c10800f2f1
9 changed files with 109 additions and 73 deletions

View File

@ -37,6 +37,9 @@ class product_template(osv.osv):
class product(osv.osv):
_inherit = 'product.product'
_columns = {
'event_ticket_ids': fields.one2many('event.event.ticket', 'product_id', 'Event Tickets'),
}
def onchange_event_ok(self, cr, uid, ids, type, event_ok, context=None):
# cannot directly forward to product.template as the ids are theoretically different

View File

@ -28,54 +28,23 @@ from openerp.tools.translate import _
class website_event(website_event):
@http.route(['/event/add_cart'], type='http', auth="public", website=True, multilang=True)
def add_cart(self, event_id, **post):
user_obj = request.registry['res.users']
order_line_obj = request.registry.get('sale.order.line')
@http.route(['/event/cart/update'], type='http', auth="public", methods=['POST'], website=True, multilang=True)
def cart_update(self, event_id, **post):
cr, uid, context = request.cr, request.uid, request.context
ticket_obj = request.registry.get('event.event.ticket')
order_obj = request.registry.get('sale.order')
order = request.website.sale_get_order(force_create=1)
partner_id = user_obj.browse(request.cr, SUPERUSER_ID, request.uid,
context=request.context).partner_id.id
fields = [k for k, v in order_line_obj._columns.items()]
values = order_line_obj.default_get(request.cr, SUPERUSER_ID, fields,
context=request.context)
_values = None
sale = False
for key, value in post.items():
try:
quantity = int(value)
assert quantity > 0
except:
quantity = None
ticket_id = key.split("-")[0] == 'ticket' and int(key.split("-")[1]) or None
if not ticket_id or not quantity:
quantity = int(value or "0")
if not quantity:
continue
ticket = ticket_obj.browse(request.cr, request.uid, ticket_id,
context=request.context)
sale = True
ticket_id = key.split("-")[0] == 'ticket' and int(key.split("-")[1]) or None
ticket = ticket_obj.browse(cr, SUPERUSER_ID, ticket_id, context=context)
request.website.sale_get_order(force_create=1)._cart_update(
product_id=ticket.product_id.id, add_qty=quantity, context=dict(context, event_ticket_id=ticket.id))
values['product_id'] = ticket.product_id.id
values['event_id'] = ticket.event_id.id
values['event_ticket_id'] = ticket.id
values['product_uom_qty'] = quantity
values['price_unit'] = ticket.price
values['order_id'] = order.id
values['name'] = "%s: %s" % (ticket.event_id.name, ticket.name)
# change and record value
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
_values = order_line_obj.product_id_change(
request.cr, SUPERUSER_ID, [], pricelist_id, ticket.product_id.id,
partner_id=partner_id, context=request.context)['value']
_values.update(values)
order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID, _values, context=request.context)
order_obj.write(request.cr, SUPERUSER_ID, [order.id], {'order_line': [(4, order_line_id)]}, context=request.context)
if not _values:
if not sale:
return request.redirect("/event/%s" % event_id)
return request.redirect("/shop/checkout")

View File

@ -1,2 +1,3 @@
import product
import website
import sale_order

View File

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
from openerp import SUPERUSER_ID
from openerp.osv import osv, fields
from openerp.tools.translate import _
# defined for access rules
class sale_order(osv.Model):
_inherit = "sale.order"
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None):
for so in self.browse(cr, uid, ids, context=context):
order_line_id = None
domain = [('order_id', '=', so.id), ('product_id', '=', product_id)]
if line_id:
domain += [('id', '=', line_id)]
elif context.get("event_ticket_id"):
domain += [('event_ticket_id', '=', context.get("event_ticket_id"))]
order_line_ids = self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
if order_line_ids:
order_line_id = order_line_ids[0]
return order_line_id
def _website_product_id_change(self, cr, uid, ids, order_id, product_id, line_id=None, context=None):
values = super(sale_order,self)._website_product_id_change(cr, uid, ids, order_id, product_id, line_id=None, context=None)
event_ticket_id = None
if context.get("event_ticket_id"):
event_ticket_id = context.get("event_ticket_id")
elif line_id:
line = self.pool.get('sale.order.line').browse(cr, SUPERUSER_ID, line_id, context=context)
if line.event_ticket_id:
event_ticket_id = line.event_ticket_id.id
else:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
if product.event_ticket_ids:
event_ticket_id = product.event_ticket_ids[0]
if event_ticket_id:
ticket = self.pool.get('event.event.ticket').browse(cr, uid, event_ticket_id, context=context)
if product_id != ticket.product_id.id:
raise osv.except_osv(_('Error!'),_("The ticket doesn't match with this product."))
values['product_id'] = ticket.product_id.id
values['event_id'] = ticket.event_id.id
values['event_ticket_id'] = ticket.id
values['price_unit'] = ticket.price
values['name'] = "%s: %s" % (ticket.event_id.name, ticket.name)
return values

View File

@ -28,7 +28,7 @@
<template id="event_description_full" inherit_id="website_event.event_description_full" inherit_option_id="website_event.event_description_full" name="Event's Ticket form">
<xpath expr="//div[@t-field='event.description']" position="before">
<form t-attf-action="/event/add_cart?event_id=#{ event.id }" method="post" t-if="event.event_ticket_ids">
<form t-attf-action="/event/cart/update?event_id=#{ event.id }" method="post" t-if="event.event_ticket_ids">
<table itemprop="offers" class="table table-striped">
<thead>
<tr>

View File

@ -234,9 +234,9 @@ class website_sale(http.Controller):
return request.redirect("/shop/cart")
@http.route(['/shop/cart/update_json'], type='json', auth="public", methods=['POST'], website=True, multilang=True)
def cart_update_json(self, product_id, add_qty=None, set_qty=None):
def cart_update_json(self, product_id, line_id, add_qty=None, set_qty=None):
order = request.website.sale_get_order(force_create=1)
quantity = order._cart_update(product_id=product_id, add_qty=add_qty, set_qty=set_qty)
quantity = order._cart_update(product_id=product_id, line_id=line_id, add_qty=add_qty, set_qty=set_qty)
return {
'quantity': quantity,
'cart_quantity': order.cart_quantity,

View File

@ -40,40 +40,51 @@ class sale_order(osv.Model):
'order': order
}
def _cart_find_product_line(self, cr, uid, ids, product_id=None, context=None):
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None):
for so in self.browse(cr, uid, ids, context=context):
line_id = None
line_ids = self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, [('order_id', '=', so.id), ('product_id', '=', product_id)], context=context)
if line_ids:
line_id = line_ids[0]
return line_id
order_line_id = None
domain = [('order_id', '=', so.id), ('product_id', '=', product_id)]
if line_id:
domain += [('id', '=', line_id)]
order_line_ids = self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
if order_line_ids:
order_line_id = order_line_ids[0]
return order_line_id
def _cart_update(self, cr, uid, ids, product_id=None, add_qty=0, set_qty=0, context=None):
def _website_product_id_change(self, cr, uid, ids, order_id, product_id, line_id=None, context=None):
so = self.pool.get('sale.order').browse(cr, uid, order_id, context=context)
values = self.pool.get('sale.order.line').product_id_change(cr, SUPERUSER_ID, [],
pricelist=so.pricelist_id.id,
product=product_id,
partner_id=so.partner_id.id,
context=context
)['value']
if line_id:
line = self.pool.get('sale.order.line').browse(cr, SUPERUSER_ID, line_id, context=context)
values['name'] = line.name
else:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
values['name'] = product.name_get()[0][1]
values['product_id'] = product_id
values['order_id'] = order_id
if values.get('tax_id') != None:
values['tax_id'] = [(6, 0, values['tax_id'])]
return values
def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None):
""" Add or set product quantity, add_qty can be negative """
sol = self.pool.get('sale.order.line')
def product_id_change(so, product_id):
values = sol.product_id_change(cr, SUPERUSER_ID, [],
pricelist=so.pricelist_id.id,
product=product_id,
partner_id=so.partner_id.id,
context=context
)['value']
values['name'] = "%s: %s" % (product.name, product.variants) if product.variants else product.name
values['product_id'] = product_id
values['order_id'] = so.id
if values.get('tax_id') != None:
values['tax_id'] = [(6, 0, values['tax_id'])]
return values
quantity = 0
for so in self.browse(cr, uid, ids, context=context):
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
line_id = so._cart_find_product_line(product_id)
line_id = so._cart_find_product_line(product_id, line_id, context=context)
# Create line if no line with product_id can be located
if not line_id:
values = product_id_change(so, product_id)
values = self._website_product_id_change(cr, uid, ids, so.id, product_id, context=context)
line_id = sol.create(cr, SUPERUSER_ID, values, context=context)
# compute new quantity
@ -87,7 +98,7 @@ class sale_order(osv.Model):
sol.unlink(cr, SUPERUSER_ID, [line_id], context=context)
else:
# update line
values = product_id_change(so, product_id)
values = self._website_product_id_change(cr, uid, ids, so.id, product_id, line_id, context=context)
values['product_uom_qty'] = quantity
sol.write(cr, SUPERUSER_ID, [line_id], values, context=context)

View File

@ -17,7 +17,8 @@ $(document).ready(function () {
var value = parseInt($input.val(), 10);
if (isNaN(value)) value = 0;
openerp.jsonRpc("/shop/cart/update_json", 'call', {
'product_id': parseInt($input.data('id'),10),
'line_id': parseInt($input.data('line-id'),10),
'product_id': parseInt($input.data('product-id'),10),
'set_qty': value})
.then(function (data) {
if (!data.quantity) {

View File

@ -547,7 +547,9 @@
</a>
</span>
<input type="text" class="js_quantity form-control"
t-att-data-id="line.product_id.id" t-att-value="int(line.product_uom_qty)"/>
t-att-data-line-id="line.id"
t-att-data-product-id="line.product_id.id"
t-att-value="int(line.product_uom_qty)"/>
<span class="input-group-addon">
<a t-attf-href="#" class="mb8 float_left js_add_cart_json">
<i class="fa fa-plus"></i>