From d85ab80f12d40c52a435cf2abebd3f0e0dbd6eec Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Wed, 15 Nov 2023 18:00:38 +0100 Subject: [PATCH] [FIX] stock: closest location Currently there is 2 behaviors: - The records is pass to the _gather function, it will order base on location complete name - The records is not pass to _gather, it will be order by id. Obviously we never want to order by id because it's not configurable and hard to undersand since id are not editable closes odoo/odoo#142237 Signed-off-by: William Henrotin (whe) --- addons/stock/models/stock_quant.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 30614e5d22f..bdac174c4a7 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -728,7 +728,7 @@ class StockQuant(models.Model): elif removal_strategy == 'lifo': return domain, 'in_date DESC, id DESC' elif removal_strategy == 'closest': - return domain, 'location_id ASC, id DESC' + return domain, False elif removal_strategy == 'least_packages': if qty > 0: return self._run_least_packages_removal_strategy_astar(domain, qty), 'in_date ASC, id' @@ -775,6 +775,8 @@ class StockQuant(models.Model): res = self.filtered_domain(domain).sorted(key=sort_key[0], reverse=sort_key[1]) else: res = self.search(domain, order=order) + if removal_strategy == "closest": + res = res.sorted(lambda q: (q.location_id.complete_name, -q.id)) return res.sorted(lambda q: not q.lot_id) def _get_available_quantity(self, product_id, location_id, lot_id=None, package_id=None, owner_id=None, strict=False, allow_negative=False):