1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
19 ##############################################################################
21 from osv import fields,osv
23 from tools import config
25 class sale_order_line(osv.osv):
26 _inherit = "sale.order.line"
28 def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
29 uom=False, qty_uos=0, uos=False, name='', partner_id=False,
30 lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):
31 res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
32 uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
33 lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag)
35 purchase_price = self.pool.get('product.product').browse(cr, uid, product).standard_price
36 res['value'].update({'purchase_price':purchase_price})
39 def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
41 for line in self.browse(cr, uid, ids):
44 if line.purchase_price:
45 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)
47 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)
51 'margin': fields.function(_product_margin, method=True, string='Margin', store=True),
52 'purchase_price': fields.float('Cost Price', digits=(16,2))
57 class sale_order(osv.osv):
58 _inherit = "sale.order"
60 def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
62 for sale in self.browse(cr, uid, ids, context=context):
64 for line in sale.order_line:
65 result[sale.id] += line.margin or 0.0
69 '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"),
74 class stock_picking(osv.osv):
75 _inherit = 'stock.picking'
78 'invoice_ids': fields.many2many('account.invoice', 'picking_invoice_rel', 'picking_id', 'invoice_id', 'Invoices', domain=[('type','=','out_invoice')]),
81 def create_invoice(self, cr, uid, ids, *args):
82 # need to carify with new requirement
85 picking_obj = self.pool.get('stock.picking')
86 picking_obj.write(cr, uid, ids, {'invoice_state' : '2binvoiced'})
87 res = picking_obj.action_invoice_create(cr, uid, ids, type='out_invoice', context={})
88 invoice_ids = res.values()
89 picking_obj.write(cr, uid, ids,{'invoice_ids': [[6,0,invoice_ids]]})
94 class account_invoice_line(osv.osv):
95 _inherit = "account.invoice.line"
97 'cost_price': fields.float('Cost Price', digits=(16, 2)),
99 def write(self, cr, uid, ids, vals, context={}):
100 if vals.get('product_id', False):
101 res = self.pool.get('product.product').read(cr, uid, [vals['product_id']], ['standard_price'])
102 vals['cost_price'] = res[0]['standard_price']
103 return super(account_invoice_line, self).write(cr, uid, ids, vals, context)
105 def create(self, cr, uid, vals, context={}):
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).create(cr, uid, vals, context)
111 account_invoice_line()
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: