{
  "name": "Alertas de Stock Bajo y Reorden Automático",
  "flow": [
    {
      "id": 1,
      "module": "builtin:BasicSchedule",
      "version": 1,
      "parameters": {
        "interval": "daily",
        "time": "07:00"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 2,
      "module": "google-sheets:searchRows",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_INVENTARIO_ID}}",
        "sheetId": "Inventario"
      },
      "mapper": {},
      "filter": {
        "name": "Stock bajo o igual al mínimo",
        "conditions": [
          { "a": "{{2.stockActual}}", "o": "number:lessThanOrEqual", "b": "{{2.stockMinimo}}" }
        ]
      },
      "metadata": {
        "designer": { "x": 300, "y": 0 },
        "restore": {
          "spreadsheetId": { "label": "Inventario" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    },
    {
      "id": 3,
      "module": "whatsapp-business:sendMessage",
      "version": 1,
      "parameters": {
        "phoneNumberId": "{{WHATSAPP_PHONE_ID}}",
        "recipientPhone": "{{ADMIN_PHONE}}"
      },
      "mapper": {
        "type": "text",
        "text": "📦 *Alerta de Stock Bajo*\n\nProducto: {{2.nombreProducto}}\nSKU: {{2.sku}}\nStock actual: {{2.stockActual}} unidades\nStock mínimo: {{2.stockMinimo}} unidades\nProveedor: {{2.proveedor}}\n\nSe generará una orden de compra automática."
      },
      "metadata": {
        "designer": { "x": 600, "y": -150 },
        "restore": {},
        "expect": [
          { "name": "phoneNumberId", "type": "text", "label": "Phone Number ID" }
        ]
      }
    },
    {
      "id": 4,
      "module": "google-docs:createFromTemplate",
      "version": 1,
      "parameters": {
        "templateId": "{{PLANTILLA_ORDEN_COMPRA_ID}}",
        "folderId": "{{CARPETA_ORDENES_ID}}",
        "name": "OC-{{formatDate(now; \"YYYYMMDD\")}}-{{2.sku}}"
      },
      "mapper": {
        "replacements": {
          "{{NUMERO_OC}}": "OC-{{formatDate(now; \"YYYYMMDD\")}}-{{random(100; 999)}}",
          "{{FECHA}}": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "{{PROVEEDOR_NOMBRE}}": "{{2.proveedor}}",
          "{{PROVEEDOR_EMAIL}}": "{{2.proveedorEmail}}",
          "{{PRODUCTO}}": "{{2.nombreProducto}}",
          "{{SKU}}": "{{2.sku}}",
          "{{CANTIDAD_SUGERIDA}}": "{{2.cantidadReorden}}",
          "{{PRECIO_UNITARIO}}": "${{formatNumber(2.precioCompra; 2)}}",
          "{{SUBTOTAL}}": "${{formatNumber(2.cantidadReorden * 2.precioCompra; 2)}}",
          "{{IVA}}": "${{formatNumber(2.cantidadReorden * 2.precioCompra * 0.15; 2)}}",
          "{{TOTAL}}": "${{formatNumber(2.cantidadReorden * 2.precioCompra * 1.15; 2)}}"
        }
      },
      "metadata": {
        "designer": { "x": 600, "y": 150 },
        "restore": {},
        "expect": [
          { "name": "templateId", "type": "text", "label": "Template ID" },
          { "name": "folderId", "type": "text", "label": "Folder ID" }
        ]
      }
    },
    {
      "id": 5,
      "module": "gmail:sendEmail",
      "version": 2,
      "parameters": {
        "account": "{{GMAIL_CONNECTION}}"
      },
      "mapper": {
        "to": "{{2.proveedorEmail}}",
        "subject": "Orden de Compra - {{2.nombreProducto}} ({{2.sku}})",
        "htmlBody": "<p>Estimado proveedor,</p><p>Por medio de la presente, le enviamos la orden de compra para el siguiente producto:</p><ul><li><strong>Producto:</strong> {{2.nombreProducto}}</li><li><strong>SKU:</strong> {{2.sku}}</li><li><strong>Cantidad:</strong> {{2.cantidadReorden}} unidades</li></ul><p>Adjunto encontrará el documento formal de la orden de compra.</p><p>Agradecemos su pronta confirmación y estimado de entrega.</p><p>Saludos cordiales.</p>",
        "attachments": [
          {
            "fileName": "OC-{{formatDate(now; \"YYYYMMDD\")}}-{{2.sku}}.pdf",
            "googleDocId": "{{4.documentId}}"
          }
        ]
      },
      "metadata": {
        "designer": { "x": 900, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "to", "type": "email", "label": "To" },
          { "name": "subject", "type": "text", "label": "Subject" }
        ]
      }
    },
    {
      "id": 6,
      "module": "google-sheets:updateRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_INVENTARIO_ID}}",
        "sheetId": "Inventario",
        "rowId": "{{2.__ROW_NUMBER__}}"
      },
      "mapper": {
        "values": {
          "8": "Pedido enviado",
          "9": "{{formatDate(now; \"DD/MM/YYYY\")}}",
          "10": "OC-{{formatDate(now; \"YYYYMMDD\")}}-{{2.sku}}"
        }
      },
      "metadata": {
        "designer": { "x": 1200, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": false,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": true
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}