import Link from "next/link"; import { auth } from "../lib/auth"; import { ownerFilter } from "../lib/authz"; import { getPipeline, getClosed, VENTA_TYPE_FILTER } from "../lib/queries"; import { PLAN_ORDER } from "../lib/derive"; import LoginPrompt from "../components/LoginPrompt"; import ErrorNotice from "../components/ErrorNotice"; import PlanFilterSelect from "../components/PlanFilterSelect"; import { DYN, ACT_COLOR, 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")}`; } const SETS = [ { key: "pipe", label: "Tubería activa" }, { key: "ganadas", label: "Ganadas" }, { key: "perdidas", label: "Perdidas" }, ]; export default async function DetallePage({ searchParams }) { const session = await auth(); if (!session?.email) return ; const { owner, set, plan } = await searchParams; const scope = ownerFilter(session, owner); const detSet = SETS.some((s) => s.key === set) ? set : "pipe"; const mes = currentMonth(); let rows; try { if (detSet === "pipe") { rows = await getPipeline(session.accessToken, scope); } else { const statecode = detSet === "ganadas" ? 1 : 2; rows = await getClosed(session.accessToken, scope, statecode, mes, undefined, VENTA_TYPE_FILTER); } } catch (error) { return ; } // Cerradas: la fase final del embudo es el resultado (Ganada / Perdida). if (detSet !== "pipe") rows = rows.map((o) => ({ ...o, faseActual: detSet === "ganadas" ? "Ganada" : "Perdida" })); const planesDisponibles = detSet === "pipe" ? PLAN_ORDER.filter((p) => rows.some((o) => (o.plan || "Sin plan de cierre") === p)) : []; const filtrados = detSet === "pipe" && plan ? rows.filter((o) => (o.plan || "Sin plan de cierre") === plan) : rows; const sorted = [...filtrados].sort((a, b) => (b.est ?? b.actual ?? 0) - (a.est ?? a.actual ?? 0)); const qs = (key) => { const p = new URLSearchParams(); if (owner) p.set("owner", owner); p.set("set", key); return `/detalle?${p.toString()}`; }; return (

Detalle

{SETS.map((s) => ( ))}
{detSet === "pipe" && planesDisponibles.length > 0 && (
)}

{sorted.length} oportunidades{detSet !== "pipe" ? ` (${mes})` : ""}

{sorted.length === 0 ? (

No hay oportunidades para este filtro.

) : (
{!scope.locked && } {detSet === "pipe" ? ( <> ) : ( <> )} {sorted.map((o) => ( {!scope.locked && } {detSet === "pipe" ? ( <> ) : ( <> )} ))}
Cliente Oportunidad Seg.EjecutivoPlan cierre Fase actual Actividad MRR est. ForecastTipo MRR Cierre
{o.cliente || "—"} {o.name} {o.segmento === "Cartera Activa" ? "CA" : "CN"} {o.owner}{(o.plan || "").replace(" - ", " ")} {o.faseActual || "—"} ● {o.actividad} {money2(o.est)} {money2(o.fc)}{o.tipo} {money2(o.actual)} {o.actualClose || "—"}
)}
); }