2017-08-10 17:06:31 +01:00
|
|
|
from collections import namedtuple
|
2017-05-03 17:11:48 +01:00
|
|
|
from datetime import datetime, timedelta
|
2017-04-27 15:43:57 +01:00
|
|
|
|
|
|
|
|
from sqlalchemy import Float, Integer
|
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,
|
|
|
|
|
Service,
|
|
|
|
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
|
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
EMAIL_TYPE
|
|
|
|
|
)
|
2017-04-26 14:16:47 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2017-08-10 17:06:31 +01:00
|
|
|
from app.utils import get_london_month_from_utc_column, convert_utc_to_bst
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@statsd(namespace="dao")
|
|
|
|
|
def get_yearly_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-26 14:16:47 +01:00
|
|
|
|
2017-08-02 15:24:14 +01:00
|
|
|
if not rates:
|
|
|
|
|
return []
|
|
|
|
|
|
2017-05-02 19:23:57 +01:00
|
|
|
def get_valid_from(valid_from):
|
|
|
|
|
return start_date if valid_from < start_date else valid_from
|
|
|
|
|
|
2017-04-26 14:16:47 +01:00
|
|
|
result = []
|
|
|
|
|
for r, n in zip(rates, rates[1:]):
|
2017-08-02 15:24:14 +01:00
|
|
|
result.append(
|
|
|
|
|
sms_yearly_billing_data_query(
|
|
|
|
|
r.rate,
|
|
|
|
|
service_id,
|
|
|
|
|
get_valid_from(r.valid_from),
|
|
|
|
|
n.valid_from
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-05-02 19:23:57 +01:00
|
|
|
result.append(
|
2017-08-02 15:24:14 +01:00
|
|
|
sms_yearly_billing_data_query(
|
|
|
|
|
rates[-1].rate,
|
|
|
|
|
service_id,
|
|
|
|
|
get_valid_from(rates[-1].valid_from),
|
|
|
|
|
end_date
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2017-04-27 15:43:57 +01:00
|
|
|
result.append(email_yearly_billing_data_query(service_id, start_date, end_date))
|
|
|
|
|
return sum(result, [])
|
|
|
|
|
|
|
|
|
|
|
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-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-04-27 15:43:57 +01:00
|
|
|
def email_yearly_billing_data_query(service_id, start_date, end_date, rate=0):
|
2017-04-26 15:31:25 +01:00
|
|
|
result = db.session.query(
|
|
|
|
|
func.count(NotificationHistory.id),
|
2017-04-27 15:43:57 +01:00
|
|
|
func.count(NotificationHistory.id),
|
|
|
|
|
rate_multiplier(),
|
2017-04-26 15:31:25 +01:00
|
|
|
NotificationHistory.notification_type,
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.international,
|
2017-04-27 15:43:57 +01:00
|
|
|
cast(rate, Integer())
|
2017-04-26 15:31:25 +01:00
|
|
|
).filter(
|
|
|
|
|
*billing_data_filter(EMAIL_TYPE, start_date, end_date, service_id)
|
|
|
|
|
).group_by(
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.notification_type,
|
2017-04-27 15:43:57 +01:00
|
|
|
rate_multiplier(),
|
|
|
|
|
NotificationHistory.international
|
2017-04-26 15:31:25 +01:00
|
|
|
).first()
|
2017-08-11 16:53:12 +01:00
|
|
|
|
2017-04-27 10:16:29 +01:00
|
|
|
if not result:
|
2017-04-27 15:43:57 +01:00
|
|
|
return [(0, 0, 1, EMAIL_TYPE, False, 0)]
|
2017-04-27 10:16:29 +01:00
|
|
|
else:
|
2017-04-27 15:43:57 +01:00
|
|
|
return [result]
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
|
2017-04-27 15:43:57 +01:00
|
|
|
def sms_yearly_billing_data_query(rate, service_id, start_date, end_date):
|
2017-04-26 15:31:25 +01:00
|
|
|
result = db.session.query(
|
2017-04-27 15:43:57 +01:00
|
|
|
cast(func.sum(NotificationHistory.billable_units * rate_multiplier()), Integer()),
|
2017-04-26 15:31:25 +01:00
|
|
|
func.sum(NotificationHistory.billable_units),
|
2017-04-27 15:43:57 +01:00
|
|
|
rate_multiplier(),
|
2017-04-26 15:31:25 +01:00
|
|
|
NotificationHistory.notification_type,
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.international,
|
2017-04-27 15:43:57 +01:00
|
|
|
cast(rate, Float())
|
2017-04-26 15:31:25 +01:00
|
|
|
).filter(
|
|
|
|
|
*billing_data_filter(SMS_TYPE, start_date, end_date, service_id)
|
|
|
|
|
).group_by(
|
2017-04-26 15:57:11 +01:00
|
|
|
NotificationHistory.notification_type,
|
|
|
|
|
NotificationHistory.international,
|
2017-04-27 15:43:57 +01:00
|
|
|
rate_multiplier()
|
|
|
|
|
).order_by(
|
|
|
|
|
rate_multiplier()
|
|
|
|
|
).all()
|
|
|
|
|
|
2017-04-26 14:16:47 +01:00
|
|
|
if not result:
|
2017-04-27 15:43:57 +01:00
|
|
|
return [(0, 0, 1, SMS_TYPE, False, rate)]
|
2017-04-26 14:16:47 +01:00
|
|
|
else:
|
2017-04-27 15:43:57 +01:00
|
|
|
return result
|
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-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-04-26 15:31:25 +01:00
|
|
|
).all()
|
|
|
|
|
|
2017-08-11 16:53:12 +01:00
|
|
|
return results
|
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())
|