Coverage for invoices/resources.py: 30%
69 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:08 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:08 -0600
1from import_export import resources, fields
2from payments.models import ManualPayment, PaymentIntent
3from .models import Invoice
6class InvoiceResource(resources.ModelResource):
7 invoice_date = fields.Field()
8 membership_type = fields.Field()
9 segment = fields.Field()
10 amount = fields.Field()
11 payment_reference = fields.Field()
12 segment_comment = fields.Field()
13 product_comment = fields.Field()
14 product_variation_comment = fields.Field()
16 class Meta:
17 model = Invoice
18 fields = [
19 "invoice_date",
20 "membership",
21 "membership_type",
22 "segment",
23 "use",
24 "sat_uuid",
25 "folio_number",
26 "amount",
27 "status",
28 "cancellation_status",
29 "cancellation_motive",
30 "payment_reference",
31 ]
33 def dehydrate_invoice_date(self, instance):
34 return instance.created_at.date()
36 def dehydrate_membership(self, instance):
37 return instance.membership.name
39 def dehydrate_membership_type(self, instance):
40 return instance.membership.type_of.name
42 def dehydrate_segment(self, instance):
43 return instance.membership.segment
45 def dehydrate_amount(self, instance):
46 return instance.total_amount
48 def dehydrate_status(self, instance):
49 return instance.get_status_display()
51 def dehydrate_cancellation_status(self, instance):
52 return instance.get_cancellation_status_display()
54 def dehydrate_cancellation_motive(self, instance):
55 return instance.get_cancellation_motive_display()
57 def dehydrate_payment_reference(self, instance):
58 product_charges = instance.product_charges.all()
60 payment_references = []
62 for product_charge in product_charges:
63 payment_allocations = product_charge.payment_allocations.all()
64 for payment_allocation in payment_allocations:
65 payment = payment_allocation.payment
66 if isinstance(payment, ManualPayment):
67 payment_references.append(payment.reference)
68 elif isinstance(payment, PaymentIntent):
69 payment_references.append(payment.stripe_pi_id)
71 return ", ".join(payment_reference for payment_reference in payment_references)
73 def dehydrate_segment_comment(self, instance):
74 product_charges = instance.product_charges.all()
76 segment_comments = []
78 for product_charge in product_charges:
79 product = product_charge.product
80 for segment in product.segments.all():
81 if segment.comment:
82 segment_comments.append(segment.comment)
84 return ", ".join(segment_comment for segment_comment in segment_comments)
86 def dehydrate_product_comment(self, instance):
87 product_charges = instance.product_charges.all()
89 product_comments = []
91 for product_charge in product_charges:
92 product = product_charge.product
93 if product.comment:
94 product_comments.append(product.comment)
96 return ", ".join(product_comment for product_comment in product_comments)
98 def dehydrate_product_variation_comment(self, instance):
99 product_charges = instance.product_charges.all()
101 product_variation_comments = []
103 for product_charge in product_charges:
104 product_purchase = product_charge.purchase
105 for variation in product_purchase.price_breakdown.get("variations", []):
106 if variation.get("comment", None):
107 product_variation_comments.append(variation.get("comment"))
109 return ", ".join(product_variation_comment for product_variation_comment in product_variation_comments)