Odoo Help


Calculate Product Cost Price through BoM

on 9/17/15, 4:08 AM 2,732 views

I use Average pricing so that my raw materials' cost will be updated with each Purchase. But how do I make my Product's price with BoMs update according to materials/products price updates? I have over 200 000 thousand products, so this cant be done manually

E.R. Spada II
On 9/18/15, 12:37 AM

We created a cron in v8 to update standard cost with bom, it is the only way. I have no idea why this is not built in. We extended the product_extended_module, here is the function:

class product_template(osv.osv):

_name = 'product.template'

_inherit = 'product.template'

Here is the wizard:

from openerp.exceptions import except_orm

from openerp.osv import fields, osv

from openerp.tools.translate import _

class wizard_price(osv.osv):

_name = "wizard.price"

_description = "Compute price wizard"

_columns = {

'info_field': fields.text('Info', readonly=True),

'real_time_accounting': fields.boolean("Generate accounting entries when real-time"),

'recursive': fields.boolean("Change prices of child BoMs too"),


def default_get(self, cr, uid, fields, context=None):

res = super(wizard_price, self).default_get(cr, uid, fields, context=context)

product_pool = self.pool.get('product.template')

product_obj = product_pool.browse(cr, uid, context.get('active_id', False))

if context is None:

context = {}

rec_id = context and context.get('active_id', False)

assert rec_id, _('Active ID is not set in Context.')

res['info_field'] = str(product_pool.compute_price(cr, uid, [], template_ids=[product_obj.id], test=True, context=context))

return res

def compute_from_bom(self, cr, uid, ids, context=None):

assert len(ids) == 1

if context is None:

context = {}

model = context.get('active_model')

if model != 'product.template':

raise except_orm(_('Wrong model!'), _('This wizard is build for product templates, while you are currently running it from a product variant.'))

rec_id = context and context.get('active_id', False)

assert rec_id, _('Active ID is not set in Context.')

prod_obj = self.pool.get('product.template')

res = self.browse(cr, uid, ids, context=context)

prod = prod_obj.browse(cr, uid, rec_id, context=context)

prod_obj.compute_price(cr, uid, [], template_ids=[prod.id], real_time_accounting=res[0].real_time_accounting, recursive=res[0].recursive, test=False, context=context)

def compute_price_schedular(self, cr, uid, context=None):

print "-------------------------------schedular calling-------------------------------------------"

template_ids = self.search(cr,uid, [("cost_method","=","standard")])

print "template_ids----",template_ids

if template_ids:

self.compute_price(cr, uid, [],template_ids=template_ids, recursive=True, test=False, real_time_accounting = False, context=None)

return True

def compute_price(self, cr, uid, product_ids, template_ids=False, recursive=False, test=False, real_time_accounting = False, context=None):


Will return test dict when the test = False

Multiple ids at once?

testdict is used to inform the user about the changes to be made


testdict = {}

if product_ids:

ids = product_ids

model = 'product.product'


ids = template_ids

model = 'product.template'

if not ids:

return True

for prod_id in ids:

bom_obj = self.pool.get('mrp.bom')

if model == 'product.product':

bom_id = bom_obj._bom_find(cr, uid, product_id=prod_id, context=context)


bom_id = bom_obj._bom_find(cr, uid, product_tmpl_id=prod_id, context=context)

if bom_id:

# In recursive mode, it will first compute the prices of child boms

if recursive:

#Search the products that are components of this bom of prod_id

bom = bom_obj.browse(cr, uid, bom_id, context=context)

#Call compute_price on these subproducts

prod_set = set([x.product_id.id for x in bom.bom_line_ids])

res = self.compute_price(cr, uid, list(prod_set), recursive=recursive, test=test, real_time_accounting = real_time_accounting, context=context)

if test:


#Use calc price to calculate and put the price on the product of the BoM if necessary

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)

if test:

testdict.update({prod_id : price})

if test:

return testdict


return True

No one can even understand your answer. Please use "Code" style from editor.

Emipro Technologies Pvt. Ltd.
on 9/18/15, 1:37 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 9/17/15, 4:08 AM
Seen: 2732 times
Last updated: 9/18/15, 12:47 AM