From 9b3892a1ea054bd9e880446004429ca054bbc916 Mon Sep 17 00:00:00 2001 From: chapeau Date: Thu, 29 Feb 2024 14:27:55 +0100 Subject: [PATCH 1/2] Sort articles by number of membership/cotisation months before saving a invoice --- cotisations/views.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cotisations/views.py b/cotisations/views.py index f98dd358..cd09962f 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -75,6 +75,8 @@ def new_facture(request, user, userid): A bit of JS is used in the template to add articles in a fancier way. If everything is correct, save each one of the articles, save the purchase object associated and finally the newly created invoice. + Each article is created and save sorted by the number of month-length + membership or connection they offer, to solve duration ambiguities. """ invoice = Facture(user=user) # The template needs the list of articles (for the JS part) @@ -98,7 +100,13 @@ def new_facture(request, user, userid): # Building a purchase for each article sold purchases = [] total_price = 0 - for art_item in articles: + # We sort articles by number of months of subscription in them, to solve month + day ambiguities issues + sorted_articles = sorted( + articles, + key=lambda art: max(art.cleaned_data["article"].duration_membership, art.cleaned_data["article"].duration_connection), + reverse=True + ) + for art_item in sorted_articles: if art_item.cleaned_data: article = art_item.cleaned_data["article"] quantity = art_item.cleaned_data["quantity"] From 96abac05bf2dd933d51c12212247efb5c609805e Mon Sep 17 00:00:00 2001 From: chapeau Date: Sat, 2 Mar 2024 15:40:22 +0100 Subject: [PATCH 2/2] Add UT for cotisation order --- cotisations/test_views.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cotisations/test_views.py b/cotisations/test_views.py index bf3a3229..d3f5c507 100644 --- a/cotisations/test_views.py +++ b/cotisations/test_views.py @@ -125,9 +125,13 @@ class NewFactureTests(TestCase): self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/users/profil/%d" % self.user.pk) invoice = self.user.facture_set.first() + cotisations = invoice.get_subscription() delta = relativedelta(self.user.end_connexion(), date) delta.microseconds = 0 self.assertEqual(delta, relativedelta(months=1, days=7)) + # Check that the cotisations are sorted + self.assertEqual(relativedelta(cotisations[0].date_end_con, cotisations[0].date_start_con), relativedelta(months=1)) + self.assertEqual(relativedelta(cotisations[1].date_end_con, cotisations[1].date_start_con), relativedelta(days=7)) def test_several_articles_creates_several_purchases(self): data = {