2014-09-02 15:48:07 +00:00
|
|
|
import calendar
|
2014-09-03 13:56:02 +00:00
|
|
|
import datetime
|
2014-09-02 15:48:07 +00:00
|
|
|
|
2014-09-03 13:56:02 +00:00
|
|
|
from dateutil import relativedelta
|
2014-07-21 12:20:04 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2014-09-02 15:48:07 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.utils import timezone
|
2014-07-21 12:20:04 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.utils import plugins
|
2014-09-10 16:53:09 +00:00
|
|
|
from orchestra.utils.python import AttributeDict
|
2014-07-21 12:20:04 +00:00
|
|
|
|
2014-09-14 09:52:45 +00:00
|
|
|
from . import settings, helpers
|
2014-09-02 15:48:07 +00:00
|
|
|
|
2014-07-21 12:20:04 +00:00
|
|
|
|
|
|
|
class ServiceHandler(plugins.Plugin):
|
2014-09-02 15:48:07 +00:00
|
|
|
"""
|
|
|
|
Separates all the logic of billing handling from the model allowing to better
|
|
|
|
customize its behaviout
|
|
|
|
"""
|
|
|
|
|
2014-07-21 12:20:04 +00:00
|
|
|
model = None
|
|
|
|
|
|
|
|
__metaclass__ = plugins.PluginMount
|
|
|
|
|
|
|
|
def __init__(self, service):
|
|
|
|
self.service = service
|
|
|
|
|
2014-07-21 15:43:36 +00:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.service, attr)
|
|
|
|
|
2014-07-21 12:20:04 +00:00
|
|
|
@classmethod
|
|
|
|
def get_plugin_choices(cls):
|
|
|
|
choices = super(ServiceHandler, cls).get_plugin_choices()
|
|
|
|
return [('', _("Default"))] + choices
|
|
|
|
|
2014-07-21 15:43:36 +00:00
|
|
|
def get_content_type(self):
|
|
|
|
if not self.model:
|
|
|
|
return self.content_type
|
|
|
|
app_label, model = self.model.split('.')
|
|
|
|
return ContentType.objects.get_by_natural_key(app_label, model.lower())
|
2014-07-21 12:20:04 +00:00
|
|
|
|
|
|
|
def matches(self, instance):
|
|
|
|
safe_locals = {
|
|
|
|
instance._meta.model_name: instance
|
|
|
|
}
|
|
|
|
return eval(self.match, safe_locals)
|
|
|
|
|
|
|
|
def get_metric(self, instance):
|
2014-07-21 15:43:36 +00:00
|
|
|
if self.metric:
|
|
|
|
safe_locals = {
|
|
|
|
instance._meta.model_name: instance
|
|
|
|
}
|
|
|
|
return eval(self.metric, safe_locals)
|
2014-09-02 15:48:07 +00:00
|
|
|
|
|
|
|
def get_billing_point(self, order, bp=None, **options):
|
2014-09-03 13:56:02 +00:00
|
|
|
not_cachable = self.billing_point == self.FIXED_DATE and options.get('fixed_point')
|
2014-09-02 15:48:07 +00:00
|
|
|
if not_cachable or bp is None:
|
|
|
|
bp = options.get('billing_point', timezone.now().date())
|
|
|
|
if not options.get('fixed_point'):
|
2014-09-03 13:56:02 +00:00
|
|
|
msg = ("Support for '%s' period and '%s' point is not implemented"
|
|
|
|
% (self.get_billing_period_display(), self.get_billing_point_display()))
|
|
|
|
if self.billing_period == self.MONTHLY:
|
2014-09-02 15:48:07 +00:00
|
|
|
date = bp
|
2014-09-03 13:56:02 +00:00
|
|
|
if self.payment_style == self.PREPAY:
|
|
|
|
date += relativedelta.relativedelta(months=1)
|
|
|
|
if self.billing_point == self.ON_REGISTER:
|
2014-09-02 15:48:07 +00:00
|
|
|
day = order.registered_on.day
|
2014-09-03 13:56:02 +00:00
|
|
|
elif self.billing_point == self.FIXED_DATE:
|
2014-09-02 15:48:07 +00:00
|
|
|
day = 1
|
2014-09-03 13:56:02 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(msg)
|
2014-09-10 16:53:09 +00:00
|
|
|
bp = datetime.datetime(year=date.year, month=date.month, day=day,
|
|
|
|
tzinfo=timezone.get_current_timezone())
|
2014-09-03 13:56:02 +00:00
|
|
|
elif self.billing_period == self.ANUAL:
|
|
|
|
if self.billing_point == self.ON_REGISTER:
|
2014-09-02 15:48:07 +00:00
|
|
|
month = order.registered_on.month
|
|
|
|
day = order.registered_on.day
|
2014-09-03 13:56:02 +00:00
|
|
|
elif self.billing_point == self.FIXED_DATE:
|
2014-09-02 15:48:07 +00:00
|
|
|
month = settings.ORDERS_SERVICE_ANUAL_BILLING_MONTH
|
|
|
|
day = 1
|
2014-09-03 13:56:02 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(msg)
|
2014-09-02 15:48:07 +00:00
|
|
|
year = bp.year
|
2014-09-03 13:56:02 +00:00
|
|
|
if self.payment_style == self.POSTPAY:
|
|
|
|
year = bo.year - relativedelta.relativedelta(years=1)
|
2014-09-02 15:48:07 +00:00
|
|
|
if bp.month >= month:
|
|
|
|
year = bp.year + 1
|
|
|
|
bp = datetime.datetime(year=year, month=month, day=day,
|
|
|
|
tzinfo=timezone.get_current_timezone())
|
2014-09-03 13:56:02 +00:00
|
|
|
elif self.billing_period == self.NEVER:
|
2014-09-02 15:48:07 +00:00
|
|
|
bp = order.registered_on
|
|
|
|
else:
|
2014-09-03 13:56:02 +00:00
|
|
|
raise NotImplementedError(msg)
|
|
|
|
if self.on_cancel != self.NOTHING and order.cancelled_on and order.cancelled_on < bp:
|
2014-09-02 15:48:07 +00:00
|
|
|
return order.cancelled_on
|
|
|
|
return bp
|
|
|
|
|
|
|
|
def get_pricing_size(self, ini, end):
|
|
|
|
rdelta = relativedelta.relativedelta(end, ini)
|
2014-09-03 13:56:02 +00:00
|
|
|
if self.get_pricing_period() == self.MONTHLY:
|
2014-09-02 15:48:07 +00:00
|
|
|
size = rdelta.months
|
2014-09-03 13:56:02 +00:00
|
|
|
days = calendar.monthrange(end.year, end.month)[1]
|
|
|
|
size += float(rdelta.days)/days
|
|
|
|
elif self.get_pricing_period() == self.ANUAL:
|
2014-09-02 15:48:07 +00:00
|
|
|
size = rdelta.years
|
2014-09-03 13:56:02 +00:00
|
|
|
days = 366 if calendar.isleap(end.year) else 365
|
|
|
|
size += float((end-ini).days)/days
|
|
|
|
elif self.get_pricing_period() == self.NEVER:
|
2014-09-02 15:48:07 +00:00
|
|
|
size = 1
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
return size
|
|
|
|
|
|
|
|
def get_pricing_slots(self, ini, end):
|
|
|
|
period = self.get_pricing_period()
|
2014-09-03 13:56:02 +00:00
|
|
|
if period == self.MONTHLY:
|
|
|
|
rdelta = relativedelta.relativedelta(months=1)
|
|
|
|
elif period == self.ANUAL:
|
|
|
|
rdelta = relativedelta.relativedelta(years=1)
|
|
|
|
elif period == self.NEVER:
|
2014-09-02 15:48:07 +00:00
|
|
|
yield ini, end
|
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
while True:
|
|
|
|
next = ini + rdelta
|
|
|
|
if next >= end:
|
|
|
|
yield ini, end
|
|
|
|
break
|
|
|
|
yield ini, next
|
|
|
|
ini = next
|
|
|
|
|
2014-09-03 13:56:02 +00:00
|
|
|
def get_price_with_orders(self, order, size, ini, end):
|
|
|
|
porders = self.orders.filter(account=order.account).filter(
|
|
|
|
Q(cancelled_on__isnull=True) | Q(cancelled_on__gt=ini)
|
2014-09-08 14:23:06 +00:00
|
|
|
).filter(registered_on__lt=end).order_by('registered_on')
|
2014-09-03 13:56:02 +00:00
|
|
|
price = 0
|
|
|
|
if self.orders_effect == self.REGISTER_OR_RENEW:
|
2014-09-14 09:52:45 +00:00
|
|
|
events = helpers.get_register_or_renew_events(porders, order, ini, end)
|
2014-09-03 13:56:02 +00:00
|
|
|
elif self.orders_effect == self.CONCURRENT:
|
2014-09-14 09:52:45 +00:00
|
|
|
events = helpers.get_register_or_cancel_events(porders, order, ini, end)
|
2014-09-03 13:56:02 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2014-09-08 14:23:06 +00:00
|
|
|
for metric, position, ratio in events:
|
|
|
|
price += self.get_price(order, metric, position=position) * size * ratio
|
2014-09-03 13:56:02 +00:00
|
|
|
return price
|
|
|
|
|
|
|
|
def get_price_with_metric(self, order, size, ini, end):
|
|
|
|
metric = order.get_metric(ini, end)
|
2014-09-08 14:23:06 +00:00
|
|
|
price = self.get_price(order, metric) * size
|
2014-09-03 13:56:02 +00:00
|
|
|
return price
|
|
|
|
|
2014-09-10 16:53:09 +00:00
|
|
|
def generate_line(self, order, price, size, ini, end):
|
|
|
|
subtotal = float(self.nominal_price) * size
|
2014-09-03 13:56:02 +00:00
|
|
|
discounts = []
|
2014-09-10 16:53:09 +00:00
|
|
|
if subtotal > price:
|
|
|
|
discounts.append(AttributeDict(**{
|
|
|
|
'type': 'volume',
|
|
|
|
'total': price-subtotal
|
|
|
|
}))
|
|
|
|
elif subtotal < price:
|
|
|
|
raise ValueError("Something is wrong!")
|
|
|
|
return AttributeDict(**{
|
|
|
|
'order': order,
|
|
|
|
'subtotal': subtotal,
|
|
|
|
'size': size,
|
|
|
|
'ini': ini,
|
|
|
|
'end': end,
|
|
|
|
'discounts': discounts,
|
|
|
|
})
|
|
|
|
|
2014-09-14 19:36:27 +00:00
|
|
|
def _generate_bill_lines(self, orders, account, **options):
|
2014-09-14 09:52:45 +00:00
|
|
|
# For the "boundary conditions" just think that:
|
|
|
|
# date(2011, 1, 1) is equivalent to datetime(2011, 1, 1, 0, 0, 0)
|
|
|
|
# In most cases:
|
|
|
|
# ini >= registered_date, end < registered_date
|
|
|
|
|
|
|
|
bp = None
|
|
|
|
lines = []
|
|
|
|
commit = options.get('commit', True)
|
|
|
|
ini = datetime.date.max
|
|
|
|
end = datetime.date.ini
|
|
|
|
# boundary lookup
|
|
|
|
for order in orders:
|
|
|
|
cini = order.registered_on
|
|
|
|
if order.billed_until:
|
|
|
|
cini = order.billed_until
|
|
|
|
bp = self.get_billing_point(order, bp=bp, **options)
|
|
|
|
order.new_billed_until = bp
|
|
|
|
ini = min(ini, cini)
|
|
|
|
end = max(end, bp) # TODO if all bp are the same ...
|
|
|
|
|
2014-09-14 19:36:27 +00:00
|
|
|
related_orders = Order.objects.filter(service=self.service, account=account)
|
|
|
|
if self.on_cancel in (self.COMPENSATE, self.REFOUND):
|
|
|
|
# Get orders pending for compensation
|
|
|
|
givers = related_orders.filter_givers(ini, end)
|
|
|
|
givers.sort(cmp=helpers.cmp_billed_until_or_registered_on)
|
|
|
|
orders.sort(cmp=helpers.cmp_billed_until_or_registered_on)
|
|
|
|
self.compensate(givers, orders)
|
|
|
|
|
|
|
|
# Get pricing orders
|
|
|
|
porders = related_orders.filter_pricing_orders(ini, end)
|
|
|
|
porders = set(orders).union(set(porders))
|
|
|
|
for ini, end, orders in self.get_chunks(porders, ini, end):
|
|
|
|
if self.pricing_period == self.ANUAL:
|
|
|
|
pass
|
|
|
|
elif self.pricing_period == self.MONTHLY:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
metric = len(orders)
|
|
|
|
for position, order in enumerate(orders):
|
|
|
|
# TODO position +1?
|
|
|
|
price = self.get_price(order, metric, position=position)
|
|
|
|
price *= size
|
|
|
|
|
|
|
|
def compensate(self, givers, receivers):
|
2014-09-14 09:52:45 +00:00
|
|
|
compensations = []
|
2014-09-14 19:36:27 +00:00
|
|
|
for order in givers:
|
2014-09-14 09:52:45 +00:00
|
|
|
if order.billed_until and order.cancelled_on and order.cancelled_on < order.billed_until:
|
|
|
|
compensations.append[Interval(order.cancelled_on, order.billed_until, order)]
|
2014-09-14 19:36:27 +00:00
|
|
|
for order in receivers:
|
|
|
|
if not order.billed_until or order.billed_until < order.new_billed_until:
|
|
|
|
# receiver
|
|
|
|
ini = order.billed_until or order.registered_on
|
|
|
|
end = order.cancelled_on or datetime.date.max
|
|
|
|
order_interval = helpers.Interval(ini, order.new_billed_until) # TODO beyond interval?
|
|
|
|
compensations, used_compensations = helpers.compensate(order_interval, compensations)
|
|
|
|
order._compensations = used_compensations
|
|
|
|
for comp in used_compensations:
|
|
|
|
comp.order.billed_until = min(comp.order.billed_until, comp.end)
|
2014-09-14 09:52:45 +00:00
|
|
|
|
|
|
|
def get_chunks(self, porders, ini, end, ix=0):
|
|
|
|
if ix >= len(porders):
|
|
|
|
return [[ini, end, []]]
|
|
|
|
order = porders[ix]
|
|
|
|
ix += 1
|
|
|
|
bu = getattr(order, 'new_billed_until', order.billed_until)
|
|
|
|
if not bu or bu <= ini or order.registered_on >= end:
|
|
|
|
return self.get_chunks(porders, ini, end, ix=ix)
|
|
|
|
result = []
|
|
|
|
if order.registered_on < end and order.registered_on > ini:
|
|
|
|
ro = order.registered_on
|
|
|
|
result = self.get_chunks(porders, ini, ro, ix=ix)
|
|
|
|
ini = ro
|
|
|
|
if bu < end:
|
|
|
|
result += self.get_chunks(porders, bu, end, ix=ix)
|
|
|
|
end = bu
|
|
|
|
chunks = self.get_chunks(porders, ini, end, ix=ix)
|
|
|
|
for chunk in chunks:
|
|
|
|
chunk[2].insert(0, order)
|
|
|
|
result.append(chunk)
|
|
|
|
return result
|
|
|
|
|
2014-09-10 16:53:09 +00:00
|
|
|
def generate_bill_lines(self, orders, **options):
|
2014-09-03 13:56:02 +00:00
|
|
|
# For the "boundary conditions" just think that:
|
|
|
|
# date(2011, 1, 1) is equivalent to datetime(2011, 1, 1, 0, 0, 0)
|
|
|
|
# In most cases:
|
|
|
|
# ini >= registered_date, end < registered_date
|
|
|
|
|
|
|
|
# TODO Perform compensations on cancelled services
|
|
|
|
if self.on_cancel in (self.COMPENSATE, self.REFOUND):
|
|
|
|
pass
|
2014-09-02 15:48:07 +00:00
|
|
|
# TODO compensations with commit=False, fuck commit or just fuck the transaction?
|
2014-09-03 13:56:02 +00:00
|
|
|
# compensate(orders, **options)
|
2014-09-02 15:48:07 +00:00
|
|
|
# TODO create discount per compensation
|
|
|
|
bp = None
|
|
|
|
lines = []
|
2014-09-10 16:53:09 +00:00
|
|
|
commit = options.get('commit', True)
|
2014-09-02 15:48:07 +00:00
|
|
|
for order in orders:
|
|
|
|
bp = self.get_billing_point(order, bp=bp, **options)
|
|
|
|
ini = order.billed_until or order.registered_on
|
2014-09-03 13:56:02 +00:00
|
|
|
if bp <= ini:
|
2014-09-02 15:48:07 +00:00
|
|
|
continue
|
|
|
|
if not self.metric:
|
|
|
|
# Number of orders metric; bill line per order
|
|
|
|
size = self.get_pricing_size(ini, bp)
|
2014-09-03 13:56:02 +00:00
|
|
|
price = self.get_price_with_orders(order, size, ini, bp)
|
2014-09-10 16:53:09 +00:00
|
|
|
lines.append(self.generate_line(order, price, size, ini, bp))
|
2014-09-02 15:48:07 +00:00
|
|
|
else:
|
|
|
|
# weighted metric; bill line per pricing period
|
|
|
|
for ini, end in self.get_pricing_slots(ini, bp):
|
|
|
|
size = self.get_pricing_size(ini, end)
|
2014-09-03 13:56:02 +00:00
|
|
|
price = self.get_price_with_metric(order, size, ini, end)
|
2014-09-10 16:53:09 +00:00
|
|
|
lines.append(self.generate_line(order, price, size, ini, end))
|
2014-09-03 13:56:02 +00:00
|
|
|
order.billed_until = bp
|
2014-09-10 16:53:09 +00:00
|
|
|
if commit:
|
|
|
|
order.save()
|
2014-09-02 15:48:07 +00:00
|
|
|
return lines
|