From cdd977abaa9d4765a9b4ca2a1895480aa250cb27 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 3 Jul 2025 14:25:02 +1000 Subject: [PATCH] Tweak "auto_allocate" serial numbers (#9942) - Allow "in production" items - Enables allocation of child builds --- src/backend/InvenTree/build/models.py | 24 +++++++++++++++++------- src/backend/InvenTree/stock/models.py | 11 +++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/backend/InvenTree/build/models.py b/src/backend/InvenTree/build/models.py index 1a81e1cb03..fd171e3d4a 100644 --- a/src/backend/InvenTree/build/models.py +++ b/src/backend/InvenTree/build/models.py @@ -938,14 +938,24 @@ class Build( for bom_item in trackable_parts: valid_part_ids = valid_parts.get(bom_item.pk, []) - items = stock.models.StockItem.objects.filter( - part__pk__in=valid_part_ids, - serial=output.serial, - quantity=1, - ).filter(stock.models.StockItem.IN_STOCK_FILTER) + # Find all matching stock items, based on serial number + stock_items = list( + stock.models.StockItem.objects.filter( + part__pk__in=valid_part_ids, + serial=output.serial, + quantity=1, + ) + ) - if items.exists() and items.count() == 1: - stock_item = items[0] + # Filter stock items to only those which are in stock + # Note that we can accept "in production" items here + available_items = filter( + lambda item: item.is_in_stock(check_in_production=False), + stock_items, + ) + + if len(available_items) == 1: + stock_item = available_items[0] # Find the 'BuildLine' object which points to this BomItem try: diff --git a/src/backend/InvenTree/stock/models.py b/src/backend/InvenTree/stock/models.py index 749dc81d1e..699aac8629 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -1592,13 +1592,17 @@ class StockItem( return self.children.count() def is_in_stock( - self, check_status: bool = True, check_quantity: bool = True + self, + check_status: bool = True, + check_quantity: bool = True, + check_in_production: bool = True, ) -> bool: """Return True if this StockItem is "in stock". - Args: + Arguments: check_status: If True, check the status of the StockItem. Defaults to True. check_quantity: If True, check the quantity of the StockItem. Defaults to True. + check_in_production: If True, check if the item is in production. Defaults to True. """ if check_status and self.status not in StockStatusGroups.AVAILABLE_CODES: return False @@ -1606,6 +1610,9 @@ class StockItem( if check_quantity and self.quantity <= 0: return False + if check_in_production and self.is_building: + return False + return all([ self.sales_order is None, # Not assigned to a SalesOrder self.belongs_to is None, # Not installed inside another StockItem