Browse Source

Compute payments amount of an invoice
Auto set invoice as paid when saving payment, payments amount > invoice amount

Fabs 11 years ago
parent
commit
27b39616a9

+ 2 - 2
coin/billing/admin.py

@@ -43,8 +43,8 @@ class InvoiceAdmin(admin.ModelAdmin):
     fields = (('number', 'date', 'status'),
        ('date_due'),
        ('member'),
-       'amount')
-    readonly_fields = ('amount',)
+       ('amount','amount_paid'))
+    readonly_fields = ('amount','amount_paid')
     form = autocomplete_light.modelform_factory(Invoice)
 
     def get_formsets(self, request, obj=None):

+ 2 - 2
coin/billing/create_subscriptions_invoices.py

@@ -10,7 +10,6 @@ from coin.members.models import Member
 from coin.billing.models import Invoice, InvoiceDetail
 
 
-
 def create_all_members_invoices_for_a_period(date=datetime.date.today()):
     """
     Pour chaque membre ayant au moins un abonnement actif, génère les factures
@@ -122,7 +121,8 @@ def create_member_invoice_for_a_period(member, date):
             # Si durée de 0jours ou dates incohérentes, alors on ajoute pas
             # (Si la period est de 0jours c'est que la facture existe déjà.)
             if (period_from<period_to):
-                # Ajout l'item de l'offre correspondant à l'abonnement à la facture
+                # Ajout l'item de l'offre correspondant à l'abonnement
+                # à la facture
                 invoice.details.create(label=offer.name,
                                        amount=offer.period_fees,
                                        quantity=quantity,

+ 32 - 2
coin/billing/models.py

@@ -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()
+

+ 2 - 1
coin/billing/urls.py

@@ -4,5 +4,6 @@ from coin.billing import views
 
 urlpatterns = patterns(
     '',
-    url(r'^invoice/(?P<id>.+).pdf$', views.invoice_pdf, name="invoice_pdf")
+    url(r'^invoice/(?P<id>.+).pdf$', views.invoice_pdf, name="invoice_pdf"),
+    url('invoice/create_all_members_invoices_for_a_period', views.gen_invoices)
 )

+ 4 - 0
coin/billing/views.py

@@ -5,7 +5,11 @@ from django.core.exceptions import PermissionDenied
 from coin.billing.models import Invoice
 from coin.members.models import Member
 from coin.html2pdf import render_as_pdf
+from coin.billing.create_subscriptions_invoices import create_all_members_invoices_for_a_period
 
+def gen_invoices(request):
+    create_all_members_invoices_for_a_period()
+    return HttpResponse('blop')
 
 def invoice_pdf(request, id):
     """