remove old platform admin billing query functions

these aren't going to be used in the future method, so to make the diffs
cleaner just delete them then re-add them. keep the column definitions
from the old query though just to make sure it's clear which things
we're changing/adding/removing
This commit is contained in:
Leo Hemsted
2022-05-31 14:27:21 +01:00
parent 2543bbe7fe
commit ac2fb6d1cb
2 changed files with 12 additions and 110 deletions

View File

@@ -34,90 +34,19 @@ from app.models import (
from app.utils import get_london_midnight_in_utc from app.utils import get_london_midnight_in_utc
def fetch_sms_free_allowance_remainder_until_date(end_date):
# ASSUMPTION: AnnualBilling has been populated for year.
billing_year = get_financial_year_for_datetime(end_date)
start_of_year = date(billing_year, 4, 1)
billable_units = func.coalesce(func.sum(FactBilling.billable_units * FactBilling.rate_multiplier), 0)
query = db.session.query(
AnnualBilling.service_id.label("service_id"),
AnnualBilling.free_sms_fragment_limit,
billable_units.label('billable_units'),
func.greatest((AnnualBilling.free_sms_fragment_limit - billable_units).cast(Integer), 0).label('sms_remainder')
).outerjoin(
# if there are no ft_billing rows for a service we still want to return the annual billing so we can use the
# free_sms_fragment_limit)
FactBilling, and_(
AnnualBilling.service_id == FactBilling.service_id,
FactBilling.bst_date >= start_of_year,
FactBilling.bst_date < end_date,
FactBilling.notification_type == SMS_TYPE,
)
).filter(
AnnualBilling.financial_year_start == billing_year,
).group_by(
AnnualBilling.service_id,
AnnualBilling.free_sms_fragment_limit,
)
return query
def fetch_sms_billing_for_all_services(start_date, end_date): def fetch_sms_billing_for_all_services(start_date, end_date):
# TODO: replace with new aggregate function containing the following columns:
# ASSUMPTION: AnnualBilling has been populated for year. # organisation_name
allowance_left_at_start_date_query = fetch_sms_free_allowance_remainder_until_date(start_date).subquery() # organisation_id
# service_name
sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier) # service_id
# free_sms_fragment_limit
# subtract sms_billable_units units accrued since report's start date to get up-to-date # sms_rate
# allowance remainder # sms_remainder
sms_allowance_left = func.greatest(allowance_left_at_start_date_query.c.sms_remainder - sms_billable_units, 0) # sms_billable_units
# chargeable_billable_sms
# billable units here are for period between start date and end date only, so to see # sms_cost
# how many are chargeable, we need to see how much free allowance was used up in the raise NotImplementedError
# period up until report's start date and then do a subtraction
chargeable_sms = func.greatest(sms_billable_units - allowance_left_at_start_date_query.c.sms_remainder, 0)
sms_cost = chargeable_sms * FactBilling.rate
query = db.session.query(
Organisation.name.label('organisation_name'),
Organisation.id.label('organisation_id'),
Service.name.label("service_name"),
Service.id.label("service_id"),
allowance_left_at_start_date_query.c.free_sms_fragment_limit,
FactBilling.rate.label('sms_rate'),
sms_allowance_left.label("sms_remainder"),
sms_billable_units.label('sms_billable_units'),
chargeable_sms.label("chargeable_billable_sms"),
sms_cost.label('sms_cost'),
).select_from(
Service
).outerjoin(
allowance_left_at_start_date_query, Service.id == allowance_left_at_start_date_query.c.service_id
).outerjoin(
Service.organisation
).join(
FactBilling, FactBilling.service_id == Service.id,
).filter(
FactBilling.bst_date >= start_date,
FactBilling.bst_date <= end_date,
FactBilling.notification_type == SMS_TYPE,
).group_by(
Organisation.name,
Organisation.id,
Service.id,
Service.name,
allowance_left_at_start_date_query.c.free_sms_fragment_limit,
allowance_left_at_start_date_query.c.sms_remainder,
FactBilling.rate,
).order_by(
Organisation.name,
Service.name
)
return query.all()
def fetch_letter_costs_and_totals_for_all_services(start_date, end_date): def fetch_letter_costs_and_totals_for_all_services(start_date, end_date):

View File

@@ -16,7 +16,6 @@ from app.dao.fact_billing_dao import (
fetch_letter_line_items_for_all_services, fetch_letter_line_items_for_all_services,
fetch_monthly_billing_for_year, fetch_monthly_billing_for_year,
fetch_sms_billing_for_all_services, fetch_sms_billing_for_all_services,
fetch_sms_free_allowance_remainder_until_date,
fetch_usage_year_for_organisation, fetch_usage_year_for_organisation,
fetch_volumes_by_service, fetch_volumes_by_service,
get_rate, get_rate,
@@ -636,32 +635,6 @@ def test_delete_billing_data(notify_db_session):
) )
def test_fetch_sms_free_allowance_remainder_until_date_with_two_services(notify_db_session):
service = create_service(service_name='has free allowance')
template = create_template(service=service)
org = create_organisation(name="Org for {}".format(service.name))
dao_add_service_to_organisation(service=service, organisation_id=org.id)
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016)
create_ft_billing(template=template, bst_date=datetime(2016, 4, 20), billable_unit=2, rate=0.11)
create_ft_billing(template=template, bst_date=datetime(2016, 5, 20), billable_unit=3, rate=0.11)
service_2 = create_service(service_name='used free allowance')
template_2 = create_template(service=service_2)
org_2 = create_organisation(name="Org for {}".format(service_2.name))
dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id)
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=20, financial_year_start=2016)
create_ft_billing(template=template_2, bst_date=datetime(2016, 4, 20), billable_unit=12, rate=0.11)
create_ft_billing(template=template_2, bst_date=datetime(2016, 4, 22), billable_unit=10, rate=0.11)
create_ft_billing(template=template_2, bst_date=datetime(2016, 5, 20), billable_unit=3, rate=0.11)
results = fetch_sms_free_allowance_remainder_until_date(datetime(2016, 5, 1)).all()
assert len(results) == 2
service_result = [row for row in results if row[0] == service.id]
assert service_result[0] == (service.id, 10, 2, 8)
service_2_result = [row for row in results if row[0] == service_2.id]
assert service_2_result[0] == (service_2.id, 20, 22, 0)
def test_fetch_sms_billing_for_all_services_for_first_quarter(notify_db_session): def test_fetch_sms_billing_for_all_services_for_first_quarter(notify_db_session):
# This test is useful because the inner query resultset is empty. # This test is useful because the inner query resultset is empty.
service = create_service(service_name='a - has free allowance') service = create_service(service_name='a - has free allowance')