This commit is contained in:
Pea Tyczynska
2022-04-26 17:38:46 +01:00
parent 229d5ec534
commit 389055d50a
3 changed files with 47 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
from datetime import date, datetime, time, timedelta
from datetime import datetime, time, timedelta
import pytz
from notifications_utils.timezones import convert_bst_to_utc
@@ -64,7 +64,7 @@ def get_current_financial_year_start_year():
def get_financial_year_for_datetime(start_date):
if type(start_date) == date:
if type(start_date) != datetime:
start_date = datetime.combine(start_date, time.min)
year = int(start_date.strftime('%Y'))

View File

@@ -1,4 +1,4 @@
from datetime import date, datetime, timedelta
from datetime import date, datetime, time, timedelta
from flask import current_app
from notifications_utils.timezones import convert_utc_to_bst
@@ -596,7 +596,7 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date):
def fetch_sms_rates_for_date_range(start_date, end_date):
Rate.query.filter(
return Rate.query.filter(
Rate.notification_type == 'sms',
Rate.valid_from >= start_date,
Rate.valid_from <= end_date
@@ -607,7 +607,7 @@ def fetch_sms_rates_for_date_range(start_date, end_date):
def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date):
rates = fetch_sms_rates_for_date_range(start_date, end_date)
if len(rates) >= 1:
if len(rates) >= 1 and (convert_utc_to_bst(rates[0].valid_from)).date() != start_date:
return fetch_sms_billing_for_organisation_with_rate_change(organisation_id, start_date, end_date, rates)
# ASSUMPTION: AnnualBilling has been populated for year.
allowance_left_at_start_date_query = fetch_sms_free_allowance_remainder_until_date(start_date).subquery()
@@ -665,13 +665,25 @@ def fetch_sms_billing_for_organisation_with_rate_change(organisation_id, start_d
sms_billings_for_organisation = []
dates = [start_date, end_date]
for rate in rates:
dates.append(rate.valid_from)
dates = dates.sorted()
dates.append(convert_utc_to_bst(rate.valid_from).date())
dates.sort()
for i, date_ in enumerate(dates):
part_of_billing_data = fetch_sms_billing_for_organisation(organisation_id, date_, dates[i+1])
loop_start_date = date_
if i == len(dates) - 1:
break
elif i == len(dates) - 2:
loop_end_date = dates[i+1]
else:
loop_end_date = dates[i+1] - timedelta(days=1)
part_of_billing_data = fetch_sms_billing_for_organisation(
organisation_id,
loop_start_date,
loop_end_date
)
sms_billings_for_organisation.append(part_of_billing_data)
# WIP think how to unite that data - do we give admin multiple dicts and then admin counts them, or do we somehow
# mash it in here?
import pdb; pdb.set_trace()
# now we need to add costs from those data points, take freshest allowance info etc.
return sms_billings_for_organisation
def fetch_usage_year_for_organisation(organisation_id, year):