import { auth } from "../../lib/auth"; import { ownerFilter } from "../../lib/authz"; import { getRenovClosed, getSystemUsers } from "../../lib/queries"; import { agg, EJECUTIVOS_RENOV } from "../../lib/derive"; import LoginPrompt from "../../components/LoginPrompt"; import OwnerControl from "../../components/OwnerControl"; import ErrorNotice from "../../components/ErrorNotice"; import Panel from "../../components/pipeline/Panel"; import WonTable from "../../components/pipeline/WonTable"; import FilterChips from "../../components/FilterChips"; import FilterableRow from "../../components/FilterableRow"; import { money, money2 } from "../../lib/constants"; import { parseFiltParam, applyFilt } from "../../lib/filterState"; export const dynamic = "force-dynamic"; const DIM_LABELS = { tipo: "Momento", owner: "Ejecutivo", linea: "Línea de Negocio", servicio: "Servicio", origen: "Origen", cliente: "Cliente" }; function currentMonth() { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`; } function retencion(w, l) { const t = w.length + l.length; return t ? Math.round((w.length / t) * 100) : null; } export default async function RenovLogradasPage({ searchParams }) { const session = await auth(); if (!session?.email) return ; const sp = await searchParams; const { owner } = sp; const scope = ownerFilter(session, owner); const mes = currentMonth(); const filt = parseFiltParam(sp.f); let wonRaw, lostRaw, allUsers; try { [wonRaw, lostRaw, allUsers] = await Promise.all([ getRenovClosed(session.accessToken, scope, 1, mes), getRenovClosed(session.accessToken, scope, 2, mes), getSystemUsers(session.accessToken), ]); } catch (error) { return ; } const owners = allUsers.filter((u) => EJECUTIVOS_RENOV.includes(u.name)); const won = applyFilt(wonRaw.map((o) => ({ ...o, val: o.actual })), filt); const lost = applyFilt(lostRaw, filt); const sumW = won.reduce((a, o) => a + o.val, 0); const sumL = lost.reduce((a, o) => a + (o.est || 0), 0); const ret = retencion(won, lost); const aTiempo = won.filter((o) => o.tipo === "Próximo a Vencer en 30/60/90").length; const pctATiempo = won.length ? Math.round((aTiempo / won.length) * 100) : 0; // Retención por ejecutivo — ganadas vs. perdidas de cada quien, en MRR. const ejecutivos = [...new Set([...won.map((o) => o.owner), ...lost.map((o) => o.owner)])]; const retPorEjec = ejecutivos .map((ow) => ({ k: ow, gana: won.filter((o) => o.owner === ow).reduce((a, o) => a + o.val, 0), pierde: lost.filter((o) => o.owner === ow).reduce((a, o) => a + (o.est || 0), 0), })) .sort((a, b) => b.gana + b.pierde - (a.gana + a.pierde)); return (

Renovaciones Logradas — {mes}

{money2(sumW)}MRR renovado
{won.length}Renovaciones logradas
{ret == null ? "n/d" : `${ret}%`} Tasa de retención (ganadas ÷ cerradas)
{money2(sumL)}MRR perdido por no renovar
{money2(won.length ? sumW / won.length : 0)}Ticket promedio renovado
{pctATiempo}% Renovadas ANTES de vencer
De cada 100 contratos que se cerraron este mes, se conservaron {ret == null ? "—" : ret}. Acá no hay meta que cumplir: la meta comercial excluye renovaciones a propósito, porque mide negocio nuevo y de expansión. Eso no las hace menores. El número que sí manda acá es la retención, y el costo de fallar es churn.
{pctATiempo < 50 && (
Solo {pctATiempo}% se renovó antes de vencer ({aTiempo} de {won.length}). El resto se firmó con el contrato ya vencido, cuando el cliente está más expuesto a la competencia.
)} {retPorEjec.length > 0 && (

Retención por ejecutivo

Ganadas vs. perdidas de cada quien, en MRR

{retPorEjec.map((r) => { const max = Math.max(...retPorEjec.flatMap((x) => [x.gana, x.pierde]), 0.1); return (
{r.k}
Gana
{money2(r.gana)}
Pierde
{money2(r.pierde)}
); })}
)}
o.val)} dimKey="tipo" /> o.val)} dimKey="owner" /> o.val)} dimKey="linea" /> o.val)} top={7} dimKey="servicio" /> o.val)} top={7} dimKey="origen" /> o.val)} top={7} dimKey="cliente" />

Detalle de renovaciones logradas

{won.length === 0 ?

No hay renovaciones logradas en este período para este filtro.

: }
); }