From 7737a32459a0c127311e0d701245395b4898ad83 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Thu, 5 Mar 2026 13:41:56 +0700 Subject: [PATCH] fix: Prevent applying internal location restrictions when the source location is external or transit. --- models/stock_location.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/models/stock_location.py b/models/stock_location.py index 6132e88..c4a14f5 100644 --- a/models/stock_location.py +++ b/models/stock_location.py @@ -15,6 +15,16 @@ class StockQuant(models.Model): def _get_allowed_locations(self): """Helper to extract allowed location IDs from context""" ctx = self.env.context + + # FIX: If we are picking from a non-internal location (like Transit, Supplier, or Customer), + # we should NOT apply strict internal location restrictions, because the stock MUST come + # from that exact external/transit location. + loc_id = ctx.get('default_location_id') + if loc_id: + loc = self.env['stock.location'].sudo().browse(loc_id) + if loc.exists() and loc.usage != 'internal': + return [] + allowed_location_ids = [] # 1. Try from explicit keys often passed by UI or patches @@ -45,6 +55,7 @@ class StockQuant(models.Model): if mo.exists() and mo.allowed_source_location_ids: allowed_location_ids = mo.allowed_source_location_ids.ids + # 4. Fallback to picking type in context # 4. Fallback to picking type in context if not allowed_location_ids: picking_type_id = ctx.get('default_picking_type_id') @@ -59,6 +70,11 @@ class StockQuant(models.Model): def _get_gather_domain(self, product_id, location_id, lot_id=None, package_id=None, owner_id=None, strict=False): """Override to apply location restrictions during reservation (gather)""" result_domain = super()._get_gather_domain(product_id, location_id, lot_id, package_id, owner_id, strict) + + # FIX: If reserving from a non-internal location, DO NOT apply internal restrictions. + if location_id and location_id.usage != 'internal': + return result_domain + allowed_location_ids = self._get_allowed_locations() if allowed_location_ids: result_domain = Domain.AND([result_domain, [('location_id', 'in', allowed_location_ids)]])