| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | [ApiController] |
| | | 10 | | [Route("api/[controller]")] |
| | | 11 | | [Authorize] |
| | | 12 | | public class AguinaldosController : ControllerBase |
| | | 13 | | { |
| | | 14 | | private readonly IAguinaldoSemestralService _service; |
| | | 15 | | private readonly IAguinaldoBajaService _bajaService; |
| | | 16 | | private readonly IAguinaldoHistoricoService _historicoService; |
| | | 17 | | private readonly IAguinaldoReciboService _reciboService; |
| | | 18 | | private readonly IAguinaldoPadronService _padronService; |
| | | 19 | | private readonly IAguinaldoReporteControlService _reporteControlService; |
| | | 20 | | private readonly ILogger<AguinaldosController> _logger; |
| | | 21 | | |
| | 39 | 22 | | public AguinaldosController( |
| | 39 | 23 | | IAguinaldoSemestralService service, |
| | 39 | 24 | | IAguinaldoBajaService bajaService, |
| | 39 | 25 | | IAguinaldoHistoricoService historicoService, |
| | 39 | 26 | | IAguinaldoReciboService reciboService, |
| | 39 | 27 | | IAguinaldoPadronService padronService, |
| | 39 | 28 | | IAguinaldoReporteControlService reporteControlService, |
| | 39 | 29 | | ILogger<AguinaldosController> logger) |
| | | 30 | | { |
| | 39 | 31 | | _service = service; |
| | 39 | 32 | | _bajaService = bajaService; |
| | 39 | 33 | | _historicoService = historicoService; |
| | 39 | 34 | | _reciboService = reciboService; |
| | 39 | 35 | | _padronService = padronService; |
| | 39 | 36 | | _reporteControlService = reporteControlService; |
| | 39 | 37 | | _logger = logger; |
| | 39 | 38 | | } |
| | | 39 | | |
| | | 40 | | [HttpPost("validar")] |
| | | 41 | | public async Task<IActionResult> Validar([FromBody] CalcularAguinaldoSemestralRequest request) |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 2 | 45 | | var resultado = await _service.ValidarHistoricoAsync(request); |
| | 1 | 46 | | return Ok(ApiResponse<AguinaldoValidacionDto>.SuccessResult(resultado)); |
| | | 47 | | } |
| | 1 | 48 | | catch (Exception ex) |
| | | 49 | | { |
| | 1 | 50 | | _logger.LogError(ex, "Error validando histórico de aguinaldo"); |
| | 1 | 51 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 52 | | } |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | [HttpPost("calcular")] |
| | | 56 | | public async Task<IActionResult> Calcular([FromBody] CalcularAguinaldoSemestralRequest request) |
| | | 57 | | { |
| | | 58 | | try |
| | | 59 | | { |
| | 3 | 60 | | var (success, resultados, error) = await _service.CalcularAguinaldoSemestralAsync(request); |
| | 2 | 61 | | if (!success) |
| | | 62 | | { |
| | 1 | 63 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error en cálculo")); |
| | | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | return Ok(ApiResponse<IEnumerable<AguinaldoSemestralDto>>.SuccessResult(resultados)); |
| | | 67 | | } |
| | 1 | 68 | | catch (Exception ex) |
| | | 69 | | { |
| | 1 | 70 | | _logger.LogError(ex, "Error calculando aguinaldo semestral"); |
| | 1 | 71 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 72 | | } |
| | 3 | 73 | | } |
| | | 74 | | |
| | | 75 | | [HttpPost("validar/baja")] |
| | | 76 | | public async Task<IActionResult> ValidarBaja([FromBody] CalcularAguinaldoBajaRequest request) |
| | | 77 | | { |
| | | 78 | | try |
| | | 79 | | { |
| | 2 | 80 | | var resultado = await _bajaService.ValidarHistoricoAsync(request); |
| | 1 | 81 | | return Ok(ApiResponse<AguinaldoValidacionDto>.SuccessResult(resultado)); |
| | | 82 | | } |
| | 1 | 83 | | catch (Exception ex) |
| | | 84 | | { |
| | 1 | 85 | | _logger.LogError(ex, "Error validando histórico de aguinaldo por baja"); |
| | 1 | 86 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 87 | | } |
| | 2 | 88 | | } |
| | | 89 | | |
| | | 90 | | [HttpPost("calcular/baja")] |
| | | 91 | | public async Task<IActionResult> CalcularBaja([FromBody] CalcularAguinaldoBajaRequest request) |
| | | 92 | | { |
| | | 93 | | try |
| | | 94 | | { |
| | 3 | 95 | | var (success, resultados, error) = await _bajaService.CalcularAguinaldoBajaAsync(request); |
| | 2 | 96 | | if (!success) |
| | | 97 | | { |
| | 1 | 98 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error en cálculo de aguinaldo por baja")); |
| | | 99 | | } |
| | | 100 | | |
| | 1 | 101 | | return Ok(ApiResponse<IEnumerable<AguinaldoBajaDto>>.SuccessResult(resultados)); |
| | | 102 | | } |
| | 1 | 103 | | catch (Exception ex) |
| | | 104 | | { |
| | 1 | 105 | | _logger.LogError(ex, "Error calculando aguinaldo por baja"); |
| | 1 | 106 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 107 | | } |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | [HttpPost("completar-historico")] |
| | | 111 | | public async Task<IActionResult> CompletarHistorico([FromBody] CompletarHistoricoAguinaldoRequest request) |
| | | 112 | | { |
| | | 113 | | try |
| | | 114 | | { |
| | 3 | 115 | | if (request.Funcionarios == null || !request.Funcionarios.Any()) |
| | 1 | 116 | | return BadRequest(ApiResponse<string>.ErrorResult("La lista de funcionarios no puede estar vacía.")); |
| | | 117 | | |
| | 2 | 118 | | var resultado = await _historicoService.CompletarHistoricoAsync(request); |
| | 1 | 119 | | return Ok(ApiResponse<CompletarHistoricoAguinaldoResultadoDto>.SuccessResult(resultado)); |
| | | 120 | | } |
| | 1 | 121 | | catch (Exception ex) |
| | | 122 | | { |
| | 1 | 123 | | _logger.LogError(ex, "Error completando histórico de aguinaldo"); |
| | 1 | 124 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 125 | | } |
| | 3 | 126 | | } |
| | | 127 | | |
| | | 128 | | [HttpGet("periodo/baja")] |
| | | 129 | | public async Task<IActionResult> ObtenerPorPeriodoBaja([FromQuery] int anio, [FromQuery] int mes) |
| | | 130 | | { |
| | | 131 | | try |
| | | 132 | | { |
| | 3 | 133 | | if (anio <= 0 || (mes != 6 && mes != 12)) |
| | | 134 | | { |
| | 1 | 135 | | return BadRequest(ApiResponse<string>.ErrorResult("El período de consulta debe usar mes 6 o 12 y un año |
| | | 136 | | } |
| | | 137 | | |
| | 2 | 138 | | var resultados = await _bajaService.ObtenerPorPeriodoAsync(anio, mes); |
| | 1 | 139 | | return Ok(ApiResponse<IEnumerable<AguinaldoBajaDto>>.SuccessResult(resultados)); |
| | | 140 | | } |
| | 1 | 141 | | catch (Exception ex) |
| | | 142 | | { |
| | 1 | 143 | | _logger.LogError(ex, "Error consultando aguinaldo por baja"); |
| | 1 | 144 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 145 | | } |
| | 3 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Genera un ZIP con los recibos PDF del aguinaldo semestral para los funcionarios indicados. |
| | | 150 | | /// Si PersonaIds es null o vacío, genera para todos los del período. |
| | | 151 | | /// POST /api/aguinaldos/recibos/semestral |
| | | 152 | | /// </summary> |
| | | 153 | | [HttpPost("recibos/semestral")] |
| | | 154 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 155 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 156 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 157 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 158 | | public async Task<IActionResult> GenerarRecibosSemestral([FromBody] GenerarRecibosAguinaldoSemestralRequest request) |
| | | 159 | | { |
| | | 160 | | try |
| | | 161 | | { |
| | 5 | 162 | | if (request.Anio <= 0 || (request.Semestre != 1 && request.Semestre != 2)) |
| | 1 | 163 | | return BadRequest(ApiResponse<string>.ErrorResult("Año y semestre inválidos. El semestre debe ser 1 o 2. |
| | | 164 | | |
| | 4 | 165 | | var personaIds = request.PersonaIds?.Count > 0 ? request.PersonaIds : null; |
| | | 166 | | |
| | 4 | 167 | | if (personaIds?.Count == 1) |
| | | 168 | | { |
| | 1 | 169 | | var (pdfBytes, fileName) = await _reciboService.GenerarPdfSemestralAsync( |
| | 1 | 170 | | personaIds[0], request.Anio, request.Semestre); |
| | 1 | 171 | | return File(pdfBytes, "application/pdf", fileName); |
| | | 172 | | } |
| | | 173 | | |
| | 3 | 174 | | var (zipStream, zipName) = await _reciboService.GenerarLoteSemestralZipAsync( |
| | 3 | 175 | | request.Anio, request.Semestre, personaIds); |
| | 1 | 176 | | return File(zipStream, "application/zip", zipName); |
| | | 177 | | } |
| | 1 | 178 | | catch (KeyNotFoundException ex) |
| | | 179 | | { |
| | 1 | 180 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 181 | | } |
| | 1 | 182 | | catch (Exception ex) |
| | | 183 | | { |
| | 1 | 184 | | _logger.LogError(ex, "Error generando recibos de aguinaldo semestral"); |
| | 1 | 185 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 186 | | } |
| | 5 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Genera el PDF del recibo de aguinaldo semestral de un funcionario individual. |
| | | 191 | | /// GET /api/aguinaldos/recibos/semestral/{personaId}?anio=2026&semestre=1 |
| | | 192 | | /// </summary> |
| | | 193 | | [HttpGet("recibos/semestral/{personaId}")] |
| | | 194 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 195 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 196 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 197 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 198 | | public async Task<IActionResult> GenerarReciboPdfSemestral( |
| | | 199 | | long personaId, |
| | | 200 | | [FromQuery] int anio, |
| | | 201 | | [FromQuery] short semestre) |
| | | 202 | | { |
| | | 203 | | try |
| | | 204 | | { |
| | 3 | 205 | | var (pdfBytes, fileName) = await _reciboService.GenerarPdfSemestralAsync(personaId, anio, semestre); |
| | 1 | 206 | | return File(pdfBytes, "application/pdf", fileName); |
| | | 207 | | } |
| | 1 | 208 | | catch (KeyNotFoundException ex) |
| | | 209 | | { |
| | 1 | 210 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 211 | | } |
| | 1 | 212 | | catch (Exception ex) |
| | | 213 | | { |
| | 1 | 214 | | _logger.LogError(ex, "Error generando PDF de aguinaldo semestral para persona {PersonaId}", personaId); |
| | 1 | 215 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 216 | | } |
| | 3 | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Genera un ZIP con los recibos PDF de aguinaldo por baja para los ids indicados. |
| | | 221 | | /// POST /api/aguinaldos/recibos/baja |
| | | 222 | | /// </summary> |
| | | 223 | | [HttpPost("recibos/baja")] |
| | | 224 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 225 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 226 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 227 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 228 | | public async Task<IActionResult> GenerarRecibosBaja([FromBody] GenerarRecibosAguinaldoBajaRequest request) |
| | | 229 | | { |
| | | 230 | | try |
| | | 231 | | { |
| | 5 | 232 | | if (request.AguinaldoBajaIds == null || !request.AguinaldoBajaIds.Any()) |
| | 1 | 233 | | return BadRequest(ApiResponse<string>.ErrorResult("Debe indicar al menos un id de aguinaldo por baja.")) |
| | | 234 | | |
| | 4 | 235 | | if (request.AguinaldoBajaIds.Count == 1) |
| | | 236 | | { |
| | 2 | 237 | | var (pdfBytes, fileName) = await _reciboService.GenerarPdfBajaAsync(request.AguinaldoBajaIds[0]); |
| | 1 | 238 | | return File(pdfBytes, "application/pdf", fileName); |
| | | 239 | | } |
| | | 240 | | |
| | 2 | 241 | | var (zipStream, zipName) = await _reciboService.GenerarLoteBajaZipAsync(request.AguinaldoBajaIds); |
| | 1 | 242 | | return File(zipStream, "application/zip", zipName); |
| | | 243 | | } |
| | 1 | 244 | | catch (KeyNotFoundException ex) |
| | | 245 | | { |
| | 1 | 246 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 247 | | } |
| | 1 | 248 | | catch (Exception ex) |
| | | 249 | | { |
| | 1 | 250 | | _logger.LogError(ex, "Error generando recibos de aguinaldo por baja"); |
| | 1 | 251 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 252 | | } |
| | 5 | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Genera el padrón LIQUIDOS de aguinaldo semestral en formato Excel. |
| | | 257 | | /// Incluye duodécimo, sueldo retiro, montepíos, sanidad y líquido a cobrar. |
| | | 258 | | /// GET /api/aguinaldos/padron/semestral?anio=2026&mes=6&esLeyVieja=false |
| | | 259 | | /// </summary> |
| | | 260 | | [HttpGet("padron/semestral")] |
| | | 261 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 262 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 263 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 264 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 265 | | public async Task<IActionResult> GenerarPadronSemestral( |
| | | 266 | | [FromQuery] int anio, |
| | | 267 | | [FromQuery] int mes, |
| | | 268 | | [FromQuery] bool esLeyVieja) |
| | | 269 | | { |
| | | 270 | | try |
| | | 271 | | { |
| | 3 | 272 | | if (anio <= 0 || (mes != 6 && mes != 12)) |
| | 1 | 273 | | return BadRequest(ApiResponse<string>.ErrorResult("El año debe ser válido y el mes debe ser 6 o 12.")); |
| | | 274 | | |
| | 2 | 275 | | var (bytes, fileName) = await _padronService.GenerarPadronExcelAsync(anio, mes, esLeyVieja); |
| | 1 | 276 | | return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); |
| | | 277 | | } |
| | 1 | 278 | | catch (Exception ex) |
| | | 279 | | { |
| | 1 | 280 | | _logger.LogError(ex, "Error generando padrón de aguinaldo semestral {Anio}/{Mes}", anio, mes); |
| | 1 | 281 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 282 | | } |
| | 3 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Genera el listado calculado de control de aguinaldo en formato Excel. |
| | | 287 | | /// Variantes: TODOS, REVISTAN, BAJAS. |
| | | 288 | | /// GET /api/aguinaldos/reporte-control?anio=2026&mes=6&esLeyVieja=false&variante=TODOS |
| | | 289 | | /// </summary> |
| | | 290 | | [HttpGet("reporte-control")] |
| | | 291 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 292 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 293 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 294 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 295 | | public async Task<IActionResult> GenerarReporteControl( |
| | | 296 | | [FromQuery] int anio, |
| | | 297 | | [FromQuery] int mes, |
| | | 298 | | [FromQuery] bool esLeyVieja, |
| | | 299 | | [FromQuery] string variante = "TODOS") |
| | | 300 | | { |
| | | 301 | | try |
| | | 302 | | { |
| | 4 | 303 | | if (anio <= 0 || (mes != 6 && mes != 12)) |
| | 1 | 304 | | return BadRequest(ApiResponse<string>.ErrorResult("El año debe ser válido y el mes debe ser 6 o 12.")); |
| | | 305 | | |
| | 3 | 306 | | var v = variante.ToUpper(); |
| | 3 | 307 | | if (v != "TODOS" && v != "REVISTAN" && v != "BAJAS") |
| | 1 | 308 | | return BadRequest(ApiResponse<string>.ErrorResult("La variante debe ser TODOS, REVISTAN o BAJAS.")); |
| | | 309 | | |
| | 2 | 310 | | var (bytes, fileName) = await _reporteControlService.GenerarExcelAsync(anio, mes, v, esLeyVieja); |
| | 1 | 311 | | return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); |
| | | 312 | | } |
| | 1 | 313 | | catch (Exception ex) |
| | | 314 | | { |
| | 1 | 315 | | _logger.LogError(ex, "Error generando reporte de control de aguinaldo {Anio}/{Mes}/{Variante}", anio, mes, v |
| | 1 | 316 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 317 | | } |
| | 4 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Genera el PDF del recibo de aguinaldo por baja individual. |
| | | 322 | | /// GET /api/aguinaldos/recibos/baja/{aguinaldoBajaId} |
| | | 323 | | /// </summary> |
| | | 324 | | [HttpGet("recibos/baja/{aguinaldoBajaId}")] |
| | | 325 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 326 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 327 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 328 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 329 | | public async Task<IActionResult> GenerarReciboPdfBaja(long aguinaldoBajaId) |
| | | 330 | | { |
| | | 331 | | try |
| | | 332 | | { |
| | 3 | 333 | | var (pdfBytes, fileName) = await _reciboService.GenerarPdfBajaAsync(aguinaldoBajaId); |
| | 1 | 334 | | return File(pdfBytes, "application/pdf", fileName); |
| | | 335 | | } |
| | 1 | 336 | | catch (KeyNotFoundException ex) |
| | | 337 | | { |
| | 1 | 338 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 339 | | } |
| | 1 | 340 | | catch (Exception ex) |
| | | 341 | | { |
| | 1 | 342 | | _logger.LogError(ex, "Error generando PDF de aguinaldo por baja {AguinaldoBajaId}", aguinaldoBajaId); |
| | 1 | 343 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 344 | | } |
| | 3 | 345 | | } |
| | | 346 | | } |