Aggregate monthly totals from billing data

This commit is contained in:
Imdad Ahad
2017-08-18 16:12:01 +01:00
parent 58baa9919c
commit e05160bdfb
2 changed files with 79 additions and 21 deletions

View File

@@ -74,19 +74,15 @@ def _get_total_billable_units_and_rate_for_notification_type(billing_data, noti_
def _transform_billing_for_month(billing_for_month): def _transform_billing_for_month(billing_for_month):
month_name = datetime.strftime(convert_utc_to_bst(billing_for_month.start_date), "%B") month_name = datetime.strftime(convert_utc_to_bst(billing_for_month.start_date), "%B")
billing_units = rate = rate_multiplier = international = 0 billing_units = rate = 0
if billing_for_month.monthly_totals: for total in billing_for_month.monthly_totals:
billing_units = billing_for_month.monthly_totals[0]['billing_units'] billing_units += (total['billing_units'] * total['rate_multiplier'])
rate = billing_for_month.monthly_totals[0]['rate'] rate = total['rate']
rate_multiplier = billing_for_month.monthly_totals[0]['rate_multiplier']
international = billing_for_month.monthly_totals[0]['international']
return { return {
"month": month_name, "month": month_name,
"billing_units": billing_units, "billing_units": billing_units,
"rate_multiplier": rate_multiplier,
"international": bool(international),
"notification_type": billing_for_month.notification_type, "notification_type": billing_for_month.notification_type,
"rate": rate "rate": rate
} }

View File

@@ -121,21 +121,17 @@ def test_get_yearly_usage_by_month_returns_correctly(client, sample_template):
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
_assert_dict_equals(resp_json[0], { _assert_dict_equals(resp_json[0], {
'billing_units': 1, 'billing_units': 2,
'international': False,
'month': 'May', 'month': 'May',
'notification_type': SMS_TYPE, 'notification_type': SMS_TYPE,
'rate': 0.12, 'rate': 0.12
'rate_multiplier': 2
}) })
_assert_dict_equals(resp_json[1], { _assert_dict_equals(resp_json[1], {
'billing_units': 2, 'billing_units': 6,
'international': False,
'month': 'June', 'month': 'June',
'notification_type': SMS_TYPE, 'notification_type': SMS_TYPE,
'rate': 0.12, 'rate': 0.12
'rate_multiplier': 3
}) })
@@ -156,8 +152,6 @@ def test_transform_billing_for_month_returns_empty_if_no_monthly_totals(sample_s
'notification_type': SMS_TYPE, 'notification_type': SMS_TYPE,
'billing_units': 0, 'billing_units': 0,
'month': 'April', 'month': 'April',
'international': False,
'rate_multiplier': 0,
'rate': 0, 'rate': 0,
}) })
@@ -183,9 +177,77 @@ def test_transform_billing_for_month_formats_monthly_totals_correctly(sample_ser
_assert_dict_equals(transformed_billing_data, { _assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE, 'notification_type': SMS_TYPE,
'billing_units': 12, 'billing_units': 60,
'month': 'April', 'month': 'April',
'international': False,
'rate_multiplier': 5,
'rate': 0.0158, 'rate': 0.0158,
}) })
def test_transform_billing_sums_billable_units(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[{
'billing_units': 1321,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}, {
'billing_units': 1,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 1322,
'month': 'April',
'rate': 0.12,
})
def test_transform_billing_calculates_with_different_rate_multipliers(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[{
'billing_units': 1321,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}, {
'billing_units': 1,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 3
}],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 1324,
'month': 'April',
'rate': 0.12,
})