124 lines
4.8 KiB
JavaScript
124 lines
4.8 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 { DYN, 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")}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
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 won, lost, allUsers;
|
||
|
|
try {
|
||
|
|
[won, 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 sumW = won.reduce((a, o) => a + (o.actual || 0), 0);
|
||
|
|
// No hay cumplimiento de meta acá — las renovaciones se excluyen a propósito de la meta comercial
|
||
|
|
// (ver CLAUDE.md): el indicador que importa es qué tanto se retiene, no cuánto se vendió.
|
||
|
|
const retencion = won.length + lost.length ? Math.round((won.length / (won.length + lost.length)) * 100) : null;
|
||
|
|
|
||
|
|
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.email} locked={scope.locked} lockedLabel={session.email} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<p className="hint" style={{ marginBottom: 8 }}>
|
||
|
|
Renovar no cuenta para el cumplimiento de meta (que mide venta nueva y expansión) — pero mueve más plata:
|
||
|
|
ene-sep 2026, $75,314 de renovaciones contra $28,058 de venta y expansión. El indicador acá es retención, no meta.
|
||
|
|
</p>
|
||
|
|
|
||
|
|
<div className="kpis">
|
||
|
|
<div className="kpi">
|
||
|
|
<span className="n">{money2(sumW)}</span>
|
||
|
|
<span className="l">Suma renovada (MRR)</span>
|
||
|
|
</div>
|
||
|
|
<div className="kpi">
|
||
|
|
<span className="n">{won.length}</span>
|
||
|
|
<span className="l">Renovaciones logradas</span>
|
||
|
|
</div>
|
||
|
|
<div className="kpi">
|
||
|
|
<span className={retencion != null && retencion < 70 ? "n pierde" : "n"}>
|
||
|
|
{retencion != null ? `${retencion}%` : "n/d"}
|
||
|
|
</span>
|
||
|
|
<span className="l">Tasa de retención (ganadas/total)</span>
|
||
|
|
</div>
|
||
|
|
<div className="kpi">
|
||
|
|
<span className="n pierde">{lost.length}</span>
|
||
|
|
<span className="l">Perdidas en el mismo período</span>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="grid2">
|
||
|
|
<Panel title="Por Ejecutivo" rows={agg(won, "owner", (o) => o.actual)} />
|
||
|
|
<Panel title="Por Línea de Negocio" rows={agg(won, "linea", (o) => o.actual)} />
|
||
|
|
</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>
|
||
|
|
) : (
|
||
|
|
<div className="tw">
|
||
|
|
<div className="ts">
|
||
|
|
<table>
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th>Cliente</th>
|
||
|
|
<th>Oportunidad</th>
|
||
|
|
{!scope.locked && <th>Ejecutivo</th>}
|
||
|
|
<th>Tipo</th>
|
||
|
|
<th>Línea de Negocio</th>
|
||
|
|
<th className="num">MRR</th>
|
||
|
|
<th>Cierre</th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
{[...won].sort((a, b) => b.actual - a.actual).map((o) => (
|
||
|
|
<tr key={o.id}>
|
||
|
|
<td>{o.cliente || "—"}</td>
|
||
|
|
<td>
|
||
|
|
<a className="opp" href={`${DYN}${o.id}`} target="_blank" rel="noopener">{o.name}</a>
|
||
|
|
</td>
|
||
|
|
{!scope.locked && <td>{o.owner}</td>}
|
||
|
|
<td>{o.tipo}</td>
|
||
|
|
<td>{o.linea}</td>
|
||
|
|
<td className="num">{money2(o.actual)}</td>
|
||
|
|
<td>{o.actualClose || "—"}</td>
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|