models.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from django.db import models
  2. from django.contrib.contenttypes.fields import GenericForeignKey
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.core.validators import MaxValueValidator
  5. class Payment(models.Model):
  6. TRANSFERT = 0
  7. WITHDRAWAL = 1
  8. PAYMENT_CHOICES = (
  9. (TRANSFERT, 'Virement'),
  10. (WITHDRAWAL, 'Prélèvement'),
  11. )
  12. limit = models.Q(app_label='adhesions', model='Adherent') \
  13. | models.Q(app_label='services', model='Service')
  14. reason_type = models.ForeignKey(ContentType, on_delete=models.CASCADE,
  15. limit_choices_to=limit)
  16. reason_id = models.PositiveIntegerField()
  17. reason = GenericForeignKey('reason_type', 'reason_id')
  18. amount = models.DecimalField(max_digits=9, decimal_places=2, verbose_name='Montant')
  19. period = models.PositiveIntegerField(validators=[MaxValueValidator(12)],
  20. verbose_name='Période (mois)')
  21. payment_method = models.IntegerField(choices=PAYMENT_CHOICES,
  22. verbose_name='Méthode de paiement')
  23. date = models.DateField(verbose_name='Date de paiement ou de début de paiement')
  24. class Meta:
  25. verbose_name = 'paiement'
  26. def reason_verbose(self):
  27. if self.reason_type.app_label == 'adhesions':
  28. return 'Cotisation de %s' % self.reason
  29. if self.reason_type.app_label == 'services':
  30. return 'Service %s' % self.reason
  31. reason_verbose.short_description = 'Paiement'
  32. def __str__(self):
  33. s = str(self.amount) + '€'
  34. if self.period:
  35. if self.period == 1:
  36. s += '/mois'
  37. elif self.period == 12:
  38. s += '/an'
  39. else:
  40. s += '/%d mois' % self.period
  41. if self.payment_method == self.TRANSFERT:
  42. s += ' (virement)'
  43. elif self.payment_method == self.WITHDRAWAL:
  44. s += ' (prélèvement)'
  45. return s