74 lines
3.5 KiB
JavaScript
74 lines
3.5 KiB
JavaScript
|
|
// Vista del equipo (gerente): cálculo puro, sin Dynamics. Recibe lo ya leído y devuelve líneas por ejecutivo.
|
||
|
|
// Todo se mide SOLO con lo registrado en el CRM (el seguimiento por WhatsApp no entra): «sin actividad» significa
|
||
|
|
// «sin actividad registrada en el CRM». Definiciones (verificadas con Marco el 2026-11-10 y con el código de Plan/Previsión):
|
||
|
|
// - Planificada: actividad no cancelada cuya fecha (diaDe) cae en el periodo. Completada: estado «completada».
|
||
|
|
// - Vencida: sigue abierta y su fecha ya pasó.
|
||
|
|
// - Sin próxima actividad: oportunidad abierta sin ninguna actividad ABIERTA con fecha de hoy en adelante.
|
||
|
|
// - Cierre vencido: fecha de cierre estimada anterior a hoy y oportunidad aún abierta.
|
||
|
|
// - Actualizada en 7 días: `modifiedon` de la oportunidad dentro de los últimos 7 días (indicio: también cuenta un
|
||
|
|
// cambio hecho por un flujo automático).
|
||
|
|
import { diaDe, addDays } from "./plan";
|
||
|
|
import { gcDe } from "./gerencia";
|
||
|
|
|
||
|
|
const vacio = () => ({
|
||
|
|
plan: 0, hechas: 0, vencidas: 0, porDia: {},
|
||
|
|
gcParticipo: 0, gcParticipara: 0, gcPresencial: 0, gcVirtual: 0, // Gerencia Comercial en las actividades del periodo (virtual incluye llamadas)
|
||
|
|
abiertas: 0, cierreVencido: 0, estancadas: 0, sinProxima: 0, actualizadas7: 0, sinPlan: 0,
|
||
|
|
casos: [],
|
||
|
|
});
|
||
|
|
|
||
|
|
// opps: filas de getPipeline + getRenovPipeline (con owner, close, estanc, plan, modificada, est, id, name, cliente).
|
||
|
|
export function armarEquipo({ nombres, actividades, opps, hoy, desde, hasta }) {
|
||
|
|
const por = Object.fromEntries(nombres.map((n) => [n, vacio()]));
|
||
|
|
// Oportunidades con al menos una actividad abierta de hoy en adelante.
|
||
|
|
const conProxima = new Set();
|
||
|
|
for (const a of actividades) {
|
||
|
|
if (a.estado !== "abierta") continue;
|
||
|
|
const d = diaDe(a);
|
||
|
|
if (d && d >= hoy) {
|
||
|
|
const id = a.oportunidad?.id || a.referente?.id;
|
||
|
|
if (id) conProxima.add(id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for (const a of actividades) {
|
||
|
|
const L = por[a.owner];
|
||
|
|
if (!L || a.estado === "cancelada") continue;
|
||
|
|
const d = diaDe(a);
|
||
|
|
if (!d || d < desde || d > hasta) continue;
|
||
|
|
L.plan++;
|
||
|
|
L.porDia[d] = (L.porDia[d] || 0) + 1;
|
||
|
|
if (a.estado === "completada") L.hechas++;
|
||
|
|
else if (d < hoy) L.vencidas++;
|
||
|
|
const g = gcDe(a);
|
||
|
|
if (g) {
|
||
|
|
if (g.estado === "participo") L.gcParticipo++; else L.gcParticipara++;
|
||
|
|
if (g.modalidad === "presencial") L.gcPresencial++; else L.gcVirtual++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const hace7 = addDays(hoy, -7);
|
||
|
|
for (const o of opps) {
|
||
|
|
const L = por[o.owner];
|
||
|
|
if (!L) continue;
|
||
|
|
L.abiertas++;
|
||
|
|
const motivos = [];
|
||
|
|
if (o.close && o.close < hoy) { L.cierreVencido++; motivos.push("Cierre vencido"); }
|
||
|
|
if (o.estanc) L.estancadas++;
|
||
|
|
if (!conProxima.has(o.id)) { L.sinProxima++; motivos.push("Sin próxima actividad"); }
|
||
|
|
if (o.modificada && o.modificada >= hace7) L.actualizadas7++;
|
||
|
|
if (o.plan === "Sin plan de cierre") L.sinPlan++;
|
||
|
|
if (motivos.length) L.casos.push({ id: o.id, nombre: o.name, cliente: o.cliente, owner: o.owner, ownerId: o.ownerId, valor: o.est || 0, cierre: o.close, motivos, forecast: o.prev, tipoCodigo: null });
|
||
|
|
}
|
||
|
|
return por;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function totalEquipo(por) {
|
||
|
|
const t = vacio();
|
||
|
|
for (const L of Object.values(por)) {
|
||
|
|
for (const k of ["plan", "hechas", "vencidas", "gcParticipo", "gcParticipara", "gcPresencial", "gcVirtual", "abiertas", "cierreVencido", "estancadas", "sinProxima", "actualizadas7", "sinPlan"]) t[k] += L[k];
|
||
|
|
for (const [d, n] of Object.entries(L.porDia)) t.porDia[d] = (t.porDia[d] || 0) + n;
|
||
|
|
}
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
export const pct = (n, d) => (d ? `${Math.round((n / d) * 100)}%` : "—");
|