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

Fix stocktake bug for counting serialized items (#12280) (#12282)

* Fix stocktake bug for counting serialized items

* Add unit test

(cherry picked from commit 414aac0224)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot]
2026-06-30 17:09:23 +10:00
committed by GitHub
parent ffc60cb189
commit 9793bba77a
2 changed files with 34 additions and 8 deletions
+3 -3
View File
@@ -2651,8 +2651,8 @@ class StockItem(
)
tracking_info['old_status_logical'] = old_status_logical
if self.updateQuantity(count):
tracking_info['quantity'] = float(count)
if self.serialized or self.updateQuantity(count):
tracking_info['quantity'] = 1 if self.serialized else float(count)
self.stocktake_date = InvenTree.helpers.current_date()
self.stocktake_user = user
@@ -2676,7 +2676,7 @@ class StockItem(
StockEvents.ITEM_COUNTED,
'stockitem.counted',
id=self.id,
quantity=float(self.quantity),
quantity=1 if self.serialized else float(self.quantity),
)
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]['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):
"""Test stock transfers."""
stock_item = StockItem.objects.get(pk=1234)