models.py 5.14 KB
Newer Older
lhark's avatar
lhark committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

chirac's avatar
chirac committed
23
24
from django.db import models

25
26
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
27
from dateutil.relativedelta import relativedelta
chibrac's avatar
chibrac committed
28
from django.forms import ValidationError
29
from django.core.validators import MinValueValidator
chirac's avatar
chirac committed
30

chibrac's avatar
chibrac committed
31

chirac's avatar
chirac committed
32
class Facture(models.Model):
33
34
    PRETTY_NAME = "Factures émises"

chirac's avatar
chirac committed
35
36
    user = models.ForeignKey('users.User', on_delete=models.PROTECT)
    paiement = models.ForeignKey('Paiement', on_delete=models.PROTECT)
37
38
    banque = models.ForeignKey('Banque', on_delete=models.PROTECT, blank=True, null=True)
    cheque = models.CharField(max_length=255, blank=True)
chirac's avatar
chirac committed
39
    date = models.DateTimeField(auto_now_add=True)
40
    valid = models.BooleanField(default=True)
41
    control = models.BooleanField(default=False)
42
43

    def prix(self):
44
        prix = Vente.objects.filter(facture=self).aggregate(models.Sum('prix'))['prix__sum']
45
46
        return prix

Dalahro's avatar
Dalahro committed
47
    def prix_total(self):
48
        return Vente.objects.filter(facture=self).aggregate(total=models.Sum(models.F('prix')*models.F('number'), output_field=models.FloatField()))['total']
Dalahro's avatar
Dalahro committed
49

50
    def name(self):
51
        name = ' - '.join(vente.name for vente in Vente.objects.filter(facture=self))
52
53
54
        return name

    def __str__(self):
Dalahro's avatar
Dalahro committed
55
        return str(self.user) + ' ' + str(self.date)
56

57
58
59
60
@receiver(post_save, sender=Facture)
def facture_post_save(sender, **kwargs):
    facture = kwargs['instance']
    user = facture.user
61
    user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
62
63
64
65

@receiver(post_delete, sender=Facture)
def facture_post_delete(sender, **kwargs):
    user = kwargs['instance'].user
66
    user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
67

68
class Vente(models.Model):
69
70
    PRETTY_NAME = "Ventes effectuées"

71
72
    facture = models.ForeignKey('Facture', on_delete=models.CASCADE)
    number = models.IntegerField(validators=[MinValueValidator(1)])
chirac's avatar
chirac committed
73
74
    name = models.CharField(max_length=255)
    prix = models.DecimalField(max_digits=5, decimal_places=2)
75
    iscotisation = models.BooleanField()
76
    duration = models.IntegerField(help_text="Durée exprimée en mois entiers", blank=True, null=True)
chirac's avatar
chirac committed
77

78
79
80
    def prix_total(self):
        return self.prix*self.number

81
82
83
84
85
86
    def clean(self):
        if hasattr(self, 'cotisation'):
            cotisation = self.cotisation
            cotisation.date_end = cotisation.date_start + relativedelta(months=self.duration*self.number)
            cotisation.save()

chirac's avatar
chirac committed
87
    def __str__(self):
88
        return str(self.name) + ' ' + str(self.facture)
chirac's avatar
chirac committed
89

90
91
92
93
94
@receiver(post_save, sender=Vente)
def vente_post_save(sender, **kwargs):
    vente = kwargs['instance']
    if vente.iscotisation:
        user = vente.facture.user
95
        user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
96
97
98
99
100
101

@receiver(post_delete, sender=Vente)
def vente_post_delete(sender, **kwargs):
    vente = kwargs['instance']
    if vente.iscotisation:
        user = vente.facture.user
102
        user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
103

chirac's avatar
chirac committed
104
class Article(models.Model):
105
106
    PRETTY_NAME = "Articles en vente"

107
    name = models.CharField(max_length=255, unique=True)
chirac's avatar
chirac committed
108
    prix = models.DecimalField(max_digits=5, decimal_places=2)
109
    iscotisation = models.BooleanField()
David Sinquin's avatar
David Sinquin committed
110
111
112
113
114
    duration = models.IntegerField(
        help_text="Durée exprimée en mois entiers",
        blank=True,
        null=True,
        min_value=0)
chibrac's avatar
chibrac committed
115
116
117
118
119

    def clean(self):
        if self.name.lower() == "solde":
            raise ValidationError("Solde est un nom d'article invalide")

chirac's avatar
chirac committed
120
121
122
123
    def __str__(self):
        return self.name

class Banque(models.Model):
124
125
    PRETTY_NAME = "Banques enregistrées"

chirac's avatar
chirac committed
126
127
128
129
130
131
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

class Paiement(models.Model):
132
133
    PRETTY_NAME = "Moyens de paiement"

chirac's avatar
chirac committed
134
135
136
137
138
    moyen = models.CharField(max_length=255)

    def __str__(self):
        return self.moyen

chibrac's avatar
chibrac committed
139
140
141
    def clean(self):
        self.moyen = self.moyen.title()

142
class Cotisation(models.Model):
143
144
    PRETTY_NAME = "Cotisations"

145
    vente = models.OneToOneField('Vente', on_delete=models.CASCADE, null=True)
146
    date_start = models.DateTimeField()
147
148
149
    date_end = models.DateTimeField()

    def __str__(self):
150
        return str(self.vente)
151