From 939df605f43db4998bf67fb7f5d5d22d65762e9e Mon Sep 17 00:00:00 2001 From: mohamadpykarian Date: Tue, 24 Nov 2026 17:15:30 +0330 Subject: [PATCH] Prevent shipping and removing allocated stock (#13298) * Prevent shipping and removing allocated stock * fix style * Use shared allocation validation for stock removal * test: cover shipment with zero allocated stock --------- Co-authored-by: mamad Co-authored-by: Oliver Co-authored-by: Matthias Mair --- src/backend/InvenTree/order/models.py | 5 ++- .../InvenTree/order/test_sales_order.py | 36 +++++++++++++++++++ src/backend/InvenTree/stock/serializers.py | 7 ++++ src/backend/InvenTree/stock/test_api.py | 35 ++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/backend/InvenTree/order/models.py b/src/backend/InvenTree/order/models.py index 7b2b94fedc..daddf0f222 100644 --- a/src/backend/InvenTree/order/models.py +++ b/src/backend/InvenTree/order/models.py @@ -3043,7 +3043,6 @@ class SalesOrderShipment( for allocation in allocations: stock_item = seen_stock_items.get(allocation.item_id) or allocation.item - quantity = allocation.quantity if (locked_qty := locked_quantities.pop(stock_item.pk, None)) is not None: # First encounter: refresh the quantity from the locked database row @@ -3053,6 +3052,10 @@ class SalesOrderShipment( continue seen_stock_items[stock_item.pk] = stock_item + quantity = min(allocation.quantity, stock_item.quantity) + + if quantity <= 0: + continue if quantity < stock_item.quantity: # Split off exactly the shipped quantity into a new StockItem, diff --git a/src/backend/InvenTree/order/test_sales_order.py b/src/backend/InvenTree/order/test_sales_order.py index 4186ebcccd..3c71f51321 100644 --- a/src/backend/InvenTree/order/test_sales_order.py +++ b/src/backend/InvenTree/order/test_sales_order.py @@ -1003,6 +1003,42 @@ class SalesOrderTest(InvenTreeAPITestCase): for line in so.lines.all(): self.assertEqual(line.shipped, line.quantity) + def test_complete_shipment_with_reduced_allocated_stock(self): + """Shipment must not count more stock than actually exists.""" + stock_item = StockItem.objects.create(part=self.part, quantity=10) + + SalesOrderAllocation.objects.create( + line=self.line, shipment=self.shipment, item=stock_item, quantity=10 + ) + + # Simulate stock being reduced while the allocation remains unchanged + StockItem.objects.filter(pk=stock_item.pk).update(quantity=2) + + self.shipment.complete_allocations( + self.shipment.allocations.all(), user=self.user + ) + + self.line.refresh_from_db() + self.assertEqual(self.line.shipped, 2) + + def test_complete_shipment_with_zero_allocated_stock(self): + """Shipment must skip allocations when no stock remains.""" + stock_item = StockItem.objects.create(part=self.part, quantity=10) + + SalesOrderAllocation.objects.create( + line=self.line, shipment=self.shipment, item=stock_item, quantity=10 + ) + + # Simulate stock being reduced to zero while the allocation remains + StockItem.objects.filter(pk=stock_item.pk).update(quantity=0) + + self.shipment.complete_allocations( + self.shipment.allocations.all(), user=self.user + ) + + self.line.refresh_from_db() + self.assertEqual(self.line.shipped, 0) + class SalesOrderAutoAllocateTest(InvenTreeTestCase): """Tests for SalesOrder.auto_allocate_stock().""" diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index 40d3a7128a..6523d12e31 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -2208,6 +2208,13 @@ class StockRemoveSerializer(StockAdjustmentSerializer): if field_value := item.get(field_name, None): extra[field_name] = field_value + if not stock_item.lock_quantity(): + continue + + validate_allocation_boundary( + quantity, stock_item.quantity, stock_item.allocation_count() + ) + stock_item.take_stock(quantity, request.user, notes=notes, **extra) diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index a2dadafb74..184ca7460f 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -4246,6 +4246,41 @@ class StockAdjustmentCustomStatusTest(StockAPITestCase): item.refresh_from_db() self.assertEqual(item.status_custom_key, self.custom_status.key) + def test_remove_allocated_stock_rejected(self): + """Stock removal must not reduce quantity below active allocations.""" + item = StockItem.objects.create( + part=self.part, location=self.source, quantity=10 + ) + + customer = company.models.Company.objects.create( + name='Allocation test customer', is_customer=True + ) + sales_order = order.models.SalesOrder.objects.create( + customer=customer, reference='SO-ALLOCATION-TEST' + ) + shipment = order.models.SalesOrderShipment.objects.create( + order=sales_order, reference='SHIP-ALLOCATION-TEST' + ) + line = order.models.SalesOrderLineItem.objects.create( + order=sales_order, part=self.part, quantity=10 + ) + + order.models.SalesOrderAllocation.objects.create( + line=line, shipment=shipment, item=item, quantity=10 + ) + + self.post( + reverse('api-stock-remove'), + { + 'items': [{'pk': item.pk, 'quantity': 2}], + 'notes': 'Remove allocated stock', + }, + expected_code=400, + ) + + item.refresh_from_db() + self.assertEqual(item.quantity, 10) + class StockTransferMergeTest(StockAPITestCase): """Tests for optional merge-on-transfer behavior."""