django-orchestra/orchestra/apps/bills/models.py

289 lines
9.7 KiB
Python
Raw Normal View History

2014-08-22 11:28:46 +00:00
import inspect
from dateutil.relativedelta import relativedelta
2014-08-22 11:28:46 +00:00
2014-07-08 15:19:15 +00:00
from django.db import models
2014-08-19 18:59:23 +00:00
from django.template import loader, Context
2014-07-23 16:24:56 +00:00
from django.utils import timezone
2014-09-18 15:07:39 +00:00
from django.utils.encoding import force_text
2014-08-19 18:59:23 +00:00
from django.utils.functional import cached_property
2014-07-23 16:24:56 +00:00
from django.utils.translation import ugettext_lazy as _
2014-08-19 18:59:23 +00:00
from orchestra.apps.accounts.models import Account
from orchestra.core import accounts
2014-09-03 13:56:02 +00:00
from orchestra.utils.functional import cached
2014-09-04 15:55:43 +00:00
from orchestra.utils.html import html_to_pdf
2014-07-23 16:24:56 +00:00
from . import settings
class BillManager(models.Manager):
def get_queryset(self):
queryset = super(BillManager, self).get_queryset()
if self.model != Bill:
2014-08-29 12:45:27 +00:00
bill_type = self.model.get_class_type()
2014-08-19 18:59:23 +00:00
queryset = queryset.filter(type=bill_type)
2014-07-23 16:24:56 +00:00
return queryset
2014-07-08 15:19:15 +00:00
class Bill(models.Model):
2014-09-18 15:07:39 +00:00
OPEN = ''
2014-07-23 16:24:56 +00:00
PAID = 'PAID'
2014-09-18 15:07:39 +00:00
PENDING = 'PENDING'
2014-07-23 16:24:56 +00:00
BAD_DEBT = 'BAD_DEBT'
2014-09-18 15:07:39 +00:00
PAYMENT_STATES = (
2014-07-23 16:24:56 +00:00
(PAID, _("Paid")),
2014-09-18 15:07:39 +00:00
(PENDING, _("Pending")),
2014-07-23 16:24:56 +00:00
(BAD_DEBT, _("Bad debt")),
)
2014-09-24 20:09:41 +00:00
BILL = 'BILL'
INVOICE = 'INVOICE'
AMENDMENTINVOICE = 'AMENDMENTINVOICE'
FEE = 'FEE'
AMENDMENTFEE = 'AMENDMENTFEE'
PROFORMA = 'PROFORMA'
2014-07-23 16:24:56 +00:00
TYPES = (
2014-09-24 20:09:41 +00:00
(INVOICE, _("Invoice")),
(AMENDMENTINVOICE, _("Amendment invoice")),
(FEE, _("Fee")),
(AMENDMENTFEE, _("Amendment Fee")),
(PROFORMA, _("Pro forma")),
2014-07-23 16:24:56 +00:00
)
2014-09-24 20:09:41 +00:00
number = models.CharField(_("number"), max_length=16, unique=True, blank=True)
2014-07-23 16:24:56 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
related_name='%(class)s')
2014-08-19 18:59:23 +00:00
type = models.CharField(_("type"), max_length=16, choices=TYPES)
2014-07-23 16:24:56 +00:00
created_on = models.DateTimeField(_("created on"), auto_now_add=True)
closed_on = models.DateTimeField(_("closed on"), blank=True, null=True)
2014-09-19 14:47:25 +00:00
# TODO rename to is_closed
2014-09-18 15:07:39 +00:00
is_open = models.BooleanField(_("is open"), default=True)
is_sent = models.BooleanField(_("is sent"), default=False)
2014-09-03 14:51:07 +00:00
due_on = models.DateField(_("due on"), null=True, blank=True)
2014-07-23 16:24:56 +00:00
last_modified_on = models.DateTimeField(_("last modified on"), auto_now=True)
2014-09-10 16:53:09 +00:00
total = models.DecimalField(max_digits=12, decimal_places=2, default=0)
2014-07-23 16:24:56 +00:00
comments = models.TextField(_("comments"), blank=True)
html = models.TextField(_("HTML"), blank=True)
objects = BillManager()
2014-09-22 15:59:53 +00:00
class Meta:
2014-09-24 20:09:41 +00:00
get_latest_by = 'id'
2014-09-22 15:59:53 +00:00
2014-07-23 16:24:56 +00:00
def __unicode__(self):
2014-08-22 11:28:46 +00:00
return self.number
2014-07-23 16:24:56 +00:00
2014-08-19 18:59:23 +00:00
@cached_property
def seller(self):
return Account.get_main().invoicecontact
@cached_property
def buyer(self):
return self.account.invoicecontact
2014-09-18 15:07:39 +00:00
@cached_property
def payment_state(self):
if self.is_open:
return self.OPEN
secured = self.transactions.secured().amount()
if secured >= self.total:
return self.PAID
elif self.transactions.exclude_rejected().exists():
return self.PENDING
return self.BAD_DEBT
def get_payment_state_display(self):
value = self.payment_state
return force_text(dict(self.PAYMENT_STATES).get(value, value))
2014-07-23 16:24:56 +00:00
@classmethod
2014-08-22 11:28:46 +00:00
def get_class_type(cls):
2014-07-23 16:24:56 +00:00
return cls.__name__.upper()
2014-08-22 11:28:46 +00:00
def get_type(self):
return self.type or self.get_class_type()
def set_number(self):
2014-07-23 16:24:56 +00:00
cls = type(self)
2014-08-22 11:28:46 +00:00
bill_type = self.get_type()
2014-09-24 20:09:41 +00:00
if bill_type == self.BILL:
raise TypeError('This method can not be used on BILL instances')
2014-08-22 11:28:46 +00:00
prefix = getattr(settings, 'BILLS_%s_NUMBER_PREFIX' % bill_type)
2014-09-18 15:07:39 +00:00
if self.is_open:
2014-07-23 16:24:56 +00:00
prefix = 'O{}'.format(prefix)
bills = cls.objects.filter(number__regex=r'^%s[1-9]+' % prefix)
last_number = bills.order_by('-number').values_list('number', flat=True).first()
if last_number is None:
last_number = 0
2014-07-23 16:24:56 +00:00
else:
last_number = int(last_number[len(prefix)+4:])
number = last_number + 1
year = timezone.now().strftime("%Y")
number_length = settings.BILLS_NUMBER_LENGTH
zeros = (number_length - len(str(number))) * '0'
number = zeros + str(number)
2014-09-24 20:09:41 +00:00
self.number = '{prefix}{year}{number}'.format(prefix=prefix, year=year, number=number)
2014-07-23 16:24:56 +00:00
def get_due_date(self, payment=None):
now = timezone.now()
if payment:
return now + payment.get_due_delta()
return now + relativedelta(months=1)
def close(self, payment=False):
2014-09-18 15:07:39 +00:00
assert self.is_open, "Bill not in Open state"
if payment is False:
payment = self.account.paymentsources.get_default()
if not self.due_on:
self.due_on = self.get_due_date(payment=payment)
2014-09-08 15:10:16 +00:00
self.total = self.get_total()
self.html = self.render(payment=payment)
2014-09-24 20:09:41 +00:00
if self.get_type() != self.PROFORMA:
2014-09-19 14:47:25 +00:00
self.transactions.create(bill=self, source=payment, amount=self.total)
self.closed_on = timezone.now()
2014-09-18 15:07:39 +00:00
self.is_open = False
self.is_sent = False
2014-09-04 15:55:43 +00:00
self.save()
def send(self):
from orchestra.apps.contacts.models import Contact
self.account.send_email(
template=settings.BILLS_EMAIL_NOTIFICATION_TEMPLATE,
context={
'bill': self,
},
contacts=(Contact.BILLING,),
attachments=[
('%s.pdf' % self.number, html_to_pdf(self.html), 'application/pdf')
]
)
2014-09-18 15:07:39 +00:00
self.is_sent = True
self.save()
def render(self, payment=False):
if payment is False:
payment = self.account.paymentsources.get_default()
2014-08-19 18:59:23 +00:00
context = Context({
'bill': self,
'lines': self.lines.all().prefetch_related('sublines'),
2014-08-19 18:59:23 +00:00
'seller': self.seller,
'buyer': self.buyer,
'seller_info': {
'phone': settings.BILLS_SELLER_PHONE,
'website': settings.BILLS_SELLER_WEBSITE,
'email': settings.BILLS_SELLER_EMAIL,
'bank_account': settings.BILLS_SELLER_BANK_ACCOUNT,
2014-08-19 18:59:23 +00:00
},
'currency': settings.BILLS_CURRENCY,
'payment': payment and payment.get_bill_context(),
'default_due_date': self.get_due_date(payment=payment),
'now': timezone.now(),
2014-08-19 18:59:23 +00:00
})
2014-09-24 20:09:41 +00:00
template_name = 'BILLS_%s_TEMPLATE' % self.get_type()
template = getattr(settings, template_name, settings.BILLS_DEFAULT_TEMPLATE)
2014-08-19 18:59:23 +00:00
bill_template = loader.get_template(template)
html = bill_template.render(context)
html = html.replace('-pageskip-', '<pdf:nextpage />')
return html
2014-07-23 16:24:56 +00:00
def save(self, *args, **kwargs):
2014-08-19 18:59:23 +00:00
if not self.type:
2014-08-22 11:28:46 +00:00
self.type = self.get_type()
2014-09-18 15:07:39 +00:00
if not self.number or (self.number.startswith('O') and not self.is_open):
2014-08-22 11:28:46 +00:00
self.set_number()
2014-07-23 16:24:56 +00:00
super(Bill, self).save(*args, **kwargs)
2014-09-03 13:56:02 +00:00
def get_subtotals(self):
subtotals = {}
2014-09-06 10:56:30 +00:00
for line in self.lines.all():
2014-09-03 13:56:02 +00:00
subtotal, taxes = subtotals.get(line.tax, (0, 0))
2014-09-10 16:53:09 +00:00
subtotal += line.get_total()
2014-09-03 13:56:02 +00:00
subtotals[line.tax] = (subtotal, (line.tax/100)*subtotal)
return subtotals
def get_total(self):
total = 0
for tax, subtotal in self.get_subtotals().iteritems():
subtotal, taxes = subtotal
total += subtotal + taxes
return total
2014-07-08 15:19:15 +00:00
2014-07-23 16:24:56 +00:00
class Invoice(Bill):
class Meta:
proxy = True
class AmendmentInvoice(Bill):
class Meta:
proxy = True
class Fee(Bill):
class Meta:
proxy = True
2014-07-08 15:19:15 +00:00
2014-07-23 16:24:56 +00:00
class AmendmentFee(Bill):
class Meta:
proxy = True
2014-09-11 14:00:20 +00:00
class ProForma(Bill):
2014-07-23 16:24:56 +00:00
class Meta:
proxy = True
2014-09-11 14:00:20 +00:00
class BillLine(models.Model):
""" Base model for bill item representation """
2014-09-11 14:00:20 +00:00
bill = models.ForeignKey(Bill, verbose_name=_("bill"), related_name='lines')
description = models.CharField(_("description"), max_length=256)
2014-09-24 20:09:41 +00:00
rate = models.DecimalField(_("rate"), blank=True, null=True, max_digits=12, decimal_places=2)
2014-09-18 15:07:39 +00:00
quantity = models.DecimalField(_("quantity"), max_digits=12, decimal_places=2)
subtotal = models.DecimalField(_("subtotal"), max_digits=12, decimal_places=2)
2014-09-03 13:56:02 +00:00
tax = models.PositiveIntegerField(_("tax"))
2014-09-14 09:52:45 +00:00
# TODO
# order_id = models.ForeignKey('orders.Order', null=True, blank=True,
# help_text=_("Informative link back to the order"))
2014-09-11 14:00:20 +00:00
amended_line = models.ForeignKey('self', verbose_name=_("amended line"),
related_name='amendment_lines', null=True, blank=True)
2014-08-22 11:28:46 +00:00
def __unicode__(self):
return "#%i" % self.number
2014-09-03 13:56:02 +00:00
@cached_property
2014-08-22 11:28:46 +00:00
def number(self):
lines = type(self).objects.filter(bill=self.bill_id)
return lines.filter(id__lte=self.id).order_by('id').count()
2014-09-10 16:53:09 +00:00
def get_total(self):
""" Computes subline discounts """
2014-09-18 15:07:39 +00:00
total = self.subtotal
2014-09-10 16:53:09 +00:00
for subline in self.sublines.all():
2014-09-18 15:07:39 +00:00
total += subline.total
return total
2014-09-10 16:53:09 +00:00
def save(self, *args, **kwargs):
2014-09-24 20:09:41 +00:00
# TODO cost and consistency of this shit
2014-09-10 16:53:09 +00:00
super(BillLine, self).save(*args, **kwargs)
2014-09-18 15:07:39 +00:00
if self.bill.is_open:
2014-09-10 16:53:09 +00:00
self.bill.total = self.bill.get_total()
self.bill.save()
2014-07-23 16:24:56 +00:00
2014-09-03 13:56:02 +00:00
class BillSubline(models.Model):
""" Subline used for describing an item discount """
2014-09-24 20:09:41 +00:00
line = models.ForeignKey(BillLine, verbose_name=_("bill line"), related_name='sublines')
description = models.CharField(_("description"), max_length=256)
total = models.DecimalField(max_digits=12, decimal_places=2)
# TODO type ? Volume and Compensation
2014-09-10 16:53:09 +00:00
def save(self, *args, **kwargs):
# TODO cost of this shit
super(BillSubline, self).save(*args, **kwargs)
2014-09-18 15:07:39 +00:00
if self.line.bill.is_open:
2014-09-10 16:53:09 +00:00
self.line.bill.total = self.line.bill.get_total()
self.line.bill.save()
2014-09-11 14:00:20 +00:00
accounts.register(Bill)