Fix incorrect chargeable_units column

This was added to migrate away from the vague "billing_units" field,
but without fixing the inconsistent data behind it:

- For emails and letters, "billing_units" was just the number sent.

- For SMS, "billing_units" really was the chargeable_units.

To avoid confusion we need two fields to represent the original mix
of data - this exposes "notifications_sent" in both APIs.
This commit is contained in:
Ben Thorner
2022-04-26 18:18:36 +01:00
parent 2bdaeabbaa
commit 99b2b4642e
4 changed files with 19 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ def serialize_ft_billing_remove_emails(rows):
"cost": float(row.cost),
"free_chargeable_units": row.free_chargeable_units,
"charged_units": row.charged_units,
"notifications_sent": row.notifications_sent,
}
for row in rows
if row.notification_type != 'email'
@@ -44,6 +45,7 @@ def serialize_ft_billing_yearly_totals(rows):
"cost": float(row.cost),
"free_chargeable_units": row.free_chargeable_units,
"charged_units": row.charged_units,
"notifications_sent": row.notifications_sent,
}
for row in rows
]

View File

@@ -202,7 +202,7 @@ def fetch_billing_totals_for_year(service_id, year):
db.session.query(
func.sum(query.c.notifications_sent).label("notifications_sent"),
# TEMPORARY: while we switch to "chargeable units"
func.sum(query.c.chargeable_units).label("billable_units"),
func.sum(query.c.billable_units).label("billable_units"),
func.sum(query.c.chargeable_units).label("chargeable_units"),
query.c.rate.label("rate"),
query.c.notification_type.label("notification_type"),
@@ -241,7 +241,7 @@ def fetch_monthly_billing_for_year(service_id, year):
func.date_trunc('month', query.c.bst_date).cast(Date).label("month"),
func.sum(query.c.notifications_sent).label("notifications_sent"),
# TEMPORARY: while we switch to "chargeable units"
func.sum(query.c.chargeable_units).label("billable_units"),
func.sum(query.c.billable_units).label("billable_units"),
func.sum(query.c.chargeable_units).label("chargeable_units"),
query.c.rate.label("rate"),
query.c.postage.label("postage"),
@@ -275,7 +275,9 @@ def query_service_email_usage_for_year(service_id, year):
FactBilling.bst_date,
FactBilling.postage, # should always be "none"
FactBilling.notifications_sent,
FactBilling.notifications_sent.label("chargeable_units"),
# TEMPORARY: while we switch to "chargeable units"
FactBilling.notifications_sent.label("billable_units"),
FactBilling.billable_units.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,
FactBilling.notifications_sent.label("charged_units"),
@@ -296,7 +298,9 @@ def query_service_letter_usage_for_year(service_id, year):
FactBilling.bst_date,
FactBilling.postage,
FactBilling.notifications_sent,
FactBilling.notifications_sent.label("chargeable_units"),
# TEMPORARY: while we switch to "chargeable units"
FactBilling.notifications_sent.label("billable_units"),
FactBilling.billable_units.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,
FactBilling.notifications_sent.label("charged_units"),
@@ -348,6 +352,8 @@ def query_service_sms_usage_for_year(service_id, year):
FactBilling.bst_date,
FactBilling.postage, # should always be "none"
FactBilling.notifications_sent,
# TEMPORARY: while we switch to "chargeable units"
chargeable_units.label("billable_units"),
chargeable_units.label("chargeable_units"),
FactBilling.rate,
FactBilling.notification_type,

View File

@@ -178,6 +178,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
assert letter_row["cost"] == 9.9
assert letter_row["free_chargeable_units"] == 0
assert letter_row["charged_units"] == 30
assert letter_row["notifications_sent"] == 30
assert sms_row["month"] == "April"
assert sms_row["notification_type"] == "sms"
@@ -189,6 +190,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
assert sms_row["cost"] == 4.212
assert sms_row["free_chargeable_units"] == 4
assert sms_row["charged_units"] == 26
assert sms_row["notifications_sent"] == 30
def set_up_yearly_data():
@@ -263,6 +265,7 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
assert json_response[0]['cost'] == 0
assert json_response[0]['free_chargeable_units'] == 0
assert json_response[0]['charged_units'] == 275
assert json_response[0]['notifications_sent'] == 275
assert json_response[1]['notification_type'] == 'letter'
assert json_response[1]['billing_units'] == 275
@@ -272,6 +275,7 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
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[1]['notifications_sent'] == 275
assert json_response[2]['notification_type'] == 'sms'
assert json_response[2]['billing_units'] == 825
@@ -281,3 +285,4 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
assert json_response[2]['cost'] == 13.3002
assert json_response[2]['free_chargeable_units'] == 4
assert json_response[2]['charged_units'] == 821
assert json_response[2]['notifications_sent'] == 825

View File

@@ -489,7 +489,7 @@ def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 1
assert results[1].billable_units == 1
assert results[1].chargeable_units == 1
assert results[1].chargeable_units == 2
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0
@@ -614,7 +614,7 @@ def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 1
assert results[1].billable_units == 1
assert results[1].chargeable_units == 1
assert results[1].chargeable_units == 2
assert results[1].rate == Decimal('0.36')
assert results[1].cost == Decimal('0.36')
assert results[1].free_chargeable_units == 0