From f2b19e4f4dfd9099c0c707686c5fc336083fe9c3 Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Wed, 14 Oct 2020 10:07:42 +0000 Subject: [PATCH] [FIX] stock: don't check parent locations when finding putaway rules Let's say we have location A, B, and C. A is the parent of B, B is the parent of C. And we have a putaway rule to move product from A to B. Now receive product at C, because currently when we can't find a putaway rule at one location, we will loop to check its parent locations. So the puteaway rule A -> B will be found, and product received at C will in the end be stored at B. After this commit, we don't check the parent locations when we can't find a putaway rule. Task 2341820 PR #63516 ENT PR odoo/enterprise#15363 UPG PR odoo/upgrade#2040 --- addons/stock/models/stock_location.py | 29 +++++++--------- addons/stock/tests/test_move.py | 49 --------------------------- 2 files changed, 13 insertions(+), 65 deletions(-) diff --git a/addons/stock/models/stock_location.py b/addons/stock/models/stock_location.py index e158a287699..48e30e07d8c 100644 --- a/addons/stock/models/stock_location.py +++ b/addons/stock/models/stock_location.py @@ -173,23 +173,20 @@ class Location(models.Model): def _get_putaway_strategy(self, product): ''' Returns the location where the product has to be put, if any compliant putaway strategy is found. Otherwise returns None.''' - current_location = self putaway_location = self.env['stock.location'] - while current_location and not putaway_location: - # Looking for a putaway about the product. - putaway_rules = current_location.putaway_rule_ids.filtered(lambda x: x.product_id == product) - if putaway_rules: - putaway_location = putaway_rules[0].location_out_id - # If not product putaway found, we're looking with category so. - else: - categ = product.categ_id - while categ: - putaway_rules = current_location.putaway_rule_ids.filtered(lambda x: x.category_id == categ) - if putaway_rules: - putaway_location = putaway_rules[0].location_out_id - break - categ = categ.parent_id - current_location = current_location.location_id + # Looking for a putaway about the product. + putaway_rules = self.putaway_rule_ids.filtered(lambda x: x.product_id == product) + if putaway_rules: + putaway_location = putaway_rules[0].location_out_id + # If not product putaway found, we're looking with category so. + else: + categ = product.categ_id + while categ: + putaway_rules = self.putaway_rule_ids.filtered(lambda x: x.category_id == categ) + if putaway_rules: + putaway_location = putaway_rules[0].location_out_id + break + categ = categ.parent_id return putaway_location def should_bypass_reservation(self): diff --git a/addons/stock/tests/test_move.py b/addons/stock/tests/test_move.py index 5049bcf3ed4..7acc47cd544 100644 --- a/addons/stock/tests/test_move.py +++ b/addons/stock/tests/test_move.py @@ -937,55 +937,6 @@ class StockMove(TransactionCase): # check if the putaway was rightly applied self.assertEqual(move1.move_line_ids.location_dest_id.id, shelf2_location.id) - def test_putaway_7(self): - """ Checks parents locations are also browsed when looking for putaways. - - WH/Stock > WH/Stock/Floor1> WH/Stock/Floor1/Rack1 > WH/Stock/Floor1/Rack1/Shelf2 - The putaway is on Floor1 to send to Shelf2 - A move from supplier to Rack1 should send to shelf2 - """ - floor1 = self.env['stock.location'].create({ - 'name': 'floor1', - 'usage': 'internal', - 'location_id': self.stock_location.id, - }) - rack1 = self.env['stock.location'].create({ - 'name': 'rack1', - 'usage': 'internal', - 'location_id': floor1.id, - }) - shelf2 = self.env['stock.location'].create({ - 'name': 'shelf2', - 'usage': 'internal', - 'location_id': rack1.id, - }) - - # putaway floor1 -> shelf2 - putaway = self.env['stock.putaway.rule'].create({ - 'product_id': self.product.id, - 'location_in_id': floor1.id, - 'location_out_id': shelf2.id, - }) - floor1.write({ - 'putaway_rule_ids': [(4, putaway.id, 0)], - }) - - # stock move supplier -> rack1 - move1 = self.env['stock.move'].create({ - 'name': 'test_putaway_6', - 'location_id': self.supplier_location.id, - 'location_dest_id': rack1.id, - 'product_id': self.product.id, - 'product_uom': self.uom_unit.id, - 'product_uom_qty': 100.0, - }) - move1._action_confirm() - self.assertEqual(move1.state, 'assigned') - self.assertEqual(len(move1.move_line_ids), 1) - - # check if the putaway was rightly applied - self.assertEqual(move1.move_line_ids.location_dest_id.id, shelf2.id) - def test_availability_1(self): """ Check that the `availability` field on a move is correctly computed when there is more than enough products in stock.