Fiscal periods and lock statuses
Status: implemented. Design: ADR 0048 D3 R2 (period records), ADR 0147 D2, D7 and D8 (one calendar with an
accounting and a stock status, reopening, the two-step locks). Behaviour: docs/specs/accounting/locking.md
(SPEC-ACCLOCK-023, -099 to -103). Code: addons/account/src/fiscal/.
The reference keeps lock dates on the company and compares every date with them. ERPForAll keeps those company
dates for display and for their setters, and stores one account.fiscal.period row per company and month: the row’s
statuses are what writers decide on, because only a row can be locked and drained safely (ADR 0147 D8). The stock
addon never reads this table; stock asks through stock_account (ADR 0147 G5).
Period rows
Section titled “Period rows”| Column | Meaning |
|---|---|
company_id, fiscal_year, kind, number | unique together; fiscal_year is the year of the fiscal year’s last day; kind is regular (numbers 1 to 12) or adjustment (13 to 16, one day long, created by the year close) |
date_from, date_to | the range, both days included; regular periods of a company never overlap (an exclusion constraint) |
status | open, closed (fiscal-year lock), hard_locking, hard_locked; a CHECK refuses any other value, and period_admission returns an internal error naming the period for a value it does not know (never Open) |
stock_lock_status | open, stock_locking, stock_locked (a CHECK); a hard_locked period is always stock_locked (a CHECK) |
*_started_by, *_started_at | set exactly while the matching lock is in progress (a CHECK) |
stock_lock_generation, hard_lock_generation | run counters of the two locks |
closed_at, hard_locked_at | when the period left open, when it reached hard_locked |
The record API refuses create, write and delete, also with elevated rights; the readonly accounting group reads the periods of its companies. No default rows ship: periods are created when something needs them.
The company has fiscalyear_lock_date, tax_lock_date, sale_lock_date, purchase_lock_date,
hard_lock_date, stock_lock_date, and the internal counter fiscal_period_serial. The three specialized
soft dates default to unset. Hard and stock dates move through their owning lock operations, never a
generic company write.
Creating periods: ensure_periods
Section titled “Creating periods: ensure_periods”use erp_addon_account::fiscal::ensure_periods;// `caller`: the BeginOptions of the unit that needs the periods (its principal and context)let created: u64 = ensure_periods(&orm, caller, company_id, date_from, date_to).await?;It creates the missing regular periods containing a day of the range for the company’s root and every branch, from
the root’s fiscal calendar, and returns the number of rows created. Call it outside the unit of work that needs the
periods (before its first statement, or after it failed for a missing period and before it is restarted): it runs in
its own short transactions as caller (the settings read, then the insert at READ COMMITTED), so the new rows are
visible to the restarted unit. It is idempotent; two concurrent calls
for the same month both succeed. Statuses of new rows are derived from the company lock dates, the latest over the
company and its ancestors. A date beyond the horizon setting, a range needing more than 1,200 periods, or a fiscal
year end that no longer matches the existing periods is refused by name. ensure_periods_with takes a calendar read
once with FiscalCalendar::read (or built with FiscalCalendar::new, which checks the horizon bounds), for imports
that ensure many ranges.
The fiscal calendar settings
Section titled “The fiscal calendar settings”Company settings, block “Fiscal periods” of the Accounting settings page, read from the root company:
| Key | Default | Bounds |
|---|---|---|
account.fiscalyear_last_day | 31 | 1 to 31, and a day the month has (29 February accepted) |
account.fiscalyear_last_month | December | January to December |
account.fiscal_period_horizon_months | 12 | 1 to 120 |
Periods are one month long (ADR 0048 D3 R2). A fiscal year ending on the 15th has periods from the 16th to the 15th;
a February end of 28 or 29 means the month end. Changing the fiscal year end after periods exist makes the next
ensure_periods that would overlap them fail loudly.
The day and the month are checked together on every settings write (the setting group account.fiscal_year_end):
setting February while the day stays 31 is refused, naming both keys. A branch shows the block too but its values are
not used; it follows its root company. Hiding the block at branch scope needs a scope the settings framework does not
have yet.
Before writing a dated row: period_admission
Section titled “Before writing a dated row: period_admission”use erp_addon_account::fiscal::{WriteClass, period_admission, require_open_periods};let verdicts = period_admission(tx, WriteClass::Accounting, &[(company_id, date)]).await?;let period_ids = require_open_periods(tx, WriteClass::Stock, &rows).await?; // refuses every lockOne verdict per (company, date): Open, NoPeriod (run ensure_periods, restart), FiscalLock (an accounting
writer applies its re-dating policy, planned with posting; a stock writer refuses), HardLock (everyone refuses), StockLock
(stock class only). Accounting writers ignore the stock status. The call takes the shared advisory lock
(PERIOD_LOCK_CLASS, period_id) of each period first, so a lock operation’s T1 waits for in-flight writers and its
drain covers the rest. Call it once per batch at the start of the writer’s unit of work. Sale, purchase and tax soft
locks, exceptions and the backdating window are not period-row questions; the lock and posting work adds them.
Status operations: status
Section titled “Status operations: status”The status operations all require account.group_account_manager and take the exclusive advisory locks of
the periods they may change, in id order.
Who may act on which company (SPEC-ACCLOCK-104). Outside superuser mode every period operation, ensure_periods
and period_admission act only on a company in the caller’s active companies (Transaction::companies) or on a
branch below one: a branch follows its parent’s lock dates, as in the reference
(odoo/addons/account/models/company.py:572-577). Anything else is refused with Access (“One of the selected companies does not exist or is not one of yours.”), also for a period or company id that does not exist, so the answer does not tell whether it
exists. Superuser mode is not limited.
What the synchronisation waits for (SPEC-ACCLOCK-105). sync_lock_dates first takes the exclusive advisory lock of
every period whose status it may change, before its refusal checks and its updates. A writer that was admitted for
such a period (it holds the shared lock) commits first; a writer that asks after the synchronisation committed sees
the new status. The wait is bounded by the transaction’s lock_timeout (OrmConfig::timeouts, or
BeginOptions::timeouts for the unit); the lock operations’ own SET LOCAL lock_timeout is planned. A writer
whose transaction already read the period before the synchronisation committed (a repeatable-read snapshot) still
sees the old status: for the hard lock the drain of the two-step run covers it; for the fiscal-year lock nothing does
yet, and the case is left to the lock-date setters (planned). A synchronisation that would change more than 1,200
periods per company of the tree at once is refused; move the lock date in smaller steps.
Same transaction, one lock order. sync_lock_dates flushes the transaction first, so a setter may write the lock
date through the record API and call it right after. Every path takes its locks in one order: the target company row,
then the root company row (fiscal_period_serial), then the period advisory locks in id order; the second step of a
lock run takes the two company rows FOR UPDATE first, so a setter and a lock run on the same company wait for each
other instead of deadlocking. That second step needs a READ COMMITTED transaction
(BeginOptions::isolation(Isolation::ReadCommitted)); in a REPEATABLE READ one it is refused with Programming.
sync_lock_dates(tx, company_id): after a setter wrote a company lock date, closes and reopens periods to match, and reopens the stock status when the stock lock date was lowered. Refused, before writing, when the fiscal-year lock would reopen a hard-locked or a stock-locked period, or when a hard or stock lock date was moved past the periods its operation locked.PeriodSet::of(tx, period_id): the period and the matching periods of its company’s branches.hard_lock_begin/hard_lock_complete/hard_lock_resetandstock_lock_begin/stock_lock_complete/stock_lock_reset: T1 (returns aLockRunwith the generations), the T2 write when the counts are zero (writes the company date withGREATESTand bumps the root’s serial), and the T2 refusal or audited cancel. Both locks run in order: T1 is refused while an earlier regular period of the company has not reached that lock. A T2 that matches no row returnsLockOutcome::NotAppliedwith the period states (cancelled, superseded, or already locked by the hard lock).
The caller owns the rest of each operation: SET LOCAL lock_timeout, committing T1, the drain, the counts T2 checks
(drafts, unreconciled statement lines, readiness, replay jobs), and the close_period precondition for companies on the
valuation bridge (ADR 0147 G4). Those are planned.
Not yet: tax_return_id and the tax-return close, the adjustment periods’ creation, the
database trigger that refuses writes to locked rows from raw SQL (it needs addon-declared SQL objects), and the stuck
display of an interrupted lock.
Accounting lock-policy foundation
Section titled “Accounting lock-policy foundation”The native erp_addon_account::locking API provides LockCheck::new(company, date, JournalClass, TaxImpact) and check_lock_dates(tx, &checks). Construct inputs from authorized business records.
The result preserves input order: LockVerdict::Allowed or a typed refusal. into_result() retains
its precise error kind and message. Fiscal-year and hard dates bind every accounting journal; sales
and purchase dates bind their journal classes only; tax dates bind report-affecting changes only.
A cutoff is inclusive. Effective dates take the maximum over the company and its ancestors.
A branch’s own date never restricts its parent. Hard-locking period rows bind checks before the
company hard date advances. A period that is merely missing does not refuse a post: action_post creates the periods its entries need inside its
own unit of work before the lock check (fiscal::ensure::ensure_for_rows, which checks the companies against the
environment the post was authorised with). It creates only the periods that contain a posted date, for the root company
and its tree, not the months between two dates. Two units that create the same new month at the same time: the second
waits for the first one’s commit and then fails with a serialization failure. Inside a retried unit of work (Orm::run,
as the RPC layer runs every call) the retry finds the period and posts; a caller that runs the unit itself without a
retry sees that failure and posts again. A date beyond the horizon setting, a lock date and a closed or locked period
still refuse.
Other writers that ask period_admission directly still get NoPeriod for a missing period.
Call once per batch in the same transaction that writes the records. State is private to the call; subsequent calls reread it, including after savepoint rollback. There is no process cache or reusable permission token. Entry and line hooks are still being integrated, so calling this function is not proof that every entry mutation already enforces it. Transaction-wide caching and user exceptions are not implemented.
change_lock_dates(tx, company, &LockPatch::new().set(LockField::Sale, Some(date))) changes soft
dates. An omitted field is unchanged; None clears it. Accounting-manager rights and company scope
are required. Generic company writes use the same policy and support one company per lock-date
patch. Company creation accepts only unset dates. Future dates are refused using the caller
environment’s current-date helper, on every path that changes them (ADR 0206).
A fiscal-year change calls sync_lock_dates and therefore respects hard/stock reopening refusals.
Sale, purchase and tax changes do not close fiscal periods. Company dates, period changes and the typed
change record in the audit trail commit together; audit failure rolls everything back. A no-op records no change event.
The operation uses a savepoint, so a caught refusal does not leave partial dates or audit events.
Hard activation is explicitly unavailable until the maintenance drain, ledger/bank checks, activation audit and applicable valuation-close checks are proven. The status primitives above are low-level building blocks, not a safe user-facing activation coordinator. Draft/unreconciled-line checks, exceptions, automatic date shifting and the lock-date wizard remain incomplete. Existing repeatable-read writers retain their snapshots; this foundation adds no soft-lock drain.
The multi-company foundation verifies the native policy across root, branch, grandchild and sibling companies. A branch uses the maximum applicable cutoff from its ancestor chain; its own later cutoff does not restrict its parent or sibling. Diagnostics retain all applicable cutoffs in date order. These are native-policy guarantees only: the ACCMC tests remain partial until the entry owners integrate posting, reset and editing paths. User lock exceptions remain unimplemented under ADR 0206.