From 684a44aae0fbde5a776f2665a92d4637d923f68e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 11 Jul 2016 22:52:55 +0200 Subject: [PATCH] =?UTF-8?q?Change=20le=20sch=C3=A9ma,=20les=20ventes=20eff?= =?UTF-8?q?ectu=C3=A9es=20sont=20maintenant=20un=20object=20=C3=A0=20part?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/admin.py | 8 +++- cotisations/forms.py | 2 - .../migrations/0013_auto_20160711_2240.py | 38 +++++++++++++++++++ cotisations/models.py | 22 +++++++++-- cotisations/views.py | 8 ++-- 5 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 cotisations/migrations/0013_auto_20160711_2240.py diff --git a/cotisations/admin.py b/cotisations/admin.py index 40b04591..821e82b0 100644 --- a/cotisations/admin.py +++ b/cotisations/admin.py @@ -1,9 +1,12 @@ from django.contrib import admin -from .models import Facture, Article, Banque, Paiement, Cotisation +from .models import Facture, Article, Banque, Paiement, Cotisation, Vente class FactureAdmin(admin.ModelAdmin): - list_display = ('user','paiement','name', 'number','prix', 'date','valid') + list_display = ('user','paiement','number', 'date','valid') + +class VenteAdmin(admin.ModelAdmin): + list_display = ('facture','name','prix','cotisation','duration') class ArticleAdmin(admin.ModelAdmin): list_display = ('name','prix','cotisation','duration') @@ -24,4 +27,5 @@ admin.site.register(Facture, FactureAdmin) admin.site.register(Article, ArticleAdmin) admin.site.register(Banque, BanqueAdmin) admin.site.register(Paiement, PaiementAdmin) +admin.site.register(Vente, VenteAdmin) admin.site.register(Cotisation, CotisationAdmin) diff --git a/cotisations/forms.py b/cotisations/forms.py index f2065cad..86cc95d5 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -42,8 +42,6 @@ class EditFactureForm(NewFactureForm): def __init__(self, *args, **kwargs): super(EditFactureForm, self).__init__(*args, **kwargs) self.fields['user'].label = 'Adherent' - self.fields['name'].label = 'Designation' - self.fields['prix'].label = 'Prix unitaire' self.fields['user'].empty_label = "Séléctionner l'adhérent propriétaire" self.fields.pop('article') diff --git a/cotisations/migrations/0013_auto_20160711_2240.py b/cotisations/migrations/0013_auto_20160711_2240.py new file mode 100644 index 00000000..000fb9f6 --- /dev/null +++ b/cotisations/migrations/0013_auto_20160711_2240.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0012_auto_20160704_0118'), + ] + + operations = [ + migrations.CreateModel( + name='Vente', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('prix', models.DecimalField(decimal_places=2, max_digits=5)), + ('cotisation', models.BooleanField()), + ('duration', models.IntegerField(null=True, blank=True, help_text='Durée exprimée en mois entiers')), + ], + ), + migrations.RemoveField( + model_name='facture', + name='name', + ), + migrations.RemoveField( + model_name='facture', + name='prix', + ), + migrations.AddField( + model_name='vente', + name='facture', + field=models.ForeignKey(to='cotisations.Facture', on_delete=django.db.models.deletion.PROTECT), + ), + ] diff --git a/cotisations/models.py b/cotisations/models.py index db064d7f..69ff45d1 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -8,12 +8,28 @@ class Facture(models.Model): cheque = models.CharField(max_length=255, blank=True) number = models.IntegerField() date = models.DateTimeField(auto_now_add=True) - name = models.CharField(max_length=255) - prix = models.DecimalField(max_digits=5, decimal_places=2) valid = models.BooleanField(default=True) + def prix(self): + prix = Vente.objects.all().filter(facture=self).aggregate(models.Sum('prix'))['prix__sum'] + return prix + + def name(self): + name = ' - '.join(vente.name for vente in Vente.objects.all().filter(facture=self)) + return name + def __str__(self): - return str(self.name) + ' ' + str(self.date) + ' ' + str(self.user) + return str(self.date) + ' ' + str(self.user) + +class Vente(models.Model): + facture = models.ForeignKey('Facture', on_delete=models.PROTECT) + 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 __str__(self): + return str(self.name) + ' ' + str(self.facture) class Article(models.Model): name = models.CharField(max_length=255) diff --git a/cotisations/views.py b/cotisations/views.py index cce0f090..3cb724ea 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -9,7 +9,7 @@ from django.contrib.auth.decorators import login_required, permission_required from django.contrib import messages from django.db.models import Max, ProtectedError -from .models import Facture, Article, Cotisation, Paiement, Banque +from .models import Facture, Article, Vente, Cotisation, Paiement, Banque from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm, NewFactureFormPdf from users.models import User from .tex import render_tex @@ -47,9 +47,10 @@ def new_facture(request, userid): if facture_form.is_valid(): new_facture = facture_form.save(commit=False) article = facture_form.cleaned_data['article'] - new_facture.prix = sum(art.prix for art in article) - new_facture.name = ' - '.join(art.name for art in 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) + new_vente.save() if any(art.cotisation for art in article): duration = sum(art.duration*facture.number for art in article if art.cotisation) create_cotis(new_facture, user, duration) @@ -76,7 +77,6 @@ def new_facture_pdf(request): return render_tex(request, 'cotisations/factures.tex', {'DATE' : timezone.now(),'dest':destinataire, 'obj':objet, 'detail':detail, 'article':tbl, 'total':prix_total, 'paid':paid, 'asso_name':ASSO_NAME, 'line1':ASSO_ADDRESS_LINE1, 'line2':ASSO_ADDRESS_LINE2, 'siret':ASSO_SIRET, 'email':ASSO_EMAIL, 'phone':ASSO_PHONE}) return form({'factureform': facture_form}, 'cotisations/facture.html', request) -@login_required @permission_required('cableur') def edit_facture(request, factureid): try: