From 24d0c103a5c7834d92a98a9b16640df6cb8295f3 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Tue, 26 May 2015 19:12:24 +0000 Subject: [PATCH] Fixes on billing --- TODO.md | 2 +- .../contrib/orchestration/middlewares.py | 5 ++- orchestra/contrib/orders/admin.py | 2 +- orchestra/contrib/services/handlers.py | 31 +++++++++++-------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/TODO.md b/TODO.md index f89c550e..45421380 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/orchestra/contrib/orchestration/middlewares.py b/orchestra/contrib/orchestration/middlewares.py index 94fa73d3..97623939 100644 --- a/orchestra/contrib/orchestration/middlewares.py +++ b/orchestra/contrib/orchestration/middlewares.py @@ -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 """ diff --git a/orchestra/contrib/orders/admin.py b/orchestra/contrib/orders/admin.py index 593c07e0..18afe8c3 100644 --- a/orchestra/contrib/orders/admin.py +++ b/orchestra/contrib/orders/admin.py @@ -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'), ) diff --git a/orchestra/contrib/services/handlers.py b/orchestra/contrib/services/handlers.py index 72868c3e..98979414 100644 --- a/orchestra/contrib/services/handlers.py +++ b/orchestra/contrib/services/handlers.py @@ -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 - 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)) + 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) + 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: