diff --git a/addons/website_event_sale/models/sale_order.py b/addons/website_event_sale/models/sale_order.py index 91f282a1c00..e0eb101c173 100644 --- a/addons/website_event_sale/models/sale_order.py +++ b/addons/website_event_sale/models/sale_order.py @@ -7,14 +7,22 @@ from openerp.tools.translate import _ 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): + def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, linked_line_id=None, optional_product_ids=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"))] + domain += linked_line_id and [('linked_line_id', '=', linked_line_id)] or [('linked_line_id', '=', False)] + if not line_id: + if optional_product_ids: + domain += [('option_line_ids.product_id', '=', pid) for pid in optional_product_ids] + else: + domain += [('option_line_ids', '=', False)] + + order_line_id = None 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] diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 8b7adb6c4b3..97fa24e6c83 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -265,8 +265,19 @@ class website_sale(http.Controller): def cart_update(self, product_id, add_qty=1, set_qty=0, goto_shop=None, **kw): cr, uid, context, pool = request.cr, request.uid, request.context, request.registry order = request.website.sale_get_order(force_create=1) + + optional_product_ids = [] + for k, v in kw.items(): + if "optional-product-" in k and int(kw.get(k.replace("product", "quantity"))): + optional_product_ids.append(int(v)) if add_qty or set_qty: - order._cart_update(product_id=int(product_id), add_qty=int(add_qty), set_qty=int(set_qty)) + line_id, quantity, option_ids = order._cart_update(product_id=int(product_id), + add_qty=int(add_qty), set_qty=int(set_qty), + optional_product_ids=optional_product_ids) + # options have all time the same quantity + for option_id in optional_product_ids: + order._cart_update(product_id=option_id, set_qty=quantity, linked_line_id=line_id) + if goto_shop: attrib_list = request.httprequest.args.getlist('attrib') keep = QueryURL('/shop', category=kw.get('category'), search=kw.get('search'), attrib=attrib_list) @@ -277,11 +288,12 @@ class website_sale(http.Controller): @http.route(['/shop/cart/update_json'], type='json', auth="public", methods=['POST'], website=True) def cart_update_json(self, product_id, line_id, add_qty=None, set_qty=None, display=True): order = request.website.sale_get_order(force_create=1) - quantity = order._cart_update(product_id=product_id, line_id=line_id, add_qty=add_qty, set_qty=set_qty) + line_id, quantity, option_ids = order._cart_update(product_id=product_id, line_id=line_id, add_qty=add_qty, set_qty=set_qty) if not display: return None return { 'quantity': quantity, + 'option_ids': option_ids, 'cart_quantity': order.cart_quantity, 'website_sale.total': request.website._render("website_sale.total", { 'website_sale_order': request.website.sale_get_order() diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index 759884ab1fd..b18888cfe20 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -4,6 +4,7 @@ import random from openerp import SUPERUSER_ID from openerp.osv import osv, orm, fields from openerp.addons.web.http import request +from openerp.tools.translate import _ class payment_transaction(orm.Model): @@ -14,6 +15,13 @@ class payment_transaction(orm.Model): 'sale_order_id': fields.many2one('sale.order', 'Sale Order'), } +class sale_order_line(osv.Model): + _inherit = "sale.order.line" + _columns = { + 'linked_line_id': fields.many2one('sale.order.line', 'Linked Order Line', domain="[('order_id','!=',order_id)]"), + 'option_line_ids': fields.one2many('sale.order.line', 'linked_line_id', string='Options Linked'), + } + class sale_order(osv.Model): _inherit = "sale.order" @@ -43,12 +51,20 @@ class sale_order(osv.Model): 'order': order } - def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None): + def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, linked_line_id=None, optional_product_ids=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)] + domain += linked_line_id and [('linked_line_id', '=', linked_line_id)] or [('linked_line_id', '=', False)] + if not line_id: + if optional_product_ids: + domain += [('option_line_ids.product_id', '=', pid) for pid in optional_product_ids] + else: + domain += [('option_line_ids', '=', False)] + + order_line_id = None 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] @@ -69,7 +85,7 @@ class sale_order(osv.Model): 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['name'] = product.description_sale values['product_id'] = product_id values['order_id'] = order_id @@ -77,17 +93,21 @@ class sale_order(osv.Model): 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): + def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, linked_line_id=None, optional_product_ids=None, context=None): """ Add or set product quantity, add_qty can be negative """ sol = self.pool.get('sale.order.line') quantity = 0 for so in self.browse(cr, uid, ids, context=context): - line_id = so._cart_find_product_line(product_id, line_id, context=context) + line_id = so._cart_find_product_line(product_id, line_id, linked_line_id, optional_product_ids, context=context) # Create line if no line with product_id can be located if not line_id: values = self._website_product_id_change(cr, uid, ids, so.id, product_id, context=context) + if linked_line_id and linked_line_id in map(int,so.order_line): + values["linked_line_id"] = linked_line_id + linked = sol.browse(cr, SUPERUSER_ID, linked_line_id, context=context) + values["name"] = _("%s\nLinked to: %s") % (values["name"], linked.product_id.name_get()[0][1]) line_id = sol.create(cr, SUPERUSER_ID, values, context=context) if add_qty: add_qty -= 1 @@ -98,16 +118,23 @@ class sale_order(osv.Model): elif add_qty != None: quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + (add_qty or 0) + # select linked product + option_ids = [line.id for line in so.order_line if line.linked_line_id.id == line_id] + # Remove zero of negative lines if quantity <= 0: - sol.unlink(cr, SUPERUSER_ID, [line_id], context=context) + sol.unlink(cr, SUPERUSER_ID, [line_id] + option_ids, context=context) else: # update line 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) - return quantity + # change quantity of linked product + if option_ids: + sol.write(cr, SUPERUSER_ID, option_ids, {'product_uom_qty': quantity}, context=context) + + return (line_id, quantity, option_ids) def _cart_accessories(self, cr, uid, ids, context=None): for order in self.browse(cr, uid, ids, context=context): diff --git a/addons/website_sale/static/src/js/website_sale.js b/addons/website_sale/static/src/js/website_sale.js index ef4d0136fb7..7543b12409a 100644 --- a/addons/website_sale/static/src/js/website_sale.js +++ b/addons/website_sale/static/src/js/website_sale.js @@ -25,6 +25,11 @@ $(document).ready(function () { location.reload(); return; } + if (data.option_ids.length) { + _.each(data.option_ids, function (line_id) { + $(".js_quantity[data-line-id="+line_id+"]").text(data.quantity); + }); + } var $q = $(".my_cart_quantity"); $q.parent().parent().removeClass("hidden", !data.quantity); $q.html(data.cart_quantity).hide().fadeIn(600); @@ -171,20 +176,6 @@ $(document).ready(function () { var quantity = parseInt($('input[name="add_qty"]:last').val() || 1, 10); var defs = []; $link.attr('disabled', 'disabled'); - $('.js_product', $form).each(function () { - var product_id = parseInt($('input.optional_product_id', this).val(),10); - var qty = parseInt($('input.js_quantity', this).val(),10); - if($('input.js_optional_same_quantity', this).val() !== '0') { - qty = quantity; - } - if (product_id && qty) { - defs.push(openerp.jsonRpc("/shop/cart/update_json", 'call', { - 'line_id': null, - 'product_id': product_id, - 'add_qty': qty, - 'display': false})); - } - }); $.when.apply($.when, defs).then(function () { if ($link.hasClass("js_goto_shop")) { $form.prepend(''); diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index b5c9c421567..8e9c897084a 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -488,8 +488,7 @@
Product not available
Total:& |
+ Total: |
|
|---|