Use new "cost" field in usage APIs

The previous, manual calculation could be incorrect depending on
which SMS rates the free allowance was attributed to.

The new field also supersedes the old "letter_total" bolt-on so we
can get cost information consistently for both types.
This commit is contained in:
Ben Thorner
2022-04-26 15:40:29 +01:00
parent 82c3e8093d
commit e0aa51c306
5 changed files with 14 additions and 12 deletions

View File

@@ -332,13 +332,14 @@ def get_annual_usage_breakdown(usage, free_sms_fragment_limit):
sms_rate = 0 if len(sms) == 0 else sms[0].get("rate", 0)
sms_chargeable_units = sum(row['chargeable_units'] for row in sms)
sms_free_allowance = free_sms_fragment_limit
sms_cost = sum(row['cost'] for row in sms)
emails = get_usage_breakdown_by_type(usage, 'email')
emails_sent = sum(row['notifications_sent'] for row in emails)
letters = get_usage_breakdown_by_type(usage, 'letter')
letters_sent = sum(row['notifications_sent'] for row in letters)
letters_cost = sum(row['letter_total'] for row in letters)
letters_cost = sum(row['cost'] for row in letters)
return {
'emails_sent': emails_sent,
@@ -346,6 +347,7 @@ def get_annual_usage_breakdown(usage, free_sms_fragment_limit):
'sms_sent': sms_chargeable_units,
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_chargeable_units)),
'sms_chargeable': max(0, sms_chargeable_units - sms_free_allowance),
'sms_cost': sms_cost,
'sms_rate': sms_rate,
'letter_sent': letters_sent,
'letter_cost': letters_cost

View File

@@ -8,9 +8,9 @@
</div>
<div class='govuk-grid-column-one-third'>
<div class="keyline-block">
{% if sms_chargeable %}
{% if sms_cost %}
{{ big_number(
sms_chargeable * sms_rate,
sms_cost,
'spent on text messages',
currency="£",
smaller=True

View File

@@ -59,7 +59,7 @@
<div class='govuk-grid-column-one-third'>
<div class="keyline-block">
{{ big_number(
(sms_chargeable * sms_rate),
sms_cost,
'spent',
currency="£",
smaller=True

View File

@@ -1020,7 +1020,7 @@ def test_usage_page(
assert '251,800 sent' in sms_column
assert '250,000 free allowance' in sms_column
assert '0 free allowance remaining' in sms_column
assert '£36.14 spent' in sms_column
assert '£29.85 spent' in sms_column
assert '1,800 at 1.65 pence' in sms_column
letter_column = normalize_spaces(annual_usage[2].text + annual_usage[5].text)
@@ -1855,7 +1855,7 @@ def test_service_dashboard_shows_usage(
) == (
'Unlimited '
'free email allowance '
'£36.14 '
'£29.85 '
'spent on text messages '
'£30.00 '
'spent on letters'

View File

@@ -2321,28 +2321,28 @@ def mock_get_usage(mocker, service_one, fake_uuid):
"chargeable_units": 1000,
"notifications_sent": 1000,
"rate": 0.00,
"letter_total": 0
"cost": 0
},
{
"notification_type": "sms",
"chargeable_units": 251500,
"notifications_sent": 105000,
"rate": 0.0165,
"letter_total": 0
"cost": 24.75 # 250K free allowance
},
{
"notification_type": "sms",
"chargeable_units": 300,
"notifications_sent": 300,
"rate": 0.0165,
"letter_total": 0
"cost": 5.1
},
{
"notification_type": "letter",
"chargeable_units": 300,
"notifications_sent": 100,
"rate": 0.1,
"letter_total": 30
"cost": 30
},
]
@@ -2467,14 +2467,14 @@ def mock_get_future_usage(mocker, service_one, fake_uuid):
'chargeable_units': 0,
'notifications_sent': 0,
'rate': 0.0158,
'letter_total': 0
'cost': 0
},
{
'notification_type': 'email',
'chargeable_units': 0,
'notifications_sent': 0,
'rate': 0.0,
'letter_total': 0
'cost': 0
}
]