2018-04-25 14:24:47 +01:00
|
|
|
from datetime import datetime, timedelta, time
|
|
|
|
|
|
2018-05-16 12:21:59 +01:00
|
|
|
from flask import current_app
|
2018-05-15 11:21:10 +01:00
|
|
|
from sqlalchemy.dialects.postgresql import insert
|
2018-05-21 10:56:16 +01:00
|
|
|
from sqlalchemy import func, case, desc, Date, Integer
|
2018-04-06 11:55:49 +01:00
|
|
|
|
|
|
|
|
from app import db
|
2018-04-25 14:24:47 +01:00
|
|
|
from app.dao.date_util import get_financial_year
|
2018-04-24 17:37:04 +01:00
|
|
|
from app.models import (
|
2018-04-25 14:24:47 +01:00
|
|
|
FactBilling,
|
|
|
|
|
Notification,
|
|
|
|
|
Service,
|
2018-04-24 17:37:04 +01:00
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
LETTER_TYPE,
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
Rate,
|
2018-05-14 16:21:16 +01:00
|
|
|
LetterRate,
|
2018-05-21 11:06:31 +01:00
|
|
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
2018-05-21 10:56:16 +01:00
|
|
|
NotificationHistory,
|
2018-05-16 12:21:59 +01:00
|
|
|
EMAIL_TYPE
|
2018-04-24 17:37:04 +01:00
|
|
|
)
|
2018-04-25 14:24:47 +01:00
|
|
|
from app.utils import convert_utc_to_bst, convert_bst_to_utc
|
2018-04-09 11:38:00 +01:00
|
|
|
|
|
|
|
|
|
2018-05-11 16:25:16 +01:00
|
|
|
def fetch_billing_totals_for_year(service_id, year):
|
|
|
|
|
year_start_date, year_end_date = get_financial_year(year)
|
2018-05-16 12:21:59 +01:00
|
|
|
"""
|
|
|
|
|
Billing for email: only record the total number of emails.
|
2018-05-16 13:18:36 +01:00
|
|
|
Billing for letters: The billing units is used to fetch the correct rate for the sheet count of the letter.
|
|
|
|
|
Total cost is notifications_sent * rate.
|
2018-05-16 12:21:59 +01:00
|
|
|
Rate multiplier does not apply to email or letters.
|
|
|
|
|
"""
|
|
|
|
|
email_and_letters = db.session.query(
|
|
|
|
|
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
|
|
|
|
func.sum(FactBilling.notifications_sent).label("billable_units"),
|
|
|
|
|
FactBilling.rate.label('rate'),
|
|
|
|
|
FactBilling.notification_type.label('notification_type')
|
|
|
|
|
).filter(
|
|
|
|
|
FactBilling.service_id == service_id,
|
|
|
|
|
FactBilling.bst_date >= year_start_date,
|
|
|
|
|
FactBilling.bst_date <= year_end_date,
|
|
|
|
|
FactBilling.notification_type.in_([EMAIL_TYPE, LETTER_TYPE])
|
|
|
|
|
).group_by(
|
|
|
|
|
FactBilling.rate,
|
|
|
|
|
FactBilling.notification_type
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
Billing for SMS using the billing_units * rate_multiplier. Billing unit of SMS is the fragment count of a message
|
|
|
|
|
"""
|
|
|
|
|
sms = db.session.query(
|
2018-05-11 16:25:16 +01:00
|
|
|
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
|
|
|
|
func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"),
|
|
|
|
|
FactBilling.rate,
|
|
|
|
|
FactBilling.notification_type
|
|
|
|
|
).filter(
|
|
|
|
|
FactBilling.service_id == service_id,
|
|
|
|
|
FactBilling.bst_date >= year_start_date,
|
2018-05-16 12:21:59 +01:00
|
|
|
FactBilling.bst_date <= year_end_date,
|
|
|
|
|
FactBilling.notification_type == SMS_TYPE
|
2018-05-11 16:25:16 +01:00
|
|
|
).group_by(
|
|
|
|
|
FactBilling.rate,
|
|
|
|
|
FactBilling.notification_type
|
2018-05-16 12:21:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
yearly_data = email_and_letters.union_all(sms).order_by(
|
|
|
|
|
'notification_type',
|
|
|
|
|
'rate'
|
2018-05-11 16:25:16 +01:00
|
|
|
).all()
|
|
|
|
|
|
|
|
|
|
return yearly_data
|
|
|
|
|
|
|
|
|
|
|
2018-04-27 15:15:55 +01:00
|
|
|
def fetch_monthly_billing_for_year(service_id, year):
|
2018-04-09 11:38:00 +01:00
|
|
|
year_start_date, year_end_date = get_financial_year(year)
|
|
|
|
|
utcnow = datetime.utcnow()
|
2018-05-04 13:09:14 +01:00
|
|
|
today = convert_utc_to_bst(utcnow)
|
2018-04-09 11:38:00 +01:00
|
|
|
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
|
2018-05-04 13:09:14 +01:00
|
|
|
if year_end_date >= today:
|
2018-04-25 14:24:47 +01:00
|
|
|
yesterday = today - timedelta(days=1)
|
|
|
|
|
for day in [yesterday, today]:
|
|
|
|
|
data = fetch_billing_data_for_day(process_day=day, service_id=service_id)
|
|
|
|
|
for d in data:
|
|
|
|
|
update_fact_billing(data=d, process_day=day)
|
|
|
|
|
|
2018-05-16 12:21:59 +01:00
|
|
|
email_and_letters = db.session.query(
|
|
|
|
|
func.date_trunc('month', FactBilling.bst_date).cast(Date).label("month"),
|
|
|
|
|
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
|
|
|
|
func.sum(FactBilling.notifications_sent).label("billable_units"),
|
|
|
|
|
FactBilling.rate.label('rate'),
|
|
|
|
|
FactBilling.notification_type.label('notification_type')
|
|
|
|
|
).filter(
|
|
|
|
|
FactBilling.service_id == service_id,
|
|
|
|
|
FactBilling.bst_date >= year_start_date,
|
|
|
|
|
FactBilling.bst_date <= year_end_date,
|
|
|
|
|
FactBilling.notification_type.in_([EMAIL_TYPE, LETTER_TYPE])
|
|
|
|
|
).group_by(
|
|
|
|
|
'month',
|
|
|
|
|
FactBilling.rate,
|
|
|
|
|
FactBilling.notification_type
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sms = db.session.query(
|
2018-05-08 13:53:44 +01:00
|
|
|
func.date_trunc('month', FactBilling.bst_date).cast(Date).label("month"),
|
2018-04-24 17:37:04 +01:00
|
|
|
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
2018-05-04 13:09:14 +01:00
|
|
|
func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"),
|
2018-04-09 11:38:00 +01:00
|
|
|
FactBilling.rate,
|
2018-04-27 15:15:55 +01:00
|
|
|
FactBilling.notification_type
|
2018-04-09 11:38:00 +01:00
|
|
|
).filter(
|
|
|
|
|
FactBilling.service_id == service_id,
|
|
|
|
|
FactBilling.bst_date >= year_start_date,
|
2018-05-16 12:21:59 +01:00
|
|
|
FactBilling.bst_date <= year_end_date,
|
|
|
|
|
FactBilling.notification_type == SMS_TYPE
|
2018-04-24 17:37:04 +01:00
|
|
|
).group_by(
|
2018-04-30 16:34:40 +01:00
|
|
|
'month',
|
2018-04-24 17:37:04 +01:00
|
|
|
FactBilling.rate,
|
2018-04-27 15:15:55 +01:00
|
|
|
FactBilling.notification_type
|
2018-05-16 12:21:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
yearly_data = email_and_letters.union_all(sms).order_by(
|
2018-04-30 16:34:40 +01:00
|
|
|
'month',
|
2018-05-16 12:21:59 +01:00
|
|
|
'notification_type',
|
|
|
|
|
'rate'
|
2018-04-09 11:38:00 +01:00
|
|
|
).all()
|
|
|
|
|
|
2018-04-24 17:37:04 +01:00
|
|
|
return yearly_data
|
2018-07-26 18:41:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_billing_data_for_service_for_day(process_day, service_id):
|
|
|
|
|
"""
|
|
|
|
|
Delete all ft_billing data for a given service on a given bst_date
|
|
|
|
|
|
|
|
|
|
Returns how many rows were deleted
|
|
|
|
|
"""
|
|
|
|
|
return FactBilling.query.filter(
|
|
|
|
|
FactBilling.bst_date == process_day,
|
|
|
|
|
FactBilling.service_id == service_id
|
|
|
|
|
).delete()
|
2018-04-24 17:37:04 +01:00
|
|
|
|
|
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
def fetch_billing_data_for_day(process_day, service_id=None):
|
|
|
|
|
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
|
|
|
|
|
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
|
2018-05-14 16:21:16 +01:00
|
|
|
# use notification_history if process day is older than 7 days
|
|
|
|
|
# this is useful if we need to rebuild the ft_billing table for a date older than 7 days ago.
|
2018-05-16 12:21:59 +01:00
|
|
|
current_app.logger.info("Populate ft_billing for {} to {}".format(start_date, end_date))
|
2018-05-14 16:21:16 +01:00
|
|
|
table = Notification
|
|
|
|
|
if start_date < datetime.utcnow() - timedelta(days=7):
|
|
|
|
|
table = NotificationHistory
|
2018-04-25 14:24:47 +01:00
|
|
|
|
2018-04-24 17:37:04 +01:00
|
|
|
transit_data = db.session.query(
|
2018-05-14 16:21:16 +01:00
|
|
|
table.template_id,
|
|
|
|
|
table.service_id,
|
|
|
|
|
table.notification_type,
|
|
|
|
|
func.coalesce(table.sent_by,
|
2018-04-24 17:37:04 +01:00
|
|
|
case(
|
|
|
|
|
[
|
2018-05-14 16:21:16 +01:00
|
|
|
(table.notification_type == 'letter', 'dvla'),
|
|
|
|
|
(table.notification_type == 'sms', 'unknown'),
|
|
|
|
|
(table.notification_type == 'email', 'ses')
|
2018-04-24 17:37:04 +01:00
|
|
|
]),
|
|
|
|
|
).label('sent_by'),
|
2018-05-21 10:56:16 +01:00
|
|
|
func.coalesce(table.rate_multiplier, 1).cast(Integer).label('rate_multiplier'),
|
2018-05-14 16:21:16 +01:00
|
|
|
func.coalesce(table.international, False).label('international'),
|
2018-05-22 17:35:21 +01:00
|
|
|
case(
|
|
|
|
|
[
|
|
|
|
|
(table.notification_type == 'letter', table.billable_units),
|
|
|
|
|
]
|
|
|
|
|
).label('letter_page_count'),
|
2018-05-14 16:21:16 +01:00
|
|
|
func.sum(table.billable_units).label('billable_units'),
|
2018-04-24 17:37:04 +01:00
|
|
|
func.count().label('notifications_sent'),
|
|
|
|
|
Service.crown,
|
|
|
|
|
).filter(
|
2018-05-21 10:56:16 +01:00
|
|
|
table.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
|
2018-05-14 16:21:16 +01:00
|
|
|
table.key_type != KEY_TYPE_TEST,
|
|
|
|
|
table.created_at >= start_date,
|
|
|
|
|
table.created_at < end_date
|
2018-04-24 17:37:04 +01:00
|
|
|
).group_by(
|
2018-05-14 16:21:16 +01:00
|
|
|
table.template_id,
|
|
|
|
|
table.service_id,
|
|
|
|
|
table.notification_type,
|
2018-04-24 17:37:04 +01:00
|
|
|
'sent_by',
|
2018-05-22 17:35:21 +01:00
|
|
|
'letter_page_count',
|
2018-05-14 16:21:16 +01:00
|
|
|
table.rate_multiplier,
|
|
|
|
|
table.international,
|
2018-04-24 17:37:04 +01:00
|
|
|
Service.crown
|
|
|
|
|
).join(
|
|
|
|
|
Service
|
|
|
|
|
)
|
|
|
|
|
if service_id:
|
2018-05-14 16:21:16 +01:00
|
|
|
transit_data = transit_data.filter(table.service_id == service_id)
|
2018-05-10 15:35:58 +01:00
|
|
|
|
2018-04-24 17:37:04 +01:00
|
|
|
return transit_data.all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_rates_for_billing():
|
|
|
|
|
non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in
|
|
|
|
|
Rate.query.order_by(desc(Rate.valid_from)).all()]
|
2018-09-14 17:52:16 +01:00
|
|
|
letter_rates = [(r.start_date, r.crown, r.sheet_count, r.rate, r.post_class) for r in
|
2018-04-24 17:37:04 +01:00
|
|
|
LetterRate.query.order_by(desc(LetterRate.start_date)).all()]
|
|
|
|
|
return non_letter_rates, letter_rates
|
|
|
|
|
|
|
|
|
|
|
2018-07-23 15:14:37 +01:00
|
|
|
def get_service_ids_that_need_billing_populated(start_date, end_date):
|
|
|
|
|
return db.session.query(
|
|
|
|
|
NotificationHistory.service_id
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.created_at >= start_date,
|
|
|
|
|
NotificationHistory.created_at <= end_date,
|
|
|
|
|
NotificationHistory.notification_type.in_([SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]),
|
|
|
|
|
NotificationHistory.billable_units != 0
|
|
|
|
|
).distinct().all()
|
|
|
|
|
|
|
|
|
|
|
2018-09-14 17:52:16 +01:00
|
|
|
def get_rate(
|
|
|
|
|
non_letter_rates, letter_rates, notification_type, date, crown=None, letter_page_count=None, post_class='second'
|
|
|
|
|
):
|
2018-04-24 17:37:04 +01:00
|
|
|
if notification_type == LETTER_TYPE:
|
2018-07-31 11:04:48 +01:00
|
|
|
if letter_page_count == 0:
|
|
|
|
|
return 0
|
2018-09-14 17:52:16 +01:00
|
|
|
return next(
|
2018-09-24 14:52:06 +01:00
|
|
|
r[3] for r in letter_rates if date >= r[0] and crown == r[1]
|
2018-09-14 17:52:16 +01:00
|
|
|
and letter_page_count == r[2] and post_class == r[4]
|
|
|
|
|
)
|
2018-04-24 17:37:04 +01:00
|
|
|
elif notification_type == SMS_TYPE:
|
2018-09-24 14:52:06 +01:00
|
|
|
return next(r[2] for r in non_letter_rates if notification_type == r[0] and date >= r[1])
|
2018-04-24 17:37:04 +01:00
|
|
|
else:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
def update_fact_billing(data, process_day):
|
2018-04-24 17:37:04 +01:00
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
2018-05-15 11:21:10 +01:00
|
|
|
rate = get_rate(non_letter_rates,
|
|
|
|
|
letter_rates,
|
|
|
|
|
data.notification_type,
|
|
|
|
|
process_day,
|
|
|
|
|
data.crown,
|
2018-09-14 17:52:16 +01:00
|
|
|
data.letter_page_count,
|
|
|
|
|
"second")
|
2018-05-15 11:21:10 +01:00
|
|
|
billing_record = create_billing_record(data, rate, process_day)
|
|
|
|
|
table = FactBilling.__table__
|
|
|
|
|
'''
|
|
|
|
|
This uses the Postgres upsert to avoid race conditions when two threads try to insert
|
|
|
|
|
at the same row. The excluded object refers to values that we tried to insert but were
|
|
|
|
|
rejected.
|
|
|
|
|
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert
|
|
|
|
|
'''
|
|
|
|
|
stmt = insert(table).values(
|
|
|
|
|
bst_date=billing_record.bst_date,
|
|
|
|
|
template_id=billing_record.template_id,
|
|
|
|
|
service_id=billing_record.service_id,
|
|
|
|
|
provider=billing_record.provider,
|
|
|
|
|
rate_multiplier=billing_record.rate_multiplier,
|
|
|
|
|
notification_type=billing_record.notification_type,
|
|
|
|
|
international=billing_record.international,
|
|
|
|
|
billable_units=billing_record.billable_units,
|
|
|
|
|
notifications_sent=billing_record.notifications_sent,
|
|
|
|
|
rate=billing_record.rate
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
stmt = stmt.on_conflict_do_update(
|
2018-05-21 14:38:25 +01:00
|
|
|
constraint="ft_billing_pkey",
|
2018-05-15 11:21:10 +01:00
|
|
|
set_={"notifications_sent": stmt.excluded.notifications_sent,
|
2018-05-22 14:49:48 +01:00
|
|
|
"billable_units": stmt.excluded.billable_units,
|
|
|
|
|
"updated_at": datetime.utcnow()
|
2018-05-15 11:21:10 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
db.session.connection().execute(stmt)
|
2018-04-24 17:37:04 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_billing_record(data, rate, process_day):
|
|
|
|
|
billing_record = FactBilling(
|
2018-05-21 11:51:06 +01:00
|
|
|
bst_date=process_day.date(),
|
2018-04-24 17:37:04 +01:00
|
|
|
template_id=data.template_id,
|
|
|
|
|
service_id=data.service_id,
|
|
|
|
|
notification_type=data.notification_type,
|
|
|
|
|
provider=data.sent_by,
|
|
|
|
|
rate_multiplier=data.rate_multiplier,
|
|
|
|
|
international=data.international,
|
|
|
|
|
billable_units=data.billable_units,
|
|
|
|
|
notifications_sent=data.notifications_sent,
|
|
|
|
|
rate=rate
|
|
|
|
|
)
|
|
|
|
|
return billing_record
|