114 lines
4.3 KiB
JavaScript
114 lines
4.3 KiB
JavaScript
|
|
import { auth } from "../../lib/auth";
|
||
|
|
import { ownerFilter } from "../../lib/authz";
|
||
|
|
import { getRenovClosed, getMotivosCierre, 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 RenovPerdidasPage({ searchParams }) {
|
||
|
|
const session = await auth();
|
||
|
|
if (!session?.email) return <LoginPrompt />;
|
||
|
|
|
||
|
|
const { owner } = await searchParams;
|
||
|
|
const scope = ownerFilter(session, owner);
|
||
|
|
const mes = currentMonth();
|
||
|
|
|
||
|
|
let lost, allUsers;
|
||
|
|
try {
|
||
|
|
[lost, allUsers] = await Promise.all([
|
||
|
|
getRenovClosed(session.accessToken, scope, 2, 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 }));
|
||
|
|
} catch (error) {
|
||
|
|
return <ErrorNotice error={error} />;
|
||
|
|
}
|
||
|
|
const owners = allUsers.filter((u) => EJECUTIVOS_RENOV.includes(u.name));
|
||
|
|
|
||
|
|
const mrrPerdido = lost.reduce((a, o) => a + (o.est || 0), 0);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div style={{ padding: 24 }}>
|
||
|
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12 }}>
|
||
|
|
<h2>Renovaciones Perdidas — {mes}</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 pierde">{lost.length}</span>
|
||
|
|
<span className="l">Renovaciones perdidas</span>
|
||
|
|
</div>
|
||
|
|
<div className="kpi">
|
||
|
|
<span className="n pierde">{money(mrrPerdido)}</span>
|
||
|
|
<span className="l">MRR estimado perdido</span>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<p className="hint">
|
||
|
|
No hay seguimiento registrado en el CRM ≠ no hubo seguimiento — la mayoría del contacto comercial se hace por
|
||
|
|
WhatsApp, que no está integrado a Dynamics (ver CLAUDE.md).
|
||
|
|
</p>
|
||
|
|
|
||
|
|
<div className="grid2">
|
||
|
|
<Panel
|
||
|
|
title="Motivo de cierre"
|
||
|
|
hint='Fuente: Cierre de oportunidad en Dynamics · "Sin registrar" = ningún motivo quedó guardado'
|
||
|
|
rows={agg(lost, "motivo", (o) => o.est)}
|
||
|
|
/>
|
||
|
|
<Panel title="Por Ejecutivo" rows={agg(lost, "owner", (o) => o.est)} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="panel" style={{ paddingBottom: 6 }}>
|
||
|
|
<h3>Detalle de renovaciones perdidas</h3>
|
||
|
|
<p className="hint">Clic en el nombre abre la oportunidad en Dynamics 365</p>
|
||
|
|
</div>
|
||
|
|
{lost.length === 0 ? (
|
||
|
|
<p>No hay renovaciones perdidas en este período para este filtro.</p>
|
||
|
|
) : (
|
||
|
|
<div className="tw">
|
||
|
|
<div className="ts">
|
||
|
|
<table>
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th>Cliente</th>
|
||
|
|
<th>Oportunidad</th>
|
||
|
|
<th>Motivo de cierre</th>
|
||
|
|
<th>Línea de Negocio</th>
|
||
|
|
{!scope.locked && <th>Ejecutivo</th>}
|
||
|
|
<th className="num">MRR est.</th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
{[...lost].sort((a, b) => (b.est || 0) - (a.est || 0)).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>
|
||
|
|
<td>{o.motivo}</td>
|
||
|
|
<td>{o.linea || "—"}</td>
|
||
|
|
{!scope.locked && <td>{o.owner}</td>}
|
||
|
|
<td className="num">{o.est ? money2(o.est) : "—"}</td>
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|