Change how we populate and retrieve MonthlyBilling totals:

1. For both email and sms, store [] in monthly_totals if
there is no billing data (no notifications sent etc.) and
return this via the API

2. General refactoring of indentation
This commit is contained in:
Imdad Ahad
2017-08-11 16:53:12 +01:00
parent 46ca086aa2
commit 94605d31fa
6 changed files with 78 additions and 92 deletions

View File

@@ -1,5 +1,8 @@
from datetime import datetime
from sqlalchemy import func
from app import db
from app.dao.dao_utils import transactional
from app.dao.date_util import get_month_start_and_end_date_in_utc, get_financial_year
@@ -31,16 +34,19 @@ def create_or_update_monthly_billing(service_id, billing_month):
_update_monthly_billing(service_id, start_date, end_date, EMAIL_TYPE)
def _monthly_billing_data_to_json(monthly):
# total cost must take into account the free allowance.
# might be a good idea to capture free allowance in this table
return [{
"billing_units": x.billing_units,
"rate_multiplier": x.rate_multiplier,
"international": x.international,
"rate": x.rate,
"total_cost": (x.billing_units * x.rate_multiplier) * x.rate
} for x in monthly]
def _monthly_billing_data_to_json(billing_data):
results = []
if billing_data:
# total cost must take into account the free allowance.
# might be a good idea to capture free allowance in this table
results = [{
"billing_units": x.billing_units,
"rate_multiplier": x.rate_multiplier,
"international": x.international,
"rate": x.rate,
"total_cost": (x.billing_units * x.rate_multiplier) * x.rate
} for x in billing_data]
return results
@transactional
@@ -82,7 +88,11 @@ def get_monthly_billing_entry(service_id, start_date, notification_type):
def get_yearly_billing_data_for_date_range(
service_id, start_date, end_date, notification_types
):
results = MonthlyBilling.query.filter(
results = db.session.query(
MonthlyBilling.notification_type,
MonthlyBilling.monthly_totals,
MonthlyBilling.start_date,
).filter(
MonthlyBilling.service_id == service_id,
MonthlyBilling.start_date >= start_date,
MonthlyBilling.end_date <= end_date,
@@ -112,5 +122,4 @@ def get_billing_data_for_financial_year(service_id, year, notification_types=[SM
results = get_yearly_billing_data_for_date_range(
service_id, start_date, end_date, notification_types
)
return results

View File

@@ -127,6 +127,7 @@ def email_yearly_billing_data_query(service_id, start_date, end_date, rate=0):
rate_multiplier(),
NotificationHistory.international
).first()
if not result:
return [(0, 0, 1, EMAIL_TYPE, False, 0)]
else:
@@ -187,9 +188,14 @@ def is_between(date, start_date, end_date):
def billing_data_per_month_query(rate, service_id, start_date, end_date, notification_type):
month = get_london_month_from_utc_column(NotificationHistory.created_at)
result = db.session.query(
if notification_type == SMS_TYPE:
filter_subq = func.sum(NotificationHistory.billable_units).label('billing_units')
elif notification_type == EMAIL_TYPE:
filter_subq = func.count(NotificationHistory.billable_units).label('billing_units')
results = db.session.query(
month.label('month'),
func.sum(NotificationHistory.billable_units).label('billing_units'),
filter_subq,
rate_multiplier().label('rate_multiplier'),
NotificationHistory.international,
NotificationHistory.notification_type,
@@ -206,17 +212,7 @@ def billing_data_per_month_query(rate, service_id, start_date, end_date, notific
rate_multiplier()
).all()
if not result and notification_type == EMAIL_TYPE:
start_date = convert_utc_to_bst(start_date)
BillingData = namedtuple(
'BillingData',
'start_date billing_units rate_multiplier international notification_type rate'
)
return [BillingData(start_date, 0, 1, False, notification_type, 0)]
else:
return result
return result
return results
def rate_multiplier():