- Nuevo endpoint GET /reportes/estado-resultados-detallado con cálculo contable:
* Ventas, Devoluciones, Ventas netas, Costo de ventas, Utilidad bruta,
Gastos operativos, Utilidad de la operación
* Fórmula: subtotal_mxn - descuento_mxn (sin impuestos), nómina usa total_mxn
* Excluye anticipos (uso_cfdi=P01 o clave_prod_serv=84111506)
* Filtro por régimen fiscal opcional
* Año anterior calculado automáticamente
- Nuevo endpoint GET /reportes/estado-resultados/drill-down:
* Nivel 1: resumen agrupado por RFC
* Nivel 2: CFDIs individuales filtrados por categoría
* Categorías: ventas, devoluciones, costo-ventas, gastos-operativos
- Nuevo endpoint GET /reportes/estado-resultados/export:
* Genera Excel con formato condicional (verde/rojo, negritas)
- Frontend:
* Tabla vertical con % vertical, año anterior y variación %
* Filas clickeables para drill-down modal de 2 niveles
* Top 10 Clientes/Proveedores mantenidos debajo
* Selector de régimen conectado al reporte
- Fix: NaN en total de drill-down nivel 2 por numeric como string en pg
* Agregado ::float en queries SQL de CFDIs individuales
157 lines
5.9 KiB
TypeScript
157 lines
5.9 KiB
TypeScript
import type { Request, Response, NextFunction } from 'express';
|
|
import * as reportesService from '../services/reportes.service.js';
|
|
import { exportEstadoResultadosToExcel } from '../services/reportes.service.js';
|
|
|
|
export async function getEstadoResultados(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, contribuyenteId } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getEstadoResultados(req.tenantPool!, inicio, fin, req.user!.tenantId, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
console.error('[reportes] Error en getEstadoResultados:', error);
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getFlujoEfectivo(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, contribuyenteId } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getFlujoEfectivo(req.tenantPool!, inicio, fin, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getComparativo(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { contribuyenteId } = req.query;
|
|
const año = parseInt(req.query.año as string) || new Date().getFullYear();
|
|
const data = await reportesService.getComparativo(req.tenantPool!, año, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getCuentasXPagar(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, regimen, contribuyenteId } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getCuentasXPagar(req.tenantPool!, inicio, fin, regimen as string, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getCuentasXCobrar(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, regimen, contribuyenteId } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getCuentasXCobrar(req.tenantPool!, inicio, fin, regimen as string, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getConcentradoRfc(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, tipo, contribuyenteId } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
const tipoRfc = (tipo as 'cliente' | 'proveedor') || 'cliente';
|
|
|
|
const data = await reportesService.getConcentradoRfc(req.tenantPool!, inicio, fin, tipoRfc, contribuyenteId as string | undefined || null);
|
|
res.json(data);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getEstadoResultadosDetallado(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, contribuyenteId, regimen } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getEstadoResultadosDetallado(
|
|
req.tenantPool!,
|
|
inicio,
|
|
fin,
|
|
req.user!.tenantId,
|
|
contribuyenteId as string | undefined || null,
|
|
regimen as string | undefined || null,
|
|
);
|
|
res.json(data);
|
|
} catch (error) {
|
|
console.error('[reportes] Error en getEstadoResultadosDetallado:', error);
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function getEstadoResultadosDrillDown(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { categoria, fechaInicio, fechaFin, contribuyenteId, regimen, rfc } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const data = await reportesService.getEstadoResultadosDrillDown(
|
|
req.tenantPool!,
|
|
categoria as string,
|
|
inicio,
|
|
fin,
|
|
contribuyenteId as string | undefined || null,
|
|
regimen as string | undefined || null,
|
|
rfc as string | undefined || null,
|
|
);
|
|
res.json(data);
|
|
} catch (error) {
|
|
console.error('[reportes] Error en getEstadoResultadosDrillDown:', error);
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
export async function exportEstadoResultados(req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
const { fechaInicio, fechaFin, contribuyenteId, regimen } = req.query;
|
|
const now = new Date();
|
|
const inicio = (fechaInicio as string) || `${now.getFullYear()}-01-01`;
|
|
const fin = (fechaFin as string) || now.toISOString().split('T')[0];
|
|
|
|
const buffer = await exportEstadoResultadosToExcel(
|
|
req.tenantPool!,
|
|
inicio,
|
|
fin,
|
|
req.user!.tenantId,
|
|
contribuyenteId as string | undefined || null,
|
|
regimen as string | undefined || null,
|
|
);
|
|
|
|
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
res.setHeader('Content-Disposition', `attachment; filename="estado-resultados-${inicio}-${fin}.xlsx"`);
|
|
res.send(buffer);
|
|
} catch (error) {
|
|
console.error('[reportes] Error en exportEstadoResultados:', error);
|
|
next(error);
|
|
}
|
|
}
|