| | | 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.ObtenerActivoAsync(); |
| | 2 | 63 | | if (periodo == null) |
| | | 64 | | { |
| | 1 | 65 | | return NotFound(ApiResponse<string>.ErrorResult("No hay un período abierto")); |
| | | 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 | | [HttpPost("{id}/cerrar-insumos")] |
| | | 98 | | public async Task<IActionResult> CerrarInsumos(long id) |
| | | 99 | | { |
| | | 100 | | try |
| | | 101 | | { |
| | 4 | 102 | | var (success, periodo, error) = await _periodoService.CerrarInsumosAsync(id); |
| | | 103 | | |
| | 4 | 104 | | if (!success) |
| | | 105 | | { |
| | 3 | 106 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 107 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 108 | | if (error?.Contains("Solo se puede", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 109 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 110 | | return UnprocessableEntity(ApiResponse<string>.ErrorResult(error ?? "Error al cerrar insumos")); |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Insumos cerrados. Período listo pa |
| | | 114 | | } |
| | 0 | 115 | | catch (Exception ex) |
| | | 116 | | { |
| | 0 | 117 | | _logger.LogError(ex, "Error al cerrar insumos del período {Id}", id); |
| | 0 | 118 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 119 | | } |
| | 4 | 120 | | } |
| | | 121 | | |
| | | 122 | | [HttpPost("{id}/reabrir-insumos")] |
| | | 123 | | [RequirePermission(PermisoEnum.ReabrirPeriodo)] |
| | | 124 | | public async Task<IActionResult> ReabrirInsumos(long id) |
| | | 125 | | { |
| | | 126 | | try |
| | | 127 | | { |
| | 2 | 128 | | var (success, periodo, error) = await _periodoService.ReabrirInsumosAsync(id); |
| | | 129 | | |
| | 2 | 130 | | if (!success) |
| | | 131 | | { |
| | 1 | 132 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 0 | 133 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 134 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al reabrir insumos")); |
| | | 135 | | } |
| | | 136 | | |
| | 1 | 137 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Insumos reabiertos. Período en est |
| | | 138 | | } |
| | 0 | 139 | | catch (Exception ex) |
| | | 140 | | { |
| | 0 | 141 | | _logger.LogError(ex, "Error al reabrir insumos del período {Id}", id); |
| | 0 | 142 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 143 | | } |
| | 2 | 144 | | } |
| | | 145 | | |
| | | 146 | | [HttpPost("{id}/calcular")] |
| | | 147 | | public async Task<IActionResult> Calcular(long id) |
| | | 148 | | { |
| | | 149 | | try |
| | | 150 | | { |
| | 3 | 151 | | var (success, periodo, error) = await _periodoService.IniciarCalculoAsync(id); |
| | | 152 | | |
| | 3 | 153 | | if (!success) |
| | | 154 | | { |
| | 2 | 155 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 156 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 157 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al iniciar cálculo")); |
| | | 158 | | } |
| | | 159 | | |
| | 1 | 160 | | return Accepted(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Cálculo iniciado. El período |
| | | 161 | | } |
| | 0 | 162 | | catch (Exception ex) |
| | | 163 | | { |
| | 0 | 164 | | _logger.LogError(ex, "Error al iniciar cálculo del período {Id}", id); |
| | 0 | 165 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 166 | | } |
| | 3 | 167 | | } |
| | | 168 | | |
| | | 169 | | [HttpPost("{id}/cerrar")] |
| | | 170 | | [RequirePermission(PermisoEnum.CerrarPeriodo)] |
| | | 171 | | public async Task<IActionResult> Cerrar(long id) |
| | | 172 | | { |
| | | 173 | | try |
| | | 174 | | { |
| | 3 | 175 | | var (success, periodo, error) = await _periodoService.CerrarAsync(id); |
| | | 176 | | |
| | 3 | 177 | | if (!success) |
| | | 178 | | { |
| | 2 | 179 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 180 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 181 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "Error al cerrar período")); |
| | | 182 | | } |
| | | 183 | | |
| | 1 | 184 | | return Ok(ApiResponse<PeriodoDto>.SuccessResult(MapearPeriodo(periodo!), "Período cerrado definitivamente.") |
| | | 185 | | } |
| | 0 | 186 | | catch (Exception ex) |
| | | 187 | | { |
| | 0 | 188 | | _logger.LogError(ex, "Error al cerrar período {Id}", id); |
| | 0 | 189 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 190 | | } |
| | 3 | 191 | | } |
| | | 192 | | |
| | | 193 | | private static PeriodoDto MapearPeriodo(Periodo periodo) |
| | | 194 | | { |
| | 7 | 195 | | return new PeriodoDto |
| | 7 | 196 | | { |
| | 7 | 197 | | Id = periodo.Id, |
| | 7 | 198 | | Anio = periodo.Anio, |
| | 7 | 199 | | Mes = periodo.Mes, |
| | 7 | 200 | | Estado = periodo.Estado, |
| | 7 | 201 | | SnapshotId = periodo.SnapshotId, |
| | 7 | 202 | | FechaApertura = periodo.FechaApertura, |
| | 7 | 203 | | UsuarioApertura = periodo.UsuarioApertura, |
| | 7 | 204 | | HashSnapshot = periodo.HashSnapshot, |
| | 7 | 205 | | FechaCierreInsumos = periodo.FechaCierreInsumos, |
| | 7 | 206 | | UsuarioCierreInsumosId = periodo.UsuarioCierreInsumosId, |
| | 7 | 207 | | FechaCalculo = periodo.FechaCalculo, |
| | 7 | 208 | | FechaCierre = periodo.FechaCierre, |
| | 7 | 209 | | UsuarioCierreId = periodo.UsuarioCierreId |
| | 7 | 210 | | }; |
| | | 211 | | } |
| | | 212 | | } |