feat(invoiceintake): invoice@ mails become draft Eingangsrechnungen #82

Merged
sharang merged 3 commits from feat/invoice-intake into main 2026-10-20 08:15:33 +00:00
Owner

Incoming invoices for invoice@ (orca-infra #121, Collectives ERP/08), with the split agreed on 2026-10-20: Strato forwards the originals to DUO, ERPNext's Email Account "Eingangsrechnung" reads the mailbox, and this service only turns those mails into drafts.

What it does per mail

Mail Result
e-invoice (XRechnung XML, ZUGFeRD PDF) E Invoice Import draft. eu_einvoice reads and validates it; the person maps supplier and lines, submits the import and creates the Purchase Invoice from it (the app's own flow)
other PDF, supplier known by sender address or domain draft Purchase Invoice: supplier, posting date = mail date, one placeholder line EINGANGSRECHNUNG at 0, the original attached again
no PDF/XML, unknown/freemail/own-domain sender, or ERPNext refuses the draft ToDo on the mail, assigned to INTAKE_ASSIGN_TO
  • It never submits, cancels, changes or deletes a document, and never touches the mail. The client refuses docstatus; a test pins the written doctypes to Purchase Invoice, E Invoice Import, File and ToDo.
  • No state of its own: which mails are done is read from ERPNext (intake_communication, unique on both doctypes; ToDo reference). Cancelling a ToDo makes the next run try again, for example after the supplier has been created.
  • Logs carry Communication and document names and counts only, no senders, subjects or amounts.
  • No amounts yet: extraction from plain PDFs via LiteLLM is a follow-up PR (PDF→text in the image, plus its own LiteLLM key).

erpsetup

It adds:

  • the custom fields Purchase Invoice.intake_communication and E Invoice Import.intake_communication (Link to the Communication, unique, read-only; Frappe stores an empty unique field as NULL, so hand-made invoices do not collide);
  • the unit Stück, the item group Eingangsrechnungen and the item EINGANGSRECHNUNG (purchase only, non-stock);
  • the item's default expense account, SKR04 6300 Sonstige betriebliche Aufwendungen. If that account is missing, the step reports "missing (not created)".

Run

ERP_BASE_URL=https://erp.breakpilot.com ERP_API_KEY=… ERP_API_SECRET=… go run ./cmd/invoiceintake -once -dry-run

As a service: image /invoiceintake, every INTAKE_INTERVAL (10m). Other settings: INTAKE_SINCE, INTAKE_ASSIGN_TO, INTAKE_EMAIL_ACCOUNT (Eingangsrechnung), INTAKE_COMPANY (Breakpilot GmbH), INTAKE_OWN_DOMAINS (breakpilot.com).

The API user only needs the standard roles Accounts User, Purchase User and Inbox User, plus the Email Account under its User Emails, so no permission tables are changed. Those roles could submit, but the service never does.

Tests

go test ./internal/invoiceintake ./internal/erpsetup runs against a fake ERPNext. It covers:

  • the e-invoice path, the plain-PDF draft (by address and by domain) and every ToDo case;
  • idempotency, including a fresh process;
  • a cancelled ToDo being retried;
  • a refused draft;
  • server errors on the e-invoice import (retried, then treated as a plain PDF);
  • dry run, bad key, and paging over 1234 mails.

🤖 Generated with Claude Code

Incoming invoices for invoice@ (orca-infra #121, Collectives ERP/08), with the split agreed on 2026-10-20: Strato forwards the originals to DUO, ERPNext's Email Account "Eingangsrechnung" reads the mailbox, and this service only turns those mails into drafts. ## What it does per mail | Mail | Result | |---|---| | e-invoice (XRechnung XML, ZUGFeRD PDF) | **E Invoice Import** draft. eu_einvoice reads and validates it; the person maps supplier and lines, submits the import and creates the Purchase Invoice from it (the app's own flow) | | other PDF, supplier known by sender address or domain | **draft Purchase Invoice**: supplier, posting date = mail date, one placeholder line `EINGANGSRECHNUNG` at 0, the original attached again | | no PDF/XML, unknown/freemail/own-domain sender, or ERPNext refuses the draft | **ToDo** on the mail, assigned to `INTAKE_ASSIGN_TO` | - It never submits, cancels, changes or deletes a document, and never touches the mail. The client refuses `docstatus`; a test pins the written doctypes to Purchase Invoice, E Invoice Import, File and ToDo. - No state of its own: which mails are done is read from ERPNext (`intake_communication`, unique on both doctypes; ToDo reference). Cancelling a ToDo makes the next run try again, for example after the supplier has been created. - Logs carry Communication and document names and counts only, no senders, subjects or amounts. - No amounts yet: extraction from plain PDFs via LiteLLM is a follow-up PR (PDF→text in the image, plus its own LiteLLM key). ## erpsetup It adds: - the custom fields `Purchase Invoice.intake_communication` and `E Invoice Import.intake_communication` (Link to the Communication, unique, read-only; Frappe stores an empty unique field as NULL, so hand-made invoices do not collide); - the unit `Stück`, the item group `Eingangsrechnungen` and the item `EINGANGSRECHNUNG` (purchase only, non-stock); - the item's default expense account, SKR04 **6300** Sonstige betriebliche Aufwendungen. If that account is missing, the step reports "missing (not created)". ## Run ``` ERP_BASE_URL=https://erp.breakpilot.com ERP_API_KEY=… ERP_API_SECRET=… go run ./cmd/invoiceintake -once -dry-run ``` As a service: image `/invoiceintake`, every `INTAKE_INTERVAL` (10m). Other settings: `INTAKE_SINCE`, `INTAKE_ASSIGN_TO`, `INTAKE_EMAIL_ACCOUNT` (Eingangsrechnung), `INTAKE_COMPANY` (Breakpilot GmbH), `INTAKE_OWN_DOMAINS` (breakpilot.com). The API user only needs the standard roles **Accounts User**, **Purchase User** and **Inbox User**, plus the Email Account under its User Emails, so no permission tables are changed. Those roles could submit, but the service never does. ## Tests `go test ./internal/invoiceintake ./internal/erpsetup` runs against a fake ERPNext. It covers: - the e-invoice path, the plain-PDF draft (by address and by domain) and every ToDo case; - idempotency, including a fresh process; - a cancelled ToDo being retried; - a refused draft; - server errors on the e-invoice import (retried, then treated as a plain PDF); - dry run, bad key, and paging over 1234 mails. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-10-20 07:19:21 +00:00
feat(invoiceintake): invoice@ mails become draft Eingangsrechnungen
ci / shared (pull_request) Successful in 15s
ci / test (pull_request) Successful in 25m30s
ci / image (pull_request) Skipped
5a6d9d26aa
New cmd/invoiceintake (orca-infra #121, Collectives ERP/08). It reads the
mails of ERPNext's Email Account "Eingangsrechnungen" through the REST API
(no IMAP of its own; Strato forwards the originals to DUO) and, per mail:

- e-invoice (XML first, then PDF): E Invoice Import draft, eu_einvoice
  reads and validates it; the person maps and creates the Purchase Invoice
- other PDF: draft Purchase Invoice for the supplier matched by sender
  address or domain, one placeholder line, the original attached again
- no PDF/XML, unknown sender, or a refused draft: a ToDo on the mail

It never submits, changes or deletes anything and keeps no state: done
mails are read back from ERPNext (intake_communication, unique; ToDo
reference). A cancelled ToDo asks for another try. Dry run with -once
-dry-run; logs carry document names and counts, no senders or subjects.

erpsetup gains the two marker fields, the placeholder item EINGANGSRECHNUNG
(unit Stück, group Eingangsrechnungen) and its expense account SKR04 6300.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang added 1 commit 2026-10-20 07:23:57 +00:00
fix(erpsetup): intake marker links to the mail
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 25m31s
ci / image (pull_request) Skipped
433baf3740
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang added 1 commit 2026-10-20 07:49:38 +00:00
fix(invoiceintake): ERPNext's email account is named Eingangsrechnung
ci / shared (pull_request) Successful in 15s
ci / test (pull_request) Successful in 25m36s
ci / image (pull_request) Skipped
f20957c858
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang merged commit cee669d2bd into main 2026-10-20 08:15:33 +00:00
Sign in to join this conversation.