| | | 1 | | using FAU.API.Security; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using FAU.Logica.Exceptions; |
| | | 5 | | using FAU.Logica.Services; |
| | | 6 | | using Microsoft.AspNetCore.Authorization; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | |
| | | 9 | | namespace FAU.API.Controllers; |
| | | 10 | | |
| | | 11 | | [ApiController] |
| | | 12 | | [Route("api/liquidacion/[controller]")] |
| | | 13 | | [Authorize(Roles = "Administrador")] |
| | | 14 | | public class PeriodosController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IPeriodoService _periodoService; |
| | | 17 | | private readonly ILogger<PeriodosController> _logger; |
| | | 18 | | |
| | 19 | 19 | | public PeriodosController(IPeriodoService periodoService, ILogger<PeriodosController> logger) |
| | | 20 | | { |
| | 19 | 21 | | _periodoService = periodoService; |
| | 19 | 22 | | _logger = logger; |
| | 19 | 23 | | } |
| | | 24 | | |
| | | 25 | | [HttpPost] |
| | | 26 | | public async Task<IActionResult> AbrirPeriodo([FromBody] AbrirPeriodoRequest request) |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | 3 | 30 | | if (!ModelState.IsValid) |
| | | 31 | | { |
| | 0 | 32 | | return BadRequest(ApiResponse<string>.ErrorResult("Solicitud de apertura de período inválida")); |
| | | 33 | | } |
| | | 34 | | |
| | 3 | 35 | | var (success, periodo, error) = await _periodoService.AbrirPeriodoAsync(request.Anio, request.Mes); |
| | | 36 | | |
| | 3 | 37 | | if (!success) |
| | | 38 | | { |
| | 2 | 39 | | if (error?.Contains("Ya existe un período activo", StringComparison.OrdinalIgnoreCase) == true) |
| | | 40 | | { |
| | 1 | 41 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 42 | | } |
| | | 43 | | |
| | 1 | 44 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al abrir el período")); |
| | | 45 | | } |
| | | 46 | | |
| | 1 | 47 | | var dto = MapearPeriodo(periodo!); |
| | 1 | 48 | | return CreatedAtAction(nameof(GetPeriodo), new { id = periodo!.Id }, ApiResponse<PeriodoDto>.SuccessResult(d |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception ex) |
| | | 51 | | { |
| | 0 | 52 | | _logger.LogError(ex, "Error al abrir período"); |
| | 0 | 53 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 54 | | } |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | [HttpGet("activo")] |
| | | 58 | | public async Task<IActionResult> GetPeriodoActivo() |
| | | 59 | | { |
| | | 60 | | try |
| | | 61 | | { |
| | 2 | 62 | | var periodo = await _periodoService.ObtenerEnCursoAsync(); |
| | 2 | 63 | | if (periodo == null) |
| | | 64 | | { |
| | 1 | 65 | | return NotFound(ApiResponse<string>.ErrorResult("No hay un período activo")); |
| | | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo))); |
| | | 69 | | } |
| | 0 | 70 | | catch (Exception ex) |
| | | 71 | | { |
| | 0 | 72 | | _logger.LogError(ex, "Error al obtener período activo"); |
| | 0 | 73 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 74 | | } |
| | 2 | 75 | | } |
| | | 76 | | |
| | | 77 | | [HttpGet("{id}")] |
| | | 78 | | public async Task<IActionResult> GetPeriodo(long id) |
| | | 79 | | { |
| | | 80 | | try |
| | | 81 | | { |
| | 2 | 82 | | var periodo = await _periodoService.ObtenerPorIdAsync(id); |
| | 2 | 83 | | if (periodo == null) |
| | | 84 | | { |
| | 1 | 85 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo))); |
| | | 89 | | } |
| | 0 | 90 | | catch (Exception ex) |
| | | 91 | | { |
| | 0 | 92 | | _logger.LogError(ex, "Error al obtener período {Id}", id); |
| | 0 | 93 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 94 | | } |
| | 2 | 95 | | } |
| | | 96 | | |
| | | 97 | | [HttpGet("{id}/precondiciones")] |
| | | 98 | | public async Task<IActionResult> GetPrecondiciones(long id) |
| | | 99 | | { |
| | | 100 | | try |
| | | 101 | | { |
| | 0 | 102 | | var precondiciones = await _periodoService.ObtenerPrecondicionesAsync(id); |
| | 0 | 103 | | if (precondiciones == null) |
| | 0 | 104 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 0 | 105 | | return Ok(ApiResponse<PrecondicionesDto>.SuccessResult(precondiciones)); |
| | | 106 | | } |
| | 0 | 107 | | catch (Exception ex) |
| | | 108 | | { |
| | 0 | 109 | | _logger.LogError(ex, "Error al obtener precondiciones del período {Id}", id); |
| | 0 | 110 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 111 | | } |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | [HttpGet("{id}/validar-insumos")] |
| | | 115 | | public async Task<IActionResult> ValidarInsumos(long id) |
| | | 116 | | { |
| | | 117 | | try |
| | | 118 | | { |
| | 0 | 119 | | var validacion = await _periodoService.ValidarInsumosAsync(id); |
| | 0 | 120 | | if (validacion == null) |
| | 0 | 121 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | 0 | 122 | | return Ok(ApiResponse<ValidacionInsumosDto>.SuccessResult(validacion)); |
| | | 123 | | } |
| | 0 | 124 | | catch (Exception ex) |
| | | 125 | | { |
| | 0 | 126 | | _logger.LogError(ex, "Error al validar insumos del período {Id}", id); |
| | 0 | 127 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | [HttpPost("{id}/cerrar-insumos")] |
| | | 132 | | public async Task<IActionResult> CerrarInsumos(long id, [FromBody] CerrarInsumosRequest? request = null) |
| | | 133 | | { |
| | | 134 | | try |
| | | 135 | | { |
| | 4 | 136 | | var (success, periodo, validacion, error) = |
| | 4 | 137 | | await _periodoService.CerrarInsumosAsync(id, request?.Confirmaciones); |
| | | 138 | | |
| | 4 | 139 | | if (!success) |
| | | 140 | | { |
| | 3 | 141 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 142 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 143 | | if (error?.Contains("Solo se puede", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 144 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 145 | | if (validacion != null) |
| | 1 | 146 | | return UnprocessableEntity(validacion); |
| | 0 | 147 | | return UnprocessableEntity(ApiResponse<string>.ErrorResult(error ?? "Error al cerrar insumos")); |
| | | 148 | | } |
| | | 149 | | |
| | 1 | 150 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Insumos cerrados. Período listo pa |
| | | 151 | | } |
| | 0 | 152 | | catch (Exception ex) |
| | | 153 | | { |
| | 0 | 154 | | _logger.LogError(ex, "Error al cerrar insumos del período {Id}", id); |
| | 0 | 155 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 156 | | } |
| | 4 | 157 | | } |
| | | 158 | | |
| | | 159 | | [HttpPost("{id}/reabrir-insumos")] |
| | | 160 | | [RequirePermission(PermisoEnum.ReabrirPeriodo)] |
| | | 161 | | public async Task<IActionResult> ReabrirInsumos(long id) |
| | | 162 | | { |
| | | 163 | | try |
| | | 164 | | { |
| | 2 | 165 | | var (success, periodo, error) = await _periodoService.ReabrirInsumosAsync(id); |
| | | 166 | | |
| | 2 | 167 | | if (!success) |
| | | 168 | | { |
| | 1 | 169 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 0 | 170 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 171 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al reabrir insumos")); |
| | | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Insumos reabiertos. Período en est |
| | | 175 | | } |
| | 0 | 176 | | catch (Exception ex) |
| | | 177 | | { |
| | 0 | 178 | | _logger.LogError(ex, "Error al reabrir insumos del período {Id}", id); |
| | 0 | 179 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 180 | | } |
| | 2 | 181 | | } |
| | | 182 | | |
| | | 183 | | [HttpPost("{id}/calcular")] |
| | | 184 | | public async Task<IActionResult> Calcular(long id) |
| | | 185 | | { |
| | | 186 | | try |
| | | 187 | | { |
| | 3 | 188 | | var (success, periodo, error) = await _periodoService.IniciarCalculoAsync(id); |
| | | 189 | | |
| | 3 | 190 | | if (!success) |
| | | 191 | | { |
| | 2 | 192 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 193 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 194 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al iniciar cálculo")); |
| | | 195 | | } |
| | | 196 | | |
| | 1 | 197 | | return Accepted(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Cálculo iniciado. El período |
| | | 198 | | } |
| | 0 | 199 | | catch (Exception ex) |
| | | 200 | | { |
| | 0 | 201 | | _logger.LogError(ex, "Error al iniciar cálculo del período {Id}", id); |
| | 0 | 202 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 203 | | } |
| | 3 | 204 | | } |
| | | 205 | | |
| | | 206 | | [HttpPost("{id}/cerrar")] |
| | | 207 | | [RequirePermission(PermisoEnum.CerrarPeriodo)] |
| | | 208 | | public async Task<IActionResult> Cerrar(long id) |
| | | 209 | | { |
| | | 210 | | try |
| | | 211 | | { |
| | 3 | 212 | | var (success, periodo, error) = await _periodoService.CerrarAsync(id); |
| | | 213 | | |
| | 3 | 214 | | if (!success) |
| | | 215 | | { |
| | 2 | 216 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 217 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 218 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al cerrar período")); |
| | | 219 | | } |
| | | 220 | | |
| | 1 | 221 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Período cerrado definitivamente.") |
| | | 222 | | } |
| | 0 | 223 | | catch (Exception ex) |
| | | 224 | | { |
| | 0 | 225 | | _logger.LogError(ex, "Error al cerrar período {Id}", id); |
| | 0 | 226 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 227 | | } |
| | 3 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static PeriodoDto MapearPeriodo(Periodo periodo) |
| | | 231 | | { |
| | 7 | 232 | | return new PeriodoDto |
| | 7 | 233 | | { |
| | 7 | 234 | | Id = periodo.Id, |
| | 7 | 235 | | Anio = periodo.Anio, |
| | 7 | 236 | | Mes = periodo.Mes, |
| | 7 | 237 | | Estado = periodo.Estado, |
| | 7 | 238 | | SnapshotId = periodo.SnapshotId, |
| | 7 | 239 | | FechaApertura = periodo.FechaApertura, |
| | 7 | 240 | | UsuarioApertura = periodo.UsuarioApertura, |
| | 7 | 241 | | HashSnapshot = periodo.HashSnapshot, |
| | 7 | 242 | | FechaCierreInsumos = periodo.FechaCierreInsumos, |
| | 7 | 243 | | UsuarioCierreInsumosId = periodo.UsuarioCierreInsumosId, |
| | 7 | 244 | | FechaCalculo = periodo.FechaCalculo, |
| | 7 | 245 | | FechaCierre = periodo.FechaCierre, |
| | 7 | 246 | | UsuarioCierreId = periodo.UsuarioCierreId |
| | 7 | 247 | | }; |
| | | 248 | | } |
| | | 249 | | } |