mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
use func.greatest rather than case statement
simplifies the query quite a bit
This commit is contained in:
@@ -34,7 +34,6 @@ from app.utils import get_london_midnight_in_utc
|
|||||||
def fetch_sms_free_allowance_remainder(start_date):
|
def fetch_sms_free_allowance_remainder(start_date):
|
||||||
# ASSUMPTION: AnnualBilling has been populated for year.
|
# ASSUMPTION: AnnualBilling has been populated for year.
|
||||||
billing_year = get_financial_year_for_datetime(start_date)
|
billing_year = get_financial_year_for_datetime(start_date)
|
||||||
print(billing_year)
|
|
||||||
start_of_year = convert_utc_to_bst(financial_year_start(billing_year))
|
start_of_year = convert_utc_to_bst(financial_year_start(billing_year))
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
FactBilling.service_id.label("service_id"),
|
FactBilling.service_id.label("service_id"),
|
||||||
@@ -62,16 +61,11 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
|
|||||||
# ASSUMPTION: AnnualBilling has been populated for year.
|
# ASSUMPTION: AnnualBilling has been populated for year.
|
||||||
billing_year = get_financial_year_for_datetime(start_date)
|
billing_year = get_financial_year_for_datetime(start_date)
|
||||||
free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery()
|
free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery()
|
||||||
|
|
||||||
sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier)
|
sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier)
|
||||||
sms_remainder = func.coalesce(free_allowance_remainder.c.sms_remainder, AnnualBilling.free_sms_fragment_limit)
|
sms_remainder = func.coalesce(free_allowance_remainder.c.sms_remainder, AnnualBilling.free_sms_fragment_limit)
|
||||||
chargeable_sms = case([(sms_remainder == 0, sms_billable_units),
|
chargeable_sms = func.greatest(sms_billable_units - sms_remainder, 0)
|
||||||
(sms_billable_units - sms_remainder <= 0, 0),
|
sms_cost = chargeable_sms * FactBilling.rate
|
||||||
(sms_billable_units - sms_remainder > 0,
|
|
||||||
sms_billable_units - sms_remainder)], else_=0)
|
|
||||||
sms_cost = case([(sms_remainder == 0, sms_billable_units * FactBilling.rate),
|
|
||||||
(sms_billable_units - sms_remainder <= 0, 0),
|
|
||||||
(sms_billable_units - sms_remainder > 0, (sms_billable_units - sms_remainder) * FactBilling.rate)
|
|
||||||
], else_=0)
|
|
||||||
|
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
Organisation.name.label('organisation_name'),
|
Organisation.name.label('organisation_name'),
|
||||||
|
|||||||
Reference in New Issue
Block a user