From 1110e8d6aa87ae6e0a1b56c6a8daf96ed8421702 Mon Sep 17 00:00:00 2001 From: Nicolas Clerc Date: Tue, 17 Dec 2024 17:58:34 +0100 Subject: [PATCH] =?UTF-8?q?fixup!=20=E2=9C=A8(backend)=20add=20owner=20ema?= =?UTF-8?q?il=20to=20order=20export?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/joanie/core/serializers/admin.py | 121 +++++++++++++++++- .../core/api/admin/orders/test_export.py | 12 ++ 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/src/backend/joanie/core/serializers/admin.py b/src/backend/joanie/core/serializers/admin.py index 6b531f17e..0e6608716 100755 --- a/src/backend/joanie/core/serializers/admin.py +++ b/src/backend/joanie/core/serializers/admin.py @@ -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"), @@ -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 @@ -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, @@ -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): """ diff --git a/src/backend/joanie/tests/core/api/admin/orders/test_export.py b/src/backend/joanie/tests/core/api/admin/orders/test_export.py index b57773786..61ba356c1 100644 --- a/src/backend/joanie/tests/core/api/admin/orders/test_export.py +++ b/src/backend/joanie/tests/core/api/admin/orders/test_export.py @@ -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) @@ -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