2017-08-15 17:26:10 +01:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
2024-11-18 09:26:04 -08:00
|
|
|
from app import db
|
2018-05-08 12:09:29 +01:00
|
|
|
from app.billing.billing_schemas import (
|
|
|
|
|
create_or_update_free_sms_fragment_limit_schema,
|
2018-05-11 16:25:16 +01:00
|
|
|
serialize_ft_billing_remove_emails,
|
|
|
|
|
serialize_ft_billing_yearly_totals,
|
2018-05-08 12:09:29 +01:00
|
|
|
)
|
|
|
|
|
from app.dao.annual_billing_dao import (
|
|
|
|
|
dao_create_or_update_annual_billing_for_year,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_get_free_sms_fragment_limit_for_year,
|
|
|
|
|
dao_update_annual_billing_for_future_years,
|
2021-04-19 13:29:04 +01:00
|
|
|
set_default_free_allowance_for_service,
|
2018-05-08 12:09:29 +01:00
|
|
|
)
|
2023-06-14 13:19:11 -07:00
|
|
|
from app.dao.date_util import get_current_calendar_year_start_year
|
2019-08-20 10:57:20 +01:00
|
|
|
from app.dao.fact_billing_dao import (
|
2021-03-10 13:55:06 +00:00
|
|
|
fetch_billing_totals_for_year,
|
|
|
|
|
fetch_monthly_billing_for_year,
|
2019-08-20 10:57:20 +01:00
|
|
|
)
|
2021-04-19 13:29:04 +01:00
|
|
|
from app.errors import register_errors
|
|
|
|
|
from app.models import Service
|
2017-10-24 13:23:24 +01:00
|
|
|
from app.schema_validation import validate
|
2017-08-15 17:26:10 +01:00
|
|
|
|
|
|
|
|
billing_blueprint = Blueprint(
|
2023-08-29 14:54:30 -07:00
|
|
|
"billing", __name__, url_prefix="/service/<uuid:service_id>/billing"
|
2017-08-15 17:26:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register_errors(billing_blueprint)
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@billing_blueprint.route("/monthly-usage")
|
2018-04-30 16:34:40 +01:00
|
|
|
def get_yearly_usage_by_monthly_from_ft_billing(service_id):
|
2018-04-27 15:15:55 +01:00
|
|
|
try:
|
2023-08-29 14:54:30 -07:00
|
|
|
year = int(request.args.get("year"))
|
2018-04-27 15:15:55 +01:00
|
|
|
except TypeError:
|
2023-08-29 14:54:30 -07:00
|
|
|
return jsonify(result="error", message="No valid year provided"), 400
|
2018-04-30 16:34:40 +01:00
|
|
|
results = fetch_monthly_billing_for_year(service_id=service_id, year=year)
|
2018-05-08 12:09:29 +01:00
|
|
|
data = serialize_ft_billing_remove_emails(results)
|
2018-05-04 13:09:14 +01:00
|
|
|
return jsonify(data)
|
2018-04-30 16:34:40 +01:00
|
|
|
|
2018-04-27 15:15:55 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@billing_blueprint.route("/yearly-usage-summary")
|
2018-05-11 16:25:16 +01:00
|
|
|
def get_yearly_billing_usage_summary_from_ft_billing(service_id):
|
|
|
|
|
try:
|
2023-08-29 14:54:30 -07:00
|
|
|
year = int(request.args.get("year"))
|
2018-05-11 16:25:16 +01:00
|
|
|
except TypeError:
|
2023-08-29 14:54:30 -07:00
|
|
|
return jsonify(result="error", message="No valid year provided"), 400
|
2018-05-11 16:25:16 +01:00
|
|
|
|
|
|
|
|
billing_data = fetch_billing_totals_for_year(service_id, year)
|
|
|
|
|
data = serialize_ft_billing_yearly_totals(billing_data)
|
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@billing_blueprint.route("/free-sms-fragment-limit", methods=["GET"])
|
2017-10-24 13:23:24 +01:00
|
|
|
def get_free_sms_fragment_limit(service_id):
|
2023-08-29 14:54:30 -07:00
|
|
|
financial_year_start = request.args.get("financial_year_start")
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
annual_billing = dao_get_free_sms_fragment_limit_for_year(
|
|
|
|
|
service_id, financial_year_start
|
|
|
|
|
)
|
2017-10-26 17:21:35 +01:00
|
|
|
|
2017-11-02 17:02:00 +00:00
|
|
|
if annual_billing is None:
|
2024-11-18 09:26:04 -08:00
|
|
|
service = db.session.get(Service, service_id)
|
2021-04-19 13:29:04 +01:00
|
|
|
# An entry does not exist in annual_billing table for that service and year.
|
2023-07-10 11:06:29 -07:00
|
|
|
# Set the annual billing to the default free allowance based on the organization type of the service.
|
2021-04-19 13:29:04 +01:00
|
|
|
|
|
|
|
|
annual_billing = set_default_free_allowance_for_service(
|
|
|
|
|
service=service,
|
2023-08-29 14:54:30 -07:00
|
|
|
year_start=int(financial_year_start) if financial_year_start else None,
|
2021-04-19 13:29:04 +01:00
|
|
|
)
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2017-11-02 17:02:00 +00:00
|
|
|
return jsonify(annual_billing.serialize_free_sms_items()), 200
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@billing_blueprint.route("/free-sms-fragment-limit", methods=["POST"])
|
2017-10-24 13:23:24 +01:00
|
|
|
def create_or_update_free_sms_fragment_limit(service_id):
|
2017-11-02 12:19:17 +00:00
|
|
|
req_args = request.get_json()
|
2017-10-26 17:21:41 +01:00
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
form = validate(req_args, create_or_update_free_sms_fragment_limit_schema)
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
update_free_sms_fragment_limit_data(
|
|
|
|
|
service_id,
|
|
|
|
|
free_sms_fragment_limit=form.get("free_sms_fragment_limit"),
|
|
|
|
|
financial_year_start=form.get("financial_year_start"),
|
|
|
|
|
)
|
2017-11-14 16:11:59 +00:00
|
|
|
return jsonify(form), 201
|
|
|
|
|
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def update_free_sms_fragment_limit_data(
|
|
|
|
|
service_id, free_sms_fragment_limit, financial_year_start
|
|
|
|
|
):
|
2023-06-14 13:19:11 -07:00
|
|
|
current_year = get_current_calendar_year_start_year()
|
2017-12-06 11:03:42 +00:00
|
|
|
if not financial_year_start:
|
|
|
|
|
financial_year_start = current_year
|
|
|
|
|
|
|
|
|
|
dao_create_or_update_annual_billing_for_year(
|
2023-08-29 14:54:30 -07:00
|
|
|
service_id, free_sms_fragment_limit, financial_year_start
|
2017-12-06 11:03:42 +00:00
|
|
|
)
|
|
|
|
|
# if we're trying to update historical data, don't touch other rows.
|
|
|
|
|
# Otherwise, make sure that future years will get the new updated value.
|
|
|
|
|
if financial_year_start >= current_year:
|
2017-12-06 14:41:32 +00:00
|
|
|
dao_update_annual_billing_for_future_years(
|
2023-08-29 14:54:30 -07:00
|
|
|
service_id, free_sms_fragment_limit, financial_year_start
|
2017-12-06 11:03:42 +00:00
|
|
|
)
|