[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
This commit is contained in:
Goffin Simon 2015-04-04 11:40:51 +02:00
parent d281e7aebd
commit 08d69774b0
5 changed files with 87 additions and 11 deletions

View File

@ -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:

View File

@ -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 && (default_price-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 && (price-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;

View File

@ -683,14 +683,12 @@
<div class="text-muted" t-esc="'\n'.join(line.name.splitlines()[1:])"/>
</td>
<td class="text-center" name="price">
<t t-if="(compute_currency(line.product_id.lst_price) - line.price_unit) &gt; 1">
<del class="text-danger" style="white-space: nowrap;"
<del class="text-danger" t-att-style="'' if (compute_currency(line.product_id.lst_price) - line.price_unit) &gt; 1 else 'display: none;'" style="text-decoration: line-through; white-space: nowrap;"
t-field="line.product_id.lst_price" t-field-options='{
"widget": "monetary",
"from_currency": "line.company_id.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>&amp;nbsp;
</t>
<span t-field="line.price_unit" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"from_currency": "website_sale_order.pricelist_id.currency_id",

View File

@ -10,7 +10,7 @@ $(document).ready(function () {
openerp.jsonRpc("/shop/modal", 'call', {
'product_id': product_id,
kwargs: {
context: openerp.website.get_context()
context: _.extend({'quantity': quantity}, openerp.website.get_context())
},
}).then(function (modal) {
var $modal = $(modal);
@ -63,6 +63,34 @@ $(document).ready(function () {
$('ul.js_add_cart_variants').each(function () {
$('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
});
$modal.on("change", 'input[name="add_qty"]', function (event) {
var product_id = $($modal.find('span.oe_price[data-oe-model="product.product"]').first()).data('oe-id');
var default_price = parseInt($('.text-danger.oe_default_price > 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 && (default_price-data[product_id]>default_price/100)).css('text-decoration', 'line-through');
});
});
});
return false;
});

View File

@ -106,15 +106,13 @@
</div>
</td>
<td>
<t t-if="(compute_currency(product.lst_price) - product.price) &gt; 0.1">
<span class="text-danger" style="text-decoration: line-through; white-space: nowrap;"
<span class="text-danger oe_optional" t-att-style="'' if (compute_currency(product.lst_price) - product.price) &gt; 0.1 else 'display: none;'" style="text-decoration: line-through; white-space: nowrap;"
t-field="product.lst_price"
t-field-options='{
"widget": "monetary",
"from_currency": "website.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/><br/>
</t>
<span class="oe_price" style="white-space: nowrap;"
t-field="product.price"
t-field-options='{