| | | 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 | | [ApiController] |
| | | 9 | | [Route("api/instructivos")] |
| | | 10 | | [Authorize(Roles = "Administrador")] |
| | | 11 | | public class InstructivosBeneficiosController : ControllerBase |
| | | 12 | | { |
| | | 13 | | private readonly IInstructivoBeneficiosService _service; |
| | | 14 | | private readonly ILogger<InstructivosBeneficiosController> _logger; |
| | | 15 | | |
| | | 16 | | public InstructivosBeneficiosController( |
| | | 17 | | IInstructivoBeneficiosService service, |
| | | 18 | | ILogger<InstructivosBeneficiosController> logger) |
| | | 19 | | { |
| | | 20 | | _service = service; |
| | | 21 | | _logger = logger; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // ── Asignación Familiar ─────────────────────────────────────────────────── |
| | | 25 | | |
| | | 26 | | [HttpGet("asig-familiar")] |
| | | 27 | | public async Task<IActionResult> GetAsigFamiliar([FromQuery] bool vigente = true) |
| | | 28 | | { |
| | | 29 | | try |
| | | 30 | | { |
| | | 31 | | var items = await _service.ObtenerAsigFamiliarAsync(vigente); |
| | | 32 | | return Ok(ApiResponse<object>.SuccessResult(items)); |
| | | 33 | | } |
| | | 34 | | catch (Exception ex) |
| | | 35 | | { |
| | | 36 | | _logger.LogError(ex, "Error al obtener instructivo asig. familiar"); |
| | | 37 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static DateTime ToFirstDayUtc(string vigenteDesdeStr) |
| | | 42 | | { |
| | | 43 | | // Parse YYYY-MM-DD directly to avoid timezone conversion issues |
| | | 44 | | // The frontend sends "YYYY-MM-01T00:00:00Z" so we extract year and month |
| | | 45 | | if (DateTime.TryParse(vigenteDesdeStr, out var dt)) |
| | | 46 | | return DateTime.SpecifyKind(new DateTime(dt.Year, dt.Month, 1), DateTimeKind.Utc); |
| | | 47 | | throw new ArgumentException($"Formato de fecha inválido: {vigenteDesdeStr}"); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | [HttpPost("asig-familiar")] |
| | | 51 | | public async Task<IActionResult> PostAsigFamiliar([FromBody] RegistrarAsigFamiliarRequest request) |
| | | 52 | | { |
| | | 53 | | try |
| | | 54 | | { |
| | | 55 | | var items = await _service.RegistrarAsigFamiliarAsync( |
| | | 56 | | ToFirstDayUtc(request.VigenteDesde.ToString()), request.Franja8, request.Franja16, request.Discapacidad) |
| | | 57 | | return Ok(ApiResponse<object>.SuccessResult(items, "Instructivo Asig. Familiar registrado correctamente")); |
| | | 58 | | } |
| | | 59 | | catch (ArgumentException ex) |
| | | 60 | | { |
| | | 61 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 62 | | } |
| | | 63 | | catch (Exception ex) |
| | | 64 | | { |
| | | 65 | | _logger.LogError(ex, "Error al registrar instructivo asig. familiar"); |
| | | 66 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // ── PSF ─────────────────────────────────────────────────────────────────── |
| | | 71 | | |
| | | 72 | | [HttpGet("psf")] |
| | | 73 | | public async Task<IActionResult> GetPsf([FromQuery] bool vigente = true) |
| | | 74 | | { |
| | | 75 | | try |
| | | 76 | | { |
| | | 77 | | var items = await _service.ObtenerPsfAsync(vigente); |
| | | 78 | | return Ok(ApiResponse<object>.SuccessResult(items)); |
| | | 79 | | } |
| | | 80 | | catch (Exception ex) |
| | | 81 | | { |
| | | 82 | | _logger.LogError(ex, "Error al obtener instructivo PSF"); |
| | | 83 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | [HttpPost("psf")] |
| | | 88 | | public async Task<IActionResult> PostPsf([FromBody] RegistrarPsfRequest request) |
| | | 89 | | { |
| | | 90 | | try |
| | | 91 | | { |
| | | 92 | | var categorias = request.Categorias |
| | | 93 | | .Select(c => ((short)c.NumeroCategoria, c.Descripcion, c.Importe)) |
| | | 94 | | .ToList() |
| | | 95 | | .AsReadOnly(); |
| | | 96 | | var items = await _service.RegistrarPsfAsync( |
| | | 97 | | ToFirstDayUtc(request.VigenteDesde.ToString()), categorias); |
| | | 98 | | return Ok(ApiResponse<object>.SuccessResult(items, "Instructivo PSF registrado correctamente")); |
| | | 99 | | } |
| | | 100 | | catch (ArgumentException ex) { return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); } |
| | | 101 | | catch (InvalidOperationException ex) { return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); } |
| | | 102 | | catch (Exception ex) |
| | | 103 | | { |
| | | 104 | | _logger.LogError(ex, "Error al registrar instructivo PSF"); |
| | | 105 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // ── Hogar Constituido ───────────────────────────────────────────────────── |
| | | 110 | | |
| | | 111 | | [HttpGet("hogar")] |
| | | 112 | | public async Task<IActionResult> GetHogar([FromQuery] bool vigente = true) |
| | | 113 | | { |
| | | 114 | | try |
| | | 115 | | { |
| | | 116 | | var items = await _service.ObtenerHogarAsync(vigente); |
| | | 117 | | return Ok(ApiResponse<object>.SuccessResult(items)); |
| | | 118 | | } |
| | | 119 | | catch (Exception ex) |
| | | 120 | | { |
| | | 121 | | _logger.LogError(ex, "Error al obtener tabla hogar constituido"); |
| | | 122 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | [HttpPost("hogar")] |
| | | 127 | | public async Task<IActionResult> PostHogar([FromBody] RegistrarHogarRequest request) |
| | | 128 | | { |
| | | 129 | | try |
| | | 130 | | { |
| | | 131 | | var tramos = request.Tramos |
| | | 132 | | .Select(t => (t.HastaHaberes, t.Importe, t.RefPorcentaje)) |
| | | 133 | | .ToList() |
| | | 134 | | .AsReadOnly(); |
| | | 135 | | var items = await _service.RegistrarHogarAsync( |
| | | 136 | | ToFirstDayUtc(request.VigenteDesde.ToString()), tramos); |
| | | 137 | | return Ok(ApiResponse<object>.SuccessResult(items, "Tabla Hogar Constituido registrada correctamente")); |
| | | 138 | | } |
| | | 139 | | catch (ArgumentException ex) { return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); } |
| | | 140 | | catch (InvalidOperationException ex) { return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); } |
| | | 141 | | catch (Exception ex) |
| | | 142 | | { |
| | | 143 | | _logger.LogError(ex, "Error al registrar tabla hogar"); |
| | | 144 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | // ── Request DTOs ────────────────────────────────────────────────────────────── |
| | | 150 | | |
| | | 151 | | public class RegistrarAsigFamiliarRequest |
| | | 152 | | { |
| | | 153 | | public DateTime VigenteDesde { get; set; } |
| | | 154 | | public decimal Franja8 { get; set; } |
| | | 155 | | public decimal Franja16 { get; set; } |
| | | 156 | | public decimal Discapacidad { get; set; } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | public class RegistrarPsfRequest |
| | | 160 | | { |
| | | 161 | | public DateTime VigenteDesde { get; set; } |
| | | 162 | | public List<PsfCategoriaRequest> Categorias { get; set; } = []; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | public class PsfCategoriaRequest |
| | | 166 | | { |
| | | 167 | | public int NumeroCategoria { get; set; } |
| | | 168 | | public string Descripcion { get; set; } = string.Empty; |
| | | 169 | | public decimal Importe { get; set; } |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | public class RegistrarHogarRequest |
| | | 173 | | { |
| | | 174 | | public DateTime VigenteDesde { get; set; } |
| | | 175 | | public List<HogarTramoRequest> Tramos { get; set; } = []; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public class HogarTramoRequest |
| | | 179 | | { |
| | 0 | 180 | | public decimal? HastaHaberes { get; set; } |
| | 0 | 181 | | public decimal Importe { get; set; } |
| | 0 | 182 | | public string? RefPorcentaje { get; set; } |
| | | 183 | | } |