2016-07-02 13:58:50 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2016-07-25 22:13:38 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
|
|
|
from django.dispatch import receiver
|
2016-07-17 18:08:56 +00:00
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
from django.core.validators import MinValueValidator
|
2016-07-02 13:58:50 +00:00
|
|
|
|
|
|
|
class Facture(models.Model):
|
|
|
|
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
|
|
|
paiement = models.ForeignKey('Paiement', on_delete=models.PROTECT)
|
2016-07-02 15:58:04 +00:00
|
|
|
banque = models.ForeignKey('Banque', on_delete=models.PROTECT, blank=True, null=True)
|
|
|
|
cheque = models.CharField(max_length=255, blank=True)
|
2016-07-02 13:58:50 +00:00
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
2016-07-11 20:52:55 +00:00
|
|
|
valid = models.BooleanField(default=True)
|
2016-07-17 18:08:56 +00:00
|
|
|
control = models.BooleanField(default=False)
|
2016-07-11 20:52:55 +00:00
|
|
|
|
|
|
|
def prix(self):
|
|
|
|
prix = Vente.objects.all().filter(facture=self).aggregate(models.Sum('prix'))['prix__sum']
|
|
|
|
return prix
|
|
|
|
|
2016-07-11 22:05:07 +00:00
|
|
|
def prix_total(self):
|
2016-07-14 11:55:46 +00:00
|
|
|
return Vente.objects.all().filter(facture=self).aggregate(total=models.Sum(models.F('prix')*models.F('number'), output_field=models.FloatField()))['total']
|
2016-07-11 22:05:07 +00:00
|
|
|
|
2016-07-11 20:52:55 +00:00
|
|
|
def name(self):
|
|
|
|
name = ' - '.join(vente.name for vente in Vente.objects.all().filter(facture=self))
|
|
|
|
return name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.date) + ' ' + str(self.user)
|
|
|
|
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_save, sender=Facture)
|
|
|
|
def facture_post_save(sender, **kwargs):
|
|
|
|
facture = kwargs['instance']
|
|
|
|
user = facture.user
|
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Facture)
|
|
|
|
def facture_post_delete(sender, **kwargs):
|
|
|
|
user = kwargs['instance'].user
|
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
|
|
|
|
2016-07-11 20:52:55 +00:00
|
|
|
class Vente(models.Model):
|
2016-07-17 18:08:56 +00:00
|
|
|
facture = models.ForeignKey('Facture', on_delete=models.CASCADE)
|
|
|
|
number = models.IntegerField(validators=[MinValueValidator(1)])
|
2016-07-02 13:58:50 +00:00
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
prix = models.DecimalField(max_digits=5, decimal_places=2)
|
2016-07-17 18:08:56 +00:00
|
|
|
iscotisation = models.BooleanField()
|
2016-07-11 20:52:55 +00:00
|
|
|
duration = models.IntegerField(help_text="Durée exprimée en mois entiers", blank=True, null=True)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2016-07-14 11:55:46 +00:00
|
|
|
def prix_total(self):
|
|
|
|
return self.prix*self.number
|
|
|
|
|
2016-07-17 18:08:56 +00:00
|
|
|
def clean(self):
|
|
|
|
if hasattr(self, 'cotisation'):
|
|
|
|
cotisation = self.cotisation
|
|
|
|
cotisation.date_end = cotisation.date_start + relativedelta(months=self.duration*self.number)
|
|
|
|
cotisation.save()
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
def __str__(self):
|
2016-07-11 20:52:55 +00:00
|
|
|
return str(self.name) + ' ' + str(self.facture)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_save, sender=Vente)
|
|
|
|
def vente_post_save(sender, **kwargs):
|
|
|
|
vente = kwargs['instance']
|
|
|
|
if vente.iscotisation:
|
|
|
|
user = vente.facture.user
|
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Vente)
|
|
|
|
def vente_post_delete(sender, **kwargs):
|
|
|
|
vente = kwargs['instance']
|
|
|
|
if vente.iscotisation:
|
|
|
|
user = vente.facture.user
|
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
prix = models.DecimalField(max_digits=5, decimal_places=2)
|
2016-07-17 18:08:56 +00:00
|
|
|
iscotisation = models.BooleanField()
|
2016-07-02 19:17:21 +00:00
|
|
|
duration = models.IntegerField(help_text="Durée exprimée en mois entiers", blank=True, null=True)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-07-02 16:30:59 +00:00
|
|
|
class Cotisation(models.Model):
|
2016-07-17 18:08:56 +00:00
|
|
|
vente = models.OneToOneField('Vente', on_delete=models.CASCADE, null=True)
|
2016-07-02 19:17:21 +00:00
|
|
|
date_start = models.DateTimeField()
|
2016-07-02 16:30:59 +00:00
|
|
|
date_end = models.DateTimeField()
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-17 18:08:56 +00:00
|
|
|
return str(self.vente)
|
2016-07-02 16:30:59 +00:00
|
|
|
|