| | | 1 | | using FAU.API.Security; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using FAU.Logica.Services; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/liquidacion/reportes-control")] |
| | | 12 | | [Authorize] |
| | | 13 | | public class ReporteControlController : ControllerBase |
| | | 14 | | { |
| | | 15 | | private readonly IReporteControlService _service; |
| | | 16 | | private readonly IPeriodoService _periodoService; |
| | | 17 | | private readonly IAuditoriaService _auditoriaService; |
| | | 18 | | private readonly ICurrentUserContext _currentUser; |
| | | 19 | | private readonly ILogger<ReporteControlController> _logger; |
| | | 20 | | |
| | 27 | 21 | | public ReporteControlController( |
| | 27 | 22 | | IReporteControlService service, |
| | 27 | 23 | | IPeriodoService periodoService, |
| | 27 | 24 | | IAuditoriaService auditoriaService, |
| | 27 | 25 | | ICurrentUserContext currentUser, |
| | 27 | 26 | | ILogger<ReporteControlController> logger) |
| | | 27 | | { |
| | 27 | 28 | | _service = service; |
| | 27 | 29 | | _periodoService = periodoService; |
| | 27 | 30 | | _auditoriaService = auditoriaService; |
| | 27 | 31 | | _currentUser = currentUser; |
| | 27 | 32 | | _logger = logger; |
| | 27 | 33 | | } |
| | | 34 | | |
| | | 35 | | // ─── Cuadro de Efectivos Superior ─────────────────────────────────────── |
| | | 36 | | |
| | | 37 | | [HttpGet("{periodoId}/efectivos-superior")] |
| | | 38 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 39 | | public async Task<IActionResult> GetEfectivosSuperior( |
| | | 40 | | long periodoId, |
| | | 41 | | [FromQuery] string? programa, |
| | | 42 | | [FromQuery] string? regimen) |
| | | 43 | | { |
| | | 44 | | try |
| | | 45 | | { |
| | 3 | 46 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 47 | | if (periodo == null) |
| | 1 | 48 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 49 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 50 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 51 | | |
| | 1 | 52 | | var result = await _service.ObtenerEfectivosSuperiorAsync(periodoId, programa, regimen); |
| | 1 | 53 | | return Ok(ApiResponse<CuadroEfectivosDto>.SuccessResult(result)); |
| | | 54 | | } |
| | 0 | 55 | | catch (Exception ex) |
| | | 56 | | { |
| | 0 | 57 | | _logger.LogError(ex, "Error al obtener efectivos superior del período {PeriodoId}", periodoId); |
| | 0 | 58 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 59 | | } |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | [HttpGet("{periodoId}/efectivos-superior/csv")] |
| | | 63 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 64 | | public async Task<IActionResult> GetEfectivosSuperiorCsv( |
| | | 65 | | long periodoId, |
| | | 66 | | [FromQuery] string? programa, |
| | | 67 | | [FromQuery] string? regimen) |
| | | 68 | | { |
| | | 69 | | try |
| | | 70 | | { |
| | 3 | 71 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 72 | | if (periodo == null) |
| | 1 | 73 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 74 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 75 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 76 | | |
| | 1 | 77 | | var csv = await _service.GenerarCsvEfectivosSuperiorAsync(periodoId, programa, regimen); |
| | 1 | 78 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteEfectivosSuperior, periodoId); |
| | 1 | 79 | | return File(csv, "text/csv", $"efectivos_superior_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 80 | | } |
| | 0 | 81 | | catch (Exception ex) |
| | | 82 | | { |
| | 0 | 83 | | _logger.LogError(ex, "Error al generar CSV efectivos superior del período {PeriodoId}", periodoId); |
| | 0 | 84 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 85 | | } |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | // ─── Cuadro de Efectivos Subalterno ───────────────────────────────────── |
| | | 89 | | |
| | | 90 | | [HttpGet("{periodoId}/efectivos-subalterno")] |
| | | 91 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 92 | | public async Task<IActionResult> GetEfectivosSubalterno( |
| | | 93 | | long periodoId, |
| | | 94 | | [FromQuery] string? programa, |
| | | 95 | | [FromQuery] string? regimen) |
| | | 96 | | { |
| | | 97 | | try |
| | | 98 | | { |
| | 3 | 99 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 100 | | if (periodo == null) |
| | 1 | 101 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 102 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 103 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 104 | | |
| | 1 | 105 | | var result = await _service.ObtenerEfectivosSubalternoAsync(periodoId, programa, regimen); |
| | 1 | 106 | | return Ok(ApiResponse<CuadroEfectivosSubalternoDto>.SuccessResult(result)); |
| | | 107 | | } |
| | 0 | 108 | | catch (Exception ex) |
| | | 109 | | { |
| | 0 | 110 | | _logger.LogError(ex, "Error al obtener efectivos subalterno del período {PeriodoId}", periodoId); |
| | 0 | 111 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 112 | | } |
| | 3 | 113 | | } |
| | | 114 | | |
| | | 115 | | [HttpGet("{periodoId}/efectivos-subalterno/csv")] |
| | | 116 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 117 | | public async Task<IActionResult> GetEfectivosSubalternoCsv( |
| | | 118 | | long periodoId, |
| | | 119 | | [FromQuery] string? programa, |
| | | 120 | | [FromQuery] string? regimen) |
| | | 121 | | { |
| | | 122 | | try |
| | | 123 | | { |
| | 3 | 124 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 125 | | if (periodo == null) |
| | 1 | 126 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 127 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 128 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 129 | | |
| | 1 | 130 | | var csv = await _service.GenerarCsvEfectivosSubalternoAsync(periodoId, programa, regimen); |
| | 1 | 131 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteEfectivosSubalterno, periodoId); |
| | 1 | 132 | | return File(csv, "text/csv", $"efectivos_subalterno_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 133 | | } |
| | 0 | 134 | | catch (Exception ex) |
| | | 135 | | { |
| | 0 | 136 | | _logger.LogError(ex, "Error al generar CSV efectivos subalterno del período {PeriodoId}", periodoId); |
| | 0 | 137 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 138 | | } |
| | 3 | 139 | | } |
| | | 140 | | |
| | | 141 | | // ─── Listín de Contralor ───────────────────────────────────────────────── |
| | | 142 | | |
| | | 143 | | [HttpGet("{periodoId}/listin")] |
| | | 144 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 145 | | public async Task<IActionResult> GetListin( |
| | | 146 | | long periodoId, |
| | | 147 | | [FromQuery] int? situacionDesde, |
| | | 148 | | [FromQuery] int? situacionHasta) |
| | | 149 | | { |
| | | 150 | | try |
| | | 151 | | { |
| | 3 | 152 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 153 | | if (periodo == null) |
| | 1 | 154 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 155 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 156 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 157 | | |
| | 1 | 158 | | var result = await _service.ObtenerListinAsync(periodoId, situacionDesde, situacionHasta); |
| | 1 | 159 | | return Ok(ApiResponse<ListinContralorDto>.SuccessResult(result)); |
| | | 160 | | } |
| | 0 | 161 | | catch (Exception ex) |
| | | 162 | | { |
| | 0 | 163 | | _logger.LogError(ex, "Error al obtener listín del período {PeriodoId}", periodoId); |
| | 0 | 164 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 165 | | } |
| | 3 | 166 | | } |
| | | 167 | | |
| | | 168 | | [HttpGet("{periodoId}/listin/csv")] |
| | | 169 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 170 | | public async Task<IActionResult> GetListinCsv( |
| | | 171 | | long periodoId, |
| | | 172 | | [FromQuery] int? situacionDesde, |
| | | 173 | | [FromQuery] int? situacionHasta) |
| | | 174 | | { |
| | | 175 | | try |
| | | 176 | | { |
| | 3 | 177 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 178 | | if (periodo == null) |
| | 1 | 179 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 180 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 181 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 182 | | |
| | 1 | 183 | | var csv = await _service.GenerarCsvListinAsync(periodoId, situacionDesde, situacionHasta); |
| | 1 | 184 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteListin, periodoId); |
| | 1 | 185 | | return File(csv, "text/csv", $"listin_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 186 | | } |
| | 0 | 187 | | catch (Exception ex) |
| | | 188 | | { |
| | 0 | 189 | | _logger.LogError(ex, "Error al generar CSV listín del período {PeriodoId}", periodoId); |
| | 0 | 190 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 191 | | } |
| | 3 | 192 | | } |
| | | 193 | | |
| | | 194 | | // ─── Comparativo SGH ───────────────────────────────────────────────────── |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Compara el padrón SGH enviado en el body contra los funcionarios activos del sistema. |
| | | 198 | | /// POST /api/liquidacion/reportes-control/{periodoId}/sgh/comparativo |
| | | 199 | | /// </summary> |
| | | 200 | | [HttpPost("{periodoId}/sgh/comparativo")] |
| | | 201 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 202 | | public async Task<IActionResult> PostComparativoSgh( |
| | | 203 | | long periodoId, |
| | | 204 | | [FromBody] List<PadronSghEntradaDto> filas) |
| | | 205 | | { |
| | | 206 | | try |
| | | 207 | | { |
| | 4 | 208 | | if (filas == null || filas.Count == 0) |
| | 1 | 209 | | return BadRequest(ApiResponse<string>.ErrorResult("El padrón SGH no puede estar vacío")); |
| | | 210 | | |
| | 3 | 211 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 212 | | if (periodo == null) |
| | 1 | 213 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 214 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 215 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 216 | | |
| | 1 | 217 | | var result = await _service.ObtenerComparativoSghAsync(periodoId, filas); |
| | 1 | 218 | | return Ok(ApiResponse<ComparativoSghDto>.SuccessResult(result)); |
| | | 219 | | } |
| | 0 | 220 | | catch (Exception ex) |
| | | 221 | | { |
| | 0 | 222 | | _logger.LogError(ex, "Error al obtener comparativo SGH del período {PeriodoId}", periodoId); |
| | 0 | 223 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 224 | | } |
| | 4 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Descarga el comparativo SGH como CSV. |
| | | 229 | | /// POST /api/liquidacion/reportes-control/{periodoId}/sgh/comparativo/csv |
| | | 230 | | /// </summary> |
| | | 231 | | [HttpPost("{periodoId}/sgh/comparativo/csv")] |
| | | 232 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 233 | | public async Task<IActionResult> PostComparativoSghCsv( |
| | | 234 | | long periodoId, |
| | | 235 | | [FromBody] List<PadronSghEntradaDto> filas) |
| | | 236 | | { |
| | | 237 | | try |
| | | 238 | | { |
| | 4 | 239 | | if (filas == null || filas.Count == 0) |
| | 1 | 240 | | return BadRequest(ApiResponse<string>.ErrorResult("El padrón SGH no puede estar vacío")); |
| | | 241 | | |
| | 3 | 242 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 243 | | if (periodo == null) |
| | 1 | 244 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 245 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 246 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 247 | | |
| | 1 | 248 | | var csv = await _service.GenerarCsvComparativoSghAsync(periodoId, filas); |
| | 1 | 249 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteComparativoSgh, periodoId); |
| | 1 | 250 | | return File(csv, "text/csv", $"comparativo_sgh_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 251 | | } |
| | 0 | 252 | | catch (Exception ex) |
| | | 253 | | { |
| | 0 | 254 | | _logger.LogError(ex, "Error al generar CSV comparativo SGH del período {PeriodoId}", periodoId); |
| | 0 | 255 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 256 | | } |
| | 4 | 257 | | } |
| | | 258 | | |
| | | 259 | | // ─── Helper ────────────────────────────────────────────────────────────── |
| | | 260 | | |
| | | 261 | | private Task RegistrarAuditoriaAsync(AccionEnum accion, long periodoId) => |
| | 4 | 262 | | _auditoriaService.LogAuditoriaAsync( |
| | 4 | 263 | | _currentUser.UserId ?? 0, |
| | 4 | 264 | | accion, |
| | 4 | 265 | | ContextoEnum.Liquidaciones, |
| | 4 | 266 | | _currentUser.Host, |
| | 4 | 267 | | entidad: "ReporteControl", |
| | 4 | 268 | | entidadId: periodoId, |
| | 4 | 269 | | detalle: new { periodoId }); |
| | | 270 | | } |