odoo/addons/website_sale_options/models/sale_order.py

59 lines
2.8 KiB
Python
Raw Permalink Normal View History

2014-06-24 13:11:53 +00:00
# -*- coding: utf-8 -*-
from openerp import SUPERUSER_ID
from openerp.osv import osv, orm, fields
from openerp.tools.translate import _
2014-06-25 09:31:39 +00:00
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)]", ondelete='cascade'),
2014-06-25 09:31:39 +00:00
'option_line_ids': fields.one2many('sale.order.line', 'linked_line_id', string='Options Linked'),
}
2014-06-24 13:11:53 +00:00
class sale_order(osv.Model):
_inherit = "sale.order"
2014-06-25 09:31:39 +00:00
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None, **kwargs):
2014-06-24 15:37:13 +00:00
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
2014-06-25 09:31:39 +00:00
linked_line_id = kwargs.get('linked_line_id')
optional_product_ids = kwargs.get('optional_product_ids')
2014-06-24 15:37:13 +00:00
for so in self.browse(cr, uid, ids, context=context):
domain = [('id', 'in', line_ids)]
2014-06-24 13:11:53 +00:00
domain += linked_line_id and [('linked_line_id', '=', linked_line_id)] or [('linked_line_id', '=', False)]
if optional_product_ids:
domain += [('option_line_ids.product_id', '=', pid) for pid in optional_product_ids]
else:
domain += [('option_line_ids', '=', False)]
2014-06-24 15:37:13 +00:00
return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
2014-06-24 13:11:53 +00:00
2014-06-25 09:31:39 +00:00
def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None, **kwargs):
2014-06-24 13:11:53 +00:00
""" Add or set product quantity, add_qty can be negative """
2014-06-25 09:31:39 +00:00
value = super(sale_order, self)._cart_update(cr, uid, ids, product_id, line_id, add_qty, set_qty, context=context, **kwargs)
linked_line_id = kwargs.get('linked_line_id')
2014-06-24 13:11:53 +00:00
sol = self.pool.get('sale.order.line')
2014-06-25 09:31:39 +00:00
line = sol.browse(cr, SUPERUSER_ID, value.get('line_id'), context=context)
2014-06-24 13:11:53 +00:00
for so in self.browse(cr, uid, ids, context=context):
2014-06-24 15:37:13 +00:00
if linked_line_id and linked_line_id in map(int,so.order_line):
linked = sol.browse(cr, SUPERUSER_ID, linked_line_id, context=context)
2014-06-25 09:31:39 +00:00
line.write({
"name": _("%s\nOption for: %s") % (line.name, linked.product_id.name_get()[0][1]),
2014-06-24 15:37:13 +00:00
"linked_line_id": linked_line_id
})
2014-06-24 13:11:53 +00:00
# select linked product
option_ids = [l for l in so.order_line if l.linked_line_id.id == line.id]
2014-06-24 13:11:53 +00:00
# update line
for l in option_ids:
super(sale_order, self)._cart_update(cr, uid, ids, l.product_id.id, l.id, add_qty, set_qty, context=context, **kwargs)
2014-06-24 13:11:53 +00:00
value['option_ids'] = [l.id for l in option_ids]
2014-06-25 09:31:39 +00:00
return value