{
  "name": "Facturas PDF a Google Sheets (FluIA)",
  "nodes": [
    {
      "parameters": {
        "content": "## Facturas PDF a Google Sheets\n1. Crea una carpeta en Google Drive para las facturas y pon su ID en el disparador y en 'Descargar PDF'.\n2. Conecta Google Drive, Anthropic y Google Sheets.\n3. La hoja necesita estas columnas: archivo, proveedor, numero, fecha, base, iva, total, moneda.\n4. Sube un PDF a la carpeta para probar.\n\nPlantilla de fluia.es",
        "height": 220,
        "width": 520
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "9eb63695-063b-43fa-b6a3-4d7f833e0c6c",
      "name": "Nota 30f2"
    },
    {
      "parameters": {
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "id",
          "value": "ID_DE_LA_CARPETA"
        },
        "event": "fileCreated",
        "options": {}
      },
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        0,
        320
      ],
      "id": "73706573-740e-457a-b070-1f965176d66b",
      "name": "Nueva factura en la carpeta",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "REEMPLAZAR",
          "name": "Google Drive (tu cuenta)"
        }
      }
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        240,
        320
      ],
      "id": "6efb5742-c7ad-40ee-873e-edc3606dfcb6",
      "name": "Descargar PDF",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "REEMPLAZAR",
          "name": "Google Drive (tu cuenta)"
        }
      }
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "data",
        "options": {
          "joinPages": true
        }
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        480,
        320
      ],
      "id": "10794ac4-5d3d-4a71-8612-310a73e1dc12",
      "name": "Leer el texto del PDF"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.text }}",
        "messages": {
          "messageValues": [
            {
              "type": "SystemMessagePromptTemplate",
              "message": "Extrae de esta factura los datos y devuélvelos exactamente en el formato JSON que se te pide. Reglas: fecha en formato AAAA-MM-DD; importes como números sin símbolo de moneda ni separador de miles, con punto decimal; moneda en código de tres letras (EUR, USD, MXN…). Si un dato no aparece, usa null. No añadas texto fuera del JSON."
            }
          ]
        },
        "batching": {},
        "hasOutputParser": true
      },
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [
        720,
        320
      ],
      "id": "58ae6ce9-9daa-4668-ab0e-4a6f5aac9f62",
      "name": "Claude extrae los datos"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "claude-sonnet-5"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.6,
      "position": [
        720,
        540
      ],
      "id": "48f538a0-9622-42b0-a652-469c1e9e3b0b",
      "name": "Claude",
      "credentials": {
        "anthropicApi": {
          "id": "REEMPLAZAR",
          "name": "Anthropic (tu clave de API)"
        }
      }
    },
    {
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\"type\": \"object\", \"properties\": {\"proveedor\": {\"type\": \"string\"}, \"numero\": {\"type\": \"string\"}, \"fecha\": {\"type\": \"string\"}, \"base\": {\"type\": \"number\"}, \"iva\": {\"type\": \"number\"}, \"total\": {\"type\": \"number\"}, \"moneda\": {\"type\": \"string\"}}, \"required\": [\"proveedor\", \"total\"]}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        960,
        540
      ],
      "id": "141c333b-8a7b-4a7c-a885-7fa0b97b7cfd",
      "name": "Formato JSON"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "ID_DE_TU_HOJA"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Facturas"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "archivo": "={{ $('Nueva factura en la carpeta').item.json.name }}",
            "proveedor": "={{ $json.output.proveedor }}",
            "numero": "={{ $json.output.numero }}",
            "fecha": "={{ $json.output.fecha }}",
            "base": "={{ $json.output.base }}",
            "iva": "={{ $json.output.iva }}",
            "total": "={{ $json.output.total }}",
            "moneda": "={{ $json.output.moneda }}"
          },
          "schema": [
            {
              "id": "archivo",
              "displayName": "archivo",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "proveedor",
              "displayName": "proveedor",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "numero",
              "displayName": "numero",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "fecha",
              "displayName": "fecha",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "base",
              "displayName": "base",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "iva",
              "displayName": "iva",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "total",
              "displayName": "total",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "moneda",
              "displayName": "moneda",
              "type": "string",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true
            }
          ],
          "matchingColumns": [],
          "attemptToConvertTypes": true,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1100,
        320
      ],
      "id": "d834b8ff-4eaa-426d-ba47-015dcc96bd6a",
      "name": "Añadir fila en Google Sheets",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "REEMPLAZAR",
          "name": "Google Sheets (tu cuenta)"
        }
      }
    }
  ],
  "connections": {
    "Nueva factura en la carpeta": {
      "main": [
        [
          {
            "node": "Descargar PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Descargar PDF": {
      "main": [
        [
          {
            "node": "Leer el texto del PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Leer el texto del PDF": {
      "main": [
        [
          {
            "node": "Claude extrae los datos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude": {
      "ai_languageModel": [
        [
          {
            "node": "Claude extrae los datos",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Formato JSON": {
      "ai_outputParser": [
        [
          {
            "node": "Claude extrae los datos",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Claude extrae los datos": {
      "main": [
        [
          {
            "node": "Añadir fila en Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "instanceId": "fluia-plantilla"
  }
}