Skip to content

Commit

Permalink
DWPF-1767 Improve new_finance_year command (#505)
Browse files Browse the repository at this point in the history
Whilst trying to run the `new_finance_year` command, we ran into some
bad data that had made it's way into the database. What we found was a
used financial code with a NAC that had an invalid economic budget code.
To resolve the issue we fixed the data by changing the NAC's economic
budget code.

This commit extends the `new_finance_year` command to detect this issue
before changing the financial year. It also serves to document the issue
we faced.
  • Loading branch information
SamDudley authored May 28, 2024
1 parent d3004cb commit 1fa39b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion forecast/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FinancialCodeAdmin(AdminReadOnly):
("programme", RelatedDropdownFilter),
("analysis1_code", RelatedDropdownFilter),
("analysis2_code", RelatedDropdownFilter),
("project_code,", RelatedDropdownFilter),
("project_code", RelatedDropdownFilter),
)

def get_readonly_fields(self, request, obj=None):
Expand Down
31 changes: 31 additions & 0 deletions forecast/management/commands/new_financial_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
get_current_financial_year,
get_year_display,
)
from forecast.models import FinancialCode
from forecast.utils.import_helpers import VALID_ECONOMIC_CODE_LIST


class Command(CommandWithUserCheck):
Expand All @@ -27,6 +29,11 @@ def run_command(self, message, command_name, *arg, **options):
return True

def handle_user(self, *args, **options):
try:
pre_new_financial_year_checks()
except NewFinancialYearError as err:
raise CommandError(str(err)) from err

current_financial_year = get_current_financial_year()
current_financial_year_display = get_year_display(current_financial_year)
new_financial_year = current_financial_year + 1
Expand Down Expand Up @@ -68,3 +75,27 @@ def handle_user(self, *args, **options):
self.stdout.write(
self.style.SUCCESS(f"FFT ready for {new_financial_year_display} ")
)


class NewFinancialYearError(Exception):
pass


def pre_new_financial_year_checks() -> None:
"""Pre-flight checks for changing to a new financial year.
This function will raise `NewFinancialYearError` if any issues are found.
Checks:
- Look for used NACs with invalid economic budget codes.
"""
# It's possible this needs to be scoped to a financial year in the future.
problem_nacs = FinancialCode.objects.exclude(
natural_account_code__economic_budget_code__in=VALID_ECONOMIC_CODE_LIST
)

if bool(problem_nacs):
problem_nac_ids = [str(nac.natural_account_code) for nac in problem_nacs]
raise NewFinancialYearError(
f"Possible problem NACs found: {", ".join(problem_nac_ids)}"
)

0 comments on commit 1fa39b6

Please sign in to comment.