When creating a Sogé credit, serch existing recent memberships and register them

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2021-09-05 21:24:16 +02:00
parent ba9ef0371a
commit b646f549d6
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 41 additions and 1 deletions

View File

@ -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.

View File

@ -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()