From e0e64d51d5001beec8098f7c29629922e267ae0d Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 15 Dec 2017 17:29:32 +0000 Subject: [PATCH] [WIP] Fix the query to count rather than sum the billing units. Need to fix the query that returns the monhtly billing, there is only one row but there should be two if there are two rates. --- app/billing/rest.py | 7 +++++-- app/commands.py | 18 ++++++++++++------ app/dao/monthly_billing_dao.py | 6 +++--- app/dao/notification_usage_dao.py | 12 +++++------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/billing/rest.py b/app/billing/rest.py index 66726bb5a..8aa6a7f31 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -9,7 +9,7 @@ from app.dao.monthly_billing_dao import ( ) from app.dao.date_util import get_financial_year, get_months_for_financial_year from app.errors import register_errors -from app.models import SMS_TYPE, EMAIL_TYPE +from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE from app.utils import convert_utc_to_bst from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year, dao_get_all_free_sms_fragment_limit, @@ -40,6 +40,9 @@ def get_yearly_usage_by_month(service_id): billing_for_month = get_monthly_billing_by_notification_type(service_id, month, SMS_TYPE) if billing_for_month: results.append(_transform_billing_for_month(billing_for_month)) + letter_billing_for_month = get_monthly_billing_by_notification_type(service_id, month, LETTER_TYPE) + if letter_billing_for_month: + results.append(_transform_billing_for_month(letter_billing_for_month)) return json.dumps(results) except TypeError: @@ -51,7 +54,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] + notification_types = [SMS_TYPE, EMAIL_TYPE, LETTER_TYPE] response = [ _get_total_billable_units_and_rate_for_notification_type(billing_data, notification_type) for notification_type in notification_types diff --git a/app/commands.py b/app/commands.py index 03061f877..f1a3ae690 100644 --- a/app/commands.py +++ b/app/commands.py @@ -205,10 +205,15 @@ def populate_monthly_billing(year): email_res = get_monthly_billing_by_notification_type( service_id, datetime(year, month, 1), EMAIL_TYPE ) + letter_res = get_monthly_billing_by_notification_type( + service_id, datetime(year, month, 1), 'letter' + ) print("Finished populating data for {} for service id {}".format(month, str(service_id))) print('SMS: {}'.format(sms_res.monthly_totals)) print('Email: {}'.format(email_res.monthly_totals)) + print('Letter: {}'.format(letter_res.monthly_totals)) + service_ids = get_service_ids_that_need_billing_populated( start_date=datetime(2016, 5, 1), end_date=datetime(2017, 8, 16) @@ -218,12 +223,13 @@ def populate_monthly_billing(year): if year == 2016: start = 4 - for service_id in service_ids: - print('Starting to populate data for service {}'.format(str(service_id))) - print('Starting populating monthly billing for {}'.format(year)) - for i in range(start, end): - print('Population for {}-{}'.format(i, year)) - populate(service_id, year, i) + # for service_id in service_ids: + # print('Starting to populate data for service {}'.format(str(service_id))) + # print('Starting populating monthly billing for {}'.format(year)) + # for i in range(start, end): + # print('Population for {}-{}'.format(i, year)) + # populate(service_id, year, i) + populate('56fa9fd8-61de-4688-8bc7-2908a77367df', year, 10) @notify_command() diff --git a/app/dao/monthly_billing_dao.py b/app/dao/monthly_billing_dao.py index ad0014c85..ed30acbbc 100644 --- a/app/dao/monthly_billing_dao.py +++ b/app/dao/monthly_billing_dao.py @@ -30,8 +30,8 @@ def get_service_ids_that_need_billing_populated(start_date, end_date): @statsd(namespace="dao") def create_or_update_monthly_billing(service_id, billing_month): start_date, end_date = get_month_start_and_end_date_in_utc(billing_month) - _update_monthly_billing(service_id, start_date, end_date, SMS_TYPE) - _update_monthly_billing(service_id, start_date, end_date, EMAIL_TYPE) + # _update_monthly_billing(service_id, start_date, end_date, SMS_TYPE) + # _update_monthly_billing(service_id, start_date, end_date, EMAIL_TYPE) _update_monthly_billing(service_id, start_date, end_date, LETTER_TYPE) @@ -114,7 +114,7 @@ def get_monthly_billing_by_notification_type(service_id, billing_month, notifica @statsd(namespace="dao") -def get_billing_data_for_financial_year(service_id, year, notification_types=[SMS_TYPE, EMAIL_TYPE]): +def get_billing_data_for_financial_year(service_id, year, notification_types=[SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]): # Update totals to the latest so we include data for today now = convert_utc_to_bst(datetime.utcnow()) create_or_update_monthly_billing(service_id=service_id, billing_month=now) diff --git a/app/dao/notification_usage_dao.py b/app/dao/notification_usage_dao.py index fe00b5145..5e7c535ef 100644 --- a/app/dao/notification_usage_dao.py +++ b/app/dao/notification_usage_dao.py @@ -134,9 +134,8 @@ def billing_data_per_month_query(rate, service_id, start_date, end_date, notific ).order_by( month, rate_multiplier() - ).all() - - return results + ) + return results.all() def rate_multiplier(): @@ -152,7 +151,7 @@ def billing_letter_data_per_month_query(service_id, start_date, end_date): crown = Service.query.get(service_id).crown results = db.session.query( month.label('month'), - func.sum(NotificationHistory.billable_units).label('billing_units'), + func.count(NotificationHistory.billable_units).label('billing_units'), rate_multiplier().label('rate_multiplier'), NotificationHistory.international, NotificationHistory.notification_type, @@ -172,6 +171,5 @@ def billing_letter_data_per_month_query(service_id, start_date, end_date): ).order_by( month, rate_multiplier() - ).all() - - return results + ) + return results.all()