Merge pull request #1594 from alphagov/vb-free-sms-history

Use annual_billing tables at backend for getting and updating free_sm…
This commit is contained in:
Venus Bailey
2017-11-15 14:57:46 +00:00
committed by GitHub
11 changed files with 186 additions and 63 deletions

View File

@@ -40,17 +40,22 @@ 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
# TODO: Comment out until data migration
# billing_api_client.create_or_update_free_sms_fragment_limit(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

@@ -112,10 +112,13 @@ def template_history(service_id):
def usage(service_id):
year, current_financial_year = requested_and_current_financial_year(request)
free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year(service_id, year)
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,
free_sms_allowance,
billing_api_client.get_billable_units(service_id, year)
)),
selected_year=year,
years=get_tuples_of_financial_years(
@@ -123,7 +126,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),
free_sms_allowance)
)
@@ -287,9 +291,8 @@ def get_dashboard_totals(statistics):
return statistics
def calculate_usage(usage):
# TODO: Don't hardcode these - get em from the API
sms_free_allowance = 250000
def calculate_usage(usage, free_sms_fragment_limit):
sms_free_allowance = free_sms_fragment_limit
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

@@ -40,7 +40,7 @@ from app.main.forms import (
ServiceEditInboundNumberForm,
SMSPrefixForm,
)
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
@@ -90,6 +90,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,
@@ -105,7 +108,9 @@ def service_settings(service_id):
letter_contact_details_count=letter_contact_details_count,
default_sms_sender=default_sms_sender,
sms_sender_count=sms_sender_count,
free_sms_fragment_limit=free_sms_fragment_limit,
prefix_sms_with_service_name=current_service['prefix_sms_with_service_name'],
)
@@ -708,13 +713,18 @@ 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 eventually after using annual_billing
free_sms_fragment_limit=form.free_sms_allowance.data,
)
# TODO: Comment out until data migration
# billing_api_client.create_or_update_free_sms_fragment_limit(service_id, form.free_sms_allowance.data)
return redirect(url_for('.service_settings', service_id=service_id))
return render_template(