2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-20 03:03:30 +00:00

[bug] Serialize location (#10882) (#10883)

* Properly set location id when serializing stock

* Add correct tracking entries

* Add unit test

(cherry picked from commit a7ff1250ba)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot]
2025-11-22 13:44:48 +11:00
committed by GitHub
parent c6ecd019dc
commit 295c4f3e5d
3 changed files with 27 additions and 4 deletions

View File

@@ -1908,7 +1908,12 @@ class StockItem(
data = dict(StockItem.objects.filter(pk=self.pk).values()[0])
if location:
data['location'] = location
if location.structural:
raise ValidationError({
'location': _('Cannot assign stock to structural location')
})
data['location_id'] = location.pk
# Set the parent ID correctly
data['parent'] = self
@@ -1921,7 +1926,17 @@ class StockItem(
history_items = []
for item in items:
# Construct a tracking entry for the new StockItem
# Construct tracking entries for the new StockItem
if entry := item.add_tracking_entry(
StockHistoryCode.SPLIT_FROM_PARENT,
user,
quantity=1,
notes=notes,
location=location,
commit=False,
):
history_items.append(entry)
if entry := item.add_tracking_entry(
StockHistoryCode.ASSIGNED_SERIAL,
user,
@@ -1938,7 +1953,9 @@ class StockItem(
StockItemTracking.objects.bulk_create(history_items)
# Remove the equivalent number of items
self.take_stock(quantity, user, notes=notes)
self.take_stock(
quantity, user, code=StockHistoryCode.STOCK_SERIZALIZED, notes=notes
)
return items

View File

@@ -53,6 +53,7 @@ class StockHistoryCode(StatusCode):
STOCK_COUNT = 10, _('Stock counted')
STOCK_ADD = 11, _('Stock manually added')
STOCK_REMOVE = 12, _('Stock manually removed')
STOCK_SERIZALIZED = 13, _('Serialized stock items')
RETURNED_TO_STOCK = 15, _('Returned to stock') # Stock item returned to stock

View File

@@ -1271,9 +1271,11 @@ class StockTreeTest(StockTestBase):
self.assertEqual(item_1.get_children().count(), 1)
self.assertEqual(item_2.parent, item_1)
loc = StockLocation.objects.filter(structural=False).first()
# Serialize the secondary item
serials = [str(i) for i in range(20)]
items = item_2.serializeStock(20, serials)
items = item_2.serializeStock(20, serials, location=loc)
self.assertEqual(len(items), 20)
self.assertEqual(StockItem.objects.count(), N + 22)
@@ -1290,6 +1292,9 @@ class StockTreeTest(StockTestBase):
self.assertEqual(child.parent, item_2)
self.assertGreater(child.lft, item_2.lft)
self.assertLess(child.rght, item_2.rght)
self.assertEqual(child.location, loc)
self.assertIsNotNone(child.location)
self.assertEqual(child.tracking_info.count(), 2)
# Delete item_2 : we expect that all children will be re-parented to item_1
item_2.delete()