diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 862f7c99fc0..c356096d4e9 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -205,8 +205,10 @@ class ProductProduct(models.Model): fifo_vals = self._run_fifo(abs(quantity), company) vals['remaining_qty'] = fifo_vals.get('remaining_qty') # In case of AVCO, fix rounding issue of standard price when needed. - if self.product_tmpl_id.cost_method == 'average': - rounding_error = currency.round(self.standard_price * self.quantity_svl - self.value_svl) + if self.product_tmpl_id.cost_method == 'average' and not float_is_zero(self.quantity_svl, precision_rounding=self.uom_id.rounding): + rounding_error = currency.round( + (self.standard_price * self.quantity_svl - self.value_svl) * abs(quantity / self.quantity_svl) + ) if rounding_error: # If it is bigger than the (smallest number of the currency * quantity) / 2, # then it isn't a rounding error but a stock valuation error, we shouldn't fix it under the hood ... diff --git a/addons/stock_account/tests/test_stockvaluation.py b/addons/stock_account/tests/test_stockvaluation.py index fffde62a713..84638b82b15 100644 --- a/addons/stock_account/tests/test_stockvaluation.py +++ b/addons/stock_account/tests/test_stockvaluation.py @@ -2108,7 +2108,7 @@ class TestStockValuation(TransactionCase): move5.move_line_ids.qty_done = 30.0 move5._action_done() - self.assertEqual(move5.stock_valuation_layer_ids.value, -477.5) + self.assertEqual(move5.stock_valuation_layer_ids.value, -477.56) # Receives 10 units but assign them to an owner, the valuation should not be impacted. move6 = self.env['stock.move'].create({ @@ -2142,7 +2142,7 @@ class TestStockValuation(TransactionCase): move7.move_line_ids.qty_done = 50.0 move7._action_done() - self.assertEqual(move7.stock_valuation_layer_ids.value, -796.0) + self.assertEqual(move7.stock_valuation_layer_ids.value, -795.94) self.assertAlmostEqual(self.product1.quantity_svl, 0.0) self.assertAlmostEqual(self.product1.value_svl, 0.0) diff --git a/addons/stock_account/tests/test_stockvaluationlayer.py b/addons/stock_account/tests/test_stockvaluationlayer.py index 9c1ce8e0381..aa17d061f78 100644 --- a/addons/stock_account/tests/test_stockvaluationlayer.py +++ b/addons/stock_account/tests/test_stockvaluationlayer.py @@ -547,6 +547,17 @@ class TestStockValuationAVCO(TestStockValuationCommon): self.assertEqual(self.product1.quantity_svl, 0) self.assertEqual(self.product1.standard_price, 1.01) + def test_rounding_svl_3(self): + self._make_in_move(self.product1, 1000, unit_cost=0.17) + self._make_in_move(self.product1, 800, unit_cost=0.23) + + self.assertEqual(self.product1.standard_price, 0.20) + + self._make_out_move(self.product1, 1000, create_picking=True) + self._make_out_move(self.product1, 800, create_picking=True) + + self.assertEqual(self.product1.value_svl, 0) + def test_return_delivery_2(self): self.product1.write({"standard_price": 1}) move1 = self._make_out_move(self.product1, 10, create_picking=True, force_assign=True)