# Export a month to DATEV

> Reconcile every original and supplemental DATEV batch against the adviser’s posted turnover and retain the evidence for month close.


Finance can export posted EUR entries from accountingd as DATEV Buchungsstapel files (EXTF 700/21/13). Each receipt identifies the journal position, chart revision, included entries, file digests and debit and credit turnover per target account. A later posting for the same month goes into a supplementary batch.

## Configure the fiscal client

1. Obtain the adviser and client numbers, fiscal year start month, account length and an approved mapping for each journal account. VAT is already posted as a separate leg. The target accounts must accept those entries without calculating tax a second time. Gross prepaid usage requires separate approval.
2. Install a JSON profile on the accountingd host and set `TELCO_DATEV_PROFILE` to its path. The [synthetic example](../examples/datev.example.json) covers one test receivable. Replace its accounts and numbers with the approved mapping. Give each change a revision and restart accountingd.
3. Give each preparer a named `finance` key on accountingd. On billd register a separate `finance` key for accountingd to read `GET /finance-close/{period}`. Set accountingd’s `TELCO_BILLD_URL` and `TELCO_CLOSE_TOKEN_FILE` for this connection. Use HTTPS across hosts in production. Connect the finance client directly to accountingd through your authenticated private ingress.

A finance credential can read historical batches in this accounting installation, including earlier profile revisions and fiscal clients. Assign it only to preparers authorized for that history.

## Preview, issue and download

Use a fresh restricted directory for each batch. Set `TELCO_ACCOUNTING_URL` to the private accounting origin and `TELCO_FINANCE_TOKEN_FILE` to the file containing your named finance key. The example uses Bash, curl, jq and uuidgen.

```bash
set -euo pipefail
umask 077
MONTH=2026-11
BASE="${TELCO_ACCOUNTING_URL:?}/finance/exports"
finance_request() {
  printf 'Authorization: Bearer %s\n' "$(<"${TELCO_FINANCE_TOKEN_FILE:?}")" |
    curl --fail-with-body -sS --header @- "$@"
}
finance_request "$BASE/preview?month=$MONTH" > preview.json
jq '{month, journal_bound, entries, rows, controls}' preview.json
```

Compare both sides of each mapped account with the ledger. An empty preview has no new entries. Finance checks the service cutoff with its other senders, including the carrier or charging system. The preview checks committed pending work at billd and accountingd. Commands still held by an external sender can arrive after the snapshot.

```bash
uuidgen > batch-id
jq '{month,preview_digest}' preview.json > request.json
batch=$(cat batch-id)
finance_request -H 'Content-Type: application/json' \
  -X PUT --data-binary @request.json "$BASE/$batch" > receipt.json

while read -r part; do
  file="EXTF_${batch}_${part}.csv"
  finance_request "$BASE/$batch/files/$part" -o "$file"
  expected=$(jq -r --argjson part "$part" '.files[] | select(.part == $part) | .sha256' receipt.json)
  printf '%s  %s\n' "$expected" "$file" | shasum -a 256 -c -
done < <(jq -r '.files[].part' receipt.json)
```

If the reply is lost, repeat the PUT with the saved UUID and request. A stale preview requires a fresh preview and review. Retrieve an existing receipt with `GET /finance/exports/{id}`. Give the adviser the original Windows-1252 CSV bytes and import every issued part once. [Accounting OpenAPI](../schemas/accounting.openapi.yaml) · [DATEV booking batch](https://developer.datev.de/de/file-formats/details/datev-format/format-description/booking-batch)

## Find every batch issued for a month

Run the register before handing over a month or when a receipt is missing. Use the journal and the adviser/client numbers from the close you are checking, including for a historical profile.

```bash
telco finance batches 2026-11 --journal telco.local/books --adviser 12345 --client 1234 > register.json
jq -r '.batches[] | [.id, .issued_on, .profile_revision, .entries, .rows, .parts] | @tsv' register.json
```

The CLI prints the original batch first, followed by its supplements in issuance order. A successful empty list means accountingd had issued no batch for those four selectors when the read began. Fetch a missing receipt with `GET /finance/exports/{id}` and match the ID against the adviser’s import log before sending its files again. Run the register again after a late posting has been exported.

For registers larger than 10,000 batches, read the private API in pages. Use `GET /finance/exports` with the same four selectors and an optional `limit` of 1–100 (default 50). Pages run newest first. Keep the selectors fixed and pass each opaque `next` value as `cursor` until `next` is null. A later issuance appears on a fresh traversal. [Accounting OpenAPI](../schemas/accounting.openapi.yaml)

## Reconcile the adviser’s import

1. Keep the adviser’s import log and check warnings or rejected rows. Obtain the **posted EUR debit and credit movements for this batch** from the target client. If the client reports only monthly turnover, take readings immediately before and after an isolated import. Subtract each column for the accounts listed in the target posting log, including accounts whose movements cancel to zero. Other postings between readings invalidate this method. Repeat for each supplementary batch. [DATEV guidance on reprocessing and import logs](https://help-center.apps.datev.de/documents/9295993)
2. Save `imported.json` using the downloadable target turnover example. Record the batch UUID from the handoff and the month, adviser and client of the actual import. Include one row per account touched by this batch, including unexpected accounts. Supply each side in whole cents as a decimal string. Preserve a minus sign if the target booked a reversal on the original side (Generalumkehr). [DATEV Generalumkehr](https://www.datev.de/students/course/view.php?id=1162)
3. Run the comparison in the restricted close directory.

```bash
telco finance reconcile receipt.json imported.json > comparison.json
```

Exit **0** means every reported account and both turnover columns agree. Exit **1** lists differences, with receipt and imported cents side by side. A missing side is `null`. Check the affected accounts against the target postings and import log before correcting anything. Exit **2** writes a JSON error to standard output. For `finance.scope`, `fields` names the batch, month, adviser or client identifiers to check. Keep both inputs with the comparison, which identifies their exact bytes by SHA-256.

If a receipt issued by an older installation fails the preview digest check, keep the issued files and compare the two turnover columns per account manually with the adviser. Reissuing or importing the batch again can duplicate postings.

For the first operational close, have the adviser test an invoice, a credit and a payment with the approved chart and tax settings. Account totals alone cannot establish that every booking reference is correct or that the adviser approved the import.

## Review the complete month

Save the issued register as `register.json`. In a fresh restricted directory, collect **every** original receipt and target extract supplied for that fiscal scope. Keep unexpected batches in the inventory so the review can flag them. Each target extract must cover all file parts for its batch. Use the canonical lowercase batch UUID as the filename.

```text
register.json
evidence/
  receipts/<batch-uuid>.json
  imports/<batch-uuid>.json
```

Keep the evidence directory still during the review. Place reports beside it. The command runs offline and accepts regular local files up to 16 MiB each and 256 MiB in total, with at most 10,000 batches in the register and 10,000 files per evidence folder. Use actual files for the register and both evidence folders; symbolic links are refused.

```bash
telco finance review-month register.json evidence > review.json
```

| Exit | Result | Finance action |
|---|---|---|
| 0 | `ready_for_review` | Every registered issue has matching evidence. Refresh the register immediately before the adviser decides and rerun against it. |
| 1 | `blocked` | Inspect each batch’s flags. Recover a missing receipt, obtain a missing target extract, or investigate an unexpected issue. For a turnover difference, run `finance reconcile` on that pair to see both sides by account. |
| 1 | `empty_needs_review` | Confirm independently whether this client was expected to have no issued batches for the month. |
| 2 | Invalid input | Correct the input and rerun. For a malformed receipt or extract, use the reported batch UUID and input type to locate the file. An inventory error calls for checking the directory layout, regular files and limits. |

Keep each decided review in its own restricted directory with the exact register, receipts and target extracts. Stage a fresh directory for a supplement or replacement input. Record the adviser’s decision, date, reviewer reference, target importer version, chart and tax approval for each profile digest, and the SHA-256 of the final report (`shasum -a 256 review.json`). Verify the journal cutoff separately for unissued bookings.

### Try a synthetic month

The [issued receipt](../examples/finance/receipt.json) and [original booking file](../examples/finance/EXTF_example.csv) cover a €119 invoice, a €20 payment and a full credit in November 2026. The [target turnover example](../examples/finance/imported.json) comes from posting the five rows in a separate SQLite training ledger. Reconcile the two JSON files to check four accounts with €258.00 debit and €258.00 credit turnover.

Download the [example register](../examples/finance/register.json), receipt and target turnover into a fresh directory as `register.json`, `receipt.json` and `imported.json`. Run:

```bash
umask 077
mkdir -p evidence/receipts evidence/imports
batch=$(jq -r '.id' receipt.json)
cp receipt.json "evidence/receipts/$batch.json"
cp imported.json "evidence/imports/$batch.json"
telco finance review-month register.json evidence > review.json
jq '{outcome, registered, compared, matched}' review.json
```

The example reports one matched batch. Move its target file outside the evidence directory and rerun. The command exits 1 and identifies the batch with `missing_import`.
