Add join to LetterRates for the billing query.

Fix the yearly totals to work for letters.
This commit is contained in:
Rebecca Law
2017-12-18 16:46:59 +00:00
parent ec28f48968
commit c4d869175a
3 changed files with 68 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from sqlalchemy import Float, Integer
from sqlalchemy import Float, Integer, and_
from sqlalchemy import func, case, cast
from sqlalchemy import literal_column
@@ -156,12 +156,18 @@ def billing_letter_data_per_month_query(service_id, start_date, end_date):
NotificationHistory.international,
NotificationHistory.notification_type,
cast(LetterRate.rate, Float()).label('rate')
).join(
LetterRate,
and_(NotificationHistory.created_at >= LetterRate.start_date,
(LetterRate.end_date == None) | # noqa
(LetterRate.end_date > NotificationHistory.created_at))
).filter(
*billing_data_filter(LETTER_TYPE, start_date, end_date, service_id),
LetterRate.sheet_count == NotificationHistory.billable_units,
LetterRate.crown == crown,
NotificationHistory.created_at.between(LetterRate.start_date, end_date),
LetterRate.post_class == 'second'
LetterRate.post_class == 'second',
NotificationHistory.created_at < end_date,
).group_by(
NotificationHistory.notification_type,
month,