{
  "name": "Renta Veh\u00edculos Workflow",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": [
          {
            "cronExpression": "0 9 * * MON"
          }
        ]
      },
      "name": "Weekly Trigger",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        100,
        100
      ]
    },
    {
      "parameters": {
        "chatId": "={{$node[\"Weekly Trigger\"].json[\"chatIdList\"]}}",
        "messageText": "Por favor confirma la placa de tu veh\u00edculo para el reporte semanal."
      },
      "name": "Ask Plate",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        300,
        100
      ],
      "credentials": {
        "telegramApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "event": "message",
        "options": {
          "onlyCommands": false
        }
      },
      "name": "Listen Plate",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        100,
        250
      ],
      "credentials": {
        "telegramApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "plate",
              "value": "={{$json[\"text\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Plate",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        300,
        250
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json[\"plate\"]}}",
        "messageText": "Env\u00eda fotos (frontal, trasera, costados e interior) y el kilometraje."
      },
      "name": "Request Media",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        500,
        250
      ],
      "credentials": {
        "telegramApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "event": "message",
        "options": {
          "onlyCommands": false
        }
      },
      "name": "Listen Media",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        100,
        400
      ],
      "credentials": {
        "telegramApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "functionCode": "const photos = []; for (const file of items[0].binary) { photos.push(file.fileName); } return [{ json: { photos, mileage: $json['text'] } }];"
      },
      "name": "Extract Media",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        300,
        400
      ]
    },
    {
      "parameters": {
        "operation": "upload",
        "binaryPropertyName": "photos",
        "folderId": "root"
      },
      "name": "Save to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        500,
        400
      ],
      "credentials": {
        "googleDriveOAuth2": "<your credential>"
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "url": "={{`https://api.contalink.com/v1/invoices`}}",
        "method": "POST",
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "chatId",
              "value": "={{$json[\"chatId\"]}}"
            },
            {
              "name": "driverName",
              "value": "={{$json[\"driverName\"]}}"
            },
            {
              "name": "plate",
              "value": "={{$json[\"plate\"]}}"
            },
            {
              "name": "date",
              "value": "={{$now.toLocaleDateString()}}"
            },
            {
              "name": "mileage",
              "value": "={{$json[\"mileage\"]}}"
            },
            {
              "name": "rental",
              "value": "={{$json[\"rentalAmount\"]}}"
            },
            {
              "name": "fuelVoucher",
              "value": "={{$json[\"voucherAmount\"]}}"
            },
            {
              "name": "commission",
              "value": "={{$json[\"voucherAmount\"] * 0.035}}"
            },
            {
              "name": "total",
              "value": "={{$json[\"rentalAmount\"] + $json[\"voucherAmount\"] * 1.035}}"
            }
          ]
        }
      },
      "name": "Create Invoice",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        700,
        400
      ],
      "credentials": {
        "httpOAuth2Api": "<your credential>"
      }
    },
    {
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_SHEET_ID",
        "range": "A1",
        "valueInputMode": "USER_ENTERED",
        "options": {},
        "values": [
          [
            "={{$json[\"driverName\"]}}",
            "={{$json[\"plate\"]}}",
            "={{$now.toISOString()}}",
            "={{$json[\"mileage\"]}}",
            "={{$json[\"rentalAmount\"]}}",
            "={{$json[\"voucherAmount\"]}}",
            "={{$json[\"rentalAmount\"] + $json[\"voucherAmount\"] * 1.035}}",
            "={{$json[\"invoiceId\"]}}"
          ]
        ]
      },
      "name": "Log to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        900,
        400
      ],
      "credentials": {
        "googleSheetsOAuth2": "<your credential>"
      }
    }
  ],
  "connections": {
    "Weekly Trigger": {
      "main": [
        [
          {
            "node": "Ask Plate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ask Plate": {
      "main": [
        [
          {
            "node": "Listen Plate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Listen Plate": {
      "main": [
        [
          {
            "node": "Set Plate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Plate": {
      "main": [
        [
          {
            "node": "Request Media",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Listen Media": {
      "main": [
        [
          {
            "node": "Extract Media",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Media": {
      "main": [
        [
          {
            "node": "Save to Drive",
            "type": "main",
            "index": 0
          },
          {
            "node": "Create Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Invoice": {
      "main": [
        [
          {
            "node": "Log to Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}