"use client"; import { useState, useMemo } from "react"; import { agg, aggOrd, ETAPA_ORDER, EJECUTIVOS_VENTA } from "../../lib/derive"; import { actividadUrl } from "../../lib/constants"; import Panel from "../pipeline/Panel"; const HORIZONTES = [ ["hoy", "Hoy"], ["semana", "Esta semana"], ["semana_pasada", "Semana pasada"], ["mes", "Este mes"], ["mes_pasado", "Mes pasado"], ["todas", "Todas"], ]; function inicioSemana(d) { const x = new Date(d); const dow = (x.getDay() + 6) % 7; x.setDate(x.getDate() - dow); x.setHours(0, 0, 0, 0); return x; } function enRango(fecha, horizonte) { if (horizonte === "todas") return true; if (!fecha) return false; const f = new Date(fecha); f.setHours(0, 0, 0, 0); const hoy = new Date(); hoy.setHours(0, 0, 0, 0); if (horizonte === "hoy") return f.getTime() === hoy.getTime(); if (horizonte === "semana" || horizonte === "semana_pasada") { const lunes = inicioSemana(hoy); if (horizonte === "semana_pasada") lunes.setDate(lunes.getDate() - 7); const domingo = new Date(lunes); domingo.setDate(lunes.getDate() + (horizonte === "semana" ? 6 : 6)); if (horizonte === "semana_pasada") domingo.setDate(lunes.getDate() + 6); return f >= lunes && f <= domingo; } if (horizonte === "mes" || horizonte === "mes_pasado") { const base = new Date(hoy.getFullYear(), hoy.getMonth() - (horizonte === "mes_pasado" ? 1 : 0), 1); return f.getFullYear() === base.getFullYear() && f.getMonth() === base.getMonth(); } return true; } function TablaTipo({ titulo, rows, locked }) { if (!rows.length) return null; return (

{titulo} — {rows.length}

{!locked && } {rows.map((a) => ( {!locked && } ))}
AsuntoReferenteObjetivoEjecutivoEstadoFecha
{a.asunto} {a.referente?.nombre || "—"} {a.objetivo || "—"}{a.owner} {a.estadoFinal} {a.scheduledendFmt || a.scheduledstartFmt || "—"}
); } export default function ActividadesClient({ actividades, locked }) { const [horizonte, setHorizonte] = useState("mes"); const filtradas = useMemo( () => actividades.filter((a) => enRango(a.fecha, horizonte)), [actividades, horizonte] ); const completadas = filtradas.filter((a) => a.estadoFinal === "Completada"); const vencidas = filtradas.filter((a) => a.estadoFinal === "Vencida"); const pendientes = filtradas.filter((a) => a.estadoFinal === "Pendiente"); const cumplimiento = filtradas.length ? Math.round((completadas.length / filtradas.length) * 100) : 0; const conObjetivo = filtradas.filter((a) => a.objetivo).length; const conOportunidad = filtradas.filter((a) => a.referente?.tipo === "opportunity"); const porEjecutivo = EJECUTIVOS_VENTA.map((nombre) => { const propias = filtradas.filter((a) => a.owner === nombre); const hechas = propias.filter((a) => a.estadoFinal === "Completada").length; const pct = propias.length ? Math.round((hechas / propias.length) * 100) : null; return { nombre, total: propias.length, hechas, pct }; }).sort((a, b) => (b.pct ?? -1) - (a.pct ?? -1)); // Oportunidades relacionadas — actividades que sí traen Ingreso Estimado/Tipo de Oportunidad // vía $expand, agrupadas por la oportunidad referida. const porOportunidad = new Map(); conOportunidad.forEach((a) => { const k = a.referente.id; if (!porOportunidad.has(k)) { porOportunidad.set(k, { nombre: a.referente.nombre, tipo: a.tipoOportunidad, est: a.ingresoEstimado || 0, n: 0 }); } porOportunidad.get(k).n++; }); const oportunidadesRelacionadas = [...porOportunidad.values()].sort((a, b) => b.n - a.n).slice(0, 12); return ( <>

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). Ventana de datos: últimos 90 días cerrados + todo lo que sigue abierto.

{HORIZONTES.map(([k, l]) => ( ))}
{filtradas.length}Actividades (sin canceladas)
{cumplimiento}%Cumplimiento (completadas/total)
{pendientes.length}Pendientes
{vencidas.length}Vencidas

Con Objetivo de Actividad asignado: {conObjetivo} ({filtradas.length ? Math.round((conObjetivo / filtradas.length) * 100) : 0}%) — adopción real del campo. Referidas a Oportunidad: {conOportunidad.length} ({filtradas.length ? Math.round((conOportunidad.length / filtradas.length) * 100) : 0}%).

Cumplimiento por ejecutivo

Completadas ÷ total de actividades (sin canceladas) de cada quien

{porEjecutivo.map((r) => (
{r.nombre}
= 70 ? "" : r.pct >= 50 ? "alerta" : "pierde"}`} style={{ width: `${Math.min(100, r.pct ?? 0)}%` }} />
{r.pct == null ? "n/d" : `${r.pct}%`} {r.hechas}/{r.total}
))}

Oportunidades relacionadas

Oportunidades con más actividades referidas en este período

{oportunidadesRelacionadas.length ? oportunidadesRelacionadas.map((o) => (
{o.nombre}
{o.n} act. {o.tipo || "—"}
)) :

Ninguna actividad de este período está referida a una Oportunidad.

}
Detalle
a.tipo === "Cita")} locked={locked} /> a.tipo === "Llamada")} locked={locked} /> a.tipo === "Tarea")} locked={locked} /> ); }