Merge pull request #2599 from alphagov/billing-for-all-services

Billing for all services
This commit is contained in:
Leo Hemsted
2019-09-02 16:38:05 +01:00
committed by GitHub
9 changed files with 591 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, date, time
from notifications_utils.timezones import convert_bst_to_utc
import pytz
@@ -61,3 +61,14 @@ def get_current_financial_year_start_year():
if now < start_date:
financial_year_start = financial_year_start - 1
return financial_year_start
def get_financial_year_for_datetime(start_date):
if type(start_date) == date:
start_date = datetime.combine(start_date, time.min)
year = int(start_date.strftime('%Y'))
if start_date < get_april_fools(year):
return year - 1
else:
return year

View File

@@ -1,12 +1,16 @@
from datetime import datetime, timedelta, time
from datetime import datetime, timedelta, time, date
from flask import current_app
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy import func, case, desc, Date, Integer
from sqlalchemy import func, case, desc, Date, Integer, and_
from app import db
from app.dao.date_util import get_financial_year
from app.dao.date_util import (
get_financial_year,
get_financial_year_for_datetime
)
from app.models import (
FactBilling,
Notification,
@@ -19,11 +23,163 @@ from app.models import (
NOTIFICATION_STATUS_TYPES_BILLABLE,
NotificationHistory,
EMAIL_TYPE,
NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS
NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS,
AnnualBilling,
Organisation,
)
from app.utils import get_london_midnight_in_utc
def fetch_sms_free_allowance_remainder(start_date):
# ASSUMPTION: AnnualBilling has been populated for year.
billing_year = get_financial_year_for_datetime(start_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 < start_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):
# ASSUMPTION: AnnualBilling has been populated for year.
free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery()
sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier)
sms_remainder = func.coalesce(
free_allowance_remainder.c.sms_remainder,
free_allowance_remainder.c.free_sms_fragment_limit
)
chargeable_sms = func.greatest(sms_billable_units - 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"),
free_allowance_remainder.c.free_sms_fragment_limit,
FactBilling.rate.label('sms_rate'),
sms_remainder.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(
free_allowance_remainder, Service.id == free_allowance_remainder.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,
free_allowance_remainder.c.free_sms_fragment_limit,
free_allowance_remainder.c.sms_remainder,
FactBilling.rate,
).order_by(
Organisation.name,
Service.name
)
return query.all()
def fetch_letter_costs_for_all_services(start_date, end_date):
query = db.session.query(
Organisation.name.label("organisation_name"),
Organisation.id.label("organisation_id"),
Service.name.label("service_name"),
Service.id.label("service_id"),
func.sum(FactBilling.notifications_sent * FactBilling.rate).label("letter_cost")
).select_from(
Service
).outerjoin(
Service.organisation
).join(
FactBilling, FactBilling.service_id == Service.id,
).filter(
FactBilling.service_id == Service.id,
FactBilling.bst_date >= start_date,
FactBilling.bst_date <= end_date,
FactBilling.notification_type == LETTER_TYPE,
).group_by(
Organisation.name,
Organisation.id,
Service.id,
Service.name,
).order_by(
Organisation.name,
Service.name
)
return query.all()
def fetch_letter_line_items_for_all_services(start_date, end_date):
query = db.session.query(
Organisation.name.label("organisation_name"),
Organisation.id.label("organisation_id"),
Service.name.label("service_name"),
Service.id.label("service_id"),
FactBilling.billable_units.label("sheet_count"),
FactBilling.rate.label("letter_rate"),
FactBilling.postage.label("postage"),
func.sum(FactBilling.notifications_sent).label("letters_sent"),
).select_from(
Service
).outerjoin(
Service.organisation
).join(
FactBilling, FactBilling.service_id == Service.id,
).filter(
FactBilling.bst_date >= start_date,
FactBilling.bst_date <= end_date,
FactBilling.notification_type == LETTER_TYPE,
).group_by(
Organisation.name,
Organisation.id,
Service.id,
Service.name,
FactBilling.billable_units,
FactBilling.rate,
FactBilling.postage
).order_by(
Organisation.name,
Service.name,
FactBilling.postage.desc(),
FactBilling.rate,
)
return query.all()
def fetch_billing_totals_for_year(service_id, year):
year_start_date, year_end_date = get_financial_year(year)
"""