{"name":"AI Invoice Processor","nodes":[{"id":"emailTrigger","type":"n8n-nodes-base.emailTrigger","name":"Invoice Email Received","position":[250,300],"parameters":{"mailbox":"invoices@company.com","filters":{"hasAttachment":true}}},{"id":"extractAttach","type":"n8n-nodes-base.code","name":"Extract PDF Attachment","position":[450,300],"parameters":{"language":"javascript","code":"const attachments = $json.attachments.filter(a => a.contentType === \"application/pdf\");\nreturn attachments.map(a => ({ json: { filename: a.filename, data: a.content, sender: items[0].json.from } }));"}},{"id":"aiExtract","type":"n8n-nodes-base.openAi","name":"AI Extract Invoice Data","position":[650,300],"parameters":{"model":"gpt-4o","prompt":"Extract all data from this invoice image/PDF. Return structured JSON:\n{\n  vendor_name, vendor_address, invoice_number, invoice_date, due_date, po_number,\n  line_items: [{description, quantity, unit_price, total}],\n  subtotal, tax_amount, tax_rate, shipping, total_amount, currency,\n  payment_terms, bank_details, notes\n}"}},{"id":"validateVendor","type":"n8n-nodes-base.httpRequest","name":"Validate Vendor","position":[850,200],"parameters":{"url":"={{$env.ACCOUNTING_API}}/vendors/search","method":"GET","queryParameters":{"name":"={{$json.vendor_name}}"}}},{"id":"matchPO","type":"n8n-nodes-base.httpRequest","name":"Match Purchase Order","position":[850,400],"parameters":{"url":"={{$env.ACCOUNTING_API}}/purchase-orders/{{$json.po_number}}","method":"GET"}},{"id":"threeWayMatch","type":"n8n-nodes-base.code","name":"Three-Way Match","position":[1050,300],"parameters":{"language":"javascript","code":"const invoice = $json.invoice;\nconst po = $json.purchase_order;\nconst issues = [];\nif (Math.abs(invoice.total_amount - po.total) > 0.01) issues.push(\"Amount mismatch\");\nif (invoice.line_items.length !== po.line_items.length) issues.push(\"Line item count differs\");\nreturn { json: { ...items[0].json, match_status: issues.length === 0 ? \"matched\" : \"review\", issues } };"}},{"id":"routeMatch","type":"n8n-nodes-base.if","name":"Match OK?","position":[1250,300],"parameters":{"conditions":{"string":[{"value1":"={{$json.match_status}}","operation":"equal","value2":"matched"}]}}},{"id":"enterAccounting","type":"n8n-nodes-base.httpRequest","name":"Create Bill in Accounting","position":[1450,200],"parameters":{"url":"={{$env.ACCOUNTING_API}}/bills","method":"POST"}},{"id":"reviewQueue","type":"n8n-nodes-base.slack","name":"Send to Review Queue","position":[1450,400],"parameters":{"channel":"#ap-review","text":"Invoice review needed: {{$json.vendor_name}} #{{$json.invoice_number}} (${{$json.total_amount}})\nIssues: {{$json.issues.join(\", \")}}"}},{"id":"logProcessed","type":"n8n-nodes-base.googleSheets","name":"Log Processed Invoice","position":[1650,300],"parameters":{"operation":"append","sheetId":"={{$env.INVOICE_LOG_SHEET}}"}}],"connections":{"Invoice Email Received":{"main":[[{"node":"Extract PDF Attachment","type":"main","index":0}]]},"Extract PDF Attachment":{"main":[[{"node":"AI Extract Invoice Data","type":"main","index":0}]]},"AI Extract Invoice Data":{"main":[[{"node":"Validate Vendor","type":"main","index":0},{"node":"Match Purchase Order","type":"main","index":0}]]},"Validate Vendor":{"main":[[{"node":"Three-Way Match","type":"main","index":0}]]},"Match Purchase Order":{"main":[[{"node":"Three-Way Match","type":"main","index":0}]]},"Three-Way Match":{"main":[[{"node":"Match OK?","type":"main","index":0}]]},"Match OK?":{"main":[[{"node":"Create Bill in Accounting","type":"main","index":0}],[{"node":"Send to Review Queue","type":"main","index":0}]]},"Create Bill in Accounting":{"main":[[{"node":"Log Processed Invoice","type":"main","index":0}]]},"Send to Review Queue":{"main":[[{"node":"Log Processed Invoice","type":"main","index":0}]]}}}