Fixes on billing

This commit is contained in:
Marc Aymerich 2015-05-26 19:12:24 +00:00
parent d00befb601
commit 24d0c103a5
4 changed files with 24 additions and 16 deletions

View File

@ -412,4 +412,4 @@ touch /tmp/somefile
# batch zone edditing
# inherit registers from parent?
# Bill metric disk 5 GB: unialber
# Bill metric disk 5 GB: unialber: include not include 5, unialbert recheck period

View File

@ -80,7 +80,10 @@ class OperationsMiddleware(object):
type(self).thread_locals.transaction.__enter__()
def leave_transaction_management(self, exception=None):
type(self).thread_locals.transaction.__exit__(exception, None, None)
locals = type(self).thread_locals
if hasattr(locals, 'transaction'):
# Don't fucking know why sometimes thread_locals does not contain a transaction
locals.transaction.__exit__(exception, None, None)
def process_request(self, request):
""" Store request on a thread local variable """

View File

@ -51,7 +51,7 @@ class OrderAdmin(AccountAdminMixin, ExtendedModelAdmin):
'id', 'service_link', 'account_link', 'content_object_link',
'display_registered_on', 'display_billed_until', 'display_cancelled_on', 'display_metric'
)
list_filter = (ActiveOrderListFilter, IgnoreOrderListFilter, 'service', BilledOrderListFilter)
list_filter = (ActiveOrderListFilter, IgnoreOrderListFilter, BilledOrderListFilter, 'service')
default_changelist_filters = (
('ignore', '0'),
)

View File

@ -497,20 +497,25 @@ class ServiceHandler(plugins.Plugin, metaclass=plugins.PluginMount):
# Recharge
if self.payment_style == self.PREPAY and order.billed_on:
rini = order.billed_on
charged = None
cmetric = None
new_metric, new_price = 0, 0
for cini, cend, metric in order.get_metric(rini, bp, changes=True):
if charged is None:
charged = metric
for cini, cend, metric in order.get_metric(rini, min(bp, order.billed_until), changes=True):
if cmetric is None:
cmetric = metric
cprice = self.get_price(account, cmetric)
if metric > cmetric:
size = self.get_price_size(cini, cend)
new_price += self.get_price(account, metric) * size
new_metric += metric
size = self.get_price_size(rini, bp)
old_price = self.get_price(account, charged) * size
if new_price > old_price:
metric = new_metric - charged
price = new_price - old_price
lines.append(self.generate_line(order, price, rini, bp, metric=metric, computed=True))
rprice = self.get_price(account, metric)
if rprice > cprice:
price = (rprice-cprice) * size
prepay_discount = cprice*size
discounts = ()
if prepay_discount:
discounts = (
('prepay', -prepay_discount),
)
lines.append(self.generate_line(order, price, cini, cend,
metric=metric, computed=True, discounts=discounts))
if order.billed_until and order.cancelled_on and order.cancelled_on >= order.billed_until:
continue
if self.billing_period != self.NEVER: