2021-03-25 08:10:22 +00:00
|
|
|
from flask import current_app
|
|
|
|
|
|
2017-11-02 17:02:00 +00:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit
|
2023-06-14 13:19:11 -07:00
|
|
|
from app.dao.date_util import get_current_calendar_year_start_year
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.models import AnnualBilling
|
2017-11-02 12:19:17 +00:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2023-08-29 14:54:30 -07:00
|
|
|
def dao_create_or_update_annual_billing_for_year(
|
|
|
|
|
service_id, free_sms_fragment_limit, financial_year_start
|
|
|
|
|
):
|
2017-11-02 12:19:17 +00:00
|
|
|
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
|
|
|
|
|
|
|
|
|
if result:
|
|
|
|
|
result.free_sms_fragment_limit = free_sms_fragment_limit
|
|
|
|
|
else:
|
2023-08-29 14:54:30 -07:00
|
|
|
result = AnnualBilling(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
financial_year_start=financial_year_start,
|
|
|
|
|
free_sms_fragment_limit=free_sms_fragment_limit,
|
|
|
|
|
)
|
2017-11-02 12:19:17 +00:00
|
|
|
db.session.add(result)
|
|
|
|
|
return result
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_annual_billing(service_id):
|
2023-08-29 14:54:30 -07:00
|
|
|
return (
|
|
|
|
|
AnnualBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
)
|
|
|
|
|
.order_by(AnnualBilling.financial_year_start)
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2023-08-29 14:54:30 -07:00
|
|
|
def dao_update_annual_billing_for_future_years(
|
|
|
|
|
service_id, free_sms_fragment_limit, financial_year_start
|
|
|
|
|
):
|
2017-11-28 13:49:14 +00:00
|
|
|
AnnualBilling.query.filter(
|
2017-11-02 12:19:17 +00:00
|
|
|
AnnualBilling.service_id == service_id,
|
2023-08-29 14:54:30 -07:00
|
|
|
AnnualBilling.financial_year_start > financial_year_start,
|
|
|
|
|
).update({"free_sms_fragment_limit": free_sms_fragment_limit})
|
2017-11-02 12:19:17 +00:00
|
|
|
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None):
|
|
|
|
|
if not financial_year_start:
|
2023-06-14 13:19:11 -07:00
|
|
|
financial_year_start = get_current_calendar_year_start_year()
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
return AnnualBilling.query.filter_by(
|
2023-08-29 14:54:30 -07:00
|
|
|
service_id=service_id, financial_year_start=financial_year_start
|
2017-10-24 13:23:24 +01:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_all_free_sms_fragment_limit(service_id):
|
2023-08-29 14:54:30 -07:00
|
|
|
return (
|
|
|
|
|
AnnualBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
)
|
|
|
|
|
.order_by(AnnualBilling.financial_year_start)
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2021-03-25 08:10:22 +00:00
|
|
|
|
|
|
|
|
|
2021-04-12 13:52:40 +01:00
|
|
|
def set_default_free_allowance_for_service(service, year_start=None):
|
2021-03-25 08:10:22 +00:00
|
|
|
default_free_sms_fragment_limits = {
|
2023-08-29 14:54:30 -07:00
|
|
|
"federal": {
|
2021-03-25 08:10:22 +00:00
|
|
|
2020: 250_000,
|
|
|
|
|
2021: 150_000,
|
2022-03-14 09:56:09 +00:00
|
|
|
2022: 40_000,
|
2021-03-25 08:10:22 +00:00
|
|
|
},
|
2023-08-29 14:54:30 -07:00
|
|
|
"state": {
|
2021-03-25 08:10:22 +00:00
|
|
|
2020: 250_000,
|
|
|
|
|
2021: 150_000,
|
2022-03-14 09:56:09 +00:00
|
|
|
2022: 40_000,
|
2021-03-25 08:10:22 +00:00
|
|
|
},
|
2023-08-29 14:54:30 -07:00
|
|
|
"other": {
|
2022-09-26 16:47:57 +00:00
|
|
|
2020: 250_000,
|
|
|
|
|
2021: 150_000,
|
|
|
|
|
2022: 40_000,
|
2023-08-29 14:54:30 -07:00
|
|
|
},
|
2021-03-25 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
if not year_start:
|
2023-06-14 13:19:11 -07:00
|
|
|
year_start = get_current_calendar_year_start_year()
|
2021-04-06 13:42:18 +01:00
|
|
|
# handle cases where the year is less than 2020 or greater than 2021
|
2021-03-30 09:08:04 +01:00
|
|
|
if year_start < 2020:
|
|
|
|
|
year_start = 2020
|
2022-03-14 09:56:09 +00:00
|
|
|
if year_start > 2022:
|
|
|
|
|
year_start = 2022
|
2023-07-10 11:06:29 -07:00
|
|
|
if service.organization_type:
|
2023-08-29 14:54:30 -07:00
|
|
|
free_allowance = default_free_sms_fragment_limits[service.organization_type][
|
|
|
|
|
year_start
|
|
|
|
|
]
|
2021-03-25 08:10:22 +00:00
|
|
|
else:
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app.logger.info(
|
|
|
|
|
f"no organization type for service {service.id}. Using other default of "
|
|
|
|
|
f"{default_free_sms_fragment_limits['other'][year_start]}"
|
|
|
|
|
)
|
|
|
|
|
free_allowance = default_free_sms_fragment_limits["other"][year_start]
|
2021-03-25 08:10:22 +00:00
|
|
|
|
2021-04-19 13:29:04 +01:00
|
|
|
return dao_create_or_update_annual_billing_for_year(
|
2023-08-29 14:54:30 -07:00
|
|
|
service.id, free_allowance, year_start
|
2021-03-25 08:10:22 +00:00
|
|
|
)
|