Skip to content

Commit

Permalink
fixup! ✨(backend) add owner email to order export
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Dec 19, 2024
1 parent 28f58ad commit 1110e8d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
121 changes: 119 additions & 2 deletions src/backend/joanie/core/serializers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ class AdminOrderExportSerializer(serializers.ModelSerializer):

class Meta:
model = models.Order
fields_labels = (
fields_labels = [
("id", "Référence de commande"),
("product", "Produit"),
("owner_name", "Propriétaire"),
Expand Down Expand Up @@ -1307,7 +1307,13 @@ class Meta:
("credit_card_brand", "Type de carte"),
("credit_card_last_numbers", "Derniers chiffres de la carte bancaire"),
("credit_card_expiration_date", "Date d'expiration de la carte bancaire"),
)
]
for i in range(1, 5):
fields_labels.append((f"installment_due_date_{i}", f"Date de paiement {i}"))
fields_labels.append(
(f"installment_amount_{i}", f"Montant du paiement {i}")
)
fields_labels.append((f"installment_state_{i}", f"État du paiement {i}"))
fields = [field for field, label in fields_labels]
read_only_fields = fields

Expand Down Expand Up @@ -1368,6 +1374,19 @@ def headers(self):
)
credit_card_expiration_date = serializers.SerializerMethodField(read_only=True)

installment_due_date_1 = serializers.SerializerMethodField(read_only=True)
installment_amount_1 = serializers.SerializerMethodField(read_only=True)
installment_state_1 = serializers.SerializerMethodField(read_only=True)
installment_due_date_2 = serializers.SerializerMethodField(read_only=True)
installment_amount_2 = serializers.SerializerMethodField(read_only=True)
installment_state_2 = serializers.SerializerMethodField(read_only=True)
installment_due_date_3 = serializers.SerializerMethodField(read_only=True)
installment_amount_3 = serializers.SerializerMethodField(read_only=True)
installment_state_3 = serializers.SerializerMethodField(read_only=True)
installment_due_date_4 = serializers.SerializerMethodField(read_only=True)
installment_amount_4 = serializers.SerializerMethodField(read_only=True)
installment_state_4 = serializers.SerializerMethodField(read_only=True)

def get_owner_name(self, instance) -> str:
"""
Return the full name of the order's owner if available,
Expand Down Expand Up @@ -1440,6 +1459,104 @@ def get_credit_card_expiration_date(self, instance) -> str:
year = instance.credit_card.expiration_year
return f"{month}/{year}"

def get_installment_value(self, instance, index, field) -> str:
"""
Return the value of the specified field for the specified installment if available,
otherwise an empty string.
"""
index -= 1
try:
value = instance.payment_schedule[index][field]
if field == "due_date":
return value.strftime("%d/%m/%Y %H:%M:%S")
return value
except (IndexError, KeyError):
return ""

def get_installment_due_date_1(self, instance) -> str:
"""
Return the due date of the first installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 1, "due_date")

def get_installment_amount_1(self, instance) -> str:
"""
Return the amount of the first installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 1, "amount")

def get_installment_state_1(self, instance) -> str:
"""
Return the state of the first installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 1, "state")

def get_installment_due_date_2(self, instance) -> str:
"""
Return the due date of the second installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 2, "due_date")

def get_installment_amount_2(self, instance) -> str:
"""
Return the amount of the second installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 2, "amount")

def get_installment_state_2(self, instance) -> str:
"""
Return the state of the second installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 2, "state")

def get_installment_due_date_3(self, instance) -> str:
"""
Return the due date of the third installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 3, "due_date")

def get_installment_amount_3(self, instance) -> str:
"""
Return the amount of the third installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 3, "amount")

def get_installment_state_3(self, instance) -> str:
"""
Return the state of the third installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 3, "state")

def get_installment_due_date_4(self, instance) -> str:
"""
Return the due date of the fourth installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 4, "due_date")

def get_installment_amount_4(self, instance) -> str:
"""
Return the amount of the fourth installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 4, "amount")

def get_installment_state_4(self, instance) -> str:
"""
Return the state of the fourth installment if available,
otherwise an empty string.
"""
return self.get_installment_value(instance, 4, "state")


class AdminOrderListExportSerializer(serializers.ListSerializer):
"""
Expand Down
12 changes: 12 additions & 0 deletions src/backend/joanie/tests/core/api/admin/orders/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def expected_csv_content(order):
),
}

for i in range(1, 5):
content[f"Date de paiement {i}"] = ""
content[f"Montant du paiement {i}"] = ""
content[f"État du paiement {i}"] = ""

if order.enrollment:
content["Session d'inscription"] = order.enrollment.course_run.title
content["Statut de la session"] = str(order.enrollment.course_run.state)
Expand All @@ -77,6 +82,13 @@ def expected_csv_content(order):
content["Solde (sur la facture)"] = str(order.main_invoice.balance)
content["État de facturation"] = order.main_invoice.state

for i, installment in enumerate(order.payment_schedule, start=1):
content[f"Date de paiement {i}"] = format_date_export(
installment.get("due_date")
)
content[f"Montant du paiement {i}"] = str(installment.get("amount"))
content[f"État du paiement {i}"] = installment.get("state")

return content


Expand Down

0 comments on commit 1110e8d

Please sign in to comment.