Browse Source

Method generate_invoice_for_a_period which generate invoice for a member for a billing period

Fabs 11 years ago
parent
commit
e45b15faf1
3 changed files with 76 additions and 3 deletions
  1. 72 0
      coin/billing/generate_invoices.py
  2. 2 1
      coin/billing/models.py
  3. 2 2
      coin/billing/urls.py

+ 72 - 0
coin/billing/generate_invoices.py

@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+import datetime
+from dateutil.relativedelta import relativedelta
+from django.http import HttpResponse
+from django.db.models import Q
+from coin.offers.models import Offer, OfferSubscription
+from coin.members.models import Member
+from coin.billing.models import Invoice, InvoiceDetail
+
+
+def generate_missing_invoices(request):
+	"""
+	Pour chaque abonnement (OfferSubscription):
+		Vérifie pour chaque période de facturation si elle a été facturée
+		Sinon génère la facture correspondante
+	"""
+	members = Member.objects.filter(
+		Q(offersubscription__resign_date__isnull=True) | Q(offersubscription__resign_date__gte=datetime.date.today()))
+	offer_subscriptions = OfferSubscription.objects.filter(
+		Q(resign_date__isnull=True) | Q(resign_date__gte=datetime.date.today()))
+	sortie = ""
+	for member in members:
+		sortie += '<br /> %s - %s' % (member, 
+			generate_invoice_for_a_period(member, datetime.date(2013,12,5)))
+
+	return HttpResponse(sortie)
+
+
+def generate_invoice_for_a_period(member, date):
+	"""
+	Génère si necessaire une facture pour un membre en prenant la date passée 
+	en paramètre comme premier mois de période. Renvoi la facture générée
+	ou Non si aucune facture n'était necessaire.
+	"""
+	invoice = None
+
+
+	offer_subscriptions = OfferSubscription.objects.filter(
+		Q(resign_date__isnull=True) | Q(resign_date__gte=datetime.date.today()),
+		member__exact=member.pk)
+
+	for offer_subscription in offer_subscriptions:
+		offer = offer_subscription.offer
+
+		invoice_test = Invoice.objects.filter(
+			period_from__lte=date,
+			period_to__gte=date,
+			details__offer__exact=offer.pk,
+			member__exact=member.pk)
+
+		if not invoice_test.exists():			
+			if invoice == None:
+				invoice = Invoice.objects.create(
+					period_from=datetime.date(date.year,date.month,1),
+					period_to=datetime.date(date.year,date.month,1),
+					date_due=datetime.date.today(),
+					member=member
+				)
+
+			new_period_to = (datetime.date(date.year,date.month,1) +
+							relativedelta(months = +offer.billing_period) -
+							relativedelta(days = +1))
+			if new_period_to > invoice.period_to:
+				invoice.period_to=new_period_to
+			
+			invoice.details.create(label=offer.name,
+				amount=offer.period_fees,
+				offer=offer)
+			print invoice.period_to
+			invoice.save();
+
+	return invoice

+ 2 - 1
coin/billing/models.py

@@ -48,7 +48,8 @@ class Invoice(models.Model):
       "Calcul le montant de la facture en fonction des éléments de détails"
       total = Decimal('0.0')
       for detail in self.details.all():
-        total += detail.amount * (detail.tax / Decimal('100.0') + Decimal('1.0'))
+        total += detail.amount * (detail.tax / Decimal('100.0') +
+                                  Decimal('1.0'))
       return total.quantize(Decimal('0.01'))
 
     @staticmethod

+ 2 - 2
coin/billing/urls.py

@@ -1,11 +1,11 @@
 from django.conf.urls import patterns, url
 from django.views.generic import DetailView
-from coin.billing import views
-
+from coin.billing import views, generate_invoices
 
 urlpatterns = patterns(
     '',
     url(r'^pdf_test$', views.pdf_test, name='pdf_test'),
     url(r'^invoice/(?P<pk>\d+)/pdf$', views.invoice_pdf),
+    url(r'^generate_invoices$', generate_invoices.generate_missing_invoices),
 
 )