From 926eb5b48f3cbe2e9c2c6860e56bcf6ca234c3cb Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 16 Mar 2022 15:54:52 +0000 Subject: [PATCH] Update app.commands.populate_annual_billing_with_defaults so that if the service has zero free allowance in the previous year, set this year to zero as well. --- app/commands.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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)