Skip to Content
Menu
This question has been flagged
2 Replies
5209 Views

Our product cost changes rapidly.  I want to update the cost of a manufactured product automatically by running the Compute Price from BOM server action, when the cost of a component of its BOM is changed. 

Avatar
Discard

which Odoo version are you on?

Author

I am using version 15

For others reading this isn't always necessary as Odoo can update the cost automatically based on the costs of purchased components that are used and the time they are used. Remember that the BoM is the recipe, not what is used. If people stick to the recipe all the time and always use the newly purchased components this strategy will work fine. If you sometimes use components in stock and/or ever use more or less components than the BoM plans for, then this option won't be better than having Odoo do it based on a FIFO or AVERAGE cost for your products (components and finished goods). With this approach, the things you make in March using products you paid for in January will be costed as if you made them using products you paid for in March.

Author

Thank you for clarifying, Ray! Typical manufacturing businesses should utilize the Odoo built-in features whenever possible. My client is an EOM manufacturer for highly customized products. They don't stock inventory. When a client wants to place an order, they always get the up-to-date cost from vendors first. Then, they will add their manufacturing cost and margin on top in their price quotation to the client. Therefore, they cannot use the built-in features to compute cost from previous POs. Especially under the current US market situation where raw materials cost increases rapidly. Therefore, they need this customization to ensure that their quotation will not go below desired margin.

Author Best Answer

Thank you, Nomad_King!  I have found the solution based on your reply.  Here is the Automated Action Python Code for the On Update trigger of the Product model:  

# Load the related BOM records for the current product
rec_boms = env['mrp.bom.line'].search([('product_id', '=', record.id)]).mapped('bom_id')

for bom in rec_boms:
    # Load the referencing product of the BOM
    for ref_prod in bom.product_tmpl_id:
        prod = env['product.product'].search([('id', '=', ref_prod.id )])

        # Call the Update Price from BOM method to update the product cost
        prod.button_bom_cost()

Avatar
Discard

Hi I got error like this.

Error:
Odoo Server Error

Traceback (most recent call last):
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
result = request.dispatch()
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 683, in dispatch
result = self._call_function(**self.params)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 359, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 347, in checked_call
result = self.endpoint(*a, **kw)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 912, in __call__
return self.method(*args, **kw)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 531, in response_wrap
response = f(*args, **kw)
File "/home/odoo/src/odoo/14.0/addons/web/controllers/main.py", line 1398, in call_button
action = self._call_kw(model, method, args, kwargs)
File "/home/odoo/src/odoo/14.0/addons/web/controllers/main.py", line 1386, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/odoo/src/odoo/14.0/odoo/api.py", line 399, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/home/odoo/src/odoo/14.0/odoo/api.py", line 386, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_cron.py", line 83, in method_direct_trigger
cron.with_user(cron.user_id).with_context(lastcall=cron.lastcall).ir_actions_server_id.run()
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_actions.py", line 632, in run
res = runner(run_self, eval_context=eval_context)
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_actions.py", line 501, in _run_action_code_multi
safe_eval(self.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/home/odoo/src/odoo/14.0/odoo/tools/safe_eval.py", line 346, in safe_eval
raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr))
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 639, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 315, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: <class 'AttributeError'>: "'NoneType' object has no attribute 'id'" while evaluating
"rec_boms = env['mrp.bom.line'].search([('product_id', '=', record.id)]).mapped('bom_id')\n\nfor bom in rec_boms:\n # Load the referencing product of the BOM\n for ref_prod in bom.product_tmpl_id:\n prod = env['product.product'].search([('id', '=', ref_prod.id )])\n\n # Call the Update Price from BOM method to update the product cost\n prod.button_bom_cost()"

Best Answer

You can create a scheduled action. This worked for me in odoo v14.

# Load all BOM records for all the products
rec_boms = self.env['mrp.bom'].search([('product_tmpl_id', '!=', False)]).mapped('product_tmpl_id.id')
    
    # Load the referencing product of the BOM
    for ref_prod in rec_boms:
        prod = self.env['product.template'].search([('id', '=', ref_prod )])
        #Update Price from BOM method to update the product cost
        prod.button_bom_cost()
Avatar
Discard
Related Posts Replies Views Activity
1
Nov 23
1589
1
Nov 22
2200
1
Aug 22
2340
2
Jan 22
5254
1
Jul 20
3198