diff --git a/src/backend/InvenTree/build/models.py b/src/backend/InvenTree/build/models.py index b23d9a35f0..cd165e7693 100644 --- a/src/backend/InvenTree/build/models.py +++ b/src/backend/InvenTree/build/models.py @@ -2469,8 +2469,16 @@ class BuildItem(InvenTree.models.InvenTreeMetadataModel): sales_allocation_count = decimal.Decimal( self.stock_item.sales_order_allocation_count() ) + transfer_allocation_count = decimal.Decimal( + self.stock_item.transfer_order_allocation_count() + ) - total_allocation = build_allocation_count + sales_allocation_count + quantity + total_allocation = ( + build_allocation_count + + sales_allocation_count + + transfer_allocation_count + + quantity + ) if total_allocation > available: error = {'quantity': _('Stock item is over-allocated')} diff --git a/src/backend/InvenTree/build/test_build.py b/src/backend/InvenTree/build/test_build.py index 5c9091f9f5..fc6fae8198 100644 --- a/src/backend/InvenTree/build/test_build.py +++ b/src/backend/InvenTree/build/test_build.py @@ -33,7 +33,13 @@ from InvenTree.unit_test import ( findOffloadedTask, stock_history_count, ) -from order.models import PurchaseOrder, PurchaseOrderLineItem +from order.models import ( + PurchaseOrder, + PurchaseOrderLineItem, + TransferOrder, + TransferOrderAllocation, + TransferOrderLineItem, +) from part.models import BomItem, BomItemSubstitute, Part, PartTestTemplate from stock.models import StockItem, StockItemTestResult, StockLocation from stock.status_codes import StockStatus @@ -352,6 +358,21 @@ class BuildTest(BuildTestBase): with self.assertRaises(ValidationError): b.clean() + # Stock which is fully allocated to a transfer order is not available + item = StockItem.objects.create(part=self.sub_part_1, quantity=10) + to = TransferOrder.objects.create(reference='TO-9999') + to_line = TransferOrderLineItem.objects.create( + order=to, part=self.sub_part_1, quantity=10 + ) + TransferOrderAllocation.objects.create(line=to_line, item=item, quantity=10) + + b = BuildItem(stock_item=item, build_line=self.line_1, quantity=10) + + with self.assertRaises(ValidationError) as err: + b.clean() + + self.assertIn('Stock item is over-allocated', str(err.exception)) + # Ok, what about we make one that does *not* fail? b = BuildItem( stock_item=self.stock_1_2,