[MERGE] forward port of branch 7.0 up to 00ec786
[odoo/odoo.git] / addons / sale_margin / sale_margin.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20
21 from openerp.osv import fields, osv
22
23 class sale_order_line(osv.osv):
24     _inherit = "sale.order.line"
25
26     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
27             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
28             lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
29         res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
30             uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
31             lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
32         if not pricelist:
33             return res
34         if context is None:
35             context = {}
36         frm_cur = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
37         to_cur = self.pool.get('product.pricelist').browse(cr, uid, [pricelist])[0].currency_id.id
38         if product:
39             product = self.pool['product.product'].browse(cr, uid, product, context=context)
40             purchase_price = product.standard_price
41             to_uom = res.get('product_uom', uom)
42             if to_uom != product.uom_id.id:
43                 purchase_price = self.pool['product.uom']._compute_price(cr, uid, product.uom_id.id, purchase_price, to_uom)
44             ctx = context.copy()
45             ctx['date'] = date_order
46             price = self.pool.get('res.currency').compute(cr, uid, frm_cur, to_cur, purchase_price, round=False, context=ctx)
47             res['value'].update({'purchase_price': price})
48         return res
49
50     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
51         res = {}
52         for line in self.browse(cr, uid, ids, context=context):
53             res[line.id] = 0
54             if line.product_id:
55                 if line.purchase_price:
56                     res[line.id] = round((line.price_unit*line.product_uos_qty*(100.0-line.discount)/100.0) -(line.purchase_price*line.product_uos_qty), 2)
57                 else:
58                     res[line.id] = round((line.price_unit*line.product_uos_qty*(100.0-line.discount)/100.0) -(line.product_id.standard_price*line.product_uos_qty), 2)
59         return res
60
61     _columns = {
62         'margin': fields.function(_product_margin, string='Margin',
63               store = True),
64         'purchase_price': fields.float('Cost Price', digits=(16,2))
65     }
66
67
68 class sale_order(osv.osv):
69     _inherit = "sale.order"
70
71     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
72         result = {}
73         for sale in self.browse(cr, uid, ids, context=context):
74             result[sale.id] = 0.0
75             for line in sale.order_line:
76                 result[sale.id] += line.margin or 0.0
77         return result
78
79     def _get_order(self, cr, uid, ids, context=None):
80         result = {}
81         for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
82             result[line.order_id.id] = True
83         return result.keys()
84
85     _columns = {
86         'margin': fields.function(_product_margin, string='Margin', help="It gives profitability by calculating the difference between the Unit Price and the cost price.", store={
87                 'sale.order.line': (_get_order, ['margin'], 20),
88                 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 20),
89                 }),
90     }
91
92
93 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: