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 <you@example.com>
Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
authored and GitHub committed 2026-11-24 13:45:30 +00:00
1 parent 54276081cc
commit 939df605f4
4 files changed
+82 -1

No files matched your search

+4 -1
View File
@@ -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,
@@ -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()."""
@@ -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)
+35
View File
@@ -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."""