| | | 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")] |
| | | 13 | | [Authorize] |
| | | 14 | | [Produces("application/json")] |
| | | 15 | | public class ReportePresupuestalController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly IReportePresupuestalService _service; |
| | | 18 | | private readonly IPeriodoService _periodoService; |
| | | 19 | | private readonly IAuditoriaService _auditoriaService; |
| | | 20 | | private readonly ICurrentUserContext _currentUser; |
| | | 21 | | private readonly ILogger<ReportePresupuestalController> _logger; |
| | | 22 | | |
| | 42 | 23 | | public ReportePresupuestalController( |
| | 42 | 24 | | IReportePresupuestalService service, |
| | 42 | 25 | | IPeriodoService periodoService, |
| | 42 | 26 | | IAuditoriaService auditoriaService, |
| | 42 | 27 | | ICurrentUserContext currentUser, |
| | 42 | 28 | | ILogger<ReportePresupuestalController> logger) |
| | | 29 | | { |
| | 42 | 30 | | _service = service; |
| | 42 | 31 | | _periodoService = periodoService; |
| | 42 | 32 | | _auditoriaService = auditoriaService; |
| | 42 | 33 | | _currentUser = currentUser; |
| | 42 | 34 | | _logger = logger; |
| | 42 | 35 | | } |
| | | 36 | | |
| | | 37 | | // ─── Resumen por programa ──────────────────────────────────────────────── |
| | | 38 | | |
| | | 39 | | [HttpGet("{periodoId}/resumen-programa")] |
| | | 40 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 41 | | [ProducesResponseType(typeof(ApiResponse<ResumenProgramaDto>), 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> GetResumenPrograma(long periodoId) |
| | | 46 | | { |
| | | 47 | | try |
| | | 48 | | { |
| | 3 | 49 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 50 | | if (periodo == null) |
| | 1 | 51 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 52 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 53 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 54 | | |
| | 1 | 55 | | var result = await _service.ObtenerResumenProgramaAsync(periodoId); |
| | 1 | 56 | | return Ok(ApiResponse<ResumenProgramaDto>.SuccessResult(result)); |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | _logger.LogError(ex, "Error al obtener resumen programa del período {PeriodoId}", periodoId); |
| | 0 | 61 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 62 | | } |
| | 3 | 63 | | } |
| | | 64 | | |
| | | 65 | | [HttpGet("{periodoId}/resumen-programa/csv")] |
| | | 66 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 67 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 68 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 69 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 70 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 71 | | public async Task<IActionResult> GetResumenProgramaCsv(long periodoId) |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 3 | 75 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 76 | | if (periodo == null) |
| | 1 | 77 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 78 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 79 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 80 | | |
| | 1 | 81 | | var csv = await _service.GenerarCsvResumenProgramaAsync(periodoId); |
| | 1 | 82 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteResumenPrograma, periodoId); |
| | 1 | 83 | | return File(csv, "text/csv", $"resumen_programa_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 84 | | } |
| | 0 | 85 | | catch (Exception ex) |
| | | 86 | | { |
| | 0 | 87 | | _logger.LogError(ex, "Error al generar CSV resumen programa del período {PeriodoId}", periodoId); |
| | 0 | 88 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 89 | | } |
| | 3 | 90 | | } |
| | | 91 | | |
| | | 92 | | // ─── Rubro 083 ─────────────────────────────────────────────────────────── |
| | | 93 | | |
| | | 94 | | [HttpGet("{periodoId}/rubro-083")] |
| | | 95 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 96 | | [ProducesResponseType(typeof(ApiResponse<SiifResumenDto>), StatusCodes.Status200OK)] |
| | | 97 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 98 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 99 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 100 | | public async Task<IActionResult> GetRubro083(long periodoId) |
| | | 101 | | { |
| | | 102 | | try |
| | | 103 | | { |
| | 3 | 104 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 105 | | if (periodo == null) |
| | 1 | 106 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 107 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 108 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 109 | | |
| | 1 | 110 | | var result = await _service.ObtenerResumenRubro083Async(periodoId); |
| | 1 | 111 | | return Ok(ApiResponse<SiifResumenDto>.SuccessResult(result)); |
| | | 112 | | } |
| | 0 | 113 | | catch (Exception ex) |
| | | 114 | | { |
| | 0 | 115 | | _logger.LogError(ex, "Error al obtener resumen rubro 083 del período {PeriodoId}", periodoId); |
| | 0 | 116 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 117 | | } |
| | 3 | 118 | | } |
| | | 119 | | |
| | | 120 | | [HttpGet("{periodoId}/rubro-083/csv")] |
| | | 121 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 122 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 123 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 124 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 125 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 126 | | public async Task<IActionResult> GetRubro083Csv(long periodoId) |
| | | 127 | | { |
| | | 128 | | try |
| | | 129 | | { |
| | 3 | 130 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 131 | | if (periodo == null) |
| | 1 | 132 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 133 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 134 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 135 | | |
| | 1 | 136 | | var csv = await _service.GenerarCsvRubro083Async(periodoId); |
| | 1 | 137 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteRubro083, periodoId); |
| | 1 | 138 | | return File(csv, "text/csv", $"rubro083_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 139 | | } |
| | 0 | 140 | | catch (Exception ex) |
| | | 141 | | { |
| | 0 | 142 | | _logger.LogError(ex, "Error al generar CSV rubro 083 del período {PeriodoId}", periodoId); |
| | 0 | 143 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 144 | | } |
| | 3 | 145 | | } |
| | | 146 | | |
| | | 147 | | // ─── Padrón por rubro ──────────────────────────────────────────────────── |
| | | 148 | | |
| | | 149 | | [HttpGet("{periodoId}/padron-rubro")] |
| | | 150 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 151 | | [ProducesResponseType(typeof(ApiResponse<PadronRubroDto>), StatusCodes.Status200OK)] |
| | | 152 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 153 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 154 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 155 | | public async Task<IActionResult> GetPadronRubro(long periodoId) |
| | | 156 | | { |
| | | 157 | | try |
| | | 158 | | { |
| | 3 | 159 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 160 | | if (periodo == null) |
| | 1 | 161 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 162 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 163 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 164 | | |
| | 1 | 165 | | var result = await _service.ObtenerPadronRubroAsync(periodoId); |
| | 1 | 166 | | return Ok(ApiResponse<PadronRubroDto>.SuccessResult(result)); |
| | | 167 | | } |
| | 0 | 168 | | catch (Exception ex) |
| | | 169 | | { |
| | 0 | 170 | | _logger.LogError(ex, "Error al obtener padrón rubro del período {PeriodoId}", periodoId); |
| | 0 | 171 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 172 | | } |
| | 3 | 173 | | } |
| | | 174 | | |
| | | 175 | | [HttpGet("{periodoId}/padron-rubro/csv")] |
| | | 176 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 177 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 178 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 179 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 180 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 181 | | public async Task<IActionResult> GetPadronRubroCsv(long periodoId) |
| | | 182 | | { |
| | | 183 | | try |
| | | 184 | | { |
| | 3 | 185 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 186 | | if (periodo == null) |
| | 1 | 187 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 188 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 189 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 190 | | |
| | 1 | 191 | | var csv = await _service.GenerarCsvPadronRubroAsync(periodoId); |
| | 1 | 192 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReportePadronRubro, periodoId); |
| | 1 | 193 | | return File(csv, "text/csv", $"padron_rubro_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 194 | | } |
| | 0 | 195 | | catch (Exception ex) |
| | | 196 | | { |
| | 0 | 197 | | _logger.LogError(ex, "Error al generar CSV padrón rubro del período {PeriodoId}", periodoId); |
| | 0 | 198 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 199 | | } |
| | 3 | 200 | | } |
| | | 201 | | |
| | | 202 | | // ─── Padrón mensual ────────────────────────────────────────────────────── |
| | | 203 | | |
| | | 204 | | [HttpGet("{periodoId}/padron-mensual")] |
| | | 205 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 206 | | [ProducesResponseType(typeof(ApiResponse<List<PadronMensualFilaDto>>), StatusCodes.Status200OK)] |
| | | 207 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 208 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 209 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 210 | | public async Task<IActionResult> GetPadronMensual(long periodoId) |
| | | 211 | | { |
| | | 212 | | try |
| | | 213 | | { |
| | 3 | 214 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 215 | | if (periodo == null) |
| | 1 | 216 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 217 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 218 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 219 | | |
| | 1 | 220 | | var result = await _service.ObtenerPadronMensualAsync(periodoId); |
| | 1 | 221 | | return Ok(ApiResponse<List<PadronMensualFilaDto>>.SuccessResult(result)); |
| | | 222 | | } |
| | 0 | 223 | | catch (Exception ex) |
| | | 224 | | { |
| | 0 | 225 | | _logger.LogError(ex, "Error al obtener padrón mensual del período {PeriodoId}", periodoId); |
| | 0 | 226 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 227 | | } |
| | 3 | 228 | | } |
| | | 229 | | |
| | | 230 | | [HttpGet("{periodoId}/padron-mensual/csv")] |
| | | 231 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 232 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 233 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 234 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 235 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 236 | | public async Task<IActionResult> GetPadronMensualCsv(long periodoId) |
| | | 237 | | { |
| | | 238 | | try |
| | | 239 | | { |
| | 3 | 240 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 241 | | if (periodo == null) |
| | 1 | 242 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 243 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 244 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 245 | | |
| | 1 | 246 | | var csv = await _service.GenerarCsvPadronMensualAsync(periodoId); |
| | 1 | 247 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReportePadronMensual, periodoId); |
| | 1 | 248 | | return File(csv, "text/csv", $"padron_mensual_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 249 | | } |
| | 0 | 250 | | catch (Exception ex) |
| | | 251 | | { |
| | 0 | 252 | | _logger.LogError(ex, "Error al generar CSV padrón mensual del período {PeriodoId}", periodoId); |
| | 0 | 253 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 254 | | } |
| | 3 | 255 | | } |
| | | 256 | | |
| | | 257 | | // ─── Movimientos ───────────────────────────────────────────────────────── |
| | | 258 | | |
| | | 259 | | [HttpGet("{periodoId}/movimientos")] |
| | | 260 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 261 | | [ProducesResponseType(typeof(ApiResponse<List<MovimientoPersonalDto>>), StatusCodes.Status200OK)] |
| | | 262 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 263 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 264 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 265 | | public async Task<IActionResult> GetMovimientos(long periodoId) |
| | | 266 | | { |
| | | 267 | | try |
| | | 268 | | { |
| | 3 | 269 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 270 | | if (periodo == null) |
| | 1 | 271 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 272 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 273 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 274 | | |
| | 1 | 275 | | var result = await _service.ObtenerMovimientosAsync(periodoId); |
| | 1 | 276 | | return Ok(ApiResponse<List<MovimientoPersonalDto>>.SuccessResult(result)); |
| | | 277 | | } |
| | 0 | 278 | | catch (Exception ex) |
| | | 279 | | { |
| | 0 | 280 | | _logger.LogError(ex, "Error al obtener movimientos del período {PeriodoId}", periodoId); |
| | 0 | 281 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 282 | | } |
| | 3 | 283 | | } |
| | | 284 | | |
| | | 285 | | [HttpGet("{periodoId}/movimientos/csv")] |
| | | 286 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 287 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 288 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 289 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 290 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 291 | | public async Task<IActionResult> GetMovimientosCsv(long periodoId) |
| | | 292 | | { |
| | | 293 | | try |
| | | 294 | | { |
| | 3 | 295 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 296 | | if (periodo == null) |
| | 1 | 297 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 298 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 299 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 300 | | |
| | 1 | 301 | | var csv = await _service.GenerarCsvMovimientosAsync(periodoId); |
| | 1 | 302 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteMovimientos, periodoId); |
| | 1 | 303 | | return File(csv, "text/csv", $"movimientos_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 304 | | } |
| | 0 | 305 | | catch (Exception ex) |
| | | 306 | | { |
| | 0 | 307 | | _logger.LogError(ex, "Error al generar CSV movimientos del período {PeriodoId}", periodoId); |
| | 0 | 308 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 309 | | } |
| | 3 | 310 | | } |
| | | 311 | | |
| | | 312 | | // ─── Diferencias ───────────────────────────────────────────────────────── |
| | | 313 | | |
| | | 314 | | [HttpGet("{periodoId}/diferencias")] |
| | | 315 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 316 | | [ProducesResponseType(typeof(ApiResponse<PadronDiferenciasDto>), StatusCodes.Status200OK)] |
| | | 317 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 318 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 319 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 320 | | public async Task<IActionResult> GetDiferencias(long periodoId) |
| | | 321 | | { |
| | | 322 | | try |
| | | 323 | | { |
| | 3 | 324 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 325 | | if (periodo == null) |
| | 1 | 326 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 327 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 328 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 329 | | |
| | 1 | 330 | | var result = await _service.ObtenerDiferenciasAsync(periodoId); |
| | 1 | 331 | | return Ok(ApiResponse<PadronDiferenciasDto>.SuccessResult(result)); |
| | | 332 | | } |
| | 0 | 333 | | catch (Exception ex) |
| | | 334 | | { |
| | 0 | 335 | | _logger.LogError(ex, "Error al obtener diferencias del período {PeriodoId}", periodoId); |
| | 0 | 336 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 337 | | } |
| | 3 | 338 | | } |
| | | 339 | | |
| | | 340 | | [HttpGet("{periodoId}/diferencias/csv")] |
| | | 341 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 342 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 343 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 344 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 345 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 346 | | public async Task<IActionResult> GetDiferenciasCsv(long periodoId) |
| | | 347 | | { |
| | | 348 | | try |
| | | 349 | | { |
| | 3 | 350 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 351 | | if (periodo == null) |
| | 1 | 352 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 353 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 354 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 355 | | |
| | 1 | 356 | | var csv = await _service.GenerarCsvDiferenciasAsync(periodoId); |
| | 1 | 357 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteDiferencias, periodoId); |
| | 1 | 358 | | return File(csv, "text/csv", $"diferencias_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 359 | | } |
| | 0 | 360 | | catch (Exception ex) |
| | | 361 | | { |
| | 0 | 362 | | _logger.LogError(ex, "Error al generar CSV diferencias del período {PeriodoId}", periodoId); |
| | 0 | 363 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 364 | | } |
| | 3 | 365 | | } |
| | | 366 | | |
| | | 367 | | // ─── No disponibles ────────────────────────────────────────────────────── |
| | | 368 | | |
| | | 369 | | [HttpGet("{periodoId}/no-disponibles")] |
| | | 370 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 371 | | [ProducesResponseType(typeof(ApiResponse<List<NoDisponibleDto>>), StatusCodes.Status200OK)] |
| | | 372 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 373 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 374 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 375 | | public async Task<IActionResult> GetNoDisponibles(long periodoId) |
| | | 376 | | { |
| | | 377 | | try |
| | | 378 | | { |
| | 3 | 379 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 380 | | if (periodo == null) |
| | 1 | 381 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 382 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 383 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 384 | | |
| | 1 | 385 | | var result = await _service.ObtenerNoDisponiblesAsync(periodoId); |
| | 1 | 386 | | return Ok(ApiResponse<List<NoDisponibleDto>>.SuccessResult(result)); |
| | | 387 | | } |
| | 0 | 388 | | catch (Exception ex) |
| | | 389 | | { |
| | 0 | 390 | | _logger.LogError(ex, "Error al obtener no disponibles del período {PeriodoId}", periodoId); |
| | 0 | 391 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 392 | | } |
| | 3 | 393 | | } |
| | | 394 | | |
| | | 395 | | [HttpGet("{periodoId}/no-disponibles/csv")] |
| | | 396 | | [RequirePermission(PermisoEnum.VerReportesPresupuestales)] |
| | | 397 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 398 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 399 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 400 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 401 | | public async Task<IActionResult> GetNoDisponiblesCsv(long periodoId) |
| | | 402 | | { |
| | | 403 | | try |
| | | 404 | | { |
| | 3 | 405 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 406 | | if (periodo == null) |
| | 1 | 407 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 2 | 408 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 409 | | return Conflict(ApiResponse<string>.ErrorResult("El período no está cerrado")); |
| | | 410 | | |
| | 1 | 411 | | var csv = await _service.GenerarCsvNoDisponiblesAsync(periodoId); |
| | 1 | 412 | | await RegistrarAuditoriaAsync(AccionEnum.GenerarReporteNoDisponibles, periodoId); |
| | 1 | 413 | | return File(csv, "text/csv", $"no_disponibles_{periodo.Anio}_{periodo.Mes:D2}.csv"); |
| | | 414 | | } |
| | 0 | 415 | | catch (Exception ex) |
| | | 416 | | { |
| | 0 | 417 | | _logger.LogError(ex, "Error al generar CSV no disponibles del período {PeriodoId}", periodoId); |
| | 0 | 418 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 419 | | } |
| | 3 | 420 | | } |
| | | 421 | | |
| | | 422 | | // ─── Helper ────────────────────────────────────────────────────────────── |
| | | 423 | | |
| | | 424 | | private Task RegistrarAuditoriaAsync(AccionEnum accion, long periodoId) => |
| | 7 | 425 | | _auditoriaService.LogAuditoriaAsync( |
| | 7 | 426 | | _currentUser.UserId ?? 0, |
| | 7 | 427 | | accion, |
| | 7 | 428 | | ContextoEnum.Liquidaciones, |
| | 7 | 429 | | _currentUser.Host, |
| | 7 | 430 | | entidad: "ReportePresupuestal", |
| | 7 | 431 | | entidadId: periodoId, |
| | 7 | 432 | | detalle: new { periodoId }); |
| | | 433 | | } |