[FIX] sale_margin: update purchase_price on copy

When we copy a sales order we were taking margins from the past which
have a high probability of not being valid anymore.

Now, once we copy a sales order, no past value is copied and the
purchase price is computed as new so we get the current margin values.

TT46721

closes odoo/odoo#148244

X-original-commit: 6da8aec93f1e66feaea16b12e245a229001cec21
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
David
2024-01-05 10:34:46 +00:00
committed by Victor Feyens
parent 6d99cf3eb6
commit 23f737a822
2 changed files with 25 additions and 1 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ class SaleOrderLine(models.Model):
"Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True)
purchase_price = fields.Float(
string="Cost", compute="_compute_purchase_price",
digits='Product Price', store=True, readonly=False, precompute=True,
digits='Product Price', store=True, readonly=False, copy=False, precompute=True,
groups="base.group_user")
@api.depends('product_id', 'company_id', 'currency_id', 'product_uom')
@@ -100,3 +100,27 @@ class TestSaleMargin(SaleCommon):
# Verify that margin field gets bind with the value.
self.assertEqual(order.margin, 100.00, "Sales order profit should be 100.00")
self.assertEqual(order.margin_percent, 0.4, "Sales order margin should be 40%")
def test_sale_margin_order_copy(self):
"""When we copy a sales order, its margins should be update to meet the current costs"""
order = self.empty_order
self.pricelist.currency_id = self.env.company.currency_id
# We buy at a specific price today and our margins go according to that
self.product.standard_price = 500.0
order.order_line = [
Command.create({
'price_unit': 1000.0,
'product_uom_qty': 10.0,
'product_id': self.product.id,
}),
]
self.assertAlmostEqual(500.0, order.order_line.purchase_price)
self.assertAlmostEqual(5000.0, order.order_line.margin)
self.assertAlmostEqual(.5, order.order_line.margin_percent)
# Later on, the cost of our product changes and so will the following sale
# margins do.
self.product.standard_price = 750.0
following_sale = order.copy()
self.assertAlmostEqual(750.0, following_sale.order_line.purchase_price)
self.assertAlmostEqual(2500.0, following_sale.order_line.margin)
self.assertAlmostEqual(.25, following_sale.order_line.margin_percent)