1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 # Copyright (C) 2010-2011 OpenERP S.A. (<http://www.openerp.com>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp.osv import fields
23 from openerp.osv import osv
28 class product_product(osv.osv):
29 _name = 'product.product'
30 _inherit = 'product.product'
33 def compute_price(self, cr, uid, ids, recursive=False, test=False, real_time_accounting = False, context=None):
35 Will return test dict when the test = False
37 testdict is used to inform the user about the changes to be made
41 bom_obj = self.pool.get('mrp.bom')
42 bom_ids = bom_obj.search(cr, uid, [('bom_id', '=', False), ('product_id','=', prod_id), ('bom_lines', '!=', False)], context=context)
45 # In recursive mode, it will first compute the prices of child boms
47 #Search the products that are components of this bom of prod_id
48 boms = bom_obj.search(cr, uid, [('bom_id', '=', bom_id)], context=context)
49 #Call compute_price on these subproducts
50 prod_set = set([x.product_id.id for x in bom_obj.browse(cr, uid, boms, context=context)])
51 res = self.compute_price(cr, uid, list(prod_set), recursive=recursive, test=test, real_time_accounting = real_time_accounting, context=context)
54 #Use calc price to calculate and put the price on the product of the BoM if necessary
55 price = self._calc_price(cr, uid, bom_obj.browse(cr, uid, bom_id, context=context), test=test, real_time_accounting = real_time_accounting, context=context)
57 testdict.update({prod_id : price})
64 def _calc_price(self, cr, uid, bom, test = False, real_time_accounting=False, context=None):
68 uom_obj = self.pool.get("product.uom")
70 for sbom in bom.bom_lines:
71 my_qty = sbom.bom_lines and 1.0 or sbom.product_qty
72 price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
75 for wline in bom.routing_id.workcenter_lines:
76 wc = wline.workcenter_id
77 cycle = wline.cycle_nbr
78 hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) * (wc.time_efficiency or 1.0)
79 price += wc.costs_cycle * cycle + wc.costs_hour * hour
80 price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id, price, bom.product_id.uom_id.id)
82 #Convert on product UoM quantities
84 price = uom_obj._compute_price(cr, uid, bom.product_uom.id, price / bom.product_qty, bom.product_id.uom_id.id)
85 product = self.pool.get("product.product").browse(cr, uid, bom.product_id.id, context=context)
87 if (product.valuation != "real_time" or not real_time_accounting):
88 self.write(cr, uid, [bom.product_id.id], {'standard_price' : price}, context=context)
90 #Call wizard function here
91 wizard_obj = self.pool.get("stock.change.standard.price")
93 ctx.update({'active_id': bom.product_id.id})
94 wiz_id = wizard_obj.create(cr, uid, {'new_price': price}, context=ctx)
95 wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
100 class product_bom(osv.osv):
104 'standard_price': fields.related('product_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: