Skip to content

Commit 295c4f3

Browse files
[bug] Serialize location (#10882) (#10883)
* Properly set location id when serializing stock * Add correct tracking entries * Add unit test (cherry picked from commit a7ff125) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent c6ecd01 commit 295c4f3

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/backend/InvenTree/stock/models.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,12 @@ def serializeStock(
19081908
data = dict(StockItem.objects.filter(pk=self.pk).values()[0])
19091909

19101910
if location:
1911-
data['location'] = location
1911+
if location.structural:
1912+
raise ValidationError({
1913+
'location': _('Cannot assign stock to structural location')
1914+
})
1915+
1916+
data['location_id'] = location.pk
19121917

19131918
# Set the parent ID correctly
19141919
data['parent'] = self
@@ -1921,7 +1926,17 @@ def serializeStock(
19211926
history_items = []
19221927

19231928
for item in items:
1924-
# Construct a tracking entry for the new StockItem
1929+
# Construct tracking entries for the new StockItem
1930+
if entry := item.add_tracking_entry(
1931+
StockHistoryCode.SPLIT_FROM_PARENT,
1932+
user,
1933+
quantity=1,
1934+
notes=notes,
1935+
location=location,
1936+
commit=False,
1937+
):
1938+
history_items.append(entry)
1939+
19251940
if entry := item.add_tracking_entry(
19261941
StockHistoryCode.ASSIGNED_SERIAL,
19271942
user,
@@ -1938,7 +1953,9 @@ def serializeStock(
19381953
StockItemTracking.objects.bulk_create(history_items)
19391954

19401955
# Remove the equivalent number of items
1941-
self.take_stock(quantity, user, notes=notes)
1956+
self.take_stock(
1957+
quantity, user, code=StockHistoryCode.STOCK_SERIZALIZED, notes=notes
1958+
)
19421959

19431960
return items
19441961

src/backend/InvenTree/stock/status_codes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class StockHistoryCode(StatusCode):
5353
STOCK_COUNT = 10, _('Stock counted')
5454
STOCK_ADD = 11, _('Stock manually added')
5555
STOCK_REMOVE = 12, _('Stock manually removed')
56+
STOCK_SERIZALIZED = 13, _('Serialized stock items')
5657

5758
RETURNED_TO_STOCK = 15, _('Returned to stock') # Stock item returned to stock
5859

src/backend/InvenTree/stock/tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,9 +1271,11 @@ def test_serialize(self):
12711271
self.assertEqual(item_1.get_children().count(), 1)
12721272
self.assertEqual(item_2.parent, item_1)
12731273

1274+
loc = StockLocation.objects.filter(structural=False).first()
1275+
12741276
# Serialize the secondary item
12751277
serials = [str(i) for i in range(20)]
1276-
items = item_2.serializeStock(20, serials)
1278+
items = item_2.serializeStock(20, serials, location=loc)
12771279

12781280
self.assertEqual(len(items), 20)
12791281
self.assertEqual(StockItem.objects.count(), N + 22)
@@ -1290,6 +1292,9 @@ def test_serialize(self):
12901292
self.assertEqual(child.parent, item_2)
12911293
self.assertGreater(child.lft, item_2.lft)
12921294
self.assertLess(child.rght, item_2.rght)
1295+
self.assertEqual(child.location, loc)
1296+
self.assertIsNotNone(child.location)
1297+
self.assertEqual(child.tracking_info.count(), 2)
12931298

12941299
# Delete item_2 : we expect that all children will be re-parented to item_1
12951300
item_2.delete()

0 commit comments

Comments
 (0)