From 4000b18fe488f7bb84236f2995900bfae9472c91 Mon Sep 17 00:00:00 2001
From: Walid <waha@odoo.com>
Date: Wed, 2 Aug 2023 16:43:30 +0000
Subject: [PATCH] [FIX] mrp_account: AVCO product valuation with component cost
 0

Steps to reproduce:
- Create a manufactured product P with AVCO valuation
- On the bom of the previous product add a component C with unit cost 5$
- Manufacture product P (Unit cost correct so far 5$)
- Change component cost to 0 and manufacture again
- Unit cost of P is still 5$ were it should be 2.5$

Bug:
since the move unit price is 0 the standard price of the product P is
used insted for the valuation of the newly created qty

Fix:
force unit price to 0 in the case of manufacturing

opw-3374462

closes odoo/odoo#130592

Signed-off-by: William Henrotin (whe) <whe@odoo.com>
---
 addons/mrp_account/models/stock_move.py           |  4 ++++
 addons/mrp_account/tests/test_valuation_layers.py | 13 +++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/addons/mrp_account/models/stock_move.py b/addons/mrp_account/models/stock_move.py
index 86c19e23894d..e65f0a407d91 100644
--- a/addons/mrp_account/models/stock_move.py
+++ b/addons/mrp_account/models/stock_move.py
@@ -27,3 +27,7 @@ class StockMove(models.Model):
         res = super(StockMove, self)._filter_anglo_saxon_moves(product)
         res += self.filtered(lambda m: m.bom_line_id.bom_id.product_tmpl_id.id == product.product_tmpl_id.id)
         return res
+
+    def _should_force_price_unit(self):
+        self.ensure_one()
+        return self.picking_type_id.code == 'mrp_operation' or super()._should_force_price_unit()
diff --git a/addons/mrp_account/tests/test_valuation_layers.py b/addons/mrp_account/tests/test_valuation_layers.py
index b0827494ed10..d0b859fe91eb 100644
--- a/addons/mrp_account/tests/test_valuation_layers.py
+++ b/addons/mrp_account/tests/test_valuation_layers.py
@@ -198,9 +198,22 @@ class TestMrpValuationStandard(TestMrpValuationCommon):
         self.assertEqual(self.product1.value_svl, 8.8 * 2)
         self.assertEqual(self.component.quantity_svl, 0)
         self.assertEqual(self.product1.quantity_svl, 2)
+        self.assertEqual(self.product1.standard_price, 8.8)
+
         self._make_out_move(self.product1, 1)
         self.assertEqual(self.product1.value_svl, 8.8)
 
+        # Update component price
+        self.component.standard_price = 0
+
+        self._make_in_move(self.component, 3)
+        mo = self._make_mo(self.bom, 3)
+        self._produce(mo)
+        mo.button_mark_done()
+        self.assertEqual(self.product1.value_svl, 8.8)
+        self.assertEqual(self.product1.quantity_svl, 4)
+        self.assertEqual(self.product1.standard_price, 2.2)
+
     def test_std_std_1(self):
         self.component.product_tmpl_id.categ_id.property_cost_method = 'standard'
         self.product1.product_tmpl_id.categ_id.property_cost_method = 'standard'
-- 
GitLab