| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | |
| | | 6 | | namespace FAU.API.Controllers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Gestión de configuración IRPF: franjas progresivas y deducciones por funcionario |
| | | 10 | | /// </summary> |
| | | 11 | | [ApiController] |
| | | 12 | | [Route("api/irpf")] |
| | | 13 | | [Authorize(Roles = "Administrador")] |
| | | 14 | | public class IrpfController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IIrpfService _irpfService; |
| | | 17 | | private readonly ILogger<IrpfController> _logger; |
| | | 18 | | |
| | 28 | 19 | | public IrpfController(IIrpfService irpfService, ILogger<IrpfController> logger) |
| | | 20 | | { |
| | 28 | 21 | | _irpfService = irpfService; |
| | 28 | 22 | | _logger = logger; |
| | 28 | 23 | | } |
| | | 24 | | |
| | | 25 | | // ── Franjas ────────────────────────────────────────────────────────────── |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Listar franjas IRPF vigentes |
| | | 29 | | /// GET /api/irpf/franjas?fecha=2025-01-01 |
| | | 30 | | /// </summary> |
| | | 31 | | [HttpGet("franjas")] |
| | | 32 | | public async Task<IActionResult> GetFranjas([FromQuery] DateTime? fecha = null) |
| | | 33 | | { |
| | | 34 | | try |
| | | 35 | | { |
| | 2 | 36 | | var franjas = await _irpfService.GetFranjasVigentesAsync(fecha); |
| | 1 | 37 | | return Ok(ApiResponse<object>.SuccessResult(franjas)); |
| | | 38 | | } |
| | 1 | 39 | | catch (Exception ex) |
| | | 40 | | { |
| | 1 | 41 | | _logger.LogError(ex, "Error al obtener franjas IRPF"); |
| | 1 | 42 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 43 | | } |
| | 2 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Obtener franja IRPF por ID |
| | | 48 | | /// GET /api/irpf/franjas/{id} |
| | | 49 | | /// </summary> |
| | | 50 | | [HttpGet("franjas/{id}")] |
| | | 51 | | public async Task<IActionResult> GetFranja(long id) |
| | | 52 | | { |
| | | 53 | | try |
| | | 54 | | { |
| | 3 | 55 | | var franja = await _irpfService.GetFranjaByIdAsync(id); |
| | 2 | 56 | | if (franja == null) |
| | 1 | 57 | | return NotFound(ApiResponse<string>.ErrorResult("Franja no encontrada")); |
| | | 58 | | |
| | 1 | 59 | | return Ok(ApiResponse<object>.SuccessResult(franja)); |
| | | 60 | | } |
| | 1 | 61 | | catch (Exception ex) |
| | | 62 | | { |
| | 1 | 63 | | _logger.LogError(ex, "Error al obtener franja IRPF {Id}", id); |
| | 1 | 64 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 65 | | } |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Crear nueva franja IRPF |
| | | 70 | | /// POST /api/irpf/franjas |
| | | 71 | | /// </summary> |
| | | 72 | | [HttpPost("franjas")] |
| | | 73 | | public async Task<IActionResult> CreateFranja([FromBody] CreateFranjaIrpfRequest request) |
| | | 74 | | { |
| | | 75 | | try |
| | | 76 | | { |
| | 3 | 77 | | var (success, franja, error) = await _irpfService.CreateFranjaAsync(request); |
| | 3 | 78 | | if (!success) |
| | | 79 | | { |
| | 2 | 80 | | if (error?.Contains("Ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 81 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 82 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear franja")); |
| | | 83 | | } |
| | | 84 | | |
| | 1 | 85 | | return CreatedAtAction( |
| | 1 | 86 | | nameof(GetFranja), |
| | 1 | 87 | | new { id = franja!.Id }, |
| | 1 | 88 | | ApiResponse<object>.SuccessResult(new { id = franja.Id, creado = true }, "Franja IRPF creada exitosament |
| | | 89 | | } |
| | 0 | 90 | | catch (Exception ex) |
| | | 91 | | { |
| | 0 | 92 | | _logger.LogError(ex, "Error al crear franja IRPF"); |
| | 0 | 93 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 94 | | } |
| | 3 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Actualizar franja IRPF |
| | | 99 | | /// PUT /api/irpf/franjas/{id} |
| | | 100 | | /// </summary> |
| | | 101 | | [HttpPut("franjas/{id}")] |
| | | 102 | | public async Task<IActionResult> UpdateFranja(long id, [FromBody] CreateFranjaIrpfRequest request) |
| | | 103 | | { |
| | | 104 | | try |
| | | 105 | | { |
| | 5 | 106 | | var (success, _, error) = await _irpfService.UpdateFranjaAsync(id, request); |
| | 4 | 107 | | if (!success) |
| | | 108 | | { |
| | 3 | 109 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 110 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 111 | | if (error?.Contains("Ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 112 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 113 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar franja")); |
| | | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Franja IRPF actualizada exitosament |
| | | 117 | | } |
| | 1 | 118 | | catch (Exception ex) |
| | | 119 | | { |
| | 1 | 120 | | _logger.LogError(ex, "Error al actualizar franja IRPF {Id}", id); |
| | 1 | 121 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 122 | | } |
| | 5 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Eliminar franja IRPF |
| | | 127 | | /// DELETE /api/irpf/franjas/{id} |
| | | 128 | | /// </summary> |
| | | 129 | | [HttpDelete("franjas/{id}")] |
| | | 130 | | public async Task<IActionResult> DeleteFranja(long id) |
| | | 131 | | { |
| | | 132 | | try |
| | | 133 | | { |
| | 3 | 134 | | var (success, error) = await _irpfService.DeleteFranjaAsync(id); |
| | 2 | 135 | | if (!success) |
| | 1 | 136 | | return NotFound(ApiResponse<string>.ErrorResult(error ?? "Franja no encontrada")); |
| | | 137 | | |
| | 1 | 138 | | return NoContent(); |
| | | 139 | | } |
| | 1 | 140 | | catch (Exception ex) |
| | | 141 | | { |
| | 1 | 142 | | _logger.LogError(ex, "Error al eliminar franja IRPF {Id}", id); |
| | 1 | 143 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 144 | | } |
| | 3 | 145 | | } |
| | | 146 | | |
| | | 147 | | // ── Deducciones ────────────────────────────────────────────────────────── |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Listar deducciones IRPF con filtros y paginación |
| | | 151 | | /// GET /api/irpf/deducciones?personaId=&tipo=&activo=&page=&pageSize= |
| | | 152 | | /// </summary> |
| | | 153 | | [HttpGet("deducciones")] |
| | | 154 | | public async Task<IActionResult> GetDeducciones( |
| | | 155 | | [FromQuery] long? personaId = null, |
| | | 156 | | [FromQuery] string? tipo = null, |
| | | 157 | | [FromQuery] bool? activo = null, |
| | | 158 | | [FromQuery] int page = 1, |
| | | 159 | | [FromQuery] int pageSize = 20) |
| | | 160 | | { |
| | | 161 | | try |
| | | 162 | | { |
| | 2 | 163 | | if (page < 1) page = 1; |
| | 2 | 164 | | if (pageSize < 1 || pageSize > 100) pageSize = 20; |
| | | 165 | | |
| | 2 | 166 | | var resultado = await _irpfService.GetDeduccionesPagedAsync(page, pageSize, personaId, tipo, activo); |
| | 1 | 167 | | return Ok(ApiResponse<object>.SuccessResult(resultado)); |
| | | 168 | | } |
| | 1 | 169 | | catch (Exception ex) |
| | | 170 | | { |
| | 1 | 171 | | _logger.LogError(ex, "Error al obtener deducciones IRPF"); |
| | 1 | 172 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 173 | | } |
| | 2 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Obtener deducción IRPF por ID |
| | | 178 | | /// GET /api/irpf/deducciones/{id} |
| | | 179 | | /// </summary> |
| | | 180 | | [HttpGet("deducciones/{id}")] |
| | | 181 | | public async Task<IActionResult> GetDeduccion(long id) |
| | | 182 | | { |
| | | 183 | | try |
| | | 184 | | { |
| | 3 | 185 | | var deduccion = await _irpfService.GetDeduccionByIdAsync(id); |
| | 2 | 186 | | if (deduccion == null) |
| | 1 | 187 | | return NotFound(ApiResponse<string>.ErrorResult("Deducción no encontrada")); |
| | | 188 | | |
| | 1 | 189 | | return Ok(ApiResponse<object>.SuccessResult(deduccion)); |
| | | 190 | | } |
| | 1 | 191 | | catch (Exception ex) |
| | | 192 | | { |
| | 1 | 193 | | _logger.LogError(ex, "Error al obtener deducción IRPF {Id}", id); |
| | 1 | 194 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 195 | | } |
| | 3 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Crear deducción IRPF para un funcionario |
| | | 200 | | /// POST /api/irpf/deducciones |
| | | 201 | | /// </summary> |
| | | 202 | | [HttpPost("deducciones")] |
| | | 203 | | public async Task<IActionResult> CreateDeduccion([FromBody] CreateDeduccionIrpfRequest request) |
| | | 204 | | { |
| | | 205 | | try |
| | | 206 | | { |
| | 3 | 207 | | var (success, deduccion, error) = await _irpfService.CreateDeduccionAsync(request); |
| | 2 | 208 | | if (!success) |
| | | 209 | | { |
| | 1 | 210 | | if (error?.Contains("Ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 211 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 212 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear deducción")); |
| | | 213 | | } |
| | | 214 | | |
| | 1 | 215 | | return CreatedAtAction( |
| | 1 | 216 | | nameof(GetDeduccion), |
| | 1 | 217 | | new { id = deduccion!.Id }, |
| | 1 | 218 | | ApiResponse<object>.SuccessResult(new { id = deduccion.Id, creado = true }, "Deducción IRPF creada exito |
| | | 219 | | } |
| | 1 | 220 | | catch (Exception ex) |
| | | 221 | | { |
| | 1 | 222 | | _logger.LogError(ex, "Error al crear deducción IRPF"); |
| | 1 | 223 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 224 | | } |
| | 3 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Actualizar deducción IRPF |
| | | 229 | | /// PUT /api/irpf/deducciones/{id} |
| | | 230 | | /// </summary> |
| | | 231 | | [HttpPut("deducciones/{id}")] |
| | | 232 | | public async Task<IActionResult> UpdateDeduccion(long id, [FromBody] UpdateDeduccionIrpfRequest request) |
| | | 233 | | { |
| | | 234 | | try |
| | | 235 | | { |
| | 4 | 236 | | var (success, _, error) = await _irpfService.UpdateDeduccionAsync(id, request); |
| | 3 | 237 | | if (!success) |
| | | 238 | | { |
| | 2 | 239 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 240 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 241 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar deducción")); |
| | | 242 | | } |
| | | 243 | | |
| | 1 | 244 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Deducción IRPF actualizada exitosam |
| | | 245 | | } |
| | 1 | 246 | | catch (Exception ex) |
| | | 247 | | { |
| | 1 | 248 | | _logger.LogError(ex, "Error al actualizar deducción IRPF {Id}", id); |
| | 1 | 249 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 250 | | } |
| | 4 | 251 | | } |
| | | 252 | | } |