146 lines
6.9 KiB
JavaScript
146 lines
6.9 KiB
JavaScript
|
|
import { auth } from "../../lib/auth";
|
||
|
|
import { ownerFilter } from "../../lib/authz";
|
||
|
|
import { getRenovPipeline, getSystemUsers } from "../../lib/queries";
|
||
|
|
import LoginPrompt from "../../components/LoginPrompt";
|
||
|
|
import OwnerControl from "../../components/OwnerControl";
|
||
|
|
import ErrorNotice from "../../components/ErrorNotice";
|
||
|
|
import Panel from "../../components/pipeline/Panel";
|
||
|
|
import TablaPipe from "../../components/pipeline/TablaPipe";
|
||
|
|
import ExecMatrix from "../../components/pipeline/ExecMatrix";
|
||
|
|
import SegTabs from "../../components/SegTabs";
|
||
|
|
import FilterChips from "../../components/FilterChips";
|
||
|
|
import { money } from "../../lib/constants";
|
||
|
|
import { EJECUTIVOS_RENOV, agg, cierreVencido } from "../../lib/derive";
|
||
|
|
import { parseFiltParam, applyFilt } from "../../lib/filterState";
|
||
|
|
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
const IND_TABS = [
|
||
|
|
{ k: "owner", lbl: "Ejecutivo" },
|
||
|
|
{ k: "cliente", lbl: "Cliente" },
|
||
|
|
{ k: "linea", lbl: "Línea" },
|
||
|
|
{ k: "competidor", lbl: "Competidor" },
|
||
|
|
];
|
||
|
|
const IND_TITULOS = {
|
||
|
|
owner: "Por Ejecutivo",
|
||
|
|
cliente: "Por Cliente",
|
||
|
|
linea: "Por Línea de Negocio",
|
||
|
|
competidor: "Por Competidor",
|
||
|
|
};
|
||
|
|
const IND_HINTS = {
|
||
|
|
owner: "Ordenado por MRR en juego",
|
||
|
|
cliente: "Las cuentas con más MRR por renovar",
|
||
|
|
linea: "Ordenado por MRR en juego",
|
||
|
|
competidor: "Contra quién se compite la renovación",
|
||
|
|
};
|
||
|
|
const IND_TOP = { owner: undefined, cliente: 7, linea: undefined, competidor: 7 };
|
||
|
|
const DIM_LABELS = { owner: "Ejecutivo", cliente: "Cliente", linea: "Línea", competidor: "Competidor", tipo: "Momento" };
|
||
|
|
|
||
|
|
export default async function RenovTuberiaPage({ searchParams }) {
|
||
|
|
const session = await auth();
|
||
|
|
if (!session?.email) return <LoginPrompt />;
|
||
|
|
|
||
|
|
const sp = await searchParams;
|
||
|
|
const { owner } = sp;
|
||
|
|
const scope = ownerFilter(session, owner);
|
||
|
|
const filt = parseFiltParam(sp.f);
|
||
|
|
const indActive = IND_TABS.some((t) => t.k === sp.ind) ? sp.ind : "owner";
|
||
|
|
const mze = sp.mze;
|
||
|
|
|
||
|
|
let allRows, allUsers;
|
||
|
|
try {
|
||
|
|
[allRows, allUsers] = await Promise.all([
|
||
|
|
getRenovPipeline(session.accessToken, scope),
|
||
|
|
session.isAdmin ? getSystemUsers(session.accessToken) : Promise.resolve([]),
|
||
|
|
]);
|
||
|
|
} catch (error) {
|
||
|
|
return <ErrorNotice error={error} />;
|
||
|
|
}
|
||
|
|
const owners = allUsers.filter((u) => EJECUTIVOS_RENOV.includes(u.name));
|
||
|
|
const rows = applyFilt(allRows, filt);
|
||
|
|
|
||
|
|
const totalEst = rows.reduce((a, r) => a + r.est, 0);
|
||
|
|
const totalFc = rows.reduce((a, r) => a + r.fc, 0);
|
||
|
|
const estanc = rows.filter((r) => r.estanc);
|
||
|
|
const estancEst = estanc.reduce((a, r) => a + r.est, 0);
|
||
|
|
const sinPlan = rows.filter((r) => r.plan === "Sin plan de cierre");
|
||
|
|
const sinPlanEst = sinPlan.reduce((a, r) => a + r.est, 0);
|
||
|
|
const vencidas = rows.filter(cierreVencido);
|
||
|
|
const yaVencido = rows.filter((r) => r.tipo === "Contrato vencido");
|
||
|
|
const pctSinPlan = rows.length ? Math.round((sinPlan.length / rows.length) * 100) : 0;
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div style={{ padding: 24 }}>
|
||
|
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12 }}>
|
||
|
|
<h2>Tubería de Renovaciones</h2>
|
||
|
|
<OwnerControl owners={owners} current={scope.locked ? session.email : scope.email} locked={scope.locked} lockedLabel={session.email} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="kpis">
|
||
|
|
<div className="kpi"><span className="n">{money(totalEst)}</span><span className="l">MRR en juego (est.)</span></div>
|
||
|
|
<div className="kpi"><span className="n">{rows.length}</span><span className="l">Renovaciones abiertas</span></div>
|
||
|
|
<div className="kpi"><span className="n">{money(totalFc)}</span><span className="l">Forecast ponderado</span></div>
|
||
|
|
<div className="kpi"><span className="n pierde">{yaVencido.length}</span><span className="l">Con el contrato YA vencido · {money(yaVencido.reduce((a, o) => a + o.est, 0))}</span></div>
|
||
|
|
<div className="kpi"><span className="n pierde">{estanc.length}</span><span className="l">Estancadas · {money(estancEst)}</span></div>
|
||
|
|
<div className="kpi"><span className="n pierde">{sinPlan.length}</span><span className="l">Sin plan de cierre · {money(sinPlanEst)}</span></div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{rows.length === 0 ? (
|
||
|
|
<p>No hay renovaciones activas para este filtro.</p>
|
||
|
|
) : (
|
||
|
|
<>
|
||
|
|
<div className="callout warn">
|
||
|
|
<b>Esto no es pipeline nuevo: es MRR que ya está facturando y hay que no perder.</b> Hay <b>{money(totalEst)}</b> en{" "}
|
||
|
|
{rows.length} renovaciones abiertas, de las cuales <b>{yaVencido.length} ya tienen el contrato vencido</b> —
|
||
|
|
ahí el cliente está sin amarre y expuesto a que lo llame la competencia. <b>{estanc.length} de {rows.length}</b>{" "}
|
||
|
|
están estancadas (+14 días sin actividad o +30 en la misma fase) por {money(estancEst)}, y{" "}
|
||
|
|
<b>{vencidas.length}</b> pasaron su fecha estimada de cierre.
|
||
|
|
</div>
|
||
|
|
{pctSinPlan > 50 && (
|
||
|
|
<div className="callout warn">
|
||
|
|
<b>{pctSinPlan}% de la tubería de renovaciones no tiene Plan de Cierre</b> ({sinPlan.length} de{" "}
|
||
|
|
{rows.length}, {money(sinPlanEst)}). Sin plan, el forecast ponderado les asigna apenas 10% y el número
|
||
|
|
de abajo queda subestimado — pero el problema de fondo no es el cálculo: es que {sinPlan.length}{" "}
|
||
|
|
contratos que vencen no tienen una fecha de firma comprometida.
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
<ExecMatrix arr={rows} titulo="Renovaciones por ejecutivo" hint="Incluye Ventas y SAC" activeDim={mze} />
|
||
|
|
<div className="sec-div"><span>Indicadores y detalle</span></div>
|
||
|
|
<FilterChips dimLabels={DIM_LABELS} />
|
||
|
|
<div className="split23">
|
||
|
|
<div className="split23-ind">
|
||
|
|
<Panel
|
||
|
|
title="Por momento de la renovación"
|
||
|
|
hint="Ya vencido vs. próximo a vencer"
|
||
|
|
rows={agg(rows, "tipo", (o) => o.est)}
|
||
|
|
cls="alerta"
|
||
|
|
dimKey="tipo"
|
||
|
|
/>
|
||
|
|
<div className="panel" style={{ paddingBottom: 4 }}>
|
||
|
|
<SegTabs tabs={IND_TABS} activeKey={indActive} paramName="ind" />
|
||
|
|
</div>
|
||
|
|
<Panel
|
||
|
|
title={IND_TITULOS[indActive]}
|
||
|
|
hint={IND_HINTS[indActive]}
|
||
|
|
rows={agg(rows, indActive, (o) => o.est)}
|
||
|
|
top={IND_TOP[indActive]}
|
||
|
|
dimKey={indActive}
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
<div>
|
||
|
|
<div className="panel" style={{ paddingBottom: 6 }}>
|
||
|
|
<h3>Detalle de la tubería de renovaciones</h3>
|
||
|
|
<p className="hint">
|
||
|
|
Scroll horizontal con la columna Cliente/Oportunidad fija · <b>Plan</b>: A en firma · B negociación
|
||
|
|
· C seguimiento · D recalificar · ⚠ cierre vencido · clic en el nombre abre Dynamics
|
||
|
|
</p>
|
||
|
|
</div>
|
||
|
|
<TablaPipe arr={rows} tipo="est" />
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|