Coverage for src/backend/InvenTree/order/migrations/0079_auto_20230304_0904.py: 31%

72 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1# Generated by Django 3.2.18 on 2023-03-04 09:04 

2 

3import logging 

4 

5from django.db import migrations 

6 

7from djmoney.contrib.exchange.exceptions import MissingRate 

8from djmoney.contrib.exchange.models import convert_money 

9from djmoney.money import Money 

10 

11from common.currency import currency_code_default 

12 

13 

14logger = logging.getLogger('inventree') 

15 

16 

17def update_purchase_order_price(apps, schema_editor): 

18 """Calculate 'total_price' field for each PurchaseOrder""" 

19 

20 PurchaseOrder = apps.get_model('order', 'purchaseorder') 

21 

22 currency = currency_code_default() 

23 

24 valid_count = 0 

25 invalid_count = 0 

26 

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

28 

29 valid = True 

30 

31 total_price = Money(0, currency) 

32 

33 for line in order.lines.all(): 

34 if line.purchase_price: 

35 try: 

36 total_price += convert_money(line.purchase_price, currency) * line.quantity 

37 except MissingRate: 

38 valid = False 

39 break 

40 

41 for line in order.extra_lines.all(): 

42 if line.price: 

43 try: 

44 total_price += convert_money(line.price, currency) * line.quantity 

45 except MissingRate: 

46 valid = False 

47 break 

48 

49 if valid: 

50 order.total_price = total_price 

51 order.save() 

52 

53 valid_count += 1 

54 else: 

55 invalid_count +=1 

56 

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

58 logger.info(f"Updated 'total_price' field for {valid_count} PurchaseOrder instances") 

59 

60 if invalid_count > 0: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true

61 logger.info(f"'total_price' field could not be updated for {invalid_count} PurchaseOrder instances") 

62 

63 

64def update_sales_order_price(apps, schema_editor): 

65 """Calculate 'total_price' field for each SalesOrder""" 

66 

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

68 

69 currency = currency_code_default() 

70 

71 valid_count = 0 

72 invalid_count = 0 

73 

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

75 

76 valid = True 

77 

78 total_price = Money(0, currency) 

79 

80 for line in order.lines.all(): 

81 if line.sale_price: 

82 try: 

83 total_price += convert_money(line.sale_price, currency) * line.quantity 

84 except MissingRate: 

85 valid = False 

86 break 

87 

88 for line in order.extra_lines.all(): 

89 if line.price: 

90 try: 

91 total_price += convert_money(line.price, currency) * line.quantity 

92 except MissingRate: 

93 valid = False 

94 break 

95 

96 if valid: 

97 order.total_price = total_price 

98 order.save() 

99 

100 valid_count += 1 

101 else: 

102 invalid_count +=1 

103 

104 if valid_count > 0: 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true

105 logger.info(f"Updated 'total_price' field for {valid_count} SalesOrder instances") 

106 

107 if invalid_count > 0: 107 ↛ 108line 107 didn't jump to line 108 because the condition on line 107 was never true

108 logger.info(f"'total_price' field could not be updated for {invalid_count} SalesOrder instances") 

109 

110 

111class Migration(migrations.Migration): 

112 

113 dependencies = [ 

114 ('order', '0078_auto_20230304_0721'), 

115 ] 

116 

117 operations = [ 

118 migrations.RunPython( 

119 update_purchase_order_price, 

120 reverse_code=migrations.RunPython.noop 

121 ), 

122 migrations.RunPython( 

123 update_sales_order_price, 

124 reverse_code=migrations.RunPython.noop, 

125 ) 

126 ]