diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index 6523d12e31..ad5ea02e11 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -2193,13 +2193,28 @@ class StockRemoveSerializer(StockAdjustmentSerializer): write_batched(), batch_offload_tasks(), ): - for item in data['items']: + # Ignore items with no stock to remove + items = [ + item + for item in data['items'] + if item['quantity'] is not None and item['quantity'] > 0 + ] + + # Lock each row first (items are already sorted by pk - deadlock avoidance), + # so that the allocation check below sees the current committed quantity + items = [item for item in items if item['pk'].lock_quantity()] + + # Fetch allocated quantities for all items at once (3 queries in total), + # rather than calling allocation_count() for each item (3 queries per item) + allocated = StockItem.bulk_allocation_count([item['pk'] for item in items]) + + for item in items: stock_item = item['pk'] quantity = item['quantity'] - # Ignore in this case - no stock to remove - if quantity is None or quantity <= 0: - continue + validate_allocation_boundary( + quantity, stock_item.quantity, allocated.get(stock_item.pk, 0) + ) # Optional fields extra = {} @@ -2208,13 +2223,6 @@ 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)