Skip to content

Commit 35e8fcd

Browse files
committed
[FIX] stock_available_location_orderpoint: Use correct quantity for current replenishments
1 parent 53fbe77 commit 35e8fcd

File tree

3 files changed

+174
-8
lines changed

3 files changed

+174
-8
lines changed

stock_available_location_orderpoint/models/product_product.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def _compute_available_quantities_dict(self):
6060
("state", "not in", ("done", "cancel")),
6161
("product_id", "in", self.ids),
6262
],
63-
["product_id"],
63+
["product_id", "product_uom_qty:sum"],
6464
["product_id"],
6565
)
6666
quantities_in_replenishments = defaultdict(lambda: defaultdict(lambda: 0))
6767
for current_move in current_moves:
6868
quantities_in_replenishments[current_move["product_id"][0]] = current_move[
69-
"product_id_count"
69+
"product_uom_qty"
7070
]
7171
for product in self:
7272
qties_replenished_for_location = {product: 0.0}

stock_available_location_orderpoint/tests/test_available_location_orderpoint.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,123 @@ def test_available_on_one_stock_location_with_shelf(self):
221221
)
222222
self.assertTrue(self.product.id in product.ids)
223223

224+
def test_available_on_different_sublocation(self):
225+
"""
226+
Remove the existing orderpoints
227+
228+
Create a location structure like:
229+
- Stock
230+
- Area 1
231+
- Shelf 1
232+
- Area 2
233+
- Shelf 2
234+
235+
Create orderpoints for both areas.
236+
237+
Create ougoing moves for both shelves:
238+
- Shelf 1: 12.0 (product 1)
239+
- Shelf 2: 2.0
240+
241+
Global quantity to replenish should be 8.0
242+
243+
Quantity for Shelf 1 should be 6.0
244+
245+
"""
246+
# Archive orderpoints
247+
self.env["stock.location.orderpoint"].search([]).write({"active": False})
248+
249+
self.area_1 = self.env["stock.location"].create(
250+
{
251+
"name": "Area 1",
252+
"location_id": self.location_dest.id,
253+
"usage": "view",
254+
}
255+
)
256+
257+
self.shelf_1 = self.env["stock.location"].create(
258+
{"name": "Shelf 1", "location_id": self.area_1.id}
259+
)
260+
261+
self.area_2 = self.env["stock.location"].create(
262+
{
263+
"name": "Area 2",
264+
"location_id": self.location_dest.id,
265+
"usage": "view",
266+
}
267+
)
268+
269+
self.shelf_2 = self.env["stock.location"].create(
270+
{"name": "Shelf 1", "location_id": self.area_2.id}
271+
)
272+
273+
# Create an orderpoint by shelf
274+
self.location_dest = self.area_1
275+
(
276+
self.orderpoint_shelf_1,
277+
self.location_src_shelf_1,
278+
) = self._create_orderpoint_complete("Area 1 Replenishment", trigger="manual")
279+
280+
self.location_dest = self.area_2
281+
(
282+
self.orderpoint_shelf_2,
283+
self.location_src_shelf_2,
284+
) = self._create_orderpoint_complete("Area 2 Replenishment", trigger="manual")
285+
286+
# Set stock on replenishment locations
287+
self.env["stock.quant"].with_context(inventory_mode=True).create(
288+
{
289+
"inventory_quantity": 6.0,
290+
"location_id": self.location_src_shelf_1.id,
291+
"product_id": self.product.id,
292+
}
293+
)._apply_inventory()
294+
self.env["stock.quant"].with_context(inventory_mode=True).create(
295+
{
296+
"inventory_quantity": 4.0,
297+
"location_id": self.location_src_shelf_2.id,
298+
"product_id": self.product.id,
299+
}
300+
)._apply_inventory()
301+
302+
self.location_dest = self.area_1
303+
move = self._create_outgoing_move(12)
304+
self.assertEqual(move.state, "confirmed")
305+
306+
self.location_dest = self.area_2
307+
move = self._create_outgoing_move(2)
308+
self.assertEqual(move.state, "confirmed")
309+
310+
self.product.invalidate_recordset()
311+
self.assertEqual(8.0, self.product.quantity_to_replenish)
312+
313+
self.product.invalidate_recordset()
314+
self.assertEqual(
315+
6.0,
316+
self.product.with_context(location=self.shelf_1.id).quantity_to_replenish,
317+
)
318+
319+
# Run replenishment on area 1
320+
self.orderpoint_shelf_1.run_replenishment()
321+
# Test all variables in different contexts
322+
self.product.invalidate_recordset()
323+
self.assertEqual(2.0, self.product.quantity_to_replenish)
324+
self.assertEqual(6.0, self.product.quantity_in_replenishments)
325+
self.product.invalidate_recordset()
326+
self.assertEqual(
327+
0.0,
328+
self.product.with_context(location=self.shelf_1.id).quantity_to_replenish,
329+
)
330+
self.product.invalidate_recordset()
331+
self.assertEqual(
332+
6.0,
333+
self.product.with_context(
334+
location=self.shelf_1.id
335+
).quantity_in_replenishments,
336+
)
337+
338+
products = self.product.search([("quantity_in_replenishments", "=", 6.0)])
339+
self.assertTrue(self.product.id in products.ids)
340+
224341
def test_action(self):
225342
action = self.product.action_open_replenishments()
226343
self.assertIn(

stock_available_location_orderpoint/tests/test_available_location_orderpoint_template.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ def test_available_on_different_sublocation(self):
356356
Create orderpoints for both areas.
357357
358358
Create ougoing moves for both shelves:
359-
- Shelf 1: 12.0
360-
- Shelf 2: 2.0
359+
- Shelf 1: 12.0 (Product 1)
360+
- Shelf 1: 4.0 (Product 2)
361+
- Shelf 2: 2.0 (Product 1)
361362
362363
Global quantity to replenish should be 8.0
363364
@@ -412,6 +413,13 @@ def test_available_on_different_sublocation(self):
412413
"product_id": self.product.id,
413414
}
414415
)._apply_inventory()
416+
self.env["stock.quant"].with_context(inventory_mode=True).create(
417+
{
418+
"inventory_quantity": 5.0,
419+
"location_id": self.location_src_shelf_1.id,
420+
"product_id": self.product_2.id,
421+
}
422+
)._apply_inventory()
415423
self.env["stock.quant"].with_context(inventory_mode=True).create(
416424
{
417425
"inventory_quantity": 4.0,
@@ -420,22 +428,63 @@ def test_available_on_different_sublocation(self):
420428
}
421429
)._apply_inventory()
422430

423-
self.location_dest = self.shelf_1
431+
self.env["stock.quant"].with_context(inventory_mode=True).create(
432+
{
433+
"inventory_quantity": 3.0,
434+
"location_id": self.location_src_shelf_2.id,
435+
"product_id": self.product_2.id,
436+
}
437+
)._apply_inventory()
438+
439+
# Product 1
440+
self.location_dest = self.area_1
424441
move = self._create_outgoing_move(12)
425442
self.assertEqual(move.state, "confirmed")
426443

427-
self.location_dest = self.shelf_2
444+
self.location_dest = self.area_2
428445
move = self._create_outgoing_move(2)
429446
self.assertEqual(move.state, "confirmed")
430447

448+
# Product 2 - with less quantity than in replenishment location
449+
self.product = self.product_2
450+
self.location_dest = self.area_1
451+
move = self._create_outgoing_move(1)
452+
self.assertEqual(move.state, "confirmed")
453+
454+
self.location_dest = self.area_2
455+
move = self._create_outgoing_move(3)
456+
self.assertEqual(move.state, "confirmed")
457+
458+
self.template.invalidate_recordset()
459+
self.assertEqual(12.0, self.template.quantity_to_replenish)
460+
431461
self.template.invalidate_recordset()
432-
self.assertEqual(8.0, self.template.quantity_to_replenish)
462+
self.assertEqual(
463+
7.0,
464+
self.template.with_context(location=self.shelf_1.id).quantity_to_replenish,
465+
)
433466

467+
# Run replenishment on area 1
468+
self.orderpoint_shelf_1.run_replenishment()
469+
# Test all variables in different contexts
470+
self.template.invalidate_recordset()
471+
self.assertEqual(5.0, self.template.quantity_to_replenish)
472+
self.assertEqual(7.0, self.template.quantity_in_replenishments)
434473
self.template.invalidate_recordset()
435474
self.assertEqual(
436-
6.0,
475+
0.0,
437476
self.template.with_context(location=self.shelf_1.id).quantity_to_replenish,
438477
)
478+
self.template.invalidate_recordset()
479+
self.assertEqual(
480+
7.0,
481+
self.template.with_context(
482+
location=self.shelf_1.id
483+
).quantity_in_replenishments,
484+
)
485+
486+
templates = self.template.search([("quantity_in_replenishments", "=", 7.0)])
487+
self.assertTrue(self.template.id in templates.ids)
439488

440489
def test_action(self):
441490
action = self.template.action_open_replenishments()

0 commit comments

Comments
 (0)