| | | 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/organismos")] |
| | | 12 | | [Authorize] |
| | | 13 | | [Produces("application/json")] |
| | | 14 | | public class ArchivoOrganismoController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IArchivoOrganismoService _service; |
| | | 17 | | private readonly IPeriodoService _periodoService; |
| | | 18 | | private readonly IAuditoriaService _auditoriaService; |
| | | 19 | | private readonly ICurrentUserContext _currentUser; |
| | | 20 | | private readonly ILogger<ArchivoOrganismoController> _logger; |
| | | 21 | | |
| | 9 | 22 | | public ArchivoOrganismoController( |
| | 9 | 23 | | IArchivoOrganismoService service, |
| | 9 | 24 | | IPeriodoService periodoService, |
| | 9 | 25 | | IAuditoriaService auditoriaService, |
| | 9 | 26 | | ICurrentUserContext currentUser, |
| | 9 | 27 | | ILogger<ArchivoOrganismoController> logger) |
| | | 28 | | { |
| | 9 | 29 | | _service = service; |
| | 9 | 30 | | _periodoService = periodoService; |
| | 9 | 31 | | _auditoriaService = auditoriaService; |
| | 9 | 32 | | _currentUser = currentUser; |
| | 9 | 33 | | _logger = logger; |
| | 9 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Vista previa del archivo BROU: filas con cuenta y excepciones sin cuenta. |
| | | 38 | | /// GET /api/liquidacion/organismos/{periodoId}/brou/preview |
| | | 39 | | /// </summary> |
| | | 40 | | [HttpGet("{periodoId}/brou/preview")] |
| | | 41 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 42 | | [ProducesResponseType(typeof(ApiResponse<BrouPreviewDto>), StatusCodes.Status200OK)] |
| | | 43 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 44 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 45 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 46 | | public async Task<IActionResult> GetBrouPreview(long periodoId) |
| | | 47 | | { |
| | | 48 | | try |
| | | 49 | | { |
| | 3 | 50 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 51 | | if (periodo == null) |
| | 1 | 52 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 53 | | |
| | 2 | 54 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 55 | | return Conflict(ApiResponse<string>.ErrorResult( |
| | 1 | 56 | | "El período no está cerrado; los archivos para organismos solo están disponibles para períodos en es |
| | | 57 | | |
| | 1 | 58 | | var preview = await _service.ObtenerPreviewBrouAsync(periodoId); |
| | 1 | 59 | | return Ok(ApiResponse<BrouPreviewDto>.SuccessResult(preview)); |
| | | 60 | | } |
| | 0 | 61 | | catch (Exception ex) |
| | | 62 | | { |
| | 0 | 63 | | _logger.LogError(ex, "Error al obtener preview BROU del período {PeriodoId}", periodoId); |
| | 0 | 64 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 65 | | } |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Descarga el archivo CSV BROU del período. |
| | | 70 | | /// GET /api/liquidacion/organismos/{periodoId}/brou |
| | | 71 | | /// </summary> |
| | | 72 | | [HttpGet("{periodoId}/brou")] |
| | | 73 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 74 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 75 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 76 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 77 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 78 | | public async Task<IActionResult> GetBrouCsv(long periodoId) |
| | | 79 | | { |
| | | 80 | | try |
| | | 81 | | { |
| | 3 | 82 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 83 | | if (periodo == null) |
| | 1 | 84 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 85 | | |
| | 2 | 86 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 87 | | return Conflict(ApiResponse<string>.ErrorResult( |
| | 1 | 88 | | "El período no está cerrado; los archivos para organismos solo están disponibles para períodos en es |
| | | 89 | | |
| | 1 | 90 | | var csvBytes = await _service.GenerarCsvBrouAsync(periodoId); |
| | | 91 | | |
| | 1 | 92 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 93 | | _currentUser.UserId ?? 0, |
| | 1 | 94 | | AccionEnum.GenerarArchivoBrou, |
| | 1 | 95 | | ContextoEnum.Liquidaciones, |
| | 1 | 96 | | _currentUser.Host, |
| | 1 | 97 | | entidad: "ArchivoBrou", |
| | 1 | 98 | | entidadId: periodoId, |
| | 1 | 99 | | detalle: new { periodoId }); |
| | | 100 | | |
| | 1 | 101 | | var timestamp = DateTime.Now.ToString("yyyyMMddHHmm"); |
| | 1 | 102 | | var fileName = $"brou_{periodo.Anio}_{periodo.Mes:D2}_{timestamp}.csv"; |
| | 1 | 103 | | return File(csvBytes, "text/csv", fileName); |
| | | 104 | | } |
| | 0 | 105 | | catch (Exception ex) |
| | | 106 | | { |
| | 0 | 107 | | _logger.LogError(ex, "Error al generar CSV BROU del período {PeriodoId}", periodoId); |
| | 0 | 108 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 109 | | } |
| | 3 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Retorna el resumen SIIF agrupado por programa × régimen. |
| | | 114 | | /// GET /api/liquidacion/organismos/{periodoId}/siif |
| | | 115 | | /// </summary> |
| | | 116 | | [HttpGet("{periodoId}/siif")] |
| | | 117 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 118 | | [ProducesResponseType(typeof(ApiResponse<SiifResumenDto>), StatusCodes.Status200OK)] |
| | | 119 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 120 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 121 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 122 | | public async Task<IActionResult> GetSiifResumen(long periodoId) |
| | | 123 | | { |
| | | 124 | | try |
| | | 125 | | { |
| | 3 | 126 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 3 | 127 | | if (periodo == null) |
| | 1 | 128 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 129 | | |
| | 2 | 130 | | if (periodo.Estado != Periodo.EstadoCerrada) |
| | 1 | 131 | | return Conflict(ApiResponse<string>.ErrorResult( |
| | 1 | 132 | | "El período no está cerrado; los archivos para organismos solo están disponibles para períodos en es |
| | | 133 | | |
| | 1 | 134 | | var resumen = await _service.ObtenerResumenSiifAsync(periodoId); |
| | 1 | 135 | | return Ok(ApiResponse<SiifResumenDto>.SuccessResult(resumen)); |
| | | 136 | | } |
| | 0 | 137 | | catch (Exception ex) |
| | | 138 | | { |
| | 0 | 139 | | _logger.LogError(ex, "Error al obtener resumen SIIF del período {PeriodoId}", periodoId); |
| | 0 | 140 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 141 | | } |
| | 3 | 142 | | } |
| | | 143 | | } |