Reputation: 1756
I have created a custom field in the model mrp.BOM structure as below to compute the total cost of BOM for a product:-
Field Name:- x_bom_total
Field Label:- Total BOM
Field Type:- Float
ReadOnly:- True
Dependency:- bom_line_ids
Dependent Field name 'bom_line_ids' is the field which display all the materials used in the product. It refernces to the model 'mrp.bom.line' in a one2many relationship model. So now in the computation part how to calculate the Total BOM for the product something like this:-
for record in self:
for each_object in record.bom_line_ids:
record['x_bom_total'] += record.bom_line_ids.qty * record.bom_line_ids.list_price
I am using odoo v11. Does anyone have an idea?
Upvotes: 1
Views: 1016
Reputation: 14801
You were on the right way, but you should consider child BOMs, too.
First your nearly correct approach without child BOMs:
for record in self:
total = 0.0
for line in record.bom_line_ids:
total += line.product_qty * line.product_id.list_price
record['x_bom_total'] = total
And now with consideration of child BOMs. You're obviously using Odoo Studio App, but i don't know if you can define methods on computed fields, but you can give it a try. A recursive function would be really nice here:
def get_bom_total(lines)
total = 0.0
for line in lines:
if line.child_bom_id:
total += get_bom_total(line.child_bom_ids) # recursive call
else:
total += line.product_qty * line.product_id.list_price
return total
for record in self:
record['x_bom_total'] = get_bom_total(record.bom_line_ids)
Upvotes: 1