From 8c747b6f01d0b8c60c046b6c3202718a5e3880c0 Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Fri, 26 Jul 2024 14:08:53 +0200 Subject: [PATCH] [FIX] stock_inventory:Resolve error when applying inventory with multiple adjustments in the same location --- stock_inventory/models/stock_inventory.py | 23 ++++++++++--- stock_inventory/models/stock_quant.py | 18 ++++------ stock_inventory/tests/test_stock_inventory.py | 34 +++++++++++++++++++ 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/stock_inventory/models/stock_inventory.py b/stock_inventory/models/stock_inventory.py index 5371950cb..a60bf408f 100644 --- a/stock_inventory/models/stock_inventory.py +++ b/stock_inventory/models/stock_inventory.py @@ -180,10 +180,13 @@ class InventoryAdjustmentsGroup(models.Model): for rec in self: quants = rec.stock_quant_ids quants_to_do = quants.filtered(lambda q: q.to_do) - count_todo = len(quants_to_do) + quants_pending_to_review = quants_to_do.filtered( + lambda q: q.current_inventory_id.id == rec.id + ) + count_pending_to_review = len(quants_pending_to_review) rec.count_stock_quants = len(quants) rec.count_stock_quants_string = "{} / {}".format( - count_todo, rec.count_stock_quants + count_pending_to_review, rec.count_stock_quants ) @api.depends("stock_move_ids") @@ -336,6 +339,7 @@ class InventoryAdjustmentsGroup(models.Model): "to_do": True, "user_id": self.responsible_id, "inventory_date": self.date, + "current_inventory_id": self.id, } ) return @@ -343,11 +347,14 @@ class InventoryAdjustmentsGroup(models.Model): def action_state_to_done(self): self.ensure_one() self.state = "done" - self.stock_quant_ids.update( + self.stock_quant_ids.filtered( + lambda q: q.current_inventory_id.id == self.id + ).update( { "to_do": False, "user_id": False, "inventory_date": False, + "current_inventory_id": False, } ) return @@ -361,11 +368,14 @@ class InventoryAdjustmentsGroup(models.Model): def action_state_to_draft(self): self.ensure_one() self.state = "draft" - self.stock_quant_ids.update( + self.stock_quant_ids.filtered( + lambda q: q.current_inventory_id.id == self.id + ).update( { "to_do": False, "user_id": False, "inventory_date": False, + "current_inventory_id": False, } ) self.stock_quant_ids = None @@ -403,7 +413,10 @@ class InventoryAdjustmentsGroup(models.Model): ) result.update( { - "domain": [("id", "in", self.stock_quant_ids.ids)], + "domain": [ + ("id", "in", self.stock_quant_ids.ids), + ("current_inventory_id", "=", self.id), + ], "search_view_id": self.env.ref("stock.quant_search_view").id, "context": context, } diff --git a/stock_inventory/models/stock_quant.py b/stock_inventory/models/stock_quant.py index 8b6a91b3e..8f72457a5 100644 --- a/stock_inventory/models/stock_quant.py +++ b/stock_inventory/models/stock_quant.py @@ -12,23 +12,18 @@ class StockQuant(models.Model): string="Stock Inventories", copy=False, ) + current_inventory_id = fields.Many2one( + "stock.inventory", + string="Current Inventory", + store=True, + ) def _apply_inventory(self): res = super()._apply_inventory() record_moves = self.env["stock.move.line"] adjustment = self.env["stock.inventory"].browse() for rec in self: - adjustment = ( - self.env["stock.inventory"] - .search([("state", "=", "in_progress")]) - .filtered( - lambda x: rec.location_id in x.location_ids - or ( - rec.location_id in x.location_ids.child_internal_location_ids - and not x.exclude_sublocation - ) - ) - ) + adjustment = rec.current_inventory_id moves = record_moves.search( [ ("product_id", "=", rec.product_id.id), @@ -59,6 +54,7 @@ class StockQuant(models.Model): } ) rec.to_do = False + rec.current_inventory_id = False if adjustment and self.env.company.stock_inventory_auto_complete: adjustment.action_auto_state_to_done() return res diff --git a/stock_inventory/tests/test_stock_inventory.py b/stock_inventory/tests/test_stock_inventory.py index 721f18552..049422ccf 100644 --- a/stock_inventory/tests/test_stock_inventory.py +++ b/stock_inventory/tests/test_stock_inventory.py @@ -555,3 +555,37 @@ class TestStockInventory(TransactionCase): expected_result, "The search function did not return the expected results", ) + + def test_13_multiple_inventories_different_products_same_location(self): + inventory1 = self.inventory_model.create( + { + "name": "Inventory1 for Product1", + "product_ids": [(6, 0, [self.product.id])], + "location_ids": [(6, 0, [self.location3.id])], + "product_selection": "manual", + } + ) + inventory2 = self.inventory_model.create( + { + "name": "Inventory2 for Product2", + "product_ids": [(6, 0, [self.product2.id])], + "location_ids": [(6, 0, [self.location3.id])], + "product_selection": "manual", + } + ) + inventory1.action_state_to_in_progress() + inventory2.action_state_to_in_progress() + self.assertEqual(inventory1.state, "in_progress") + self.assertEqual(inventory2.state, "in_progress") + self.assertEqual( + inventory1.stock_quant_ids.filtered( + lambda q: q.product_id == self.product + ).current_inventory_id, + inventory1, + ) + self.assertEqual( + inventory2.stock_quant_ids.filtered( + lambda q: q.product_id == self.product2 + ).current_inventory_id, + inventory2, + )