From 08d69774b045f33efe94a3c0b2f41b1f059e314f Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Sat, 4 Apr 2015 11:40:51 +0200 Subject: [PATCH] [FIX] website_sale: Pricelist witn min qty Each time a product is added in the cart, the qty of this product must be checked to adjust the price if there is a rule (with min qty) in the partner's pricelist. opw:630049 --- addons/website_sale/controllers/main.py | 9 ++++ .../static/src/js/website_sale.js | 51 +++++++++++++++++-- addons/website_sale/views/templates.xml | 4 +- .../static/src/js/website_sale.js | 30 ++++++++++- .../website_sale_options/views/templates.xml | 4 +- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index cb59aecde71..47a1eae07ee 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -933,4 +933,13 @@ class website_sale(http.Controller): ret['lines'] = self.order_lines_2_google_api(order.order_line) return ret + @http.route(['/shop/get_unit_price'], type='json', auth="public", methods=['POST'], website=True) + def get_unit_price(self, product_ids, add_qty, **kw): + cr, uid, context, pool = request.cr, request.uid, request.context, request.registry + products = pool['product.product'].browse(cr, uid, product_ids, context=context) + partner = pool['res.users'].browse(cr, uid, uid, context=context).partner_id + pricelist_id = request.session.get('sale_order_code_pricelist_id') or partner.property_product_pricelist.id + prices = pool['product.pricelist'].price_rule_get_multi(cr, uid, [], [(product, add_qty, partner) for product in products], context=context) + return {product_id: prices[product_id][pricelist_id][0] for product_id in product_ids} + # vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/website_sale/static/src/js/website_sale.js b/addons/website_sale/static/src/js/website_sale.js index d700ef25cec..fb6b76c83a8 100644 --- a/addons/website_sale/static/src/js/website_sale.js +++ b/addons/website_sale/static/src/js/website_sale.js @@ -19,17 +19,56 @@ $('.oe_website_sale').each(function () { }); }); + $(oe_website_sale).on("change", 'input[name="add_qty"]', function (event) { + product_ids = []; + var product_dom = $("ul.js_add_cart_variants[data-attribute_value_ids]").first(); + product_dom.data("attribute_value_ids").forEach(function(entry) { + product_ids.push(entry[0]);}); + var qty = $(event.target).closest('form').find('input[name="add_qty"]').val(); + + openerp.jsonRpc("/shop/get_unit_price", 'call', {'product_ids': product_ids,'add_qty': parseInt(qty)}) + .then(function (data) { + var current = product_dom.data("attribute_value_ids"); + for(var j=0; j < current.length; j++){ + current[j][2] = data[current[j][0]]; + } + product_dom.attr("data-attribute_value_ids", JSON.stringify(current)).trigger("change"); + }); + }); + // change for css $(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) { $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled"); }); - $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () { + $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function (event) { var $input = $(this); var value = parseInt($input.val(), 10); + var $dom = $(event.target).closest('tr'); + var default_price = parseFloat($dom.find('.text-danger > span.oe_currency_value').text()); + var $dom_optional = $dom.nextUntil(':not(.optional_product.info)'); var line_id = parseInt($input.data('line-id'),10); + var product_id = parseInt($input.data('product-id'),10); + var product_ids = [product_id]; + $dom_optional.each(function(){ + product_ids.push($(this).find('span[data-oe-model="product.product"]').data('oe-id')); + }); if (isNaN(value)) value = 0; - openerp.jsonRpc("/shop/cart/update_json", 'call', { + openerp.jsonRpc("/shop/get_unit_price", 'call', { + 'product_ids': product_ids, + 'add_qty': value}) + .then(function (res) { + //basic case + $dom.find('span.oe_currency_value').last().text(res[product_id].toFixed(2)); + $dom.find('.text-danger').toggle(res[product_id] default_price/100)); + //optional case + $dom_optional.each(function(){ + var id = $(this).find('span[data-oe-model="product.product"]').data('oe-id'); + var price = parseFloat($(this).find(".text-danger > span.oe_currency_value").text()); + $(this).find("span.oe_currency_value").last().text(res[id].toFixed(2)); + $(this).find('.text-danger').toggle(res[id]price/100)); + }); + openerp.jsonRpc("/shop/cart/update_json", 'call', { 'line_id': line_id, 'product_id': parseInt($input.data('product-id'),10), 'set_qty': value}) @@ -46,6 +85,7 @@ $('.oe_website_sale').each(function () { $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity); $("#cart_total").replaceWith(data['website_sale.total']); }); + }); }); // hack to add and rome from cart with json @@ -101,12 +141,13 @@ $('.oe_website_sale').each(function () { $img.attr("src", "/website/image/product.product/" + $(this).val() + "/image"); }); - $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) { - var $ul = $(this).parents('ul.js_add_cart_variants:first'); + $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change, ul[data-attribute_value_ids]', function (ev) { + var $ul = $(ev.target).closest('ul.js_add_cart_variants'); var $parent = $ul.closest('.js_product'); var $product_id = $parent.find('input.product_id').first(); var $price = $parent.find(".oe_price:first .oe_currency_value"); var $default_price = $parent.find(".oe_default_price:first .oe_currency_value"); + var $optional_price = $parent.find(".oe_optional:first .oe_currency_value"); var variant_ids = $ul.data("attribute_value_ids"); var values = []; $parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () { @@ -122,8 +163,10 @@ $('.oe_website_sale').each(function () { $default_price.html(price_to_str(variant_ids[k][3])); if (variant_ids[k][3]-variant_ids[k][2]>0.2) { $default_price.closest('.oe_website_sale').addClass("discount"); + $optional_price.closest('.oe_optional').show().css('text-decoration', 'line-through'); } else { $default_price.closest('.oe_website_sale').removeClass("discount"); + $optional_price.closest('.oe_optional').hide(); } product_id = variant_ids[k][0]; break; diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index 7a185aeba81..51878f60d3b 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -683,14 +683,12 @@
- - &nbsp; - span.oe_currency_value').text()); + var $dom = $(event.target).closest('tr'); + var qty = $dom.find('input[name="add_qty"]').val(); + var product_ids = [product_id]; + var $products_dom = []; + $modal.find("ul.js_add_cart_variants[data-attribute_value_ids]").each(function(){ + var $el = $(this); + $products_dom.push($el); + _.each($el.data("attribute_value_ids"), function (values) { + product_ids.push(values[0]); + }); + }); + openerp.jsonRpc("/shop/get_unit_price", 'call', {'product_ids': product_ids, 'add_qty': parseInt(qty)}) + .then(function (data) { + for(var i=0; i < $products_dom.length; i++) { + current = $products_dom[i].data("attribute_value_ids"); + for(var j=0; j < current.length; j++){ + current[j][2] = data[current[j][0]]; + } + $products_dom[i].attr("data-attribute_value_ids", JSON.stringify(current)).trigger("change"); + } + $dom.find(".oe_price .oe_currency_value").text(data[product_id].toFixed(2)); + $dom.find('.text-danger.oe_default_price').toggle(data[product_id]default_price/100)).css('text-decoration', 'line-through'); + }); + }); }); return false; }); diff --git a/addons/website_sale_options/views/templates.xml b/addons/website_sale_options/views/templates.xml index df4e47f7d63..d91df2e6c10 100644 --- a/addons/website_sale_options/views/templates.xml +++ b/addons/website_sale_options/views/templates.xml @@ -106,15 +106,13 @@
- -
-