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 000000000000..a39cef41ad92 --- /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 7eafd2032fc5..20ac6a116be6 100644 --- a/src/backend/InvenTree/order/test_migrations.py +++ b/src/backend/InvenTree/order/test_migrations.py @@ -96,7 +96,7 @@ def prepare(self): customer = Company.objects.create( name='My customer', - description='A customer we sell stuff too', + description='A customer we sell stuff to', is_customer=True, )