2018-05-08 12:09:29 +01:00
|
|
|
from calendar import monthrange
|
2018-04-06 11:55:49 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
|
|
2019-04-03 14:52:41 +01:00
|
|
|
from datetime import datetime, timedelta, date
|
2019-08-22 17:48:24 +01:00
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
2018-09-20 15:49:15 +01:00
|
|
|
import pytest
|
|
|
|
|
|
2018-11-26 12:53:39 +00:00
|
|
|
from notifications_utils.timezones import convert_utc_to_bst
|
|
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
from app import db
|
2018-04-25 15:40:44 +01:00
|
|
|
from app.dao.fact_billing_dao import (
|
2018-07-26 18:41:06 +01:00
|
|
|
delete_billing_data_for_service_for_day,
|
|
|
|
|
fetch_billing_data_for_day,
|
2018-05-11 16:25:16 +01:00
|
|
|
fetch_billing_totals_for_year,
|
2018-07-26 18:41:06 +01:00
|
|
|
fetch_monthly_billing_for_year,
|
|
|
|
|
get_rate,
|
|
|
|
|
get_rates_for_billing,
|
2019-08-06 13:29:59 +01:00
|
|
|
fetch_sms_free_allowance_remainder,
|
|
|
|
|
fetch_sms_billing_for_all_services,
|
|
|
|
|
fetch_letter_costs_for_all_services, fetch_letter_line_items_for_all_services)
|
|
|
|
|
from app.dao.organisation_dao import dao_add_service_to_organisation
|
2018-10-24 17:07:30 +01:00
|
|
|
from app.models import (
|
|
|
|
|
FactBilling,
|
|
|
|
|
Notification,
|
|
|
|
|
NOTIFICATION_STATUS_TYPES,
|
|
|
|
|
)
|
2018-04-24 17:37:04 +01:00
|
|
|
from tests.app.db import (
|
|
|
|
|
create_ft_billing,
|
|
|
|
|
create_service,
|
2018-04-25 14:24:47 +01:00
|
|
|
create_template,
|
2018-04-25 15:40:44 +01:00
|
|
|
create_notification,
|
|
|
|
|
create_rate,
|
2019-05-30 10:37:57 +01:00
|
|
|
create_letter_rate,
|
2019-08-06 13:29:59 +01:00
|
|
|
create_notification_history,
|
|
|
|
|
create_annual_billing,
|
2019-08-22 17:48:24 +01:00
|
|
|
create_organisation,
|
|
|
|
|
set_up_usage_data
|
2018-04-24 17:37:04 +01:00
|
|
|
)
|
2018-04-06 11:55:49 +01:00
|
|
|
|
|
|
|
|
|
2018-05-11 16:25:16 +01:00
|
|
|
def set_up_yearly_data():
|
|
|
|
|
service = create_service()
|
|
|
|
|
sms_template = create_template(service=service, template_type="sms")
|
|
|
|
|
email_template = create_template(service=service, template_type="email")
|
|
|
|
|
letter_template = create_template(service=service, template_type="letter")
|
|
|
|
|
for year in (2016, 2017):
|
|
|
|
|
for month in range(1, 13):
|
|
|
|
|
mon = str(month).zfill(2)
|
|
|
|
|
for day in range(1, monthrange(year, month)[1] + 1):
|
|
|
|
|
d = str(day).zfill(2)
|
|
|
|
|
create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d),
|
|
|
|
|
service=service,
|
|
|
|
|
template=sms_template,
|
|
|
|
|
notification_type='sms',
|
|
|
|
|
rate=0.162)
|
|
|
|
|
create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d),
|
|
|
|
|
service=service,
|
|
|
|
|
template=email_template,
|
|
|
|
|
notification_type='email',
|
|
|
|
|
rate=0)
|
|
|
|
|
create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d),
|
|
|
|
|
service=service,
|
|
|
|
|
template=letter_template,
|
|
|
|
|
notification_type='letter',
|
2018-09-26 11:28:59 +01:00
|
|
|
rate=0.33,
|
|
|
|
|
postage='second')
|
2018-05-22 17:35:21 +01:00
|
|
|
create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d),
|
|
|
|
|
service=service,
|
|
|
|
|
template=letter_template,
|
|
|
|
|
notification_type='letter',
|
2018-09-26 11:28:59 +01:00
|
|
|
rate=0.30,
|
|
|
|
|
postage='second')
|
2018-05-11 16:25:16 +01:00
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
def test_fetch_billing_data_for_today_includes_data_with_the_right_status(notify_db_session):
|
2018-04-06 11:55:49 +01:00
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service, template_type="email")
|
2018-04-25 17:00:19 +01:00
|
|
|
for status in ['created', 'technical-failure']:
|
2018-04-25 14:24:47 +01:00
|
|
|
create_notification(template=template, status=status)
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
2018-04-25 17:00:19 +01:00
|
|
|
assert results == []
|
|
|
|
|
for status in ['delivered', 'sending', 'temporary-failure']:
|
|
|
|
|
create_notification(template=template, status=status)
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
2018-04-06 11:55:49 +01:00
|
|
|
assert len(results) == 1
|
2018-04-25 14:24:47 +01:00
|
|
|
assert results[0].notifications_sent == 3
|
2018-04-06 11:55:49 +01:00
|
|
|
|
2018-04-09 11:38:00 +01:00
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service, template_type="email")
|
|
|
|
|
for key_type in ['normal', 'test', 'team']:
|
|
|
|
|
create_notification(template=template, status='delivered', key_type=key_type)
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 1
|
|
|
|
|
assert results[0].notifications_sent == 2
|
|
|
|
|
|
|
|
|
|
|
2019-05-30 10:37:57 +01:00
|
|
|
@freeze_time('2018-04-02 01:20:00')
|
2018-04-25 14:24:47 +01:00
|
|
|
def test_fetch_billing_data_for_today_includes_data_with_the_right_date(notify_db_session):
|
2019-04-03 15:34:02 +01:00
|
|
|
process_day = datetime(2018, 4, 1, 13, 30, 0)
|
2018-04-09 11:38:00 +01:00
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service, template_type="email")
|
2018-04-25 14:24:47 +01:00
|
|
|
create_notification(template=template, status='delivered', created_at=process_day)
|
|
|
|
|
create_notification(template=template, status='delivered', created_at=datetime(2018, 3, 31, 23, 23, 23))
|
|
|
|
|
|
|
|
|
|
create_notification(template=template, status='delivered', created_at=datetime(2018, 3, 31, 20, 23, 23))
|
|
|
|
|
create_notification(template=template, status='sending', created_at=process_day + timedelta(days=1))
|
|
|
|
|
|
|
|
|
|
day_under_test = convert_utc_to_bst(process_day)
|
|
|
|
|
results = fetch_billing_data_for_day(day_under_test)
|
|
|
|
|
assert len(results) == 1
|
|
|
|
|
assert results[0].notifications_sent == 2
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_template_and_notification_type(notify_db_session):
|
2018-04-25 14:24:47 +01:00
|
|
|
service = create_service()
|
|
|
|
|
email_template = create_template(service=service, template_type="email")
|
2018-04-25 15:40:44 +01:00
|
|
|
sms_template = create_template(service=service, template_type="sms")
|
|
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].notifications_sent == 1
|
|
|
|
|
assert results[1].notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_service(notify_db_session):
|
|
|
|
|
service_1 = create_service()
|
|
|
|
|
service_2 = create_service(service_name='Service 2')
|
|
|
|
|
email_template = create_template(service=service_1)
|
|
|
|
|
sms_template = create_template(service=service_2)
|
2018-04-25 14:24:47 +01:00
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].notifications_sent == 1
|
|
|
|
|
assert results[1].notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_provider(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
create_notification(template=template, status='delivered', sent_by='mmg')
|
|
|
|
|
create_notification(template=template, status='delivered', sent_by='firetext')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].notifications_sent == 1
|
|
|
|
|
assert results[1].notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_rate_mulitplier(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
create_notification(template=template, status='delivered', rate_multiplier=1)
|
|
|
|
|
create_notification(template=template, status='delivered', rate_multiplier=2)
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].notifications_sent == 1
|
|
|
|
|
assert results[1].notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_international(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
create_notification(template=template, status='delivered', international=True)
|
|
|
|
|
create_notification(template=template, status='delivered', international=False)
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].notifications_sent == 1
|
|
|
|
|
assert results[1].notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
|
2018-04-27 15:15:55 +01:00
|
|
|
def test_fetch_billing_data_for_day_is_grouped_by_notification_type(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
sms_template = create_template(service=service, template_type='sms')
|
|
|
|
|
email_template = create_template(service=service, template_type='email')
|
|
|
|
|
letter_template = create_template(service=service, template_type='letter')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
create_notification(template=letter_template, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 3
|
|
|
|
|
notification_types = [x[2] for x in results if x[2] in ['email', 'sms', 'letter']]
|
|
|
|
|
assert len(notification_types) == 3
|
|
|
|
|
|
|
|
|
|
|
2018-09-26 11:28:59 +01:00
|
|
|
def test_fetch_billing_data_for_day_groups_by_postage(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
letter_template = create_template(service=service, template_type='letter')
|
|
|
|
|
email_template = create_template(service=service, template_type='email')
|
|
|
|
|
create_notification(template=letter_template, status='delivered', postage='first')
|
|
|
|
|
create_notification(template=letter_template, status='delivered', postage='first')
|
|
|
|
|
create_notification(template=letter_template, status='delivered', postage='second')
|
|
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_billing_data_for_day_sets_postage_for_emails_and_sms_to_none(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
sms_template = create_template(service=service, template_type='sms')
|
|
|
|
|
email_template = create_template(service=service, template_type='email')
|
|
|
|
|
create_notification(template=sms_template, status='delivered')
|
|
|
|
|
create_notification(template=email_template, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
assert results[0].postage == 'none'
|
|
|
|
|
assert results[1].postage == 'none'
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_fetch_billing_data_for_day_returns_empty_list(notify_db_session):
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(today)
|
|
|
|
|
assert results == []
|
|
|
|
|
|
|
|
|
|
|
2018-05-14 16:21:16 +01:00
|
|
|
def test_fetch_billing_data_for_day_uses_notification_history(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
sms_template = create_template(service=service, template_type='sms')
|
2019-05-30 10:37:57 +01:00
|
|
|
create_notification_history(template=sms_template, status='delivered',
|
|
|
|
|
created_at=datetime.utcnow() - timedelta(days=8))
|
|
|
|
|
create_notification_history(template=sms_template, status='delivered',
|
|
|
|
|
created_at=datetime.utcnow() - timedelta(days=8))
|
2018-05-14 16:21:16 +01:00
|
|
|
|
|
|
|
|
Notification.query.delete()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
results = fetch_billing_data_for_day(process_day=datetime.utcnow() - timedelta(days=8), service_id=service.id)
|
|
|
|
|
assert len(results) == 1
|
|
|
|
|
assert results[0].notifications_sent == 2
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_fetch_billing_data_for_day_returns_list_for_given_service(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
service_2 = create_service(service_name='Service 2')
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
template_2 = create_template(service=service_2)
|
|
|
|
|
create_notification(template=template, status='delivered')
|
|
|
|
|
create_notification(template=template_2, status='delivered')
|
|
|
|
|
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(process_day=today, service_id=service.id)
|
|
|
|
|
assert len(results) == 1
|
|
|
|
|
assert results[0].service_id == service.id
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 17:07:30 +01:00
|
|
|
def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session):
|
|
|
|
|
service = create_service()
|
|
|
|
|
sms_template = create_template(service=service, template_type='sms')
|
|
|
|
|
email_template = create_template(service=service, template_type='email')
|
|
|
|
|
letter_template = create_template(service=service, template_type='letter')
|
|
|
|
|
for status in NOTIFICATION_STATUS_TYPES:
|
|
|
|
|
create_notification(template=sms_template, status=status)
|
|
|
|
|
create_notification(template=email_template, status=status)
|
|
|
|
|
create_notification(template=letter_template, status=status)
|
|
|
|
|
today = convert_utc_to_bst(datetime.utcnow())
|
|
|
|
|
results = fetch_billing_data_for_day(process_day=today, service_id=service.id)
|
|
|
|
|
|
|
|
|
|
sms_results = [x for x in results if x[2] == 'sms']
|
|
|
|
|
email_results = [x for x in results if x[2] == 'email']
|
|
|
|
|
letter_results = [x for x in results if x[2] == 'letter']
|
|
|
|
|
assert 7 == sms_results[0][7]
|
|
|
|
|
assert 7 == email_results[0][7]
|
|
|
|
|
assert 3 == letter_results[0][7]
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_get_rates_for_billing(notify_db_session):
|
|
|
|
|
create_rate(start_date=datetime.utcnow(), value=12, notification_type='email')
|
|
|
|
|
create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms')
|
|
|
|
|
create_rate(start_date=datetime.utcnow(), value=33, notification_type='email')
|
2019-04-03 15:34:02 +01:00
|
|
|
create_letter_rate(start_date=datetime.utcnow(), rate=0.66, post_class='first')
|
|
|
|
|
create_letter_rate(start_date=datetime.utcnow(), rate=0.33, post_class='second')
|
2018-04-25 15:40:44 +01:00
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
|
|
|
|
|
|
|
|
|
assert len(non_letter_rates) == 3
|
2019-04-03 15:34:02 +01:00
|
|
|
assert len(letter_rates) == 2
|
2018-04-25 15:40:44 +01:00
|
|
|
|
|
|
|
|
|
2019-04-03 14:52:41 +01:00
|
|
|
@freeze_time('2017-06-01 12:00')
|
2018-04-25 15:40:44 +01:00
|
|
|
def test_get_rate(notify_db_session):
|
2019-04-03 15:34:02 +01:00
|
|
|
create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=1.2, notification_type='email')
|
|
|
|
|
create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=2.2, notification_type='sms')
|
|
|
|
|
create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=3.3, notification_type='email')
|
|
|
|
|
create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.66, post_class='first')
|
|
|
|
|
create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.3, post_class='second')
|
2019-04-03 14:52:41 +01:00
|
|
|
|
2018-04-25 15:40:44 +01:00
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
2018-04-25 16:17:56 +01:00
|
|
|
rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='sms',
|
2019-04-03 14:52:41 +01:00
|
|
|
date=date(2017, 6, 1))
|
2018-04-25 15:40:44 +01:00
|
|
|
letter_rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates,
|
|
|
|
|
notification_type='letter',
|
|
|
|
|
crown=True,
|
2018-05-22 17:35:21 +01:00
|
|
|
letter_page_count=1,
|
2019-04-03 14:52:41 +01:00
|
|
|
date=date(2017, 6, 1))
|
2018-04-25 15:40:44 +01:00
|
|
|
|
|
|
|
|
assert rate == 2.2
|
2018-07-10 11:31:18 +01:00
|
|
|
assert letter_rate == Decimal('0.3')
|
2018-04-25 15:40:44 +01:00
|
|
|
|
|
|
|
|
|
2018-09-20 15:49:15 +01:00
|
|
|
@pytest.mark.parametrize("letter_post_class,expected_rate", [("first", "0.61"), ("second", "0.35")])
|
|
|
|
|
def test_get_rate_filters_letters_by_post_class(notify_db_session, letter_post_class, expected_rate):
|
2019-04-03 15:34:02 +01:00
|
|
|
create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), sheet_count=2, rate=0.61, post_class='first')
|
|
|
|
|
create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), sheet_count=2, rate=0.35, post_class='second')
|
|
|
|
|
|
2018-09-20 15:49:15 +01:00
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
|
|
|
|
rate = get_rate(non_letter_rates, letter_rates, "letter", datetime(2018, 10, 1), True, 2, letter_post_class)
|
|
|
|
|
assert rate == Decimal(expected_rate)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("date,expected_rate", [(datetime(2018, 9, 30), '0.33'), (datetime(2018, 10, 1), '0.35')])
|
|
|
|
|
def test_get_rate_chooses_right_rate_depending_on_date(notify_db_session, date, expected_rate):
|
2019-04-03 15:34:02 +01:00
|
|
|
create_letter_rate(start_date=datetime(2016, 1, 1, 0, 0), sheet_count=2, rate=0.33, post_class='second')
|
|
|
|
|
create_letter_rate(start_date=datetime(2018, 9, 30, 23, 0), sheet_count=2, rate=0.35, post_class='second')
|
|
|
|
|
|
2018-09-20 15:49:15 +01:00
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
|
|
|
|
rate = get_rate(non_letter_rates, letter_rates, "letter", date, True, 2, "second")
|
|
|
|
|
assert rate == Decimal(expected_rate)
|
|
|
|
|
|
|
|
|
|
|
2018-07-31 11:04:48 +01:00
|
|
|
def test_get_rate_for_letters_when_page_count_is_zero(notify_db_session):
|
|
|
|
|
non_letter_rates, letter_rates = get_rates_for_billing()
|
|
|
|
|
letter_rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates,
|
|
|
|
|
notification_type='letter',
|
|
|
|
|
crown=True,
|
|
|
|
|
letter_page_count=0,
|
|
|
|
|
date=datetime.utcnow())
|
|
|
|
|
assert letter_rate == 0
|
|
|
|
|
|
|
|
|
|
|
2018-04-27 15:15:55 +01:00
|
|
|
def test_fetch_monthly_billing_for_year(notify_db_session):
|
2018-04-25 14:24:47 +01:00
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service, template_type="sms")
|
2018-04-24 17:37:04 +01:00
|
|
|
for i in range(1, 31):
|
|
|
|
|
create_ft_billing(bst_date='2018-06-{}'.format(i),
|
|
|
|
|
service=service,
|
|
|
|
|
template=template,
|
2018-04-25 14:24:47 +01:00
|
|
|
notification_type='sms',
|
2018-05-04 13:09:14 +01:00
|
|
|
rate_multiplier=2,
|
2018-04-25 14:24:47 +01:00
|
|
|
rate=0.162)
|
|
|
|
|
for i in range(1, 32):
|
|
|
|
|
create_ft_billing(bst_date='2018-07-{}'.format(i),
|
|
|
|
|
service=service,
|
|
|
|
|
template=template,
|
|
|
|
|
notification_type='sms',
|
|
|
|
|
rate=0.158)
|
|
|
|
|
|
2018-04-27 15:15:55 +01:00
|
|
|
results = fetch_monthly_billing_for_year(service_id=service.id, year=2018)
|
2018-04-24 17:37:04 +01:00
|
|
|
|
2018-04-25 14:24:47 +01:00
|
|
|
assert len(results) == 2
|
2018-05-08 13:53:44 +01:00
|
|
|
assert str(results[0].month) == "2018-06-01"
|
2018-04-27 15:15:55 +01:00
|
|
|
assert results[0].notifications_sent == 30
|
2018-05-04 13:09:14 +01:00
|
|
|
assert results[0].billable_units == Decimal('60')
|
2018-04-27 15:15:55 +01:00
|
|
|
assert results[0].rate == Decimal('0.162')
|
|
|
|
|
assert results[0].notification_type == 'sms'
|
2018-09-27 13:58:01 +01:00
|
|
|
assert results[0].postage == 'none'
|
2018-04-27 15:15:55 +01:00
|
|
|
|
2018-05-08 13:53:44 +01:00
|
|
|
assert str(results[1].month) == "2018-07-01"
|
2018-04-27 15:15:55 +01:00
|
|
|
assert results[1].notifications_sent == 31
|
|
|
|
|
assert results[1].billable_units == Decimal('31')
|
|
|
|
|
assert results[1].rate == Decimal('0.158')
|
|
|
|
|
assert results[1].notification_type == 'sms'
|
2018-09-27 13:58:01 +01:00
|
|
|
assert results[1].postage == 'none'
|
2018-04-25 14:24:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time('2018-08-01 13:30:00')
|
2018-04-27 15:15:55 +01:00
|
|
|
def test_fetch_monthly_billing_for_year_adds_data_for_today(notify_db_session):
|
2018-04-25 14:24:47 +01:00
|
|
|
service = create_service()
|
|
|
|
|
template = create_template(service=service, template_type="email")
|
|
|
|
|
for i in range(1, 32):
|
|
|
|
|
create_ft_billing(bst_date='2018-07-{}'.format(i),
|
|
|
|
|
service=service,
|
|
|
|
|
template=template,
|
|
|
|
|
notification_type='email',
|
|
|
|
|
rate=0.162)
|
|
|
|
|
create_notification(template=template, status='delivered')
|
|
|
|
|
|
|
|
|
|
assert db.session.query(FactBilling.bst_date).count() == 31
|
2018-04-27 15:15:55 +01:00
|
|
|
results = fetch_monthly_billing_for_year(service_id=service.id,
|
|
|
|
|
year=2018)
|
2018-04-25 14:24:47 +01:00
|
|
|
assert db.session.query(FactBilling.bst_date).count() == 32
|
|
|
|
|
assert len(results) == 2
|
2018-04-27 15:15:55 +01:00
|
|
|
|
|
|
|
|
|
2018-04-30 16:34:40 +01:00
|
|
|
def test_fetch_monthly_billing_for_year_return_financial_year(notify_db_session):
|
2018-05-11 16:25:16 +01:00
|
|
|
service = set_up_yearly_data()
|
2018-04-27 15:15:55 +01:00
|
|
|
|
|
|
|
|
results = fetch_monthly_billing_for_year(service.id, 2016)
|
2018-05-08 12:09:29 +01:00
|
|
|
# returns 3 rows, per month, returns financial year april to end of march
|
2018-04-27 15:15:55 +01:00
|
|
|
# Orders by Month
|
|
|
|
|
|
2018-05-22 17:35:21 +01:00
|
|
|
assert len(results) == 48
|
2018-05-08 13:53:44 +01:00
|
|
|
assert str(results[0].month) == "2016-04-01"
|
2018-04-30 16:34:40 +01:00
|
|
|
assert results[0].notification_type == 'email'
|
2018-05-08 12:09:29 +01:00
|
|
|
assert results[0].notifications_sent == 30
|
|
|
|
|
assert results[0].billable_units == 30
|
|
|
|
|
assert results[0].rate == Decimal('0')
|
2018-05-08 13:53:44 +01:00
|
|
|
assert str(results[1].month) == "2016-04-01"
|
2018-04-30 16:34:40 +01:00
|
|
|
assert results[1].notification_type == 'letter'
|
2018-05-08 12:09:29 +01:00
|
|
|
assert results[1].notifications_sent == 30
|
|
|
|
|
assert results[1].billable_units == 30
|
2018-05-22 17:35:21 +01:00
|
|
|
assert results[1].rate == Decimal('0.30')
|
|
|
|
|
assert str(results[1].month) == "2016-04-01"
|
|
|
|
|
assert results[2].notification_type == 'letter'
|
2018-05-08 12:09:29 +01:00
|
|
|
assert results[2].notifications_sent == 30
|
|
|
|
|
assert results[2].billable_units == 30
|
2018-05-22 17:35:21 +01:00
|
|
|
assert results[2].rate == Decimal('0.33')
|
|
|
|
|
assert str(results[3].month) == "2016-04-01"
|
|
|
|
|
assert results[3].notification_type == 'sms'
|
|
|
|
|
assert results[3].notifications_sent == 30
|
|
|
|
|
assert results[3].billable_units == 30
|
|
|
|
|
assert results[3].rate == Decimal('0.162')
|
|
|
|
|
assert str(results[4].month) == "2016-05-01"
|
|
|
|
|
assert str(results[47].month) == "2017-03-01"
|
2018-05-11 16:25:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_billing_totals_for_year(notify_db_session):
|
|
|
|
|
service = set_up_yearly_data()
|
|
|
|
|
results = fetch_billing_totals_for_year(service_id=service.id, year=2016)
|
|
|
|
|
|
2018-05-22 17:35:21 +01:00
|
|
|
assert len(results) == 4
|
2018-05-11 16:25:16 +01:00
|
|
|
assert results[0].notification_type == 'email'
|
|
|
|
|
assert results[0].notifications_sent == 365
|
|
|
|
|
assert results[0].billable_units == 365
|
|
|
|
|
assert results[0].rate == Decimal('0')
|
|
|
|
|
|
|
|
|
|
assert results[1].notification_type == 'letter'
|
|
|
|
|
assert results[1].notifications_sent == 365
|
|
|
|
|
assert results[1].billable_units == 365
|
2018-05-22 17:35:21 +01:00
|
|
|
assert results[1].rate == Decimal('0.3')
|
2018-05-11 16:25:16 +01:00
|
|
|
|
2018-05-22 17:35:21 +01:00
|
|
|
assert results[2].notification_type == 'letter'
|
2018-05-11 16:25:16 +01:00
|
|
|
assert results[2].notifications_sent == 365
|
|
|
|
|
assert results[2].billable_units == 365
|
2018-05-22 17:35:21 +01:00
|
|
|
assert results[2].rate == Decimal('0.33')
|
|
|
|
|
|
|
|
|
|
assert results[3].notification_type == 'sms'
|
|
|
|
|
assert results[3].notifications_sent == 365
|
|
|
|
|
assert results[3].billable_units == 365
|
|
|
|
|
assert results[3].rate == Decimal('0.162')
|
2018-07-26 18:41:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_billing_data(notify_db_session):
|
|
|
|
|
service_1 = create_service(service_name='1')
|
|
|
|
|
service_2 = create_service(service_name='2')
|
|
|
|
|
sms_template = create_template(service_1, 'sms')
|
|
|
|
|
email_template = create_template(service_1, 'email')
|
|
|
|
|
other_service_template = create_template(service_2, 'sms')
|
|
|
|
|
|
|
|
|
|
existing_rows_to_delete = [ # noqa
|
|
|
|
|
create_ft_billing('2018-01-01', 'sms', sms_template, service_1, billable_unit=1),
|
|
|
|
|
create_ft_billing('2018-01-01', 'email', email_template, service_1, billable_unit=2)
|
|
|
|
|
]
|
|
|
|
|
other_day = create_ft_billing('2018-01-02', 'sms', sms_template, service_1, billable_unit=3)
|
|
|
|
|
other_service = create_ft_billing('2018-01-01', 'sms', other_service_template, service_2, billable_unit=4)
|
|
|
|
|
|
|
|
|
|
delete_billing_data_for_service_for_day('2018-01-01', service_1.id)
|
|
|
|
|
|
|
|
|
|
current_rows = FactBilling.query.all()
|
|
|
|
|
assert sorted(x.billable_units for x in current_rows) == sorted(
|
|
|
|
|
[other_day.billable_units, other_service.billable_units]
|
|
|
|
|
)
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_sms_free_allowance_remainder_with_two_services(notify_db_session):
|
|
|
|
|
service = create_service(service_name='has free allowance')
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
org = create_organisation(name="Org for {}".format(service.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
|
|
|
|
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016)
|
|
|
|
|
create_ft_billing(service=service, template=template,
|
|
|
|
|
bst_date=datetime(2016, 4, 20), notification_type='sms', billable_unit=2, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service, template=template, bst_date=datetime(2016, 5, 20), notification_type='sms',
|
|
|
|
|
billable_unit=3, rate=0.11)
|
|
|
|
|
|
|
|
|
|
service_2 = create_service(service_name='used free allowance')
|
|
|
|
|
template_2 = create_template(service=service_2)
|
|
|
|
|
org_2 = create_organisation(name="Org for {}".format(service_2.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id)
|
|
|
|
|
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=20, financial_year_start=2016)
|
|
|
|
|
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 4, 20), notification_type='sms',
|
|
|
|
|
billable_unit=12, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 4, 22), notification_type='sms',
|
|
|
|
|
billable_unit=10, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 5, 20), notification_type='sms',
|
|
|
|
|
billable_unit=3, rate=0.11)
|
|
|
|
|
results = fetch_sms_free_allowance_remainder(datetime(2016, 5, 1)).all()
|
|
|
|
|
assert len(results) == 2
|
|
|
|
|
service_result = [row for row in results if row[0] == service.id]
|
|
|
|
|
assert service_result[0] == (service.id, 10, 2, 8)
|
|
|
|
|
service_2_result = [row for row in results if row[0] == service_2.id]
|
|
|
|
|
assert service_2_result[0] == (service_2.id, 20, 22, 0)
|
|
|
|
|
|
|
|
|
|
|
2019-08-23 17:02:44 +01:00
|
|
|
def test_fetch_sms_billing_for_all_services_for_first_quarter(notify_db_session):
|
|
|
|
|
# This test is useful because the inner query resultset is empty.
|
|
|
|
|
service = create_service(service_name='a - has free allowance')
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
org = create_organisation(name="Org for {}".format(service.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
|
|
|
|
create_annual_billing(service_id=service.id, free_sms_fragment_limit=25000, financial_year_start=2019)
|
|
|
|
|
create_ft_billing(service=service, template=template,
|
|
|
|
|
bst_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=44, rate=0.11)
|
|
|
|
|
results = fetch_sms_billing_for_all_services(datetime(2019, 4, 1), datetime(2019, 5, 30))
|
|
|
|
|
assert len(results) == 1
|
|
|
|
|
assert results[0] == (org.name, org.id, service.name, service.id, 25000, Decimal('0.11'), 25000, 44, 0,
|
|
|
|
|
Decimal('0'))
|
|
|
|
|
|
|
|
|
|
|
2019-08-06 13:29:59 +01:00
|
|
|
def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session):
|
|
|
|
|
service = create_service(service_name='a - has free allowance')
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
org = create_organisation(name="Org for {}".format(service.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
|
|
|
|
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
|
|
|
|
create_ft_billing(service=service, template=template,
|
|
|
|
|
bst_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=2, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
|
|
|
|
billable_unit=2, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 22), notification_type='sms',
|
|
|
|
|
billable_unit=1, rate=0.11)
|
|
|
|
|
|
|
|
|
|
service_2 = create_service(service_name='b - used free allowance')
|
|
|
|
|
template_2 = create_template(service=service_2)
|
|
|
|
|
org_2 = create_organisation(name="Org for {}".format(service_2.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id)
|
|
|
|
|
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
|
|
|
|
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 4, 20), notification_type='sms',
|
|
|
|
|
billable_unit=12, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
|
|
|
|
billable_unit=3, rate=0.11)
|
|
|
|
|
service_3 = create_service(service_name='c - partial allowance')
|
|
|
|
|
template_3 = create_template(service=service_3)
|
|
|
|
|
org_3 = create_organisation(name="Org for {}".format(service_3.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service_3, organisation_id=org_3.id)
|
|
|
|
|
create_annual_billing(service_id=service_3.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
|
|
|
|
create_ft_billing(service=service_3, template=template_3, bst_date=datetime(2019, 4, 20), notification_type='sms',
|
|
|
|
|
billable_unit=5, rate=0.11)
|
|
|
|
|
create_ft_billing(service=service_3, template=template_3, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
|
|
|
|
billable_unit=7, rate=0.11)
|
|
|
|
|
|
|
|
|
|
service_4 = create_service(service_name='d - email only')
|
|
|
|
|
email_template = create_template(service=service_4, template_type='email')
|
|
|
|
|
org_4 = create_organisation(name="Org for {}".format(service_4.name))
|
|
|
|
|
dao_add_service_to_organisation(service=service_4, organisation_id=org_4.id)
|
|
|
|
|
create_annual_billing(service_id=service_4.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
|
|
|
|
create_ft_billing(service=service_4, template=email_template, bst_date=datetime(2019, 5, 22), notifications_sent=5,
|
|
|
|
|
notification_type='email', billable_unit=0, rate=0)
|
|
|
|
|
|
|
|
|
|
results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31))
|
|
|
|
|
assert len(results) == 3
|
2019-08-19 14:44:08 +01:00
|
|
|
# (organisation_name, organisation_id, service_name, free_sms_fragment_limit, sms_rate,
|
|
|
|
|
# sms_remainder, sms_billable_units, chargeable_billable_sms, sms_cost)
|
|
|
|
|
assert results[0] == (org.name, org.id, service.name, service.id, 10, Decimal('0.11'), 8, 3, 0, Decimal('0'))
|
|
|
|
|
assert results[1] == (org_2.name, org_2.id, service_2.name, service_2.id, 10, Decimal('0.11'), 0, 3, 3,
|
|
|
|
|
Decimal('0.33'))
|
|
|
|
|
assert results[2] == (org_3.name, org_3.id, service_3.name, service_3.id, 10, Decimal('0.11'), 5, 7, 2,
|
|
|
|
|
Decimal('0.22'))
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_sms_billing_for_all_services_without_an_organisation_appears(notify_db_session):
|
2019-08-22 17:48:24 +01:00
|
|
|
org, org_2, service, service_2, service_3, service_sms_only = set_up_usage_data(datetime(2019, 5, 1))
|
2019-08-06 13:29:59 +01:00
|
|
|
results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31))
|
|
|
|
|
|
2019-08-22 17:48:24 +01:00
|
|
|
assert len(results) == 2
|
|
|
|
|
# organisation_name, organisation_id, service_name, service_id, free_sms_fragment_limit,
|
|
|
|
|
# sms_rate, sms_remainder, sms_billable_units, chargeable_billable_units, sms_cost
|
|
|
|
|
assert results[0] == (org.name, org.id, service.name, service.id, 10, Decimal('0.11'), 8, 3, 0, Decimal('0'))
|
2019-08-23 17:02:44 +01:00
|
|
|
assert results[1] == (None, None, service_sms_only.name, service_sms_only.id, 10, Decimal('0.11'),
|
|
|
|
|
0, 3, 3, Decimal('0.33'))
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_letter_costs_for_all_services(notify_db_session):
|
2019-08-22 17:48:24 +01:00
|
|
|
org, org_2, service, service_2, service_3, service_sms_only = set_up_usage_data(datetime(2019, 6, 1))
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
results = fetch_letter_costs_for_all_services(datetime(2019, 6, 1), datetime(2019, 9, 30))
|
|
|
|
|
|
2019-08-19 14:23:09 +01:00
|
|
|
assert len(results) == 3
|
|
|
|
|
assert results[0] == (org.name, org.id, service.name, service.id, Decimal('3.40'))
|
|
|
|
|
assert results[1] == (org_2.name, org_2.id, service_2.name, service_2.id, Decimal('14.00'))
|
|
|
|
|
assert results[2] == (None, None, service_3.name, service_3.id, Decimal('8.25'))
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_letter_line_items_for_all_service(notify_db_session):
|
2019-08-22 17:48:24 +01:00
|
|
|
org_1, org_2, service_1, service_2, service_3, service_sms_only = set_up_usage_data(datetime(2019, 6, 1))
|
|
|
|
|
|
2019-08-06 13:29:59 +01:00
|
|
|
results = fetch_letter_line_items_for_all_services(datetime(2019, 6, 1), datetime(2019, 9, 30))
|
2019-08-22 17:48:24 +01:00
|
|
|
|
2019-08-19 14:23:09 +01:00
|
|
|
assert len(results) == 5
|
|
|
|
|
assert results[0] == (org_1.name, org_1.id, service_1.name, service_1.id, 2, Decimal('0.45'), 'second', 6)
|
|
|
|
|
assert results[1] == (org_1.name, org_1.id, service_1.name, service_1.id, 1, Decimal("0.35"), 'first', 2)
|
|
|
|
|
assert results[2] == (org_2.name, org_2.id, service_2.name, service_2.id, 5, Decimal("0.65"), 'second', 20)
|
|
|
|
|
assert results[3] == (org_2.name, org_2.id, service_2.name, service_2.id, 3, Decimal("0.50"), 'first', 2)
|
|
|
|
|
assert results[4] == (None, None, service_3.name, service_3.id, 4, Decimal("0.55"), 'second', 15)
|