From 553af7e61f08d52f68a374d58e74c951b0b38c26 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 14 Jul 2016 13:55:46 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'articles=20mutliples=20en=20quantit?= =?UTF-8?q?=C3=A9=20diff=C3=A9rente=20et=20un=20peu=20de=20js=20pour=20un?= =?UTF-8?q?=20formulaire=20dynamique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/admin.py | 4 +- cotisations/forms.py | 10 ++--- .../migrations/0014_auto_20160712_0245.py | 24 ++++++++++++ cotisations/models.py | 7 +++- .../templates/cotisations/facture.html | 30 +++++++++++++-- cotisations/views.py | 38 +++++++++++-------- 6 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 cotisations/migrations/0014_auto_20160712_0245.py diff --git a/cotisations/admin.py b/cotisations/admin.py index 821e82b0..cbad4542 100644 --- a/cotisations/admin.py +++ b/cotisations/admin.py @@ -3,10 +3,10 @@ from django.contrib import admin from .models import Facture, Article, Banque, Paiement, Cotisation, Vente class FactureAdmin(admin.ModelAdmin): - list_display = ('user','paiement','number', 'date','valid') + list_display = ('user','paiement','date','valid') class VenteAdmin(admin.ModelAdmin): - list_display = ('facture','name','prix','cotisation','duration') + list_display = ('facture','name','prix','number','cotisation','duration') class ArticleAdmin(admin.ModelAdmin): list_display = ('name','prix','cotisation','duration') diff --git a/cotisations/forms.py b/cotisations/forms.py index bf323602..c3a484c0 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -4,8 +4,6 @@ from django import forms from .models import Article, Paiement, Facture, Banque, Vente class NewFactureForm(ModelForm): - article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article", widget=forms.CheckboxSelectMultiple()) - def __init__(self, *args, **kwargs): super(NewFactureForm, self).__init__(*args, **kwargs) self.fields['cheque'].required = False @@ -23,13 +21,15 @@ class NewFactureForm(ModelForm): paiement = cleaned_data.get("paiement") cheque = cleaned_data.get("cheque") banque = cleaned_data.get("banque") - if paiement.moyen=="chèque" and not (cheque and banque): + if not paiement: + raise forms.ValidationError("Le moyen de paiement est obligatoire") + elif paiement.moyen=="chèque" and not (cheque and banque): raise forms.ValidationError("Le numero de chèque et la banque sont obligatoires") return cleaned_data class SelectArticleForm(Form): - article = forms.ModelChoiceField(queryset=Article.objects.all(), label="Article") - quantity = forms.IntegerField(label="Quantité") + article = forms.ModelChoiceField(queryset=Article.objects.all(), label="Article", required=True) + quantity = forms.IntegerField(label="Quantité", required=True) class NewFactureFormPdf(Form): article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article") diff --git a/cotisations/migrations/0014_auto_20160712_0245.py b/cotisations/migrations/0014_auto_20160712_0245.py new file mode 100644 index 00000000..b71385bf --- /dev/null +++ b/cotisations/migrations/0014_auto_20160712_0245.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0013_auto_20160711_2240'), + ] + + operations = [ + migrations.RemoveField( + model_name='facture', + name='number', + ), + migrations.AddField( + model_name='vente', + name='number', + field=models.IntegerField(default=1), + preserve_default=False, + ), + ] diff --git a/cotisations/models.py b/cotisations/models.py index aa002c33..71513a42 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -6,7 +6,6 @@ class Facture(models.Model): paiement = models.ForeignKey('Paiement', on_delete=models.PROTECT) banque = models.ForeignKey('Banque', on_delete=models.PROTECT, blank=True, null=True) cheque = models.CharField(max_length=255, blank=True) - number = models.IntegerField() date = models.DateTimeField(auto_now_add=True) valid = models.BooleanField(default=True) @@ -15,7 +14,7 @@ class Facture(models.Model): return prix def prix_total(self): - return self.prix()*self.number + return Vente.objects.all().filter(facture=self).aggregate(total=models.Sum(models.F('prix')*models.F('number'), output_field=models.FloatField()))['total'] def name(self): name = ' - '.join(vente.name for vente in Vente.objects.all().filter(facture=self)) @@ -26,11 +25,15 @@ class Facture(models.Model): class Vente(models.Model): facture = models.ForeignKey('Facture', on_delete=models.PROTECT) + number = models.IntegerField() name = models.CharField(max_length=255) prix = models.DecimalField(max_digits=5, decimal_places=2) cotisation = models.BooleanField() duration = models.IntegerField(help_text="Durée exprimée en mois entiers", blank=True, null=True) + def prix_total(self): + return self.prix*self.number + def __str__(self): return str(self.name) + ' ' + str(self.facture) diff --git a/cotisations/templates/cotisations/facture.html b/cotisations/templates/cotisations/facture.html index 50721ef7..659c3dce 100644 --- a/cotisations/templates/cotisations/facture.html +++ b/cotisations/templates/cotisations/facture.html @@ -1,5 +1,6 @@ {% extends "cotisations/sidebar.html" %} {% load bootstrap3 %} +{% load staticfiles%} {% block title %}Création et modification de factures{% endblock %} @@ -9,10 +10,33 @@
{% csrf_token %} {% bootstrap_form factureform %} - {% for vente in venteform %} - {% bootstrap_form vente %} - {% endfor %} {{ venteform.management_form }} +
+ {% for form in venteform.forms %} + {{ form.as_p }} + {% endfor %} +
+ + {% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
+ + + + + {% endblock %} diff --git a/cotisations/views.py b/cotisations/views.py index 19d9dca0..3f90417a 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -12,7 +12,7 @@ from django.forms import modelformset_factory, formset_factory import os from .models import Facture, Article, Vente, Cotisation, Paiement, Banque -from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm, NewFactureFormPdf +from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm, NewFactureFormPdf, SelectArticleForm from users.models import User from .tex import render_tex from re2o.settings_local import ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE, LOGO_PATH @@ -47,22 +47,28 @@ def new_facture(request, userid): return redirect("/cotisations/") facture = Facture(user=user) facture_form = NewFactureForm(request.POST or None, instance=facture) - ArticleFormSet = formset_factory(ArticleForm) - if facture_form.is_valid(): + article_formset = formset_factory(SelectArticleForm) + article_formset = article_formset(request.POST or None) + if facture_form.is_valid() and article_formset.is_valid(): new_facture = facture_form.save(commit=False) - article = facture_form.cleaned_data['article'] - new_facture.save() - for art in article: - new_vente = Vente.objects.create(facture=new_facture, name=art.name, prix=art.prix, cotisation=art.cotisation, duration=art.duration, number=1) - new_vente.save() - if any(art.cotisation for art in article): - duration = sum(art.duration for art in article if art.cotisation) - create_cotis(new_facture, user, duration) - messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name ) - else: - messages.success(request, "La facture a été crée") - return redirect("/users/profil/" + userid) - return form({'factureform': facture_form}, 'cotisations/facture.html', request) + articles = article_formset + if any(art.cleaned_data for art in articles): + new_facture.save() + 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) + create_cotis(new_facture, user, duration) + messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name ) + 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) @login_required @permission_required('trésorier')