1 # -*- coding: utf-8 -*-
4 from openerp import SUPERUSER_ID
5 from openerp.osv import osv, orm, fields
6 from openerp.addons.web.http import request
9 class payment_transaction(orm.Model):
10 _inherit = 'payment.transaction'
13 # link with the sale order
14 'sale_order_id': fields.many2one('sale.order', 'Sale Order'),
17 class sale_order(osv.Model):
18 _inherit = "sale.order"
20 def _cart_qty(self, cr, uid, ids, field_name, arg, context=None):
22 for order in self.browse(cr, uid, ids, context=context):
23 res[order.id] = int(sum(l.product_uom_qty for l in (order.website_order_line or [])))
27 'website_order_line': fields.one2many(
28 'sale.order.line', 'order_id',
29 string='Order Lines displayed on Website', readonly=True,
30 help='Order Lines to be displayed on the website. They should not be used for computation purpose.',
32 'cart_quantity': fields.function(_cart_qty, type='integer', string='Cart Quantity'),
33 'payment_acquirer_id': fields.many2one('payment.acquirer', 'Payment Acquirer', on_delete='set null'),
34 'payment_tx_id': fields.many2one('payment.transaction', 'Transaction', on_delete='set null'),
37 def _get_errors(self, cr, uid, order, context=None):
40 def _get_website_data(self, cr, uid, order, context):
42 'partner': order.partner_id.id,
46 def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None, **kwargs):
47 for so in self.browse(cr, uid, ids, context=context):
48 domain = [('order_id', '=', so.id), ('product_id', '=', product_id)]
50 domain += [('id', '=', line_id)]
51 return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
53 def _website_product_id_change(self, cr, uid, ids, order_id, product_id, line_id=None, context=None):
54 so = self.pool.get('sale.order').browse(cr, uid, order_id, context=context)
56 values = self.pool.get('sale.order.line').product_id_change(cr, SUPERUSER_ID, [],
57 pricelist=so.pricelist_id.id,
59 partner_id=so.partner_id.id,
64 line = self.pool.get('sale.order.line').browse(cr, SUPERUSER_ID, line_id, context=context)
65 values['name'] = line.name
67 product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
68 values['name'] = product.description_sale or product.name
70 values['product_id'] = product_id
71 values['order_id'] = order_id
72 if values.get('tax_id') != None:
73 values['tax_id'] = [(6, 0, values['tax_id'])]
76 def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None, **kwargs):
77 """ Add or set product quantity, add_qty can be negative """
78 sol = self.pool.get('sale.order.line')
81 for so in self.browse(cr, uid, ids, context=context):
83 line_ids = so._cart_find_product_line(product_id, line_id, context=context, **kwargs)
87 # Create line if no line with product_id can be located
89 values = self._website_product_id_change(cr, uid, ids, so.id, product_id, context=context)
90 line_id = sol.create(cr, SUPERUSER_ID, values, context=context)
94 # compute new quantity
98 quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + (add_qty or 0)
100 # Remove zero of negative lines
102 sol.unlink(cr, SUPERUSER_ID, [line_id], context=context)
105 values = self._website_product_id_change(cr, uid, ids, so.id, product_id, line_id, context=context)
106 values['product_uom_qty'] = quantity
107 sol.write(cr, SUPERUSER_ID, [line_id], values, context=context)
109 return {'line_id': line_id, 'quantity': quantity}
111 def _cart_accessories(self, cr, uid, ids, context=None):
112 for order in self.browse(cr, uid, ids, context=context):
113 s = set(j.id for l in (order.website_order_line or []) for j in (l.product_id.accessory_product_ids or []))
114 s -= set(l.product_id.id for l in order.order_line)
115 product_ids = random.sample(s, min(len(s),3))
116 return self.pool['product.product'].browse(cr, uid, product_ids, context=context)
118 class website(orm.Model):
122 'pricelist_id': fields.related('user_id','partner_id','property_product_pricelist',
123 type='many2one', relation='product.pricelist', string='Default Pricelist'),
124 'currency_id': fields.related('pricelist_id','currency_id',
125 type='many2one', relation='res.currency', string='Default Currency'),
128 def sale_product_domain(self, cr, uid, ids, context=None):
129 return [("sale_ok", "=", True)]
131 def sale_get_order(self, cr, uid, ids, force_create=False, code=None, update_pricelist=None, context=None):
132 sale_order_obj = self.pool['sale.order']
133 sale_order_id = request.session.get('sale_order_id')
135 # create so if needed
136 if not sale_order_id and (force_create or code):
137 # TODO cache partner_id session
138 partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
140 for w in self.browse(cr, uid, ids):
142 'user_id': w.user_id.id,
143 'partner_id': partner.id,
144 'pricelist_id': partner.property_product_pricelist.id,
145 'section_id': self.pool.get('ir.model.data').get_object_reference(cr, uid, 'website', 'salesteam_website_sales')[1],
147 sale_order_id = sale_order_obj.create(cr, SUPERUSER_ID, values, context=context)
148 values = sale_order_obj.onchange_partner_id(cr, SUPERUSER_ID, [], partner.id, context=context)['value']
149 sale_order_obj.write(cr, SUPERUSER_ID, [sale_order_id], values, context=context)
150 request.session['sale_order_id'] = sale_order_id
152 # TODO cache partner_id session
153 partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
155 sale_order = sale_order_obj.browse(cr, SUPERUSER_ID, sale_order_id, context=context)
156 if not sale_order.exists():
157 request.session['sale_order_id'] = None
160 # check for change of pricelist with a coupon
161 if code and code != sale_order.pricelist_id.code:
162 pricelist_ids = self.pool['product.pricelist'].search(cr, SUPERUSER_ID, [('code', '=', code)], context=context)
164 pricelist_id = pricelist_ids[0]
165 request.session['sale_order_code_pricelist_id'] = pricelist_id
166 update_pricelist = True
167 request.session['sale_order_code_pricelist_id'] = False
169 pricelist_id = request.session.get('sale_order_code_pricelist_id') or partner.property_product_pricelist.id
171 # check for change of partner_id ie after signup
172 if sale_order.partner_id.id != partner.id and request.website.partner_id.id != partner.id:
173 flag_pricelist = False
174 if pricelist_id != sale_order.pricelist_id.id:
175 flag_pricelist = True
176 fiscal_position = sale_order.fiscal_position and sale_order.fiscal_position.id or False
178 values = sale_order_obj.onchange_partner_id(cr, SUPERUSER_ID, [sale_order_id], partner.id, context=context)['value']
179 order_lines = map(int,sale_order.order_line)
180 values.update(sale_order_obj.onchange_fiscal_position(cr, SUPERUSER_ID, [],
181 values['fiscal_position'], [[6, 0, order_lines]], context=context)['value'])
183 values['partner_id'] = partner.id
184 sale_order_obj.write(cr, SUPERUSER_ID, [sale_order_id], values, context=context)
186 if flag_pricelist or values.get('fiscal_position') != fiscal_position:
187 update_pricelist = True
189 # update the pricelist
191 values = {'pricelist_id': pricelist_id}
192 values.update(sale_order.onchange_pricelist_id(pricelist_id, None)['value'])
193 sale_order.write(values)
194 for line in sale_order.order_line:
195 sale_order._cart_update(product_id=line.product_id.id, add_qty=0)
197 # update browse record
198 if (code and code != sale_order.pricelist_id.code) or sale_order.partner_id.id != partner.id:
199 sale_order = sale_order_obj.browse(cr, SUPERUSER_ID, sale_order.id, context=context)
203 def sale_get_transaction(self, cr, uid, ids, context=None):
204 transaction_obj = self.pool.get('payment.transaction')
205 tx_id = request.session.get('sale_transaction_id')
207 tx_ids = transaction_obj.search(cr, uid, [('id', '=', tx_id), ('state', 'not in', ['cancel'])], context=context)
209 return transaction_obj.browse(cr, uid, tx_ids[0], context=context)
211 request.session['sale_transaction_id'] = False
214 def sale_reset(self, cr, uid, ids, context=None):
215 request.session.update({
216 'sale_order_id': False,
217 'sale_transaction_id': False,
218 'sale_order_code_pricelist_id': False,