informe-comercial-alfa/app/components/plan/SinActividadPanel.js

115 lines
4.9 KiB
JavaScript
Raw Normal View History

"use client";
import { useMemo, useState } from "react";
import { money, registroUrl } from "../../lib/constants";
import AgendarBoton from "../actividades/AgendarBoton";
import { COD_RENOVACION, objetivoSugerido } from "../../lib/plan";
const fechaTxt = (iso) => iso.split("-").reverse().join("/");
// Huecos de planificación: oportunidades con cierre estimado en el período y sin actividad agendada,
// con un botón por tipo (Tarea / Cita / Llamada) que abre el formulario nuevo en Dynamics ya ligado a
// la oportunidad. Solo pantalla (no va en el PDF). Compartido por Plan del Día y Plan de Actividades.
const tipoDe = (o) => (COD_RENOVACION.has(o.tipoCodigo) ? "Renovación" : "Venta");
const pctDe = (o) => parseInt(o.forecast || "", 10) || 0;
const CLAVES = {
tipo: (o) => tipoDe(o),
valor: (o) => o.valor || 0,
forecast: (o) => pctDe(o),
};
// Encabezado ordenable: 1.er clic = descendente (↓ mayor primero), 2.º = ascendente, 3.º = vuelve al orden por prioridad.
function Th({ col, orden, setOrden, className, children }) {
const activo = orden.col === col;
const flecha = activo ? (orden.dir === "desc" ? " ▼" : " ▲") : " ↕";
const siguiente = () => setOrden(!activo ? { col, dir: "desc" } : orden.dir === "desc" ? { col, dir: "asc" } : { col: null, dir: "desc" });
return (
<th className={className} aria-sort={activo ? (orden.dir === "desc" ? "descending" : "ascending") : "none"}>
<button type="button" onClick={siguiente} style={{ all: "unset", cursor: "pointer", fontWeight: "inherit" }} title="Clic para ordenar de mayor a menor, de menor a mayor o volver al orden original">
{children}
<span style={{ opacity: activo ? 1 : 0.4 }}>{flecha}</span>
</button>
</th>
);
}
export default function SinActividadPanel({ filas, titulo, hint, vacio, nota }) {
const [orden, setOrden] = useState({ col: null, dir: "desc" });
const [tipo, setTipo] = useState("todos");
const visibles = useMemo(() => {
let r = tipo === "todos" ? filas : filas.filter((o) => tipoDe(o) === tipo);
if (orden.col) {
const k = CLAVES[orden.col];
const m = orden.dir === "desc" ? -1 : 1;
r = [...r].sort((a, b) => {
const x = k(a), y = k(b);
return (typeof x === "string" ? x.localeCompare(y, "es") : x - y) * m;
});
}
return r;
}, [filas, orden, tipo]);
return (
<section className="panel plan-sin no-print">
<h3>{titulo} ({tipo === "todos" ? filas.length : `${visibles.length} de ${filas.length}`})</h3>
<p className="hint">{hint}</p>
{nota && <p className="hint plan-sin-nota">{nota}</p>}
{filas.length > 0 && (
<p className="hint" style={{ display: "flex", gap: 8, alignItems: "center" }}>
<label htmlFor="sin-tipo">Mostrar tipo:</label>
<select id="sin-tipo" value={tipo} onChange={(e) => setTipo(e.target.value)}>
<option value="todos">Todos</option>
<option value="Venta">Venta</option>
<option value="Renovación">Renovación</option>
</select>
<span>· Clic en Tipo, Valor estimado o Forecast para ordenar</span>
</p>
)}
{filas.length ? (
<div className="ts" style={{ maxHeight: 460, overflowY: "auto" }}>
<table>
<thead>
<tr>
<th>Ejecutivo</th>
<th>Cliente</th>
<Th col="tipo" orden={orden} setOrden={setOrden}>Tipo</Th>
<Th col="valor" orden={orden} setOrden={setOrden} className="num">Valor estimado</Th>
<th>Cierre estimado</th>
<Th col="forecast" orden={orden} setOrden={setOrden}>Forecast</Th>
<th>Objetivo sugerido</th>
<th>Agendar</th>
</tr>
</thead>
<tbody>
{visibles.map((o) => (
<tr key={o.id}>
<td>{o.owner}</td>
<td>
<a className="opp" href={registroUrl("opportunity", o.id)} target="_blank" rel="noopener">
{o.cliente || o.nombre || "—"}
</a>
{o.prioridadAlta && <span className="plan-prio" title="Forecast 80% o 90%: lo más cercano al cierre">Prioridad</span>}
</td>
<td>{tipoDe(o)}</td>
<td className="num">{money(o.valor)}</td>
<td>{fechaTxt(o.cierre)}</td>
<td>{o.forecast || "—"}</td>
<td className="plan-sugerido" title="Guía según la etapa de forecast; al agendar, elige este objetivo en la actividad">
{objetivoSugerido(o) || "—"}
</td>
<td>
<AgendarBoton opp={o} />
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<p className="hint" style={{ margin: 0 }}>✓ {vacio}</p>
)}
</section>
);
}