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