From 7ddb56a36c7bf9ca222210b82fdf400ae5c702a6 Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Tue, 9 Apr 2024 09:39:21 +0000 Subject: [PATCH] [FIX] stock_move_location: fix planned transfer incorrect reserved_uom_qty This PR fixes stock quant matching in _get_gather_domain by setting strict=True, ensuring precise matching based on lot, package, and location. This change addresses issues arising from the previous approach, which could lead to the inclusion of unnecessary quants, such as those without an owner or lot. --- .../tests/test_move_location.py | 71 +++++++++++++++++++ .../wizard/stock_move_location.py | 4 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/stock_move_location/tests/test_move_location.py b/stock_move_location/tests/test_move_location.py index 34f036fb5..87de962f9 100644 --- a/stock_move_location/tests/test_move_location.py +++ b/stock_move_location/tests/test_move_location.py @@ -151,6 +151,77 @@ class TestMoveLocation(TestsCommon): [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 123.0], ) + def test_planned_transfer_strict(self): + product = self.env["product.product"].create( + {"name": "Test", "type": "product", "tracking": "lot"} + ) + lot = self.env["stock.lot"].create( + { + "name": "Test lot", + "product_id": product.id, + } + ) + self.set_product_amount( + product, + self.internal_loc_1, + 10.0, + ) + self.set_product_amount( + product, + self.internal_loc_1, + 10.0, + lot_id=lot, + ) + wizard = self._create_wizard(self.internal_loc_1, self.internal_loc_2) + wizard.onchange_origin_location() + wizard = wizard.with_context(planned=True) + location_lines = wizard.stock_move_location_line_ids.filtered( + lambda r: r.product_id.id != product.id or r.lot_id.id != lot.id + ) + location_lines.unlink() + wizard.action_move_location() + picking = wizard.picking_id + self.assertEqual(picking.state, "assigned") + self.assertEqual( + len(wizard.stock_move_location_line_ids), len(picking.move_line_ids) + ) + location_line = wizard.stock_move_location_line_ids + wizard_lines = [ + location_line.product_id.id, + location_line.lot_id.id, + location_line.move_quantity, + ] + line = picking.move_line_ids + picking_lines = [line.product_id.id, line.lot_id.id, line.reserved_uom_qty] + self.assertEqual( + wizard_lines, + picking_lines, + "Mismatch between move location lines and move lines", + ) + self.assertEqual( + picking.move_line_ids.reserved_uom_qty, + 10.0, + ) + + # Create planned transfer for same quant + wizard = self._create_wizard(self.internal_loc_1, self.internal_loc_2) + wizard.onchange_origin_location() + wizard = wizard.with_context(planned=True) + location_lines = wizard.stock_move_location_line_ids.filtered( + lambda r: r.product_id.id != product.id or r.lot_id.id != lot.id + ) + location_lines.unlink() + wizard.action_move_location() + picking = wizard.picking_id + self.assertEqual(picking.state, "assigned") + self.assertEqual( + len(wizard.stock_move_location_line_ids), len(picking.move_line_ids) + ) + self.assertEqual( + picking.move_line_ids.mapped("reserved_uom_qty"), + [0.0], + ) + def test_quant_transfer(self): """Test quants transfer.""" quants = self.product_lots.stock_quant_ids diff --git a/stock_move_location/wizard/stock_move_location.py b/stock_move_location/wizard/stock_move_location.py index 53380c3ea..ee6b50513 100644 --- a/stock_move_location/wizard/stock_move_location.py +++ b/stock_move_location/wizard/stock_move_location.py @@ -218,7 +218,7 @@ class StockMoveLocationWizard(models.TransientModel): lot_id=line.lot_id, package_id=line.package_id, owner_id=line.owner_id, - strict=False, + strict=True, ) move._update_reserved_quantity( line.move_quantity, @@ -227,7 +227,7 @@ class StockMoveLocationWizard(models.TransientModel): lot_id=line.lot_id, package_id=line.package_id, owner_id=line.owner_id, - strict=False, + strict=True, ) # Force the state to be assigned, instead of _action_assign, # to avoid discarding the selected move_location_line.