From 2706ec4c73ec22701b4f9f1dd4aa44cc020ba64a Mon Sep 17 00:00:00 2001 From: David McDonald Date: Mon, 25 Apr 2022 11:48:50 +0100 Subject: [PATCH] Take sms_rate from monthly usage data At the moment, we put the sms rate on the usage page for each months billing data by taking the single sms rate for the year. The assumption that there will be a single sms rate for the year is no longer going to be true. Therefore, instead we take the sms rate from the monthly data itself which tells us the rate for a months worth of sent SMS. --- app/main/views/dashboard.py | 9 ++ app/templates/views/usage.html | 4 +- tests/app/main/views/test_dashboard.py | 112 +++++++++++++++++++++---- 3 files changed, 109 insertions(+), 16 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 6c8fe6af5..1d143ec31 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -429,6 +429,7 @@ def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, bi 'letters': letter_billing, 'sms_paid_count': breakdown['paid'], 'sms_free_count': breakdown['free'], + 'sms_rate': breakdown['sms_rate'], } @@ -476,23 +477,31 @@ def get_free_paid_breakdown_for_month( ): allowance = free_sms_fragment_limit + # makes the assumption that there is either no item in `monthly_usage` because they have not sent any SMS + # or that they have sent SMS and that there is only a single item in `monthly_usage` because they have only + # been sent at a single rate during the month + sms_rate = monthly_usage[0]['rate'] if len(monthly_usage) else 0 + total_monthly_billing_units = get_sum_billing_units(monthly_usage) if cumulative < allowance: return { 'paid': 0, 'free': total_monthly_billing_units, + 'sms_rate': sms_rate, } elif previous_cumulative < allowance: remaining_allowance = allowance - previous_cumulative return { 'paid': total_monthly_billing_units - remaining_allowance, 'free': remaining_allowance, + 'sms_rate': sms_rate, } else: return { 'paid': total_monthly_billing_units, 'free': 0, + 'sms_rate': sms_rate, } diff --git a/app/templates/views/usage.html b/app/templates/views/usage.html index 26b63e559..4479c97fa 100644 --- a/app/templates/views/usage.html +++ b/app/templates/views/usage.html @@ -97,7 +97,7 @@ {% endcall %} {% call field(align='left') %} {{ big_number( - (sms_rate * month.sms_paid_count) + month.letter_total, + (month.sms_rate * month.sms_paid_count) + month.letter_total, currency="£", smallest=True ) }} @@ -107,7 +107,7 @@ {% endif %} {% if month.sms_paid_count %}
  • {{ month.sms_paid_count|message_count('sms') }} at - {{- ' {:.2f}p'.format(sms_rate * 100) }}
  • + {{- ' {:.2f}p'.format(month.sms_rate * 100) }} {% endif %} {% for letter in month.letters%} {% if letter.billing_units %} diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index dfafc6b29..e69751cfc 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -1586,27 +1586,111 @@ def test_get_free_paid_breakdown_for_billable_units(now, expected_number_of_mont }, { 'month': 'June', 'international': False, 'rate_multiplier': 1, - 'notification_type': 'sms', 'rate': 1.65, 'billing_units': 100000 + 'notification_type': 'sms', 'rate': 1.71, 'billing_units': 100000 }, { 'month': 'February', 'international': False, 'rate_multiplier': 1, - 'notification_type': 'sms', 'rate': 1.65, 'billing_units': 2000 + 'notification_type': 'sms', 'rate': 1.71, 'billing_units': 2000 }, ] ) assert list(billing_units) == [ - {'sms_free_count': 100000, 'name': 'April', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 100000, 'name': 'May', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 50000, 'name': 'June', 'sms_paid_count': 50000, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'July', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'August', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'September', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'October', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'November', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'December', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'January', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'February', 'sms_paid_count': 2000, 'letter_total': 0, 'letters': []}, - {'sms_free_count': 0, 'name': 'March', 'sms_paid_count': 0, 'letter_total': 0, 'letters': []} + { + 'sms_free_count': 100000, + 'name': 'April', + 'sms_paid_count': 0, + 'sms_rate': 1.65, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 100000, + 'name': 'May', + 'sms_paid_count': 0, + 'sms_rate': 1.65, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 50000, + 'name': 'June', + 'sms_paid_count': 50000, + 'sms_rate': 1.71, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'July', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'August', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'September', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'October', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'November', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'December', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'January', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'February', + 'sms_paid_count': 2000, + 'sms_rate': 1.71, + 'letter_total': 0, + 'letters': [] + }, + { + 'sms_free_count': 0, + 'name': 'March', + 'sms_paid_count': 0, + 'sms_rate': 0, + 'letter_total': 0, + 'letters': [] + }, ][:expected_number_of_months]