2017-05-03 17:11:48 +01:00
|
|
|
from datetime import datetime, timedelta
|
2017-04-27 15:43:57 +01:00
|
|
|
|
2018-02-06 09:35:33 +00:00
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2017-12-18 16:46:59 +00:00
|
|
|
from sqlalchemy import Float, Integer, and_
|
2017-05-02 10:00:47 +01:00
|
|
|
from sqlalchemy import func, case, cast
|
2017-04-27 15:43:57 +01:00
|
|
|
from sqlalchemy import literal_column
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
from app import db
|
2017-08-10 17:06:31 +01:00
|
|
|
from app.dao.date_util import get_financial_year
|
|
|
|
|
from app.models import (
|
|
|
|
|
NotificationHistory,
|
|
|
|
|
Rate,
|
|
|
|
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
|
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
SMS_TYPE,
|
2017-12-11 16:55:23 +00:00
|
|
|
EMAIL_TYPE,
|
|
|
|
|
LETTER_TYPE,
|
|
|
|
|
LetterRate,
|
|
|
|
|
Service
|
2017-08-10 17:06:31 +01:00
|
|
|
)
|
2017-08-21 13:46:16 +01:00
|
|
|
from app.utils import get_london_month_from_utc_column
|
2017-04-27 15:43:57 +01:00
|
|
|
|
|
|
|
|
|
2017-07-18 18:21:35 +01:00
|
|
|
@statsd(namespace="dao")
|
2017-08-10 17:06:31 +01:00
|
|
|
def get_billing_data_for_month(service_id, start_date, end_date, notification_type):
|
|
|
|
|
results = []
|
2017-08-02 15:24:14 +01:00
|
|
|
|
2017-08-10 17:06:31 +01:00
|
|
|
if notification_type == EMAIL_TYPE:
|
|
|
|
|
return billing_data_per_month_query(0, service_id, start_date, end_date, EMAIL_TYPE)
|
2017-08-02 15:24:14 +01:00
|
|
|
|
2017-08-10 17:06:31 +01:00
|
|
|
elif notification_type == SMS_TYPE:
|
|
|
|
|
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
|
|
|
|
|
|
|
|
|
|
if not rates:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
# so the start end date in the query are the valid from the rate, not the month
|
|
|
|
|
# - this is going to take some thought
|
|
|
|
|
for r, n in zip(rates, rates[1:]):
|
|
|
|
|
results.extend(
|
|
|
|
|
billing_data_per_month_query(
|
|
|
|
|
r.rate, service_id, max(r.valid_from, start_date),
|
|
|
|
|
min(n.valid_from, end_date), SMS_TYPE
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
results.extend(
|
|
|
|
|
billing_data_per_month_query(
|
|
|
|
|
rates[-1].rate, service_id, max(rates[-1].valid_from, start_date),
|
|
|
|
|
end_date, SMS_TYPE
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-12-14 17:17:05 +00:00
|
|
|
elif notification_type == LETTER_TYPE:
|
|
|
|
|
results.extend(billing_letter_data_per_month_query(service_id, start_date, end_date))
|
2017-07-18 18:21:35 +01:00
|
|
|
|
2017-08-10 17:06:31 +01:00
|
|
|
return results
|
2017-07-18 18:21:35 +01:00
|
|
|
|
|
|
|
|
|
2017-04-27 15:43:57 +01:00
|
|
|
@statsd(namespace="dao")
|
|
|
|
|
def get_monthly_billing_data(service_id, year):
|
|
|
|
|
start_date, end_date = get_financial_year(year)
|
2017-07-25 14:26:42 +01:00
|
|
|
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
|
2017-04-27 15:43:57 +01:00
|
|
|
|
2017-08-02 15:24:14 +01:00
|
|
|
if not rates:
|
|
|
|
|
return []
|
|
|
|
|
|
2017-04-27 15:43:57 +01:00
|
|
|
result = []
|
|
|
|
|
for r, n in zip(rates, rates[1:]):
|
2017-08-10 17:06:31 +01:00
|
|
|
result.extend(billing_data_per_month_query(r.rate, service_id, r.valid_from, n.valid_from, SMS_TYPE))
|
|
|
|
|
result.extend(billing_data_per_month_query(rates[-1].rate, service_id, rates[-1].valid_from, end_date, SMS_TYPE))
|
2017-04-27 15:43:57 +01:00
|
|
|
|
|
|
|
|
return [(datetime.strftime(x[0], "%B"), x[1], x[2], x[3], x[4], x[5]) for x in result]
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def billing_data_filter(notification_type, start_date, end_date, service_id):
|
2017-04-26 15:31:25 +01:00
|
|
|
return [
|
|
|
|
|
NotificationHistory.notification_type == notification_type,
|
2017-05-02 10:00:47 +01:00
|
|
|
NotificationHistory.created_at.between(start_date, end_date),
|
2017-04-26 15:31:25 +01:00
|
|
|
NotificationHistory.service_id == service_id,
|
|
|
|
|
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
|
|
|
|
|
NotificationHistory.key_type != KEY_TYPE_TEST
|
|
|
|
|
]
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
|
2017-07-25 14:26:42 +01:00
|
|
|
def get_rates_for_daterange(start_date, end_date, notification_type):
|
2017-04-28 16:55:41 +01:00
|
|
|
rates = Rate.query.filter(Rate.notification_type == notification_type).order_by(Rate.valid_from).all()
|
2017-08-10 17:06:31 +01:00
|
|
|
|
|
|
|
|
if not rates:
|
|
|
|
|
return []
|
|
|
|
|
|
2017-04-28 16:55:41 +01:00
|
|
|
results = []
|
|
|
|
|
for current_rate, current_rate_expiry_date in zip(rates, rates[1:]):
|
2017-05-02 10:00:47 +01:00
|
|
|
if is_between(current_rate.valid_from, start_date, end_date) or \
|
2017-05-03 17:11:48 +01:00
|
|
|
is_between(current_rate_expiry_date.valid_from - timedelta(microseconds=1), start_date, end_date):
|
2017-04-28 16:55:41 +01:00
|
|
|
results.append(current_rate)
|
|
|
|
|
|
2017-05-02 10:00:47 +01:00
|
|
|
if is_between(rates[-1].valid_from, start_date, end_date):
|
2017-04-28 16:55:41 +01:00
|
|
|
results.append(rates[-1])
|
|
|
|
|
|
|
|
|
|
if not results:
|
2017-07-25 15:50:14 +01:00
|
|
|
for x in reversed(rates):
|
|
|
|
|
if start_date >= x.valid_from:
|
|
|
|
|
results.append(x)
|
|
|
|
|
break
|
2017-04-28 16:55:41 +01:00
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
2017-05-02 10:00:47 +01:00
|
|
|
def is_between(date, start_date, end_date):
|
|
|
|
|
return start_date <= date <= end_date
|
2017-04-26 15:31:25 +01:00
|
|
|
|
|
|
|
|
|
2017-09-22 12:22:40 +01:00
|
|
|
@statsd(namespace="dao")
|
2017-08-10 17:06:31 +01:00
|
|
|
def billing_data_per_month_query(rate, service_id, start_date, end_date, notification_type):
|
2017-04-26 15:31:25 +01:00
|
|
|
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
2017-08-11 16:53:12 +01:00
|
|
|
if notification_type == SMS_TYPE:
|
|
|
|
|
filter_subq = func.sum(NotificationHistory.billable_units).label('billing_units')
|
|
|
|
|
elif notification_type == EMAIL_TYPE:
|
|
|
|
|
filter_subq = func.count(NotificationHistory.billable_units).label('billing_units')
|
|
|
|
|
|
|
|
|
|
results = db.session.query(
|
2017-07-25 11:43:41 +01:00
|
|
|
month.label('month'),
|
2017-08-11 16:53:12 +01:00
|
|
|
filter_subq,
|
2017-07-25 11:43:41 +01:00
|
|
|
rate_multiplier().label('rate_multiplier'),
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.international,
|
2017-04-27 15:43:57 +01:00
|
|
|
NotificationHistory.notification_type,
|
2017-07-25 11:43:41 +01:00
|
|
|
cast(rate, Float()).label('rate')
|
2017-04-26 15:31:25 +01:00
|
|
|
).filter(
|
2017-08-10 17:06:31 +01:00
|
|
|
*billing_data_filter(notification_type, start_date, end_date, service_id)
|
2017-04-26 15:31:25 +01:00
|
|
|
).group_by(
|
|
|
|
|
NotificationHistory.notification_type,
|
2017-04-27 15:43:57 +01:00
|
|
|
month,
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.rate_multiplier,
|
2017-04-27 15:43:57 +01:00
|
|
|
NotificationHistory.international
|
2017-04-26 15:31:25 +01:00
|
|
|
).order_by(
|
2017-04-27 15:43:57 +01:00
|
|
|
month,
|
|
|
|
|
rate_multiplier()
|
2017-12-15 17:29:32 +00:00
|
|
|
)
|
|
|
|
|
return results.all()
|
2017-04-26 15:31:25 +01:00
|
|
|
|
|
|
|
|
|
2017-04-27 15:43:57 +01:00
|
|
|
def rate_multiplier():
|
|
|
|
|
return cast(case([
|
|
|
|
|
(NotificationHistory.rate_multiplier == None, literal_column("'1'")), # noqa
|
|
|
|
|
(NotificationHistory.rate_multiplier != None, NotificationHistory.rate_multiplier), # noqa
|
|
|
|
|
]), Integer())
|
2017-12-11 16:55:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@statsd(namespace="dao")
|
|
|
|
|
def billing_letter_data_per_month_query(service_id, start_date, end_date):
|
|
|
|
|
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
|
|
|
|
crown = Service.query.get(service_id).crown
|
|
|
|
|
results = db.session.query(
|
|
|
|
|
month.label('month'),
|
2017-12-15 17:29:32 +00:00
|
|
|
func.count(NotificationHistory.billable_units).label('billing_units'),
|
2017-12-11 16:55:23 +00:00
|
|
|
rate_multiplier().label('rate_multiplier'),
|
|
|
|
|
NotificationHistory.international,
|
|
|
|
|
NotificationHistory.notification_type,
|
|
|
|
|
cast(LetterRate.rate, Float()).label('rate')
|
2017-12-18 16:46:59 +00:00
|
|
|
).join(
|
|
|
|
|
LetterRate,
|
|
|
|
|
and_(NotificationHistory.created_at >= LetterRate.start_date,
|
|
|
|
|
(LetterRate.end_date == None) | # noqa
|
|
|
|
|
(LetterRate.end_date > NotificationHistory.created_at))
|
2017-12-11 16:55:23 +00:00
|
|
|
).filter(
|
|
|
|
|
LetterRate.sheet_count == NotificationHistory.billable_units,
|
|
|
|
|
LetterRate.crown == crown,
|
2017-12-18 16:46:59 +00:00
|
|
|
LetterRate.post_class == 'second',
|
2017-12-19 13:50:21 +00:00
|
|
|
NotificationHistory.created_at < end_date,
|
|
|
|
|
*billing_data_filter(LETTER_TYPE, start_date, end_date, service_id)
|
2017-12-11 16:55:23 +00:00
|
|
|
).group_by(
|
|
|
|
|
NotificationHistory.notification_type,
|
|
|
|
|
month,
|
|
|
|
|
NotificationHistory.rate_multiplier,
|
2017-12-14 17:17:05 +00:00
|
|
|
NotificationHistory.international,
|
|
|
|
|
LetterRate.rate
|
2017-12-11 16:55:23 +00:00
|
|
|
).order_by(
|
|
|
|
|
month,
|
|
|
|
|
rate_multiplier()
|
2017-12-15 17:29:32 +00:00
|
|
|
)
|
|
|
|
|
return results.all()
|