Merge pull request #4225 from alphagov/free-allowance-api-181934027

Change annual usage to work with multiple SMS rates
This commit is contained in:
Ben Thorner
2022-04-29 13:22:54 +01:00
committed by GitHub
5 changed files with 179 additions and 110 deletions

View File

@@ -150,8 +150,7 @@ def usage(service_id):
start=current_financial_year - 2,
end=current_financial_year,
),
**calculate_usage(yearly_usage,
free_sms_allowance)
**get_annual_usage_breakdown(yearly_usage, free_sms_allowance)
)
@@ -315,7 +314,7 @@ def get_dashboard_partials(service_id):
),
'usage': render_template(
'views/dashboard/_usage.html',
**calculate_usage(yearly_usage, free_sms_allowance),
**get_annual_usage_breakdown(yearly_usage, free_sms_allowance),
),
}
@@ -327,31 +326,28 @@ def get_dashboard_totals(statistics):
return statistics
def calculate_usage(usage, free_sms_fragment_limit):
sms_breakdowns = [breakdown for breakdown in usage if breakdown['notification_type'] == 'sms']
# this relies on the assumption: only one SMS rate per financial year.
sms_rate = 0 if len(sms_breakdowns) == 0 else sms_breakdowns[0].get("rate", 0)
sms_sent = get_sum_billing_units(sms_breakdowns)
def get_annual_usage_breakdown(usage, free_sms_fragment_limit):
sms = get_usage_breakdown_by_type(usage, 'sms')
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 = [breakdown["billing_units"] for breakdown in usage if breakdown['notification_type'] == 'email']
emails_sent = 0 if len(emails) == 0 else emails[0]
emails = get_usage_breakdown_by_type(usage, 'email')
emails_sent = sum(row['notifications_sent'] for row in emails)
letters = [(breakdown["billing_units"], breakdown['letter_total']) for breakdown in usage if
breakdown['notification_type'] == 'letter']
letter_sent = sum(row[0] for row in letters)
letter_cost = sum(row[1] for row in letters)
letters = get_usage_breakdown_by_type(usage, 'letter')
letters_sent = sum(row['notifications_sent'] for row in letters)
letters_cost = sum(row['cost'] for row in letters)
return {
'emails_sent': emails_sent,
'sms_free_allowance': sms_free_allowance,
'sms_sent': sms_sent,
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_sent)),
'sms_chargeable': max(0, sms_sent - sms_free_allowance),
'sms_rate': sms_rate,
'letter_sent': letter_sent,
'letter_cost': letter_cost
'sms_sent': sms_chargeable_units,
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_chargeable_units)),
'sms_cost': sms_cost,
'sms_breakdown': sms,
'letter_sent': letters_sent,
'letter_cost': letters_cost
}
@@ -404,6 +400,10 @@ def get_sum_billing_units(billing_units, month=None):
return sum(b['billing_units'] for b in billing_units)
def get_usage_breakdown_by_type(usage, notification_type):
return [row for row in usage if row['notification_type'] == notification_type]
def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, billing_units):
cumulative = 0
sms_units = [x for x in billing_units if x['notification_type'] == 'sms']

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

@@ -33,13 +33,15 @@
{% if sms_free_allowance > 0 %}
{{ big_number(sms_allowance_remaining, 'free allowance remaining', smaller=True) }}
{% endif %}
{% if sms_chargeable %}
{{ big_number(
sms_chargeable,
'at {:.2f} pence per message'.format(sms_rate * 100),
smaller=True
) }}
{% endif %}
{% for row in sms_breakdown %}
{% if row.charged_units > 0 %}
{{ big_number(
row.charged_units,
'at {:.2f} pence per message'.format(row.rate * 100),
smaller=True
) }}
{% endif %}
{% endfor %}
</div>
</div>
<div class='govuk-grid-column-one-third'>
@@ -59,7 +61,7 @@
<div class='govuk-grid-column-one-third'>
<div class="keyline-block">
{{ big_number(
(sms_chargeable * sms_rate),
sms_cost,
'spent',
currency="£",
smaller=True