{
  "name": "Eingangsrechnungen mit KI auslesen und zur Freigabe schicken",
  "nodes": [
    {
      "parameters": {
        "mailbox": "INBOX",
        "postProcessAction": "read",
        "downloadAttachments": true,
        "options": {}
      },
      "id": "0b941369-4c69-5eca-996e-6283696716c1",
      "name": "Postfach rechnung@ prüfen",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [
        -440,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c4e090dc-18b9-53e5-92b0-49b58ed2753b",
              "name": "ki_endpunkt",
              "value": "https://api.openai.com/v1/chat/completions",
              "type": "string"
            },
            {
              "id": "eee2689e-4fed-5635-b3f6-60e083d58765",
              "name": "ki_modell",
              "value": "gpt-5-mini",
              "type": "string"
            },
            {
              "id": "69ce16dd-6198-5d2a-820f-c98a644790c0",
              "name": "absender",
              "value": "automation@ihre-firma.de",
              "type": "string"
            },
            {
              "id": "8b8d8c95-d6e0-503f-8e16-0a574de8f7da",
              "name": "freigabe_an",
              "value": "chef@ihre-firma.de",
              "type": "string"
            },
            {
              "id": "c6932e6e-1185-55a9-bea8-3fc9ed2b1e18",
              "name": "buchhaltung_an",
              "value": "buchhaltung@ihre-firma.de",
              "type": "string"
            },
            {
              "id": "9056b4a8-b47a-5721-af5e-3ae6ad7aa9ef",
              "name": "freigabe_ab_euro",
              "value": 1000,
              "type": "number"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {
          "includeBinary": true
        }
      },
      "id": "b48d8447-a1f4-5285-be0e-b3dcf273cc38",
      "name": "Konfiguration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -200,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Jeden Anhang als eigenes Item weitergeben (nur PDF und XML)\nconst out = [];\nfor (const item of $input.all()) {\n  for (const [key, bin] of Object.entries(item.binary ?? {})) {\n    const name = (bin.fileName || '').toLowerCase();\n    if (!/(\\.pdf|\\.xml)$/.test(name)) continue;\n    out.push({\n      json: { betreff: item.json.subject, von: item.json.from, datei: bin.fileName, typ: name.endsWith('.xml') ? 'xml' : 'pdf' },\n      binary: { data: bin },\n    });\n  }\n}\nreturn out;"
      },
      "id": "90c798c3-1df3-5ba8-b144-31d3b2bbddfa",
      "name": "Anhänge einzeln",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        40,
        0
      ]
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "data",
        "options": {}
      },
      "id": "eab5e42c-269c-5329-b9e8-6f4b0a4dbcd4",
      "name": "PDF-Text lesen",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        260,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Konfiguration').first().json.ki_endpunkt }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: $('Konfiguration').first().json.ki_modell, response_format: { type: 'json_object' }, messages: [ { role: 'system', content: \"Du liest deutsche Eingangsrechnungen. Antworte nur mit JSON: {lieferant, rechnungsnummer, rechnungsdatum (YYYY-MM-DD), leistungsdatum, netto, ust, brutto, waehrung, iban, faellig_am (YYYY-MM-DD), skonto, kostenstelle_vorschlag, ist_rechnung (true/false)}. Fehlende Werte als null. Erfinde nichts.\" }, { role: 'user', content: 'Rechnungstext:\\n' + ($json.text || '').slice(0, 15000) } ] }) }}",
        "options": {
          "timeout": 60000
        }
      },
      "id": "d7761682-9c4f-54be-bfaa-282967b4c196",
      "name": "KI: Rechnungsdaten erkennen",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Antwort des Sprachmodells (JSON) auslesen\nreturn $input.all().map((item, i) => {\n  const raw = item.json.choices?.[0]?.message?.content ?? '{}';\n  let data = {};\n  try { data = JSON.parse(raw); } catch (e) { data = { fehler: 'Antwort war kein JSON', roh: raw }; }\n  return { json: { ...data } };\n});"
      },
      "id": "35f06d11-761d-5980-a7e0-c7cf9b735e38",
      "name": "Antwort auslesen",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "e9b250f9-d00f-5eaf-8b70-8b30050ff1f7",
              "leftValue": "={{ $json.brutto }}",
              "rightValue": "={{ $('Konfiguration').first().json.freigabe_ab_euro }}",
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "34bf0a6a-113b-53d0-87f3-951bcd338cb0",
      "name": "Über Freigabegrenze?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        920,
        0
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $('Konfiguration').first().json.absender }}",
        "toEmail": "={{ $('Konfiguration').first().json.freigabe_an }}",
        "subject": "=Freigabe nötig: {{ $json.lieferant }} {{ $json.brutto }} €",
        "emailFormat": "text",
        "text": "=Neue Eingangsrechnung zur Freigabe\n\nLieferant: {{ $json.lieferant }}\nRechnungsnr.: {{ $json.rechnungsnummer }} vom {{ $json.rechnungsdatum }}\nBrutto: {{ $json.brutto }} {{ $json.waehrung }} (netto {{ $json.netto }}, USt {{ $json.ust }})\nFällig: {{ $json.faellig_am }} · Skonto: {{ $json.skonto }}\nIBAN: {{ $json.iban }}\nVorschlag Kostenstelle: {{ $json.kostenstelle_vorschlag }}\n\nBitte prüfen und freigeben.",
        "options": {}
      },
      "id": "736dce36-3428-5c6d-8a7b-f00016f3d9be",
      "name": "Zur Freigabe senden",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1160,
        -100
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $('Konfiguration').first().json.absender }}",
        "toEmail": "={{ $('Konfiguration').first().json.buchhaltung_an }}",
        "subject": "=Rechnung erfasst: {{ $json.lieferant }} {{ $json.rechnungsnummer }}",
        "emailFormat": "text",
        "text": "=Automatisch erfasst (unter Freigabegrenze)\n\nLieferant: {{ $json.lieferant }}\nRechnungsnr.: {{ $json.rechnungsnummer }} vom {{ $json.rechnungsdatum }}\nBrutto: {{ $json.brutto }} {{ $json.waehrung }}\nFällig: {{ $json.faellig_am }}\nIBAN: {{ $json.iban }}",
        "options": {}
      },
      "id": "bc93d439-ba4a-5a0b-a50f-8e8b4b8397e1",
      "name": "An Buchhaltung",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1160,
        100
      ]
    },
    {
      "parameters": {
        "content": "## Eingangsrechnungen mit KI auslesen\nVorlage von roiwerk.com\n\n1. IMAP-Zugang für Ihr Rechnungspostfach hinterlegen\n2. OpenAI-kompatiblen Zugang hinterlegen (EU-Alternative: Mistral oder Azure OpenAI, Endpunkt in **Konfiguration** ändern)\n3. Empfänger und Freigabegrenze in **Konfiguration** anpassen\n4. SMTP-Zugang in den beiden E-Mail-Nodes wählen\n\nXML-Rechnungen (XRechnung) besser mit der XRechnung-Vorlage verarbeiten: dort steht alles schon strukturiert im XML.",
        "height": 340,
        "width": 420
      },
      "id": "ffc16b01-15c1-5840-a9cc-8140455843ec",
      "name": "Hinweis",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -260,
        -420
      ]
    }
  ],
  "connections": {
    "Postfach rechnung@ prüfen": {
      "main": [
        [
          {
            "node": "Konfiguration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Konfiguration": {
      "main": [
        [
          {
            "node": "Anhänge einzeln",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anhänge einzeln": {
      "main": [
        [
          {
            "node": "PDF-Text lesen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PDF-Text lesen": {
      "main": [
        [
          {
            "node": "KI: Rechnungsdaten erkennen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "KI: Rechnungsdaten erkennen": {
      "main": [
        [
          {
            "node": "Antwort auslesen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Antwort auslesen": {
      "main": [
        [
          {
            "node": "Über Freigabegrenze?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Über Freigabegrenze?": {
      "main": [
        [
          {
            "node": "Zur Freigabe senden",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "An Buchhaltung",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "https://roiwerk.com/vorlagen/n8n-workflow-vorlagen"
  },
  "tags": []
}