[IMP] website_sale_options: clean code; add ondelete cascade for optional product on sale order
This commit is contained in:
parent
412a7593be
commit
216456127d
|
@ -7,26 +7,15 @@ 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, linked_line_id=None, optional_product_ids=None, context=None):
|
||||
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None, **kwargs):
|
||||
line_ids = super(sale_order, self)._cart_find_product_line(cr, uid, ids, product_id, line_id, context=context)
|
||||
if line_id:
|
||||
return line_ids
|
||||
for so in self.browse(cr, uid, ids, context=context):
|
||||
|
||||
domain = [('order_id', '=', so.id), ('product_id', '=', product_id)]
|
||||
if line_id:
|
||||
domain += [('id', '=', line_id)]
|
||||
elif context.get("event_ticket_id"):
|
||||
domain = [('id', 'in', line_ids)]
|
||||
if 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]
|
||||
return order_line_id
|
||||
return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
|
||||
|
||||
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=line_id, context=None)
|
||||
|
|
|
@ -278,17 +278,10 @@ class website_sale(http.Controller):
|
|||
return request.website.render("website_sale.cart", values)
|
||||
|
||||
@http.route(['/shop/cart/update'], type='http', auth="public", methods=['POST'], website=True)
|
||||
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
|
||||
prod_obj = pool['product.product']
|
||||
|
||||
order = request.website.sale_get_order(force_create=1)
|
||||
order._cart_update(product_id=int(product_id), add_qty=int(add_qty), set_qty=int(set_qty))
|
||||
|
||||
if goto_shop:
|
||||
return request.redirect("/shop/product/%s" % slug(prod_obj.browse(cr, uid, product_id).product_tmpl_id))
|
||||
else:
|
||||
return request.redirect("/shop/cart")
|
||||
def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):
|
||||
cr, uid, context = request.cr, request.uid, request.context
|
||||
request.website.sale_get_order(force_create=1)._cart_update(product_id=int(product_id), add_qty=add_qty, set_qty=set_qty)
|
||||
return request.redirect("/shop/cart")
|
||||
|
||||
@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):
|
||||
|
|
|
@ -53,22 +53,11 @@ class website_sale_options(website_sale):
|
|||
@http.route(['/shop/modal'], type='json', auth="public", methods=['POST'], website=True)
|
||||
def modal(self, product_id, **kw):
|
||||
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
||||
currency_obj = pool['res.currency']
|
||||
if not context.get('pricelist'):
|
||||
context['pricelist'] = int(self.get_pricelist())
|
||||
|
||||
product = pool['product.product'].browse(cr, uid, int(product_id), context=context)
|
||||
|
||||
attribute_value_ids = []
|
||||
if request.website.pricelist_id.id != context['pricelist']:
|
||||
website_currency_id = request.website.currency_id.id
|
||||
currency_id = self.get_pricelist().currency_id.id
|
||||
for p in product.product_variant_ids:
|
||||
price = currency_obj.compute(cr, uid, website_currency_id, currency_id, p.lst_price)
|
||||
attribute_value_ids.append([p.id, map(int, p.attribute_value_ids), p.price, price])
|
||||
else:
|
||||
attribute_value_ids = [[p.id, map(int, p.attribute_value_ids), p.price, p.lst_price] for p in product.product_variant_ids]
|
||||
|
||||
return request.website._render("website_sale_options.modal", {
|
||||
'product': product,
|
||||
'get_attribute_value_ids': self.get_attribute_value_ids,
|
||||
|
|
|
@ -8,7 +8,7 @@ from openerp.tools.translate import _
|
|||
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)]"),
|
||||
'linked_line_id': fields.many2one('sale.order.line', 'Linked Order Line', domain="[('order_id','!=',order_id)]", ondelete='cascade'),
|
||||
'option_line_ids': fields.one2many('sale.order.line', 'linked_line_id', string='Options Linked'),
|
||||
}
|
||||
|
||||
|
@ -17,20 +17,17 @@ class sale_order(osv.Model):
|
|||
|
||||
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None, **kwargs):
|
||||
line_ids = super(sale_order, self)._cart_find_product_line(cr, uid, ids, product_id, line_id, context=context)
|
||||
if line_id:
|
||||
return line_ids
|
||||
linked_line_id = kwargs.get('linked_line_id')
|
||||
optional_product_ids = kwargs.get('optional_product_ids')
|
||||
|
||||
for so in self.browse(cr, uid, ids, context=context):
|
||||
domain = [('order_id', '=', so.id), ('product_id', '=', product_id), ('id', 'in', line_ids)]
|
||||
if line_id:
|
||||
domain += [('id', '=', line_id)]
|
||||
domain = [('id', 'in', line_ids)]
|
||||
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)]
|
||||
|
||||
if optional_product_ids:
|
||||
domain += [('option_line_ids.product_id', '=', pid) for pid in optional_product_ids]
|
||||
else:
|
||||
domain += [('option_line_ids', '=', False)]
|
||||
return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
|
||||
|
||||
def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None, **kwargs):
|
||||
|
@ -54,14 +51,10 @@ class sale_order(osv.Model):
|
|||
option_ids = [l.id for l in so.order_line if l.linked_line_id.id == line.id]
|
||||
|
||||
if option_ids:
|
||||
# Remove zero of negative lines
|
||||
if value.get('quantity') <= 0:
|
||||
sol.unlink(cr, SUPERUSER_ID, option_ids, context=context)
|
||||
else:
|
||||
# update line
|
||||
sol.write(cr, SUPERUSER_ID, option_ids, {
|
||||
'product_uom_qty': value.get('quantity')
|
||||
}, context=context)
|
||||
# update line
|
||||
sol.write(cr, SUPERUSER_ID, option_ids, {
|
||||
'product_uom_qty': value.get('quantity')
|
||||
}, context=context)
|
||||
|
||||
value['option_ids'] = option_ids
|
||||
return value
|
||||
|
|
Loading…
Reference in New Issue