Coverage for src/backend/InvenTree/order/migrations/0055_auto_20211025_0645.py: 33%

34 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-10-25 06:45 

2 

3from django.db import migrations 

4 

5 

6from order.status_codes import SalesOrderStatus 

7 

8 

9def add_shipment(apps, schema_editor): 

10 """ 

11 Create a SalesOrderShipment for each existing SalesOrder instance. 

12 

13 Any "allocations" are marked against that shipment. 

14 

15 For each existing SalesOrder instance, we create a default SalesOrderShipment, 

16 and associate each SalesOrderAllocation with this shipment 

17 """ 

18 

19 Allocation = apps.get_model('order', 'salesorderallocation') 

20 SalesOrder = apps.get_model('order', 'salesorder') 

21 Shipment = apps.get_model('order', 'salesordershipment') 

22 

23 n = 0 

24 

25 for order in SalesOrder.objects.all(): 25 ↛ 27line 25 didn't jump to line 27 because the loop on line 25 never started

26 

27 """ 

28 We only create an automatic shipment for "PENDING" orders, 

29 as SalesOrderAllocations were historically deleted for "SHIPPED" or "CANCELLED" orders 

30 """ 

31 

32 allocations = Allocation.objects.filter( 

33 line__order=order 

34 ) 

35 

36 if allocations.count() == 0 and order.status != SalesOrderStatus.PENDING: # pragma: no cover 

37 continue 

38 

39 # Create a new Shipment instance against this order 

40 shipment = Shipment.objects.create( 

41 order=order, 

42 ) 

43 

44 if order.status == SalesOrderStatus.SHIPPED: # pragma: no cover 

45 shipment.shipment_date = order.shipment_date 

46 

47 shipment.save() 

48 

49 # Iterate through each allocation associated with this order 

50 for allocation in allocations: # pragma: no cover 

51 allocation.shipment = shipment 

52 allocation.save() 

53 

54 n += 1 

55 

56 if n > 0: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true

57 print(f"\nCreated SalesOrderShipment for {n} SalesOrder instances") 

58 

59 

60def reverse_add_shipment(apps, schema_editor): # pragma: no cover 

61 """ 

62 Reverse the migration, delete and SalesOrderShipment instances 

63 """ 

64 

65 Allocation = apps.get_model('order', 'salesorderallocation') 

66 

67 # First, ensure that all SalesOrderAllocation objects point to a null shipment 

68 for allocation in Allocation.objects.exclude(shipment=None): 

69 allocation.shipment = None 

70 allocation.save() 

71 

72 SOS = apps.get_model('order', 'salesordershipment') 

73 

74 n = SOS.objects.count() 

75 

76 print(f"Deleting {n} SalesOrderShipment instances") 

77 

78 SOS.objects.all().delete() 

79 

80 

81class Migration(migrations.Migration): 

82 

83 dependencies = [ 

84 ('order', '0054_salesorderallocation_shipment'), 

85 ] 

86 

87 operations = [ 

88 migrations.RunPython( 

89 add_shipment, 

90 reverse_code=reverse_add_shipment, 

91 ) 

92 ]