Add "charged_units" to usage APIs

This commit is contained in:
Ben Thorner
2022-04-26 17:56:17 +01:00
parent 2999fa6714
commit 2bdaeabbaa
4 changed files with 40 additions and 8 deletions
+2
View File
@@ -24,6 +24,7 @@ def serialize_ft_billing_remove_emails(rows):
"postage": row.postage,
"cost": float(row.cost),
"free_chargeable_units": row.free_chargeable_units,
"charged_units": row.charged_units,
}
for row in rows
if row.notification_type != 'email'
@@ -42,6 +43,7 @@ def serialize_ft_billing_yearly_totals(rows):
"letter_total": float(row.billable_units * row.rate) if row.notification_type == 'letter' else 0,
"cost": float(row.cost),
"free_chargeable_units": row.free_chargeable_units,
"charged_units": row.charged_units,
}
for row in rows
]
+17 -8
View File
@@ -208,6 +208,7 @@ def fetch_billing_totals_for_year(service_id, year):
query.c.notification_type.label("notification_type"),
func.sum(query.c.cost).label("cost"),
func.sum(query.c.free_chargeable_units).label("free_chargeable_units"),
func.sum(query.c.charged_units).label("charged_units"),
).group_by(
query.c.rate,
query.c.notification_type
@@ -247,6 +248,7 @@ def fetch_monthly_billing_for_year(service_id, year):
query.c.notification_type.label("notification_type"),
func.sum(query.c.cost).label("cost"),
func.sum(query.c.free_chargeable_units).label("free_chargeable_units"),
func.sum(query.c.charged_units).label("charged_units"),
).group_by(
query.c.rate,
query.c.notification_type,
@@ -276,6 +278,7 @@ def query_service_email_usage_for_year(service_id, year):
FactBilling.notifications_sent.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,
FactBilling.notifications_sent.label("charged_units"),
literal(0).label("free_chargeable_units"),
literal(0).label("cost"),
).filter(
@@ -296,6 +299,7 @@ def query_service_letter_usage_for_year(service_id, year):
FactBilling.notifications_sent.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,
FactBilling.notifications_sent.label("charged_units"),
literal(0).label("free_chargeable_units"),
(FactBilling.notifications_sent * FactBilling.rate).label("cost"),
).filter(
@@ -330,6 +334,16 @@ def query_service_sms_usage_for_year(service_id, year):
0
)
charged_units = func.greatest(
chargeable_units - cumulative_free_remainder,
literal(0)
).cast(Integer) # for some reason the result is a String!
free_chargeable_units = func.least(
cumulative_free_remainder,
chargeable_units,
).cast(Integer) # for some reason the result is a Decimal
return db.session.query(
FactBilling.bst_date,
FactBilling.postage, # should always be "none"
@@ -337,14 +351,9 @@ def query_service_sms_usage_for_year(service_id, year):
chargeable_units.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,
func.least(
cumulative_free_remainder,
chargeable_units,
).cast(Integer).label("free_chargeable_units"), # for some reason the result is a Decimal
(
func.greatest(chargeable_units - cumulative_free_remainder, literal(0)) *
FactBilling.rate
).label("cost")
charged_units.label("charged_units"),
free_chargeable_units.label("free_chargeable_units"),
(charged_units * FactBilling.rate).label("cost")
).outerjoin(
AnnualBilling,
AnnualBilling.service_id == service_id
+5
View File
@@ -177,6 +177,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
assert letter_row["postage"] == "second"
assert letter_row["cost"] == 9.9
assert letter_row["free_chargeable_units"] == 0
assert letter_row["charged_units"] == 30
assert sms_row["month"] == "April"
assert sms_row["notification_type"] == "sms"
@@ -187,6 +188,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
# free allowance is 4, so (30 - 4) * 0.162
assert sms_row["cost"] == 4.212
assert sms_row["free_chargeable_units"] == 4
assert sms_row["charged_units"] == 26
def set_up_yearly_data():
@@ -260,6 +262,7 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
assert json_response[0]['letter_total'] == 0
assert json_response[0]['cost'] == 0
assert json_response[0]['free_chargeable_units'] == 0
assert json_response[0]['charged_units'] == 275
assert json_response[1]['notification_type'] == 'letter'
assert json_response[1]['billing_units'] == 275
@@ -268,6 +271,7 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
assert json_response[1]['letter_total'] == 90.75
assert json_response[1]['cost'] == 90.75
assert json_response[1]['free_chargeable_units'] == 0
assert json_response[1]['charged_units'] == 275
assert json_response[2]['notification_type'] == 'sms'
assert json_response[2]['billing_units'] == 825
@@ -276,3 +280,4 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
assert json_response[2]['letter_total'] == 0
assert json_response[2]['cost'] == 13.3002
assert json_response[2]['free_chargeable_units'] == 4
assert json_response[2]['charged_units'] == 821
+16
View File
@@ -430,6 +430,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
assert results[0].free_chargeable_units == 0
assert results[0].charged_units == 30
assert str(results[1].month) == "2016-04-01"
assert results[1].notification_type == 'letter'
@@ -439,6 +440,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[1].rate == Decimal('0.30')
assert results[1].cost == Decimal('9')
assert results[1].free_chargeable_units == 0
assert results[1].charged_units == 30
assert str(results[1].month) == "2016-04-01"
assert results[2].notification_type == 'letter'
@@ -448,6 +450,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[2].rate == Decimal('0.33')
assert results[2].cost == Decimal('9.9')
assert results[2].free_chargeable_units == 0
assert results[2].charged_units == 30
assert str(results[3].month) == "2016-04-01"
assert results[3].notification_type == 'sms'
@@ -458,6 +461,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
# free allowance is 10, so (30 - 10) * 0.162
assert results[3].cost == Decimal('3.24')
assert results[3].free_chargeable_units == 10
assert results[3].charged_units == 20
assert str(results[4].month) == "2016-05-01"
assert str(results[47].month) == "2017-03-01"
@@ -479,6 +483,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[0].rate == Decimal('0.33')
assert results[0].cost == Decimal('0.33')
assert results[0].free_chargeable_units == 0
assert results[0].charged_units == 1
assert str(results[1].month) == "2018-05-01"
assert results[1].notification_type == 'letter'
@@ -488,6 +493,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0
assert results[1].charged_units == 1
assert str(results[2].month) == "2018-05-01"
assert results[2].notification_type == 'sms'
@@ -498,6 +504,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
# 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 results[2].charged_units == 0
assert str(results[3].month) == "2018-05-01"
assert results[3].notification_type == 'sms'
@@ -508,6 +515,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
# 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
assert results[3].charged_units == 3
@freeze_time('2018-08-01 13:30:00')
@@ -543,6 +551,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
assert results[0].free_chargeable_units == 0
assert results[0].charged_units == 365
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 365
@@ -551,6 +560,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[1].rate == Decimal('0.3')
assert results[1].cost == Decimal('109.5')
assert results[1].free_chargeable_units == 0
assert results[1].charged_units == 365
assert results[2].notification_type == 'letter'
assert results[2].notifications_sent == 365
@@ -559,6 +569,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[2].rate == Decimal('0.33')
assert results[2].cost == Decimal('120.45')
assert results[2].free_chargeable_units == 0
assert results[2].charged_units == 365
assert results[3].notification_type == 'sms'
assert results[3].notifications_sent == 365
@@ -567,6 +578,7 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[3].rate == Decimal('0.162')
assert results[3].cost == Decimal('0')
assert results[3].free_chargeable_units == 365
assert results[3].charged_units == 0
def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_session):
@@ -597,6 +609,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[0].rate == Decimal('0.33')
assert results[0].cost == Decimal('0.33')
assert results[0].free_chargeable_units == 0
assert results[0].charged_units == 1
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 1
@@ -605,6 +618,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0
assert results[1].charged_units == 1
assert results[2].notification_type == 'sms'
assert results[2].notifications_sent == 1
@@ -614,6 +628,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
# 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[2].charged_units == 0
assert results[3].notification_type == 'sms'
assert results[3].notifications_sent == 2
@@ -623,6 +638,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
# 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
assert results[3].charged_units == 3
def test_delete_billing_data(notify_db_session):