mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 04:25:42 +00:00
[PUI] Serialize Stock (#8192)
* Add PUI form to serialize existing stock item * Remove debug statement * Ensure that stock item trees are rebuilt correctly after serialization - No idea how this has not been detected previously * Add unit test to ensure child_items annotation works as expected * Add link to parent item in stock detail page * Refactor to use new placeholder hook
This commit is contained in:
@ -1572,6 +1572,13 @@ class StockItem(
|
||||
# Remove the equivalent number of items
|
||||
self.take_stock(quantity, user, notes=notes)
|
||||
|
||||
# Rebuild the stock tree
|
||||
try:
|
||||
StockItem.objects.partial_rebuild(tree_id=self.tree_id)
|
||||
except Exception:
|
||||
logger.warning('Failed to rebuild stock tree during serializeStock')
|
||||
StockItem.objects.rebuild()
|
||||
|
||||
@transaction.atomic
|
||||
def copyHistoryFrom(self, other):
|
||||
"""Copy stock history from another StockItem."""
|
||||
@ -1806,7 +1813,7 @@ class StockItem(
|
||||
for tree_id in tree_ids:
|
||||
StockItem.objects.partial_rebuild(tree_id=tree_id)
|
||||
except Exception:
|
||||
logger.warning('Rebuilding entire StockItem tree')
|
||||
logger.warning('Rebuilding entire StockItem tree during merge_stock_items')
|
||||
StockItem.objects.rebuild()
|
||||
|
||||
@transaction.atomic
|
||||
|
@ -925,6 +925,46 @@ class StockItemListTest(StockAPITestCase):
|
||||
self.list_url, {'location_detail': True, 'tests': True}, max_query_count=35
|
||||
)
|
||||
|
||||
def test_child_items(self):
|
||||
"""Test that the 'child_items' annotation works as expected."""
|
||||
# Create a trackable part
|
||||
my_part = Part.objects.create(
|
||||
name='Test Part', description='Test Part Description', trackable=True
|
||||
)
|
||||
|
||||
# Create an initial stock item
|
||||
parent_item = StockItem.objects.create(
|
||||
part=my_part, quantity=10, location=StockLocation.objects.first()
|
||||
)
|
||||
|
||||
# Serialize this stock item
|
||||
parent_item.serializeStock(
|
||||
5, [1, 2, 3, 4, 5], user=self.user, notes='Some notes'
|
||||
)
|
||||
|
||||
parent_item.refresh_from_db()
|
||||
|
||||
# Check that the parent item has 5 child items
|
||||
self.assertEqual(parent_item.get_descendants(include_self=False).count(), 5)
|
||||
self.assertEqual(my_part.stock_items.count(), 6)
|
||||
|
||||
# Fetch stock list via API
|
||||
response = self.get(reverse('api-stock-list'), {'part': my_part.pk})
|
||||
|
||||
self.assertEqual(len(response.data), 6)
|
||||
|
||||
# Fetch stock detail
|
||||
response = self.get(reverse('api-stock-detail', kwargs={'pk': parent_item.pk}))
|
||||
|
||||
self.assertEqual(response.data['child_items'], 5)
|
||||
|
||||
for child in parent_item.get_children():
|
||||
response = self.get(reverse('api-stock-detail', kwargs={'pk': child.pk}))
|
||||
|
||||
self.assertEqual(response.data['parent'], parent_item.pk)
|
||||
self.assertEqual(response.data['quantity'], 1)
|
||||
self.assertEqual(response.data['child_items'], 0)
|
||||
|
||||
|
||||
class CustomStockItemStatusTest(StockAPITestCase):
|
||||
"""Tests for custom stock item statuses."""
|
||||
|
Reference in New Issue
Block a user