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

91 lines
3.6 KiB
Python
Raw Normal View History

2014-09-03 13:56:02 +00:00
import datetime
from django.utils.translation import ugettext_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
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:
service = line.order.service
2014-09-11 14:00:20 +00:00
# Create bill if needed
if bill is None or service.is_fee:
2014-09-22 15:59:53 +00:00
if proforma:
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()
if not bill:
bill = ProForma.objects.create(account=account, is_open=True)
2014-09-11 14:00:20 +00:00
elif service.is_fee:
bill = Fee.objects.create(account=account)
else:
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()
if not bill:
bill = Invoice.objects.create(account=account, is_open=True)
2014-09-11 14:00:20 +00:00
bills.append(bill)
# Create bill line
2015-04-14 14:29:22 +00:00
quantity = line.metric*line.size
if quantity != 0:
billine = bill.lines.create(
2014-09-03 13:56:02 +00:00
rate=service.nominal_price,
2014-09-23 14:01:58 +00:00
quantity=line.metric*line.size,
2015-04-14 14:29:22 +00:00
verbose_quantity=self.get_verbose_quantity(line),
2014-09-18 15:07:39 +00:00
subtotal=line.subtotal,
2014-09-03 13:56:02 +00:00
tax=service.tax,
2014-09-11 14:00:20 +00:00
description=self.get_line_description(line),
2014-09-26 15:05:20 +00:00
order=line.order,
order_billed_on=line.order.old_billed_on,
order_billed_until=line.order.old_billed_until
2015-04-14 14:29:22 +00:00
)
self.create_sublines(billine, 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-11 14:00:20 +00:00
def get_line_description(self, line):
service = line.order.service
if service.is_fee:
return self.format_period(line.ini, line.end)
2014-09-23 14:01:58 +00:00
description = line.order.description
if service.billing_period != service.NEVER:
description += " %s" % self.format_period(line.ini, line.end)
return description
2015-04-14 14:29:22 +00:00
def get_verbose_quantity(self, line):
# service = line.order.service
# if service.metric and service.billing_period != service.NEVER and service.pricing_period == service.NEVER:
metric = format(line.metric, '.2f').rstrip('0').rstrip('.')
if metric.endswith('.00'):
metric = metric.split('.')[0]
size = format(line.size, '.2f').rstrip('0').rstrip('.')
if size.endswith('.00'):
size = metric.split('.')[0]
if metric == '1':
return size
if size == '1':
return metric
return "%s×%s" % (metric, size)
# return ''
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
)