Commit 0f1a5a82 authored by David Sinquin's avatar David Sinquin

Adapatation des views pour le menu en js

parent 1873a10f
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:
......
......@@ -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()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment