Layouts#

A layout says which kinds of lines an entity’s statements have and in what order. Model.entity(..., layout=...) builds the entity’s co.pl and co.bs from it. Without layout=, an entity uses the general layout of its standard: b.IFRS.general, b.USGAAP.general or b.JGAAP.general. b.layouts is the module that holds the custom-layout API (Layout, Kind, Stage, Standard) and the presets (IFRS, USGAAP, JGAAP, bank).

Built-in standards and layouts#

boki.layouts.IFRS#

The IFRS standard. Its general layout has gross profit, operating income, pretax income and net income (“Profit for the year”); goodwill is not amortized, and the Excel balance sheet shows non-current assets first.

boki.layouts.USGAAP#

The US GAAP standard. Its general layout has gross profit, operating income, pretax income and net income; goodwill is not amortized except under the private-company alternative, and current assets are shown first.

boki.layouts.JGAAP#

The Japanese GAAP standard. Its general layout has gross profit, operating income, ordinary income, pretax income and net income; it amortizes goodwill and shows current assets first.

boki.layouts.bank#

The bank layout: net interest income, provisions and non-interest items on the income statement, and earning assets, deposits and allowances on the balance sheet. It is shared across standards: pass layout=b.layouts.bank to Model.entity with any standard. It has no operating income or gross profit subtotal.

Defining a custom layout#

A layout is data: a tuple of Kind and Stage items for the income statement, a tuple of balance-sheet Kind items, and the names of the two income-statement kinds that automatic entries use.

>>> import boki as b
>>> from boki.layouts import Kind, Layout, Stage
>>> royalty = Kind("royalty", "income", cf="pl", mechanism="pl")
>>> cost = Kind("cost", "expense", cf="pl", mechanism="pl")
>>> layout = Layout(
...     "Licensing",
...     pl=(royalty, cost, Stage("net_income", lambda pl: pl.royalty.total - pl.cost.total)),
...     bs=(Kind("escrow", "asset", "current"),
...         Kind("note", "liability", "noncurrent", "financing", mechanism="loan")),
...     interest_kind="cost",
...     depreciation_kind="cost")
>>> m = b.Model("Licensing", start="2027-01", periods=2, step=b.year)
>>> co = m.entity("A", standard=b.IFRS, layout=layout)
>>> fee = co.pl.royalty("Fee", 100)
>>> m.render().value(co.net_income, "2027")
100

A layout is not checked when it is made. Model.entity builds the statements from it and raises when it cannot, and some mistakes surface only when a line is booked. The rules below are what the code enforces.

Mechanisms#

A balance-sheet kind has a mechanism that decides what a call of its maker books and which template documents it. The valid mechanisms are:

Mechanism

What a call books

Template page

"line"

A balance, moved against cash or held at a target

Balance-sheet lines (line)

"fixed_asset"

A depreciating asset bought with cash

Fixed assets (fixed_asset)

"loan"

Borrowing with repayment and interest

Loans (loan)

"construction"

Construction in progress

Construction in progress (construction)

"capital"

Share capital and other equity

Capital and other equity lines (capital)

"dividend"

A dividend from retained earnings

Dividends (dividend)

Any other mechanism on a balance-sheet kind makes Model.entity raise KeyError when it builds the balance sheet. The income statement does not read the mechanism of a PL kind: "pl" is the convention. Two things about PL kinds come from other fields. A PL kind whose side is "income" is an income kind, and any other side is treated as an expense. A PL kind named income_tax gets the income tax maker, which takes rate= and base=; the other PL kinds share one maker (Revenue and expense lines (pl_line), Income tax (income_tax)).

>>> bad = Layout(
...     "Bad mechanism",
...     pl=(royalty, cost, Stage("net_income", lambda pl: pl.royalty.total - pl.cost.total)),
...     bs=(Kind("pool", "asset", "current", mechanism="pool"),),
...     interest_kind="cost", depreciation_kind="cost")
>>> try:
...     b.Model("Bad", start="2027-01", periods=1, step=b.year).entity("A", layout=bad)
... except KeyError as error:
...     print("KeyError", error)
KeyError 'pool'

Required kinds and stages#

  • net_income. Every layout needs a stage named net_income, because the entity reads co.pl.net_income when it is created. Without it, Model.entity raises AttributeError.

  • interest_kind and depreciation_kind. They name the PL kinds that take loan interest and depreciation. They are not looked up when the layout is made. A KeyError is raised when a line of a loan kind is booked and interest_kind is not a PL kind of the layout, and when a line of a fixed_asset or construction kind is booked and depreciation_kind is not.

  • income_tax. bs.deferred_tax books its expense into a PL kind named income_tax, and raises AttributeError when the layout has none.

  • A non-revenue income kind. oci_release of a recycled OCI component books into the first of other_income, non_operating_income, non_interest_income, special_gain and finance_income that the layout has, and raises ValueError when it has none.

  • Optional kinds. Cash, retained earnings and the shared accounts that settle=, recognize= and vat= open need no kind. Kinds named deferred_tax_asset, deferred_tax_liability and oci are filled when the layout has them.

>>> def build(layout, body=None):
...     m = b.Model("Rules", start="2027-01", periods=1, step=b.year)
...     try:
...         co = m.entity("A", standard=b.IFRS, currency=b.units.JPY, layout=layout)
...         if body is not None:
...             body(co)
...     except (AttributeError, KeyError, ValueError) as error:
...         return type(error).__name__
...     return "ok"
>>> no_net_income = Layout("No net income", pl=(royalty, cost), bs=(), interest_kind="cost", depreciation_kind="cost")
>>> build(no_net_income)
'AttributeError'
>>> loans = Layout(
...     "Loans",
...     pl=(royalty, cost, Stage("net_income", lambda pl: pl.royalty.total - pl.cost.total)),
...     bs=(Kind("note", "liability", "noncurrent", "financing", mechanism="loan"),),
...     interest_kind="missing", depreciation_kind="cost")
>>> build(loans)
'ok'
>>> build(loans, lambda co: co.bs.note("Note", draw={0: 100 * b.units.JPY}))
'KeyError'
>>> build(layout, lambda co: co.bs.deferred_tax("Difference", 100, rate=0.3))
'AttributeError'

Stage dependencies#

Model.entity builds every kind first and then the stages in the order of the layout. A stage’s formula receives co.pl and may read the total of any kind of the layout, wherever the kind is listed, and any stage listed before it. A formula that reads a stage listed after it, or a name the layout does not have, raises AttributeError. The formula may be called again when the first amount fixes the entity’s currency, so it should depend on pl only.

>>> def stages(*items):
...     return Layout("Stages", pl=(royalty, cost, *items), bs=(), interest_kind="cost", depreciation_kind="cost")
>>> net = Stage("net_income", lambda pl: pl.profit)
>>> profit = Stage("profit", lambda pl: pl.royalty.total - pl.cost.total)
>>> build(stages(profit, net))
'ok'
>>> build(stages(net, profit))
'AttributeError'

Names#

Kind and stage names become attributes of co.pl and co.bs, and nothing checks them against the names the statement already uses. Choose names that are unique and that the statement does not use:

  • In co.pl, kinds are set first and stages after them, so a stage with the name of a kind replaces the kind’s maker. A repeated name keeps the last one.

  • In co.bs, cash is set first, then the kinds, then equity, total_assets, total_liabilities and total_equity. A kind named cash replaces the cash balance in co.bs.cash. A kind named equity or total_* is replaced by the total. A kind named like a method, opening, asset, liability, deferred_tax or sell, shadows that method on that balance sheet, so co.bs.opening(cash=...) would call the maker instead.

  • boki.layouts is the module, not a class: b.layouts.bank is the bank layout, and b.layouts.Layout is the layout class.

>>> clash = Layout("Clash", pl=(royalty, cost, Stage("net_income", lambda pl: pl.royalty.total - pl.cost.total)),
...                bs=(Kind("opening", "asset", "current"), Kind("equity", "equity", cf="financing")),
...                interest_kind="cost", depreciation_kind="cost")
>>> m = b.Model("Clash", start="2027-01", periods=1, step=b.year)
>>> co = m.entity("A", layout=clash)
>>> type(co.bs.opening).__name__, type(co.bs.equity).__name__
('LineMaker', '_Equity')

Classes#

class boki.layouts.Layout(name, pl, bs, interest_kind, depreciation_kind)#

Which kinds of lines an entity’s statements have, in what order, with the subtotals between them.

A layout is data: the kinds of the income statement and their subtotals, the kinds of the balance sheet, and the two income-statement kinds that automatic entries use. Model.entity(..., layout=...) builds the entity’s co.pl and co.bs from it. Without layout=, an entity uses the general layout of its standard. A Layout is immutable and shared by every entity that uses it. The custom-layout page explains how to define one.

Parameters:
namestr

Name of the layout, shown in error messages and in repr(entity). Required.

pltuple

Kind and Stage items of the income statement in presentation order. Required. A layout needs a stage called net_income.

bstuple of Kind

The balance-sheet kinds. Required; it may be empty. Cash, retained earnings and the shared accounts that settle= and vat= open are not kinds: every entity has them.

interest_kindstr

Name of the PL kind where interest on loans is expensed. Required; it must be a PL kind of the layout if the layout has a loan kind.

depreciation_kindstr

Name of the PL kind where depreciation is expensed. Required; it must be a PL kind of the layout if the layout has a fixed-asset or construction kind.

Attributes:
namestr

The layout’s name.

pltuple

The income-statement kinds and stages in presentation order.

bstuple[Kind, …]

The balance-sheet kinds.

interest_kindstr

The PL kind that takes loan interest.

depreciation_kindstr

The PL kind that takes depreciation.

See also

boki.layouts.Kind

One line kind of a layout.

boki.layouts.Stage

One subtotal of a layout.

boki.layouts.Standard

An accounting standard, which owns a general layout.

Notes

The properties pl_kinds, income_kinds and stages list the names of the PL kinds, the names of the income kinds, and the subtotals.

A layout is not checked when it is made. Model.entity builds the statements from it and raises when the layout cannot be built, and some mistakes surface only when a line is booked; the module page lists each rule and its error. The layouts of the built-in standards are b.IFRS.general, b.USGAAP.general and b.JGAAP.general, and b.layouts.bank is shared across standards.

Examples

A custom layout with one income kind, one expense kind and the required net income, then an entity that uses it:

>>> import boki as b
>>> from boki.layouts import Kind, Layout, Stage
>>> layout = Layout(
...     "Licensing",
...     pl=(Kind("royalty", "income", cf="pl", mechanism="pl"), Kind("cost", "expense", cf="pl", mechanism="pl"),
...         Stage("net_income", lambda pl: pl.royalty.total - pl.cost.total)),
...     bs=(Kind("escrow", "asset", "current"),),
...     interest_kind="cost", depreciation_kind="cost")
>>> layout.pl_kinds, layout.income_kinds
(('royalty', 'cost'), ('royalty',))
>>> m = b.Model("Layout", start="2027-01", periods=1, step=b.year)
>>> co = m.entity("A", layout=layout)
>>> royalty = co.pl.royalty("Royalty", 100)
>>> m.render().value(co.net_income, "2027")
100
bs_kind(name)#

Return the balance-sheet kind with a name.

Parameters:
namestr

The name of a balance-sheet kind, such as "loan".

Returns:
Kind | None

The kind, or None if the layout has no balance-sheet kind of that name.

Examples

>>> import boki as b
>>> b.IFRS.general.bs_kind("loan").mechanism
'loan'
>>> b.layouts.bank.bs_kind("receivable") is None
True
property income_kinds#

The names of the income-statement kinds that are income, in presentation order.

Returns:
tuple[str, …]

The names of the PL kinds whose side is "income". The other PL kinds are expenses.

Examples

>>> import boki as b
>>> b.IFRS.general.income_kinds
('revenue', 'other_income', 'finance_income')
property pl_kinds#

The names of the income-statement kinds, in presentation order.

Returns:
tuple[str, …]

One name for each Kind in pl; the stages are not included.

Examples

>>> import boki as b
>>> b.layouts.bank.pl_kinds
('interest_income', 'interest_expense', 'provision', 'non_interest_income', 'non_interest_expense', 'income_tax')
property stages#

The subtotals of the income statement, in presentation order.

Returns:
tuple[Stage, …]

One Stage for each subtotal in pl; the kinds are not included.

Examples

>>> import boki as b
>>> [stage.name for stage in b.USGAAP.general.stages]
['gross_profit', 'operating_income', 'pretax_income', 'net_income']
class boki.layouts.Kind(name, side, section=None, cf='operating', tags=frozenset({}), contra=False, mechanism='line')#

A kind of line in a layout: a named group of lines on one side of a statement, and how it behaves.

Every kind of a layout becomes a LineMaker on the entity, named after the kind: a PL kind is co.pl.<name> and a balance-sheet kind is co.bs.<name>. The kind decides the side, the default cash-flow section, and, for the balance sheet, the mechanism that says what a call books (a plain balance, a loan, a fixed asset). A Kind is immutable.

Parameters:
namestr

Name of the kind, which becomes the attribute co.pl.<name> or co.bs.<name>. Required. It must be unique in the layout and must not be a name the statement already uses; see the custom-layout page.

sidestr

Required. "asset", "liability" or "equity" for a balance-sheet kind, and "income" or "expense" for a PL kind. A PL kind whose side is not "income" is treated as an expense.

sectionstr, optional

"current" or "noncurrent" for a balance-sheet kind, which the Excel balance sheet uses to group its lines. The default is None, for PL kinds and for kinds with no group.

cfstr, default=”operating”

The cash-flow section of the cash that a line of this kind moves, unless the line gives its own: "operating", "investing" or "financing" for a balance-sheet kind, and "pl" for a PL kind.

tagsfrozenset, default=frozenset()

Labels copied to every account of the kind, such as frozenset({"interest_bearing"}). boki reads a few internal tags; others do not change values.

contrabool, default=False

Marks an asset that is carried as a negative amount, such as an allowance. It is recorded on the kind and does not change values or statements today.

mechanismstr, default=”line”

What a call of a balance-sheet kind books: "line" (a balance), "fixed_asset", "loan", "construction", "capital" or "dividend". Any other value makes Model.entity raise KeyError when it builds the balance sheet. PL kinds use "pl" by convention; the income statement ignores it.

Attributes:
namestr

The kind’s name.

sidestr

The side of the statement.

sectionstr | None

The balance-sheet section, or None.

cfstr

The default cash-flow section.

tagsfrozenset

The labels copied to the kind’s accounts.

contrabool

Whether the kind is a contra asset.

mechanismstr

The mechanism of the kind.

See also

boki.layouts.Layout

A layout is a list of kinds and subtotals.

boki.layouts.Stage

A subtotal between kinds.

Notes

A PL kind named income_tax gets the income tax maker, which takes rate= and base=, whatever its mechanism. The built-in layouts list the general kinds; see the balance-sheet rows.

Examples

A royalty income kind for the income statement, and a loan kind for the balance sheet:

>>> from boki.layouts import Kind
>>> royalty = Kind("royalty", "income", cf="pl", mechanism="pl")
>>> note = Kind("note", "liability", "noncurrent", "financing", mechanism="loan")
>>> royalty.side, note.section, note.mechanism
('income', 'noncurrent', 'loan')
class boki.layouts.Stage(name, formula)#

A subtotal of the income statement, such as gross profit, built from the totals of kinds and earlier subtotals.

A layout lists stages between its kinds in presentation order. Model.entity builds each stage as a series named after it, co.pl.<name>, by calling formula with the entity’s PL. A Stage is immutable.

Parameters:
namestr

Name of the subtotal, which becomes the attribute co.pl.<name> and the label key of the statement row. Required. A layout needs a stage called net_income.

formulaCallable[[PL], Series]

Function that receives the entity’s co.pl and returns the subtotal as a Series, such as lambda pl: pl.revenue.total - pl.cogs.total. It may use the total of any kind of the layout and any stage listed before this one. Required.

Attributes:
namestr

The subtotal’s name.

formulaCallable[[“PL”], “Series”]

The function that builds the subtotal.

Raises:
AttributeError

At construction through Model.entity, if formula reads a kind or stage that the layout does not have, or a stage that is listed after this one.

See also

boki.layouts.Layout

A layout lists its stages among its kinds.

boki.layouts.Kind

A line kind that a stage adds up.

Notes

Expenses are positive amounts, so a subtotal subtracts them. Model.entity calls formula when it builds the statement, and may call it again when the first amount fixes the entity’s currency, so write it as a function of pl only.

Examples

A subtotal from the total of two kinds, evaluated on an entity:

>>> import boki as b
>>> from boki.layouts import Kind, Layout, Stage
>>> layout = Layout(
...     "Simple",
...     pl=(Kind("sales", "income", cf="pl", mechanism="pl"), Kind("costs", "expense", cf="pl", mechanism="pl"),
...         Stage("net_income", lambda pl: pl.sales.total - pl.costs.total)),
...     bs=(), interest_kind="costs", depreciation_kind="costs")
>>> m = b.Model("Stage", start="2027-01", periods=1, step=b.year)
>>> co = m.entity("A", layout=layout)
>>> sales = co.pl.sales("Sales", 100)
>>> costs = co.pl.costs("Costs", 30)
>>> m.render().value(co.pl.net_income, "2027")
70
class boki.layouts.Standard(name, general, labels=<factory>, display=<factory>, bs_order='current_first')#

An accounting standard: its name, its general layout, and how its statements are named and ordered.

b.IFRS, b.USGAAP and b.JGAAP are the built-in standards, and Model.entity(standard=...) takes one. The standard decides which general layout an entity gets when it names none, the names its statements show (IFRS says “Profit for the year”, US GAAP “Net income”), the order of the Excel balance sheet, and the standard-specific accounting that the rest of boki applies by standard.name (goodwill, deferred tax, OCI). A Standard is immutable.

Parameters:
namestr

The standard’s name, such as "IFRS", "US GAAP" or "J-GAAP". Required. boki’s standard-specific rules branch on these three names.

generalLayout

The layout an entity uses when it names none. Required.

labelsdict, optional

Display names by kind, as {kind: name}. The default is an empty dict. The current statements do not read it; they take names from display and the style.

displaydict, optional

Statement names by language, as {"en": {key: name}}. The standard’s name for a key comes before the style’s and the common table’s, such as {"en": {"net_income": "Profit for the year"}}. The default is an empty dict.

bs_orderstr, default=”current_first”

Order of the Excel balance sheet: "current_first" (US GAAP and J-GAAP: current assets first, liabilities before equity) or "noncurrent_first" (IFRS: non-current assets first, equity before liabilities).

Attributes:
namestr

The standard’s name.

generalLayout

The general layout.

labelsdict

Display names by kind.

displaydict

Statement names by language.

bs_orderstr

The balance-sheet order.

See also

boki.layouts.Layout

The layout a standard owns.

Notes

The built-in standards differ in more than names. For example, goodwill is amortized under J-GAAP and not under IFRS, and the standards disagree on what belongs in operating income and EBITDA; see the notes of the line makers and of Group.own. repr of a standard is <Standard NAME>.

Examples

The built-in standards and what each gives an entity:

>>> import boki as b
>>> b.IFRS.name, b.IFRS.general.name, b.IFRS.bs_order
('IFRS', 'IFRS general', 'noncurrent_first')
>>> m = b.Model("Standard", start="2027-01", periods=1, step=b.year)
>>> m.entity("A", standard=b.USGAAP).layout.name
'US GAAP general'