Record the quantity actually moved when a transfer exceeds stock (#13295) (#13300)

Requesting a transfer quantity larger than the stock item quantity moves
the entire item, but the tracking entry recorded the requested quantity.
Clamp the quantity to the item quantity before recording the event.

Fixes #13259


(cherry picked from commit dfc5ca9c04)

Co-authored-by: Quimxi <quimxi6@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-17 09:07:22 +11:00
1 parent 3701f52387
commit de4ce76cac
2 files changed
+42

No files matched your search

+4
View File
@@ -3177,6 +3177,10 @@ class StockItem(
return new_stock is not None
# Requesting more than the available quantity moves the entire item,
# so record the quantity which is actually moved
quantity = min(quantity, Decimal(self.quantity))
# Moving into the same location triggers a different history code
same_location = location == self.location
+38
View File
@@ -3138,6 +3138,44 @@ class StocktakeTest(StockAPITestCase):
status_code=status.HTTP_400_BAD_REQUEST,
)
def test_transfer_more_than_available(self):
"""Transferring more than the available quantity moves the entire item.
The tracking entry must record the quantity which was actually moved,
not the (larger) quantity which was requested.
"""
item = StockItem.objects.create(
part=Part.objects.get(pk=1),
location=StockLocation.objects.get(pk=5),
quantity=10,
)
url = reverse('api-stock-transfer')
# Move to a different location
self.post(
url,
{'items': [{'pk': item.pk, 'quantity': 25}], 'location': 1},
expected_code=201,
)
item.refresh_from_db()
self.assertEqual(item.location.pk, 1)
self.assertEqual(item.quantity, 10)
entry = stock_events(item, event_type=StockHistoryCode.STOCK_MOVE).last()
self.assertEqual(entry.additional_data['deltas']['quantity'], 10)
# "Move" into the same location (records a stock update)
self.post(
url,
{'items': [{'pk': item.pk, 'quantity': 25}], 'location': 1},
expected_code=201,
)
entry = stock_events(item, event_type=StockHistoryCode.STOCK_UPDATE).last()
self.assertEqual(entry.additional_data['deltas']['quantity'], 10)
def test_count_with_location(self):
"""Test that the stock count endpoint correctly handles the optional location field."""
url = reverse('api-stock-count')