Only include ft_billing data for current financial year

Test get_live_services_data endpoint

Expand dao_fetch_live_services_data test with more ft_billing records
This commit is contained in:
Pea Tyczynska
2019-04-29 15:49:12 +01:00
parent 669db0b4ca
commit c15d7878fc
4 changed files with 61 additions and 14 deletions

View File

@@ -7,8 +7,8 @@ import pytz
def get_months_for_financial_year(year):
return [
convert_bst_to_utc(month) for month in (
get_months_for_year(4, 13, year) +
get_months_for_year(1, 4, year + 1)
get_months_for_year(4, 13, year)
+ get_months_for_year(1, 4, year + 1)
)
if convert_bst_to_utc(month) < datetime.now()
]
@@ -22,6 +22,14 @@ def get_financial_year(year):
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
def get_current_financial_year():
now = datetime.utcnow()
current_month = int(now.strftime('%-m'))
current_year = int(now.strftime('%Y'))
year = current_year if current_month > 3 else current_year - 1
return get_financial_year(year)
def get_april_fools(year):
"""
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,

View File

@@ -7,6 +7,7 @@ from sqlalchemy.orm import joinedload
from flask import current_app
from app import db
from app.dao.date_util import get_current_financial_year
from app.dao.dao_utils import (
transactional,
version_class,
@@ -75,6 +76,11 @@ def dao_count_live_services():
def dao_fetch_live_services_data():
year_start_date, year_end_date = get_current_financial_year()
this_year_ft_billing = FactBilling.query.filter(
FactBilling.bst_date >= year_start_date,
FactBilling.bst_date <= year_end_date,
).subquery()
data = db.session.query(
Service.id,
Organisation.name.label("organisation_name"),
@@ -89,18 +95,18 @@ def dao_fetch_live_services_data():
Service.volume_email,
Service.volume_letter,
case([
(FactBilling.notification_type == 'email', func.sum(FactBilling.notifications_sent))
(this_year_ft_billing.c.notification_type == 'email', func.sum(this_year_ft_billing.c.notifications_sent))
], else_=0).label("email_totals"),
case([
(FactBilling.notification_type == 'sms', func.sum(FactBilling.notifications_sent))
(this_year_ft_billing.c.notification_type == 'sms', func.sum(this_year_ft_billing.c.notifications_sent))
], else_=0).label("sms_totals"),
case([
(FactBilling.notification_type == 'letter', func.sum(FactBilling.notifications_sent))
(this_year_ft_billing.c.notification_type == 'letter', func.sum(this_year_ft_billing.c.notifications_sent))
], else_=0).label("letter_totals"),
).outerjoin(
Service.organisation
).outerjoin(
FactBilling, Service.id == FactBilling.service_id
this_year_ft_billing, Service.id == this_year_ft_billing.c.service_id
).outerjoin(
User, Service.go_live_user_id == User.id
).group_by(
@@ -116,7 +122,7 @@ def dao_fetch_live_services_data():
Service.volume_sms,
Service.volume_email,
Service.volume_letter,
FactBilling.notification_type
this_year_ft_billing.c.notification_type
).all()
results = []
for row in data: