Merge pull request #4221 from alphagov/usage-tidyup

Show monthly usage rate correctly if the sms rate has changed during the financial year
This commit is contained in:
Ben Thorner
2022-04-27 15:37:45 +01:00
committed by GitHub
4 changed files with 129 additions and 39 deletions

View File

@@ -406,7 +406,6 @@ def get_sum_billing_units(billing_units, month=None):
def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, billing_units): def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, billing_units):
cumulative = 0 cumulative = 0
letter_cumulative = 0
sms_units = [x for x in billing_units if x['notification_type'] == 'sms'] sms_units = [x for x in billing_units if x['notification_type'] == 'sms']
letter_units = [x for x in billing_units if x['notification_type'] == 'letter'] letter_units = [x for x in billing_units if x['notification_type'] == 'letter']
for month in get_months_for_financial_year(year): for month in get_months_for_financial_year(year):
@@ -424,14 +423,13 @@ def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, bi
letter_total = 0 letter_total = 0
for x in letter_billing: for x in letter_billing:
letter_total += x.cost letter_total += x.cost
letter_cumulative += letter_total
yield { yield {
'name': month, 'name': month,
'letter_total': letter_total, 'letter_total': letter_total,
'letter_cumulative': letter_cumulative, 'letters': letter_billing,
'paid': breakdown['paid'], 'sms_paid_count': breakdown['paid'],
'free': breakdown['free'], 'sms_free_count': breakdown['free'],
'letters': letter_billing 'sms_rate': breakdown['sms_rate'],
} }
@@ -479,23 +477,31 @@ def get_free_paid_breakdown_for_month(
): ):
allowance = free_sms_fragment_limit 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) total_monthly_billing_units = get_sum_billing_units(monthly_usage)
if cumulative < allowance: if cumulative < allowance:
return { return {
'paid': 0, 'paid': 0,
'free': total_monthly_billing_units, 'free': total_monthly_billing_units,
'sms_rate': sms_rate,
} }
elif previous_cumulative < allowance: elif previous_cumulative < allowance:
remaining_allowance = allowance - previous_cumulative remaining_allowance = allowance - previous_cumulative
return { return {
'paid': total_monthly_billing_units - remaining_allowance, 'paid': total_monthly_billing_units - remaining_allowance,
'free': remaining_allowance, 'free': remaining_allowance,
'sms_rate': sms_rate,
} }
else: else:
return { return {
'paid': total_monthly_billing_units, 'paid': total_monthly_billing_units,
'free': 0, 'free': 0,
'sms_rate': sms_rate,
} }

View File

@@ -97,17 +97,17 @@
{% endcall %} {% endcall %}
{% call field(align='left') %} {% call field(align='left') %}
{{ big_number( {{ big_number(
(sms_rate * month.paid) + month.letter_total, (month.sms_rate * month.sms_paid_count) + month.letter_total,
currency="£", currency="£",
smallest=True smallest=True
) }} ) }}
<ul> <ul>
{% if month.free %} {% if month.sms_free_count %}
<li class="tabular-numbers">{{ month.free|format_thousands }} free {{ month.free|message_count_label('sms', suffix='') }}</li> <li class="tabular-numbers">{{ month.sms_free_count|format_thousands }} free {{ month.sms_free_count|message_count_label('sms', suffix='') }}</li>
{% endif %} {% endif %}
{% if month.paid %} {% if month.sms_paid_count %}
<li class="tabular-numbers">{{ month.paid|message_count('sms') }} at <li class="tabular-numbers">{{ month.sms_paid_count|message_count('sms') }} at
{{- ' {:.2f}p'.format(sms_rate * 100) }}</li> {{- ' {:.2f}p'.format(month.sms_rate * 100) }}</li>
{% endif %} {% endif %}
{% for letter in month.letters%} {% for letter in month.letters%}
{% if letter.billing_units %} {% if letter.billing_units %}
@@ -115,7 +115,7 @@
{{ letter.rate | format_number_in_pounds_as_currency }}</li> {{ letter.rate | format_number_in_pounds_as_currency }}</li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if not (month.free or month.paid or month.letters) %} {% if not (month.sms_free_count or month.sms_paid_count or month.letters) %}
<li aria-hidden="true"></li> <li aria-hidden="true"></li>
{% endif %} {% endif %}
</ul> </ul>

View File

@@ -1022,8 +1022,8 @@ def test_usage_page(
assert 'March' in table assert 'March' in table
assert '£28.99' in table assert '£28.99' in table
assert '140 free text messages' in table assert '140 free text messages' in table
assert '£20.30' in table assert '£20.91' in table
assert '1,230 text messages at 1.65p' in table assert '1,230 text messages at 1.70p' in table
@freeze_time("2012-03-31 12:12:12") @freeze_time("2012-03-31 12:12:12")
@@ -1064,8 +1064,8 @@ def test_usage_page_with_letters(
assert 'March' in table assert 'March' in table
assert '£28.99' in table assert '£28.99' in table
assert '140 free text messages' in table assert '140 free text messages' in table
assert '£20.30' in table assert '£20.91' in table
assert '1,230 text messages at 1.65p' in table assert '1,230 text messages at 1.70p' in table
assert '10 second class letters at 31p' in normalize_spaces(table) assert '10 second class letters at 31p' in normalize_spaces(table)
assert '5 first class letters at 33p' in normalize_spaces(table) assert '5 first class letters at 33p' in normalize_spaces(table)
assert '10 international letters at 84p' in normalize_spaces(table) assert '10 international letters at 84p' in normalize_spaces(table)
@@ -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, '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, '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) == [ assert list(billing_units) == [
{'free': 100000, 'name': 'April', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, {
{'free': 100000, 'name': 'May', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'sms_free_count': 100000,
{'free': 50000, 'name': 'June', 'paid': 50000, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'name': 'April',
{'free': 0, 'name': 'July', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'sms_paid_count': 0,
{'free': 0, 'name': 'August', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'sms_rate': 1.65,
{'free': 0, 'name': 'September', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'letter_total': 0,
{'free': 0, 'name': 'October', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'letters': []
{'free': 0, 'name': 'November', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, },
{'free': 0, 'name': 'December', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, {
{'free': 0, 'name': 'January', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'sms_free_count': 100000,
{'free': 0, 'name': 'February', 'paid': 2000, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0}, 'name': 'May',
{'free': 0, 'name': 'March', 'paid': 0, 'letter_total': 0, 'letters': [], 'letter_cumulative': 0} '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] ][:expected_number_of_months]

View File

@@ -2334,56 +2334,56 @@ def mock_get_billable_units(mocker):
{ {
'month': 'April', 'month': 'April',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 249500, 'billing_units': 249500,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'April', 'month': 'April',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 100, 'billing_units': 100,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'April', 'month': 'April',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 200, 'billing_units': 200,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'April', 'month': 'April',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 60, 'billing_units': 60,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'March', 'month': 'March',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 1000, 'billing_units': 1000,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'March', 'month': 'March',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 100, 'billing_units': 100,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'March', 'month': 'March',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 100, 'billing_units': 100,
'postage': 'none', 'postage': 'none',
}, },
{ {
'month': 'March', 'month': 'March',
'notification_type': 'sms', 'notification_type': 'sms',
'rate': 0.0165, 'rate': 0.017,
'billing_units': 30, 'billing_units': 30,
'postage': 'none', 'postage': 'none',
}, },