{
  "name": "05 - AI Document Processor & Data Extractor",
  "nodes": [
    {
      "id": "node-1",
      "name": "Gmail Trigger - Invoice Inbox",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [240, 300],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail account" } },
      "parameters": {
        "filters": { "labelIds": ["Label_invoices"], "readStatus": "unread", "attachmentsFilter": "hasAttachment" },
        "pollTimes": { "item": [{ "mode": "everyX", "value": 15, "unit": "minutes" }] },
        "downloadAttachments": true
      }
    },
    {
      "id": "node-2",
      "name": "OpenAI - Extract Invoice Data (Vision)",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.8,
      "position": [480, 300],
      "credentials": { "openAiApi": { "id": "YOUR_OPENAI_CREDENTIAL_ID", "name": "OpenAI account" } },
      "parameters": {
        "resource": "chat",
        "operation": "complete",
        "modelId": { "__rl": true, "value": "gpt-4o", "mode": "list", "cachedResultName": "gpt-4o" },
        "messages": {
          "values": [
            {
              "role": "user",
              "content": "=Extract all data from this invoice. Return ONLY valid JSON: {vendor_name, invoice_number, invoice_date, due_date, subtotal, tax_amount, total_amount, currency, line_items: [{description, quantity, unit_price, total}], payment_terms}\n\nEmail subject: {{ $json.subject }}\nFrom: {{ $json.from.value[0].address }}"
            }
          ]
        },
        "options": { "temperature": 0.1 }
      }
    },
    {
      "id": "node-3",
      "name": "Code - Parse & Validate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [720, 300],
      "parameters": {
        "jsCode": "let data;\ntry { data = JSON.parse($input.first().json.message.content); }\ncatch(e) { throw new Error('AI returned invalid JSON'); }\nif (!data.vendor_name || !data.total_amount) throw new Error('Missing critical fields');\nconst email = $('Gmail Trigger - Invoice Inbox').first().json;\nreturn [{ json: { ...data, sender_email: email.from.value[0].address, email_subject: email.subject, processed_at: new Date().toISOString() } }];"
      }
    },
    {
      "id": "node-4",
      "name": "QuickBooks - Create Bill",
      "type": "n8n-nodes-base.quickbooks",
      "typeVersion": 1,
      "position": [960, 160],
      "credentials": { "quickBooksOAuth2Api": { "id": "YOUR_QB_CREDENTIAL_ID", "name": "QuickBooks account" } },
      "parameters": {
        "resource": "bill",
        "operation": "create",
        "additionalFields": {
          "DocNumber": "={{ $json.invoice_number }}",
          "TxnDate": "={{ $json.invoice_date }}",
          "DueDate": "={{ $json.due_date }}",
          "VendorRef": "={{ $json.vendor_name }}",
          "TotalAmt": "={{ $json.total_amount }}"
        }
      }
    },
    {
      "id": "node-5",
      "name": "Google Drive - Save Invoice PDF",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [960, 440],
      "credentials": { "googleDriveOAuth2Api": { "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID", "name": "Google Drive account" } },
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "name": "={{ $json.vendor_name }}_{{ $json.invoice_number }}_{{ $json.invoice_date }}.pdf",
        "folderId": { "__rl": true, "value": "YOUR_INVOICES_FOLDER_ID", "mode": "id" },
        "options": {}
      }
    },
    {
      "id": "node-6",
      "name": "Airtable - Log Invoice",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [1200, 300],
      "credentials": { "airtableTokenApi": { "id": "YOUR_AIRTABLE_CREDENTIAL_ID", "name": "Airtable account" } },
      "parameters": {
        "resource": "record",
        "operation": "create",
        "baseId": { "__rl": true, "value": "YOUR_AIRTABLE_BASE_ID", "mode": "id" },
        "tableId": { "__rl": true, "value": "Invoices", "mode": "name" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Vendor": "={{ $json.vendor_name }}",
            "Invoice Number": "={{ $json.invoice_number }}",
            "Total Amount": "={{ $json.total_amount }}",
            "Currency": "={{ $json.currency }}",
            "Invoice Date": "={{ $json.invoice_date }}",
            "Due Date": "={{ $json.due_date }}",
            "Status": "Extracted - Pending Review",
            "Processed At": "={{ $json.processed_at }}"
          }
        }
      }
    },
    {
      "id": "node-7",
      "name": "Gmail - Notify Accounting",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1440, 300],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail account" } },
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "accounting@yourcompany.com",
        "subject": "=✅ Invoice Processed: {{ $json.vendor_name }} — ${{ $json.total_amount }}",
        "message": "=Invoice automatically extracted and logged.\n\nVendor: {{ $json.vendor_name }}\nInvoice #: {{ $json.invoice_number }}\nAmount: ${{ $json.total_amount }} {{ $json.currency }}\nDue: {{ $json.due_date }}\n\n✅ Added to QuickBooks Bills\n✅ Saved to Google Drive\n✅ Logged in Airtable",
        "options": {}
      }
    }
  ],
  "connections": {
    "Gmail Trigger - Invoice Inbox": { "main": [[{ "node": "OpenAI - Extract Invoice Data (Vision)", "type": "main", "index": 0 }]] },
    "OpenAI - Extract Invoice Data (Vision)": { "main": [[{ "node": "Code - Parse & Validate", "type": "main", "index": 0 }]] },
    "Code - Parse & Validate": {
      "main": [[
        { "node": "QuickBooks - Create Bill", "type": "main", "index": 0 },
        { "node": "Google Drive - Save Invoice PDF", "type": "main", "index": 0 }
      ]]
    },
    "QuickBooks - Create Bill": { "main": [[{ "node": "Airtable - Log Invoice", "type": "main", "index": 0 }]] },
    "Google Drive - Save Invoice PDF": { "main": [[{ "node": "Airtable - Log Invoice", "type": "main", "index": 0 }]] },
    "Airtable - Log Invoice": { "main": [[{ "node": "Gmail - Notify Accounting", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "active": false,
  "tags": [{ "name": "Portfolio" }, { "name": "Document Automation" }, { "name": "AI" }]
}
