{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "downloadAttachments": true,
        "options": {}
      },
      "id": "1d7641f6-3f78-441c-8db4-939c456da4b9",
      "name": "Email Trigger (IMAP)5",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [
        -4352,
        2208
      ],
      "credentials": {
        "imap": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fileName": "/tmp/invoice-{{$executionId}}.pdf",
        "dataPropertyName": "attachment_0",
        "options": {}
      },
      "id": "34763e17-c9a4-45e6-a87a-f31021642d78",
      "name": "Write Binary File5",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        -4112,
        2208
      ]
    },
    {
      "parameters": {
        "command": "pdftoppm /tmp/invoice-{{$executionId}}.pdf /tmp/invoice-{{$executionId}} -png"
      },
      "id": "4ec8f1c6-dc28-4616-afff-59afe2c2f200",
      "name": "Convert PDF \u2192 PNG5",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        -3872,
        2208
      ]
    },
    {
      "parameters": {
        "command": "tesseract /tmp/invoice-{{$executionId}}-1.png stdout -l pol"
      },
      "id": "13377dc8-fb63-4314-a654-4269c4367874",
      "name": "OCR (Tesseract)5",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        -3632,
        2208
      ]
    },
    {
      "parameters": {
        "jsCode": "// Pobierz pe\u0142ny tekst z OCR\nconst text = $json[\"stdout\"];\n\n// Numer faktury\nconst matchInvoice = text.match(/Nr\\s+([A-Z0-9\\/_-]+)/i);\nlet invoiceNumber = matchInvoice ? matchInvoice[1] : \"Faktura_bez_numeru\";\n\n// Data (np. 2025-09-05)\nconst matchDate = text.match(/(\\d{4}-\\d{2}-\\d{2})/);\nlet invoiceDate = matchDate ? matchDate[1] : null;\n\nlet year = null;\nlet month = null;\nif (invoiceDate) {\n  const d = new Date(invoiceDate);\n  year = d.getFullYear();\n  month = String(d.getMonth() + 1).padStart(2, \"0\");\n}\n\nreturn [{ json: { invoiceNumber, invoiceDate, year, month } }];"
      },
      "id": "a44bff37-ca47-4fca-bff9-b3839c1c3874",
      "name": "Extract Invoice Data3",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -3392,
        2208
      ]
    },
    {
      "parameters": {
        "resource": "folder",
        "name": "={{ $json.year }}",
        "options": {}
      },
      "id": "c230b958-5567-4383-802f-129aa481ffb1",
      "name": "Create Year Folder3",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 2,
      "position": [
        -3152,
        2000
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "folder",
        "name": "={{ $('Extract Invoice Data3').item.json.month }}",
        "options": {}
      },
      "id": "bee4d4b2-f64a-4fd0-8807-57e0db48f45a",
      "name": "Create Month Folder3",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 2,
      "position": [
        -2992,
        2000
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "folder",
        "name": "Zakupy",
        "options": {}
      },
      "id": "2830020f-73e2-488e-bdeb-f88d9d62ec18",
      "name": "Create Zakupy Folder3",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 2,
      "position": [
        -2832,
        2000
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "combine",
        "combinationMode": "multiplex",
        "options": {}
      },
      "id": "47b45961-53dc-4ff0-9973-545c447ed5d5",
      "name": "Merge PDF + Metadata2",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        -2992,
        2384
      ]
    },
    {
      "parameters": {
        "binaryData": true,
        "binaryPropertyName": "attachment_0",
        "name": "={{$json.invoiceNumber}}.pdf",
        "parents": [
          "={{ $('Create Zakupy Folder3').item.json.id }}"
        ],
        "options": {}
      },
      "id": "9991c8f0-e042-4d42-aff7-b8ad28cd42b9",
      "name": "Google Drive (Upload PDF)5",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 2,
      "position": [
        -2672,
        2384
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Email Trigger (IMAP)5": {
      "main": [
        [
          {
            "node": "Write Binary File5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write Binary File5": {
      "main": [
        [
          {
            "node": "Convert PDF \u2192 PNG5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert PDF \u2192 PNG5": {
      "main": [
        [
          {
            "node": "OCR (Tesseract)5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OCR (Tesseract)5": {
      "main": [
        [
          {
            "node": "Extract Invoice Data3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Invoice Data3": {
      "main": [
        [
          {
            "node": "Create Year Folder3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge PDF + Metadata2",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Create Year Folder3": {
      "main": [
        [
          {
            "node": "Create Month Folder3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Month Folder3": {
      "main": [
        [
          {
            "node": "Create Zakupy Folder3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Zakupy Folder3": {
      "main": [
        [
          {
            "node": "Merge PDF + Metadata2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge PDF + Metadata2": {
      "main": [
        [
          {
            "node": "Google Drive (Upload PDF)5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  }
}