Added a new endpoint for yearly usage totals using ft_billing.

This commit is contained in:
Rebecca Law
2018-05-11 16:25:16 +01:00
parent 99d1357c37
commit d98581cfe6
6 changed files with 193 additions and 43 deletions

View File

@@ -1,5 +1,6 @@
from datetime import datetime
create_or_update_free_sms_fragment_limit_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST annual billing schema",
@@ -24,3 +25,17 @@ def serialize_ft_billing_remove_emails(data):
}
results.append(json_result)
return results
def serialize_ft_billing_yearly_totals(data):
yearly_totals = []
for total in data:
json_result = {
"notification_type": total.notification_type,
"billing_units": total.billable_units,
"rate": float(total.rate),
"letter_total": float(total.billable_units * total.rate) if total.notification_type == 'letter' else 0
}
yearly_totals.append(json_result)
return yearly_totals

View File

@@ -5,7 +5,8 @@ from flask import Blueprint, jsonify, request
from app.billing.billing_schemas import (
create_or_update_free_sms_fragment_limit_schema,
serialize_ft_billing_remove_emails
serialize_ft_billing_remove_emails,
serialize_ft_billing_yearly_totals,
)
from app.dao.annual_billing_dao import (
dao_get_free_sms_fragment_limit_for_year,
@@ -15,7 +16,7 @@ from app.dao.annual_billing_dao import (
)
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_months_for_financial_year
from app.dao.fact_billing_dao import fetch_monthly_billing_for_year
from app.dao.fact_billing_dao import fetch_monthly_billing_for_year, fetch_billing_totals_for_year
from app.dao.monthly_billing_dao import (
get_billing_data_for_financial_year,
get_monthly_billing_by_notification_type
@@ -47,6 +48,18 @@ def get_yearly_usage_by_monthly_from_ft_billing(service_id):
return jsonify(data)
@billing_blueprint.route('/ft-yearly-usage-summary')
def get_yearly_billing_usage_summary_from_ft_billing(service_id):
try:
year = int(request.args.get('year'))
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400
billing_data = fetch_billing_totals_for_year(service_id, year)
data = serialize_ft_billing_yearly_totals(billing_data)
return jsonify(data)
@billing_blueprint.route('/monthly-usage')
def get_yearly_usage_by_month(service_id):
try:
@@ -70,7 +83,7 @@ def get_yearly_billing_usage_summary(service_id):
try:
year = int(request.args.get('year'))
billing_data = get_billing_data_for_financial_year(service_id, year)
notification_types = [SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]
notification_types = [EMAIL_TYPE, LETTER_TYPE, SMS_TYPE]
response = [
_get_total_billable_units_and_rate_for_notification_type(billing_data, notification_type)
for notification_type in notification_types
@@ -102,8 +115,8 @@ def _get_total_billable_units_and_rate_for_notification_type(billing_data, noti_
return {
"notification_type": noti_type,
"billing_units": total_sent,
"rate": rate,
"letter_total": letter_total
"rate": float(rate),
"letter_total": round(float(letter_total), 3)
}
@@ -132,7 +145,7 @@ def _transform_billing_for_month_letters(billing_for_month):
"month": month_name,
"billing_units": (total['billing_units'] * total['rate_multiplier']),
"notification_type": billing_for_month.notification_type,
"rate": total['rate']
"rate": float(total['rate'])
}
x.append(y)
if len(billing_for_month.monthly_totals) == 0:

View File

@@ -27,4 +27,4 @@ def create_nightly_billing(day_start=None):
update_fact_billing(data, process_day)
current_app.logger.info(
"create-nightly-billing task complete. {} rows updated for day: {}".format(len(transit_data, process_day)))
"create-nightly-billing task complete. {} rows updated for day: {}".format(len(transit_data), process_day))

View File

@@ -19,6 +19,31 @@ from app.models import (
from app.utils import convert_utc_to_bst, convert_bst_to_utc
def fetch_billing_totals_for_year(service_id, year):
year_start_date, year_end_date = get_financial_year(year)
yearly_data = db.session.query(
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"),
FactBilling.service_id,
FactBilling.rate,
FactBilling.notification_type
).filter(
FactBilling.service_id == service_id,
FactBilling.bst_date >= year_start_date,
FactBilling.bst_date <= year_end_date
).group_by(
FactBilling.service_id,
FactBilling.rate,
FactBilling.notification_type
).order_by(
FactBilling.service_id,
FactBilling.notification_type
).all()
return yearly_data
def fetch_monthly_billing_for_year(service_id, year):
year_start_date, year_end_date = get_financial_year(year)
utcnow = datetime.utcnow()