91 lines
3.6 KiB
JavaScript
91 lines
3.6 KiB
JavaScript
|
|
// Selector de período por fecha de cierre real (actualclosedate) — compartido por Resumen, Ganadas
|
||
|
|
// y Perdemos. Ventana [from, to) en "YYYY-MM-DD", lista para pasar a getClosedRange().
|
||
|
|
|
||
|
|
export const PERIODOS = [
|
||
|
|
{ k: "mes", lbl: "Este mes" },
|
||
|
|
{ k: "semana", lbl: "Esta semana" },
|
||
|
|
{ k: "mesAnt", lbl: "Mes anterior" },
|
||
|
|
{ k: "semanaAnt", lbl: "Semana anterior" },
|
||
|
|
];
|
||
|
|
|
||
|
|
export function periodoActivo(k) {
|
||
|
|
return PERIODOS.some((p) => p.k === k) ? k : "mes";
|
||
|
|
}
|
||
|
|
|
||
|
|
function isoDate(d) {
|
||
|
|
return d.toISOString().slice(0, 10);
|
||
|
|
}
|
||
|
|
function addDays(d, n) {
|
||
|
|
const r = new Date(d);
|
||
|
|
r.setDate(r.getDate() + n);
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
// Lunes de la semana de `d` — semana comercial Lun-Dom.
|
||
|
|
function mondayOf(d) {
|
||
|
|
const r = new Date(d);
|
||
|
|
const day = r.getDay(); // 0=Dom, 1=Lun, ...
|
||
|
|
r.setDate(r.getDate() + (day === 0 ? -6 : 1 - day));
|
||
|
|
r.setHours(0, 0, 0, 0);
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
// mensual: true si el período es un mes calendario completo — la meta (metas.json) es mensual, así
|
||
|
|
// que el cumplimiento de meta solo es comparable en esos períodos, no en semana/semana anterior.
|
||
|
|
export function rangoDePeriodo(periodo) {
|
||
|
|
const hoy = new Date();
|
||
|
|
if (periodo === "semana") {
|
||
|
|
const ini = mondayOf(hoy);
|
||
|
|
return { from: isoDate(ini), to: isoDate(addDays(ini, 7)), lbl: "esta semana", mensual: false };
|
||
|
|
}
|
||
|
|
if (periodo === "semanaAnt") {
|
||
|
|
const iniEsta = mondayOf(hoy);
|
||
|
|
const ini = addDays(iniEsta, -7);
|
||
|
|
return { from: isoDate(ini), to: isoDate(iniEsta), lbl: "la semana anterior", mensual: false };
|
||
|
|
}
|
||
|
|
if (periodo === "mesAnt") {
|
||
|
|
const iniEste = new Date(hoy.getFullYear(), hoy.getMonth(), 1);
|
||
|
|
const ini = new Date(hoy.getFullYear(), hoy.getMonth() - 1, 1);
|
||
|
|
return { from: isoDate(ini), to: isoDate(iniEste), lbl: "el mes anterior", mensual: true };
|
||
|
|
}
|
||
|
|
const ini = new Date(hoy.getFullYear(), hoy.getMonth(), 1);
|
||
|
|
const fin = new Date(hoy.getFullYear(), hoy.getMonth() + 1, 1);
|
||
|
|
return { from: isoDate(ini), to: isoDate(fin), lbl: "este mes", mensual: true };
|
||
|
|
}
|
||
|
|
|
||
|
|
// Selector de período para Previsión de Ventas — a diferencia de rangoDePeriodo() (hacia atrás,
|
||
|
|
// sobre actualclosedate), este mira hacia adelante sobre estimatedclosedate ("Atrasadas" queda
|
||
|
|
// aparte, calculado directo en prevision/page.js). [from, to) — to exclusivo; finIncl (to - 1 día)
|
||
|
|
// es lo que espera decorarActividadVentana() (derive.js), que usa límites inclusivos.
|
||
|
|
export const PERIODOS_PREVISION = [
|
||
|
|
{ k: "mes", lbl: "Este mes" },
|
||
|
|
{ k: "semana", lbl: "Esta semana" },
|
||
|
|
{ k: "mesProx", lbl: "Próximo mes" },
|
||
|
|
{ k: "semanaProx", lbl: "Próxima semana" },
|
||
|
|
];
|
||
|
|
|
||
|
|
export function periodoPrevisionActivo(k) {
|
||
|
|
return PERIODOS_PREVISION.some((p) => p.k === k) ? k : "mes";
|
||
|
|
}
|
||
|
|
|
||
|
|
export function rangoDePeriodoPrevision(periodo) {
|
||
|
|
const hoy = new Date();
|
||
|
|
if (periodo === "semana") {
|
||
|
|
const ini = mondayOf(hoy);
|
||
|
|
const to = addDays(ini, 7);
|
||
|
|
return { from: isoDate(ini), to: isoDate(to), finIncl: isoDate(addDays(to, -1)), lbl: "esta semana" };
|
||
|
|
}
|
||
|
|
if (periodo === "semanaProx") {
|
||
|
|
const ini = addDays(mondayOf(hoy), 7);
|
||
|
|
const to = addDays(ini, 7);
|
||
|
|
return { from: isoDate(ini), to: isoDate(to), finIncl: isoDate(addDays(to, -1)), lbl: "la próxima semana" };
|
||
|
|
}
|
||
|
|
if (periodo === "mesProx") {
|
||
|
|
const ini = new Date(hoy.getFullYear(), hoy.getMonth() + 1, 1);
|
||
|
|
const to = new Date(hoy.getFullYear(), hoy.getMonth() + 2, 1);
|
||
|
|
return { from: isoDate(ini), to: isoDate(to), finIncl: isoDate(addDays(to, -1)), lbl: "el próximo mes" };
|
||
|
|
}
|
||
|
|
const ini = new Date(hoy.getFullYear(), hoy.getMonth(), 1);
|
||
|
|
const to = new Date(hoy.getFullYear(), hoy.getMonth() + 1, 1);
|
||
|
|
return { from: isoDate(ini), to: isoDate(to), finIncl: isoDate(addDays(to, -1)), lbl: "este mes" };
|
||
|
|
}
|