Merge pull request #1129 from alphagov/use-bst-for-month

Use BST to calculate monthly billing
This commit is contained in:
Rebecca Law
2017-07-26 09:56:15 +01:00
committed by GitHub
4 changed files with 17 additions and 14 deletions

View File

@@ -298,5 +298,5 @@ def populate_monthly_billing():
# this will overwrite the existing amount. # this will overwrite the existing amount.
yesterday = datetime.utcnow() - timedelta(days=1) yesterday = datetime.utcnow() - timedelta(days=1)
start_date, end_date = get_month_start_end_date(yesterday) start_date, end_date = get_month_start_end_date(yesterday)
services = get_service_ids_that_need_sms_billing_populated(start_date, end_date=end_date) services = get_service_ids_that_need_sms_billing_populated(start_date=start_date, end_date=end_date)
[create_or_update_monthly_billing_sms(service_id=s.service_id, billing_month=start_date) for s in services] [create_or_update_monthly_billing_sms(service_id=s.service_id, billing_month=yesterday) for s in services]

View File

@@ -2,6 +2,8 @@ from datetime import datetime, timedelta
import pytz import pytz
from app.utils import convert_bst_to_utc
def get_financial_year(year): def get_financial_year(year):
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1) return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
@@ -28,4 +30,4 @@ def get_month_start_end_date(month_year):
_, num_days = calendar.monthrange(month_year.year, month_year.month) _, num_days = calendar.monthrange(month_year.year, month_year.month)
first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0) first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0)
last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999) last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999)
return first_day, last_day return convert_bst_to_utc(first_day), convert_bst_to_utc(last_day)

View File

@@ -26,7 +26,7 @@ def create_or_update_monthly_billing_sms(service_id, billing_month):
# update monthly # update monthly
monthly_totals = _monthly_billing_data_to_json(monthly) monthly_totals = _monthly_billing_data_to_json(monthly)
row = MonthlyBilling.query.filter_by(year=billing_month.year, row = MonthlyBilling.query.filter_by(year=billing_month.year,
month=datetime.strftime(billing_month, "%B"), month=datetime.strftime(end_date, "%B"),
notification_type='sms').first() notification_type='sms').first()
if row: if row:
row.monthly_totals = monthly_totals row.monthly_totals = monthly_totals
@@ -34,7 +34,7 @@ def create_or_update_monthly_billing_sms(service_id, billing_month):
row = MonthlyBilling(service_id=service_id, row = MonthlyBilling(service_id=service_id,
notification_type=SMS_TYPE, notification_type=SMS_TYPE,
year=billing_month.year, year=billing_month.year,
month=datetime.strftime(billing_month, "%B"), month=datetime.strftime(end_date, "%B"),
monthly_totals=monthly_totals) monthly_totals=monthly_totals)
db.session.add(row) db.session.add(row)

View File

@@ -17,14 +17,15 @@ def test_get_april_fools():
assert april_fools.tzinfo is None assert april_fools.tzinfo is None
@pytest.mark.parametrize("month, year, expected_end", @pytest.mark.parametrize("month, year, expected_start, expected_end",
[(7, 2017, 31), [
(2, 2016, 29), (7, 2017, datetime(2017, 6, 30, 23, 00, 00), datetime(2017, 7, 31, 22, 59, 59, 99999)),
(2, 2017, 28), (2, 2016, datetime(2016, 2, 1, 00, 00, 00), datetime(2016, 2, 29, 23, 59, 59, 99999)),
(9, 2018, 30), (2, 2017, datetime(2017, 2, 1, 00, 00, 00), datetime(2017, 2, 28, 23, 59, 59, 99999)),
(12, 2019, 31)]) (9, 2018, datetime(2018, 8, 31, 23, 00, 00), datetime(2018, 9, 30, 22, 59, 59, 99999)),
def test_get_month_start_end_date(month, year, expected_end): (12, 2019, datetime(2019, 12, 1, 00, 00, 00), datetime(2019, 12, 31, 23, 59, 59, 99999))])
def test_get_month_start_end_date(month, year, expected_start, expected_end):
month_year = datetime(year, month, 10, 13, 30, 00) month_year = datetime(year, month, 10, 13, 30, 00)
result = get_month_start_end_date(month_year) result = get_month_start_end_date(month_year)
assert result[0] == datetime(year, month, 1, 0, 0, 0, 0) assert result[0] == expected_start
assert result[1] == datetime(year, month, expected_end, 23, 59, 59, 99999) assert result[1] == expected_end