fix: Prevent applying internal location restrictions when the source location is external or transit.

This commit is contained in:
Suherdy Yacob 2026-03-05 13:41:56 +07:00
parent 79818cc017
commit 7737a32459

View File

@ -15,6 +15,16 @@ class StockQuant(models.Model):
def _get_allowed_locations(self): def _get_allowed_locations(self):
"""Helper to extract allowed location IDs from context""" """Helper to extract allowed location IDs from context"""
ctx = self.env.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 = [] allowed_location_ids = []
# 1. Try from explicit keys often passed by UI or patches # 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: if mo.exists() and mo.allowed_source_location_ids:
allowed_location_ids = mo.allowed_source_location_ids.ids allowed_location_ids = mo.allowed_source_location_ids.ids
# 4. Fallback to picking type in context
# 4. Fallback to picking type in context # 4. Fallback to picking type in context
if not allowed_location_ids: if not allowed_location_ids:
picking_type_id = ctx.get('default_picking_type_id') 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): 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)""" """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) 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() allowed_location_ids = self._get_allowed_locations()
if allowed_location_ids: if allowed_location_ids:
result_domain = Domain.AND([result_domain, [('location_id', 'in', allowed_location_ids)]]) result_domain = Domain.AND([result_domain, [('location_id', 'in', allowed_location_ids)]])