Add DAO for getting billable units/financial year

In order to invoice people we need to know how many text message
fragments they’ve sent per month.

This should be per (government) financial year, ie April 1st to April
1st because we’ll only ever show a page for one year (because the
250,000 allowance is topped up at the start of every financial year).

This commit only does the DAO bit, not the REST bit.
This commit is contained in:
Chris Hill-Scott
2016-09-30 17:17:28 +01:00
parent 8d188f4664
commit 6a5e947220
2 changed files with 71 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ from datetime import (
from flask import current_app
from werkzeug.datastructures import MultiDict
from sqlalchemy import (desc, func, or_, and_, asc)
from sqlalchemy import (desc, func, or_, and_, asc, cast, Text)
from sqlalchemy.orm import joinedload
from app import db
@@ -209,6 +209,24 @@ def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page
)
@statsd(namespace="dao")
def get_notification_billable_unit_count_per_month(service_id, year):
start, end = get_financial_year(year)
return db.session.query(
func.to_char(NotificationHistory.created_at, "FMMonth"),
func.sum(NotificationHistory.billable_units)
).group_by(
func.to_char(NotificationHistory.created_at, "FMMonth"),
func.to_char(NotificationHistory.created_at, "YYYY-MM")
).order_by(
func.to_char(NotificationHistory.created_at, "YYYY-MM")
).filter(
NotificationHistory.service_id == service_id,
NotificationHistory.created_at >= start,
NotificationHistory.created_at < end
).all()
@statsd(namespace="dao")
def get_notification_with_personalisation(service_id, notification_id, key_type):
filter_dict = {'service_id': service_id, 'id': notification_id}
@@ -319,3 +337,10 @@ def dao_timeout_notifications(timeout_period_in_seconds):
update({'status': NOTIFICATION_TEMPORARY_FAILURE, 'updated_at': update_at}, synchronize_session=False)
db.session.commit()
return updated
def get_financial_year(year):
return (
date(year, 4, 1),
date(year + 1, 4, 1)
)