|
@@ -4,19 +4,28 @@ from django.core.exceptions import ValidationError
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
|
-class RecurringPayment(models.Model):
|
|
|
- def get_last_validated_update(self):
|
|
|
- for update in self.updates.all():
|
|
|
- if update.validated:
|
|
|
- return update
|
|
|
- get_last_validated_update.short_description = 'Paiement'
|
|
|
+class CurrentPaymentManager(models.Manager):
|
|
|
+ def get_queryset(self):
|
|
|
+ qs = super().get_queryset()
|
|
|
+ qs = qs.annotate(
|
|
|
+ payment_method=models.Subquery(
|
|
|
+ PaymentUpdate.objects.filter(payment=models.OuterRef('pk'))
|
|
|
+ .values('payment_method')[:1]
|
|
|
+ )
|
|
|
+ )
|
|
|
+ qs = qs.annotate(
|
|
|
+ active=models.Case(
|
|
|
+ models.When(payment_method__isnull=True, then=None),
|
|
|
+ models.When(payment_method=PaymentUpdate.STOP, then=False),
|
|
|
+ default=True,
|
|
|
+ output_field=models.NullBooleanField()
|
|
|
+ )
|
|
|
+ )
|
|
|
+ return qs
|
|
|
|
|
|
- def payment_type(self):
|
|
|
- if hasattr(self, 'adhesion'):
|
|
|
- return 'Adhésion'
|
|
|
- if hasattr(self, 'service'):
|
|
|
- return 'Service'
|
|
|
- payment_type.short_description = 'Type'
|
|
|
+
|
|
|
+class RecurringPayment(models.Model):
|
|
|
+ objects = CurrentPaymentManager()
|
|
|
|
|
|
@property
|
|
|
def debtor(self):
|
|
@@ -25,6 +34,13 @@ class RecurringPayment(models.Model):
|
|
|
if hasattr(self, 'service'):
|
|
|
return self.service.adhesion
|
|
|
|
|
|
+ def payment_type(self):
|
|
|
+ if hasattr(self, 'adhesion'):
|
|
|
+ return 'Adhésion'
|
|
|
+ if hasattr(self, 'service'):
|
|
|
+ return 'Service'
|
|
|
+ payment_type.short_description = 'Type'
|
|
|
+
|
|
|
def payment_object(self):
|
|
|
return self.adhesion if hasattr(self, 'adhesion') else self.service
|
|
|
|
|
@@ -32,31 +48,18 @@ class RecurringPayment(models.Model):
|
|
|
return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=(self.pk,))
|
|
|
|
|
|
@property
|
|
|
- def active(self):
|
|
|
- payment = self.get_last_validated_update()
|
|
|
- return payment is not None and payment.payment_method != PaymentUpdate.STOP
|
|
|
+ def current(self):
|
|
|
+ return self.updates.last()
|
|
|
|
|
|
- @property
|
|
|
- def amount(self):
|
|
|
- payment = self.get_last_validated_update()
|
|
|
- if payment is not None and payment.payment_method != PaymentUpdate.STOP:
|
|
|
- return payment.amount
|
|
|
- else:
|
|
|
- return None
|
|
|
+ def is_active(self):
|
|
|
+ return self.current and self.current.payment_method != PaymentUpdate.STOP
|
|
|
|
|
|
class Meta:
|
|
|
verbose_name = 'paiement récurrent'
|
|
|
verbose_name_plural = 'paiements récurrents'
|
|
|
|
|
|
- def display(self):
|
|
|
- payment = self.get_last_validated_update()
|
|
|
- return str(payment) if payment else "pas de paiement"
|
|
|
-
|
|
|
def __str__(self):
|
|
|
- if hasattr(self, 'adhesion'):
|
|
|
- return 'Cotisation adhérent %s' % self.adhesion
|
|
|
- if hasattr(self, 'service'):
|
|
|
- return 'Contribution au service %s' % self.service
|
|
|
+ return str(self.current or 'non renseignée')
|
|
|
|
|
|
|
|
|
class PaymentUpdate(models.Model):
|