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

1# Generated by Django 3.2.5 on 2021-11-26 12:10 

2 

3from django.db import migrations 

4 

5from order.status_codes import SalesOrderStatus 

6 

7 

8def calculate_shipped_quantity(apps, schema_editor): 

9 """ 

10 In migration 0057 we added a new field 'shipped' to the SalesOrderLineItem model. 

11 

12 This field is used to record the number of items shipped, 

13 even if the actual stock items get deleted from the database. 

14 

15 For existing orders in the database, we calculate this as follows: 

16 

17 - If the order is "shipped" then we use the total quantity 

18 - Otherwise, we use the "fulfilled" calculated quantity 

19 

20 """ 

21 

22 StockItem = apps.get_model('stock', 'stockitem') 

23 SalesOrderLineItem = apps.get_model('order', 'salesorderlineitem') 

24 

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

26 

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 ) 

35 

36 q = sum(item.quantity for item in items) 

37 

38 item.shipped = q 

39 

40 item.save() 

41 

42 

43class Migration(migrations.Migration): 

44 

45 dependencies = [ 

46 ('order', '0057_salesorderlineitem_shipped'), 

47 ] 

48 

49 operations = [ 

50 migrations.RunPython( 

51 calculate_shipped_quantity, 

52 reverse_code=migrations.RunPython.noop 

53 ) 

54 ]