Skip to content

Commit

Permalink
Merge PR #1917 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by jbaudoux
  • Loading branch information
OCA-git-bot committed Oct 7, 2024
2 parents e68f225 + 670a887 commit fd74e44
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions stock_helper/models/stock_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down

0 comments on commit fd74e44

Please sign in to comment.