diff --git a/app/dao/notification_usage_dao.py b/app/dao/notification_usage_dao.py index d51e96153..1ee3134bd 100644 --- a/app/dao/notification_usage_dao.py +++ b/app/dao/notification_usage_dao.py @@ -1,5 +1,6 @@ from datetime import datetime, timedelta +from flask import current_app from sqlalchemy import Float, Integer from sqlalchemy import func, case, cast from sqlalchemy import literal_column @@ -11,7 +12,7 @@ from app.models import (NotificationHistory, NOTIFICATION_STATUS_TYPES_BILLABLE, KEY_TYPE_TEST, SMS_TYPE, - EMAIL_TYPE) + EMAIL_TYPE, Service) from app.statsd_decorators import statsd from app.utils import get_london_month_from_utc_column @@ -158,6 +159,8 @@ def rate_multiplier(): @statsd(namespace="dao") def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date, end_date, service_id): + free_sms_limit = Service.free_sms_fragment_limit() + billable_units = 0 total_cost = 0.0 @@ -176,15 +179,14 @@ def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date ) billable_units_by_rate_boundry = result.scalar() if billable_units_by_rate_boundry: - if billable_units >= 250000: + if billable_units >= free_sms_limit: total_cost += int(billable_units_by_rate_boundry) * rate_boundary['rate'] - elif billable_units + billable_units_by_rate_boundry > 250000: - remaining_free_allowance = abs(250000 - billable_units) + elif billable_units + billable_units_by_rate_boundry > free_sms_limit: + remaining_free_allowance = abs(free_sms_limit - billable_units) total_cost += ((billable_units_by_rate_boundry - remaining_free_allowance) * rate_boundary) else: total_cost += 0 billable_units += int(billable_units_by_rate_boundry) - return billable_units, total_cost diff --git a/tests/app/dao/test_notification_usage_dao.py b/tests/app/dao/test_notification_usage_dao.py index d4aec8531..c20b76a4c 100644 --- a/tests/app/dao/test_notification_usage_dao.py +++ b/tests/app/dao/test_notification_usage_dao.py @@ -2,6 +2,7 @@ import uuid from datetime import datetime, timedelta import pytest +from flask import current_app from app.dao.date_util import get_financial_year from app.dao.notification_usage_dao import ( @@ -15,8 +16,7 @@ from app.models import ( Rate, NOTIFICATION_DELIVERED, NOTIFICATION_STATUS_TYPES_BILLABLE, - NOTIFICATION_STATUS_TYPES_NON_BILLABLE, - Notification) + NOTIFICATION_STATUS_TYPES_NON_BILLABLE) from tests.app.conftest import sample_notification, sample_email_template, sample_letter_template, sample_service from tests.app.db import create_notification from freezegun import freeze_time @@ -266,6 +266,8 @@ def set_up_rate(notify_db, start_date, value): @freeze_time("2016-01-10 12:00:00.000000") def test_returns_total_billable_units_for_sms_notifications(notify_db, notify_db_session, sample_service): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 0.016) sample_notification( @@ -288,6 +290,8 @@ def test_returns_total_billable_units_for_sms_notifications(notify_db, notify_db def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) sample_notification( @@ -309,6 +313,8 @@ def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notificati def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications_for_several_rates( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2) set_up_rate(notify_db, datetime(2016, 10, 1), 4) set_up_rate(notify_db, datetime(2017, 1, 1), 6) @@ -350,6 +356,8 @@ def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notificati def test_returns_total_billable_units_for_sms_notifications_for_several_rates_where_dates_match_rate_boundary( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2) set_up_rate(notify_db, datetime(2016, 10, 1), 4) set_up_rate(notify_db, datetime(2017, 1, 1), 6) @@ -388,6 +396,8 @@ def test_returns_total_billable_units_for_sms_notifications_for_several_rates_wh def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and_emails( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) email_template = sample_email_template(notify_db, notify_db_session, service=sample_service) @@ -426,6 +436,8 @@ def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and def test_returns_total_billable_units_for_sms_notifications_for_only_requested_service( notify_db, notify_db_session ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) service_1 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4())) @@ -463,6 +475,8 @@ def test_returns_total_billable_units_for_sms_notifications_for_only_requested_s def test_returns_total_billable_units_for_sms_notifications_handling_null_values( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) sample_notification( @@ -488,6 +502,9 @@ def test_returns_total_billable_units_for_sms_notifications_handling_null_values def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notifications( notify_db, notify_db_session, sample_service, billable_units, states ): + + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) for state in states: @@ -514,6 +531,8 @@ def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notif def test_restricts_to_time_period_when_returning_billable_units_for_sms_notifications( notify_db, notify_db_session, sample_service ): + current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 + set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) sample_notification(