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