{
  "name": "Muhasebe · Dekonttan ERP kaydına",
  "nodes": [
    {
      "parameters": {
        "pollTimes": { "item": [{ "mode": "everyMinute" }] },
        "simple": false,
        "filters": { "q": "has:attachment (dekont OR makbuz OR receipt)" },
        "options": { "downloadAttachments": true }
      },
      "id": "a1b2c3d4-0001-4000-8000-000000000001",
      "name": "Dekont e-postası geldi",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [0, 300]
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "attachment_0",
        "options": {}
      },
      "id": "a1b2c3d4-0002-4000-8000-000000000002",
      "name": "PDF / görselden metin",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "anthropicApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"claude-sonnet-5\",\n  \"max_tokens\": 1024,\n  \"system\": \"Sen bir muhasebe asistanısın. Dekont metninden yalnızca JSON döndür: tarih (YYYY-AA-GG), tutar (sayı), para_birimi, karsi_taraf, aciklama, hesap_kodu, guven (0-1), sorunlar (liste). Emin olmadığın alanı boş bırak, tahmin etme.\",\n  \"messages\": [{ \"role\": \"user\", \"content\": {{ JSON.stringify($json.text) }} }]\n}",
        "options": {}
      },
      "id": "a1b2c3d4-0003-4000-8000-000000000003",
      "name": "Claude AI · veri çıkarma",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [480, 300]
    },
    {
      "parameters": {
        "jsCode": "// Claude yanıtını ayrıştır, zorunlu alanları ve hesap kodu biçimini denetle\nconst out = [];\nfor (const item of $input.all()) {\n  const raw = item.json.content?.[0]?.text ?? '{}';\n  let d;\n  try { d = JSON.parse(raw); } catch { d = { sorunlar: ['Yanıt JSON değil'] }; }\n  const sorunlar = [...(d.sorunlar ?? [])];\n  if (!d.tarih) sorunlar.push('Tarih yok');\n  if (!(d.tutar > 0)) sorunlar.push('Tutar geçersiz');\n  if (!/^\\d{3}(\\.\\d{2}){0,3}$/.test(d.hesap_kodu ?? '')) sorunlar.push('Hesap kodu biçimi hatalı');\n  if ((d.guven ?? 0) < 0.8) sorunlar.push('Düşük güven');\n  out.push({ json: { ...d, sorunlar, gecerli: sorunlar.length === 0, kaynak_eposta: $('Dekont e-postası geldi').item.json.id } });\n}\nreturn out;"
      },
      "id": "a1b2c3d4-0004-4000-8000-000000000004",
      "name": "Doğrula ve hesap kodu kontrolü",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [720, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.gecerli }}",
              "rightValue": true,
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "a1b2c3d4-0005-4000-8000-000000000005",
      "name": "Sorun var mı?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [960, 300]
    },
    {
      "parameters": {
        "resource": "worksheet",
        "operation": "append",
        "workbook": { "__rl": true, "mode": "list", "value": "" },
        "worksheet": { "__rl": true, "mode": "list", "value": "" },
        "dataMode": "autoMap",
        "options": {}
      },
      "id": "a1b2c3d4-0006-4000-8000-000000000006",
      "name": "Excel kontrol tablosuna yaz",
      "type": "n8n-nodes-base.microsoftExcel",
      "typeVersion": 2.1,
      "position": [1200, 200]
    },
    {
      "parameters": {
        "operation": "sendAndWait",
        "sendTo": "muhasebe@sirket.com",
        "subject": "=Onay: {{ $json.karsi_taraf }} · {{ $json.tutar }} {{ $json.para_birimi }}",
        "message": "=Tarih: {{ $json.tarih }}\nTutar: {{ $json.tutar }} {{ $json.para_birimi }}\nHesap kodu: {{ $json.hesap_kodu }}\nAçıklama: {{ $json.aciklama }}\n\nKayıt Excel kontrol tablosuna eklendi. Onaylarsanız Ovis ERP'ye aktarılacak.",
        "approvalOptions": { "values": { "approvalType": "double" } },
        "options": {}
      },
      "id": "a1b2c3d4-0007-4000-8000-000000000007",
      "name": "Muhasebe onayı",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1440, 200],
      "webhookId": "b7c1e2f0-0007-4000-8000-000000000007"
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "c2",
              "leftValue": "={{ $json.data.approved }}",
              "rightValue": true,
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "a1b2c3d4-0008-4000-8000-000000000008",
      "name": "Onaylandı mı?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1680, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.OVIS_ERP_URL }}/api/muhasebe/fis",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"tarih\": \"{{ $('Doğrula ve hesap kodu kontrolü').item.json.tarih }}\",\n  \"tutar\": {{ $('Doğrula ve hesap kodu kontrolü').item.json.tutar }},\n  \"hesap_kodu\": \"{{ $('Doğrula ve hesap kodu kontrolü').item.json.hesap_kodu }}\",\n  \"aciklama\": \"{{ $('Doğrula ve hesap kodu kontrolü').item.json.aciklama }}\"\n}",
        "options": {}
      },
      "id": "a1b2c3d4-0009-4000-8000-000000000009",
      "name": "Ovis ERP · otomatik kayıt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1920, 120]
    },
    {
      "parameters": {
        "resource": "worksheet",
        "operation": "update",
        "workbook": { "__rl": true, "mode": "list", "value": "" },
        "worksheet": { "__rl": true, "mode": "list", "value": "" },
        "dataMode": "autoMap",
        "options": {}
      },
      "id": "a1b2c3d4-0010-4000-8000-000000000010",
      "name": "Denetim izi: durum güncelle",
      "type": "n8n-nodes-base.microsoftExcel",
      "typeVersion": 2.1,
      "position": [2160, 200]
    },
    {
      "parameters": {
        "sendTo": "muhasebe@sirket.com",
        "subject": "=Kontrol gerekli: {{ $json.sorunlar.join(', ') }}",
        "message": "=Dekont otomatik işlenemedi.\nSorunlar: {{ $json.sorunlar.join(', ') }}\nKaynak e-posta: {{ $json.kaynak_eposta }}",
        "options": {}
      },
      "id": "a1b2c3d4-0011-4000-8000-000000000011",
      "name": "Hatalı dekontu bildir",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1200, 420],
      "webhookId": "b7c1e2f0-0011-4000-8000-000000000011"
    }
  ],
  "connections": {
    "Dekont e-postası geldi": { "main": [[{ "node": "PDF / görselden metin", "type": "main", "index": 0 }]] },
    "PDF / görselden metin": { "main": [[{ "node": "Claude AI · veri çıkarma", "type": "main", "index": 0 }]] },
    "Claude AI · veri çıkarma": { "main": [[{ "node": "Doğrula ve hesap kodu kontrolü", "type": "main", "index": 0 }]] },
    "Doğrula ve hesap kodu kontrolü": { "main": [[{ "node": "Sorun var mı?", "type": "main", "index": 0 }]] },
    "Sorun var mı?": {
      "main": [
        [{ "node": "Excel kontrol tablosuna yaz", "type": "main", "index": 0 }],
        [{ "node": "Hatalı dekontu bildir", "type": "main", "index": 0 }]
      ]
    },
    "Excel kontrol tablosuna yaz": { "main": [[{ "node": "Muhasebe onayı", "type": "main", "index": 0 }]] },
    "Muhasebe onayı": { "main": [[{ "node": "Onaylandı mı?", "type": "main", "index": 0 }]] },
    "Onaylandı mı?": {
      "main": [
        [{ "node": "Ovis ERP · otomatik kayıt", "type": "main", "index": 0 }],
        [{ "node": "Denetim izi: durum güncelle", "type": "main", "index": 0 }]
      ]
    },
    "Ovis ERP · otomatik kayıt": { "main": [[{ "node": "Denetim izi: durum güncelle", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
