# Taxes in IMS2 — Business Logic and Reference Setup Guide

This document is for specialists who maintain reference data (accountant,
system administrator). Regular users never deal with tax setup while
entering documents — everything is configured once here and then applied
automatically.

---

## 1. Main rule: receiving is manual, expenditure is automatic

| Document type | How tax is determined |
|---|---|
| **Receiving** | Entered **manually** by the operator in the document's extra sums (Charges → Taxes), taken as-is from the supplier's invoice. The system never calculates or validates it. |
| **Expenditure / Sale** | Calculated **automatically** when the document is processed. The operator enters nothing. Rolling the document back removes the calculated tax; re-processing calculates it afresh (no doubling). |

**Why**: the tax amount on a receiving document is whatever the supplier
billed — it must not be changed, even if it "looks wrong" (accountants have
confirmed this) — it's entered exactly as stated. On a sale/expenditure,
however, computing the tax correctly is our own responsibility, so the
engine handles it automatically.

If an expenditure document ends up with no calculated tax, the tax
configuration for the involved item(s)/operation is simply missing in the
reference data (see §3) — this is not a system bug, it's an unconfigured
reference.

---

## 2. Where tax rates come from

Three references participate in the calculation:

```
Item     ──▶  Tax Group      ──▶┐
                                  ├──▶ Tax Rate
Document ──▶  Operation      ──▶┘
```

### 2.1. Priority: operation overrides tax group

For each document line, up to 3 taxes are resolved, slot by slot (1st, 2nd, 3rd):

- If the document's **operation** has a tax code set for that slot
  — **it** is used.
- If the operation's slot is empty (0) — the tax code from the line's
  **item tax group** is used instead.

**Example**: a regular sale (operation "Sale") has no taxes configured of its
own — the item's tax group is used (e.g. "Standard Goods" → 20% VAT).
Operation "Export Sale" has slot 1 explicitly set to "0% Export" — an export
sale then goes through with no tax **even though** the item has its normal
VAT tax group.

### 2.2. The rate depends on the document date

The rate reference stores **validity periods** — when a rate
changes, the old record is not deleted, it is closed with an end date, and a
new record is added. Calculation always uses the rate that was in effect **on
the document's date**, not the current one — so historical
documents never silently move to a new rate retroactively.

---

## 3. Reading the three key tax fields

| Field | Question | Values |
|---|---|---|
| **Calc Level** | At what level is it calculated? | `0` = per line (each item line separately, then summed), `1` = per document (calculated once on the grand total) |
| **Apply Order** | In what order relative to other taxes? | integer — lower applies earlier |
| **Base** | Which amount is the rate applied to? | `0` = the original amount (ignoring other taxes), `1` = original amount **+ taxes already computed with a lower Apply Order** — tax-on-tax |

### Worked examples

**Example A — simple VAT (single rate, EU/most countries)**

Line: 100.00, tax "VAT 20%": `Calc Level=0` (per line), `Apply Order=1`, `Base=0`.

```
Step 1: VAT 20% of 100.00 = 20.00
Total tax: 20.00
```

**Example B — GST + QST (Canada, tax-on-tax)**

Line: 100.00. The item's tax group has 2 taxes configured:
- slot 1 = "GST 5%": `Apply Order=1`, `Base=0` (from the original amount)
- slot 2 = "QST 9.975%": `Apply Order=2`, `Base=1` (from the amount **including** GST)

```
Step 1: GST 5% of 100.00              = 5.00
Step 2: QST 9.975% of (100.00+5.00)=105.00 = 10.47
Total tax: 15.47
```

**Example C — per-line excise + per-document sales tax**

Document with two lines: an excisable item at 50.00 and a regular item at 30.00.
- "Excise 10%" (only on the excisable item): `Calc Level=0` (per line), `Apply Order=1`, `Base=0`
- "Sales Tax 8%" (set on the document's operation): `Calc Level=1` (per document), `Apply Order=2`, `Base=0` (calculated from the line total, no tax-on-tax needed here)

```
Step 1 (per line "Excise"): 10% of 50.00 = 5.00
Step 2 (per document "Sales Tax"): 8% of (50.00+30.00)=80.00 = 6.40
Total tax: 11.40
```

**Example D — tax-free export (operation override)**

Item with a regular tax group "Standard Goods" (VAT 20%), but the
document's operation is "Export Sale" with slot 1 = "Export 0%".

```
Operation overrides the item's tax group → "Export 0%" is used instead
Step 1: Export 0% of 100.00 = 0.00
Total tax: 0.00
```

---

## 4. Setup guide: filling in the references

Tax rates ("Taxes") and tax groups ("Category of Goods")
are ordinary references, opened through **Directory (F9)** → pick the name
from the list (the same universal editor used for every reference — see
`docs/en/manual/24_directory.md`). The operation type is the only
one of the three with its own dedicated button on the **Tools** panel,
bypassing the general Directory menu (see
`docs/en/manual/13_tools_overview.md`).

### 4.1. Directory → Taxes — create a new tax

1. New → fill in:
   - **Tax code** — a number you choose, must be unique among
     **currently active** taxes — later periods of the same tax reuse the
     same code, see below
   - **Name** — e.g. "VAT 20%", "GST", "Export 0%"
   - **Rate** — in % (fractional rates supported — 9.975)
   - **Calc Level / Apply Order / Base** — see §3
   - **Valid From** — the date the rate takes effect
   - **Valid To** — leave empty (this is the current active period)
   - **Accounting code** — if bookkeeping is used (optional)

### 4.2. Directory → Taxes — changing an existing tax's rate

**Do not edit the existing record directly** — doing so loses the record of
what rate applied previously (breaking calculations for older documents).
Instead:

1. Find the tax's current active record (**Valid To** is empty).
2. Set its **Valid To** = the day before the new rate takes effect.
3. Create a **new** record (New) with the same tax code, the new rate,
   a new **Valid From** (the effective date), **Valid To** left empty.

The old record stays in the reference forever — it's history, not clutter.

### 4.3. Directory → Category of Goods — attach a tax to an item group

1. Open (or create) a tax group.
2. In the three tax-slot fields click "..." — this opens the tax
   list, pick the one you need. If several taxes apply
   (tax-on-tax), fill in several slots — the order between them is
   determined by each tax's own **Apply Order** field (§3), not by which
   slot you put it in here.
3. Items are attached to a group via the item card's tax group field.

### 4.4. Tools → Operations — override tax for an operation

Use this when a specific kind of operation must **always** be taxed a
certain way regardless of the item (export, tax-free services, a special
rate, etc.):

1. Open the operation (e.g. "Export Sale").
2. In the three tax-slot fields, pick the tax(es) that should
   apply for THIS operation.
3. Leaving a slot empty (0) means that slot falls back to the line's item
   tax group (no override for that slot).

---

## 5. Verifying the calculation — "how was this tax calculated" report

For any processed document you can get a step-by-step breakdown of the
calculation (no need to take the system's word for it) — the same logic as
the examples in §3, but for a specific document: each step with the tax
name, level, base, rate, and amount. Ask the developer / use the in-app
report (F5 in the relevant section) when you need to explain to an
accountant where a figure in a specific document came from.

---

## 6. Checklist when setting up a new tax

- [ ] There is a record in **Directory → Taxes** with the correct rate and **Valid From**
- [ ] **Calc Level** is set (per line / per document)
- [ ] **Apply Order** is set, if this tax participates in a chain with others
- [ ] **Base** is set, if this tax is "on top of" another tax
- [ ] The tax is attached to either an item's **Tax Group** or a document's
      **Operation** (or both — the operation wins)
- [ ] Verified on a test expenditure document via the "how was this tax
      calculated" report
