From dfc5ca9c04d73c087a45365587c5e099d3524b8d Mon Sep 17 00:00:00 2001 From: Quimxi Date: Mon, 16 Nov 2026 22:05:57 +0100 Subject: [PATCH] Record the quantity actually moved when a transfer exceeds stock (#13295) 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 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 20c688c9df..af894af38f 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -3183,6 +3183,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 9d3c8cf39b..a9e0ad1eef 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -3344,6 +3344,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')