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)