From 3554429baa91a1d72b6743ae7af4fa23e8f4249b Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 11 Dec 2024 07:52:18 +1100 Subject: [PATCH] SalesOrder Data Migration (#8585) * Add data migration to remedy bug * Add migration tests * Remove faulty migration test * Tweak filter * Update migration file --------- Co-authored-by: Matthias Mair --- .../migrations/0105_auto_20241128_0431.py | 52 +++++++++++++++++++ .../InvenTree/order/test_migrations.py | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py diff --git a/src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py b/src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py new file mode 100644 index 0000000000..a39cef41ad --- /dev/null +++ b/src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py @@ -0,0 +1,52 @@ +# Generated by Django 4.2.16 on 2024-11-28 04:31 + +from django.db import migrations + + +def update_shipment_date(apps, schema_editor): + """ + Update the shipment date for existing SalesOrderAllocation objects + """ + + from order.status_codes import SalesOrderStatusGroups + + SalesOrder = apps.get_model('order', 'SalesOrder') + + # Find any orders which are "complete" but missing a shipment date + orders = SalesOrder.objects.filter( + status__in=SalesOrderStatusGroups.COMPLETE, + shipment_date__isnull=True + ) + + updated_orders = 0 + + for order in orders: + # Find the latest shipment date for any associated allocations + shipments = order.shipments.filter(shipment_date__isnull=False) + latest_shipment = shipments.order_by('-shipment_date').first() + + if not latest_shipment: + continue + + # Update the order with the new shipment date + order.shipment_date = latest_shipment.shipment_date + order.save() + + updated_orders += 1 + + if updated_orders > 0: + print(f"Updated {updated_orders} SalesOrder objects with missing shipment_date") + + +class Migration(migrations.Migration): + + dependencies = [ + ('order', '0104_alter_returnorderlineitem_quantity'), + ] + + operations = [ + migrations.RunPython( + update_shipment_date, + reverse_code=migrations.RunPython.noop + ) + ] diff --git a/src/backend/InvenTree/order/test_migrations.py b/src/backend/InvenTree/order/test_migrations.py index 7eafd2032f..20ac6a116b 100644 --- a/src/backend/InvenTree/order/test_migrations.py +++ b/src/backend/InvenTree/order/test_migrations.py @@ -96,7 +96,7 @@ class TestShipmentMigration(MigratorTestCase): customer = Company.objects.create( name='My customer', - description='A customer we sell stuff too', + description='A customer we sell stuff to', is_customer=True, )