Fix query to populate ft_billing table.

The group by for the query was wrong which would result in 2 rows with different totals but the same unique key, so the second row would update the first row. Meaning we had incorrect numbers for the billing data.
Because some of the data had null for the sent_by column, the select would turn the Null --> dvla, but that same function was not used in the group by. So any time we had missing sent_by data we would end up with 2 rows where one would overwrite the other.
This commit is contained in:
Rebecca Law
2019-11-15 10:23:48 +00:00
parent 0979b41562
commit db0d45966f
2 changed files with 38 additions and 15 deletions

View File

@@ -336,25 +336,20 @@ def _query_for_billing_data(table, notification_type, start_date, end_date, serv
EMAIL_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE,
LETTER_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS
}
sent_by_func = func.coalesce(table.sent_by, case(
[(table.notification_type == 'letter', 'dvla'),
(table.notification_type == 'sms', 'unknown'),
(table.notification_type == 'email', 'ses')]), )
letter_page_count = case([(table.notification_type == 'letter', table.billable_units), ])
query = db.session.query(
table.template_id,
table.service_id,
table.notification_type,
func.coalesce(table.sent_by,
case(
[
(table.notification_type == 'letter', 'dvla'),
(table.notification_type == 'sms', 'unknown'),
(table.notification_type == 'email', 'ses')
]),
).label('sent_by'),
sent_by_func.label('sent_by'),
func.coalesce(table.rate_multiplier, 1).cast(Integer).label('rate_multiplier'),
func.coalesce(table.international, False).label('international'),
case(
[
(table.notification_type == 'letter', table.billable_units),
]
).label('letter_page_count'),
letter_page_count.label('letter_page_count'),
func.sum(table.billable_units).label('billable_units'),
func.count().label('notifications_sent'),
Service.crown,
@@ -370,8 +365,8 @@ def _query_for_billing_data(table, notification_type, start_date, end_date, serv
table.template_id,
table.service_id,
table.notification_type,
'sent_by',
'letter_page_count',
sent_by_func,
letter_page_count,
table.rate_multiplier,
table.international,
Service.crown,