[FIX] stock_account: compensate rounding error

To reproduce the issue:
1. Create a product:
   - Type: Storable
   - Category:
     - Costing Method: AVCO
2. IN 1000 @ 0.17
3. IN 800 @ 0.23
4. OUT 1000
5. OUT 800
6. Open the inventory valuation of the product

Error: the total value is $-6.00, it should be zero

Once all products received, the standard price is $0.20. Its value
has been rounded because the real value is
`(1000 * .17 + 800 * .23) / 1800 = 0,196666667`
The standard price will create a difference when using the products,
because:
`(1800 * .20 = 360) != (1000 * .17 + 800 * .23 = 354)`
That's the reason why a feature tries to compensate such rounding
errors. So, step 4, when preparing the values of the out-SVL, we
check if there is a rounding error, and we find a difference of $6,
which is correct. However, the difference is above the treshold, so
we will not consider it as a rounding error:
https://github.com/odoo/odoo/blob/3ff51daa93a1d670b8f67f79418d4dd48e94875f/addons/stock_account/models/product.py#L197-L200

Here is the issue: the threshold is based on the outgoing quantity
(1000) while the value difference is based on the whole quantity
(1800). This difference should also be proportional to the outgoing
quantity.

Note: The fix will still not work with a small quantity. The only and
best solution is to change the type of the SVL unit cost into a
float.

OPW-3101374

X-original-commit: 35d8b4c2a223970520961bf8bfc574bb309af090
Part-of: odoo/odoo#114525
Co-authored-by: Adrien Widart (awt) <awt@odoo.com>
This commit is contained in:
William Henrotin
2023-03-07 16:56:21 +01:00
committed by Adrien Widart (awt)
co-authored by Adrien Widart
parent 35030e0597
commit c05003a2c2
3 changed files with 17 additions and 4 deletions
+4 -2
View File
@@ -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 ...
@@ -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)
@@ -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)