django-orchestra-test/orchestra/apps/orders/billing.py

57 lines
2.1 KiB
Python
Raw Normal View History

2014-09-03 13:56:02 +00:00
import datetime
from django.utils.translation import ugettext_lazy as _
2014-09-03 13:56:02 +00:00
from orchestra.apps.bills.models import Invoice, Fee, BillLine, BillSubline
class BillsBackend(object):
def create_bills(self, account, lines):
invoice = None
2014-09-03 14:51:07 +00:00
bills = []
2014-09-10 16:53:09 +00:00
for line in lines:
service = line.order.service
2014-09-03 13:56:02 +00:00
if service.is_fee:
2014-09-03 14:51:07 +00:00
fee, __ = Fee.objects.get_or_create(account=account, status=Fee.OPEN)
2014-09-10 16:53:09 +00:00
storedline = fee.lines.create(
2014-09-03 14:51:07 +00:00
rate=service.nominal_price,
2014-09-10 16:53:09 +00:00
amount=line.size,
total=line.subtotal, tax=0,
description=self.format_period(line.ini, line.end),
2014-09-03 14:51:07 +00:00
)
2014-09-10 16:53:09 +00:00
self.create_sublines(storedline, line.discounts)
2014-09-03 14:51:07 +00:00
bills.append(fee)
2014-09-03 13:56:02 +00:00
else:
if invoice is None:
invoice, __ = Invoice.objects.get_or_create(account=account,
status=Invoice.OPEN)
2014-09-03 14:51:07 +00:00
bills.append(invoice)
2014-09-10 16:53:09 +00:00
description = line.order.description
2014-09-03 13:56:02 +00:00
if service.billing_period != service.NEVER:
2014-09-10 16:53:09 +00:00
description += " %s" % self.format_period(line.ini, line.end)
storedline = invoice.lines.create(
2014-09-03 13:56:02 +00:00
description=description,
rate=service.nominal_price,
2014-09-10 16:53:09 +00:00
amount=line.size,
# TODO rename line.total > subtotal
total=line.subtotal,
2014-09-03 13:56:02 +00:00
tax=service.tax,
)
2014-09-10 16:53:09 +00:00
self.create_sublines(storedline, line.discounts)
2014-09-03 14:51:07 +00:00
return bills
2014-09-03 13:56:02 +00:00
def format_period(self, ini, end):
2014-09-10 16:53:09 +00:00
ini = ini.strftime("%b, %Y")
end = (end-datetime.timedelta(seconds=1)).strftime("%b, %Y")
if ini == end:
return ini
return _("{ini} to {end}").format(ini=ini, end=end)
2014-09-03 13:56:02 +00:00
def create_sublines(self, line, discounts):
2014-09-10 16:53:09 +00:00
for discount in discounts:
2014-09-03 13:56:02 +00:00
line.sublines.create(
2014-09-10 16:53:09 +00:00
description=_("Discount per %s") % discount.type,
total=discount.total,
2014-09-03 13:56:02 +00:00
)