merge from main

This commit is contained in:
Kenneth Kehl
2023-06-19 07:56:10 -07:00
20 changed files with 138 additions and 107 deletions

View File

@@ -2,7 +2,7 @@ from flask import current_app
from app import db
from app.dao.dao_utils import autocommit
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_current_calendar_year_start_year
from app.models import AnnualBilling
@@ -38,7 +38,7 @@ def dao_update_annual_billing_for_future_years(service_id, free_sms_fragment_lim
def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None):
if not financial_year_start:
financial_year_start = get_current_financial_year_start_year()
financial_year_start = get_current_calendar_year_start_year()
return AnnualBilling.query.filter_by(
service_id=service_id,
@@ -72,7 +72,7 @@ def set_default_free_allowance_for_service(service, year_start=None):
}
}
if not year_start:
year_start = get_current_financial_year_start_year()
year_start = get_current_calendar_year_start_year()
# handle cases where the year is less than 2020 or greater than 2021
if year_start < 2020:
year_start = 2020

View File

@@ -15,12 +15,12 @@ def get_months_for_year(start, end, year):
return [datetime(year, month, 1) for month in range(start, end)]
def get_financial_year(year):
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
def get_calendar_year(year):
return get_new_years(year), get_new_years(year + 1) - timedelta(microseconds=1)
def get_financial_year_dates(year):
year_start_datetime, year_end_datetime = get_financial_year(year)
def get_calendar_year_dates(year):
year_start_datetime, year_end_datetime = get_calendar_year(year)
return (
year_start_datetime.date(),
@@ -28,16 +28,15 @@ def get_financial_year_dates(year):
)
def get_current_financial_year():
def get_current_calendar_year():
now = datetime.utcnow()
current_month = int(now.strftime('%-m'))
current_year = int(now.strftime('%Y'))
year = current_year if current_month > 3 else current_year - 1
return get_financial_year(year)
year = current_year
return get_calendar_year(year)
def get_april_fools(year):
return datetime(year, 4, 1, 0, 0, 0)
def get_new_years(year):
return datetime(year, 1, 1, 0, 0, 0)
def get_month_start_and_end_date_in_utc(month_year):
@@ -53,21 +52,21 @@ def get_month_start_and_end_date_in_utc(month_year):
return first_day, last_day
def get_current_financial_year_start_year():
def get_current_calendar_year_start_year():
now = datetime.now()
financial_year_start = now.year
start_date, end_date = get_financial_year(now.year)
start_date, end_date = get_calendar_year(now.year)
if now < start_date:
financial_year_start = financial_year_start - 1
return financial_year_start
def get_financial_year_for_datetime(start_date):
def get_calendar_year_for_datetime(start_date):
if type(start_date) == date:
start_date = datetime.combine(start_date, time.min)
year = int(start_date.strftime('%Y'))
if start_date < get_april_fools(year):
if start_date < get_new_years(year):
return year - 1
else:
return year

View File

@@ -7,8 +7,8 @@ from sqlalchemy.sql.expression import case, literal
from app import db
from app.dao.date_util import (
get_financial_year_dates,
get_financial_year_for_datetime,
get_calendar_year_dates,
get_calendar_year_for_datetime,
)
from app.dao.organisation_dao import dao_get_organisation_live_services
from app.models import (
@@ -31,7 +31,7 @@ from app.utils import get_midnight_in_utc
def fetch_sms_free_allowance_remainder_until_date(end_date):
# ASSUMPTION: AnnualBilling has been populated for year.
billing_year = get_financial_year_for_datetime(end_date)
billing_year = get_calendar_year_for_datetime(end_date)
start_of_year = date(billing_year, 4, 1)
billable_units = func.coalesce(func.sum(FactBilling.billable_units * FactBilling.rate_multiplier), 0)
@@ -177,7 +177,7 @@ def fetch_monthly_billing_for_year(service_id, year):
Since the data in ft_billing is only refreshed once a day for all services,
we also update the table on-the-fly if we need accurate data for this year.
"""
_, year_end = get_financial_year_dates(year)
_, year_end = get_calendar_year_dates(year)
today = datetime.utcnow().date()
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
@@ -216,7 +216,7 @@ def fetch_monthly_billing_for_year(service_id, year):
def query_service_email_usage_for_year(service_id, year):
year_start, year_end = get_financial_year_dates(year)
year_start, year_end = get_calendar_year_dates(year)
return db.session.query(
FactBilling.local_date,
@@ -265,7 +265,7 @@ def query_service_sms_usage_for_year(service_id, year):
on a given local_date. This means we don't need to worry about how to assign
free allowance if it happens to run out when a rate changes.
"""
year_start, year_end = get_financial_year_dates(year)
year_start, year_end = get_calendar_year_dates(year)
this_rows_chargeable_units = FactBilling.billable_units * FactBilling.rate_multiplier
# Subquery for the number of chargeable units in all rows preceding this one,
@@ -568,7 +568,7 @@ def query_organisation_sms_usage_for_year(organisation_id, year):
"""
See docstring for query_service_sms_usage_for_year()
"""
year_start, year_end = get_financial_year_dates(year)
year_start, year_end = get_calendar_year_dates(year)
this_rows_chargeable_units = FactBilling.billable_units * FactBilling.rate_multiplier
# Subquery for the number of chargeable units in all rows preceding this one,
@@ -622,7 +622,7 @@ def query_organisation_sms_usage_for_year(organisation_id, year):
def fetch_usage_year_for_organisation(organisation_id, year):
year_start, year_end = get_financial_year_dates(year)
year_start, year_end = get_calendar_year_dates(year)
today = datetime.utcnow().date()
services = dao_get_organisation_live_services(organisation_id)

View File

@@ -8,7 +8,7 @@ from sqlalchemy.sql.expression import and_, asc, case, func
from app import db
from app.dao.dao_utils import VersionOptions, autocommit, version_class
from app.dao.date_util import get_current_financial_year
from app.dao.date_util import get_current_calendar_year
from app.dao.organisation_dao import dao_get_organisation_by_email_address
from app.dao.service_sms_sender_dao import insert_service_sms_sender
from app.dao.service_user_dao import dao_get_service_user
@@ -79,7 +79,7 @@ def dao_count_live_services():
def dao_fetch_live_services_data():
year_start_date, year_end_date = get_current_financial_year()
year_start_date, year_end_date = get_current_calendar_year()
most_recent_annual_billing = db.session.query(
AnnualBilling.service_id,