django-orchestra/orchestra/apps/orders/backends.py

56 lines
2 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-03 13:56:02 +00:00
for order, nominal_price, size, ini, end, discounts in lines:
service = order.service
if service.is_fee:
2014-09-03 14:51:07 +00:00
fee, __ = Fee.objects.get_or_create(account=account, status=Fee.OPEN)
line = fee.lines.create(
rate=service.nominal_price,
amount=size,
total=nominal_price, tax=0,
description=self.format_period(ini, end),
2014-09-03 14:51:07 +00:00
)
2014-09-03 13:56:02 +00:00
self.create_sublines(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-03 13:56:02 +00:00
description = order.description
if service.billing_period != service.NEVER:
description += " %s" % self.format_period(ini, end)
2014-09-03 13:56:02 +00:00
line = invoice.lines.create(
description=description,
rate=service.nominal_price,
amount=size,
total=nominal_price,
tax=service.tax,
)
self.create_sublines(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):
ini = 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):
for name, value in discounts:
line.sublines.create(
description=_("Discount per %s") % name,
total=value,
)