Skip to content
ERPForAll DocsDevelopment

    Journal entry numbering: number chains and the allocation point

    Status: implemented. Design: ADR 0048 D2 (numbering point and lock order). Behaviour: docs/specs/accounting/numbering.md (ACCSEQ). The posting step that calls this service is not available yet; until then the tests number a stand-in document (crates/erp-test-models/src/test_numbering.rs).

    account.sequence.chain holds one row per (journal_id, kind, period_key), unique in the database. The row’s next_number is the number the next document of the chain gets; last_name is the last name it handed out.

    ColumnMeaning
    kindthe sequence kind: document, refund or payment (a selection: another addon adds a kind with selection_add; the debit-note addon will add debit)
    period_keythe reset period of the journal’s declared pattern: never, 2026, 2026-07, 2025-26 or 2025-26-07 (see Sequences)
    company_idthe journal’s company; internal users of that company or a branch of it may read the row

    Rows are created on first use. Only the numbering service writes them: users have read access only. There are no default records: the reference keeps no such table (its next number is read from the highest entry name).

    use erp_addon_account::numbering::{assign_numbers, LockWait, NumberRequest};
    let wait = LockWait::from_settings(tx).await?;
    let requests: Vec<NumberRequest<'_>> = moves.iter().map(|m| NumberRequest {
    document_id: m.id,
    company_id: m.company_id,
    journal_id: m.journal_id,
    kind: "document",
    pattern: &journal_pattern, // the journal's declared SequencePattern for this kind
    date: m.date,
    fiscal_year: m.fiscal_year, // the move company's fiscal year; needed by the fiscal resets
    reference: m.reference.as_deref(),
    }).collect();
    let numbers = assign_numbers(tx, wait, &requests).await?;
    // write numbers[i].name, .sequence_number and .chain_id on the documents, in the same transaction

    assign_numbers:

    1. groups the requests by chain and orders each chain’s documents by (date, reference, id), a missing reference sorting as empty text and text compared by code point (the reference’s posting order);
    2. sets the configured wait as the transaction’s lock_timeout;
    3. creates the missing chain rows, then locks every chain row of the batch with SELECT ... FOR UPDATE, in (journal_id, kind, period_key) order (lock step 4 of ADR 0048 D2.1);
    4. raises each chain’s counter with one UPDATE and renders the names with the pattern;
    5. puts the transaction’s own lock_timeout back and returns the numbers in request order.

    Call it last in the posting transaction: the chain rows stay locked until that transaction commits or rolls back, so only the numbering step and the commit run under the lock. The document table must carry the two unique rules of ADR 0048 D2.5, UNIQUE (journal_id, name) and UNIQUE (chain_id, sequence_number): then the database itself refuses a duplicate number, whatever path wrote it.

    SituationWhat happens
    Two postings take numbers of the same chainthe second waits for the first. When the first commits, the second fails with Concurrency/40001 (a REPEATABLE READ transaction may not see the new counter); Orm::run runs its unit again and it takes the next number. Keep posting inside Orm::run.
    A posting rolls back, or a savepoint around the numbering rolls backthe counter rolls back with it: the next posting gets the same number, so no gap appears
    The chain is held longer than the configured waitthe call fails with Lock, SQLSTATE 55P03 and Condition::LockTimeout, naming the chain and the setting. It is never retried, by this service or by Orm::run.
    A transaction that has not reached the numbering stepholds no chain lock and blocks no other posting
    One batch asks for a chain with two patterns or two companies, an undeclared kind, a document twice, or a fiscal pattern without a fiscal yearValidation (or Programming for the missing fiscal year), before any statement
    ParameterScopeDefaultFloorHard bound
    account.sequence_lock_wait_msDatabase, Technical → System Parameters5,000 ms100 ms60,000 ms

    LockWait::from_settings reads it through the typed native parameter API. LockWait::from_millis builds a wait inside the same bounds and refuses any other value by name.

    Accounting 1.9 transfers the stored Settings value during upgrade. The transfer uses the global Settings lock, retains the legacy value for rollback, and audits displaced parameter text without advancing the revision. Updating the scope tuple invalidates earlier write snapshots. The dev/testing activation controller drains the previous server while it moves the value and starts the candidate. Changes made later in System Parameters apply to the upgraded service. A rollback to the previous binary reads and edits the retained Settings value. A subsequent forward attempt checks both stored values so it cannot discard a Settings edit acknowledged after rollback.

    • the journal’s declared pattern fields and the journal_id many2one;
    • writing the numbers on account.move in the posting step, hand-typed and imported names;
    • the resequence engine, derived gap flags and the chain-end deletion rule, the opt-in gap-allowed mode, and the self-billing partner kind: planned as follow-up work.