| | | 1 | | using System.Security.Claims; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | [ApiController] |
| | | 10 | | [Route("api/parametros-liquidacion")] |
| | | 11 | | [Authorize(Roles = "Administrador")] |
| | | 12 | | public class ParametrosLiquidacionController : ControllerBase |
| | | 13 | | { |
| | | 14 | | private readonly IParametroLiquidacionService _service; |
| | | 15 | | private readonly ILogger<ParametrosLiquidacionController> _logger; |
| | | 16 | | |
| | 6 | 17 | | public ParametrosLiquidacionController( |
| | 6 | 18 | | IParametroLiquidacionService service, |
| | 6 | 19 | | ILogger<ParametrosLiquidacionController> logger) |
| | | 20 | | { |
| | 6 | 21 | | _service = service; |
| | 6 | 22 | | _logger = logger; |
| | 6 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Obtener parámetros de liquidación actuales |
| | | 27 | | /// GET /api/parametros-liquidacion |
| | | 28 | | /// </summary> |
| | | 29 | | [HttpGet] |
| | | 30 | | public async Task<IActionResult> GetActual() |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | 2 | 34 | | var parametro = await _service.ObtenerActualAsync(); |
| | 2 | 35 | | if (parametro == null) |
| | 1 | 36 | | return NotFound(ApiResponse<string>.ErrorResult("No existe configuración de parámetros")); |
| | | 37 | | |
| | 1 | 38 | | return Ok(ApiResponse<object>.SuccessResult(parametro)); |
| | | 39 | | } |
| | 0 | 40 | | catch (Exception ex) |
| | | 41 | | { |
| | 0 | 42 | | _logger.LogError(ex, "Error al obtener parámetros de liquidación"); |
| | 0 | 43 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 44 | | } |
| | 2 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Guardar parámetros de liquidación (crear o actualizar) |
| | | 49 | | /// PUT /api/parametros-liquidacion |
| | | 50 | | /// </summary> |
| | | 51 | | [HttpPut] |
| | | 52 | | public async Task<IActionResult> Guardar([FromBody] ConfigurarParametrosLiquidacionRequest request) |
| | | 53 | | { |
| | | 54 | | try |
| | | 55 | | { |
| | 2 | 56 | | var usuarioIdStr = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
| | 2 | 57 | | if (!long.TryParse(usuarioIdStr, out var usuarioId)) |
| | 0 | 58 | | return Unauthorized(ApiResponse<string>.ErrorResult("No se pudo identificar al usuario")); |
| | | 59 | | |
| | 2 | 60 | | var (success, parametro, error) = await _service.GuardarAsync(usuarioId, request); |
| | 2 | 61 | | if (!success) |
| | 1 | 62 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al guardar parámetros")); |
| | | 63 | | |
| | 1 | 64 | | return Ok(ApiResponse<object>.SuccessResult(parametro, "Parámetros de liquidación guardados exitosamente")); |
| | | 65 | | } |
| | 0 | 66 | | catch (Exception ex) |
| | | 67 | | { |
| | 0 | 68 | | _logger.LogError(ex, "Error al guardar parámetros de liquidación"); |
| | 0 | 69 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 70 | | } |
| | 2 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Obtener franjas IRPF actuales |
| | | 75 | | /// GET /api/parametros-liquidacion/franjas-irpf |
| | | 76 | | /// </summary> |
| | | 77 | | [HttpGet("franjas-irpf")] |
| | | 78 | | public async Task<IActionResult> GetFranjas() |
| | | 79 | | { |
| | | 80 | | try |
| | | 81 | | { |
| | 1 | 82 | | var configuracion = await _service.ObtenerFranjasAsync(); |
| | 1 | 83 | | if (configuracion == null) |
| | 0 | 84 | | return NotFound(ApiResponse<string>.ErrorResult("No existen franjas IRPF configuradas")); |
| | | 85 | | |
| | 1 | 86 | | return Ok(ApiResponse<object>.SuccessResult(configuracion)); |
| | | 87 | | } |
| | 0 | 88 | | catch (Exception ex) |
| | | 89 | | { |
| | 0 | 90 | | _logger.LogError(ex, "Error al obtener franjas IRPF"); |
| | 0 | 91 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 92 | | } |
| | 1 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Guardar franjas IRPF (reemplaza todas las existentes) |
| | | 97 | | /// PUT /api/parametros-liquidacion/franjas-irpf |
| | | 98 | | /// </summary> |
| | | 99 | | [HttpPut("franjas-irpf")] |
| | | 100 | | public async Task<IActionResult> GuardarFranjas([FromBody] ConfigurarFranjasIrpfRequest request) |
| | | 101 | | { |
| | | 102 | | try |
| | | 103 | | { |
| | 1 | 104 | | var (success, configuracion, error) = await _service.GuardarFranjasAsync(request); |
| | 1 | 105 | | if (!success) |
| | 1 | 106 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al guardar franjas IRPF")); |
| | | 107 | | |
| | 0 | 108 | | return Ok(ApiResponse<object>.SuccessResult(configuracion, "Franjas IRPF guardadas exitosamente")); |
| | | 109 | | } |
| | 0 | 110 | | catch (Exception ex) |
| | | 111 | | { |
| | 0 | 112 | | _logger.LogError(ex, "Error al guardar franjas IRPF"); |
| | 0 | 113 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 114 | | } |
| | 1 | 115 | | } |
| | | 116 | | } |