From de4ce76cac469cfdb98d8859641ed0d02754bf8a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:07:22 +1100 Subject: [PATCH] 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 dfc5ca9c04d73c087a45365587c5e099d3524b8d) Co-authored-by: Quimxi Co-authored-by: Claude Opus 5.5 --- src/backend/InvenTree/stock/models.py | 4 +++ src/backend/InvenTree/stock/test_api.py | 38 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/backend/InvenTree/stock/models.py b/src/backend/InvenTree/stock/models.py index 5ba0db69d1..6ef16a7821 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -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 diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index a2b3ddea13..be9c284c0c 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -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')