{
  "name": "Alertas de Inventario Bajo y Reabastecimiento",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 8
            }
          ]
        }
      },
      "id": "c5d6e7f8-0015-4a01-b001-ef1234567001",
      "name": "Schedule Trigger - Diario 8AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Inventario"
        },
        "options": {
          "range": "A:G"
        }
      },
      "id": "c5d6e7f8-0015-4a02-b002-ef1234567002",
      "name": "Google Sheets - Leer Inventario",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [500, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ parseInt($json.stock_actual) }}",
              "operation": "smallerEqual",
              "value2": "={{ parseInt($json.stock_minimo) }}"
            }
          ]
        }
      },
      "id": "c5d6e7f8-0015-4a03-b003-ef1234567003",
      "name": "IF - Stock Bajo",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [750, 300]
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n  const stockActual = parseInt(item.json.stock_actual);\n  const stockMinimo = parseInt(item.json.stock_minimo);\n  const promedioVentasMes = parseInt(item.json.promedio_ventas_mes || 30);\n  \n  // Calcular cantidad sugerida: promedio mensual + buffer 20%\n  const cantidadSugerida = Math.ceil(promedioVentasMes * 1.2);\n  \n  results.push({\n    json: {\n      producto: item.json.producto,\n      stockActual,\n      stockMinimo,\n      cantidadSugerida,\n      proveedor: item.json.proveedor,\n      correoProveedor: item.json.correo_proveedor,\n      urgencia: stockActual === 0 ? 'CRÍTICA' : stockActual <= stockMinimo / 2 ? 'ALTA' : 'MEDIA'\n    }\n  });\n}\n\nreturn results;"
      },
      "id": "c5d6e7f8-0015-4a04-b004-ef1234567004",
      "name": "Function - Generar Orden de Compra",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [1000, 300]
    },
    {
      "parameters": {
        "sendTo": "=dueño@negocio.com",
        "subject": "=⚠️ Alerta de Inventario Bajo - {{ $json.urgencia }} | {{ $json.producto }}",
        "emailType": "html",
        "message": "=<h2>⚠️ Alerta de Inventario Bajo</h2><table border='1' cellpadding='8' style='border-collapse:collapse;width:100%;'><tr style='background:#ff6b6b;color:white;'><th>Producto</th><th>Stock Actual</th><th>Stock Mínimo</th><th>Urgencia</th><th>Cantidad Sugerida</th><th>Proveedor</th></tr><tr><td>{{ $json.producto }}</td><td style='text-align:center;color:red;'><strong>{{ $json.stockActual }}</strong></td><td style='text-align:center;'>{{ $json.stockMinimo }}</td><td style='text-align:center;'>{{ $json.urgencia }}</td><td style='text-align:center;color:green;'><strong>{{ $json.cantidadSugerida }}</strong></td><td>{{ $json.proveedor }}</td></tr></table><p><em>Orden de compra sugerida generada automáticamente.</em></p>",
        "options": {}
      },
      "id": "c5d6e7f8-0015-4a05-b005-ef1234567005",
      "name": "Gmail - Alerta al Dueño",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1250, 200],
      "credentials": {
        "gmailOAuth2": {
          "id": "",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "={{ $json.correoProveedor }}",
        "subject": "=Orden de Compra - {{ $json.producto }} ({{ $json.cantidadSugerida }} unidades)",
        "emailType": "html",
        "message": "=<h2>Orden de Compra</h2><p>Estimados,</p><p>Por medio del presente solicitamos la siguiente orden de compra:</p><table border='1' cellpadding='8' style='border-collapse:collapse;'><tr><th>Producto</th><th>Cantidad</th></tr><tr><td>{{ $json.producto }}</td><td>{{ $json.cantidadSugerida }} unidades</td></tr></table><p>Por favor confirmar disponibilidad y tiempo de entrega.</p><p>Saludos cordiales.</p>",
        "options": {}
      },
      "id": "c5d6e7f8-0015-4a06-b006-ef1234567006",
      "name": "Gmail - Orden al Proveedor",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1250, 400],
      "credentials": {
        "gmailOAuth2": {
          "id": "",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger - Diario 8AM": {
      "main": [
        [
          {
            "node": "Google Sheets - Leer Inventario",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Leer Inventario": {
      "main": [
        [
          {
            "node": "IF - Stock Bajo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF - Stock Bajo": {
      "main": [
        [
          {
            "node": "Function - Generar Orden de Compra",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Function - Generar Orden de Compra": {
      "main": [
        [
          {
            "node": "Gmail - Alerta al Dueño",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gmail - Orden al Proveedor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "e-commerce"
    },
    {
      "name": "inventario"
    },
    {
      "name": "alertas"
    }
  ]
}
