2
0
mirror of https://github.com/inventree/InvenTree.git synced 2026-07-04 06:00:38 +00:00

Fix stocktake bug for counting serialized items (#12280)

* Fix stocktake bug for counting serialized items

* Add unit test
This commit is contained in:
Oliver
2026-06-30 15:39:05 +10:00
committed by GitHub
parent 635ea8179e
commit 414aac0224
2 changed files with 34 additions and 8 deletions
+3 -3
View File
@@ -2706,8 +2706,8 @@ class StockItem(
) )
tracking_info['old_status_logical'] = old_status_logical tracking_info['old_status_logical'] = old_status_logical
if self.updateQuantity(count): if self.serialized or self.updateQuantity(count):
tracking_info['quantity'] = float(count) tracking_info['quantity'] = 1 if self.serialized else float(count)
self.stocktake_date = InvenTree.helpers.current_date() self.stocktake_date = InvenTree.helpers.current_date()
self.stocktake_user = user self.stocktake_user = user
@@ -2731,7 +2731,7 @@ class StockItem(
StockEvents.ITEM_COUNTED, StockEvents.ITEM_COUNTED,
'stockitem.counted', 'stockitem.counted',
id=self.id, id=self.id,
quantity=float(self.quantity), quantity=1 if self.serialized else float(self.quantity),
) )
return True return True
+26
View File
@@ -2289,6 +2289,32 @@ class StocktakeTest(StockAPITestCase):
self.assertEqual(response.data['items'][0]['pk'], 1234) self.assertEqual(response.data['items'][0]['pk'], 1234)
self.assertEqual(response.data['items'][0]['quantity'], target[endpoint]) self.assertEqual(response.data['items'][0]['quantity'], target[endpoint])
def test_count_serialized(self):
"""Test that counting a serialized stock item correctly updates stocktake_date."""
import datetime
# Fixture item pk=501 is a serialized item (serial=1)
item = StockItem.objects.get(pk=501)
self.assertTrue(item.serialized)
self.assertEqual(item.quantity, 1)
# Clear any existing stocktake date so we can verify it gets set
item.stocktake_date = None
item.save()
url = reverse('api-stock-count')
# Count the serialized item — quantity must be 1
data = {'items': [{'pk': item.pk, 'quantity': 1}]}
response = self.post(url, data, expected_code=201)
self.assertEqual(response.data['items'][0]['pk'], item.pk)
self.assertEqual(response.data['items'][0]['quantity'], '1.00000')
# stocktake_date must have been set to today
item.refresh_from_db()
self.assertEqual(item.stocktake_date, datetime.date.today())
def test_transfer(self): def test_transfer(self):
"""Test stock transfers.""" """Test stock transfers."""
stock_item = StockItem.objects.get(pk=1234) stock_item = StockItem.objects.get(pk=1234)