2018-08-31 12:46:35 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
from preferences.models import PaymentMethod, Cotisation
|
2018-11-22 21:52:15 +00:00
|
|
|
from gestion.models import ConsumptionHistory
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
class School(models.Model):
|
2018-10-05 22:03:02 +00:00
|
|
|
name = models.CharField(max_length=255, verbose_name="Nom")
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
class CotisationHistory(models.Model):
|
2018-11-22 21:52:15 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("validate_consumptionhistory", "Peut (in)valider les cotisations"),
|
|
|
|
)
|
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
WAITING = 0
|
|
|
|
VALID = 1
|
|
|
|
INVALID = 2
|
|
|
|
VALIDATION_CHOICES = (
|
|
|
|
(WAITING, 'En attente de validation'),
|
|
|
|
(VALID, 'Validée'),
|
|
|
|
(INVALID, 'Invalidée'),
|
|
|
|
)
|
|
|
|
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client")
|
|
|
|
amount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="Montant")
|
|
|
|
duration = models.PositiveIntegerField(verbose_name="Durée")
|
|
|
|
paymentDate = models.DateTimeField(auto_now_add=True, verbose_name="Date du paiement")
|
|
|
|
endDate = models.DateTimeField(verbose_name="Fin de la cotisation")
|
|
|
|
paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT, verbose_name="Moyen de paiement")
|
|
|
|
cotisation = models.ForeignKey(Cotisation, on_delete=models.PROTECT, verbose_name="Type de cotisation")
|
|
|
|
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="cotisation_made")
|
|
|
|
valid = models.IntegerField(choices=VALIDATION_CHOICES, default=WAITING)
|
|
|
|
|
|
|
|
class WhiteListHistory(models.Model):
|
2018-08-31 12:46:35 +00:00
|
|
|
user = models.ForeignKey(User, on_delete=models.PROTECT)
|
|
|
|
paymentDate = models.DateTimeField(auto_now_add=True)
|
|
|
|
endDate = models.DateTimeField()
|
2018-10-05 22:03:02 +00:00
|
|
|
duration = models.PositiveIntegerField(verbose_name="Durée", help_text="Durée de l'accès gracieux en jour")
|
|
|
|
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="whitelist_made")
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
class Profile(models.Model):
|
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
|
|
credit = models.DecimalField(max_digits=5, decimal_places=2, default=0)
|
|
|
|
debit = models.DecimalField(max_digits=5, decimal_places=2, default=0)
|
|
|
|
school = models.ForeignKey(School, on_delete=models.PROTECT, blank=True, null=True)
|
|
|
|
cotisationEnd = models.DateTimeField(blank=True, null=True)
|
|
|
|
|
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def balance(self):
|
2018-08-31 12:46:35 +00:00
|
|
|
return self.credit - self.debit
|
|
|
|
|
2018-09-01 19:57:09 +00:00
|
|
|
def positiveBalance(self):
|
2018-10-05 22:03:02 +00:00
|
|
|
return self.balance >= 0
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def rank(self):
|
|
|
|
return Profile.objects.filter(debit__gte=self.debit).count()
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def alcohol(self):
|
2018-11-22 21:52:15 +00:00
|
|
|
consumptions = ConsumptionHistory.objects.filter(customer=self.user).select_related('product')
|
|
|
|
alcohol = 0
|
|
|
|
for consumption in consumptions:
|
|
|
|
product = consumption.product
|
|
|
|
alcohol += consumption.quantity * float(product.deg) * product.volume * 0.79 /10 /1000
|
|
|
|
return alcohol
|
2018-08-31 12:46:35 +00:00
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.user)
|
|
|
|
|
2018-08-31 12:46:35 +00:00
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def create_user_profile(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
Profile.objects.create(user=instance)
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def save_user_profile(sender, instance, **kwargs):
|
2018-11-22 21:52:15 +00:00
|
|
|
instance.profile.save()
|
|
|
|
|
|
|
|
def str_user(self):
|
|
|
|
return self.username + " (" + self.first_name + " " + self.last_name + ", " + str(self.profile.balance) + "€)"
|
|
|
|
|
|
|
|
User.add_to_class("__str__", str_user)
|