2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-01 04:56:45 +00:00
InvenTree/InvenTree/stock/migrations/0061_auto_20210511_0911.py

118 lines
3.3 KiB
Python

# Generated by Django 3.2 on 2021-05-10 23:11
from django.db import migrations
from InvenTree.status_codes import StockHistoryCode
def update_history(apps, schema_editor):
"""
Update each existing StockItemTracking object,
convert the recorded "quantity" to a delta
"""
StockItem = apps.get_model('stock', 'stockitem')
StockItemTracking = apps.get_model('stock', 'stockitemtracking')
update_count = 0
for item in StockItem.objects.all():
history = StockItemTracking.objects.filter(item=item).order_by('date')
if history.count() == 0:
continue
quantity = history[0].quantity
for entry in history:
updated = False
q = entry.quantity
if not q == quantity:
entry.deltas = {
'quantity': float(q),
}
updated = True
quantity = q
# Try to "guess" the "type" of tracking entry, based on the title
title = entry.title.lower()
tracking_type = None
if 'completed build' in title:
tracking_type = StockHistoryCode.BUILD_OUTPUT_COMPLETED
elif 'removed' in title and 'item' in title:
tracking_type = StockHistoryCode.STOCK_REMOVE
elif 'split from existing' in title:
tracking_type = StockHistoryCode.SPLIT_FROM_PARENT
elif 'moved to' in title:
tracking_type = StockHistoryCode.STOCK_MOVE
elif 'created stock item' in title:
tracking_type = StockHistoryCode.CREATED
elif 'add serial number' in title:
tracking_type = StockHistoryCode.ASSIGNED_SERIAL
elif 'returned from customer' in title:
tracking_type = StockHistoryCode.RETURNED_FROM_CUSTOMER
elif 'counted' in title:
tracking_type = StockHistoryCode.STOCK_COUNT
elif 'added' in title:
tracking_type = StockHistoryCode.STOCK_ADD
elif 'assigned to customer' in title:
tracking_type = StockHistoryCode.SENT_TO_CUSTOMER
elif 'installed into stock item' in title:
tracking_type = StockHistoryCode.INSTALLED_INTO_ASSEMBLY
elif 'uninstalled into location' in title:
tracking_type = StockHistoryCode.REMOVED_FROM_ASSEMBLY
elif 'installed stock item' in title:
tracking_type = StockHistoryCode.INSTALLED_CHILD_ITEM
elif 'received items' in title:
tracking_type = StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER
if tracking_type is not None:
entry.tracking_type = tracking_type
updated = True
if updated:
entry.save()
update_count += 1
print(f"\n==========================\nUpdated {update_count} StockItemHistory entries")
def reverse_update(apps, schema_editor):
"""
"""
pass
class Migration(migrations.Migration):
dependencies = [
('stock', '0060_auto_20210511_1713'),
]
operations = [
migrations.RunPython(update_history, reverse_code=reverse_update)
]