summaryrefslogtreecommitdiff
path: root/addons/stock/tests/test_quant.py
diff options
context:
space:
mode:
authorSimon Lejeune <sle@openerp.com>2017-09-11 15:55:21 +0200
committerSimon Lejeune <sle@openerp.com>2017-09-12 10:50:04 +0200
commitd08b4407be3cf9b404087056a413fbdc9eecbd9d (patch)
treef0bc89105d18826aef3e9b11167985a8d1d22fe2 /addons/stock/tests/test_quant.py
parentbb8f7bf9c3ef6f924912682802f02b1638212231 (diff)
[REF] stock: `_get_available_qty` and tracked products
Suppose you have a product tracked by serial number. In your inventory, you have two quants: -1 for serial1, +1 for serial2. If you create a move for 1 product and try to run `action_assign`, it didn't work before this patch because stock.move's `_update_reserved_quantity` is guarded by a call to an unstricted `_get_available_quantity` (unstricted meaning considering all quants regardless of their lot_id/package_id/owner_id). This call resulted in an available quantity of 0, thus the stock move could not be reserved To fix this issue, we patched `_get_available_quantity` so that it rightly considers products of different lot_id as different products, i.e. it returns an available quantity of +1 in the initial scenario. Of course, we consider all quants of the same lot_id, i.e. if you have -1 for serial1 and +1 for serial2, the available quantity amounts to 0. In another scenario, let's say we have only -1 serial1 in stock, if we run `_get_available_quantity`, the method should return 0: nothing is reservable. We continue this reasonment by returning 0 everytime the available quantity is negative, tracked or not. This is probably what a method named `_get_available_quantity` should have done since the beginning. There's only a single case where we need to know the quantity even if it's negative: it's just after we move a quant to its destination location. If this quant was tracked and resulted in a negative quant (i.e. we moved something we didn't have), we try to compensate with an untracked quant. In this special case, we do not want to ignore the negative quants when calling `_get_available_quantity`. That's why we introduce a new argument to this method. We could have added a new method `_get_quantity` for clarity sake.
Diffstat (limited to 'addons/stock/tests/test_quant.py')
-rw-r--r--addons/stock/tests/test_quant.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/addons/stock/tests/test_quant.py b/addons/stock/tests/test_quant.py
index 0dd7af02db7..f28fa3c4ef2 100644
--- a/addons/stock/tests/test_quant.py
+++ b/addons/stock/tests/test_quant.py
@@ -131,7 +131,8 @@ class StockQuant(TransactionCase):
'quantity': 5.0,
'reserved_quantity': 0.0,
})
- self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location), -5.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location), 0.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location, allow_negative=True), -5.0)
def test_get_available_quantity_7(self):
""" Quantity availability with only one tracked quant in a location.
@@ -153,7 +154,8 @@ class StockQuant(TransactionCase):
'reserved_quantity': 20.0,
'lot_id': lot1.id,
})
- self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location, lot_id=lot1), -10.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location, lot_id=lot1), 0.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location, lot_id=lot1, allow_negative=True), -10.0)
def test_get_available_quantity_8(self):
""" Quantity availability with a consumable product.
@@ -282,7 +284,8 @@ class StockQuant(TransactionCase):
'type': 'product',
})
self.env['stock.quant']._update_available_quantity(product1, stock_location, -1.0)
- self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location), -1.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location), 0.0)
+ self.assertEqual(self.env['stock.quant']._get_available_quantity(product1, stock_location, allow_negative=True), -1.0)
def test_decrease_available_quantity_2(self):
""" Decrease the available quantity when multiple quants are already in a location.