Coverage for src/backend/InvenTree/order/migrations/0058_auto_20211126_1210.py: 53%
15 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1# Generated by Django 3.2.5 on 2021-11-26 12:10
3from django.db import migrations
5from order.status_codes import SalesOrderStatus
8def calculate_shipped_quantity(apps, schema_editor):
9 """
10 In migration 0057 we added a new field 'shipped' to the SalesOrderLineItem model.
12 This field is used to record the number of items shipped,
13 even if the actual stock items get deleted from the database.
15 For existing orders in the database, we calculate this as follows:
17 - If the order is "shipped" then we use the total quantity
18 - Otherwise, we use the "fulfilled" calculated quantity
20 """
22 StockItem = apps.get_model('stock', 'stockitem')
23 SalesOrderLineItem = apps.get_model('order', 'salesorderlineitem')
25 for item in SalesOrderLineItem.objects.all(): # pragma: no cover 25 ↛ 27line 25 didn't jump to line 27 because the loop on line 25 never started
27 if item.order.status == SalesOrderStatus.SHIPPED:
28 item.shipped = item.quantity
29 else:
30 # Calculate total stock quantity of items allocated to this order?
31 items = StockItem.objects.filter(
32 sales_order=item.order,
33 part=item.part
34 )
36 q = sum(item.quantity for item in items)
38 item.shipped = q
40 item.save()
43class Migration(migrations.Migration):
45 dependencies = [
46 ('order', '0057_salesorderlineitem_shipped'),
47 ]
49 operations = [
50 migrations.RunPython(
51 calculate_shipped_quantity,
52 reverse_code=migrations.RunPython.noop
53 )
54 ]