[IMP] website_sale: optional product have all time the same quantity of the liked product.

This commit is contained in:
Christophe Matthieu 2014-06-17 14:23:15 +02:00
parent 9867d63b5d
commit 9f77cb9d1f
5 changed files with 75 additions and 32 deletions

View File

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

View File

@ -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()

View File

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

View File

@ -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('<input type="hidden" name="goto_shop" value="1"/>');

View File

@ -488,8 +488,7 @@
<p class="css_not_available_msg bg-danger" style="position:absolute; padding: 15px;">Product not available</p>
</td>
<td>
<input type="hidden" class="js_optional_same_quantity" value="0"/>
<input type="hidden" class="js_quantity" t-attf-name="optional-quantity-#{option_inc}" value="0"/>
<input type="hidden" class="js_optional_same_quantity" t-attf-name="optional-quantity-#{option_inc}" value="0"/>
<a href="#" class="js_add"><strong>Add to Cart</strong></a>
<span class="js_remove hidden">
<span class="js_item">1 Item</span><span class="js_items hidden">5 Items</span><br/>
@ -797,11 +796,14 @@
</td>
<td t-if="line.product_id.product_tmpl_id">
<div>
<a t-attf-href="/shop/product/#{ slug(line.product_id.product_tmpl_id) }">
<a t-if="not line.linked_line_id" t-attf-href="/shop/product/#{ slug(line.product_id.product_tmpl_id) }">
<strong t-esc="line.product_id.name_get()[0][1]"/>
</a>
<t t-if="line.linked_line_id">
<strong t-esc="line.product_id.name_get()[0][1]"/>
</t>
</div>
<div class="text-muted" t-field="line.product_id.description_sale"/>
<div class="text-muted" t-field="line.name"/>
</td>
<td class="text-center" name="price">
<t t-if="abs(line.product_id.lst_price - line.price_unit) &gt; 0.2">
@ -816,8 +818,8 @@
"display_currency": "website.pricelist_id.currency_id"
}'/>
</td>
<td>
<div class="input-group">
<td class="text-center">
<div class="input-group" t-if="not line.linked_line_id">
<span class="input-group-addon">
<a t-attf-href="#" class="mb8 js_add_cart_json">
<i class="fa fa-minus"></i>
@ -833,6 +835,9 @@
</a>
</span>
</div>
<t t-if="line.linked_line_id">
<span class="js_quantity" t-att-data-line-id="line.id" t-esc="int(line.product_uom_qty)"/>
</t>
</td>
</tr>
@ -1295,7 +1300,7 @@
<table class='pull-right mb16' id="cart_total" t-if="website_sale_order">
<thead>
<tr width="100" style="border-top: 1px solid #000" id="order_total">
<th><h3>Total:&amp; </h3></th>
<th><h3>Total: </h3></th>
<th class="text-right">
<h3><span t-field="website_sale_order.amount_total" t-field-options='{
"widget": "monetary",