[REF]: Replace the condition 'if not context' with 'if context is None'.
[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             purchase_price = self.pool.get('product.product').browse(cr, uid, product).standard_price
34             res['value'].update({'purchase_price': purchase_price})
35         return res
36
37     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
38         res = {}
39         for line in self.browse(cr, uid, ids, context=context):
40             res[line.id] = 0
41             if line.product_id:
42                 if line.purchase_price:
43                     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)
44                 else:
45                     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)
46         return res
47
48     _columns = {
49         'margin': fields.function(_product_margin, method=True, string='Margin', store=True),
50         'purchase_price': fields.float('Cost Price', digits=(16,2))
51     }
52
53 sale_order_line()
54
55 class sale_order(osv.osv):
56     _inherit = "sale.order"
57
58     def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
59         result = {}
60         for sale in self.browse(cr, uid, ids, context=context):
61             result[sale.id] = 0.0
62             for line in sale.order_line:
63                 result[sale.id] += line.margin or 0.0
64         return result
65
66     _columns = {
67         '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"),
68     }
69
70 sale_order()
71
72 class stock_picking(osv.osv):
73     _inherit = 'stock.picking'
74
75     _columns = {
76         'invoice_ids': fields.many2many('account.invoice', 'picking_invoice_rel', 'picking_id', 'invoice_id', 'Invoices', domain=[('type', '=', 'out_invoice')]),
77     }
78
79     def create_invoice(self, cr, uid, ids, *args):
80         # need to carify with new requirement
81         invoice_ids = []
82         margin_deduce = 0.0
83         picking_obj = self.pool.get('stock.picking')
84         picking_obj.write(cr, uid, ids, {'invoice_state': '2binvoiced'})
85         res = picking_obj.action_invoice_create(cr, uid, ids, type='out_invoice', context={})
86         invoice_ids = res.values()
87         picking_obj.write(cr, uid, ids, {'invoice_ids': [[6, 0, invoice_ids]]})
88         return True
89
90 stock_picking()
91
92 class account_invoice_line(osv.osv):
93     _inherit = "account.invoice.line"
94     _columns = {
95         'cost_price': fields.float('Cost Price', digits=(16, 2)),
96     }
97     def write(self, cr, uid, ids, vals, context=None):
98         if vals.get('product_id', False):
99             res = self.pool.get('product.product').read(cr, uid, [vals['product_id']], ['standard_price'])
100             vals['cost_price'] = res[0]['standard_price']
101         return super(account_invoice_line, self).write(cr, uid, ids, vals, context)
102
103     def create(self, cr, uid, vals, context=None):
104         if vals.get('product_id',False):
105             res = self.pool.get('product.product').read(cr, uid, [vals['product_id']], ['standard_price'])
106             vals['cost_price'] = res[0]['standard_price']
107         return super(account_invoice_line, self).create(cr, uid, vals, context)
108
109 account_invoice_line()
110
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: