|
@@ -1,12 +1,16 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
import datetime
|
|
|
import random
|
|
|
+import uuid
|
|
|
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 OfferSubscription
|
|
|
from coin.members.models import Member
|
|
|
+from coin.html2pdf import render_as_pdf
|
|
|
|
|
|
|
|
|
def next_invoice_number():
|
|
@@ -16,22 +20,21 @@ def next_invoice_number():
|
|
|
random.randrange(100, 999),
|
|
|
random.randrange(100, 999))
|
|
|
|
|
|
-
|
|
|
class Invoice(models.Model):
|
|
|
|
|
|
INVOICES_STATUS_CHOICES = (
|
|
|
- ('draft', u'Brouillon'),
|
|
|
('open', u'A payer'),
|
|
|
('closed', u'Reglée'),
|
|
|
('trouble', u'Litige')
|
|
|
)
|
|
|
|
|
|
+ validated = models.BooleanField(default=False, verbose_name='Facture validée')
|
|
|
number = models.CharField(max_length=25,
|
|
|
default=next_invoice_number,
|
|
|
unique=True,
|
|
|
verbose_name='Numéro')
|
|
|
status = models.CharField(max_length=50, choices=INVOICES_STATUS_CHOICES,
|
|
|
- default='draft',
|
|
|
+ default='open',
|
|
|
verbose_name='Statut')
|
|
|
date = models.DateField(default=datetime.date.today, null=True)
|
|
|
date_due = models.DateField(
|
|
@@ -44,6 +47,9 @@ class Invoice(models.Model):
|
|
|
related_name='invoices',
|
|
|
verbose_name='Membre',
|
|
|
on_delete=models.SET_NULL)
|
|
|
+ pdf = models.FileField(upload_to='invoices',
|
|
|
+ null=True, blank=True,
|
|
|
+ verbose_name=u'Facture en PDF')
|
|
|
|
|
|
def amount(self):
|
|
|
"Calcul le montant de la facture en fonction des éléments de détails"
|
|
@@ -73,7 +79,25 @@ class Invoice(models.Model):
|
|
|
|
|
|
def has_owner(self, username):
|
|
|
"Check if passed username (ex gmajax) is owner of the invoice"
|
|
|
- return (self.username == username)
|
|
|
+ return (self.member.username == username)
|
|
|
+
|
|
|
+ def generate_pdf(self):
|
|
|
+ "Make and store a pdf file for the invoice"
|
|
|
+ pdf_file = render_as_pdf('billing/invoice_pdf.html', {"invoice": self})
|
|
|
+ self.pdf.save(u'%s_%s.pdf' % (self.number, uuid.uuid4()), pdf_file)
|
|
|
+
|
|
|
+ def validate(self):
|
|
|
+ """
|
|
|
+ Switch invoice to validate mode. This set to False the draft field
|
|
|
+ and generate the pdf
|
|
|
+ """
|
|
|
+ # if not self.is_validated():
|
|
|
+ self.validated = True
|
|
|
+ self.save()
|
|
|
+ self.generate_pdf()
|
|
|
+
|
|
|
+ def is_validated(self):
|
|
|
+ return self.validated and bool(self.pdf)
|
|
|
|
|
|
def __unicode__(self):
|
|
|
return u'#%s %0.2f€ %s' % (self.number, self.amount(), self.date_due)
|