Skip to content

Account Roles

When a module posts a journal entry it needs a GL account — the default income account for a sale, the bank-charges account for a processing fee, the payroll-payable account for a payslip. Never ship a blank-required “pick an account” config field for these. A blank field means nothing works until someone hand-maps it, and a forgotten one surfaces as a runtime error mid-transaction.

Instead, resolve an account role: a stable, chart-independent handle for “the account that plays this part”. Roles are the seam between a feature that needs an income account and whichever concrete account a given company’s chart uses for it — including a customer who tore down the shipped chart and imported their own.

Account = get_model("Account")
fee_account = await Account.resolve_role("bank_charges", company_id=company_id)

resolve_role(role, company_id=None, fallback_type=None, required=True) returns the Account that plays role for a company, resolving in order:

  1. The company’s explicit mapping for that role (an AccountRoleMapping row) — the authoritative, per-company binding. Set on install, on company creation, when a company adopts a fiscal localization, and by the user in Setup → Key Accounts.
  2. The first account of the role’s fallback type in that company’s own chart (or a global account) — a sensible default of the right kind, so an unmapped role still posts somewhere valid.
  3. Otherwise a clear, user-actionable error pointing at Setup → Key Accounts — it does not invent an account.

company_id defaults to the company being acted for (env.company_id) — inside as_company(X) that is X, otherwise the user’s default company. It is deliberately not the first ticked company in the switcher, whose order is arbitrary: a role resolved that way posts to whichever company happened to sort first. Pass company_id explicitly whenever you are posting for a company the reader didn’t pick — a document’s own company, a subsidiary, a cron over every company — rather than relying on the default.

Pass required=False for soft contexts (a form default) — it returns None instead of raising when nothing resolves, so opening a form never errors.

A company-level “Default Income Account” setting beside a default_income role is the same account asked for twice, in two places, with nothing on either screen saying which one is in force. If your setting would name one role at the role’s own granularity, don’t ship it — resolve the role at the posting and let the user choose the account once, in Setup → Key Accounts.

A field that narrows the role is different and entirely fine, because it answers a question the role cannot: a per-product-category income account, a per-integration fee account, a rental security deposit that merely defaults to the down-payment role. Check yours first, resolve the role when it’s blank:

fee_account = self.integration.payment_fee_account or await Account.resolve_role(
"bank_charges", company_id=company_id
)

To seed such a field from its role when a record is created, use apply_role_defaults in _default_get rather than hand-rolling the lookup:

async def _default_get(cls, context=None):
defaults = await super()._default_get(context)
return await get_model("AccountRole").apply_role_defaults(
defaults, {"income_account": "default_income"}, company_id=env_ctx.get().company_id
)

AccountRoleMapping.set_account(role, account, company_id) is the programmatic form of editing a row on the Key Accounts screen — the one upsert for the (company, role) pair, safe to call for a company the reader hasn’t selected in the switcher. account takes a record or an id; None clears the binding, so the role resolves by type again.

Mapping = get_model("AccountRoleMapping")
await Mapping.set_account("bank_charges", account, company_id)

Roles are data, seeded from data/account_roles.yaml in any module (they compose, so a third-party module can add its own). A role names a requirement — it carries no account code/name:

- data_type: AccountRole
identifier: my_clearing # the handle you resolve by
apply_once: true
name: My Clearing Account
description: What this role is used for (shown on the Key Accounts screen).
account_type: Current Liability
fallback_type: Current Liability # defaults to account_type
default_account_identifier: my_clearing # the account that fills it in your chart
required: false # true → part of the Key Accounts checklist + guardrail
category: Payments # group heading on the Key Accounts screen
sequence: 320

default_account_identifier is the identifier of the account that plays the role in the shipped chart — used to pre-bind the company’s mapping. Ship that account in your module’s chart/data.

A role added to a database that already has companies reaches them on the next update: the invoicing module’s update binds every company’s unmapped roles to their best-guess account (the shipped account by identifier, else the first of the fallback type), exactly as company creation does. An existing mapping — including one a customer changed — is never touched. Until then, resolve_role falls back by type, so a posting never waits on it.

The chart contract (for localization authors)

Section titled “The chart contract (for localization authors)”

Every chart of accounts — the base invoicing chart and every l10n_* chart — must define an account for every role’s default_account_identifier. That is what lets resolution land on step 1 (the explicit mapping) on every localization rather than a fuzzy type fallback. It is enforced by invoicing/tests/test_account_roles.py, which fails if any shipped chart is missing a role account, and which also lints that no new company-scoped ManyToOne("Account") config field ships without a resolve_role fallback in its module.

A localization usually numbers its accounts one way and ships them in data/chart_of_accounts.yaml. Where a country has several standard charts for the same accounts — Germany’s DATEV SKR 03 and SKR 04 — the pack ships each as a file of its own (e.g. charts/skr03.yaml, charts/skr04.yaml, same entry format, outside data/ so they are not seeded) and declares them as AccountChart records:

data/account_charts.yaml
- data_type: AccountChart
identifier: de_chart_skr03
name: SKR 03 (Prozessgliederung)
localization: l10n_de # the pack's own module
file: charts/skr03.yaml # relative to the module
sequence: 10 # the lowest is the chart a company gets until it chooses
- data_type: AccountChart
identifier: de_chart_skr04
name: SKR 04 (Abschlussgliederung)
localization: l10n_de
file: charts/skr04.yaml
sequence: 20
  • One account, one identifier, in every chart. The charts number the same shared accounts (keyed by identifier); a company sees its own chart’s number and name on each. An account that exists in one chart only is simply listed in that file only.
  • Each company chooses in Invoicing → Setup → Chart of Accounts (the account_chart setting, offered only the selected localization’s charts); blank means the first chart by sequence. Saving another chart renumbers the company’s accounts; the accounts only the previous chart had are taken away from the company unless something was posted to them.
  • Accounts are created on adoption, already linked to the company. An entry with a type whose identifier no module has seeded is created when a company adopts the chart, so a thousand-account national chart is never shown, unlinked, to every company in the database. Accounts your shipped records must reference at install (the accounts your taxes post to) still go in a seeded data/ file — give them the same code and name as your first chart.
  • The chart contract applies to every declared chart: test_account_roles.py checks each file an AccountChart names, not only data/chart_of_accounts.yaml.

The role→account binding is per-company (AccountRoleMapping), not tied to account identifiers — so a company that imports its own chart re-points the roles at their accounts once, in Setup → Key Accounts (pre-filled by type so it’s a confirm, not a blank form), and everything that posts a journal entry keeps working. The onboarding “Map Key Accounts” step walks a new company through confirming the required roles.

Proving your postings reconcile: BooksCheck

Section titled “Proving your postings reconcile: BooksCheck”

A module that keeps a subledger beside the general ledger — cost layers, a depreciation board, a deferral schedule — holds a second account of a figure the GL also holds, and the two must agree. BooksCheck (shipped by invoicing) is the one place those agreements are written down. BooksCheck.run(company_id) returns every discrepancy in a company’s books as a list of dicts (check, subject, first_label/first, second_label/second, difference, model, record_ids, company_id); an empty list means the books reconcile. The base checks are: every posted entry balances, the trial balance is zero, and each reconcilable account’s open items (Σ unreconciled residual) equal its balance.

Add your module’s own invariant by extending the model and appending to checks:

from fullfinity.engine.base import *
class BooksCheckMyModule(Model):
__inherit__ = "BooksCheck"
async def checks(cls, company):
found = await super().checks(company)
for doc in await get_model("MyDocument").filter(company=company.id, state="Posted").all():
found += cls.compare(
company, t("My document total"), doc.name,
t("Header total"), doc.amount_total,
t("Sum of lines"), await doc.lines_total(),
"MyDocument", [doc.id], currency=doc.currency,
)
return found
  • run puts the company in force (as_company) before calling checks, so reads resolve for it; still filter by company explicitly.
  • cls.compare(...) returns a one-item list when the two figures differ by more than half the smallest unit of the currency they are kept in (the company’s unless you pass currency=), else an empty list — never use a looser epsilon, or a one-cent bug passes.
  • cls.account_balance(company, account_ids) is the posted balance (debit − credit) of a set of accounts in the company’s currency, for comparing a subledger against the GL.
  • Compare two figures that must be equal by construction. A check that can legitimately disagree (a figure that includes items the other cannot see) reports noise, and a check that cries wolf gets ignored.