[FIX]problem not impact precision of UoM in sale order
[odoo/odoo.git] / addons / sale_layout / sale_layout.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24 import decimal_precision as dp
25
26 class sale_order_line(osv.osv):
27
28     def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
29         res = {}
30         line_ids = []
31         for id in ids:
32             res[id] = 0.0
33         for line in self.browse(cr, uid, ids, context=context):
34             if line.layout_type == 'article':
35                 line_ids.append(line.id)
36         if line_ids:
37             res_article = super(sale_order_line, self)._amount_line(cr, uid, line_ids, field_name, arg, context)
38             res.update(res_article)
39         return res
40
41     def invoice_line_create(self, cr, uid, ids, context=None):
42         new_ids = []
43         list_seq = []
44         for line in self.browse(cr, uid, ids, context=context):
45             if line.layout_type == 'article':
46                 new_ids.append(line.id)
47                 list_seq.append(line.sequence)
48         invoice_line_ids = super(sale_order_line, self).invoice_line_create(cr, uid, new_ids, context)
49         pool_inv_line = self.pool.get('account.invoice.line')
50         seq = 0
51         for obj_inv_line in pool_inv_line.browse(cr, uid, invoice_line_ids, context=context):
52             pool_inv_line.write(cr, uid, [obj_inv_line.id], {'sequence': list_seq[seq]}, context=context)
53             seq += 1
54         return invoice_line_ids
55
56     def onchange_sale_order_line_view(self, cr, uid, id, type, context={}, *args):
57         temp = {}
58         temp['value'] = {}
59         if (not type):
60             return {}
61         if type != 'article':
62             temp = {
63                 'value': {
64                 'product_id': False,
65                 'uos_id': False,
66                 'account_id': False,
67                 'price_unit': 0.0,
68                 'price_subtotal': 0.0,
69                 'quantity': 0,
70                 'discount': 0.0,
71                 'invoice_line_tax_id': False,
72                 'account_analytic_id': False,
73                 'product_uom_qty': 0.0,
74                 },
75             }
76             if type == 'line':
77                 temp['value']['name'] = ' '
78             if type == 'break':
79                 temp['value']['name'] = ' '
80             if type == 'subtotal':
81                 temp['value']['name'] = 'Sub Total'
82             return temp
83         return {}
84
85     def create(self, cr, user, vals, context=None):
86         if vals.has_key('layout_type'):
87             if vals['layout_type'] == 'line':
88                 vals['name'] = ' '
89             if vals['layout_type'] == 'break':
90                 vals['name'] = ' '
91             if vals['layout_type'] != 'article':
92                 vals['product_uom_qty']= 0
93         return super(sale_order_line, self).create(cr, user, vals, context)
94
95     def write(self, cr, user, ids, vals, context=None):
96         if vals.has_key('layout_type'):
97             if vals['layout_type'] == 'line':
98                 vals['name'] = ' '
99             if vals['layout_type'] == 'break':
100                 vals['name'] = ' '
101         return super(sale_order_line, self).write(cr, user, ids, vals, context)
102
103     def copy(self, cr, uid, id, default=None, context=None):
104         if default is None:
105             default = {}
106         default['layout_type'] = self.browse(cr, uid, id, context=context).layout_type
107         return super(sale_order_line, self).copy(cr, uid, id, default, context)
108
109     _order = "order_id, sequence asc"
110     _description = "Sales Order line"
111     _inherit = "sale.order.line"
112     _columns = {
113         'layout_type': fields.selection([
114                 ('article', 'Product'),
115                 ('title', 'Title'),
116                 ('text', 'Note'),
117                 ('subtotal', 'Sub Total'),
118                 ('line', 'Separator Line'),
119                 ('break', 'Page Break'),]
120             ,'Line Type', select=True, required=True),
121         'sequence': fields.integer('Line Sequence', select=True),
122         'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Sale Price'), readonly=True, states={'draft': [('readonly', False)]}),
123         'product_uom_qty': fields.float('Quantity (UoM)', digits_compute= dp.get_precision('Product UoM')),
124         'product_uom': fields.many2one('product.uom', 'Product UoM'),
125         # Override the field to call the overridden _amount_line function
126         'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal', digits_compute= dp.get_precision('Sale Price')),
127     }
128
129     _defaults = {
130         'layout_type': 'article',
131     }
132
133 sale_order_line()
134
135 class one2many_mod2(fields.one2many):
136     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
137         if not values:
138             values = {}
139         res = {}
140         for id in ids:
141             res[id] = []
142         ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id, 'in', ids), ('layout_type', '=', 'article')], limit=self._limit)
143         for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
144             res[r[self._fields_id]].append( r['id'] )
145         return res
146
147
148 class sale_order(osv.osv):
149
150     def copy(self, cr, uid, id, default=None, context=None):
151         if default is None:
152             default = {}
153         default['order_line'] = False
154         return super(sale_order, self).copy(cr, uid, id, default, context)
155
156     _inherit = "sale.order"
157     _columns = {
158         'abstract_line_ids': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)]}),
159         'order_line': one2many_mod2('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)]}),
160     }
161
162 sale_order()
163
164 class stock_picking(osv.osv):
165     _inherit = "stock.picking"
166
167     def _prepare_invoice_line(self, cr, uid, group, picking, move_line, invoice_id, invoice_vals, context=None):
168         vals = super(stock_picking, self)._prepare_invoice_line(cr, uid, group, picking, move_line, invoice_id, invoice_vals, context=context)
169
170         if move_line.sale_line_id:
171             vals['sequence'] = move_line.sale_line_id.sequence
172
173         return vals
174
175 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: