From b6e7399189bbebb165e3485ac7323a727dd2affc Mon Sep 17 00:00:00 2001 From: David Sinquin Date: Thu, 14 Jul 2016 20:51:45 +0200 Subject: [PATCH] Adapatation des views pour le menu en js --- cotisations/forms.py | 7 ++++--- cotisations/views.py | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index c3a484c0..d9360272 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -1,6 +1,7 @@ from django import forms from django.forms import ModelForm, Form from django import forms +from django.core.validators import MinValueValidator from .models import Article, Paiement, Facture, Banque, Vente class NewFactureForm(ModelForm): @@ -29,11 +30,11 @@ class NewFactureForm(ModelForm): class SelectArticleForm(Form): article = forms.ModelChoiceField(queryset=Article.objects.all(), label="Article", required=True) - quantity = forms.IntegerField(label="Quantité", required=True) + quantity = forms.IntegerField(label="Quantité", validators=[MinValueValidator(1)], required=True) class NewFactureFormPdf(Form): article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article") - number = forms.IntegerField(label="Quantité") + number = forms.IntegerField(label="Quantité", validators=[MinValueValidator(1)]) paid = forms.BooleanField(label="Payé", required=False) dest = forms.CharField(required=True, max_length=255, label="Destinataire") chambre = forms.CharField(required=False, max_length=10, label="Adresse") @@ -47,7 +48,7 @@ class EditFactureForm(NewFactureForm): super(EditFactureForm, self).__init__(*args, **kwargs) self.fields['user'].label = 'Adherent' self.fields['user'].empty_label = "Séléctionner l'adhérent propriétaire" - self.fields.pop('article') + self.fields['valid'].label = 'Validité de la facture' class ArticleForm(ModelForm): class Meta: diff --git a/cotisations/views.py b/cotisations/views.py index 3f90417a..516da18c 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -46,29 +46,34 @@ def new_facture(request, userid): messages.error(request, u"Utilisateur inexistant" ) return redirect("/cotisations/") facture = Facture(user=user) + # Le template a besoin de connaitre les articles pour le js + article_list = Article.objects.all() + # On envoie la form fature et un formset d'articles facture_form = NewFactureForm(request.POST or None, instance=facture) - article_formset = formset_factory(SelectArticleForm) - article_formset = article_formset(request.POST or None) + article_formset = formset_factory(SelectArticleForm)(request.POST or None) if facture_form.is_valid() and article_formset.is_valid(): new_facture = facture_form.save(commit=False) articles = article_formset + # Si au moins un article est rempli if any(art.cleaned_data for art in articles): new_facture.save() + duration = 0 for art_item in articles: if art_item.cleaned_data: article = art_item.cleaned_data['article'] quantity = art_item.cleaned_data['quantity'] new_vente = Vente.objects.create(facture=new_facture, name=article.name, prix=article.prix, cotisation=article.cotisation, duration=article.duration, number=quantity) new_vente.save() - if any(art.cleaned_data['article'].cotisation for art in articles): - duration = sum(art.cleaned_data['article'].duration*art.cleaned_data['quantity'] for art in articles if art.cleaned_data['article'].cotisation) + if art_item.cleaned_data['article'].cotisation: + duration += art_item.cleaned_data['article'].duration*art_item.cleaned_data['quantity'] + if duration: create_cotis(new_facture, user, duration) - messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name ) + messages.success(request, "La cotisation a été prolongée pour l'adhérent %s jusqu'au %s" % (user.name, user.end_adhesion()) ) else: messages.success(request, "La facture a été crée") return redirect("/users/profil/" + userid) messages.error(request, u"Il faut au moins un article valide pour créer une facture" ) - return form({'factureform': facture_form, 'venteform': article_formset}, 'cotisations/facture.html', request) + return form({'factureform': facture_form, 'venteform': article_formset, 'articlelist': article_list}, 'cotisations/new_facture.html', request) @login_required @permission_required('trésorier') @@ -115,7 +120,7 @@ def edit_facture(request, factureid): return redirect("/cotisations/") facture_form = EditFactureForm(request.POST or None, instance=facture) ventes_objects = Vente.objects.filter(facture=facture) - vente_form_set = modelformset_factory(Vente, fields=('name','prix','number'), can_delete=True) + vente_form_set = modelformset_factory(Vente, fields=('name','number'), extra=0, max_num=len(ventes_objects)) vente_form = vente_form_set(request.POST or None, queryset=ventes_objects) if facture_form.is_valid() and vente_form.is_valid(): facture_form.save()