{
  "name": "Generador Automático de Cotizaciones en PDF",
  "flow": [
    {
      "id": 1,
      "module": "google-forms:watchResponses",
      "version": 1,
      "parameters": {
        "formId": "{{GOOGLE_FORM_COTIZACIONES_ID}}",
        "limit": 5
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "formId", "type": "text", "label": "Form ID" }
        ]
      }
    },
    {
      "id": 2,
      "module": "google-sheets:getRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_PRODUCTOS_ID}}",
        "sheetId": "Productos",
        "filter": "A:A={{1.answers.producto}}"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 300, "y": 0 },
        "restore": {
          "spreadsheetId": { "label": "Catálogo de Productos" },
          "sheetId": { "label": "Productos" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" },
          { "name": "sheetId", "type": "text", "label": "Sheet Name" }
        ]
      }
    },
    {
      "id": 3,
      "module": "builtin:BasicMath",
      "version": 1,
      "parameters": {},
      "mapper": {
        "operations": [
          {
            "name": "subtotal",
            "expression": "{{2.precioUnitario}} * {{1.answers.cantidad}}"
          },
          {
            "name": "iva",
            "expression": "{{3.subtotal}} * 0.15"
          },
          {
            "name": "total",
            "expression": "{{3.subtotal}} + {{3.iva}}"
          },
          {
            "name": "numeroCotizacion",
            "expression": "COT-{{formatDate(now; \"YYYYMMDD\")}}-{{random(1000; 9999)}}"
          }
        ]
      },
      "metadata": {
        "designer": { "x": 600, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 4,
      "module": "google-docs:createFromTemplate",
      "version": 1,
      "parameters": {
        "templateId": "{{PLANTILLA_COTIZACION_ID}}",
        "folderId": "{{CARPETA_COTIZACIONES_ID}}",
        "name": "{{3.numeroCotizacion}} - {{1.answers.clienteNombre}}"
      },
      "mapper": {
        "replacements": {
          "{{NUMERO_COTIZACION}}": "{{3.numeroCotizacion}}",
          "{{FECHA}}": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "{{CLIENTE_NOMBRE}}": "{{1.answers.clienteNombre}}",
          "{{CLIENTE_EMAIL}}": "{{1.answers.clienteEmail}}",
          "{{CLIENTE_TELEFONO}}": "{{1.answers.clienteTelefono}}",
          "{{PRODUCTO}}": "{{1.answers.producto}}",
          "{{CANTIDAD}}": "{{1.answers.cantidad}}",
          "{{PRECIO_UNITARIO}}": "${{formatNumber(2.precioUnitario; 2)}}",
          "{{SUBTOTAL}}": "${{formatNumber(3.subtotal; 2)}}",
          "{{IVA_15}}": "${{formatNumber(3.iva; 2)}}",
          "{{TOTAL}}": "${{formatNumber(3.total; 2)}}"
        }
      },
      "metadata": {
        "designer": { "x": 900, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "templateId", "type": "text", "label": "Template Document ID" },
          { "name": "folderId", "type": "text", "label": "Destination 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": 1200, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "url", "type": "url", "label": "URL" }
        ]
      }
    },
    {
      "id": 6,
      "module": "gmail:sendEmail",
      "version": 2,
      "parameters": {
        "account": "{{GMAIL_CONNECTION}}"
      },
      "mapper": {
        "to": "{{1.answers.clienteEmail}}",
        "subject": "Cotización {{3.numeroCotizacion}} - {{1.answers.producto}}",
        "htmlBody": "<p>Estimado/a {{1.answers.clienteNombre}},</p><p>Adjunto encontrará la cotización solicitada No. <strong>{{3.numeroCotizacion}}</strong>.</p><p><strong>Resumen:</strong></p><ul><li>Producto: {{1.answers.producto}}</li><li>Cantidad: {{1.answers.cantidad}}</li><li>Subtotal: ${{formatNumber(3.subtotal; 2)}}</li><li>IVA (15%): ${{formatNumber(3.iva; 2)}}</li><li>Total: ${{formatNumber(3.total; 2)}}</li></ul><p>Esta cotización tiene una validez de 15 días.</p><p>Saludos cordiales.</p>",
        "attachments": [
          {
            "fileName": "{{3.numeroCotizacion}}.pdf",
            "data": "{{5.data}}"
          }
        ]
      },
      "metadata": {
        "designer": { "x": 1500, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "to", "type": "email", "label": "To" },
          { "name": "subject", "type": "text", "label": "Subject" }
        ]
      }
    },
    {
      "id": 7,
      "module": "google-sheets:addRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_COTIZACIONES_ID}}",
        "sheetId": "Registro",
        "includeHeaders": true
      },
      "mapper": {
        "values": {
          "0": "{{3.numeroCotizacion}}",
          "1": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "2": "{{1.answers.clienteNombre}}",
          "3": "{{1.answers.clienteEmail}}",
          "4": "{{1.answers.producto}}",
          "5": "{{1.answers.cantidad}}",
          "6": "{{formatNumber(3.total; 2)}}",
          "7": "Enviada"
        }
      },
      "metadata": {
        "designer": { "x": 1800, "y": 0 },
        "restore": {
          "spreadsheetId": { "label": "Registro de Cotizaciones" },
          "sheetId": { "label": "Registro" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" },
          { "name": "sheetId", "type": "text", "label": "Sheet Name" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}