import { Suspense } from "react"; import { contextoCampo } from "../../lib/campoPagina"; import { getPipeline, getRenovPipeline, getActividades, getClosedRange, VENTA_TYPE_FILTER, REN_TYPE_FILTER } from "../../lib/queries"; import { rangoDePeriodoPrevision } from "../../lib/periodos"; import { getMetaGlobal, getMetas } from "../../lib/metas"; import { actAncla } from "../../lib/derive"; import { localDeIso, minAHora } from "../../lib/agendador"; import LoginPrompt from "../../components/LoginPrompt"; import ErrorNotice from "../../components/ErrorNotice"; import OwnerM from "../components/OwnerM"; import TuberiaLista from "../components/TuberiaLista"; export const dynamic = "force-dynamic"; // Mismos criterios que /prevision («Cierran este mes»: tipos de previsión, sin plan D ni sin plan; forecast ponderado por el // plan de cierre) y /tuberia («Toda la tubería»). Interruptor Ventas ↔ Renovaciones: nunca se suman (la meta excluye renovaciones). const PREVIS_TIPOS = ["Venta - Cliente Nuevo", "Up-Selling", "Cross-Selling"]; const PREVIS_PLANES_EXCL = ["Sin plan de cierre", "Plan D - Recalificar"]; export default async function Tuberia({ searchParams }) { const sp = await searchParams; const c = await contextoCampo(sp); if (!c) return ; const ren = sp.t === "ren"; const rango = rangoDePeriodoPrevision("mes"); const mesIni = rango.from.slice(0, 7); let rows, acts, cerradas; try { [rows, acts, cerradas] = await Promise.all([ ren ? getRenovPipeline(c.session.accessToken, c.scope) : getPipeline(c.session.accessToken, c.scope), getActividades(c.session.accessToken, c.scope), getClosedRange(c.session.accessToken, c.scope, 1, rango.from, rango.to, undefined, ren ? REN_TYPE_FILTER : VENTA_TYPE_FILTER), ]); } catch (e) { return ; } const base = ren ? rows : rows.filter((o) => PREVIS_TIPOS.includes(o.tipo) && !PREVIS_PLANES_EXCL.includes(o.plan)); const delMes = base.filter((o) => o.close && o.close >= rango.from && o.close < rango.to); const logrado = cerradas.reduce((a, o) => a + (o.actual || 0), 0); const prevision = delMes.reduce((a, o) => a + o.fc, 0); const meta = ren ? null : c.unico ? getMetas()[c.unico] || null : getMetaGlobal() || null; // Próxima actividad abierta por oportunidad (hoy en adelante). const prox = new Map(); for (const a of acts) { if (a.estado !== "abierta" || a.referente?.tipo !== "opportunity") continue; const anc = actAncla(a); if (!anc || String(anc).slice(0, 10) < c.hoy) continue; const inst = a.tipo === "Tarea" ? a.scheduledend || a.scheduledstart : a.scheduledstart || a.scheduledend; if (!inst) continue; const l = localDeIso(inst); const ant = prox.get(a.referente.id); if (!ant || `${l.dia}${String(l.min).padStart(4, "0")}` < ant.k) prox.set(a.referente.id, { k: `${l.dia}${String(l.min).padStart(4, "0")}`, tipo: a.tipo, dia: l.dia, hora: a.tipo === "Tarea" ? null : minAHora(l.min) }); } const mapa = (o) => ({ id: o.id, cliente: o.cliente || o.name, nombre: o.name, mrr: o.est, pct: o.prevPct || 0, tipo: o.tipo, cierre: o.close, estanc: !!o.estanc, diasEstanc: o.dsa != null && o.dsa > 14 ? o.dsa : o.diasEnFase, prox: prox.get(o.id) || null, }); const ord = (a, b) => b.pct - a.pct || b.mrr - a.mrr; return ( <> {c.selector && } a + o.est, 0)} /> ); }