Skip to Content
Droot Developers

Period Balances

Ledger models' balances per month, so ledger reports read their period, not all history.

A ledger model ("ledger": true in models.json) is written only by posting, and its reports (a general ledger, a trial balance, a balance sheet, a partner ledger, bank balances, a stock ledger, leave balances) add up its rows. An opening balance needs every row before the period asked for, so without help such a report reads the whole history every time, and gets slower every month.

A ledger model can declare balances. The kernel then keeps, beside the rows, one balance per company, dimension values and month: the sums of the declared measures over the rows dated in that month. A report reads the months before its period from the balances and only the rest from the rows. Its cost then follows the period asked for, not the history. Nothing changes in the report's own metadata, and its figures are exactly the same.

Declaring Balances

{
  "key": "accounting.journal-line",
  "label": "Journal line",
  "scoped": true,
  "ledger": true,
  "balances": {
    "dims": ["bookEntity", "ledgerKey", "accountId", "accountCode", "accountType", "partyId", "costCentre"],
    "date": "date",
    "measures": ["debit", "credit"],
    "period": "month"
  }
}
  • dims: the fields a report filters or groups by. Text, select, lookup, boolean, integer or user fields of the model, or fields another app adds to it (projects__project). The company (scopeEntityId) is always kept: leave it out. At most 24.
  • date: the date (or datetime) field whose month a row is summed into: the field reports compare with their From and To dates.
  • measures: the number, integer or currency fields reports add up. At most 16. A currency field's currency must be the company's (no currency field) or a field listed in dims.
  • period: "month".

A rollup, mirror or encrypted field can't be named. balances is structural: a tenant can't change it.

Choose the dimensions from the reports that read the model: every field their filters, measure conditions, dimensions and pivots name, and the fields people can filter on (filterFields). A field left out only means a report that names it reads the rows, as before. Each dimension multiplies the number of balances only by the values that actually occur together in a month, so fields that follow from another (an account's code, type and category) cost nothing.

Which Reads Use Them

A read takes the months before its earliest date from the balances only when the answer is provably the same:

  • its filter, the reader's company scope and data-access policy, and every measure's where name only dimensions, the company and the date field;
  • its dimensions and pivot are dimensions, or the date bucketed by month, quarter or year;
  • its measures are sums of declared measures (a field, or sub/add of two), or ratios and formulas of other measures; a count, average, minimum, maximum, distinct, median or weighted measure is fine only when its where excludes every month before the period ("where": { "date": { "$gte": { "$param": "from" } } });
  • it doesn't follow a related-field path.

The earliest date is the earliest one the filter and the measure conditions compare the date field with (or the first day of this month when there is none). A filter with a lower date bound ($gte From) gains nothing: such a read reads its rows. So an opening balance belongs in a measure ("where": { "date": { "$lt": { "$param": "from" } } }), with only an upper bound ($lte To) in the filter, which is how the standard ledger reports are written.

A report that counts rows (a count measure without a date condition) always reads its rows: drop the count, or bound it to the period, to let it use the balances.

Keeping Them Exact

The kernel updates a model's balances in the same transaction as every write of its rows (a posting, a reversal, a correction, an import of migrated history, an import's undo), so they never drift from the rows. A balance holds the row count and, per measure, the sum and how many rows hold a number, so a month with no rows left has no balance.

Building and Checking

After an app is installed or upgraded with a new or changed declaration, the worker builds the balances in the background, a slice at a time, month by month; until the build is done, reports read the rows as before. A raw data migration that changes rows in place marks the balances stale, and they are rebuilt the same way.

Every night the worker checks every model's balances against its rows, one read of each per month. A month that differs is rebuilt from its rows, and the operators (people with the Executor permission) get a notice naming the months. It should never happen: a repeat means something writes the rows outside the kernel.

From the command line, on the server:

npm run balances:rebuild -w kernel -- accounting.journal-line   # rebuild one model (every model when none is named)
npm run balances:rebuild -w kernel -- --resume                  # continue an interrupted build
npm run balances:rebuild -w kernel -- --tie-out                 # the nightly check, now
npm run balances:rebuild -w kernel -- --status                  # each model's state

A rebuild works one month per transaction and is safe while people post. Reports keep using the balances during a rebuild of balances that were already built.