2017-08-15 17:26:10 +01:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
from app.dao.date_util import get_current_financial_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(
|
|
|
|
|
'billing',
|
|
|
|
|
__name__,
|
|
|
|
|
url_prefix='/service/<uuid:service_id>/billing'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register_errors(billing_blueprint)
|
|
|
|
|
|
|
|
|
|
|
2018-07-27 14:41:53 +01: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:
|
|
|
|
|
year = int(request.args.get('year'))
|
|
|
|
|
except TypeError:
|
|
|
|
|
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
|
|
|
|
2018-07-27 14:41:53 +01: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:
|
|
|
|
|
year = int(request.args.get('year'))
|
|
|
|
|
except TypeError:
|
|
|
|
|
return jsonify(result='error', message='No valid year provided'), 400
|
|
|
|
|
|
|
|
|
|
billing_data = fetch_billing_totals_for_year(service_id, year)
|
|
|
|
|
data = serialize_ft_billing_yearly_totals(billing_data)
|
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
|
|
|
2017-10-24 13:23:24 +01:00
|
|
|
@billing_blueprint.route('/free-sms-fragment-limit', methods=["GET"])
|
|
|
|
|
def get_free_sms_fragment_limit(service_id):
|
|
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
financial_year_start = request.args.get('financial_year_start')
|
|
|
|
|
|
2017-11-02 17:02:00 +00: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:
|
2021-04-19 13:29:04 +01:00
|
|
|
service = Service.query.get(service_id)
|
|
|
|
|
# An entry does not exist in annual_billing table for that service and year.
|
|
|
|
|
# Set the annual billing to the default free allowance based on the organisation type of the service.
|
|
|
|
|
|
|
|
|
|
annual_billing = set_default_free_allowance_for_service(
|
|
|
|
|
service=service,
|
|
|
|
|
year_start=int(financial_year_start) if financial_year_start else None
|
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
@billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])
|
|
|
|
|
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
|
|
|
|
2017-11-14 16:11:59 +00: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'))
|
|
|
|
|
return jsonify(form), 201
|
|
|
|
|
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2017-12-06 11:03:42 +00:00
|
|
|
def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, financial_year_start):
|
2017-11-02 12:19:17 +00:00
|
|
|
current_year = get_current_financial_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(
|
|
|
|
|
service_id,
|
|
|
|
|
free_sms_fragment_limit,
|
|
|
|
|
financial_year_start
|
|
|
|
|
)
|
|
|
|
|
# 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(
|
2017-12-06 11:03:42 +00:00
|
|
|
service_id,
|
|
|
|
|
free_sms_fragment_limit,
|
|
|
|
|
financial_year_start
|
|
|
|
|
)
|