From 16a9ff3506f62decdd467046c44e8a73d3f4bbe3 Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Wed, 18 Sep 2024 09:53:58 +0200 Subject: [PATCH] [IMP] stock_inventory_lockdown: do not block movements of parent location if child bin location is being reviwed --- .../models/stock_inventory.py | 30 ++++++++++++++----- .../models/stock_move_line.py | 21 ++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/stock_inventory_lockdown/models/stock_inventory.py b/stock_inventory_lockdown/models/stock_inventory.py index 436847fc8479..b9d933775264 100644 --- a/stock_inventory_lockdown/models/stock_inventory.py +++ b/stock_inventory_lockdown/models/stock_inventory.py @@ -11,19 +11,33 @@ class StockInventory(models.Model): @api.model def _get_locations_open_inventories(self, locations_ids=None): - inventory_domain = [("state", "=", "in_progress")] - if locations_ids: - inventory_domain.append(("location_ids", "child_of", locations_ids)) - inventories = self.search(inventory_domain) + if not locations_ids: + return [] + inventory_domain_same_location = [ + ("state", "=", "in_progress"), + ("location_ids", "in", locations_ids), + ] + inventories_same_location = self.search(inventory_domain_same_location) + inventory_domain_parent = [ + ("state", "=", "in_progress"), + ("exclude_sublocation", "=", False), + ] + inventories_possible_parent = self.search(inventory_domain_parent) + inventories_parent = self.env["stock.inventory"] + for inventory in inventories_possible_parent: + for location in inventory.location_ids: + if any( + location_id in location.child_internal_location_ids.ids + for location_id in locations_ids + ): + inventories_parent |= inventory + inventories = inventories_same_location | inventories_parent if not inventories: # Early exit if no match found return [] location_ids = inventories.mapped("location_ids") - # Extend to the children Locations location_domain = [ - "|", - ("location_id", "in", location_ids.ids), - ("location_id", "child_of", location_ids.ids), + ("id", "in", location_ids.ids), ("usage", "in", ["internal", "transit"]), ] return self.env["stock.location"].search(location_domain) diff --git a/stock_inventory_lockdown/models/stock_move_line.py b/stock_inventory_lockdown/models/stock_move_line.py index 1c859f9277d2..db10b9655605 100644 --- a/stock_inventory_lockdown/models/stock_move_line.py +++ b/stock_inventory_lockdown/models/stock_move_line.py @@ -26,20 +26,11 @@ def _check_locked_location(self): ]._get_locations_open_inventories( [move_line.location_dest_id.id, move_line.location_id.id] ) - reserved_origin_loc = move_line.location_id - dest_loc = move_line.location_dest_id - if ( - locked_location_ids - and not any( - [ - move_line.location_dest_id.usage == "inventory", - move_line.location_id.usage == "inventory", - ] - ) - and ( - reserved_origin_loc in locked_location_ids - or dest_loc in locked_location_ids - ) + if locked_location_ids and not any( + [ + move_line.location_dest_id.usage == "inventory", + move_line.location_id.usage == "inventory", + ] ): location_names = locked_location_ids.mapped("complete_name") raise ValidationError( @@ -47,7 +38,7 @@ def _check_locked_location(self): "Inventory adjusment underway at " "the following location(s):\n- %s\n" "Moving products to or from these locations is " - "not allowed until the inventory check is complete." + "not allowed until the inventory adjustment is complete." ) % "\n - ".join(location_names) )