diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 2f4117bbe..e29727536 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -409,11 +409,9 @@ def get_monthly_usage_breakdown(year, monthly_usage): sms_rate = monthly_sms[0]['rate'] if len(monthly_sms) else 0 monthly_letters = [row for row in letters if row['month'] == month] + letter_cost = sum(row['letter_cost'] for row in monthly_letters) letter_breakdown = format_letter_details_for_month(monthly_letters) - letter_cost = 0 - for x in letter_breakdown: - letter_cost += x.cost yield { 'month': month, 'letter_cost': letter_cost, @@ -446,7 +444,7 @@ def format_letter_details_for_month(letter_units_for_month): letter_details = LetterDetails( sent=sum(x['billing_units'] for x in rate_group), rate=rate_group[0]['rate'], - cost=(sum(x['billing_units'] for x in rate_group) * rate_group[0]['rate']), + cost=sum(x['letter_cost'] for x in rate_group), postage_description=rate_group[0]['postage'] ) diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index dde099c8f..1a47d4ced 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -1102,13 +1102,56 @@ def test_usage_page_displays_letters_ordered_by_postage( mock_get_free_sms_fragment_limit ): monthly_usage = [ - {'month': 'April', 'notification_type': 'letter', 'rate': 0.5, 'billing_units': 1, 'postage': 'second'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 1, 'billing_units': 1, 'postage': 'europe'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 1, 'billing_units': 2, 'postage': 'rest-of-word'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 1.5, 'billing_units': 7, 'postage': 'europe'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 0.3, 'billing_units': 3, 'postage': 'second'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 0.5, 'billing_units': 1, 'postage': 'first'}, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 0.5, + 'billing_units': 1, + 'postage': 'second', + 'letter_cost': 0.5, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 1, + 'billing_units': 1, + 'postage': 'europe', + 'letter_cost': 1, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 1, + 'billing_units': 2, + 'postage': 'rest-of-word', + 'letter_cost': 2, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 1.5, + 'billing_units': 7, + 'postage': 'europe', + 'letter_cost': 11.5, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 0.3, + 'billing_units': 3, + 'postage': 'second', + 'letter_cost': 0.9, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 0.5, + 'billing_units': 1, + 'postage': 'first', + 'letter_cost': 0.5, + }, ] + mocker.patch('app.billing_api_client.get_monthly_usage_for_service', return_value=monthly_usage) service_one['permissions'].append('letter') page = client_request.get( @@ -1136,12 +1179,48 @@ def test_usage_page_displays_letters_split_by_month_and_postage( mock_get_free_sms_fragment_limit ): billable_units_resp = [ - {'month': 'April', 'notification_type': 'letter', 'rate': 0.5, 'billing_units': 1, 'postage': 'second'}, - {'month': 'April', 'notification_type': 'letter', 'rate': 1, 'billing_units': 1, 'postage': 'europe'}, - {'month': 'May', 'notification_type': 'letter', 'rate': 1, 'billing_units': 7, 'postage': 'europe'}, - {'month': 'May', 'notification_type': 'letter', 'rate': 0.5, 'billing_units': 3, 'postage': 'second'}, - {'month': 'May', 'notification_type': 'letter', 'rate': 0.7, 'billing_units': 1, 'postage': 'first'}, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 0.5, + 'billing_units': 1, + 'postage': 'second', + 'letter_cost': 0.5, + }, + { + 'month': 'April', + 'notification_type': 'letter', + 'rate': 1, + 'billing_units': 1, + 'postage': 'europe', + 'letter_cost': 1, + }, + { + 'month': 'May', + 'notification_type': 'letter', + 'rate': 1, + 'billing_units': 7, + 'postage': 'europe', + 'letter_cost': 7, + }, + { + 'month': 'May', + 'notification_type': 'letter', + 'rate': 0.5, + 'billing_units': 3, + 'postage': 'second', + 'letter_cost': 1.5, + }, + { + 'month': 'May', + 'notification_type': 'letter', + 'rate': 0.7, + 'billing_units': 1, + 'postage': 'first', + 'letter_cost': 0.7, + }, ] + mocker.patch('app.billing_api_client.get_monthly_usage_for_service', return_value=billable_units_resp) service_one['permissions'].append('letter') page = client_request.get( diff --git a/tests/conftest.py b/tests/conftest.py index 1d6940f6d..388805cc5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2367,6 +2367,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 1230, 'sms_free_allowance_used': 0, 'sms_cost': 20.91, + 'letter_cost': 0, }, { 'month': 'February', @@ -2377,6 +2378,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 960, 'sms_free_allowance_used': 140, 'sms_cost': 15.84, + 'letter_cost': 0, }, { 'month': 'February', @@ -2387,6 +2389,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 0, 'sms_free_allowance_used': 0, 'sms_cost': 0, + 'letter_cost': 3.1, }, { 'month': 'February', @@ -2397,6 +2400,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 0, 'sms_free_allowance_used': 0, 'sms_cost': 0, + 'letter_cost': 1.65, }, { 'month': 'February', @@ -2407,6 +2411,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 0, 'sms_free_allowance_used': 0, 'sms_cost': 0, + 'letter_cost': 2.52, }, { 'month': 'February', @@ -2417,6 +2422,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 0, 'sms_free_allowance_used': 0, 'sms_cost': 0, + 'letter_cost': 5.88, }, { 'month': 'April', @@ -2427,6 +2433,7 @@ def mock_get_monthly_usage_for_service(mocker): 'sms_charged': 0, 'sms_free_allowance_used': 249860, 'sms_cost': 0, + 'letter_cost': 0, }, ]