{
 "name": "otorize · Fatturazione con AI e approvazione",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "ordine",
    "options": {}
   },
   "id": "6d604798-af0d-492b-995a-57c850db0bbd",
   "name": "Ordine dal gestionale",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "96891f4d-00d8-4c52-bf00-293d220891cf"
  },
  {
   "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: 'Estrai cliente, importo e valuta da: ' + $json.body.text }] }) }}",
    "options": {
     "timeout": 40000
    }
   },
   "id": "208b43c6-3237-450a-a31d-b327260c16ae",
   "name": "Claude: leggi l'ordine",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    240,
    0
   ]
  },
  {
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "de820156-478b-4278-aa4a-a741aeacd853",
       "name": "number",
       "value": "=FT-{{ $now.toFormat('yyMMdd-HHmmss') }}",
       "type": "string"
      },
      {
       "id": "89f1c3a6-4d73-4132-928f-49f9e6518448",
       "name": "customer",
       "value": "={{ $json.customer }}",
       "type": "string"
      },
      {
       "id": "c4d9de8e-6c33-4d18-acd0-67ec31cf310d",
       "name": "amount",
       "value": "={{ $json.amount }}",
       "type": "number"
      },
      {
       "id": "f81c80ad-b41f-42df-8fc6-43f547140e60",
       "name": "currency",
       "value": "={{ $json.currency }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "id": "76808b1a-c5df-48af-adb7-c8ac840fbfd3",
   "name": "Prepara la richiesta",
   "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": "c7d6b2f1-508b-49e7-8401-d2bb234279a5",
   "name": "otorize: chiedi approvazione",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    720,
    0
   ]
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict"
     },
     "conditions": [
      {
       "id": "86f47902-0f0b-467b-a7c2-e40195dbd6b0",
       "leftValue": "={{ $json.proposal.status }}",
       "rightValue": "approved",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "7cd67e0c-b02d-4c9b-8161-58a199270389",
   "name": "Approvata?",
   "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": "fd17d0d1-99aa-4f94-a23d-ed12e630342e",
   "name": "Invia allo SDI",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1200,
    -140
   ]
  },
  {
   "parameters": {
    "method": "POST",
    "url": "={{ 'https://api.otorize.app/v1/proposals/' + $('otorize: chiedi approvazione').item.json.proposal.id + '/result' }}",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ status: 'executed', result: { sdi_id: String($json.data.id), stato: $json.data.ei_status } }) }}",
    "options": {
     "timeout": 40000
    },
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   },
   "id": "87fdf4f5-0a08-4ee7-8085-f51cf5e5e65d",
   "name": "otorize: registra l'esito",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1440,
    -140
   ]
  },
  {
   "parameters": {
    "jsCode": "// Una riga per fattura nel registro di controllo.\nreturn $input.all().map((i) => ({ json: { numero: i.json.proposal.params.number, cliente: i.json.proposal.params.customer, stato: i.json.proposal.status } }));"
   },
   "id": "52ae44b9-b322-426e-8b5e-31d68510a5f6",
   "name": "Scrivi nel registro",
   "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: 'Fattura ' + $json.proposal.params.number + ' non inviata: ' + $json.proposal.status }) }}",
    "options": {
     "timeout": 40000
    }
   },
   "id": "c9f1a941-073c-4165-9d4d-7a26675446a9",
   "name": "Avvisa l'amministrazione",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1200,
    140
   ]
  }
 ],
 "connections": {
  "Ordine dal gestionale": {
   "main": [
    [
     {
      "node": "Claude: leggi l'ordine",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Claude: leggi l'ordine": {
   "main": [
    [
     {
      "node": "Prepara la richiesta",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Prepara la richiesta": {
   "main": [
    [
     {
      "node": "otorize: chiedi approvazione",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "otorize: chiedi approvazione": {
   "main": [
    [
     {
      "node": "Approvata?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approvata?": {
   "main": [
    [
     {
      "node": "Invia allo SDI",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Avvisa l'amministrazione",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Invia allo SDI": {
   "main": [
    [
     {
      "node": "otorize: registra l'esito",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "otorize: registra l'esito": {
   "main": [
    [
     {
      "node": "Scrivi nel registro",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {
  "Ordine dal gestionale": [
   {
    "json": {
     "body": {
      "ordine": "4471",
      "text": "Confermiamo l'ordine di 12 licenze annuali a 200 euro l'una, totale 2.400 euro + IVA. Rossi Srl"
     }
    }
   }
  ],
  "Claude: leggi l'ordine": [
   {
    "json": {
     "customer": "Rossi Srl",
     "amount": 2400,
     "currency": "EUR"
    }
   }
  ],
  "Invia allo SDI": [
   {
    "json": {
     "data": {
      "id": 90231577,
      "type": "invoice",
      "ei_status": "sent"
     }
    }
   }
  ],
  "Scrivi nel registro": [
   {
    "json": {
     "ok": true
    }
   }
  ],
  "Avvisa l'amministrazione": [
   {
    "json": {
     "ok": true
    }
   }
  ]
 }
}