diff --git a/stock_helper/models/stock_location.py b/stock_helper/models/stock_location.py index 03ad17023..e335c40eb 100644 --- a/stock_helper/models/stock_location.py +++ b/stock_helper/models/stock_location.py @@ -20,8 +20,8 @@ class StockLocation(models.Model): # below one of the other location without using SQL. return func(self.parent_path.startswith(other.parent_path) for other in others) - def get_closest_warehouse(self): - """Returns closest warehouse for current location. + def _get_closest_warehouse(self): + """Returns dict of closest warehouse per location By default the get_warehouse (which is located in the odoo core module stock) returns the warehouse via searching the view_location_id with parent_of. @@ -31,17 +31,30 @@ class StockLocation(models.Model): With this methods we will really get the closest warehouse of a location """ - self.ensure_one() - location_ids = [int(x) for x in self.parent_path.split("/") if x] warehouses = ( self.env["stock.warehouse"] - .search([("view_location_id", "in", location_ids)]) + .search([]) .sorted(lambda w: w.view_location_id.parent_path, reverse=True) ) - for warehouse in warehouses: - if self.parent_path.startswith(warehouse.view_location_id.parent_path): - return warehouse - return warehouses.browse() + res = {} + for location in self: + wh = False + if location.parent_path: + for warehouse in warehouses: + if location.parent_path.startswith( + warehouse.view_location_id.parent_path + ): + wh = warehouse + break + res[location.id] = wh + return res + + def get_closest_warehouse(self): + """Returns closest warehouse for current location.""" + self.ensure_one() + location_and_warehouse = self._get_closest_warehouse() + warehouse = location_and_warehouse[self.id] + return warehouse or self.env["stock.warehouse"] def _get_source_location_from_route(self, route, procure_method): self.ensure_one()