{
  "name": "Generador de Reportes Personalizados por Email",
  "flow": [
    {
      "id": 1,
      "module": "builtin:BasicSchedule",
      "version": 1,
      "parameters": {
        "interval": "weekly",
        "day": "monday",
        "time": "07:00"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 2,
      "module": "google-sheets:getRows",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_VENTAS_ID}}",
        "sheetId": "Ventas",
        "range": "A:H"
      },
      "mapper": {
        "filter": {
          "dateColumn": "A",
          "from": "{{formatDate(addDays(now; -7); \"DD/MM/YYYY\")}}",
          "to": "{{formatDate(now; \"DD/MM/YYYY\")}}"
        }
      },
      "metadata": {
        "designer": { "x": 300, "y": -100 },
        "restore": {
          "spreadsheetId": { "label": "Datos de Ventas" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    },
    {
      "id": 3,
      "module": "google-sheets:getRows",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_LEADS_ID}}",
        "sheetId": "Leads",
        "range": "A:F"
      },
      "mapper": {
        "filter": {
          "dateColumn": "A",
          "from": "{{formatDate(addDays(now; -7); \"DD/MM/YYYY\")}}",
          "to": "{{formatDate(now; \"DD/MM/YYYY\")}}"
        }
      },
      "metadata": {
        "designer": { "x": 300, "y": 100 },
        "restore": {
          "spreadsheetId": { "label": "Datos de Leads" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    },
    {
      "id": 4,
      "module": "google-docs:createFromTemplate",
      "version": 1,
      "parameters": {
        "templateId": "{{PLANTILLA_REPORTE_ID}}",
        "folderId": "{{CARPETA_REPORTES_ID}}/{{formatDate(now; \"YYYY-MM\")}}",
        "name": "Reporte Semanal - {{formatDate(addDays(now; -7); \"DD MMM\")}} al {{formatDate(now; \"DD MMM YYYY\")}}"
      },
      "mapper": {
        "replacements": {
          "{{FECHA_INICIO}}": "{{formatDate(addDays(now; -7); \"DD/MM/YYYY\")}}",
          "{{FECHA_FIN}}": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "{{TOTAL_VENTAS}}": "${{formatNumber(sum(2; \"total\"); 2)}}",
          "{{NUM_TRANSACCIONES}}": "{{length(2)}}",
          "{{TICKET_PROMEDIO}}": "${{formatNumber(sum(2; \"total\") / length(2); 2)}}",
          "{{TOTAL_LEADS}}": "{{length(3)}}",
          "{{LEADS_CALIENTES}}": "{{length(filter(3; \"clasificacion\"; \"Caliente\"))}}",
          "{{TASA_CONVERSION}}": "{{formatNumber((length(filter(3; \"estado\"; \"Cerrado\")) / length(3)) * 100; 1)}}%",
          "{{VARIACION_VENTAS}}": "{{formatNumber(((sum(2; \"total\") - VENTAS_SEMANA_ANTERIOR) / VENTAS_SEMANA_ANTERIOR) * 100; 1)}}%"
        }
      },
      "metadata": {
        "designer": { "x": 600, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "templateId", "type": "text", "label": "Template ID" },
          { "name": "folderId", "type": "text", "label": "Folder ID" }
        ]
      }
    },
    {
      "id": 5,
      "module": "http:ActionSendData",
      "version": 3,
      "parameters": {},
      "mapper": {
        "url": "https://docs.google.com/document/d/{{4.documentId}}/export?format=pdf",
        "method": "GET",
        "headers": [
          { "name": "Authorization", "value": "Bearer {{GOOGLE_ACCESS_TOKEN}}" }
        ]
      },
      "metadata": {
        "designer": { "x": 900, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "url", "type": "url", "label": "URL" }
        ]
      }
    },
    {
      "id": 6,
      "module": "google-drive:uploadFile",
      "version": 2,
      "parameters": {
        "folderId": "{{CARPETA_REPORTES_ID}}/{{formatDate(now; \"YYYY-MM\")}}"
      },
      "mapper": {
        "name": "Reporte_Semanal_{{formatDate(now; \"YYYY-MM-DD\")}}.pdf",
        "data": "{{5.data}}",
        "mimeType": "application/pdf"
      },
      "metadata": {
        "designer": { "x": 1200, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "folderId", "type": "text", "label": "Folder ID" }
        ]
      }
    },
    {
      "id": 7,
      "module": "gmail:sendEmail",
      "version": 2,
      "parameters": {
        "account": "{{GMAIL_CONNECTION}}"
      },
      "mapper": {
        "to": "{{LISTA_DESTINATARIOS}}",
        "subject": "📊 Reporte Semanal - {{formatDate(addDays(now; -7); \"DD MMM\")}} al {{formatDate(now; \"DD MMM YYYY\")}}",
        "htmlBody": "<p>Buen día,</p><p>Adjunto el reporte semanal de rendimiento del negocio correspondiente al período <strong>{{formatDate(addDays(now; -7); \"DD/MM/YYYY\")}} - {{formatDate(now; \"DD/MM/YYYY\")}}</strong>.</p><p><strong>Resumen ejecutivo:</strong></p><ul><li>Ventas totales: ${{formatNumber(sum(2; \"total\"); 2)}}</li><li>Transacciones: {{length(2)}}</li><li>Nuevos leads: {{length(3)}}</li></ul><p>Para más detalles, revise el PDF adjunto.</p><p>Saludos cordiales.</p>",
        "attachments": [
          {
            "fileName": "Reporte_Semanal_{{formatDate(now; \"YYYY-MM-DD\")}}.pdf",
            "data": "{{5.data}}"
          }
        ]
      },
      "metadata": {
        "designer": { "x": 1500, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "to", "type": "email", "label": "To" },
          { "name": "subject", "type": "text", "label": "Subject" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": false,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": true
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}