Use annual_billing tables at backend for getting and updating free_sms_fragment_limit

This commit is contained in:
venusbb
2017-10-31 11:22:57 +00:00
parent e665f35acc
commit 8ea38ba7b6
10 changed files with 161 additions and 17 deletions

View File

@@ -20,7 +20,8 @@ from app.notify_client.models import InvitedUser
from app import (
invite_api_client,
user_api_client,
service_api_client
service_api_client,
billing_api_client
)
from app.utils import (
@@ -40,17 +41,21 @@ def _add_invited_user_to_service(invited_user):
def _create_service(service_name, organisation_type, email_from, form):
free_sms_fragment_limit = current_app.config['DEFAULT_FREE_SMS_FRAGMENT_LIMITS'].get(organisation_type)
try:
service_id = service_api_client.create_service(
service_name=service_name,
organisation_type=organisation_type,
message_limit=current_app.config['DEFAULT_SERVICE_LIMIT'],
free_sms_fragment_limit=current_app.config['DEFAULT_FREE_SMS_FRAGMENT_LIMITS'].get(organisation_type),
free_sms_fragment_limit=free_sms_fragment_limit,
restricted=True,
user_id=session['user_id'],
email_from=email_from,
)
session['service_id'] = service_id
billing_api_client.create_or_update_free_sms_fragment_limit_for_year(service_id, free_sms_fragment_limit)
return service_id, None
except HTTPError as e:
if e.status_code == 400 and e.message['name']:

View File

@@ -115,7 +115,9 @@ def usage(service_id):
return render_template(
'views/usage.html',
months=list(get_free_paid_breakdown_for_billable_units(
year, billing_api_client.get_billable_units(service_id, year)
year,
billing_api_client.get_free_sms_fragment_limit_for_year(service_id, year),
billing_api_client.get_billable_units(service_id, year)
)),
selected_year=year,
years=get_tuples_of_financial_years(
@@ -123,7 +125,8 @@ def usage(service_id):
start=current_financial_year - 1,
end=current_financial_year + 1,
),
**calculate_usage(billing_api_client.get_service_usage(service_id, year))
**calculate_usage(billing_api_client.get_service_usage(service_id, year),
billing_api_client.get_free_sms_fragment_limit_for_year(service_id, year))
)
@@ -287,9 +290,9 @@ def get_dashboard_totals(statistics):
return statistics
def calculate_usage(usage):
def calculate_usage(usage, free_sms_fragment_limit):
# TODO: Don't hardcode these - get em from the API
sms_free_allowance = 250000
sms_free_allowance = free_sms_fragment_limit # VB-Progress
sms_rate = 0 if len(usage) == 0 else usage[0].get("rate", 0)
sms_sent = get_sum_billing_units(breakdown for breakdown in usage if breakdown['notification_type'] == 'sms')
@@ -355,14 +358,14 @@ def get_sum_billing_units(billing_units, month=None):
return sum(b['billing_units'] * b.get('rate_multiplier', 1) for b in billing_units)
def get_free_paid_breakdown_for_billable_units(year, billing_units):
def get_free_paid_breakdown_for_billable_units(year, free_sms_fragment_limit, billing_units):
cumulative = 0
for month in get_months_for_financial_year(year):
previous_cumulative = cumulative
monthly_usage = get_sum_billing_units(billing_units, month)
cumulative += monthly_usage
breakdown = get_free_paid_breakdown_for_month(
cumulative, previous_cumulative,
free_sms_fragment_limit, cumulative, previous_cumulative,
[billing_month for billing_month in billing_units if billing_month['month'] == month]
)
yield {
@@ -373,11 +376,12 @@ def get_free_paid_breakdown_for_billable_units(year, billing_units):
def get_free_paid_breakdown_for_month(
free_sms_fragment_limit,
cumulative,
previous_cumulative,
monthly_usage
):
allowance = 250000
allowance = free_sms_fragment_limit
total_monthly_billing_units = get_sum_billing_units(monthly_usage)

View File

@@ -39,7 +39,7 @@ from app.main.forms import (
FreeSMSAllowance,
ServiceEditInboundNumberForm,
)
from app import user_api_client, current_service, organisations_client, inbound_number_client
from app import user_api_client, current_service, organisations_client, inbound_number_client, billing_api_client
from notifications_utils.formatters import formatted_list
@@ -89,6 +89,9 @@ def service_settings(service_id):
default_sms_sender = next(
(Field(x['sms_sender'], html='escape') for x in sms_senders if x['is_default']), "None"
)
free_sms_fragment_limit = billing_api_client.get_free_sms_fragment_limit_for_year(service_id)
return render_template(
'views/service-settings.html',
organisation=organisation,
@@ -103,7 +106,8 @@ def service_settings(service_id):
default_letter_contact_block=default_letter_contact_block,
letter_contact_details_count=letter_contact_details_count,
default_sms_sender=default_sms_sender,
sms_sender_count=sms_sender_count
sms_sender_count=sms_sender_count,
free_sms_fragment_limit=free_sms_fragment_limit
)
@@ -704,13 +708,16 @@ def set_organisation_type(service_id):
@user_has_permissions(admin_override=True)
def set_free_sms_allowance(service_id):
form = FreeSMSAllowance(free_sms_allowance=current_service['free_sms_fragment_limit'])
form = FreeSMSAllowance(free_sms_allowance=billing_api_client.get_free_sms_fragment_limit_for_year(service_id))
if form.validate_on_submit():
service_api_client.update_service(
service_id,
# TODO: Retire this after new end points are added.
free_sms_fragment_limit=form.free_sms_allowance.data,
)
form.set_free_sms_allowance = \
billing_api_client.create_or_update_free_sms_fragment_limit_for_year(service_id,
form.free_sms_allowance.data)
return redirect(url_for('.service_settings', service_id=service_id))
return render_template(

View File

@@ -1,4 +1,6 @@
from app.notify_client import NotifyAdminAPIClient
from flask import current_app
from notifications_python_client.errors import HTTPError
class BillingAPIClient(NotifyAdminAPIClient):
@@ -23,3 +25,47 @@ class BillingAPIClient(NotifyAdminAPIClient):
'/service/{0}/billing/yearly-usage-summary'.format(service_id),
params=dict(year=year)
)
def get_free_sms_fragment_limit_for_year(self, service_id, year=None):
try:
if year is None:
result = self.get(
'/service/{0}/billing/free-sms-fragment-limit/current-year'.format(service_id)
)
else:
result = self.get(
'/service/{0}/billing/free-sms-fragment-limit'.format(service_id),
params=dict(financial_year_start=year)
)
return result['free_sms_fragment_limit']
except HTTPError:
current_app.logger.info(
'Requested free_sms_fragment_limit entry for service {0} and year {1} does not exist'
.format(service_id, year))
return -1
def get_free_sms_fragment_limit_for_all_years(self, service_id, year=None):
try:
return self.get(
'/service/{0}/billing/free-sms-fragment-limit'.format(service_id),
)
except HTTPError:
current_app.logger.info(
'No free_sms_fragment_limit entry exists for service {0} '
.format(service_id, year))
return []
def create_or_update_free_sms_fragment_limit_for_year(self, service_id, free_sms_fragment_limit, year=None):
if year is None:
data = {
"free_sms_fragment_limit": free_sms_fragment_limit,
}
else:
data = {
"financial_year_start": year,
"free_sms_fragment_limit": free_sms_fragment_limit,
}
return self.post(
url='/service/{0}/billing/free-sms-fragment-limit'.format(service_id),
data=data
)

View File

@@ -194,7 +194,7 @@
{% endcall %}
{% call row() %}
{{ text_field('Free text message allowance')}}
{{ text_field('{:,}'.format(current_service.free_sms_fragment_limit or 0)) }}
{{ text_field('{:,}'.format(free_sms_fragment_limit or 0)) }}
{{ edit_field('Change', url_for('.set_free_sms_allowance', service_id=current_service.id)) }}
{% endcall %}
{% call row() %}