se agregaron endpoints para ver el monto total por servico filtrando por el periodo de inicio y el periodo de termino

This commit is contained in:
2026-03-03 17:25:25 -06:00
parent c4c14f8520
commit a42cfb6247
2 changed files with 50 additions and 1 deletions
@@ -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);
}
}
@@ -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