{
 "name": "otorize · AI invoicing with approval",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "order",
    "options": {}
   },
   "id": "1b1e0740-090f-41ef-8333-92d2d6454fc5",
   "name": "New order from the ERP",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "5bc04aba-491d-45a1-8bcd-06b99b41af2f"
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://api.anthropic.com/v1/messages",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ model: 'claude-sonnet-4-5', max_tokens: 500, messages: [{ role: 'user', content: 'Extract customer, amount and currency from: ' + $json.body.text }] }) }}",
    "options": {
     "timeout": 40000
    }
   },
   "id": "6819b01e-f959-42b6-873a-74c4daf5dbe5",
   "name": "Claude: read the order",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    240,
    0
   ]
  },
  {
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "76513e8e-8fb2-40d3-95b5-5d416b92396f",
       "name": "number",
       "value": "=INV-{{ $now.toFormat('yyMMdd-HHmmss') }}",
       "type": "string"
      },
      {
       "id": "a53eda68-3e53-424b-bb96-bd2c3d6cdb4c",
       "name": "customer",
       "value": "={{ $json.customer }}",
       "type": "string"
      },
      {
       "id": "dba55d29-2106-4fe1-b2e8-287cce1c9630",
       "name": "amount",
       "value": "={{ $json.amount }}",
       "type": "number"
      },
      {
       "id": "b24900a3-005f-4daa-84c7-1b0237bbe767",
       "name": "currency",
       "value": "={{ $json.currency }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "id": "4d1a5815-5611-40b5-82dc-84565a5c1907",
   "name": "Prepare the request",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    480,
    0
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://api.otorize.app/v1/rooms/ROOM_ID/proposals?wait=25",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ action: 'invoice.send', params: { number: $json.number, customer: $json.customer, amount: $json.amount, currency: $json.currency } }) }}",
    "options": {
     "timeout": 40000
    },
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   },
   "id": "f9d61cdd-60c2-4afe-acd4-0e08914736c1",
   "name": "otorize: ask for approval",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    720,
    0
   ]
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict"
     },
     "conditions": [
      {
       "id": "e91ff61a-02b0-4e1d-8db3-b42c43652119",
       "leftValue": "={{ $json.proposal.status }}",
       "rightValue": "approved",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "a23d3ca3-ee8c-461c-804a-311b437590ad",
   "name": "Approved?",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2,
   "position": [
    960,
    0
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://api-v2.fattureincloud.it/c/COMPANY_ID/issued_documents",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ data: { type: 'invoice', entity: { name: $json.proposal.params.customer }, amount_net: $json.proposal.params.amount } }) }}",
    "options": {
     "timeout": 40000
    }
   },
   "id": "89f807f5-1f74-4a54-aa54-8c6ebce98962",
   "name": "Send the invoice",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1200,
    -140
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "={{ 'https://api.otorize.app/v1/proposals/' + $('otorize: ask for approval').item.json.proposal.id + '/result' }}",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ status: 'executed', result: { sdi_id: String($json.data.id), state: $json.data.ei_status } }) }}",
    "options": {
     "timeout": 40000
    },
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   },
   "id": "cf5f3166-f181-4424-9ca0-7b39dfa8bd53",
   "name": "otorize: report the outcome",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1440,
    -140
   ]
  },
  {
   "parameters": {
    "jsCode": "// One line per invoice in the control log.\nreturn $input.all().map((i) => ({ json: { number: i.json.proposal.params.number, customer: i.json.proposal.params.customer, state: i.json.proposal.status } }));"
   },
   "id": "3b2877fc-a9b2-407d-b957-db22e22f6ef1",
   "name": "Write to the log",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    1680,
    -140
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://example.webhook.office.com/webhookb2/esempio",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ text: 'Invoice ' + $json.proposal.params.number + ' not sent: ' + $json.proposal.status }) }}",
    "options": {
     "timeout": 40000
    }
   },
   "id": "9a7d81a6-2a9d-4ca1-83b2-fd71088fe0f8",
   "name": "Notify accounts",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1200,
    140
   ]
  }
 ],
 "connections": {
  "New order from the ERP": {
   "main": [
    [
     {
      "node": "Claude: read the order",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Claude: read the order": {
   "main": [
    [
     {
      "node": "Prepare the request",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Prepare the request": {
   "main": [
    [
     {
      "node": "otorize: ask for approval",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "otorize: ask for approval": {
   "main": [
    [
     {
      "node": "Approved?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approved?": {
   "main": [
    [
     {
      "node": "Send the invoice",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Notify accounts",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Send the invoice": {
   "main": [
    [
     {
      "node": "otorize: report the outcome",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "otorize: report the outcome": {
   "main": [
    [
     {
      "node": "Write to the log",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {
  "New order from the ERP": [
   {
    "json": {
     "body": {
      "order": "4471",
      "text": "We confirm the order of 12 annual licences at EUR 200 each, total EUR 2,400 + VAT. Rossi Srl"
     }
    }
   }
  ],
  "Claude: read the order": [
   {
    "json": {
     "customer": "Rossi Srl",
     "amount": 2400,
     "currency": "EUR"
    }
   }
  ],
  "Send the invoice": [
   {
    "json": {
     "data": {
      "id": 90231577,
      "type": "invoice",
      "ei_status": "sent"
     }
    }
   }
  ],
  "Write to the log": [
   {
    "json": {
     "ok": true
    }
   }
  ],
  "Notify accounts": [
   {
    "json": {
     "ok": true
    }
   }
  ]
 }
}