From a42cfb624740c6b08e6d1ab482c61530bbd5e437 Mon Sep 17 00:00:00 2001 From: Tyrannusss Date: Tue, 3 Mar 2026 17:25:25 -0600 Subject: [PATCH] se agregaron endpoints para ver el monto total por servico filtrando por el periodo de inicio y el periodo de termino --- .../detalle_servicio.controller.ts | 14 ++++++- .../detalle_servicio.service.ts | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/detalle_servicio/detalle_servicio.controller.ts b/src/detalle_servicio/detalle_servicio.controller.ts index 089ce3a..96a54c6 100644 --- a/src/detalle_servicio/detalle_servicio.controller.ts +++ b/src/detalle_servicio/detalle_servicio.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common'; import { DetalleServicioService } from './detalle_servicio.service'; import { FindReciboByRangeDto } from 'src/recibo/dto/create-recibo.dto'; import { JwtAuthGuard } from 'src/user/jwt.guard'; @@ -25,4 +25,16 @@ export class DetalleServicioController { return this.detalleServicioService.findByDateRange(data.desde, data.hasta); } + +@Get('totales-por-periodo') +async getTotalesPorPeriodo( + @Query('periodoInicio') periodoInicio?: string, + @Query('periodoFin') periodoFin?: string, + @Query('servicioId') servicioId?: string, +) { + const inicio = periodoInicio ? parseInt(periodoInicio) : undefined; + const fin = periodoFin ? parseInt(periodoFin) : undefined; + const servicio = servicioId ? parseInt(servicioId) : undefined; + return this.detalleServicioService.getTotalesPorServicioYPeriodo(inicio, fin, servicio); +} } diff --git a/src/detalle_servicio/detalle_servicio.service.ts b/src/detalle_servicio/detalle_servicio.service.ts index ac570ef..22efd68 100644 --- a/src/detalle_servicio/detalle_servicio.service.ts +++ b/src/detalle_servicio/detalle_servicio.service.ts @@ -35,5 +35,42 @@ export class DetalleServicioService { .addGroupBy('servicio.servicio') .getRawMany(); } + +async getTotalesPorServicioYPeriodo(periodoInicio?: number, periodoFin?: number, servicioId?: number) { + const query = this.detalleServicioRepository + .createQueryBuilder('ds') + .select('s.servicio', 'nombre_servicio') + .addSelect('p.semestre', 'periodo') + .addSelect('p.id_periodo', 'id_periodo') + .addSelect('SUM(ds.monto)', 'monto_total') + .innerJoin('ds.servicio', 's') + .innerJoin('ds.periodo', 'p'); + + if (periodoInicio && periodoFin) { + query.andWhere('p.id_periodo BETWEEN :inicio AND :fin', { inicio: periodoInicio, fin: periodoFin }); + } else if (periodoInicio) { + query.andWhere('p.id_periodo >= :inicio', { inicio: periodoInicio }); + } else if (periodoFin) { + query.andWhere('p.id_periodo <= :fin', { fin: periodoFin }); + } + + if (servicioId) { + query.andWhere('s.id_servicio = :servicioId', { servicioId }); + } + + const resultados = await query + .groupBy('s.id_servicio') + .addGroupBy('p.id_periodo') + .orderBy('p.id_periodo') + .addOrderBy('s.servicio') + .getRawMany(); + + return resultados.map(item => ({ + nombre_servicio: item.nombre_servicio, + periodo: item.periodo, + monto_total: parseFloat(item.monto_total) || 0, + })); +} + } //IO \ No newline at end of file