Skip to content
ERPForAll DocsDevelopment

    Journal entries and journal items: drafts and posting

    erp_addon_account::moves holds the journal entry header account.move ([AccountMove]) and its items account.move.line ([AccountMoveLine]). An entry is created, edited, checked for balance and deleted as a draft, then posted, cancelled or returned to draft through the posting service. Screens (form, list, search) come with the accounting UI tasks; the three operations are RPC methods for now.

    ModelStoredRead through the entry or company
    account.movename, ref, date, state, move_type, journal_id, company_id, currency_id, partner_idcompany_currency_id
    account.move.linemove_id, company_id, journal_id, date, parent_state (stored related of the entry), account_id, partner_id, name, sequence, display_type, currency_id, amount_currency, balance, is_storno, date_maturitymove_name, ref, company_currency_id

    debit and credit are virtual generated columns over balance and is_storno (ADR 0048 D1); a storno line keeps a negative amount on its own side. Both models declare owner_only_stored_fields = []: another addon that adds a stored field to them fails the registry build (ADR 0048 D7) and keeps its data in a companion table instead.

    Default order: entries by date, then number, then id, all descending; items by date descending, then entry descending, then id (no copy of the entry number on the line).

    • Journal: the first general journal (journal order) of the entry’s company or one of its parents that the caller can read; one in the entry’s currency first when a currency is given. None: a UserError asking for a journal.
    • Company: the active company when the journal belongs to it or to one of its parents, else the journal’s company.
    • Currency: the journal’s, else the company’s. A line takes the entry’s currency.
    • Date: today in the caller’s time zone. Type: entry (invoice types arrive with the invoicing tasks).
    • Changing the journal of a draft clears its number (name), unless the same write gives one.

    debit and credit are flagged input_adapted = true, so a create or write that names them reaches the line’s _adapt_generated_input before the generated-column guard (ADR 0215), on every path (typed and dynamic calls, nested line_ids commands, imports):

    • debit = d gives balance = d, credit = c gives balance = -c, both together give d - c.
    • On a write, a side written as zero keeps the stored amount of the other side; a non-zero side replaces it.
    • Both sides non-zero on one line, or a balance that contradicts the debit and credit given with it, is refused (Validation).

    In the company currency amount_currency equals balance: the written one sets the other, and two different values are refused. Monetary values are rounded on their currency’s increment by the ORM (ADR 0087).

    When a line’s currency is not the company currency, the amount that is not given is converted through the exchange rate (SPEC-ACCMOVE-014, 025, 032, 034):

    CallGivenResult
    createamount_currency onlybalance = amount_currency converted into the company currency
    createbalance (or debit/credit) onlyamount_currency = the balance converted into the line currency
    createboth, or neitherkept as given (zero when neither); no rate is read and the two are not compared
    writeamount_currency without balancebalance re-derived; with currency_id in the same write, in the new currency
    writebalance without amount_currencythe stored amount_currency is kept
    writecurrency_id aloneboth amounts are kept
    • The rate is the spot rate of the entry’s company (its root company’s rates, else shared ones) on the entry’s date, under the base rule RateDateRule::StrictlyBefore: the latest rate strictly before that date; with none before it, the earliest later one (erp_addon_base::money, SPEC-BASE-061). Invoices, which convert at their invoice date, are not delivered yet.
    • The rate is an exact ratio: no float is involved. The quotient is rounded once, on the increment of the currency it is converted into, half away from zero (CURRENCY_ROUNDING_MODE, ADR 0046 D2/D3).
    • A currency without any rate is refused with base’s UserError, which names the currency, the company and the date. No rate of one is assumed.
    • Conversions are batched: one rate read per company and entry date (and per 255 currencies), never one per line.
    • The rates are read as the caller: internal users read res.currency.rate, so no right is added and nothing is elevated.
    • Rounding each line on its own can leave an entry that balances in the line currency unbalanced in the company currency (1, 1 and -2 EUR at a rate of 3 give 0.33, 0.33 and -0.67 USD). The balance check then refuses it like any other unbalanced entry.
    • A change of the entry date re-derives nothing on the server. In the reference this is form behaviour (onchange), not delivered yet.

    After a create, write or delete of entries or lines, the touched entries must balance: one SQL aggregate over their lines, compared with zero exactly (amounts are on the currency increment). An unbalanced entry is a UserError that names the entry and the difference. Nested line_ids commands are checked once, after all of them ran. The context key check_move_validity = false skips the check (a form saving an incomplete draft); any value other than a boolean is a Programming error.

    • An accountable line (display_type = product) needs an account; a section, subsection or note line has no amount and no account (named CHECK constraints).
    • move_id never changes: a write that moves a line to another entry, directly or through line_ids link commands, is refused. Delete the line and add a new one to the other entry.
    • Account, journal and partner must suit the entry’s company (check_company).
    • Deleting an entry deletes its lines.
    • is_storno = true is refused until the company’s storno setting exists (not delivered yet).

    The state of an entry changes only through three methods of account.move (module moves::post); a caller-written state (or parent_state on a line) is refused, also as superuser. Each needs account.group_account_user (or superuser mode) and write access to every entry, and is atomic: a refusal leaves every entry as it was.

    • action_post ([post_moves]) posts draft entries. It collects every refusal of the batch and raises them together as one UserError, one line each: an entry not in draft, an entry without an accountable line, an archived journal, an inactive currency, an archived account, an account none of whose companies is the entry’s company or one of its parents, an unbalanced entry. An entry dated where a lock date refuses it is re-dated to the day after the latest violated lock, adjusted to the journal’s numbering reset (ADR 0147 D4, the reference’s rule); the new date is checked again and a period that is not open refuses it. A missing fiscal period is refused naming ensure_periods. Then one statement sets the entries and their items to posted with the final date, and the entries take the next numbers of their journals (numbering, ADR 0048 D2.3 step 7): a credit note in the refund chain of a journal that keeps one (R before the code), everything else in the document chain. For plain entries the number of statements does not depend on the batch size.
      • The lock check holds the rows of the checked companies and their parents shared until the transaction ends: a lock date committed after the posting transaction began fails the posting with a serialization error (40001) and the unit of work is retried under the new date, and a lock-date change waits for postings in flight.
      • A draft that carries a name is refused with the collected checks: a posted entry never returns to draft, so such a name was written by hand, and it could repeat a posted number or take one the chain gives out later. Clear the name to post the entry.
      • The draft invoices of the batch are first rebuilt from their product lines (invoice::sync_lines), so a price, quantity, discount or tax written on an invoice line directly is in the tax line, the receivable or payable line and the totals before the invoice checks read them. This part writes each changed line and header on its own.
      • A re-dated entry keeps its company-currency balances; they are not converted again at the new date.
    • button_draft ([reset_moves]) returns cancelled entries to draft. A posted entry is refused: every company’s posting policy is reversal-only (ADR 0146) until the opt-in policy exists; a posted entry is corrected by a reversal (not available yet).
    • button_cancel ([cancel_moves]) cancels draft entries; a posted one is refused for the same reason.

    An entry with a line that carries taxes, or with a tax line, reaches the tax report: the tax lock date binds it at posting and re-dates it like the other locks.

    A posted or cancelled entry keeps everything but its reference (moves::post::POSTED_MOVE_WRITABLE), and its items everything but their label and due date (POSTED_LINE_WRITABLE, both lists from ADR 0048 D3 R1b). Any other field written on one, the invoice fields included (invoice and due dates, totals, payment state, invoice lines, quantity, price, discount, product, subtotals) and state, is refused with a UserError that names the fields. The check runs on the final values of the write, after the invoice date has set the accounting date. Items cannot be added to such an entry or deleted from it, and it cannot be deleted.

    Adding, changing or deleting an item takes its entry’s header row in id order with a version update, the order posting locks headers in. An item change and a posting of the same entry therefore never both commit: whichever commits second fails with a serialization error (40001), and its retry sees the other’s result.

    These guards read and lock an entry by the id the caller gave, so they ask the caller’s rights on that entry first: write access for a change of the entry or of its items (adding and deleting an item included), unlink access for a delete. A caller without them gets the access refusal, which never carries the state or the number of the entry, and takes no lock.

    Not delivered yet: the database guards on posted rows (the balance and line-state triggers of ADR 0048 D3 and the posting batch row), the markers that record a re-dating (ADR 0147 D4), the outbox event per posting batch, hashing, scheduled posting and invoice-specific checks.

    Upgrading account to 1.2 drops the draft-only CHECK constraints of the earlier version (migrations/1.2/pre-drop-draft-only-checks.sql).

    account.group_account_readonly reads entries and items; account.group_account_user creates, writes and deletes them. Each model has the generated company rule of its company_id (exact company). The reference’s invoicing group is not delivered yet.

    Rules the database takes over with the posted-row guards

    Section titled “Rules the database takes over with the posted-row guards”

    Checked by the ORM for now, by triggers once the posted-row guards are delivered (ADR 0048 D3): company of accounts and journals (R4c), the line’s entry never changing (R1a), amount in currency equal to the balance in the company currency (R4d), amounts on the currency increment (R4e).