models.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. validated = models.BooleanField(default=False, verbose_name='Paiement validé')
  25. class Meta:
  26. verbose_name = 'paiement'
  27. permissions = (
  28. ('validate_payment', 'Peut valider les paiements'),
  29. )
  30. def reason_verbose(self):
  31. if self.reason_type.app_label == 'adhesions':
  32. return 'Cotisation de %s' % self.reason
  33. if self.reason_type.app_label == 'services':
  34. return 'Service %s' % self.reason
  35. reason_verbose.short_description = 'Paiement'
  36. def __str__(self):
  37. s = str(self.amount) + '€'
  38. if self.period:
  39. if self.period == 1:
  40. s += '/mois'
  41. elif self.period == 12:
  42. s += '/an'
  43. else:
  44. s += '/%d mois' % self.period
  45. if self.payment_method == self.TRANSFERT:
  46. s += ' (virement)'
  47. elif self.payment_method == self.WITHDRAWAL:
  48. s += ' (prélèvement)'
  49. return s