diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py
index 9e0dba73d..1aa5c1c37 100644
--- a/app/main/views/dashboard.py
+++ b/app/main/views/dashboard.py
@@ -435,8 +435,12 @@ def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, bi
free_sms_fragment_limit, cumulative, previous_cumulative,
[billing_month for billing_month in sms_units if billing_month['month'] == month]
)
- letter_billing = [(x['billing_units'], x['rate'], (x['billing_units'] * x['rate']))
+ letter_billing = [(x['billing_units'], x['rate'], (x['billing_units'] * x['rate']), x['postage'])
for x in letter_units if x['month'] == month]
+
+ if letter_billing:
+ letter_billing.sort(key=lambda x: (x[3], x[1]))
+
letter_total = 0
for x in letter_billing:
letter_total += x[2]
diff --git a/app/templates/views/usage-with-letters.html b/app/templates/views/usage-with-letters.html
index 7d26976d6..ebda8c463 100644
--- a/app/templates/views/usage-with-letters.html
+++ b/app/templates/views/usage-with-letters.html
@@ -113,7 +113,7 @@
{% for letter in month.letters%}
{% if letter[0] %}
- - {{ "{:,}".format(letter[0])}} letters at {{ '{:.0f}p'.format(letter[1] * 100) }}
+
- {{ "{:,} {}".format(letter[0], letter[3])}} class letters at {{ '{:.0f}p'.format(letter[1] * 100) }}
{% endif %}
{% endfor %}
diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py
index 0cac648ea..dbdd4604d 100644
--- a/tests/app/main/views/test_dashboard.py
+++ b/tests/app/main/views/test_dashboard.py
@@ -808,8 +808,35 @@ def test_usage_page_with_letters(
assert '140 free text messages' in table
assert '£20.30' in table
assert '1,230 text messages at 1.65p' in table
- assert '10 letters at 31p' in table
- assert '5 letters at 33p' in table
+ assert '10 second class letters at 31p' in table
+ assert '5 first class letters at 33p' in table
+
+
+@freeze_time("2012-04-30 12:12:12")
+def test_usage_page_displays_letters_ordered_by_postage(
+ mocker,
+ logged_in_client,
+ service_one,
+ mock_get_usage,
+ 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': 0.3, 'billing_units': 3, 'postage': 'second'},
+ {'month': 'April', 'notification_type': 'letter', 'rate': 0.5, 'billing_units': 1, 'postage': 'first'},
+ ]
+ mocker.patch('app.billing_api_client.get_billable_units_ft', return_value=billable_units_resp)
+ service_one['permissions'].append('letter')
+ response = logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID))
+
+ page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
+ row_for_april = page.find('table').find('tr', class_='table-row')
+ postage_details = row_for_april.find_all('li', class_='tabular-numbers')
+
+ assert len(postage_details) == 3
+ assert normalize_spaces(postage_details[0].text) == '1 first class letters at 50p'
+ assert normalize_spaces(postage_details[1].text) == '3 second class letters at 30p'
+ assert normalize_spaces(postage_details[2].text) == '1 second class letters at 50p'
def test_usage_page_with_year_argument(