This question has been flagged

I've created 2 new margin custom fields:

x_studio_total_margen_2 in sale.order.line that calculates a new margin from other costs.

x_studio_margen_2 in sale.order that is supposed to be the sum of every x_studio_total_margen_2 line added by the new formula:

for record in self:
    total = 0
    for line in record.order_line:
        total += line.x_studio_total_margen_2
    record['x_studio_margen_2'] += total

And it does the sum but it's not working well because it only makes a sum when I would like to have a correction update. For example:

If I have a product that costs 2€ and the sell price_unit is 6€ I will have a x_studio_margen_2 = 4€ but if I decide to change the price_unit to 5€due to whatever, x_studio_margen_2 is not going to be 3€ but the sum of the last x_studio_margen_2 = 4 + the new margin = 3, so the updated margin will be 7€ while it's supposed to be 3€.

Same happens when if I delete a product, x_studio_margen_2 doesn't changes, it's going to be the last number.

How can I fix it??

I know where the problem is but I don't get a solution.

Thank you!


Avatar
Discard

First avoid study

Author

I don't get if you mean that I'm not working on solving this by myself or if you mean to avoid Studio app from Odoo.