Forecasts, targets and reports in Horilla CRM all depend on one idea: which fiscal year, quarter and period is “now”. If that becomes outdated, for example, when a new year starts but the old one is still marked as current, forecast screens may show the wrong period.
The update_fiscal_year management command keeps these flags accurate and ensures the next fiscal year is created in advance. This post explains what the command does, how it runs automatically, and when to run it manually.
Usage
python manage.py update_fiscal_year
The command takes no arguments and checks every company’s fiscal year configuration.
Background: Configuration vs. Instances
Horilla CRM separates fiscal years into two parts:
- Configuration (FiscalYear): Set once per company. It stores the fiscal year type (standard or custom), start month and day, format (quarter-based or year-based), and whether the year name is based on its starting or ending year.
- Instances (FiscalYearInstance): The actual fiscal years generated from that configuration, such as “FY 2026”. Each instance contains its own quarters (Quarter) and periods (Period), with an is_current flag at every level.
The update_fiscal_year command reads the configuration and keeps the generated instances synchronized with the current date.
What the Command Does
For each company’s fiscal year configuration, the service behind the command (FiscalYearService.check_and_update_fiscal_years) performs the following steps:
- Finds the current fiscal year: Looks for the instance marked with is_current. If no current year exists for the configuration, it skips that configuration.
- Checks whether the current year has ended: If today’s date is past its end date, the command removes the current flag from the old year.
- Moves to the next fiscal year: If the next year already exists, it marks it as current. Otherwise, it creates the next year, including its quarters and periods, and marks it as current.
- Creates the following year in advance: Even during the middle of a fiscal year, the service ensures that the next year’s calendar is available for planning.
- Refreshes quarter and period flags: Within the current year, it marks exactly one quarter and one period as current based on today’s date and clears the flags from the others. If today’s date does not fall within any quarter, all quarter flags are cleared.
Reading the Output
The command prints one line for each change:
Updated current fiscal year to FY 2027
Created new fiscal year: FY 2027
Created new fiscal year: FY 2028
If no changes are required, it prints:
All fiscal years are up to date
The “Created” messages indicate that new fiscal years were generated. The “Updated” message indicates that the current-year flag was moved to another year.
When a fiscal year ends, you will typically see an update message for the new current year and a creation message for the following year.
How Dates Are Calculated
The command uses the company’s fiscal year configuration to calculate the start and end dates.
- Standard fiscal years: Run for exactly one calendar year. For example, a year starting on 1 January ends on 31 December.
- Custom fiscal years: Run for one year minus one extra day, which supports calendars such as 52-week fiscal years.
- Next-year start date: Every new fiscal year starts on the day after the previous year ends, avoiding gaps or overlaps.
- Standard quarters: Each quarter covers three months.
- Quarter-based custom formats: Use 13-week quarters, with periods following the 4-4-5, 4-5-4 or 5-4-4 week patterns.
- Year-based formats: Use four-week periods.
- Fiscal year names: Depend on the configuration. For example, FY 2026 can represent a year named after its starting year or, depending on the setting, its ending year.
It Already Runs Automatically
In most cases, you do not need to run this command manually.
There are two places where Horilla keeps fiscal year information updated:
1. Background Scheduler
The scheduler in horilla/contrib/core/scheduler.py starts a background APScheduler job that runs update_fiscal_year every 12 hours.
The same scheduler also clears expired recycle bin records every four hours.
2. Forecast Screens
The forecast dashboard and forecast type tab call the same service method for the current company when they load. This ensures that users see the correct fiscal year and period even if the background scheduler has not run yet.
When to Run It Manually
Although the command runs automatically, there are situations where running it manually is useful:
- After deployment or database restoration: Ensures fiscal year flags are correct before users start working.
- When the background scheduler is not running: Useful in environments where only web workers are started.
- After changing the system date: Helps synchronize fiscal year information with the updated date.
- In test environments: Useful after loading test data or modifying dates.
- From cron or deployment scripts: Can be added as an additional safeguard alongside the built-in scheduler.
Good to Know
- Safe to run repeatedly: If the next fiscal year already exists, the service reuses it instead of creating a duplicate. Running the command again without any relevant changes does not create additional records.
- Does not rebuild existing years: Changes to a company’s fiscal year configuration are handled through the settings screen, which regenerates the fiscal years. This command only handles rolling the calendar forward.
- Companies without a current year are skipped: If a configuration has no instance marked as current, the command leaves it unchanged. Initial fiscal year records must be generated through the settings screen first.
- Forecasts follow the updated periods: Forecast periods are linked to fiscal year periods, so keeping the current period accurate ensures that forecast screens display the correct information. After a fiscal year rollover, you may also need to run recalculate_forecasts.
The update_fiscal_year management command keeps each company’s fiscal calendar up to date. It retires completed fiscal years, activates the next year, creates future fiscal years in advance, and maintains the current-quarter and current-period flags.
Horilla runs this command automatically every 12 hours, but you can also execute it manually whenever you need to synchronize fiscal year information.