ya se pueden eliminar las filas de las plantillas
This commit is contained in:
@@ -2,13 +2,13 @@ import { Router } from 'express';
|
||||
import { db } from '../db.js';
|
||||
import { authRequired } from '../middleware/auth.js';
|
||||
import {
|
||||
inRange,
|
||||
sumIngresos,
|
||||
sumGastos,
|
||||
sumPersonal,
|
||||
weekKey,
|
||||
monthKey,
|
||||
parseFecha,
|
||||
filterRowsByDate,
|
||||
} from '../utils/calculations.js';
|
||||
|
||||
const router = Router();
|
||||
@@ -21,8 +21,60 @@ function loadRows(sheetType) {
|
||||
.map((r) => JSON.parse(r.data));
|
||||
}
|
||||
|
||||
function filterByDate(rows, fechaField, desde, hasta) {
|
||||
return rows.filter((r) => inRange(r[fechaField], desde, hasta));
|
||||
function countSinFecha(rows, fechaField) {
|
||||
return rows.filter((r) => !r[fechaField] || String(r[fechaField]).trim() === '').length;
|
||||
}
|
||||
|
||||
function resolvePeriod(tipo, desde, hasta) {
|
||||
const today = new Date();
|
||||
const todayStr = today.toISOString().slice(0, 10);
|
||||
let d = desde || null;
|
||||
let h = hasta || null;
|
||||
|
||||
if (tipo === 'todos') {
|
||||
return { desde: null, hasta: null, label: 'Todo el historial (sin filtro de fecha)' };
|
||||
}
|
||||
|
||||
if (tipo === 'diario') {
|
||||
const f = h || d || todayStr;
|
||||
return { desde: f, hasta: f, label: `Día ${f}` };
|
||||
}
|
||||
|
||||
if (tipo === 'semanal') {
|
||||
const ref = parseFecha(h || d || todayStr);
|
||||
const day = ref.getDay();
|
||||
const diff = day === 0 ? -6 : 1 - day;
|
||||
const monday = new Date(ref);
|
||||
monday.setDate(ref.getDate() + diff);
|
||||
const sunday = new Date(monday);
|
||||
sunday.setDate(monday.getDate() + 6);
|
||||
d = monday.toISOString().slice(0, 10);
|
||||
h = sunday.toISOString().slice(0, 10);
|
||||
return { desde: d, hasta: h, label: `Semana ${d} al ${h}` };
|
||||
}
|
||||
|
||||
if (tipo === 'mensual' && !d && !h) {
|
||||
d = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-01`;
|
||||
const last = new Date(today.getFullYear(), today.getMonth() + 1, 0);
|
||||
h = last.toISOString().slice(0, 10);
|
||||
return {
|
||||
desde: d,
|
||||
hasta: h,
|
||||
label: `Mes ${d.slice(0, 7)} (${d} al ${h})`,
|
||||
};
|
||||
}
|
||||
|
||||
if (d && h) {
|
||||
return { desde: d, hasta: h, label: `Personalizado ${d} al ${h}` };
|
||||
}
|
||||
if (d) {
|
||||
return { desde: d, hasta: null, label: `Desde ${d}` };
|
||||
}
|
||||
if (h) {
|
||||
return { desde: null, hasta: h, label: `Hasta ${h}` };
|
||||
}
|
||||
|
||||
return { desde: null, hasta: null, label: 'Sin filtro de fecha' };
|
||||
}
|
||||
|
||||
function buildReport(desde, hasta, include = {}) {
|
||||
@@ -35,20 +87,22 @@ function buildReport(desde, hasta, include = {}) {
|
||||
personal: include.personal !== false,
|
||||
};
|
||||
|
||||
const ventas = inc.ventas ? filterByDate(loadRows('ventas'), 'fecha', desde, hasta) : [];
|
||||
const servicios = inc.servicios ? filterByDate(loadRows('servicios'), 'fecha', desde, hasta) : [];
|
||||
const alquileres = inc.alquileres ? filterByDate(loadRows('alquileres'), 'fecha', desde, hasta) : [];
|
||||
const gastosDiarios = inc.gastos_diarios
|
||||
? filterByDate(loadRows('gastos_diarios'), 'fecha', desde, hasta)
|
||||
: [];
|
||||
const gastosFijos = inc.gastos_fijos
|
||||
? filterByDate(loadRows('gastos_fijos'), 'fecha', desde, hasta)
|
||||
: [];
|
||||
const ventasAll = inc.ventas ? loadRows('ventas') : [];
|
||||
const serviciosAll = inc.servicios ? loadRows('servicios') : [];
|
||||
const alquileresAll = inc.alquileres ? loadRows('alquileres') : [];
|
||||
const gastosDiariosAll = inc.gastos_diarios ? loadRows('gastos_diarios') : [];
|
||||
const gastosFijosAll = inc.gastos_fijos ? loadRows('gastos_fijos') : [];
|
||||
|
||||
const ventas = filterRowsByDate(ventasAll, 'fecha', desde, hasta);
|
||||
const servicios = filterRowsByDate(serviciosAll, 'fecha', desde, hasta);
|
||||
const alquileres = filterRowsByDate(alquileresAll, 'fecha', desde, hasta);
|
||||
const gastosDiarios = filterRowsByDate(gastosDiariosAll, 'fecha', desde, hasta);
|
||||
const gastosFijos = filterRowsByDate(gastosFijosAll, 'fecha', desde, hasta);
|
||||
|
||||
const personalRows = inc.personal ? loadRows('personal') : [];
|
||||
const mensualizados = personalRows.filter((r) => r.tipo === 'mensualizado');
|
||||
const eventuales = inc.personal
|
||||
? filterByDate(
|
||||
? filterRowsByDate(
|
||||
personalRows.filter((r) => r.tipo === 'eventual'),
|
||||
'fecha',
|
||||
desde,
|
||||
@@ -71,21 +125,40 @@ function buildReport(desde, hasta, include = {}) {
|
||||
);
|
||||
const gastosVariablesTotal = sumGastos(gastosVariables);
|
||||
const totalGastos = gastosDiariosTotal + gastosFijosTotal;
|
||||
|
||||
const totalPersonal = sumPersonal(mensualizados, eventuales);
|
||||
|
||||
const resultadoNeto = totalIngresos - totalGastos - totalPersonal;
|
||||
|
||||
return {
|
||||
periodo: { desde: desde || null, hasta: hasta || null },
|
||||
periodo: { desde, hasta },
|
||||
desglose: {
|
||||
ventas: tVentas,
|
||||
servicios: tServicios,
|
||||
alquileres: tAlquileres,
|
||||
ventas: { ...tVentas, filas: ventas.length },
|
||||
servicios: { ...tServicios, filas: servicios.length },
|
||||
alquileres: { ...tAlquileres, filas: alquileres.length },
|
||||
gastosDiarios: { total: gastosDiariosTotal, filas: gastosDiarios.length },
|
||||
gastosFijos: { total: gastosFijosTotal, filas: gastosFijos.length },
|
||||
gastosVariables: { total: gastosVariablesTotal },
|
||||
personal: { total: totalPersonal, mensualizados: mensualizados.length, eventuales: eventuales.length },
|
||||
personal: {
|
||||
total: totalPersonal,
|
||||
mensualizados: mensualizados.length,
|
||||
eventuales: eventuales.length,
|
||||
},
|
||||
},
|
||||
meta: {
|
||||
totalRegistros: {
|
||||
ventas: ventasAll.length,
|
||||
servicios: serviciosAll.length,
|
||||
alquileres: alquileresAll.length,
|
||||
gastos_diarios: gastosDiariosAll.length,
|
||||
gastos_fijos: gastosFijosAll.length,
|
||||
personal: personalRows.length,
|
||||
},
|
||||
sinFecha: {
|
||||
ventas: countSinFecha(ventasAll, 'fecha'),
|
||||
servicios: countSinFecha(serviciosAll, 'fecha'),
|
||||
alquileres: countSinFecha(alquileresAll, 'fecha'),
|
||||
gastos_diarios: countSinFecha(gastosDiariosAll, 'fecha'),
|
||||
gastos_fijos: countSinFecha(gastosFijosAll, 'fecha'),
|
||||
},
|
||||
},
|
||||
totales: {
|
||||
totalIngresos,
|
||||
@@ -100,29 +173,6 @@ function buildReport(desde, hasta, include = {}) {
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
const { desde, hasta, tipo = 'mensual', include } = req.query;
|
||||
let d = desde;
|
||||
let h = hasta;
|
||||
|
||||
const today = new Date();
|
||||
if (tipo === 'diario') {
|
||||
const f = (h || d || today.toISOString().slice(0, 10));
|
||||
d = f;
|
||||
h = f;
|
||||
} else if (tipo === 'semanal') {
|
||||
const ref = parseFecha(h || d || today.toISOString().slice(0, 10));
|
||||
const day = ref.getDay();
|
||||
const diff = day === 0 ? -6 : 1 - day;
|
||||
const monday = new Date(ref);
|
||||
monday.setDate(ref.getDate() + diff);
|
||||
const sunday = new Date(monday);
|
||||
sunday.setDate(monday.getDate() + 6);
|
||||
d = monday.toISOString().slice(0, 10);
|
||||
h = sunday.toISOString().slice(0, 10);
|
||||
} else if (tipo === 'mensual' && !d && !h) {
|
||||
d = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-01`;
|
||||
const last = new Date(today.getFullYear(), today.getMonth() + 1, 0);
|
||||
h = last.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
let includeObj = {};
|
||||
if (include) {
|
||||
@@ -133,14 +183,17 @@ router.get('/', (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const report = buildReport(d, h, includeObj);
|
||||
const period = resolvePeriod(tipo, desde, hasta);
|
||||
const report = buildReport(period.desde, period.hasta, includeObj);
|
||||
report.periodoLabel = period.label;
|
||||
|
||||
if (tipo === 'semanal' || tipo === 'mensual') {
|
||||
const gastosDiariosAll = loadRows('gastos_diarios');
|
||||
const gastosEnPeriodo = filterRowsByDate(gastosDiariosAll, 'fecha', period.desde, period.hasta);
|
||||
const weekly = {};
|
||||
for (const g of gastosDiariosAll) {
|
||||
const wk = weekKey(g.fecha);
|
||||
if (!inRange(g.fecha, d, h)) continue;
|
||||
for (const g of gastosEnPeriodo) {
|
||||
const ref = g.fecha || period.desde || period.hasta;
|
||||
const wk = weekKey(ref);
|
||||
weekly[wk] = (weekly[wk] || 0) + (Number(g.monto) || 0);
|
||||
}
|
||||
report.cierreSemanal = weekly;
|
||||
@@ -148,12 +201,12 @@ router.get('/', (req, res) => {
|
||||
|
||||
if (tipo === 'mensual') {
|
||||
const monthly = {};
|
||||
for (const g of gastosDiariosAll) {
|
||||
const mk = monthKey(g.fecha);
|
||||
if (!inRange(g.fecha, d, h)) continue;
|
||||
for (const g of gastosEnPeriodo) {
|
||||
const ref = g.fecha || period.desde || period.hasta;
|
||||
const mk = monthKey(ref);
|
||||
monthly[mk] = (monthly[mk] || 0) + (Number(g.monto) || 0);
|
||||
}
|
||||
const fijosAll = filterByDate(loadRows('gastos_fijos'), 'fecha', d, h);
|
||||
const fijosAll = filterRowsByDate(loadRows('gastos_fijos'), 'fecha', period.desde, period.hasta);
|
||||
report.cierreMensual = {
|
||||
gastosDiariosPorMes: monthly,
|
||||
gastosFijosEnPeriodo: sumGastos(fijosAll),
|
||||
|
||||
Reference in New Issue
Block a user