From 670a88722fb746b8e9a3a51e8616acd4502fd718 Mon Sep 17 00:00:00 2001 From: Michael Tietz Date: Thu, 18 Jan 2024 15:30:14 +0100 Subject: [PATCH] [IMP] stock_helper: Getting closest warehouse for record set --- stock_helper/models/stock_location.py | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/stock_helper/models/stock_location.py b/stock_helper/models/stock_location.py index 03ad17023dcf..e335c40ebd76 100644 --- a/stock_helper/models/stock_location.py +++ b/stock_helper/models/stock_location.py @@ -20,8 +20,8 @@ def is_sublocation_of(self, others, func=any): # 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 @@ def get_closest_warehouse(self): 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()