django-orchestra/orchestra/contrib/orders/billing.py

97 lines
3.7 KiB
Python
Raw Permalink Normal View History

from django.utils.translation import gettext_lazy as _
2015-04-05 10:46:24 +00:00
from orchestra.contrib.bills.models import Invoice, Fee, ProForma
2014-09-03 13:56:02 +00:00
class BillsBackend(object):
2014-09-11 14:00:20 +00:00
def create_bills(self, account, lines, **options):
bill = None
ant_bill = None
2014-09-03 14:51:07 +00:00
bills = []
2014-09-19 14:47:25 +00:00
create_new = options.get('new_open', False)
2014-09-22 15:59:53 +00:00
proforma = options.get('proforma', False)
2014-09-10 16:53:09 +00:00
for line in lines:
2015-04-20 14:23:10 +00:00
quantity = line.metric*line.size
if quantity == 0:
continue
2014-09-10 16:53:09 +00:00
service = line.order.service
2014-09-11 14:00:20 +00:00
# Create bill if needed
if proforma:
if ant_bill is None:
2014-09-11 14:00:20 +00:00
if create_new:
bill = ProForma.objects.create(account=account)
else:
2014-09-22 15:59:53 +00:00
bill = ProForma.objects.filter(account=account, is_open=True).last()
2015-11-03 11:09:04 +00:00
if bill:
bill.updated()
else:
2014-09-22 15:59:53 +00:00
bill = ProForma.objects.create(account=account, is_open=True)
bills.append(bill)
2014-09-11 14:00:20 +00:00
else:
bill = ant_bill
ant_bill = bill
elif service.is_fee:
bill = Fee.objects.create(account=account)
bills.append(bill)
else:
if ant_bill is None:
2014-09-11 14:00:20 +00:00
if create_new:
bill = Invoice.objects.create(account=account)
else:
2014-09-22 15:59:53 +00:00
bill = Invoice.objects.filter(account=account, is_open=True).last()
2015-11-03 11:09:04 +00:00
if bill:
bill.updated()
else:
2014-09-22 15:59:53 +00:00
bill = Invoice.objects.create(account=account, is_open=True)
bills.append(bill)
else:
bill = ant_bill
ant_bill = bill
2014-09-11 14:00:20 +00:00
# Create bill line
2015-04-20 14:23:10 +00:00
billine = bill.lines.create(
rate=service.nominal_price,
quantity=line.metric*line.size,
verbose_quantity=self.get_verbose_quantity(line),
subtotal=line.subtotal,
tax=service.tax,
description=self.get_line_description(line),
start_on=line.ini,
end_on=line.end if service.billing_period != service.NEVER else None,
order=line.order,
order_billed_on=line.order.old_billed_on,
order_billed_until=line.order.old_billed_until
)
self.create_sublines(billine, line.discounts)
2014-09-03 14:51:07 +00:00
return bills
2014-09-03 13:56:02 +00:00
2015-04-20 14:23:10 +00:00
# def format_period(self, ini, end):
# 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-11 14:00:20 +00:00
def get_line_description(self, line):
service = line.order.service
2014-09-23 14:01:58 +00:00
description = line.order.description
return description
2015-04-14 14:29:22 +00:00
def get_verbose_quantity(self, line):
metric = format(line.metric, '.2f').rstrip('0').rstrip('.')
2016-04-06 19:00:16 +00:00
metric = metric.strip('0').strip('.')
2015-04-14 14:29:22 +00:00
size = format(line.size, '.2f').rstrip('0').rstrip('.')
2016-04-06 19:00:16 +00:00
size = size.strip('0').strip('.')
2015-04-14 14:29:22 +00:00
if metric == '1':
return size
if size == '1':
return metric
return "%s×%s" % (metric, size)
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-26 15:05:20 +00:00
description=_("Discount per %s") % discount.type.lower(),
2014-09-10 16:53:09 +00:00
total=discount.total,
2014-09-26 15:05:20 +00:00
type=discount.type,
2014-09-03 13:56:02 +00:00
)