{
 "name": "TWWP: supplier invoice capture and PO match (UC-0302)",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "twwp-accounts-payable-invoice-capture-0302",
    "responseMode": "responseNode",
    "options": {}
   },
   "id": "webhook",
   "name": "Webhook (synthetic input)",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "3d6d79d9-6690-5915-a56e-965412b0b9d9"
  },
  {
   "parameters": {
    "jsCode": "const b = $input.first().json.body || {};\nconst hint = (b.approved !== undefined && typeof b.approved !== 'boolean') ? 'approved must be true or false, written without quotes (not yes, and not \"true\")' : undefined;\nconst lines = String(b.email || '').split(/\\r?\\n/).map(s => s.trim());\nconst get = k => { const l = lines.find(x => x.toLowerCase().startsWith(k + ':')); return l ? l.slice(k.length + 1).trim() : null; };\nconst supplier = get('supplier'), po = get('po'), taxPct = parseFloat((get('tax') || '0').replace('%', ''));\nconst items = lines.filter(x => /^line:/i.test(x)).map(x => { const p = x.slice(5).split('|').map(s => s.trim()); return { desc: p[0], qty: Number(p[1]), unit: Number(p[2]) }; });\nconst net = Math.round(items.reduce((s, i) => s + i.qty * i.unit, 0) * 100) / 100;\nconst tax = Math.round(net * taxPct) / 100, total = Math.round((net + tax) * 100) / 100;\nconst order = (b.purchase_orders || {})[po];\nconst mismatches = [];\nif (!order) mismatches.push('no purchase order ' + po);\nelse for (const i of items) { const o = order.find(x => x.desc === i.desc); if (!o) mismatches.push('not on order: ' + i.desc); else { if (o.qty !== i.qty) mismatches.push('quantity ' + i.desc + ': ordered ' + o.qty + ', invoiced ' + i.qty); if (o.unit !== i.unit) mismatches.push('price ' + i.desc + ': ordered ' + o.unit + ', invoiced ' + i.unit); } }\nreturn [{ json: { hint, invoice: { supplier, po, lines: items, net, tax, total }, mismatches, hold_reasons: mismatches } }];"
   },
   "id": "extract-and-decide",
   "name": "Extract and decide",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    240,
    0
   ]
  },
  {
   "parameters": {
    "jsCode": "// DEMO ONLY. A person's approval is stood in for by the request field approved === true, so the tests run without a screen.\n// In real use replace this node with a SEPARATE approval step (a person's click or a signed event: authenticated, bound to this exact draft, asked again if the draft changes).\n// FIXED LOGIC, not a model field: anything that would send, pay or change data ALWAYS waits for approval. The extract / AI step can only ADD reasons to hold (hold_reasons);\n// it can never remove the hold, skip the approval, or open the gate. A missing or malformed hold_reasons is treated as a hold.\nconst j = $input.first().json;\nconst body = ($('Webhook (synthetic input)').first().json.body) || {};\nconst approved = body.approved === true;\nconst modelHolds = !Array.isArray(j.hold_reasons) || j.hold_reasons.length > 0;\nreturn [{ json: { ...j, gate_open: approved && !modelHolds } }];"
   },
   "id": "approval-check-(demo-toggle)",
   "name": "Approval check (demo toggle)",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    360,
    0
   ]
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict"
     },
     "conditions": [
      {
       "id": "approved",
       "leftValue": "={{ $json.gate_open }}",
       "rightValue": true,
       "operator": {
        "type": "boolean",
        "operation": "true",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "gate",
   "name": "Approval gate",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    600,
    0
   ]
  },
  {
   "parameters": {
    "jsCode": "const j = $input.first().json;\nreturn [{ json: { status: 'routed_to_payment_local', invoice: j.invoice, match_ok: true } }];"
   },
   "id": "local-stub-(drafts-only)",
   "name": "Local stub (drafts only)",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    840,
    -100
   ]
  },
  {
   "parameters": {
    "jsCode": "const j = $input.first().json;\nreturn [{ json: { ...(j.hint ? { approval_hint: j.hint } : {}), status: j.mismatches.length ? 'needs_review' : 'awaiting_approver', invoice: j.invoice, mismatches: j.mismatches } }];"
   },
   "id": "hold-for-approval",
   "name": "Hold for approval",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    840,
    100
   ]
  },
  {
   "parameters": {
    "respondWith": "json",
    "responseBody": "={{ $json }}",
    "options": {}
   },
   "id": "respond:-released",
   "name": "Respond: released",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    1080,
    -100
   ]
  },
  {
   "parameters": {
    "respondWith": "json",
    "responseBody": "={{ $json }}",
    "options": {}
   },
   "id": "respond:-held",
   "name": "Respond: held",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    1080,
    100
   ]
  },
  {
   "parameters": {
    "content": "APPROVAL GATE (demo): the approver signs off (approved=true). A price or quantity mismatch always needs review, even if approved. The request field approved stands in for a person: in real use replace the Approval check node with a separate, authenticated approval bound to the draft, never model output. Synthetic data only. No node here sends anything outside this machine; the 'AI step' is a deterministic stand-in to swap for an AI node.",
    "height": 230,
    "width": 440
   },
   "id": "note",
   "name": "Note",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    560,
    -220
   ]
  }
 ],
 "connections": {
  "Webhook (synthetic input)": {
   "main": [
    [
     {
      "node": "Extract and decide",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Extract and decide": {
   "main": [
    [
     {
      "node": "Approval check (demo toggle)",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approval check (demo toggle)": {
   "main": [
    [
     {
      "node": "Approval gate",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approval gate": {
   "main": [
    [
     {
      "node": "Local stub (drafts only)",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Hold for approval",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Local stub (drafts only)": {
   "main": [
    [
     {
      "node": "Respond: released",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Hold for approval": {
   "main": [
    [
     {
      "node": "Respond: held",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1",
  "saveDataSuccessExecution": "none",
  "saveDataErrorExecution": "none"
 },
 "id": "twwpuc0302",
 "active": false,
 "pinData": {},
 "tags": []
}