{
 "id": "karnema01PurchaseRequest",
 "name": "کارنما ۱ — ثبت درخواست خرید از ERP",
 "nodes": [
  {
   "parameters": {},
   "name": "اجرای دستی (آزمایش)",
   "type": "n8n-nodes-base.manualTrigger",
   "typeVersion": 1,
   "position": [
    0,
    0
   ]
  },
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "erp/purchase-request",
    "responseMode": "lastNode",
    "options": {}
   },
   "name": "ERP: سند درخواست خرید",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    200
   ],
   "webhookId": "a1f5c0d2-0000-4000-8000-000000000001"
  },
  {
   "parameters": {
    "mode": "manual",
    "assignments": {
     "assignments": [
      {
       "id": "سند ERP-0",
       "name": "processId",
       "value": "={{ $env.KARNEMA_PURCHASE_PROCESS_ID }}",
       "type": "string"
      },
      {
       "id": "سند ERP-1",
       "name": "docNumber",
       "value": "={{ $json.body?.docNumber ?? 'PR-1405-0042' }}",
       "type": "string"
      },
      {
       "id": "سند ERP-2",
       "name": "description",
       "value": "={{ $json.body?.description ?? 'لپ‌تاپ برای تیم فنی (۳ دستگاه)' }}",
       "type": "string"
      },
      {
       "id": "سند ERP-3",
       "name": "amount",
       "value": "={{ $json.body?.amount ?? 1250000000 }}",
       "type": "number"
      },
      {
       "id": "سند ERP-4",
       "name": "urgency",
       "value": "={{ $json.body?.urgency ?? 'فوری' }}",
       "type": "string"
      },
      {
       "id": "سند ERP-5",
       "name": "notes",
       "value": "={{ $json.body?.notes ?? 'ثبت خودکار از ERP با n8n' }}",
       "type": "string"
      },
      {
       "id": "سند ERP-6",
       "name": "requesterMobile",
       "value": "={{ $json.body?.requesterMobile ?? '09300001016' }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "name": "سند ERP",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    240,
    100
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "={{ $env.KARNEMA_BASE_URL }}/processes/{{ $json.processId }}/requests",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "options": {
     "response": {
      "response": {
       "fullResponse": false,
       "neverError": false
      }
     }
    },
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "Idempotency-Key",
       "value": "={{ $json.docNumber }}"
      },
      {
       "name": "X-Act-As",
       "value": "={{ $json.requesterMobile }}"
      }
     ]
    },
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ title: $json.docNumber + ' · ' + $json.description, values: [ { label: 'شرح کالا یا خدمت', value: $json.description }, { label: 'مبلغ تقریبی (ریال)', value: String($json.amount) }, { label: 'فوریت', value: $json.urgency }, { label: 'توضیحات', value: $json.notes } ] }) }}"
   },
   "name": "کارنما: ثبت درخواست",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    480,
    100
   ],
   "credentials": {
    "httpHeaderAuth": {
     "id": "karnemaApiKey",
     "name": "Karnema API Key"
    }
   }
  },
  {
   "parameters": {
    "mode": "manual",
    "assignments": {
     "assignments": [
      {
       "id": "پاسخ به ERP-0",
       "name": "trackingNumber",
       "value": "={{ $json.trackingNumber }}",
       "type": "number"
      },
      {
       "id": "پاسخ به ERP-1",
       "name": "requestId",
       "value": "={{ $json.id }}",
       "type": "string"
      },
      {
       "id": "پاسخ به ERP-2",
       "name": "status",
       "value": "={{ $json.status }}",
       "type": "string"
      },
      {
       "id": "پاسخ به ERP-3",
       "name": "currentSteps",
       "value": "={{ $json.currentSteps.join('، ') }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "name": "پاسخ به ERP",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    720,
    100
   ]
  },
  {
   "parameters": {
    "content": "## ثبت درخواست خرید از ERP\nERP سند را به وب‌هوک `erp/purchase-request` می‌فرستد (docNumber، description، amount، urgency، notes).\nn8n آن را در «فرآیند درخواست خرید» کارنما ثبت می‌کند؛ شمارهٔ سند به‌عنوان Idempotency-Key می‌رود تا تکرار، درخواست دوم نسازد.\nپاسخ: شمارهٔ پیگیری و مرحلهٔ فعلی.\nمتغیر: KARNEMA_PURCHASE_PROCESS_ID",
    "height": 260,
    "width": 420
   },
   "name": "راهنما",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -420,
    -260
   ]
  }
 ],
 "connections": {
  "اجرای دستی (آزمایش)": {
   "main": [
    [
     {
      "node": "سند ERP",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "ERP: سند درخواست خرید": {
   "main": [
    [
     {
      "node": "سند ERP",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "سند ERP": {
   "main": [
    [
     {
      "node": "کارنما: ثبت درخواست",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "کارنما: ثبت درخواست": {
   "main": [
    [
     {
      "node": "پاسخ به ERP",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "active": false,
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {},
 "tags": []
}