{
  "name": "Calidad de Lata \u2014 5) Reporte y planilla",
  "nodes": [
    {
      "id": "sched",
      "name": "Cada d\u00eda 07:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 7 * * *"
            }
          ]
        }
      }
    },
    {
      "id": "pg-kpis",
      "name": "KPIs del d\u00eda",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        220,
        0
      ],
      "retryOnFail": true,
      "maxTries": 2,
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "SELECT COUNT(*) AS evidencias, COUNT(r.evidence_id) AS procesadas, COUNT(*) FILTER (WHERE r.resultado = 'OK') AS ok, COUNT(*) FILTER (WHERE r.resultado = 'No OK') AS no_ok, COUNT(*) FILTER (WHERE r.revision_manual) AS revisiones, COUNT(*) FILTER (WHERE e.estado = 'duplicada') AS duplicadas, COUNT(*) FILTER (WHERE r.coherencia = false) AS incoherencias, COALESCE(SUM(r.tokens), 0) AS tokens, ROUND((COALESCE(SUM(r.tokens), 0) * 0.000005)::numeric, 4) AS costo_usd FROM evidencias e LEFT JOIN resultados r USING (evidence_id) WHERE e.capturado_en > now() - interval '24 hours';",
        "options": {}
      }
    },
    {
      "id": "pg-detalle",
      "name": "Detalle para planilla",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        440,
        0
      ],
      "retryOnFail": true,
      "maxTries": 2,
      "alwaysOutputData": true,
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "SELECT evidence_id, linea, equipo, capturado_en, tipo_foto, resultado, confianza, revision_manual, calidad_impresion, coherencia, motivo, textos::text AS textos, defectos::text AS defectos, hora_pantalla, tokens, revisado_por, estado_ingesta, imagen_ref FROM v_evidencias_completas WHERE capturado_en > now() - interval '24 hours' ORDER BY capturado_en;",
        "options": {}
      }
    },
    {
      "id": "csv",
      "name": "Armar planilla CSV",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        660,
        0
      ],
      "parameters": {
        "operation": "csv",
        "binaryPropertyName": "data",
        "options": {
          "fileName": "=planilla-calidad-{{ $now.format('yyyy-MM-dd') }}.csv"
        }
      }
    },
    {
      "id": "tg-doc",
      "name": "Enviar planilla",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        880,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "parameters": {
        "resource": "message",
        "operation": "sendDocument",
        "chatId": "-1003908341093",
        "binaryData": true,
        "binaryPropertyName": "data",
        "additionalFields": {}
      }
    },
    {
      "id": "tg-resumen",
      "name": "Resumen KPIs",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1100,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "-1003908341093",
        "text": "=\ud83d\udcca <b>REPORTE CALIDAD DE LATA \u2014 \u00faltimas 24 h</b>\n\n\ud83e\uddfe <b>Evidencias:</b> {{ $('KPIs del d\u00eda').first().json.evidencias }} (procesadas: {{ $('KPIs del d\u00eda').first().json.procesadas }})\n\u2705 <b>OK:</b> {{ $('KPIs del d\u00eda').first().json.ok }}\n\ud83d\udea8 <b>No OK:</b> {{ $('KPIs del d\u00eda').first().json.no_ok }}\n\ud83d\udd0d <b>Revisiones manuales:</b> {{ $('KPIs del d\u00eda').first().json.revisiones }}\n\u267b\ufe0f <b>Duplicadas:</b> {{ $('KPIs del d\u00eda').first().json.duplicadas }}\n\u2696\ufe0f <b>Incoherencias:</b> {{ $('KPIs del d\u00eda').first().json.incoherencias }}\n\n\ud83c\udfab <b>Tokens IA:</b> {{ $('KPIs del d\u00eda').first().json.tokens }} \u00b7 \ud83d\udcb5 <b>Costo est.:</b> USD {{ $('KPIs del d\u00eda').first().json.costo_usd }}\n\ud83d\udcce Planilla CSV adjunta arriba \u00b7 \ud83d\udd52 {{ $now.format('dd/MM HH:mm') }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      }
    },
    {
      "id": "sticky-5",
      "name": "Nota-reporte",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -220
      ],
      "parameters": {
        "content": "### 5) Reporte y planilla (minuta: automatizar planillas + indicadores)\nCada d\u00eda 07:00: KPIs de las \u00faltimas 24 h + planilla CSV con el detalle (desde v_evidencias_completas) enviados al grupo de Telegram. Requiere correr db/vistas-bi.sql. El horario se cambia en el Schedule Trigger.",
        "color": 4,
        "height": 180,
        "width": 560
      }
    }
  ],
  "connections": {
    "Cada d\u00eda 07:00": {
      "main": [
        [
          {
            "node": "KPIs del d\u00eda",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "KPIs del d\u00eda": {
      "main": [
        [
          {
            "node": "Detalle para planilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Detalle para planilla": {
      "main": [
        [
          {
            "node": "Armar planilla CSV",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Armar planilla CSV": {
      "main": [
        [
          {
            "node": "Enviar planilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar planilla": {
      "main": [
        [
          {
            "node": "Resumen KPIs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "I59vNrbU4KKGkHXF"
  }
}