{
  "name": "گالری ۸: پردازش فاکتور از پیوست ایمیل",
  "nodes": [
    {
      "parameters": {
        "content": "## پردازش فاکتور\n۱) ایمیل با پیوست PDF در برچسب «فاکتور»\n۲) جدا کردن پیوست‌ها و استخراج متن\n۳) استخراج فیلدها با مدل\n۴) بررسی با کد: فیلدهای لازم، جمع، تاریخ\n۵) تکراری؟ (فروشنده + شماره)\n۶) سالم ← Sheets و Drive؛ مشکوک ← بررسی انسانی\n\n⚠️ نمونه آموزشی loOkiYar؛ غیرفعال. گره‌های بنفش (سنجاق‌دار) داده نمایشی ساختگی دارند و در اجرای دستی به سرویس واقعی وصل نمی‌شوند.",
        "height": 314,
        "width": 480,
        "color": 4
      },
      "id": "6764aedd-31eb-5622-86d9-5fe24d006cb0",
      "name": "یادداشت 1",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -454
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 5,
              "unit": "minutes"
            }
          ]
        },
        "simple": false,
        "filters": {
          "readStatus": "unread",
          "q": "has:attachment filename:pdf",
          "labelIds": [
            "REPLACE_WITH_LABEL_ID_invoices"
          ]
        },
        "options": {}
      },
      "id": "b1b3da64-137a-5018-81b3-c4b8f09f655f",
      "name": "ایمیل فاکتور",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        100
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "e6161cc65c9b5504",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Gmail Trigger with \"Download Attachments\" puts files in binary keys attachment_0, attachment_1, ...\nconst out = [];\nfor (const item of $input.all()) {\n  for (const [key, file] of Object.entries(item.binary || {})) {\n    if (file.mimeType !== 'application/pdf') continue;\n    out.push({ json: { message_id: item.json.id, from: item.json.from?.value?.[0]?.address, file_name: file.fileName }, binary: { data: file } });\n  }\n}\nreturn out;"
      },
      "id": "2d15799d-8a39-5150-a540-a9d83fa85dbf",
      "name": "یک آیتم برای هر پیوست",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        100
      ]
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "data",
        "options": {}
      },
      "id": "066973e3-c7d1-58c0-a3e9-d804e45db108",
      "name": "متن PDF",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        440,
        100
      ]
    },
    {
      "parameters": {
        "text": "={{ ($json.text || '').slice(0, 12000) }}",
        "schemaType": "fromAttributes",
        "attributes": {
          "attributes": [
            {
              "name": "vendor",
              "type": "string",
              "description": "نام فروشنده",
              "required": true
            },
            {
              "name": "invoice_number",
              "type": "string",
              "description": "شماره فاکتور",
              "required": true
            },
            {
              "name": "invoice_date",
              "type": "date",
              "description": "تاریخ فاکتور (میلادی)"
            },
            {
              "name": "subtotal",
              "type": "number",
              "description": "جمع پیش از مالیات"
            },
            {
              "name": "tax",
              "type": "number",
              "description": "مالیات"
            },
            {
              "name": "total",
              "type": "number",
              "description": "مبلغ نهایی",
              "required": true
            },
            {
              "name": "currency",
              "type": "string",
              "description": "واحد پول، مثل IRR یا EUR"
            }
          ]
        },
        "options": {
          "systemPromptTemplate": "Extract only values written in the invoice text. If a value is missing, omit it. Never guess."
        }
      },
      "id": "60b429f9-cf4e-5b96-b7c2-61024d33c310",
      "name": "استخراج فیلدها",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        660,
        100
      ]
    },
    {
      "parameters": {
        "model": "openai/gpt-5-mini",
        "options": {
          "temperature": 0
        }
      },
      "id": "90fd4b5a-4d79-5733-b20e-254f0913b2ea",
      "name": "مدل (OpenRouter)",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        660,
        340
      ],
      "credentials": {
        "openRouterApi": {
          "id": "fce14e07dac85574",
          "name": "OpenRouter account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map((item, i) => {\n  const x = item.json.output || {};\n  const src = $('یک آیتم برای هر پیوست').all()[i].json;\n  const problems = [];\n  for (const f of ['vendor', 'invoice_number', 'total']) if (x[f] === undefined || x[f] === '') problems.push('missing ' + f);\n  if (x.subtotal != null && x.tax != null && x.total != null && Math.abs(x.subtotal + x.tax - x.total) > 1) problems.push('subtotal + tax != total');\n  if (x.total != null && x.total <= 0) problems.push('total <= 0');\n  const key = ((x.vendor || '') + '|' + (x.invoice_number || '')).toLowerCase().replace(/\\s+/g, '');\n  return { json: { ...x, ...src, dedupe_key: key, problems, ok: problems.length === 0 } };\n});"
      },
      "id": "52ebc8ff-c1cf-5425-abb4-ccc2361debbe",
      "name": "بررسی قواعد",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        100
      ]
    },
    {
      "parameters": {
        "operation": "removeItemsSeenInPreviousExecutions",
        "dedupeValue": "={{ $json.dedupe_key }}",
        "options": {}
      },
      "id": "b83aa469-7124-52dc-9e3c-6b8d0d5e0452",
      "name": "فاکتور تکراری نیست",
      "type": "n8n-nodes-base.removeDuplicates",
      "typeVersion": 2,
      "position": [
        1120,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "62dffe44-ba32-5992-ab96-6f52ef3aca20",
              "leftValue": "={{ $json.ok }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "345bac88-9e97-5ff3-9daf-55979d1d3c5c",
      "name": "سالم است؟",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1340,
        100
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "REPLACE_WITH_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Invoices"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "da5973dd-b128-5a71-b08d-ea8a41e89d76",
      "name": "ثبت در دفتر فاکتورها",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1580,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "6fd5b108914d5fc7",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $vars.TELEGRAM_CHAT_ID || 'REPLACE_WITH_CHAT_ID' }}",
        "text": "=🧾 فاکتور نیازمند بررسی: {{ $json.file_name }} از {{ $json.from }}\nمشکل: {{ $json.problems.join('، ') }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "f86c3dc6-e434-5b68-b0d1-1e4afae54626",
      "name": "بررسی انسانی",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1580,
        220
      ],
      "credentials": {
        "telegramApi": {
          "id": "536244d78d565cab",
          "name": "Telegram bot"
        }
      }
    }
  ],
  "connections": {
    "مدل (OpenRouter)": {
      "ai_languageModel": [
        [
          {
            "node": "استخراج فیلدها",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "ایمیل فاکتور": {
      "main": [
        [
          {
            "node": "یک آیتم برای هر پیوست",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "یک آیتم برای هر پیوست": {
      "main": [
        [
          {
            "node": "متن PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "متن PDF": {
      "main": [
        [
          {
            "node": "استخراج فیلدها",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "استخراج فیلدها": {
      "main": [
        [
          {
            "node": "بررسی قواعد",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "بررسی قواعد": {
      "main": [
        [
          {
            "node": "فاکتور تکراری نیست",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "فاکتور تکراری نیست": {
      "main": [
        [
          {
            "node": "سالم است؟",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "سالم است؟": {
      "main": [
        [
          {
            "node": "ثبت در دفتر فاکتورها",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "بررسی انسانی",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "id": "LkyGal08Invoices",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "lookiyar-academy-demo"
  },
  "tags": [
    {
      "name": "loOkiYar Academy"
    },
    {
      "name": "gallery"
    }
  ],
  "pinData": {
    "ایمیل فاکتور": [
      {
        "json": {
          "id": "demo-inv-1",
          "subject": "فاکتور مهر",
          "from": {
            "value": [
              {
                "address": "billing@paper-supplier.example"
              }
            ]
          },
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "یک آیتم برای هر پیوست": [
      {
        "json": {
          "message_id": "demo-inv-1",
          "from": "billing@paper-supplier.example",
          "file_name": "INV-2041.pdf",
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "message_id": "demo-inv-1",
          "from": "billing@paper-supplier.example",
          "file_name": "INV-2042.pdf",
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "متن PDF": [
      {
        "json": {
          "text": "Paper Supplier Ltd. Invoice INV-2041 Date 2026-10-15 Subtotal 1,200 EUR VAT 0 Total 1,200 EUR",
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "text": "Paper Supplier Ltd. Invoice INV-2042 Date 2026-10-18 Subtotal 500 VAT 90 Total 650 EUR",
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "استخراج فیلدها": [
      {
        "json": {
          "output": {
            "vendor": "Paper Supplier Ltd.",
            "invoice_number": "INV-2041",
            "invoice_date": "2026-10-15",
            "subtotal": 1200,
            "tax": 0,
            "total": 1200,
            "currency": "EUR"
          },
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "output": {
            "vendor": "Paper Supplier Ltd.",
            "invoice_number": "INV-2042",
            "invoice_date": "2026-10-18",
            "subtotal": 500,
            "tax": 90,
            "total": 650,
            "currency": "EUR"
          },
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "ثبت در دفتر فاکتورها": [
      {
        "json": {
          "vendor": "Paper Supplier Ltd.",
          "invoice_number": "INV-2041",
          "total": 1200,
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "بررسی انسانی": [
      {
        "json": {
          "ok": true,
          "result": {
            "message_id": 1001
          },
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ]
  }
}