From b646f549d631e2b130b85b922310a36ab2e9c120 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 5 Sep 2021 21:24:16 +0200 Subject: [PATCH] =?UTF-8?q?When=20creating=20a=20Sog=C3=A9=20credit,=20ser?= =?UTF-8?q?ch=20existing=20recent=20memberships=20and=20register=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yohann D'ANELLO --- apps/treasury/models.py | 37 +++++++++++++++++++++++++++++++++++++ apps/wei/models.py | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/apps/treasury/models.py b/apps/treasury/models.py index 0b5948fd..39fb8523 100644 --- a/apps/treasury/models.py +++ b/apps/treasury/models.py @@ -11,7 +11,10 @@ from django.db.models import Q from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext_lazy as _ + +from member.models import Club, Membership from note.models import NoteSpecial, SpecialTransaction, MembershipTransaction, NoteUser +from wei.models import WEIClub class Invoice(models.Model): @@ -305,6 +308,40 @@ class SogeCredit(models.Model): return self.credit_transaction.total if self.valid \ else sum(transaction.total for transaction in self.transactions.all()) + def update_transactions(self): + """ + The Sogé credit may be created after the user already paid its memberships. + We query transactions and update the credit, if it is unvalid. + """ + if self.valid: + return + + bde = Club.objects.get(name="BDE") + kfet = Club.objects.get(name="Kfet") + wei = WEIClub.objects.order_by('-year').first() + bde_qs = Membership.objects.filter(user=self.user, club=bde, date_start__gte=bde.membership_start) + kfet_qs = Membership.objects.filter(user=self.user, club=kfet, date_start__gte=kfet.membership_start) + wei_qs = Membership.objects.filter(user=self.user, club=wei, date_start__gte=wei.membership_start) + + if bde_qs.exists(): + m = bde_qs.get() + if m.transaction not in self.transactions.all(): + self.transactions.add(m.transaction) + + if kfet_qs.exists(): + m = kfet_qs.get() + if m.transaction not in self.transactions.all(): + self.transactions.add(m.transaction) + + if wei_qs.exists(): + m = wei_qs.get() + if m.transaction not in self.transactions.all(): + self.transactions.add(m.transaction) + + for tr in self.transactions.all(): + tr.valid = False + tr.save() + def invalidate(self): """ Invalidating a Société générale delete the transaction of the bank if it was already created. diff --git a/apps/wei/models.py b/apps/wei/models.py index b59a0dfd..9e99cd1c 100644 --- a/apps/wei/models.py +++ b/apps/wei/models.py @@ -364,8 +364,11 @@ class WEIMembership(Membership): # to treasurers. transaction.refresh_from_db() from treasury.models import SogeCredit - soge_credit = SogeCredit.objects.get_or_create(user=self.user)[0] + soge_credit, created = SogeCredit.objects.get_or_create(user=self.user) soge_credit.refresh_from_db() transaction.save() soge_credit.transactions.add(transaction) soge_credit.save() + + soge_credit.update_transactions() + soge_credit.save()