import { auth } from "../../lib/auth"; import { ownerFilter } from "../../lib/authz"; import { getRenovClosed, getMotivosCierre, getSystemUsers } from "../../lib/queries"; import { agg, EJECUTIVOS_RENOV, catsWonLost } 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 ComparePanelMotivo from "../../components/pipeline/ComparePanelMotivo"; import { DYN, money2 } from "../../lib/constants"; export const dynamic = "force-dynamic"; 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 RenovPerdidasPage({ searchParams }) { const session = await auth(); if (!session?.email) return ; const { owner } = await searchParams; const scope = ownerFilter(session, owner); const mes = currentMonth(); let lost, won, allUsers; try { [lost, won, allUsers] = await Promise.all([ getRenovClosed(session.accessToken, scope, 2, mes), getRenovClosed(session.accessToken, scope, 1, mes), getSystemUsers(session.accessToken), ]); const motivos = await getMotivosCierre(lost.map((o) => o.id), session.accessToken); lost = lost.map((o) => ({ ...o, motivo: motivos[o.id]?.motivo || "Sin registrar", competidor: motivos[o.id]?.competidor || o.competidor })); } catch (error) { return ; } const owners = allUsers.filter((u) => EJECUTIVOS_RENOV.includes(u.name)); const sumL = lost.reduce((a, o) => a + (o.est || 0), 0); const ret = retencion(won, lost); const churn = ret == null ? null : 100 - ret; const depu = lost.filter((o) => o.motivo === "Descartada sin motivo (depuración)").length; const conMotivo = lost.length - depu; const catsLinea = catsWonLost(won, lost, "linea"); return (

Renovaciones Perdidas — {mes}

{lost.length}Contratos no renovados
{money2(sumL)}MRR perdido
20 ? "pierde" : ""}`}>{churn == null ? "n/d" : `${churn}%`} Churn del período
{conMotivo}Con motivo real declarado
{money2(lost.length ? sumL / lost.length : 0)}Ticket promedio perdido
Cada contrato de esta lista es MRR que ya estaba adentro y se fue.{" "} {depu > 0 && ( <>Ojo con la lectura: {depu} de {lost.length} cerraron como Envejecida/Cancelada/Agotada, que es depuración de registros y no un rechazo del cliente — el análisis de causa real se apoya en las{" "} {conMotivo} restantes. )} Y recordá que el seguimiento por WhatsApp no está integrado a Dynamics: cuando el motivo queda vacío, lo correcto es decir «no hay motivo registrado en el CRM», no «no se gestionó».
{catsLinea.length > 0 && ( )}
o.est)} cls="pierde" /> o.est)} cls="pierde" /> o.competidor && o.competidor !== "Sin registrar"), "competidor", (o) => o.est)} cls="pierde" top={7} /> o.est)} cls="pierde" /> o.est)} cls="pierde" /> o.est)} cls="pierde" top={7} />

Detalle de contratos no renovados

Clic en el nombre abre la oportunidad en Dynamics 365

{lost.length === 0 ? (

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

) : (
{!scope.locked && } {[...lost].sort((a, b) => (b.est || 0) - (a.est || 0)).map((o) => ( {!scope.locked && } ))}
Cliente Oportunidad Motivo de cierre Línea de NegocioEjecutivoMRR est.
{o.cliente || "—"} {o.name} {o.motivo} {o.linea || "—"}{o.owner}{o.est ? money2(o.est) : "—"}
)}
); }