mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Merge pull request #1867 from alphagov/use-ft-billing-for-usage
Update ft_billing table to use integer data types
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from datetime import datetime, timedelta, time
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import func, case, desc, Date
|
||||
|
||||
from app import db
|
||||
@@ -20,6 +19,31 @@ from app.models import (
|
||||
from app.utils import convert_utc_to_bst, convert_bst_to_utc
|
||||
|
||||
|
||||
def fetch_billing_totals_for_year(service_id, year):
|
||||
year_start_date, year_end_date = get_financial_year(year)
|
||||
|
||||
yearly_data = db.session.query(
|
||||
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
||||
func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"),
|
||||
FactBilling.service_id,
|
||||
FactBilling.rate,
|
||||
FactBilling.notification_type
|
||||
).filter(
|
||||
FactBilling.service_id == service_id,
|
||||
FactBilling.bst_date >= year_start_date,
|
||||
FactBilling.bst_date <= year_end_date
|
||||
).group_by(
|
||||
FactBilling.service_id,
|
||||
FactBilling.rate,
|
||||
FactBilling.notification_type
|
||||
).order_by(
|
||||
FactBilling.service_id,
|
||||
FactBilling.notification_type
|
||||
).all()
|
||||
|
||||
return yearly_data
|
||||
|
||||
|
||||
def fetch_monthly_billing_for_year(service_id, year):
|
||||
year_start_date, year_end_date = get_financial_year(year)
|
||||
utcnow = datetime.utcnow()
|
||||
@@ -97,6 +121,7 @@ def fetch_billing_data_for_day(process_day, service_id=None):
|
||||
)
|
||||
if service_id:
|
||||
transit_data = transit_data.filter(Notification.service_id == service_id)
|
||||
|
||||
return transit_data.all()
|
||||
|
||||
|
||||
@@ -121,7 +146,6 @@ def update_fact_billing(data, process_day):
|
||||
inserted_records = 0
|
||||
updated_records = 0
|
||||
non_letter_rates, letter_rates = get_rates_for_billing()
|
||||
print("process_day: {} {}".format(type(process_day), process_day))
|
||||
update_count = FactBilling.query.filter(
|
||||
FactBilling.bst_date == datetime.date(process_day),
|
||||
FactBilling.template_id == data.template_id,
|
||||
@@ -147,8 +171,6 @@ def update_fact_billing(data, process_day):
|
||||
inserted_records += 1
|
||||
updated_records += update_count
|
||||
db.session.commit()
|
||||
current_app.logger.info('ft_billing for {}: {} rows updated, {} rows inserted'
|
||||
.format(process_day, updated_records, inserted_records))
|
||||
|
||||
|
||||
def create_billing_record(data, rate, process_day):
|
||||
|
||||
Reference in New Issue
Block a user