Exemple #1
0
def get_yearly_usage_by_month(service_id):
    try:
        year = int(request.args.get('year'))
        results = []
        for month in get_months_for_financial_year(year):
            billing_for_month = get_monthly_billing_by_notification_type(service_id, month, SMS_TYPE)
            if billing_for_month:
                results.append(_transform_billing_for_month_sms(billing_for_month))
            letter_billing_for_month = get_monthly_billing_by_notification_type(service_id, month, LETTER_TYPE)
            if letter_billing_for_month:
                results.extend(_transform_billing_for_month_letters(letter_billing_for_month))
        return json.dumps(results)

    except TypeError:
        return jsonify(result='error', message='No valid year provided'), 400
Exemple #2
0
def test_get_monthly_billing_by_notification_type_filters_by_type(
        notify_db, notify_db_session):
    service = create_service(service_name="Service One")

    create_monthly_billing_entry(service=service,
                                 monthly_totals=[{
                                     "billing_units": 138,
                                     "rate": 0.0158,
                                     "rate_multiplier": 1,
                                     "total_cost": 2.1804,
                                     "international": None
                                 }],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=SMS_TYPE)

    create_monthly_billing_entry(service=service,
                                 monthly_totals=[],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=EMAIL_TYPE)

    monthly_billing_data = get_monthly_billing_by_notification_type(
        service.id, APR_2016, EMAIL_TYPE)

    _assert_monthly_billing(monthly_billing_data, service.id, 'email',
                            APR_2016_MONTH_START, APR_2016_MONTH_END)
    assert monthly_billing_data.monthly_totals == []
Exemple #3
0
def test_get_monthly_billing_by_notification_type_returns_correct_totals(
        notify_db, notify_db_session):
    service = create_service(service_name="Service One")

    create_monthly_billing_entry(service=service,
                                 monthly_totals=[{
                                     "billing_units": 12,
                                     "rate": 0.0158,
                                     "rate_multiplier": 5,
                                     "total_cost": 2.1804,
                                     "international": False
                                 }],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=SMS_TYPE)

    monthly_billing_data = get_monthly_billing_by_notification_type(
        service.id, APR_2016, SMS_TYPE)

    _assert_monthly_billing(monthly_billing_data, service.id, 'sms',
                            APR_2016_MONTH_START, APR_2016_MONTH_END)
    _assert_monthly_billing_totals(
        monthly_billing_data.monthly_totals[0], {
            "billing_units": 12,
            "rate_multiplier": 5,
            "international": False,
            "rate": 0.0158,
            "total_cost": 2.1804
        })
Exemple #4
0
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_sms(
        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,
        })
Exemple #5
0
def test_transform_billing_for_month_formats_monthly_totals_correctly(
        sample_service):
    create_monthly_billing_entry(service=sample_service,
                                 monthly_totals=[{
                                     "billing_units": 12,
                                     "rate": 0.0158,
                                     "rate_multiplier": 5,
                                     "total_cost": 2.1804,
                                     "international": False
                                 }],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=SMS_TYPE)

    transformed_billing_data = _transform_billing_for_month_sms(
        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': 60,
            'month': 'April',
            'rate': 0.0158,
        })
Exemple #6
0
def test_update_monthly_billing_overwrites_old_totals(sample_template):
    create_rate(APR_2016_MONTH_START, 0.123, SMS_TYPE)
    create_notification(template=sample_template,
                        created_at=APR_2016_MONTH_START,
                        billable_units=1,
                        status='delivered')

    create_or_update_monthly_billing(sample_template.service_id,
                                     APR_2016_MONTH_END)
    first_update = get_monthly_billing_by_notification_type(
        sample_template.service_id, APR_2016_MONTH_START, SMS_TYPE)

    _assert_monthly_billing(first_update, sample_template.service.id, 'sms',
                            APR_2016_MONTH_START, APR_2016_MONTH_END)
    _assert_monthly_billing_totals(
        first_update.monthly_totals[0], {
            "billing_units": 1,
            "rate_multiplier": 1,
            "international": False,
            "rate": 0.123,
            "total_cost": 0.123
        })

    first_updated_at = first_update.updated_at

    with freeze_time(APR_2016_MONTH_START + timedelta(days=3)):
        create_notification(template=sample_template,
                            billable_units=2,
                            status='delivered')
        create_or_update_monthly_billing(sample_template.service_id,
                                         APR_2016_MONTH_END)

    second_update = get_monthly_billing_by_notification_type(
        sample_template.service_id, APR_2016_MONTH_START, SMS_TYPE)

    _assert_monthly_billing_totals(
        second_update.monthly_totals[0], {
            "billing_units": 3,
            "rate_multiplier": 1,
            "international": False,
            "rate": 0.123,
            "total_cost": 0.369
        })

    assert second_update.updated_at == APR_2016_MONTH_START + timedelta(days=3)
    assert first_updated_at != second_update.updated_at
Exemple #7
0
    def populate(service_id, year, month):
        # TODO: generated billing_month should be in UTC but this is untested.
        create_or_update_monthly_billing(service_id,
                                         billing_month=datetime(
                                             year, month, 1))
        sms_res = get_monthly_billing_by_notification_type(
            service_id, datetime(year, month, 1), SMS_TYPE)
        email_res = get_monthly_billing_by_notification_type(
            service_id, datetime(year, month, 1), EMAIL_TYPE)
        letter_res = get_monthly_billing_by_notification_type(
            service_id, datetime(year, month, 1), 'letter')

        print("Finished populating data for {} for service id {}".format(
            month, str(service_id)))
        print('SMS: {}'.format(sms_res.monthly_totals))
        print('Email: {}'.format(email_res.monthly_totals))
        print('Letter: {}'.format(letter_res.monthly_totals))
Exemple #8
0
def test_transform_billing_for_month_returns_empty_if_no_monthly_totals(
        sample_service):
    create_monthly_billing_entry(service=sample_service,
                                 monthly_totals=[],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=SMS_TYPE)

    transformed_billing_data = _transform_billing_for_month_sms(
        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': 0,
            'month': 'April',
            'rate': 0,
        })
Exemple #9
0
def test_get_monthly_billing_by_notification_type_normalises_start_date(
        notify_db, notify_db_session):
    service = create_service(service_name="Service One")

    create_monthly_billing_entry(service=service,
                                 monthly_totals=[{
                                     "billing_units": 321,
                                     "rate": 0.0158,
                                     "rate_multiplier": 1,
                                     "total_cost": 2.1804,
                                     "international": None
                                 }],
                                 start_date=APR_2016_MONTH_START,
                                 end_date=APR_2016_MONTH_END,
                                 notification_type=SMS_TYPE)

    monthly_billing_data = get_monthly_billing_by_notification_type(
        service.id, APR_2016 + timedelta(days=5), SMS_TYPE)

    assert monthly_billing_data.start_date == APR_2016_MONTH_START
    assert monthly_billing_data.monthly_totals[0]['billing_units'] == 321