{
  "id": "aihInvoice",
  "name": "AIHotple - PDF invoice extraction and validation",
  "active": false,
  "nodes": [
    {
      "id": "start",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        0,
        0
      ]
    },
    {
      "id": "two-fictional-invoices",
      "name": "Two fictional invoices",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "return ['invoice-complete.pdf','invoice-missing-date.pdf'].map(file=>({json:{file,url:'http://127.0.0.1:5683/fixtures/'+file}}));"
      },
      "position": [
        220,
        0
      ]
    },
    {
      "id": "download-pdf",
      "name": "Download PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "={{$json.url}}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "data"
            }
          }
        }
      },
      "position": [
        440,
        0
      ]
    },
    {
      "id": "extract-pdf-text",
      "name": "Extract PDF text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "data",
        "options": {}
      },
      "position": [
        660,
        0
      ]
    },
    {
      "id": "extract-invoice-fields",
      "name": "Extract invoice fields",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/extract",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {text:$json.text} }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        880,
        0
      ]
    },
    {
      "id": "validate-against-source",
      "name": "Validate against source",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "return $input.all().map((item,i)=>{const text=$('Extract PDF text').all()[i].json.text||'';const issues=[];let fields={};try{fields=JSON.parse(item.json.raw.replace(/^```(?:json)?\\s*|```$/g,''));}catch{issues.push('invalid_json');}if(!fields||Array.isArray(fields)||typeof fields!=='object'){fields={};issues.push('invalid_object');}if(typeof fields.vendor!=='string'||!text.includes(fields.vendor))issues.push('vendor_not_in_source');if(typeof fields.invoice_id!=='string'||!text.includes(fields.invoice_id))issues.push('invoice_id_not_in_source');if(typeof fields.total!=='number'||!Number.isFinite(fields.total)||fields.total<=0)issues.push('invalid_total');const m=text.match(/TOTAL USD\\s+([\\d,.]+)/);if(!m||Math.round(Number(m[1].replaceAll(',',''))*100)!==Math.round(fields.total*100))issues.push('total_mismatch');if(fields.currency!=='USD'||!text.includes('USD'))issues.push('currency_mismatch');const due=text.match(/Due date\\s+(\\d{4}-\\d{2}-\\d{2})/);if(!due||fields.due_date!==due[1])issues.push('due_date_missing_or_mismatch');return {json:{...fields,raw:item.json.raw,source_text:text,status:issues.length?'hold':'ready_for_review',issues:[...new Set(issues)]}};});"
      },
      "position": [
        1100,
        0
      ]
    },
    {
      "id": "save-review-rows",
      "name": "Save review rows",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/record",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {kind:\"invoice\",...$json} }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        1320,
        0
      ]
    },
    {
      "id": "spreadsheet-review-columns",
      "name": "Spreadsheet review columns",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "return $input.all().map(x=>({json:{invoice_id:x.json.invoice_id||'',vendor:x.json.vendor||'',currency:x.json.currency||'',total:typeof x.json.total==='number'?x.json.total:'',due_date:x.json.due_date||'',status:x.json.status,issues:x.json.issues.join('; '),raw_model_output:x.json.raw}}));"
      },
      "position": [
        1540,
        0
      ]
    },
    {
      "id": "create-review-csv",
      "name": "Create review CSV",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "parameters": {
        "operation": "csv",
        "binaryPropertyName": "data",
        "options": {
          "fileName": "invoice-review.csv",
          "headerRow": true
        }
      },
      "position": [
        1760,
        0
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Two fictional invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Two fictional invoices": {
      "main": [
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "Extract PDF text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF text": {
      "main": [
        [
          {
            "node": "Extract invoice fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract invoice fields": {
      "main": [
        [
          {
            "node": "Validate against source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate against source": {
      "main": [
        [
          {
            "node": "Save review rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save review rows": {
      "main": [
        [
          {
            "node": "Spreadsheet review columns",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet review columns": {
      "main": [
        [
          {
            "node": "Create review CSV",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all"
  }
}
