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,