Fixes
[odoo/odoo.git] / addons / website_sale / models / sale_order.py
1 # -*- coding: utf-8 -*-
2 import random
3
4 from openerp import SUPERUSER_ID
5 from openerp.osv import osv, orm, fields
6 from openerp.addons.web.http import request
7
8
9 class payment_transaction(orm.Model):
10     _inherit = 'payment.transaction'
11
12     _columns = {
13         # link with the sale order
14         'sale_order_id': fields.many2one('sale.order', 'Sale Order'),
15     }
16
17 class sale_order(osv.Model):
18     _inherit = "sale.order"
19
20     def _cart_qty(self, cr, uid, ids, field_name, arg, context=None):
21         res = dict()
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 [])))
24         return res
25
26     _columns = {
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.',
31         ),
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'),
35     }
36
37     def _get_errors(self, cr, uid, order, context=None):
38         return []
39
40     def _get_website_data(self, cr, uid, order, context):
41         return {
42             'partner': order.partner_id.id,
43             'order': order
44         }
45
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)]
49             if line_id:
50                 domain += [('id', '=', line_id)]
51             return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
52
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)
55
56         values = self.pool.get('sale.order.line').product_id_change(cr, SUPERUSER_ID, [],
57             pricelist=so.pricelist_id.id,
58             product=product_id,
59             partner_id=so.partner_id.id,
60             context=context
61         )['value']
62
63         if line_id:
64             line = self.pool.get('sale.order.line').browse(cr, SUPERUSER_ID, line_id, context=context)
65             values['name'] = line.name
66         else:
67             product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
68             values['name'] = product.description_sale or product.name
69
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'])]
74         return values
75
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')
79
80         quantity = 0
81         for so in self.browse(cr, uid, ids, context=context):
82             if line_id != False:
83                 line_ids = so._cart_find_product_line(product_id, line_id, context=context, **kwargs)
84                 if line_ids:
85                     line_id = line_ids[0]
86
87             # Create line if no line with product_id can be located
88             if not line_id:
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)
91                 if add_qty:
92                     add_qty -= 1
93
94             # compute new quantity
95             if set_qty:
96                 quantity = set_qty
97             elif add_qty != None:
98                 quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + (add_qty or 0)
99
100             # Remove zero of negative lines
101             if quantity <= 0:
102                 sol.unlink(cr, SUPERUSER_ID, [line_id], context=context)
103             else:
104                 # update line
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)
108
109         return {'line_id': line_id, 'quantity': quantity}
110
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)
117
118 class website(orm.Model):
119     _inherit = 'website'
120
121     _columns = {
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'),
126     }
127
128     def sale_product_domain(self, cr, uid, ids, context=None):
129         return [("sale_ok", "=", True)]
130
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')
134         sale_order = None
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
139
140             for w in self.browse(cr, uid, ids):
141                 values = {
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],
146                 }
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
151         if sale_order_id:
152             # TODO cache partner_id session
153             partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
154
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
158                 return None
159
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)
163                 if pricelist_ids:
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
168
169             pricelist_id = request.session.get('sale_order_code_pricelist_id') or partner.property_product_pricelist.id
170
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
177
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'])
182
183                 values['partner_id'] = partner.id
184                 sale_order_obj.write(cr, SUPERUSER_ID, [sale_order_id], values, context=context)
185
186                 if flag_pricelist or values.get('fiscal_position') != fiscal_position:
187                     update_pricelist = True
188
189             # update the pricelist
190             if update_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)
196
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)
200
201         return sale_order
202
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')
206         if tx_id:
207             tx_ids = transaction_obj.search(cr, uid, [('id', '=', tx_id), ('state', 'not in', ['cancel'])], context=context)
208             if tx_ids:
209                 return transaction_obj.browse(cr, uid, tx_ids[0], context=context)
210             else:
211                 request.session['sale_transaction_id'] = False
212         return False
213
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,
219         })
220
221