diff --git a/app/commands.py b/app/commands.py index 3f27237a9..a499f48f8 100644 --- a/app/commands.py +++ b/app/commands.py @@ -807,9 +807,27 @@ def populate_annual_billing_with_defaults(year, missing_services_only): active_services = Service.query.filter( Service.active ).all() + previous_year = year - 1 + services_with_zero_free_allowance = db.session.query(AnnualBilling.service_id).filter( + AnnualBilling.financial_year_start == previous_year, + AnnualBilling.free_sms_fragment_limit == 0 + ).all() for service in active_services: - set_default_free_allowance_for_service(service, year) + + # If a service has free_sms_fragment_limit for the previous year + # set the free allowance for this year to 0 as well. + # Else use the default free allowance for the service. + if service.id in [x.service_id for x in services_with_zero_free_allowance]: + print(f'update service {service.id} to 0') + dao_create_or_update_annual_billing_for_year( + service_id=service.id, + free_sms_fragment_limit=0, + financial_year_start=year + ) + else: + print(f'update service {service.id} with default') + set_default_free_allowance_for_service(service, year) @click.option('-u', '--user-id', required=True) diff --git a/tests/app/test_commands.py b/tests/app/test_commands.py index 4d3f991c8..16b21116a 100644 --- a/tests/app/test_commands.py +++ b/tests/app/test_commands.py @@ -1,10 +1,14 @@ +import pytest + from app.commands import ( insert_inbound_numbers_from_file, local_dev_broadcast_permissions, + populate_annual_billing_with_defaults, ) from app.dao.inbound_numbers_dao import dao_get_available_inbound_numbers from app.dao.services_dao import dao_add_user_to_service -from tests.app.db import create_user +from app.models import AnnualBilling +from tests.app.db import create_annual_billing, create_service, create_user def test_insert_inbound_numbers_from_file(notify_db_session, notify_api, tmpdir): @@ -36,3 +40,43 @@ def test_local_dev_broadcast_permissions( assert len(user.get_permissions(sample_service.id)) == 0 assert len(user.get_permissions(sample_broadcast_service.id)) > 0 + + +@pytest.mark.parametrize("organisation_type, expected_allowance", + [('central', 40000), + ('local', 20000), + ('nhs_gp', 10000)]) +def test_populate_annual_billing_with_defaults( + notify_db_session, notify_api, organisation_type, expected_allowance +): + service = create_service(service_name=organisation_type, organisation_type=organisation_type) + + notify_api.test_cli_runner().invoke( + populate_annual_billing_with_defaults, ['-y', 2022] + ) + + results = AnnualBilling.query.filter( + AnnualBilling.financial_year_start == 2022, + AnnualBilling.service_id == service.id + ).all() + + assert len(results) == 1 + assert results[0].free_sms_fragment_limit == expected_allowance + + +def test_populate_annual_billing_with_defaults_sets_free_allowance_to_zero_if_previous_year_is_zero( + notify_db_session, notify_api +): + service = create_service(organisation_type='central') + create_annual_billing(service_id=service.id, free_sms_fragment_limit=0, financial_year_start=2021) + notify_api.test_cli_runner().invoke( + populate_annual_billing_with_defaults, ['-y', 2022] + ) + + results = AnnualBilling.query.filter( + AnnualBilling.financial_year_start == 2022, + AnnualBilling.service_id == service.id + ).all() + + assert len(results) == 1 + assert results[0].free_sms_fragment_limit == 0