This question has been flagged
1 Reply
8623 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

Avatar
Discard
Best Answer

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'

else:

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)

else:

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:

testdict.update(res)

#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

else:

return True

Avatar
Discard

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