models.py 4.06 KB
Newer Older
1 2 3 4 5 6 7 8
from django.db import models
from django import forms
from django.forms import ModelForm


class Facture(models.Model):
    user = models.ForeignKey('users.User', on_delete=models.PROTECT)
    paiement = models.ForeignKey('Paiement', on_delete=models.PROTECT)
9 10
    banque = models.ForeignKey('Banque', on_delete=models.PROTECT, blank=True, null=True)
    cheque = models.CharField(max_length=255, blank=True)
11 12 13 14
    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)
15
    valid = models.BooleanField(default=True)
16 17

    def __str__(self):
18
        return str(self.name) + ' ' + str(self.date) + ' ' + str(self.user)
19 20 21 22

class Article(models.Model):
    name = models.CharField(max_length=255)
    prix = models.DecimalField(max_digits=5, decimal_places=2)
23
    cotisation = models.BooleanField()
24
    duration = models.IntegerField(help_text="Durée exprimée en mois entiers", blank=True, null=True)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

    def __str__(self):
        return self.name

class Banque(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

class Paiement(models.Model):
    moyen = models.CharField(max_length=255)

    def __str__(self):
        return self.moyen

41
class Cotisation(models.Model):
42
    facture = models.OneToOneField('Facture', on_delete=models.PROTECT)
43
    date_start = models.DateTimeField()
44 45 46 47 48
    date_end = models.DateTimeField()

    def __str__(self):
        return str(self.facture)

49 50 51 52 53 54 55 56 57
class NewFactureForm(ModelForm):
    article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article")

    def __init__(self, *args, **kwargs):
        super(NewFactureForm, self).__init__(*args, **kwargs)
        self.fields['number'].label = 'Quantité'
        self.fields['cheque'].required = False
        self.fields['banque'].required = False
        self.fields['cheque'].label = 'Numero de chèque'
58 59
        self.fields['banque'].empty_label = "Non renseigné"
        self.fields['paiement'].empty_label = "Séléctionner un moyen de paiement"
60 61 62

    class Meta:
        model = Facture
63 64
        fields = ['paiement','banque','cheque','number']

65 66 67 68 69 70 71 72 73
    def clean(self):
        cleaned_data=super(NewFactureForm, self).clean()
        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):
            raise forms.ValidationError("Le numero de chèque et la banque sont obligatoires")
        return cleaned_data

74 75 76
class EditFactureForm(NewFactureForm):
    class Meta(NewFactureForm.Meta):
        fields = '__all__'
77 78 79 80 81 82

    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'
83
        self.fields['user'].empty_label = "Séléctionner l'adhérent propriétaire"
84
        self.fields.pop('article')
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        self.fields['name'].label = "Désignation de l'article"

class DelArticleForm(ModelForm):
    articles = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Articles actuels",  widget=forms.CheckboxSelectMultiple)

    class Meta:
        fields = ['articles']
        model = Article

class PaiementForm(ModelForm):
    class Meta:
        model = Paiement
        fields = ['moyen']

    def __init__(self, *args, **kwargs):
        super(PaiementForm, self).__init__(*args, **kwargs)
        self.fields['moyen'].label = 'Moyen de paiement à ajouter'

class DelPaiementForm(ModelForm):
    paiements = forms.ModelMultipleChoiceField(queryset=Paiement.objects.all(), label="Moyens de paiement actuels",  widget=forms.CheckboxSelectMultiple)

    class Meta:
        exclude = ['moyen']
        model = Paiement