1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2014 OpenERP S.A. (<http://www.openerp.com>).
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 openerp.osv import fields
22 from openerp.osv import osv
25 class product_template(osv.osv):
26 _name = 'product.template'
27 _inherit = 'product.template'
30 def compute_price(self, cr, uid, product_ids, template_ids=False, recursive=False, test=False, real_time_accounting = False, context=None):
32 Will return test dict when the test = False
34 testdict is used to inform the user about the changes to be made
39 model = 'product.product'
42 model = 'product.template'
44 bom_obj = self.pool.get('mrp.bom')
45 if model == 'product.product':
46 bom_id = bom_obj._bom_find(cr, uid, product_id=prod_id, context=context)
48 bom_id = bom_obj._bom_find(cr, uid, product_tmpl_id=prod_id, context=context)
50 # In recursive mode, it will first compute the prices of child boms
52 #Search the products that are components of this bom of prod_id
53 bom = bom_obj.browse(cr, uid, bom_id, context=context)
55 #Call compute_price on these subproducts
56 prod_set = set([x.product_id.id for x in bom.bom_line_ids])
57 res = self.compute_price(cr, uid, list(prod_set), recursive=recursive, test=test, real_time_accounting = real_time_accounting, context=context)
60 #Use calc price to calculate and put the price on the product of the BoM if necessary
61 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)
63 testdict.update({prod_id : price})
70 def _calc_price(self, cr, uid, bom, test = False, real_time_accounting=False, context=None):
74 uom_obj = self.pool.get("product.uom")
75 tmpl_obj = self.pool.get('product.template')
76 for sbom in bom.bom_line_ids:
77 my_qty = sbom.product_qty
78 if not sbom.attribute_value_ids:
79 # No attribute_value_ids means the bom line is not variant specific
80 price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
83 for wline in bom.routing_id.workcenter_lines:
84 wc = wline.workcenter_id
85 cycle = wline.cycle_nbr
86 hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) * (wc.time_efficiency or 1.0)
87 price += wc.costs_cycle * cycle + wc.costs_hour * hour
88 price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id, price, bom.product_id.uom_id.id)
90 #Convert on product UoM quantities
92 price = uom_obj._compute_price(cr, uid, bom.product_uom.id, price / bom.product_qty, bom.product_id.uom_id.id)
94 product = tmpl_obj.browse(cr, uid, bom.product_tmpl_id.id, context=context)
96 if (product.valuation != "real_time" or not real_time_accounting):
97 tmpl_obj.write(cr, uid, [product.id], {'standard_price' : price}, context=context)
99 #Call wizard function here
100 wizard_obj = self.pool.get("stock.change.standard.price")
102 ctx.update({'active_id': product.id, 'active_model': 'product.template'})
103 wiz_id = wizard_obj.create(cr, uid, {'new_price': price}, context=ctx)
104 wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
108 class product_bom(osv.osv):
112 'standard_price': fields.related('product_tmpl_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: