> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nmbr.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Journal Entry Segment Templates

> Build a custom, multi-part code for the Journal Entry report.

Some companies use **segmented account codes** in their journal entries: a single account reference assembled from parts, such as a base code and a department, combined into a value like `5000-ENG` rather than one flat code.

<Accordion title="New to journal entries?" icon="lightbulb">
  A **journal entry** is how accounting records a transaction: each line assigns an amount to an account, like wages or taxes payable. A payroll produces one, and the **Journal Entry report** is that entry laid out line by line.
</Accordion>

Nmbr offers **segment templates** to build that code on the Journal Entry report. You define how the parts are assembled, and Nmbr renders the result into two columns, **Generated Expense Code** and **Generated Liability Code**.

<Accordion title="Why two sides: expense and liability?" icon="lightbulb">
  A payroll line records both an **expense** (the employer's cost) and the **liability** it creates (what's owed until it's paid out). That's why segments, templates, and the report columns all come in an expense and a liability form.
</Accordion>

Segment templates are optional, and each applies only to the line items it matches. They build on Nmbr's accounting concepts, so if those are new to you, start with the [Accounting](/guides/advanced/accounting) guide.

## How this differs from an accounting code

A generated code is not an accounting code. Both can appear on the Journal Entry report, but they come from separate systems.

**[Accounting codes](/guides/advanced/accounting)**

* A distinct entity in Nmbr: a fixed expense or liability code.
* Often imported from an external accounting platform, and drive the automated export back to it (Xero, QuickBooks).
* Assigned to a line manually, or by accounting code rules based on attributes like the primary tag or line item type.
* Rules are driven by a single primary tag group.

**Generated codes**

* Computed when the Journal Entry report is built, from a segment template you define globally.
* Can combine any number of tag groups, accounting codes, and freeform text.
* Not a fixed, labelled value from a known set.
* Never part of the export to Xero or QuickBooks.

## How a generated code is built

A generated code is built from a **segment template** on an accounting code rule: an ordered list of **segments**, some of which read values from tags.

### The segment template

A segment template is defined on a **global** accounting code rule (one with no tag or tag group scope). A rule holds one template per side, in `expense_segment_template` and `liability_segment_template`. When the Journal Entry report is built, Nmbr renders the template's segments in order and joins them into that side's generated code.

### Segments

Each segment resolves to a value from one of three sources:

* **`accounting_code`**: a fixed accounting code's `code`. Set `id` to the accounting code's `id`.
* **`tag_group`**: a code carried by one of the row's tags. Set `tag_group_id` to the tag group's `id`.
* **`text`**: a literal string you supply.

A `tag_group` segment reads its value from the tag itself. Each tag carries a per-side code, `expense_segment` and `liability_segment`, set when you create or update the tag (see [Setting up tag groups and tags](/guides/advanced/tagging#setting-up-tag-groups-and-tags)). On each report row, the segment takes the code from the row's tag in that group.

### Formatting

The `accounting_code` and `tag_group` segments also accept optional formatting:

* `prefix` / `suffix`: literal strings wrapped around the resolved value.
* `transform`: an ordered list of `uppercase`, `lowercase`, `alphanumeric` (strip non-alphanumeric characters).
* `max_length`: truncate the resolved value to this many characters.
* `fallback`: a value to use when the segment resolves empty. Without one, an empty segment is dropped, prefix and suffix included.

Each of those segments is processed in a fixed order: resolve the raw value, apply the fallback if it's empty, run the transforms, truncate to `max_length`, then wrap with `prefix` and `suffix`.

### The result

When a rule's template matches a line, the rendered string appears in **Generated Expense Code** or **Generated Liability Code** on the Journal Entry report. The columns are always present; on a given line only the relevant side is filled. Bank, payable, and total rows stay blank.

### Which template applies

Templates resolve on their own when the report is built, separately from how [accounting codes are routed](/guides/advanced/accounting#rule-precedence) during payroll calculation. The precedence is the same: for each side of a line, Nmbr uses the template from the most specific matching global rule.

1. `business_preset_id`
2. `type` + `subtype`
3. `type`
4. the catch-all: no `type`, `subtype`, or `business_preset_id`

The expense and liability sides resolve independently, so a line's two columns can come from different rules. Set one catch-all template as a default, then override it for specific line item types.

Because templates are defined only on global rules, this precedence has no tag dimension. A tag or tag-group-scoped rule never carries a template.

## Example

Give two tags an expense segment. The "Engineering" department tag:

```json theme={null}
{
  "expense_segment": "eng"
}
```

And the "Toronto" location tag:

```json theme={null}
{
  "expense_segment": "tor"
}
```

Set an expense template on the global rule for wages. It emits the wages accounting code, then the department, then the location, each uppercased. The department falls back to `GEN` when a row has none:

```json theme={null}
{
  "segments": [
    { "type": "accounting_code", "id": "agcode_01HXWAGES00000000000000" },
    { "type": "tag_group", "tag_group_id": "taggrp_01HXDEPT00000000000000", "prefix": "-", "transform": ["uppercase"], "fallback": "GEN" },
    { "type": "tag_group", "tag_group_id": "taggrp_01HXCITY00000000000000", "prefix": "-", "transform": ["uppercase"] }
  ]
}
```

For an Engineering wages line in Toronto where the wages code is `5000`, the report's **Generated Expense Code** reads:

```
5000-ENG-TOR
```

A wages line with no department resolves the second segment to its fallback: `5000-GEN-TOR`.

## Behaviour and limits

* **Global rules only.** Templates are defined on rules with no tag or tag group scope.
* **Report only.** Generated codes never affect the accounting code that syncs to the connected accounting platform. See [Exporting journal entries](/guides/advanced/accounting#exporting-journal-entries).

## Related

* [Tagging and Journal Entry Tracking](/guides/advanced/tagging) for tags, tag groups, and how they split allocations.
* [Accounting](/guides/advanced/accounting) for accounting codes, rules, and the Journal Entry report.
