From 23f737a822772631e879a4d6c21c7b52e136aad7 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 21 Dec 2023 15:24:30 +0000 Subject: [PATCH] [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) --- addons/sale_margin/models/sale_order_line.py | 2 +- addons/sale_margin/tests/test_sale_margin.py | 24 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/addons/sale_margin/models/sale_order_line.py b/addons/sale_margin/models/sale_order_line.py index 18b02dd9762..e0b9a9cf0e7 100644 --- a/addons/sale_margin/models/sale_order_line.py +++ b/addons/sale_margin/models/sale_order_line.py @@ -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') diff --git a/addons/sale_margin/tests/test_sale_margin.py b/addons/sale_margin/tests/test_sale_margin.py index cb1e254bad8..ed15705c503 100644 --- a/addons/sale_margin/tests/test_sale_margin.py +++ b/addons/sale_margin/tests/test_sale_margin.py @@ -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)