{
  "name": "AI Invoice Clerk v2 (2026 Edition)",
  "nodes": [
    {
      "parameters": {
        "content": "## \ud83e\uddfe AI Invoice Clerk v2 (Refined)\n\n**2026 Standard Upgrade:**\n1. **AI Extraction:** Gemini 1.5 Flash handles Japanese Invoices.\n2. **Logic Check:** Verifies T-Number & Tax calculation via Code node.\n3. **Human-in-the-loop:** Flags errors in Slack for manual approval.",
        "height": 300,
        "width": 450,
        "color": 3
      },
      "id": "c00f7a32-3913-4496-9ee9-4511797cf866",
      "name": "Sticky Note - Refined",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1120,
        48
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "SHEET_ID",
              "value": "YOUR_SHEET_ID_HERE",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "TEST_MODE",
              "value": "false",
              "type": "string"
            }
          ]
        }
      },
      "id": "5dd52a17-aea5-45be-a49d-ecddc1a30145",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -304,
        128
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-1.5-flash",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "Extract details from this invoice for Japanese Invoicing System compliance.\nReturn JSON ONLY.\n\nRequired fields:\n- vendor_name\n- registration_number (Starts with T + 13 digits)\n- date (YYYY-MM-DD)\n- amount_subtotal (Number)\n- amount_tax (Number)\n- amount_total (Number)\n- summary"
            }
          ]
        },
        "options": {
          "responseMimeType": "application/json"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1,
      "position": [
        128,
        256
      ],
      "id": "5de97095-0c67-4bb7-8830-e57208d973e3",
      "name": "Gemini (Vision)",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// \ud83e\udde0 Business Logic & Validation Node\nconst mode = $('Config').first().json.TEST_MODE;\nlet rawData = {};\n\nif (mode === 'true' || mode === true) {\n  rawData = {\n    vendor_name: \"Mock Corp\",\n    registration_number: \"T1234567890123\",\n    date: \"2026-04-01\",\n    amount_subtotal: 1000,\n    amount_tax: 100,\n    amount_total: 1100,\n    summary: \"Test data\"\n  };\n} else {\n  rawData = JSON.parse($('Gemini (Vision)').first().json.output);\n}\n\n// 1. Validation: T-Number format (Japan Invoice System)\nconst regNum = rawData.registration_number || \"\";\nconst isRegValid = /^T\\d{13}$/.test(regNum);\n\n// 2. Validation: Tax calculation (Subtotal + Tax = Total)\nconst calcTotal = Number(rawData.amount_subtotal) + Number(rawData.amount_tax);\nconst isTaxValid = Math.abs(calcTotal - Number(rawData.amount_total)) < 1;\n\n// 3. Status Decision\nlet status = \"\u2705 OK\";\nlet alerts = [];\nif (!isRegValid) alerts.push(\"Invalid Reg Number\");\nif (!isTaxValid) alerts.push(\"Tax Mismatch\");\nif (alerts.length > 0) status = \"\u26a0\ufe0f Review Required: \" + alerts.join(\", \");\n\nreturn {\n  json: {\n    ...rawData,\n    validation_status: status,\n    is_error: alerts.length > 0\n  }\n};"
      },
      "id": "c6f42702-1f44-4fa5-84bc-93ea66652bd8",
      "name": "Logic & Validation",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        128
      ]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $node.Config.json.SHEET_ID }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Invoices"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Vendor": "={{ $json.vendor_name }}",
            "RegNumber": "={{ $json.registration_number }}",
            "Total": "={{ $json.amount_total }}",
            "Status": "={{ $json.validation_status }}",
            "ProcessedAt": "={{ $now.format('yyyy-MM-dd HH:mm') }}"
          }
        }
      },
      "id": "55a5db49-a237-494b-a4a9-3f518f5621ea",
      "name": "Save Draft to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        704,
        128
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "general",
          "mode": "list"
        },
        "text": "=\ud83e\uddfe *AI Invoice Analysis Complete*",
        "jsonParameters": true,
        "blocksUi": {
          "blocksValues": [
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": "=\u53d6\u5f15\u5148: *{{ $json.vendor_name }}*\n\u91d1\u984d: *\u00a5{{ $json.amount_total.toLocaleString() }}*\n\u5224\u5b9a: *{{ $json.validation_status }}*"
              }
            },
            {
              "type": "divider"
            },
            {
              "type": "context",
              "elements": [
                {
                  "type": "mrkdwn",
                  "text": "=\u767b\u9332\u756a\u53f7: {{ $json.registration_number }} | \u65e5\u4ed8: {{ $json.date }}"
                }
              ]
            }
          ]
        }
      },
      "id": "eeb53c6f-cbed-425c-945b-6c3eb6b661b2",
      "name": "Notify Slack (Rich)",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        960,
        128
      ],
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Logic & Validation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini (Vision)": {
      "main": [
        [
          {
            "node": "Logic & Validation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Logic & Validation": {
      "main": [
        [
          {
            "node": "Save Draft to Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Draft to Sheets": {
      "main": [
        [
          {
            "node": "Notify Slack (Rich)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}