mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 13:38:53 -04:00
normalise join patterns across billing queries
select from service, join to org and ft_billing
This commit is contained in:
@@ -78,7 +78,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
|
|||||||
Organisation.name.label('organisation_name'),
|
Organisation.name.label('organisation_name'),
|
||||||
Organisation.id.label('organisation_id'),
|
Organisation.id.label('organisation_id'),
|
||||||
Service.name.label("service_name"),
|
Service.name.label("service_name"),
|
||||||
FactBilling.service_id.label("service_id"),
|
Service.id.label("service_id"),
|
||||||
free_allowance_remainder.c.free_sms_fragment_limit,
|
free_allowance_remainder.c.free_sms_fragment_limit,
|
||||||
FactBilling.rate.label('sms_rate'),
|
FactBilling.rate.label('sms_rate'),
|
||||||
sms_remainder.label("sms_remainder"),
|
sms_remainder.label("sms_remainder"),
|
||||||
@@ -100,7 +100,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
|
|||||||
).group_by(
|
).group_by(
|
||||||
Organisation.name,
|
Organisation.name,
|
||||||
Organisation.id,
|
Organisation.id,
|
||||||
FactBilling.service_id,
|
Service.id,
|
||||||
Service.name,
|
Service.name,
|
||||||
free_allowance_remainder.c.free_sms_fragment_limit,
|
free_allowance_remainder.c.free_sms_fragment_limit,
|
||||||
free_allowance_remainder.c.sms_remainder,
|
free_allowance_remainder.c.sms_remainder,
|
||||||
@@ -118,10 +118,14 @@ def fetch_letter_costs_for_all_services(start_date, end_date):
|
|||||||
Organisation.name.label("organisation_name"),
|
Organisation.name.label("organisation_name"),
|
||||||
Organisation.id.label("organisation_id"),
|
Organisation.id.label("organisation_id"),
|
||||||
Service.name.label("service_name"),
|
Service.name.label("service_name"),
|
||||||
FactBilling.service_id.label("service_id"),
|
Service.id.label("service_id"),
|
||||||
func.sum(FactBilling.notifications_sent * FactBilling.rate).label("letter_cost")
|
func.sum(FactBilling.notifications_sent * FactBilling.rate).label("letter_cost")
|
||||||
|
).select_from(
|
||||||
|
Service
|
||||||
).outerjoin(
|
).outerjoin(
|
||||||
Organisation, Service.organisation_id == Organisation.id
|
Service.organisation
|
||||||
|
).join(
|
||||||
|
FactBilling, FactBilling.service_id == Service.id,
|
||||||
).filter(
|
).filter(
|
||||||
FactBilling.service_id == Service.id,
|
FactBilling.service_id == Service.id,
|
||||||
FactBilling.bst_date >= start_date,
|
FactBilling.bst_date >= start_date,
|
||||||
@@ -130,7 +134,7 @@ def fetch_letter_costs_for_all_services(start_date, end_date):
|
|||||||
).group_by(
|
).group_by(
|
||||||
Organisation.name,
|
Organisation.name,
|
||||||
Organisation.id,
|
Organisation.id,
|
||||||
FactBilling.service_id,
|
Service.id,
|
||||||
Service.name,
|
Service.name,
|
||||||
).order_by(
|
).order_by(
|
||||||
Organisation.name,
|
Organisation.name,
|
||||||
@@ -145,13 +149,17 @@ def fetch_letter_line_items_for_all_services(start_date, end_date):
|
|||||||
Organisation.name.label("organisation_name"),
|
Organisation.name.label("organisation_name"),
|
||||||
Organisation.id.label("organisation_id"),
|
Organisation.id.label("organisation_id"),
|
||||||
Service.name.label("service_name"),
|
Service.name.label("service_name"),
|
||||||
FactBilling.service_id.label("service_id"),
|
Service.id.label("service_id"),
|
||||||
FactBilling.billable_units.label("sheet_count"),
|
FactBilling.billable_units.label("sheet_count"),
|
||||||
FactBilling.rate.label("letter_rate"),
|
FactBilling.rate.label("letter_rate"),
|
||||||
FactBilling.postage.label("postage"),
|
FactBilling.postage.label("postage"),
|
||||||
func.sum(FactBilling.notifications_sent).label("letters_sent"),
|
func.sum(FactBilling.notifications_sent).label("letters_sent"),
|
||||||
|
).select_from(
|
||||||
|
Service
|
||||||
).outerjoin(
|
).outerjoin(
|
||||||
Organisation, Service.organisation_id == Organisation.id
|
Service.organisation
|
||||||
|
).join(
|
||||||
|
FactBilling, FactBilling.service_id == Service.id,
|
||||||
).filter(
|
).filter(
|
||||||
FactBilling.service_id == Service.id,
|
FactBilling.service_id == Service.id,
|
||||||
FactBilling.bst_date >= start_date,
|
FactBilling.bst_date >= start_date,
|
||||||
@@ -160,7 +168,7 @@ def fetch_letter_line_items_for_all_services(start_date, end_date):
|
|||||||
).group_by(
|
).group_by(
|
||||||
Organisation.name,
|
Organisation.name,
|
||||||
Organisation.id,
|
Organisation.id,
|
||||||
FactBilling.service_id,
|
Service.id,
|
||||||
Service.name,
|
Service.name,
|
||||||
FactBilling.billable_units,
|
FactBilling.billable_units,
|
||||||
FactBilling.rate,
|
FactBilling.rate,
|
||||||
|
|||||||
Reference in New Issue
Block a user