[MERGE] merge with latest stable
[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 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):
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)
32         if product:
33             price = self.pool.get('product.product').browse(cr, uid, product).standard_price
34             partner_pricelist = self.pool.get('res.partner').browse(cr, uid, partner_id).property_product_pricelist
35
36             if partner_pricelist:
37                 to_cur = partner_pricelist.currency_id.id
38                 frm_cur = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
39                 price = self.pool.get('res.currency').compute(cr, uid, frm_cur, to_cur, price, round=False)
40
41             res['value'].update({'purchase_price': price})
42         return res
43
44     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
45         res = {}
46         for line in self.browse(cr, uid, ids, context=context):
47             res[line.id] = 0
48             if line.product_id:
49                 if line.purchase_price:
50                     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)
51                 else:
52                     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)
53         return res
54
55     _columns = {
56         'margin': fields.function(_product_margin, method=True, string='Margin', store=True),
57         'purchase_price': fields.float('Cost Price', digits=(16,2))
58     }
59
60 sale_order_line()
61
62 class sale_order(osv.osv):
63     _inherit = "sale.order"
64
65     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
66         result = {}
67         for sale in self.browse(cr, uid, ids, context=context):
68             result[sale.id] = 0.0
69             for line in sale.order_line:
70                 result[sale.id] += line.margin or 0.0
71         return result
72
73     _columns = {
74         'margin': fields.function(_product_margin, method=True, string='Margin', store=True, help="It gives profitability by calculating the difference between the Unit Price and Cost Price."),
75     }
76
77 sale_order()
78
79 class stock_picking(osv.osv):
80     _inherit = 'stock.picking'
81
82     _columns = {
83         'invoice_ids': fields.many2many('account.invoice', 'picking_invoice_rel', 'picking_id', 'invoice_id', 'Invoices', domain=[('type', '=', 'out_invoice')]),
84     }
85
86     def action_invoice_create(self, cr, uid, ids, journal_id=False,
87             group=False, type='out_invoice', context=None):
88         # need to carify with new requirement
89         invoice_ids = []            
90         if context is None:
91             context = {}
92         picking_obj = self.pool.get('stock.picking')
93         res = super(stock_picking, self).action_invoice_create(cr, uid, ids, journal_id=journal_id,group=group, type=type, context=context)        
94         invoice_ids = res.values()
95         picking_obj.write(cr, uid, ids, {'invoice_ids': [[6, 0, invoice_ids]]})
96         return res
97
98 stock_picking()
99
100 class account_invoice_line(osv.osv):
101     _inherit = "account.invoice.line"
102     _columns = {
103         'cost_price': fields.float('Cost Price', digits=(16, 2)),
104     }
105     def write(self, cr, uid, ids, vals, context=None):
106         if vals.get('product_id', False):
107             res = self.pool.get('product.product').read(cr, uid, [vals['product_id']], ['standard_price'])
108             vals['cost_price'] = res[0]['standard_price']
109         return super(account_invoice_line, self).write(cr, uid, ids, vals, context)
110
111     def create(self, cr, uid, vals, context=None):
112         if vals.get('product_id',False):
113             res = self.pool.get('product.product').read(cr, uid, [vals['product_id']], ['standard_price'])
114             vals['cost_price'] = res[0]['standard_price']
115         return super(account_invoice_line, self).create(cr, uid, vals, context)
116
117 account_invoice_line()
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: