2017-07-18 18:21:35 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2017-08-11 16:53:12 +01:00
|
|
|
|
2017-07-13 17:22:11 +01:00
|
|
|
from app import db
|
2017-07-25 14:26:42 +01:00
|
|
|
from app.dao.dao_utils import transactional
|
2017-08-10 17:01:48 +01:00
|
|
|
from app.dao.date_util import get_month_start_and_end_date_in_utc, get_financial_year
|
2017-07-18 18:21:35 +01:00
|
|
|
from app.dao.notification_usage_dao import get_billing_data_for_month
|
2017-08-10 17:01:48 +01:00
|
|
|
from app.models import (
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
EMAIL_TYPE,
|
|
|
|
|
MonthlyBilling,
|
2017-12-14 17:17:05 +00:00
|
|
|
NotificationHistory,
|
|
|
|
|
LETTER_TYPE
|
2017-08-10 17:01:48 +01:00
|
|
|
)
|
2017-07-25 14:26:42 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2017-08-10 17:01:48 +01:00
|
|
|
from app.utils import convert_utc_to_bst
|
2017-07-24 15:13:18 +01:00
|
|
|
|
|
|
|
|
|
2017-08-10 17:01:48 +01:00
|
|
|
def get_service_ids_that_need_billing_populated(start_date, end_date):
|
2017-07-24 15:13:18 +01:00
|
|
|
return db.session.query(
|
|
|
|
|
NotificationHistory.service_id
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.created_at >= start_date,
|
|
|
|
|
NotificationHistory.created_at <= end_date,
|
2017-08-10 17:01:48 +01:00
|
|
|
NotificationHistory.notification_type.in_([SMS_TYPE, EMAIL_TYPE]),
|
2017-07-24 15:13:18 +01:00
|
|
|
NotificationHistory.billable_units != 0
|
|
|
|
|
).distinct().all()
|
2017-07-13 17:22:11 +01:00
|
|
|
|
|
|
|
|
|
2017-09-22 12:22:40 +01:00
|
|
|
@statsd(namespace="dao")
|
2017-08-10 17:01:48 +01:00
|
|
|
def create_or_update_monthly_billing(service_id, billing_month):
|
|
|
|
|
start_date, end_date = get_month_start_and_end_date_in_utc(billing_month)
|
2017-12-15 17:29:32 +00:00
|
|
|
# _update_monthly_billing(service_id, start_date, end_date, SMS_TYPE)
|
|
|
|
|
# _update_monthly_billing(service_id, start_date, end_date, EMAIL_TYPE)
|
2017-12-14 17:17:05 +00:00
|
|
|
_update_monthly_billing(service_id, start_date, end_date, LETTER_TYPE)
|
2017-08-10 17:01:48 +01:00
|
|
|
|
|
|
|
|
|
2017-08-11 16:53:12 +01:00
|
|
|
def _monthly_billing_data_to_json(billing_data):
|
|
|
|
|
results = []
|
|
|
|
|
if billing_data:
|
|
|
|
|
# total cost must take into account the free allowance.
|
|
|
|
|
# might be a good idea to capture free allowance in this table
|
|
|
|
|
results = [{
|
|
|
|
|
"billing_units": x.billing_units,
|
|
|
|
|
"rate_multiplier": x.rate_multiplier,
|
|
|
|
|
"international": x.international,
|
|
|
|
|
"rate": x.rate,
|
|
|
|
|
"total_cost": (x.billing_units * x.rate_multiplier) * x.rate
|
|
|
|
|
} for x in billing_data]
|
|
|
|
|
return results
|
2017-07-31 17:47:53 +01:00
|
|
|
|
2017-08-10 17:01:48 +01:00
|
|
|
|
2017-09-22 12:22:40 +01:00
|
|
|
@statsd(namespace="dao")
|
2017-08-10 17:01:48 +01:00
|
|
|
@transactional
|
|
|
|
|
def _update_monthly_billing(service_id, start_date, end_date, notification_type):
|
|
|
|
|
billing_data = get_billing_data_for_month(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
start_date=start_date,
|
|
|
|
|
end_date=end_date,
|
|
|
|
|
notification_type=notification_type
|
|
|
|
|
)
|
|
|
|
|
monthly_totals = _monthly_billing_data_to_json(billing_data)
|
|
|
|
|
row = get_monthly_billing_entry(service_id, start_date, notification_type)
|
2017-07-18 18:21:35 +01:00
|
|
|
if row:
|
|
|
|
|
row.monthly_totals = monthly_totals
|
2017-07-26 13:19:17 +01:00
|
|
|
row.updated_at = datetime.utcnow()
|
2017-07-18 18:21:35 +01:00
|
|
|
else:
|
2017-07-31 17:47:53 +01:00
|
|
|
row = MonthlyBilling(
|
|
|
|
|
service_id=service_id,
|
2017-08-10 17:01:48 +01:00
|
|
|
notification_type=notification_type,
|
2017-07-31 17:47:53 +01:00
|
|
|
monthly_totals=monthly_totals,
|
|
|
|
|
start_date=start_date,
|
|
|
|
|
end_date=end_date
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-18 18:21:35 +01:00
|
|
|
db.session.add(row)
|
|
|
|
|
|
|
|
|
|
|
2017-07-31 17:47:53 +01:00
|
|
|
def get_monthly_billing_entry(service_id, start_date, notification_type):
|
|
|
|
|
entry = MonthlyBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
start_date=start_date,
|
|
|
|
|
notification_type=notification_type
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
|
|
2017-07-25 14:26:42 +01:00
|
|
|
@statsd(namespace="dao")
|
2017-08-10 17:09:47 +01:00
|
|
|
def get_yearly_billing_data_for_date_range(
|
|
|
|
|
service_id, start_date, end_date, notification_types
|
|
|
|
|
):
|
2017-08-11 16:53:12 +01:00
|
|
|
results = db.session.query(
|
|
|
|
|
MonthlyBilling.notification_type,
|
|
|
|
|
MonthlyBilling.monthly_totals,
|
|
|
|
|
MonthlyBilling.start_date,
|
|
|
|
|
).filter(
|
2017-08-10 17:09:47 +01:00
|
|
|
MonthlyBilling.service_id == service_id,
|
|
|
|
|
MonthlyBilling.start_date >= start_date,
|
|
|
|
|
MonthlyBilling.end_date <= end_date,
|
|
|
|
|
MonthlyBilling.notification_type.in_(notification_types)
|
|
|
|
|
).order_by(
|
|
|
|
|
MonthlyBilling.notification_type
|
|
|
|
|
).all()
|
2017-07-18 18:21:35 +01:00
|
|
|
|
2017-08-10 17:09:47 +01:00
|
|
|
return results
|
2017-07-18 18:21:35 +01:00
|
|
|
|
2017-08-10 17:09:47 +01:00
|
|
|
|
|
|
|
|
@statsd(namespace="dao")
|
|
|
|
|
def get_monthly_billing_by_notification_type(service_id, billing_month, notification_type):
|
|
|
|
|
billing_month_in_bst = convert_utc_to_bst(billing_month)
|
|
|
|
|
start_date, _ = get_month_start_and_end_date_in_utc(billing_month_in_bst)
|
|
|
|
|
return get_monthly_billing_entry(service_id, start_date, notification_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@statsd(namespace="dao")
|
2017-12-15 17:29:32 +00:00
|
|
|
def get_billing_data_for_financial_year(service_id, year, notification_types=[SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]):
|
2017-08-10 17:09:47 +01:00
|
|
|
# Update totals to the latest so we include data for today
|
|
|
|
|
now = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
create_or_update_monthly_billing(service_id=service_id, billing_month=now)
|
|
|
|
|
|
|
|
|
|
start_date, end_date = get_financial_year(year)
|
|
|
|
|
|
|
|
|
|
results = get_yearly_billing_data_for_date_range(
|
|
|
|
|
service_id, start_date, end_date, notification_types
|
|
|
|
|
)
|
|
|
|
|
return results
|