Add "free_chargeable_units" to service usage APIs

This represents the number of chargeable_units that were actually
free due to the free allowance - they won't be included in "cost".
Although the existing calculations in Admin [^1][^2] will still be
correct with a change in SMS rates - it's cost that's the problem
- it makes sense to have all the knowledge about calculating usage
consistently in these two APIs.

[^1]: 474d7dfda8/app/main/views/dashboard.py (L490)
[^2]: c63660d56d/app/main/views/dashboard.py (L350)
This commit is contained in:
Ben Thorner
2022-04-21 12:55:30 +01:00
parent ff32000180
commit 2999fa6714
4 changed files with 31 additions and 0 deletions

View File

@@ -429,6 +429,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[0].chargeable_units == 30
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
assert results[0].free_chargeable_units == 0
assert str(results[1].month) == "2016-04-01"
assert results[1].notification_type == 'letter'
@@ -437,6 +438,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[1].chargeable_units == 30
assert results[1].rate == Decimal('0.30')
assert results[1].cost == Decimal('9')
assert results[1].free_chargeable_units == 0
assert str(results[1].month) == "2016-04-01"
assert results[2].notification_type == 'letter'
@@ -445,6 +447,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[2].chargeable_units == 30
assert results[2].rate == Decimal('0.33')
assert results[2].cost == Decimal('9.9')
assert results[2].free_chargeable_units == 0
assert str(results[3].month) == "2016-04-01"
assert results[3].notification_type == 'sms'
@@ -454,6 +457,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[3].rate == Decimal('0.162')
# free allowance is 10, so (30 - 10) * 0.162
assert results[3].cost == Decimal('3.24')
assert results[3].free_chargeable_units == 10
assert str(results[4].month) == "2016-05-01"
assert str(results[47].month) == "2017-03-01"
@@ -474,6 +478,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[0].chargeable_units == 1
assert results[0].rate == Decimal('0.33')
assert results[0].cost == Decimal('0.33')
assert results[0].free_chargeable_units == 0
assert str(results[1].month) == "2018-05-01"
assert results[1].notification_type == 'letter'
@@ -482,6 +487,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[1].chargeable_units == 1
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0
assert str(results[2].month) == "2018-05-01"
assert results[2].notification_type == 'sms'
@@ -491,6 +497,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[2].rate == Decimal('0.015')
# 4 free units sent on the 16th, 0 on the 17th
assert results[2].cost == Decimal('0')
assert results[2].free_chargeable_units == 4
assert str(results[3].month) == "2018-05-01"
assert results[3].notification_type == 'sms'
@@ -500,6 +507,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[3].rate == Decimal('0.162')
# 1 free unit on the 16th, 1 on the 17th (+ 3 paid)
assert results[3].cost == Decimal('0.486')
assert results[3].free_chargeable_units == 2
@freeze_time('2018-08-01 13:30:00')
@@ -534,6 +542,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[0].chargeable_units == 365
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
assert results[0].free_chargeable_units == 0
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 365
@@ -541,6 +550,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[1].chargeable_units == 365
assert results[1].rate == Decimal('0.3')
assert results[1].cost == Decimal('109.5')
assert results[1].free_chargeable_units == 0
assert results[2].notification_type == 'letter'
assert results[2].notifications_sent == 365
@@ -548,6 +558,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[2].chargeable_units == 365
assert results[2].rate == Decimal('0.33')
assert results[2].cost == Decimal('120.45')
assert results[2].free_chargeable_units == 0
assert results[3].notification_type == 'sms'
assert results[3].notifications_sent == 365
@@ -555,6 +566,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[3].chargeable_units == 365
assert results[3].rate == Decimal('0.162')
assert results[3].cost == Decimal('0')
assert results[3].free_chargeable_units == 365
def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_session):
@@ -584,6 +596,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[0].chargeable_units == 1
assert results[0].rate == Decimal('0.33')
assert results[0].cost == Decimal('0.33')
assert results[0].free_chargeable_units == 0
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 1
@@ -591,6 +604,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[1].chargeable_units == 1
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0
assert results[2].notification_type == 'sms'
assert results[2].notifications_sent == 1
@@ -599,6 +613,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[2].rate == Decimal('0.015')
# 4 units sent on the 16th, 0 on the 17th
assert results[2].cost == Decimal('0')
assert results[2].free_chargeable_units == 4
assert results[3].notification_type == 'sms'
assert results[3].notifications_sent == 2
@@ -607,6 +622,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[3].rate == Decimal('0.162')
# 1 free unit on the 16th, 1 on the 17th (+ 3 paid)
assert results[3].cost == Decimal('0.486') # (5 - 2) * 0.162
assert results[3].free_chargeable_units == 2
def test_delete_billing_data(notify_db_session):