| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Exceptions; |
| | | 4 | | using FAU.Logica.Services; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | using System.ComponentModel.DataAnnotations; |
| | | 9 | | |
| | | 10 | | namespace FAU.API.Controllers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gestión del Formulario 3100 (deducciones IRPF) y personas a cargo |
| | | 14 | | /// </summary> |
| | | 15 | | [ApiController] |
| | | 16 | | [Route("api/form3100")] |
| | | 17 | | [Authorize] |
| | | 18 | | [Produces("application/json")] |
| | | 19 | | public class Form3100Controller : ControllerBase |
| | | 20 | | { |
| | | 21 | | private readonly IForm3100Service _form3100Service; |
| | | 22 | | private readonly ICurrentUserContext _currentUser; |
| | | 23 | | private readonly ILogger<Form3100Controller> _logger; |
| | | 24 | | |
| | 17 | 25 | | public Form3100Controller( |
| | 17 | 26 | | IForm3100Service form3100Service, |
| | 17 | 27 | | ICurrentUserContext currentUser, |
| | 17 | 28 | | ILogger<Form3100Controller> logger) |
| | | 29 | | { |
| | 17 | 30 | | _form3100Service = form3100Service; |
| | 17 | 31 | | _currentUser = currentUser; |
| | 17 | 32 | | _logger = logger; |
| | 17 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Crear una nueva versión del Form 3100 |
| | | 37 | | /// POST /api/form3100 |
| | | 38 | | /// </summary> |
| | | 39 | | [HttpPost] |
| | | 40 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 41 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 42 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 43 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 44 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 45 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status423Locked)] |
| | | 46 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 47 | | public async Task<IActionResult> Crear([FromBody] CrearForm3100Request request) |
| | | 48 | | { |
| | | 49 | | try |
| | | 50 | | { |
| | 6 | 51 | | if (string.IsNullOrWhiteSpace(request.CedulaTitular)) |
| | 0 | 52 | | return BadRequest(ApiResponse<string>.ErrorResult("cedulaTitular es requerida")); |
| | | 53 | | |
| | 6 | 54 | | if (request.VigenteDesdeMes < 1 || request.VigenteDesdeMes > 12) |
| | 1 | 55 | | return BadRequest(ApiResponse<string>.ErrorResult("vigenteDesdeMes debe estar entre 1 y 12")); |
| | | 56 | | |
| | 5 | 57 | | if (request.VigenteDesdeAnio < 2000 || request.VigenteDesdeAnio > 2100) |
| | 0 | 58 | | return BadRequest(ApiResponse<string>.ErrorResult("vigenteDesdeAnio inválido")); |
| | | 59 | | |
| | 5 | 60 | | if (request.CappuCategoria.HasValue && (request.CappuCategoria < 1 || request.CappuCategoria > 10)) |
| | 1 | 61 | | return BadRequest(ApiResponse<string>.ErrorResult("cappuCategoria debe estar entre 1 y 10")); |
| | | 62 | | |
| | 4 | 63 | | if (request.CappuImporte < 0) |
| | 0 | 64 | | return BadRequest(ApiResponse<string>.ErrorResult("cappuImporte no puede ser negativo")); |
| | | 65 | | |
| | 4 | 66 | | if (request.ReintegroAporte < 0) |
| | 0 | 67 | | return BadRequest(ApiResponse<string>.ErrorResult("reintegroAporte no puede ser negativo")); |
| | | 68 | | |
| | 4 | 69 | | if (!string.IsNullOrEmpty(request.FondoSolidaridad) && |
| | 4 | 70 | | !new[] { "0.5_BPC", "1_BPC", "2_BPC" }.Contains(request.FondoSolidaridad)) |
| | 0 | 71 | | return BadRequest(ApiResponse<string>.ErrorResult("fondoSolidaridad debe ser '0.5_BPC', '1_BPC' o '2_BPC |
| | | 72 | | |
| | 4 | 73 | | var form = new Form3100 |
| | 4 | 74 | | { |
| | 4 | 75 | | CappuCategoria = request.CappuCategoria, |
| | 4 | 76 | | CappuImporte = request.CappuImporte, |
| | 4 | 77 | | ReintegroAporte = request.ReintegroAporte, |
| | 4 | 78 | | FondoSolidaridad = request.FondoSolidaridad, |
| | 4 | 79 | | AdicionalFondoSolidaridad = request.AdicionalFondoSolidaridad, |
| | 4 | 80 | | AplicaMinimoNoImponible = request.AplicaMinimoNoImponible, |
| | 4 | 81 | | ConyugeNombre = request.ConyugeNombre, |
| | 4 | 82 | | ConyugeApellido = request.ConyugeApellido, |
| | 4 | 83 | | ConyugeFechaNacimiento = request.ConyugeFechaNacimiento, |
| | 4 | 84 | | ConyugeCedula = request.ConyugeCedula, |
| | 4 | 85 | | ConyugeSexo = request.ConyugeSexo, |
| | 4 | 86 | | ConyugeNacionalidad = request.ConyugeNacionalidad, |
| | 4 | 87 | | VigenteDesde_Mes = (short)request.VigenteDesdeMes, |
| | 4 | 88 | | VigenteDesde_Anio = (short)request.VigenteDesdeAnio |
| | 4 | 89 | | }; |
| | | 90 | | |
| | 4 | 91 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 4 | 92 | | var creado = await _form3100Service.CrearAsync(request.CedulaTitular, form, usuarioId); |
| | | 93 | | |
| | 1 | 94 | | return CreatedAtAction( |
| | 1 | 95 | | nameof(GetById), |
| | 1 | 96 | | new { id = creado.Id }, |
| | 1 | 97 | | ApiResponse<object>.SuccessResult(new { id = creado.Id, creado = true }, "Form 3100 creado exitosamente" |
| | | 98 | | } |
| | 1 | 99 | | catch (ArgumentException ex) |
| | | 100 | | { |
| | 1 | 101 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 102 | | } |
| | 1 | 103 | | catch (PeriodoBloqueadoException ex) |
| | | 104 | | { |
| | 1 | 105 | | return StatusCode(StatusCodes.Status423Locked, ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 106 | | } |
| | 1 | 107 | | catch (InvalidOperationException ex) |
| | | 108 | | { |
| | 1 | 109 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 110 | | } |
| | 0 | 111 | | catch (Exception ex) |
| | | 112 | | { |
| | 0 | 113 | | _logger.LogError(ex, "Error al crear Form 3100"); |
| | 0 | 114 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 115 | | } |
| | 6 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Obtener historial de versiones del Form 3100 por cédula |
| | | 120 | | /// GET /api/form3100?cedulaTitular=...&vigenteDesdeAnio=...&vigenteDesdeMes=... |
| | | 121 | | /// </summary> |
| | | 122 | | [HttpGet] |
| | | 123 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 124 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 125 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 126 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 127 | | public async Task<IActionResult> GetHistorial( |
| | | 128 | | [FromQuery] string cedulaTitular, |
| | | 129 | | [FromQuery] int? vigenteDesdeAnio = null, |
| | | 130 | | [FromQuery] int? vigenteDesdeMes = null) |
| | | 131 | | { |
| | | 132 | | try |
| | | 133 | | { |
| | 2 | 134 | | if (string.IsNullOrWhiteSpace(cedulaTitular)) |
| | 1 | 135 | | return BadRequest(ApiResponse<string>.ErrorResult("cedulaTitular es requerida")); |
| | | 136 | | |
| | 1 | 137 | | var historial = await _form3100Service.GetHistorialAsync(cedulaTitular, vigenteDesdeAnio, vigenteDesdeMes); |
| | | 138 | | |
| | 1 | 139 | | var response = historial.Select(MapToDto); |
| | 1 | 140 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 141 | | } |
| | 0 | 142 | | catch (Exception ex) |
| | | 143 | | { |
| | 0 | 144 | | _logger.LogError(ex, "Error al obtener historial Form 3100"); |
| | 0 | 145 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 146 | | } |
| | 2 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Obtener versión específica del Form 3100 por id |
| | | 151 | | /// GET /api/form3100/{id} |
| | | 152 | | /// </summary> |
| | | 153 | | [HttpGet("{id}")] |
| | | 154 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 155 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 156 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 157 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 158 | | public async Task<IActionResult> GetById(long id) |
| | | 159 | | { |
| | | 160 | | try |
| | | 161 | | { |
| | 2 | 162 | | var form = await _form3100Service.GetByIdAsync(id); |
| | 2 | 163 | | if (form == null) |
| | 1 | 164 | | return NotFound(ApiResponse<string>.ErrorResult("Form 3100 no encontrado")); |
| | | 165 | | |
| | 1 | 166 | | return Ok(ApiResponse<object>.SuccessResult(MapToDto(form))); |
| | | 167 | | } |
| | 0 | 168 | | catch (Exception ex) |
| | | 169 | | { |
| | 0 | 170 | | _logger.LogError(ex, "Error al obtener Form 3100 {Id}", id); |
| | 0 | 171 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 172 | | } |
| | 2 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Obtener versión vigente del Form 3100 para un período |
| | | 177 | | /// GET /api/form3100/vigente?cedulaTitular=...&anio=...&mes=... |
| | | 178 | | /// </summary> |
| | | 179 | | [HttpGet("vigente")] |
| | | 180 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 181 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 182 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 183 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 184 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 185 | | public async Task<IActionResult> GetVigente( |
| | | 186 | | [FromQuery] string cedulaTitular, |
| | | 187 | | [FromQuery] int anio, |
| | | 188 | | [FromQuery] int mes) |
| | | 189 | | { |
| | | 190 | | try |
| | | 191 | | { |
| | 2 | 192 | | if (string.IsNullOrWhiteSpace(cedulaTitular)) |
| | 0 | 193 | | return BadRequest(ApiResponse<string>.ErrorResult("cedulaTitular es requerida")); |
| | 2 | 194 | | if (mes < 1 || mes > 12) |
| | 0 | 195 | | return BadRequest(ApiResponse<string>.ErrorResult("mes debe estar entre 1 y 12")); |
| | | 196 | | |
| | 2 | 197 | | var form = await _form3100Service.GetVigenteParaPeriodoAsync(cedulaTitular, anio, mes); |
| | 2 | 198 | | if (form == null) |
| | 1 | 199 | | return NotFound(ApiResponse<string>.ErrorResult("No se encontró Form 3100 vigente para el período indica |
| | | 200 | | |
| | 1 | 201 | | return Ok(ApiResponse<object>.SuccessResult(MapToDto(form))); |
| | | 202 | | } |
| | 0 | 203 | | catch (Exception ex) |
| | | 204 | | { |
| | 0 | 205 | | _logger.LogError(ex, "Error al obtener Form 3100 vigente"); |
| | 0 | 206 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 207 | | } |
| | 2 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Listar personas a cargo de una versión del Form 3100 |
| | | 212 | | /// GET /api/form3100/{id}/personas-cargo |
| | | 213 | | /// </summary> |
| | | 214 | | [HttpGet("{id}/personas-cargo")] |
| | | 215 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 216 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 217 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 218 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 219 | | public async Task<IActionResult> GetPersonasACargo(long id) |
| | | 220 | | { |
| | | 221 | | try |
| | | 222 | | { |
| | 1 | 223 | | var form = await _form3100Service.GetByIdAsync(id); |
| | 1 | 224 | | if (form == null) |
| | 0 | 225 | | return NotFound(ApiResponse<string>.ErrorResult("Form 3100 no encontrado")); |
| | | 226 | | |
| | 1 | 227 | | var personas = form.PersonasACargo?.Select(p => new |
| | 1 | 228 | | { |
| | 1 | 229 | | id = p.Id, nombre = p.Nombre, apellido = p.Apellido, |
| | 1 | 230 | | fechaNacimiento = p.FechaNacimiento, cedula = p.Cedula, |
| | 1 | 231 | | sexo = p.Sexo, porcentajeAtribucion = p.PorcentajeAtribucion, |
| | 1 | 232 | | relacion = p.Relacion, discapacidad = p.Discapacidad |
| | 1 | 233 | | }); |
| | 1 | 234 | | return Ok(ApiResponse<object>.SuccessResult(personas ?? Enumerable.Empty<object>())); |
| | | 235 | | } |
| | 0 | 236 | | catch (Exception ex) |
| | | 237 | | { |
| | 0 | 238 | | _logger.LogError(ex, "Error al listar personas a cargo de Form 3100 {Id}", id); |
| | 0 | 239 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 240 | | } |
| | 1 | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Agregar persona a cargo a una versión del Form 3100 |
| | | 245 | | /// POST /api/form3100/{id}/personas-cargo |
| | | 246 | | /// </summary> |
| | | 247 | | [HttpPost("{id}/personas-cargo")] |
| | | 248 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 249 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 250 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 251 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 252 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status423Locked)] |
| | | 253 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 254 | | public async Task<IActionResult> AgregarPersonaACargo(long id, [FromBody] AgregarPersonaACargoRequest request) |
| | | 255 | | { |
| | | 256 | | try |
| | | 257 | | { |
| | 2 | 258 | | var persona = new Form3100PersonaACargo |
| | 2 | 259 | | { |
| | 2 | 260 | | Nombre = request.Nombre, |
| | 2 | 261 | | Apellido = request.Apellido, |
| | 2 | 262 | | FechaNacimiento = request.FechaNacimiento.Kind == DateTimeKind.Unspecified |
| | 2 | 263 | | ? DateTime.SpecifyKind(request.FechaNacimiento.Date, DateTimeKind.Utc) |
| | 2 | 264 | | : request.FechaNacimiento.Date.ToUniversalTime(), |
| | 2 | 265 | | Cedula = request.Cedula, |
| | 2 | 266 | | Sexo = request.Sexo, |
| | 2 | 267 | | PorcentajeAtribucion = (short)request.PorcentajeAtribucion, |
| | 2 | 268 | | Relacion = request.Relacion, |
| | 2 | 269 | | Discapacidad = request.Discapacidad |
| | 2 | 270 | | }; |
| | | 271 | | |
| | 2 | 272 | | var creada = await _form3100Service.AgregarPersonaACargoAsync(id, persona); |
| | | 273 | | |
| | 1 | 274 | | return StatusCode(201, ApiResponse<object>.SuccessResult( |
| | 1 | 275 | | new { id = creada.Id, creado = true }, |
| | 1 | 276 | | "Persona a cargo agregada exitosamente")); |
| | | 277 | | } |
| | 0 | 278 | | catch (PeriodoBloqueadoException ex) |
| | | 279 | | { |
| | 0 | 280 | | return StatusCode(StatusCodes.Status423Locked, ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 281 | | } |
| | 1 | 282 | | catch (ArgumentException ex) |
| | | 283 | | { |
| | 1 | 284 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 285 | | } |
| | 0 | 286 | | catch (Exception ex) |
| | | 287 | | { |
| | 0 | 288 | | _logger.LogError(ex, "Error al agregar persona a cargo a Form 3100 {Id}", id); |
| | 0 | 289 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 290 | | } |
| | 2 | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Eliminar persona a cargo de una versión del Form 3100 |
| | | 295 | | /// DELETE /api/form3100/{id}/personas-cargo/{personaCargoId} |
| | | 296 | | /// </summary> |
| | | 297 | | [HttpDelete("{id}/personas-cargo/{personaCargoId}")] |
| | | 298 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 299 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 300 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 301 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 302 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status423Locked)] |
| | | 303 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 304 | | public async Task<IActionResult> EliminarPersonaACargo(long id, long personaCargoId) |
| | | 305 | | { |
| | | 306 | | try |
| | | 307 | | { |
| | 1 | 308 | | await _form3100Service.EliminarPersonaACargoAsync(id, personaCargoId); |
| | 1 | 309 | | return NoContent(); |
| | | 310 | | } |
| | 0 | 311 | | catch (PeriodoBloqueadoException ex) |
| | | 312 | | { |
| | 0 | 313 | | return StatusCode(StatusCodes.Status423Locked, ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 314 | | } |
| | 0 | 315 | | catch (ArgumentException ex) |
| | | 316 | | { |
| | 0 | 317 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 318 | | } |
| | 0 | 319 | | catch (Exception ex) |
| | | 320 | | { |
| | 0 | 321 | | _logger.LogError(ex, "Error al eliminar persona a cargo {PersonaCargoId} de Form 3100 {Id}", personaCargoId, |
| | 0 | 322 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 323 | | } |
| | 1 | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Subir archivo escaneado del Form 3100 |
| | | 328 | | /// POST /api/form3100/{id}/archivo (multipart/form-data, campo "file") |
| | | 329 | | /// </summary> |
| | | 330 | | [HttpPost("{id}/archivo")] |
| | | 331 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 332 | | [RequestSizeLimit(20_000_000)] // 20 MB |
| | | 333 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 334 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 335 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 336 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 337 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status423Locked)] |
| | | 338 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 339 | | public async Task<IActionResult> SubirArchivo(long id, IFormFile file) |
| | | 340 | | { |
| | | 341 | | try |
| | | 342 | | { |
| | 0 | 343 | | if (file == null || file.Length == 0) |
| | 0 | 344 | | return BadRequest(ApiResponse<string>.ErrorResult("Archivo requerido")); |
| | | 345 | | |
| | 0 | 346 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 0 | 347 | | await using var stream = file.OpenReadStream(); |
| | 0 | 348 | | var rutaRelativa = await _form3100Service.SubirArchivoAsync( |
| | 0 | 349 | | id, |
| | 0 | 350 | | stream, |
| | 0 | 351 | | file.FileName, |
| | 0 | 352 | | file.ContentType, |
| | 0 | 353 | | usuarioId); |
| | | 354 | | |
| | 0 | 355 | | return Ok(ApiResponse<object>.SuccessResult( |
| | 0 | 356 | | new { rutaRelativa, subido = true }, |
| | 0 | 357 | | "Archivo subido exitosamente")); |
| | 0 | 358 | | } |
| | 0 | 359 | | catch (PeriodoBloqueadoException ex) |
| | | 360 | | { |
| | 0 | 361 | | return StatusCode(StatusCodes.Status423Locked, ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 362 | | } |
| | 0 | 363 | | catch (ArgumentException ex) |
| | | 364 | | { |
| | 0 | 365 | | if (ex.Message.Contains("no encontrado", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 366 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | 0 | 367 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 368 | | } |
| | 0 | 369 | | catch (Exception ex) |
| | | 370 | | { |
| | 0 | 371 | | _logger.LogError(ex, "Error al subir archivo de Form 3100 {Id}", id); |
| | 0 | 372 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// Descargar archivo escaneado del Form 3100 |
| | | 378 | | /// GET /api/form3100/{id}/archivo |
| | | 379 | | /// </summary> |
| | | 380 | | /// <summary> |
| | | 381 | | /// GET /api/form3100/{id}/archivo?inline=true |
| | | 382 | | /// inline=true → Content-Disposition: inline (para mostrar en visor del navegador) |
| | | 383 | | /// inline=false → Content-Disposition: attachment (para forzar descarga, default) |
| | | 384 | | /// </summary> |
| | | 385 | | [HttpGet("{id}/archivo")] |
| | | 386 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 387 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 388 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 389 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 390 | | public async Task<IActionResult> DescargarArchivo(long id, [FromQuery] bool inline = true) |
| | | 391 | | { |
| | | 392 | | try |
| | | 393 | | { |
| | 1 | 394 | | var (contenido, nombreOriginal, contentType) = await _form3100Service.ObtenerArchivoAsync(id); |
| | | 395 | | |
| | 0 | 396 | | if (inline) |
| | | 397 | | { |
| | 0 | 398 | | Response.Headers.ContentDisposition = |
| | 0 | 399 | | $"inline; filename=\"{nombreOriginal}\"; filename*=UTF-8''{Uri.EscapeDataString(nombreOriginal)}"; |
| | 0 | 400 | | return File(contenido, contentType); |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | return File(contenido, contentType, nombreOriginal); |
| | | 404 | | } |
| | 0 | 405 | | catch (ArgumentException ex) |
| | | 406 | | { |
| | 0 | 407 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 408 | | } |
| | 1 | 409 | | catch (KeyNotFoundException ex) |
| | | 410 | | { |
| | 1 | 411 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 412 | | } |
| | 0 | 413 | | catch (StorageFileNotFoundException ex) |
| | | 414 | | { |
| | 0 | 415 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 416 | | } |
| | 0 | 417 | | catch (Exception ex) |
| | | 418 | | { |
| | 0 | 419 | | _logger.LogError(ex, "Error al descargar archivo de Form 3100 {Id}", id); |
| | 0 | 420 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 421 | | } |
| | 1 | 422 | | } |
| | | 423 | | |
| | | 424 | | private static object MapToDto(Form3100 f) |
| | | 425 | | { |
| | 2 | 426 | | return new |
| | 2 | 427 | | { |
| | 2 | 428 | | id = f.Id, |
| | 2 | 429 | | personaId = f.PersonaId, |
| | 2 | 430 | | cappuCategoria = f.CappuCategoria, |
| | 2 | 431 | | cappuImporte = f.CappuImporte, |
| | 2 | 432 | | reintegroAporte = f.ReintegroAporte, |
| | 2 | 433 | | fondoSolidaridad = f.FondoSolidaridad, |
| | 2 | 434 | | adicionalFondoSolidaridad = f.AdicionalFondoSolidaridad, |
| | 2 | 435 | | aplicaMinimoNoImponible = f.AplicaMinimoNoImponible, |
| | 2 | 436 | | conyugeNombre = f.ConyugeNombre, |
| | 2 | 437 | | conyugeApellido = f.ConyugeApellido, |
| | 2 | 438 | | conyugeFechaNacimiento = f.ConyugeFechaNacimiento, |
| | 2 | 439 | | conyugeCedula = f.ConyugeCedula, |
| | 2 | 440 | | conyugeSexo = f.ConyugeSexo, |
| | 2 | 441 | | conyugeNacionalidad = f.ConyugeNacionalidad, |
| | 2 | 442 | | vigenteDesdeMes = f.VigenteDesde_Mes, |
| | 2 | 443 | | vigenteDesdeAnio = f.VigenteDesde_Anio, |
| | 2 | 444 | | documentoId = f.DocumentoId, |
| | 2 | 445 | | fechaCreacion = f.FechaCreacion, |
| | 2 | 446 | | usuarioCreacionId = f.UsuarioCreacionId, |
| | 0 | 447 | | personasACargo = f.PersonasACargo?.Select(p => new |
| | 0 | 448 | | { |
| | 0 | 449 | | id = p.Id, |
| | 0 | 450 | | nombre = p.Nombre, |
| | 0 | 451 | | apellido = p.Apellido, |
| | 0 | 452 | | fechaNacimiento = p.FechaNacimiento, |
| | 0 | 453 | | cedula = p.Cedula, |
| | 0 | 454 | | sexo = p.Sexo, |
| | 0 | 455 | | porcentajeAtribucion = p.PorcentajeAtribucion, |
| | 0 | 456 | | relacion = p.Relacion, |
| | 0 | 457 | | discapacidad = p.Discapacidad |
| | 0 | 458 | | }) |
| | 2 | 459 | | }; |
| | | 460 | | } |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | public class CrearForm3100Request |
| | | 464 | | { |
| | | 465 | | [Required(ErrorMessage = "cedulaTitular es requerida")] |
| | | 466 | | [MaxLength(12)] |
| | | 467 | | public string CedulaTitular { get; set; } = string.Empty; |
| | | 468 | | |
| | | 469 | | public short? CappuCategoria { get; set; } |
| | | 470 | | |
| | | 471 | | [Range(0, double.MaxValue)] |
| | | 472 | | public decimal CappuImporte { get; set; } |
| | | 473 | | |
| | | 474 | | [Range(0, double.MaxValue)] |
| | | 475 | | public decimal ReintegroAporte { get; set; } |
| | | 476 | | |
| | | 477 | | [MaxLength(10)] |
| | | 478 | | public string? FondoSolidaridad { get; set; } |
| | | 479 | | |
| | | 480 | | public bool AdicionalFondoSolidaridad { get; set; } |
| | | 481 | | |
| | | 482 | | public bool AplicaMinimoNoImponible { get; set; } |
| | | 483 | | |
| | | 484 | | [MaxLength(100)] |
| | | 485 | | public string? ConyugeNombre { get; set; } |
| | | 486 | | |
| | | 487 | | [MaxLength(100)] |
| | | 488 | | public string? ConyugeApellido { get; set; } |
| | | 489 | | |
| | | 490 | | public DateTime? ConyugeFechaNacimiento { get; set; } |
| | | 491 | | |
| | | 492 | | [MaxLength(20)] |
| | | 493 | | public string? ConyugeCedula { get; set; } |
| | | 494 | | |
| | | 495 | | [MaxLength(1)] |
| | | 496 | | public string? ConyugeSexo { get; set; } |
| | | 497 | | |
| | | 498 | | [MaxLength(100)] |
| | | 499 | | public string? ConyugeNacionalidad { get; set; } |
| | | 500 | | |
| | | 501 | | [Required] |
| | | 502 | | [Range(1, 12)] |
| | | 503 | | public int VigenteDesdeMes { get; set; } |
| | | 504 | | |
| | | 505 | | [Required] |
| | | 506 | | [Range(2000, 2100)] |
| | | 507 | | public int VigenteDesdeAnio { get; set; } |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | public class AgregarPersonaACargoRequest |
| | | 511 | | { |
| | | 512 | | [Required] |
| | | 513 | | [MaxLength(100)] |
| | | 514 | | public string Nombre { get; set; } = string.Empty; |
| | | 515 | | |
| | | 516 | | [Required] |
| | | 517 | | [MaxLength(100)] |
| | | 518 | | public string Apellido { get; set; } = string.Empty; |
| | | 519 | | |
| | | 520 | | [Required] |
| | | 521 | | public DateTime FechaNacimiento { get; set; } |
| | | 522 | | |
| | | 523 | | [MaxLength(20)] |
| | | 524 | | public string? Cedula { get; set; } |
| | | 525 | | |
| | | 526 | | [Required] |
| | | 527 | | [MaxLength(1)] |
| | | 528 | | public string Sexo { get; set; } = string.Empty; |
| | | 529 | | |
| | | 530 | | [Required] |
| | | 531 | | [Range(0, 100)] |
| | | 532 | | public int PorcentajeAtribucion { get; set; } |
| | | 533 | | |
| | | 534 | | [Required] |
| | | 535 | | [MaxLength(100)] |
| | | 536 | | public string Relacion { get; set; } = string.Empty; |
| | | 537 | | |
| | | 538 | | public bool Discapacidad { get; set; } |
| | | 539 | | } |