mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Update letter data for usage-for-all-services report
Usage for all services is a platform admin report that groups letters by postage. We want it to show `europe` and `rest-of-world` letters under a single category of `international`, so this updates the query to do that and to order appropriately.
This commit is contained in:
@@ -4,7 +4,7 @@ from flask import current_app
|
||||
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy import func, desc, Date, Integer, and_
|
||||
from sqlalchemy.sql.expression import literal
|
||||
from sqlalchemy.sql.expression import case, literal
|
||||
|
||||
from app import db
|
||||
from app.dao.date_util import (
|
||||
@@ -28,6 +28,7 @@ from app.models import (
|
||||
NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS,
|
||||
AnnualBilling,
|
||||
Organisation,
|
||||
INTERNATIONAL_POSTAGE_TYPES,
|
||||
)
|
||||
from app.utils import get_london_midnight_in_utc, get_notification_table_to_use
|
||||
|
||||
@@ -146,13 +147,21 @@ def fetch_letter_costs_for_all_services(start_date, end_date):
|
||||
|
||||
|
||||
def fetch_letter_line_items_for_all_services(start_date, end_date):
|
||||
formatted_postage = case(
|
||||
[(FactBilling.postage.in_(INTERNATIONAL_POSTAGE_TYPES), "international")], else_=FactBilling.postage
|
||||
).label("postage")
|
||||
|
||||
postage_order = case(((formatted_postage == "second", 1),
|
||||
(formatted_postage == "first", 2),
|
||||
(formatted_postage == "international", 3)))
|
||||
|
||||
query = db.session.query(
|
||||
Organisation.name.label("organisation_name"),
|
||||
Organisation.id.label("organisation_id"),
|
||||
Service.name.label("service_name"),
|
||||
Service.id.label("service_id"),
|
||||
FactBilling.rate.label("letter_rate"),
|
||||
FactBilling.postage.label("postage"),
|
||||
formatted_postage,
|
||||
func.sum(FactBilling.notifications_sent).label("letters_sent"),
|
||||
).select_from(
|
||||
Service
|
||||
@@ -170,11 +179,11 @@ def fetch_letter_line_items_for_all_services(start_date, end_date):
|
||||
Service.id,
|
||||
Service.name,
|
||||
FactBilling.rate,
|
||||
FactBilling.postage
|
||||
formatted_postage
|
||||
).order_by(
|
||||
Organisation.name,
|
||||
Service.name,
|
||||
FactBilling.postage.desc(),
|
||||
postage_order,
|
||||
FactBilling.rate,
|
||||
)
|
||||
return query.all()
|
||||
|
||||
@@ -1356,6 +1356,8 @@ SECOND_CLASS = 'second'
|
||||
EUROPE = 'europe'
|
||||
REST_OF_WORLD = 'rest-of-world'
|
||||
POSTAGE_TYPES = [FIRST_CLASS, SECOND_CLASS, EUROPE, REST_OF_WORLD]
|
||||
UK_POSTAGE_TYPES = [FIRST_CLASS, SECOND_CLASS]
|
||||
INTERNATIONAL_POSTAGE_TYPES = [EUROPE, REST_OF_WORLD]
|
||||
RESOLVE_POSTAGE_FOR_FILE_NAME = {
|
||||
FIRST_CLASS: 1,
|
||||
SECOND_CLASS: 2,
|
||||
|
||||
@@ -9,6 +9,7 @@ from app.dao.fact_billing_dao import (
|
||||
)
|
||||
from app.dao.fact_notification_status_dao import fetch_notification_status_totals_for_all_services
|
||||
from app.errors import register_errors, InvalidRequest
|
||||
from app.models import UK_POSTAGE_TYPES
|
||||
from app.platform_stats.platform_stats_schema import platform_stats_request
|
||||
from app.service.statistics import format_admin_stats
|
||||
from app.schema_validation import validate
|
||||
@@ -65,7 +66,8 @@ def get_usage_for_all_services():
|
||||
letter_breakdown = fetch_letter_line_items_for_all_services(start_date, end_date)
|
||||
|
||||
lb_by_service = [
|
||||
(lb.service_id, "{} {} class letters at {}p".format(lb.letters_sent, lb.postage, int(lb.letter_rate * 100)))
|
||||
(lb.service_id,
|
||||
f"{lb.letters_sent} {postage_description(lb.postage)} letters at {format_letter_rate(lb.letter_rate)}")
|
||||
for lb in letter_breakdown
|
||||
]
|
||||
combined = {}
|
||||
@@ -106,3 +108,17 @@ def get_usage_for_all_services():
|
||||
x['organisation_name'],
|
||||
x['service_name']
|
||||
)))
|
||||
|
||||
|
||||
def postage_description(postage):
|
||||
if postage in UK_POSTAGE_TYPES:
|
||||
return f'{postage} class'
|
||||
else:
|
||||
return 'international'
|
||||
|
||||
|
||||
def format_letter_rate(number):
|
||||
if number >= 1:
|
||||
return f"£{number:,.2f}"
|
||||
|
||||
return f"{number * 100:.0f}p"
|
||||
|
||||
Reference in New Issue
Block a user