From 608c71841d70beabe80c61196a7907e3a2404f2a Mon Sep 17 00:00:00 2001 From: DavidJForgeFlow Date: Wed, 17 Apr 2024 11:33:54 +0200 Subject: [PATCH] [IMP] stock_inventory: adds exclude_sublocation flag --- stock_inventory/models/stock_inventory.py | 21 ++++++--- stock_inventory/tests/test_stock_inventory.py | 45 +++++++++++++++++++ stock_inventory/views/stock_inventory.xml | 5 +++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/stock_inventory/models/stock_inventory.py b/stock_inventory/models/stock_inventory.py index 78947198257c..fec6e898bbde 100644 --- a/stock_inventory/models/stock_inventory.py +++ b/stock_inventory/models/stock_inventory.py @@ -66,6 +66,11 @@ class InventoryAdjustmentsGroup(models.Model): compute="_compute_count_stock_moves", string="Stock Moves Lines" ) + exclude_sublocation = fields.Boolean( + help="If enabled, it will only take into account " + "the locations settled, not the child ones." + ) + @api.depends("stock_quant_ids") def _compute_count_stock_quants(self): self.count_stock_quants = len(self.stock_quant_ids) @@ -99,11 +104,17 @@ def _get_quants(self, locations): return self.env["stock.quant"].search(domain) def _get_base_domain(self, locations): - return [ - "|", - ("location_id", "in", locations.mapped("id")), - ("location_id", "in", locations.child_ids.ids), - ] + return ( + [ + ("location_id", "in", locations.mapped("id")), + ] + if self.exclude_sublocation + else [ + "|", + ("location_id", "in", locations.mapped("id")), + ("location_id", "child_of", locations.child_ids.ids), + ] + ) def _get_domain_all_quants(self, base_domain): return base_domain diff --git a/stock_inventory/tests/test_stock_inventory.py b/stock_inventory/tests/test_stock_inventory.py index 67b2a976cc51..ba8ed55cadae 100644 --- a/stock_inventory/tests/test_stock_inventory.py +++ b/stock_inventory/tests/test_stock_inventory.py @@ -314,3 +314,48 @@ def test_05_category_selection(self): self.assertEqual(inventory1.stock_move_ids.product_id.id, self.product2.id) self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location3.id) inventory1.action_state_to_done() + + def test_06_exclude_sub_locations(self): + inventory1 = self.inventory_model.create( + { + "name": "Inventory_Test_1", + "product_selection": "all", + "location_ids": [self.location1.id], + "exclude_sublocation": True, + } + ) + inventory1.action_state_to_in_progress() + inventory2 = self.inventory_model.create( + { + "name": "Inventory_Test_2", + "product_selection": "all", + "location_ids": [self.location1.id], + "exclude_sublocation": True, + } + ) + with self.assertRaises(ValidationError), self.cr.savepoint(): + inventory2.action_state_to_in_progress() + self.assertEqual(inventory1.state, "in_progress") + self.assertEqual( + inventory1.stock_quant_ids.ids, + [self.quant1.id], + ) + inventory1.action_state_to_draft() + self.assertEqual(inventory1.stock_quant_ids.ids, []) + inventory1.action_state_to_in_progress() + self.assertEqual(inventory1.count_stock_moves, 0) + self.assertEqual(inventory1.count_stock_quants, 1) + self.assertEqual(inventory1.count_stock_quants_string, "1 / 1") + inventory1.action_view_inventory_adjustment() + self.quant1.inventory_quantity = 92 + self.quant1.action_apply_inventory() + inventory1._compute_count_stock_quants() + inventory1.action_view_stock_moves() + self.assertEqual(inventory1.count_stock_moves, 1) + self.assertEqual(inventory1.count_stock_quants, 1) + self.assertEqual(inventory1.count_stock_quants_string, "0 / 1") + self.assertEqual(inventory1.stock_move_ids.qty_done, 8) + self.assertEqual(inventory1.stock_move_ids.product_id.id, self.product.id) + self.assertEqual(inventory1.stock_move_ids.lot_id.id, self.lot_1.id) + self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location1.id) + inventory1.action_state_to_done() diff --git a/stock_inventory/views/stock_inventory.xml b/stock_inventory/views/stock_inventory.xml index 8da3607de9d2..df592f25fae1 100644 --- a/stock_inventory/views/stock_inventory.xml +++ b/stock_inventory/views/stock_inventory.xml @@ -82,6 +82,11 @@ attrs="{'readonly':[('state', 'in', ['in_progress', 'done'])]}" required="1" /> +