|
@@ -4,6 +4,8 @@ import calendar
|
|
|
import random
|
|
|
from decimal import Decimal
|
|
|
from django.db import models
|
|
|
+from django.db.models.signals import post_save
|
|
|
+from django.dispatch import receiver
|
|
|
from coin.offers.models import Offer, OfferSubscription
|
|
|
from coin.members.models import Member
|
|
|
|
|
@@ -40,6 +42,18 @@ class Invoice(models.Model):
|
|
|
for detail in self.details.all():
|
|
|
total += detail.total()
|
|
|
return total.quantize(Decimal('0.01'))
|
|
|
+ amount.short_description = 'Montant'
|
|
|
+
|
|
|
+ def amount_paid(self):
|
|
|
+ """
|
|
|
+ Calcul le montant payé de la facture en fonction des éléments
|
|
|
+ de paiements
|
|
|
+ """
|
|
|
+ total = Decimal('0.0')
|
|
|
+ for payment in self.payments.all():
|
|
|
+ total += payment.amount
|
|
|
+ return total.quantize(Decimal('0.01'))
|
|
|
+ amount_paid.short_description = 'Montant payé'
|
|
|
|
|
|
def has_owner(self, uid):
|
|
|
"Check if passed uid (ex gmajax) is owner of the invoice"
|
|
@@ -117,10 +131,26 @@ class Payment(models.Model):
|
|
|
default='transfer',
|
|
|
choices=PAYMENT_MEAN_CHOICES,
|
|
|
verbose_name='Moyen de paiement')
|
|
|
- amount = models.DecimalField(max_digits=7, decimal_places=2, null=True,
|
|
|
+ amount = models.DecimalField(max_digits=5, decimal_places=2, null=True,
|
|
|
verbose_name='Montant')
|
|
|
date = models.DateField(default=datetime.date.today)
|
|
|
- invoice = models.ForeignKey(Invoice, verbose_name='Facture')
|
|
|
+ invoice = models.ForeignKey(Invoice, verbose_name='Facture',
|
|
|
+ related_name='payments')
|
|
|
+
|
|
|
+ def __unicode__(self):
|
|
|
+ return u'Paiment de %0.2f€' % self.amount
|
|
|
|
|
|
class Meta:
|
|
|
verbose_name = 'paiement'
|
|
|
+
|
|
|
+@receiver(post_save, sender=Payment)
|
|
|
+def set_invoice_as_paid_if_needed(sender, instance, **kwargs):
|
|
|
+ """
|
|
|
+ Lorsqu'un paiement est enregistré, vérifie si la facture est alors
|
|
|
+ complétement payée. Dans ce cas elle passe en réglée
|
|
|
+ """
|
|
|
+ if (instance.invoice.amount_paid >= instance.invoice.amount and
|
|
|
+ instance.invoice.status == 'open'):
|
|
|
+ instance.invoice.status = 'closed'
|
|
|
+ instance.invoice.save()
|
|
|
+
|