8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 01:13:46 +00:00

Deplace les check et la creation de la cotisation dans models

This commit is contained in:
Gabriel Detraz 2017-07-24 02:33:38 +02:00 committed by root
parent f9c9c53ad7
commit 78d4c6bb80
2 changed files with 27 additions and 18 deletions

View file

@ -28,6 +28,7 @@ from dateutil.relativedelta import relativedelta
from django.forms import ValidationError from django.forms import ValidationError
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.utils import timezone
class Facture(models.Model): class Facture(models.Model):
PRETTY_NAME = "Factures émises" PRETTY_NAME = "Factures émises"
@ -78,11 +79,35 @@ class Vente(models.Model):
def prix_total(self): def prix_total(self):
return self.prix*self.number return self.prix*self.number
def clean(self): def update_cotisation(self):
if hasattr(self, 'cotisation'): if hasattr(self, 'cotisation'):
cotisation = self.cotisation cotisation = self.cotisation
cotisation.date_end = cotisation.date_start + relativedelta(months=self.duration*self.number) cotisation.date_end = cotisation.date_start + relativedelta(months=self.duration*self.number)
cotisation.save() cotisation.save()
return
def create_cotis(self, date_start=False):
""" Update et crée l'objet cotisation associé à une facture, prend en argument l'user, la facture pour la quantitéi, et l'article pour la durée"""
if not hasattr(self, 'cotisation'):
cotisation=Cotisation(vente=self)
if date_start:
end_adhesion = Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.filter(user=self.facture.user).exclude(valid=False))).filter(date_start__lt=date_start).aggregate(Max('date_end'))['date_end__max']
else:
end_adhesion = self.facture.user.end_adhesion()
date_start = date_start or timezone.now()
end_adhesion = end_adhesion or date_start
date_max = max(end_adhesion, date_start)
cotisation.date_start = date_max
cotisation.date_end = cotisation.date_start + relativedelta(months=self.duration*self.number)
cotisation.save()
return
def save(self, *args, **kwargs):
# On verifie que si iscotisation, duration est présent
if self.iscotisation and not self.duration:
raise ValidationError("Cotisation et durée doivent être présents ensembles")
self.update_cotisation()
super(Vente, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return str(self.name) + ' ' + str(self.facture) return str(self.name) + ' ' + str(self.facture)
@ -91,6 +116,7 @@ class Vente(models.Model):
def vente_post_save(sender, **kwargs): def vente_post_save(sender, **kwargs):
vente = kwargs['instance'] vente = kwargs['instance']
if vente.iscotisation: if vente.iscotisation:
vente.create_cotis()
user = vente.facture.user user = vente.facture.user
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False) user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)

View file

@ -53,21 +53,6 @@ def form(ctx, template, request):
c.update(csrf(request)) c.update(csrf(request))
return render(request, template, c) return render(request, template, c)
def create_cotis(vente, user, duration, date_start=False):
""" Update et crée l'objet cotisation associé à une facture, prend en argument l'user, la facture pour la quantitéi, et l'article pour la durée"""
cotisation=Cotisation(vente=vente)
if date_start:
end_adhesion = Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.filter(user=user).exclude(valid=False))).filter(date_start__lt=date_start).aggregate(Max('date_end'))['date_end__max']
else:
end_adhesion = user.end_adhesion()
date_start = date_start or timezone.now()
end_adhesion = end_adhesion or date_start
date_max = max(end_adhesion, date_start)
cotisation.date_start = date_max
cotisation.date_end = cotisation.date_start + relativedelta(months=duration)
cotisation.save()
return
@login_required @login_required
@permission_required('cableur') @permission_required('cableur')
def new_facture(request, userid): def new_facture(request, userid):
@ -113,8 +98,6 @@ def new_facture(request, userid):
new_vente.save() new_vente.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") reversion.set_comment("Création")
if art_item.cleaned_data['article'].iscotisation:
create_cotis(new_vente, user, art_item.cleaned_data['article'].duration*art_item.cleaned_data['quantity'])
if any(art_item.cleaned_data['article'].iscotisation for art_item in articles if art_item.cleaned_data): if any(art_item.cleaned_data['article'].iscotisation for art_item in articles if art_item.cleaned_data):
messages.success(request, "La cotisation a été prolongée pour l'adhérent %s jusqu'au %s" % (user.pseudo, user.end_adhesion()) ) messages.success(request, "La cotisation a été prolongée pour l'adhérent %s jusqu'au %s" % (user.pseudo, user.end_adhesion()) )
else: else: