139 lines
7 KiB
JavaScript
139 lines
7 KiB
JavaScript
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 { money, 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 RenovLogradasPage({ searchParams }) {
|
|
const session = await auth();
|
|
if (!session?.email) return <LoginPrompt />;
|
|
|
|
const { owner } = await searchParams;
|
|
const scope = ownerFilter(session, owner);
|
|
const mes = currentMonth();
|
|
|
|
let wonRaw, lost, allUsers;
|
|
try {
|
|
[wonRaw, lost, allUsers] = await Promise.all([
|
|
getRenovClosed(session.accessToken, scope, 1, mes),
|
|
getRenovClosed(session.accessToken, scope, 2, mes),
|
|
getSystemUsers(session.accessToken),
|
|
]);
|
|
} catch (error) {
|
|
return <ErrorNotice error={error} />;
|
|
}
|
|
const owners = allUsers.filter((u) => EJECUTIVOS_RENOV.includes(u.name));
|
|
const won = wonRaw.map((o) => ({ ...o, val: o.actual }));
|
|
|
|
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 (
|
|
<div style={{ padding: 24 }}>
|
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12 }}>
|
|
<h2>Renovaciones Logradas — {mes}</h2>
|
|
<OwnerControl owners={owners} current={scope.locked ? [session.email] : scope.emails} locked={scope.locked} lockedLabel={session.email} />
|
|
</div>
|
|
|
|
<div className="kpis">
|
|
<div className="kpi"><span className="n">{money2(sumW)}</span><span className="l">MRR renovado</span></div>
|
|
<div className="kpi"><span className="n">{won.length}</span><span className="l">Renovaciones logradas</span></div>
|
|
<div className="kpi">
|
|
<span className={`n ${ret != null && ret < 80 ? "pierde" : ""}`}>{ret == null ? "n/d" : `${ret}%`}</span>
|
|
<span className="l">Tasa de retención (ganadas ÷ cerradas)</span>
|
|
</div>
|
|
<div className="kpi"><span className="n pierde">{money2(sumL)}</span><span className="l">MRR perdido por no renovar</span></div>
|
|
<div className="kpi"><span className="n">{money2(won.length ? sumW / won.length : 0)}</span><span className="l">Ticket promedio renovado</span></div>
|
|
<div className="kpi">
|
|
<span className={`n ${pctATiempo < 50 ? "pierde" : ""}`}>{pctATiempo}%</span>
|
|
<span className="l">Renovadas ANTES de vencer</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={`callout ${ret != null && ret < 80 ? "warn" : "info"}`}>
|
|
<b>De cada 100 contratos que se cerraron este mes, se conservaron {ret == null ? "—" : ret}.</b> 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.
|
|
</div>
|
|
{pctATiempo < 50 && (
|
|
<div className="callout warn">
|
|
<b>Solo {pctATiempo}% se renovó antes de vencer</b> ({aTiempo} de {won.length}). Las otras se firmaron con
|
|
el contrato ya vencido — funciona, pero es la ventana donde el cliente está más expuesto a que lo llame la
|
|
competencia. Adelantar la gestión al tipo «Próximo a Vencer 30/60/90» es la palanca directa sobre este
|
|
número.
|
|
</div>
|
|
)}
|
|
|
|
{retPorEjec.length > 0 && (
|
|
<div className="panel">
|
|
<h3>Retención por ejecutivo</h3>
|
|
<p className="hint">Ganadas vs. perdidas de cada quien, en MRR</p>
|
|
{retPorEjec.map((r) => {
|
|
const max = Math.max(...retPorEjec.flatMap((x) => [x.gana, x.pierde]), 0.1);
|
|
return (
|
|
<div key={r.k} style={{ padding: "7px 4px", borderBottom: "1px solid var(--linea)" }}>
|
|
<div style={{ fontSize: 12.5, marginBottom: 5 }}>{r.k}</div>
|
|
<div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 3 }}>
|
|
<span style={{ fontSize: 10, color: "var(--gana)", width: 52 }}>Gana</span>
|
|
<div className="bar-cell"><div className="bar-fill" style={{ width: `${(r.gana / max) * 100}%` }} /></div>
|
|
<span className="v">{money2(r.gana)}</span>
|
|
</div>
|
|
<div style={{ display: "flex", alignItems: "center", gap: 8 }}>
|
|
<span style={{ fontSize: 10, color: "var(--pierde)", width: 52 }}>Pierde</span>
|
|
<div className="bar-cell"><div className="bar-fill pierde" style={{ width: `${(r.pierde / max) * 100}%` }} /></div>
|
|
<span className="v">{money2(r.pierde)}</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
<div className="grid3">
|
|
<Panel title="Por momento de la renovación" hint="A tiempo vs. tarde" rows={agg(won, "tipo", (o) => o.val)} />
|
|
<Panel title="Por Ejecutivo" hint="Ordenado por MRR renovado" rows={agg(won, "owner", (o) => o.val)} />
|
|
<Panel title="Por Línea de Negocio" hint="Ordenado por MRR renovado" rows={agg(won, "linea", (o) => o.val)} />
|
|
<Panel title="Por Servicio / Producto" hint="Ordenado por MRR renovado" rows={agg(won, "servicio", (o) => o.val)} top={7} />
|
|
<Panel title="Por Origen" hint="Canal registrado de la oportunidad" rows={agg(won, "origen", (o) => o.val)} top={7} />
|
|
<Panel title="Por Cliente" hint="Las cuentas que más MRR renovaron" rows={agg(won, "cliente", (o) => o.val)} top={7} />
|
|
</div>
|
|
|
|
<div className="panel" style={{ paddingBottom: 6 }}>
|
|
<h3>Detalle de renovaciones logradas</h3>
|
|
<p className="hint">Clic en el nombre abre la oportunidad en Dynamics 365</p>
|
|
</div>
|
|
{won.length === 0 ? <p>No hay renovaciones logradas en este período para este filtro.</p> : <WonTable rows={won} locked={scope.locked} />}
|
|
</div>
|
|
);
|
|
}
|