
# HZN-API-006 — ERP Interface: Cost Bookings, Budgets, Events

**How a commercial system (ERP) exchanges budget data with Horizon.**
Interface description in the sense of GEFMA 410 for catalogue 14, criterion 5
of GEFMA 444 (HZN-ADR-0251
§C.2: *"import and REST"*).

> **Language.** This document is written in English because
> HZN-STD-001 §10 requires it for public
> interface documentation. Every other Horizon document remains German.

> **Status.** Everything below runs in the product as of 2026-11-05 and is
> checked by tests named in each section. **Not part of it:** a dedicated SAP
> or DATEV adapter (built when a customer asks for it, `ADR-0251` §C.2).

---

## 1. Overview

| # | Data object | Direction | Trigger | Format | Section |
|---|---|---|---|---|---|
| 1 | Cost bookings (invoices, postings) | ERP → Horizon | ERP export, uploaded by a person or a script | CSV or xlsx, import plan `kostenbuchungen-erp` | §2 |
| 2 | Budgets, cost centres, suppliers | both | on demand | JSON over HTTPS | §3 |
| 3 | Changes in Horizon (e.g. a booking released) | Horizon → ERP | event in Horizon | JSON webhook, signed | §4 |

**Leading system.** The ERP leads for postings and creditor accounts. Horizon
leads for budgets, their release, tolerance and coverage rings (catalogue 14,
criteria 1–11). A booking that exists in both is recognised by its voucher
number and budget number (§2.3), never by its position in a file.

## 2. Cost bookings from the ERP (file import)

### 2.1 The file

One row per posting line. Header row required; the column names below are
what the shipped plan expects. Column order is free. Separator `;` or `,`,
UTF-8, or an xlsx workbook (first sheet).

| Column | Horizon field | Required | Format | Notes |
|---|---|---|---|---|
| `Belegnummer` | `costBooking.voucherNumber` | yes | text | Part of the key (§2.3) |
| `Buchungsdatum` | `costBooking.bookingDate` | yes | `14.10.2026`, `2026-10-14` or an Excel day number | |
| `Buchungstext` | `costBooking.name` | yes | text | |
| `Betrag` | `costBooking.amount` | yes | `1.234,50` or `1234.50` | Currency of the tenant (EUR by default) |
| `Budgetnummer` | `costBooking.budget` | yes | text | Matched against `budget.budgetNumber` (internal order, WBS element); part of the key |
| `Kreditorenkonto` | `costBooking.supplier` | no | text | Matched against `organisation.creditorAccount`; empty leaves the supplier empty |

Example:

```
Belegnummer;Buchungsdatum;Buchungstext;Betrag;Budgetnummer;Kreditorenkonto
RE-2026-0815;14.10.2026;Dachrinne erneuert;1.234,50;IA-4711;70001
RE-2026-0816;20.10.2026;Kleinmaterial;89,90;IA-4711;
```

A customer whose ERP names its columns differently changes the plan once on
the page "Daten importieren" and saves it; a shipped plan the customer has
changed is never overwritten by a package update (`ADR-0089` 1.9.0 No. 4).

### 2.2 Matching references

A budget is found by its **budget number**, a supplier by its **creditor
account** — the ERP does not know Horizon's names. Matching ignores case and
surrounding blanks. No match, or more than one, rejects **that row** with
`recordImport.referenceNotFound` or `recordImport.referenceAmbiguous`; the
other rows are imported. The error report is available as a file with the
cells as they were in the upload (`ADR-0089`, schema 296).

### 2.3 Repeatable runs

The key of a row is `Belegnummer` + `Budgetnummer`. Horizon remembers it per
tenant and plan (`meta.import_key`, schema 285). The same file a second time
creates nothing; a row whose amount or text changed updates the booking it
created before. A voucher split across two budgets gives two bookings.

### 2.4 State of an imported booking

An imported booking is created in the initial state **"geplant"** of its
state model, like one entered by hand, and then — in the same row — takes
the transition **"Direkt abrechnen"** named by the plan (`transition`,
HZN-ADR-0089 addendum 1.12.0). Within the tolerance of
its budget it ends up **"abgerechnet"** and counts as "Ist" right away.
Above the tolerance the transition's condition is not met: the booking
stays "geplant" and waits for "Direkt abrechnen (Freigabe)" by the budget
lead (catalogue 14 No. 3, 8). The run result counts these per level as
`waiting`. Bookings in order of the file: the first ones that still fit
are settled, the rest wait.

The import does not write the state field: a state change is an action with
its own rights, never a field write ([HZN-API-002](guide.html) §6.4).
Only the new booking takes the transition; a row that changes an existing
booking leaves its state alone.

### 2.5 Running an import

Interactively: page "Daten importieren", the plan is suggested when the
header matches. By HTTP (same rights as the page — permission to create
records of the type):

1. `POST /api/record-import/file` `{"action": "announce", "name": "buchungen.csv"}`
   → `201 {"file", "uploadUrl", "validUntil"}`; upload the bytes to
   `uploadUrl`; then `{"action": "complete", "file": <id>}`. The file is
   virus-scanned before it is read.
2. `POST /api/record-import/run` `{"file": <id>, "planKey": "kostenbuchungen-erp", "dryRun": true}`
   → `202` with the run id. `GET /api/record-import/run?run=<id>` returns
   state and result (created, changed, unchanged, rejected rows). Repeat with
   `"dryRun": false` to write.

Tests: `DomainPackageIT.erpCostBookingPlanIsShipped`, `RecordImportIT`,
`RecordImportOverHttpIT`.

## 3. Reading and writing budgets, cost centres, suppliers (REST)

The general record API applies ([HZN-API-002](guide.html),
conventions in HZN-API-001). Relevant object types of
the package `fachmodell-basis`:

| Object type | Fields of interest for the ERP |
|---|---|
| `budget` | `name`, `budgetNumber`, `plannedAmount`, `fiscalYear`, `costCentre`, `parentBudget`, `status`; computed: `bookedTotal`, `commitment`, `actualCost`, `remainingAmount`, `utilisation` |
| `costBooking` | `name`, `budget`, `amount`, `bookingDate`, `voucherNumber`, `supplier`, `billingStage` (state) |
| `costCentre` | `name`, `costCentreNumber` |
| `organisation` | `name`, `creditorAccount`, `debtorAccount` |

Read the model of the tenant before relying on a field — a customer may have
renamed or added fields (`HZN-API-002` §2.5).

## 4. Changes from Horizon (outbound events)

A tenant can subscribe up to five HTTPS endpoints
(`/api/outbound-events`, page "Ausgehende Ereignisse", `ADR-0269`). Events:
`record.created`, `record.changed`, `record.stateChanged`,
`record.commented`, `record.deactivated`, `record.approvalDecided`, filtered
by object type and an optional condition ("only if"). Each delivery is signed
with the subscription's key and retried with back-off; the format is
described in `asyncapi.json` next to this document.

Typical ERP use: subscribe to `record.stateChanged` for `costBooking` with
the condition `billingStage == "beauftragt"` to receive orders released in
Horizon.

## 5. Authentication and limits

**Programs use a service account** (HZN-ADR-0084
N.2–N.4). A service account is an account without a person: it has roles like
a user and may do nothing until it gets one.

1. An administrator with "manage users" creates it under *Settings → Access →
   Service accounts* and copies client ID (`hzsvc_c_…`) and secret
   (`hzsvc_s_…`). The secret is shown once; Horizon stores only its hash.
   A secret is valid for twelve months; several can be valid at once, so a
   change costs no downtime: on the same page, *Issue new secret* adds one to
   an existing account, the program switches to it, then the old one is
   revoked (`HZN-ADR-0084` N.6).
2. The administrator gives it a role under *Permissions*. To write over the API it
   also needs the capability `api.write`.
3. The program exchanges client ID and secret for a token:

   ```
   POST /api/service-accounts/token
   Content-Type: application/x-www-form-urlencoded

   grant_type=client_credentials&client_id=hzsvc_c_…&client_secret=hzsvc_s_…
   → 200 {"access_token": "hzsvc_t_…", "token_type": "Bearer", "expires_in": 3600}
   ```

   HTTP Basic authentication (`client_secret_basic`) works as well. A wrong
   or revoked secret, or a deactivated account, gives `401
   {"error": "invalid_client"}`.
4. Every call carries `Authorization: Bearer hzsvc_t_…`. The tenant is the
   one of the service account; there is nothing to choose.

People can still use an access token of their own (`ADR-0205`); everything
then runs with that person's rights.

- **Limits:** the size limit of an upload applies (`FileService`); the token
  address is rate-limited per caller like every path under `/api/`. On top,
  a client ID may exchange **60 times per hour**; the 61st gets `429
  {"error": "temporarily_unavailable"}` with `Retry-After` in seconds
  (`HZN-ADR-0084` N.8). A token is valid for an hour: keep it and exchange
  again shortly before `expires_in` runs out, not before every call. Failed
  exchanges with a wrong secret do not count.

## 6. Error handling

| Situation | Behaviour |
|---|---|
| Header does not fit the plan | Run refused before it starts, column named |
| Budget number or creditor account not found / ambiguous | That row rejected, others imported, error report as file |
| Value cannot be read (amount, date) | That row rejected with the cell |
| Same file twice | Nothing created (§2.3) |
| Webhook endpoint down | Retries with back-off; the subscription shows waiting deliveries |
