126 lines
5.6 KiB
JavaScript
126 lines
5.6 KiB
JavaScript
import { auth } from "../lib/auth";
|
|
import { ownerFilter } from "../lib/authz";
|
|
import { getPipeline, 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 MatrizCalidad from "../components/pipeline/MatrizCalidad";
|
|
import TablaPipe from "../components/pipeline/TablaPipe";
|
|
import ExecMatrix from "../components/pipeline/ExecMatrix";
|
|
import { money } from "../lib/constants";
|
|
import { EJECUTIVOS_VENTA, agg, hayClasif, cierreVencido } from "../lib/derive";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export default async function TuberiaPage({ searchParams }) {
|
|
const session = await auth();
|
|
if (!session?.email) return <LoginPrompt />;
|
|
|
|
const { owner } = await searchParams;
|
|
const scope = ownerFilter(session, owner);
|
|
|
|
let rows, allUsers;
|
|
try {
|
|
[rows, allUsers] = await Promise.all([
|
|
getPipeline(session.accessToken, scope),
|
|
session.isAdmin ? getSystemUsers(session.accessToken) : Promise.resolve([]),
|
|
]);
|
|
} catch (error) {
|
|
return <ErrorNotice error={error} />;
|
|
}
|
|
const owners = allUsers.filter((u) => EJECUTIVOS_VENTA.includes(u.name));
|
|
|
|
const sorted = [...rows].sort((a, b) => b.est - a.est);
|
|
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 estancadas = estanc.length;
|
|
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 enOfertaONegociacion = rows.filter((r) => r.faseActual === "Oferta" || r.faseActual === "Negociación").length;
|
|
|
|
return (
|
|
<div style={{ padding: 24 }}>
|
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12 }}>
|
|
<h2>Tubería Activa</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">{rows.length}</span>
|
|
<span className="l">Oportunidades activas</span>
|
|
</div>
|
|
<div className="kpi">
|
|
<span className="n">{money(totalEst)}</span>
|
|
<span className="l">Estimado total</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">{estancadas}</span>
|
|
<span className="l">Estancadas (>14d sin actividad o >30d en fase)</span>
|
|
</div>
|
|
<div className="kpi">
|
|
<span className="n pierde">{sinPlan.length}</span>
|
|
<span className="l">Sin plan de cierre · {money(sinPlanEst)}</span>
|
|
</div>
|
|
</div>
|
|
|
|
{sorted.length === 0 ? (
|
|
<p>No hay oportunidades activas para este filtro.</p>
|
|
) : (
|
|
<>
|
|
<div className="callout warn">
|
|
<b>¿Qué es estancamiento?</b> Una oportunidad está estancada si lleva <b>más de 14 días sin ninguna
|
|
actividad</b> (llamada/cita/correo) <b>o más de 30 días atascada en la misma fase</b>. Hoy hay{" "}
|
|
<b>{estancadas} de {rows.length}</b> así ({rows.length ? Math.round((estancadas / rows.length) * 100) : 0}%) —{" "}
|
|
{money(estancEst)} de MRR sin movimiento. <b>{vencidas.length}</b> ya pasaron su fecha estimada de cierre.
|
|
Y <b>{enOfertaONegociacion}</b> de {rows.length} están en Oferta o Negociación: el embudo está cargado al
|
|
final — casi no entra nada nuevo por arriba.
|
|
</div>
|
|
<ExecMatrix arr={rows} titulo="Tubería por ejecutivo" />
|
|
<div className="sec-div"><span>Indicadores y detalle</span></div>
|
|
<div className="split23">
|
|
<div className="split23-ind">
|
|
{hayClasif(rows) && <MatrizCalidad arr={rows} />}
|
|
<Panel
|
|
title="Luz Amarilla — qué la detiene"
|
|
hint="Bloqueador declarado por el ejecutivo"
|
|
rows={agg(rows, "luz", (o) => o.est)}
|
|
cls="alerta"
|
|
top={7}
|
|
/>
|
|
<Panel title="Por Tipo de Oportunidad" rows={agg(rows, "tipo", (o) => o.est)} />
|
|
<Panel title="Por Origen de Oportunidad" rows={agg(rows, "origen", (o) => o.est)} top={7} />
|
|
<Panel title="Por Competidor" rows={agg(rows, "competidor", (o) => o.est)} top={7} />
|
|
<Panel title="Por Línea de Negocio" rows={agg(rows, "linea", (o) => o.est)} />
|
|
<Panel title="Por Servicio / Producto" rows={agg(rows, "servicio", (o) => o.est)} top={7} />
|
|
</div>
|
|
<div>
|
|
<div className="panel" style={{ paddingBottom: 6 }}>
|
|
<h3>Detalle de la tubería activa</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 · <b>Actividad</b>: ● verde activa, ámbar tibia, naranja fría, rojo
|
|
congelada · ⚠ cierre vencido · clic en el nombre abre Dynamics
|
|
</p>
|
|
</div>
|
|
<TablaPipe arr={rows} tipo="est" />
|
|
</div>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|