[IMP] stock_helper: Getting closest warehouse for record set

This commit is contained in:
Michael Tietz
2024-01-18 15:30:14 +01:00
parent 6724f4126d
commit 670a88722f

View File

@@ -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()