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