| | | 1 | | using FAU.Entidades; |
| | | 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 | | using System.ComponentModel.DataAnnotations; |
| | | 8 | | |
| | | 9 | | namespace FAU.API.Controllers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Gestión de descuentos personales del período |
| | | 13 | | /// </summary> |
| | | 14 | | [ApiController] |
| | | 15 | | [Route("api/descuentos-personales")] |
| | | 16 | | [Authorize] |
| | | 17 | | [Produces("application/json")] |
| | | 18 | | public class DescuentosPersonalesController : ControllerBase |
| | | 19 | | { |
| | | 20 | | private readonly IDescuentoPersonalService _service; |
| | | 21 | | private readonly ICurrentUserContext _currentUser; |
| | | 22 | | private readonly ILogger<DescuentosPersonalesController> _logger; |
| | | 23 | | |
| | | 24 | | public DescuentosPersonalesController( |
| | | 25 | | IDescuentoPersonalService service, |
| | | 26 | | ICurrentUserContext currentUser, |
| | | 27 | | ILogger<DescuentosPersonalesController> logger) |
| | | 28 | | { |
| | | 29 | | _service = service; |
| | | 30 | | _currentUser = currentUser; |
| | | 31 | | _logger = logger; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Crear un descuento personal en borrador |
| | | 36 | | /// POST /api/descuentos-personales |
| | | 37 | | /// </summary> |
| | | 38 | | [HttpPost] |
| | | 39 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearDescuentosPersonales)] |
| | | 40 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 41 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 42 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 43 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 44 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 45 | | public async Task<IActionResult> Crear([FromBody] CrearDescuentoPersonalRequest request) |
| | | 46 | | { |
| | | 47 | | try |
| | | 48 | | { |
| | | 49 | | if (request.PeriodoId <= 0) |
| | | 50 | | return BadRequest(ApiResponse<string>.ErrorResult("periodoId es requerido")); |
| | | 51 | | if (string.IsNullOrWhiteSpace(request.CedulaTitular)) |
| | | 52 | | return BadRequest(ApiResponse<string>.ErrorResult("cedulaTitular es requerida")); |
| | | 53 | | if (request.AcreedorId <= 0) |
| | | 54 | | return BadRequest(ApiResponse<string>.ErrorResult("acreedorId es requerido")); |
| | | 55 | | if (request.Importe <= 0) |
| | | 56 | | return BadRequest(ApiResponse<string>.ErrorResult("importe debe ser mayor a cero")); |
| | | 57 | | |
| | | 58 | | var usuarioId = _currentUser.UserId ?? 0; |
| | | 59 | | var creado = await _service.CrearAsync( |
| | | 60 | | request.PeriodoId, |
| | | 61 | | request.CedulaTitular, |
| | | 62 | | request.CodigoConcepto, |
| | | 63 | | request.AcreedorId, |
| | | 64 | | request.Importe, |
| | | 65 | | request.Observaciones, |
| | | 66 | | usuarioId, |
| | | 67 | | request.FechaComunicacion); |
| | | 68 | | |
| | | 69 | | return CreatedAtAction( |
| | | 70 | | nameof(Listar), |
| | | 71 | | new { periodoId = request.PeriodoId }, |
| | | 72 | | ApiResponse<object>.SuccessResult( |
| | | 73 | | new { id = creado.Id, creado = true }, |
| | | 74 | | "Descuento personal creado exitosamente")); |
| | | 75 | | } |
| | | 76 | | catch (ArgumentException ex) |
| | | 77 | | { |
| | | 78 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 79 | | } |
| | | 80 | | catch (InvalidOperationException ex) |
| | | 81 | | { |
| | | 82 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 83 | | } |
| | | 84 | | catch (Exception ex) |
| | | 85 | | { |
| | | 86 | | _logger.LogError(ex, "Error al crear descuento personal"); |
| | | 87 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Listar descuentos personales del período |
| | | 93 | | /// GET /api/descuentos-personales?periodoId=...&cedulaTitular=...&codigoConcepto=...&estado=... |
| | | 94 | | /// </summary> |
| | | 95 | | [HttpGet] |
| | | 96 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerDescuentosPersonales)] |
| | | 97 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 98 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 99 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 100 | | public async Task<IActionResult> Listar( |
| | | 101 | | [FromQuery] long periodoId, |
| | | 102 | | [FromQuery] string? cedulaTitular = null, |
| | | 103 | | [FromQuery] int? codigoConcepto = null, |
| | | 104 | | [FromQuery] string? estado = null) |
| | | 105 | | { |
| | | 106 | | try |
| | | 107 | | { |
| | | 108 | | if (periodoId <= 0) |
| | | 109 | | return BadRequest(ApiResponse<string>.ErrorResult("periodoId es requerido")); |
| | | 110 | | |
| | | 111 | | var items = await _service.ListarAsync(periodoId, cedulaTitular, codigoConcepto, estado); |
| | | 112 | | |
| | | 113 | | var response = items.Select(d => new |
| | | 114 | | { |
| | | 115 | | id = d.Id, |
| | | 116 | | periodoId = d.PeriodoId, |
| | | 117 | | personaId = d.PersonaId, |
| | | 118 | | cedula = d.Persona?.Cedula, |
| | | 119 | | nombreCompleto = d.Persona?.NombreCompleto, |
| | | 120 | | acreedorId = d.AcreedorId, |
| | | 121 | | acreedorCodigo = d.Acreedor?.Codigo, |
| | | 122 | | acreedorNombre = d.Acreedor?.Nombre, |
| | | 123 | | categoriaDeficit = d.Acreedor?.CategoriaDeficit, |
| | | 124 | | ordenLegalDeficit = d.Acreedor?.OrdenLegalDeficit, |
| | | 125 | | catalogoItemId = d.CatalogoItemId, |
| | | 126 | | codigoConcepto = d.CatalogoItem?.Codigo, |
| | | 127 | | nombreConcepto = d.CatalogoItem?.Nombre, |
| | | 128 | | importe = d.Importe, |
| | | 129 | | estado = d.Estado, |
| | | 130 | | observaciones = d.Observaciones, |
| | | 131 | | fechaComunicacion = d.FechaComunicacion, |
| | | 132 | | fechaCreacion = d.FechaCreacion, |
| | | 133 | | usuarioCreacionId = d.UsuarioCreacionId |
| | | 134 | | }); |
| | | 135 | | |
| | | 136 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 137 | | } |
| | | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | | 140 | | _logger.LogError(ex, "Error al listar descuentos personales"); |
| | | 141 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Editar un descuento personal en borrador |
| | | 147 | | /// PUT /api/descuentos-personales/{id} |
| | | 148 | | /// </summary> |
| | | 149 | | [HttpPut("{id}")] |
| | | 150 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarDescuentosPersonales)] |
| | | 151 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 152 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 153 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 154 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 155 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 156 | | public async Task<IActionResult> Editar(long id, [FromBody] EditarDescuentoPersonalRequest request) |
| | | 157 | | { |
| | | 158 | | try |
| | | 159 | | { |
| | | 160 | | if (request.Importe <= 0) |
| | | 161 | | return BadRequest(ApiResponse<string>.ErrorResult("importe debe ser mayor a cero")); |
| | | 162 | | |
| | | 163 | | var actualizado = await _service.EditarAsync(id, request.Importe, request.Observaciones, request.FechaComuni |
| | | 164 | | return Ok(ApiResponse<object>.SuccessResult( |
| | | 165 | | new { id = actualizado.Id, actualizado = true }, |
| | | 166 | | "Descuento personal actualizado exitosamente")); |
| | | 167 | | } |
| | | 168 | | catch (ArgumentException ex) |
| | | 169 | | { |
| | | 170 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 171 | | } |
| | | 172 | | catch (InvalidOperationException ex) |
| | | 173 | | { |
| | | 174 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 175 | | } |
| | | 176 | | catch (Exception ex) |
| | | 177 | | { |
| | | 178 | | _logger.LogError(ex, "Error al actualizar descuento personal {Id}", id); |
| | | 179 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Eliminar un descuento personal en borrador |
| | | 185 | | /// DELETE /api/descuentos-personales/{id} |
| | | 186 | | /// </summary> |
| | | 187 | | [HttpDelete("{id}")] |
| | | 188 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarDescuentosPersonales)] |
| | | 189 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 190 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 191 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 192 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 193 | | public async Task<IActionResult> Eliminar(long id) |
| | | 194 | | { |
| | | 195 | | try |
| | | 196 | | { |
| | | 197 | | await _service.EliminarAsync(id); |
| | | 198 | | return NoContent(); |
| | | 199 | | } |
| | | 200 | | catch (ArgumentException ex) |
| | | 201 | | { |
| | | 202 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 203 | | } |
| | | 204 | | catch (InvalidOperationException ex) |
| | | 205 | | { |
| | | 206 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 207 | | } |
| | | 208 | | catch (Exception ex) |
| | | 209 | | { |
| | | 210 | | _logger.LogError(ex, "Error al eliminar descuento personal {Id}", id); |
| | | 211 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Confirmar un descuento personal en borrador |
| | | 217 | | /// POST /api/descuentos-personales/{id}/confirmar |
| | | 218 | | /// </summary> |
| | | 219 | | [HttpPost("{id}/confirmar")] |
| | | 220 | | [FAU.API.Security.RequirePermission(PermisoEnum.ConfirmarDescuentosPersonales)] |
| | | 221 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 222 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 223 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 224 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 225 | | public async Task<IActionResult> ConfirmarUno(long id) |
| | | 226 | | { |
| | | 227 | | try |
| | | 228 | | { |
| | | 229 | | var confirmado = await _service.ConfirmarAsync(id); |
| | | 230 | | return Ok(ApiResponse<object>.SuccessResult( |
| | | 231 | | new { confirmado = true }, |
| | | 232 | | $"Descuento personal {confirmado.Id} confirmado exitosamente")); |
| | | 233 | | } |
| | | 234 | | catch (ArgumentException ex) |
| | | 235 | | { |
| | | 236 | | return NotFound(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 237 | | } |
| | | 238 | | catch (InvalidOperationException ex) |
| | | 239 | | { |
| | | 240 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 241 | | } |
| | | 242 | | catch (Exception ex) |
| | | 243 | | { |
| | | 244 | | _logger.LogError(ex, "Error al confirmar descuento personal {Id}", id); |
| | | 245 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Confirmar todos los borradores de un período |
| | | 251 | | /// POST /api/descuentos-personales/confirmar |
| | | 252 | | /// </summary> |
| | | 253 | | [HttpPost("confirmar")] |
| | | 254 | | [FAU.API.Security.RequirePermission(PermisoEnum.ConfirmarDescuentosPersonales)] |
| | | 255 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 256 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 257 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 258 | | public async Task<IActionResult> Confirmar([FromBody] ConfirmarDescuentosRequest request) |
| | | 259 | | { |
| | | 260 | | try |
| | | 261 | | { |
| | | 262 | | if (request.PeriodoId <= 0) |
| | | 263 | | return BadRequest(ApiResponse<string>.ErrorResult("periodoId es requerido")); |
| | | 264 | | |
| | | 265 | | var cantidad = await _service.ConfirmarBorradoresAsync(request.PeriodoId); |
| | | 266 | | return Ok(ApiResponse<object>.SuccessResult( |
| | | 267 | | new { confirmados = cantidad }, |
| | | 268 | | $"Se confirmaron {cantidad} descuento(s) personal(es)")); |
| | | 269 | | } |
| | | 270 | | catch (Exception ex) |
| | | 271 | | { |
| | | 272 | | _logger.LogError(ex, "Error al confirmar descuentos personales del período {PeriodoId}", request.PeriodoId); |
| | | 273 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 274 | | } |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | public class CrearDescuentoPersonalRequest |
| | | 279 | | { |
| | | 280 | | [Required] |
| | 12 | 281 | | public long PeriodoId { get; set; } |
| | | 282 | | |
| | | 283 | | [Required] |
| | | 284 | | [MaxLength(12)] |
| | 15 | 285 | | public string CedulaTitular { get; set; } = string.Empty; |
| | | 286 | | |
| | | 287 | | [Required] |
| | 7 | 288 | | public int CodigoConcepto { get; set; } |
| | | 289 | | |
| | | 290 | | [Required] |
| | 11 | 291 | | public long AcreedorId { get; set; } |
| | | 292 | | |
| | | 293 | | [Required] |
| | 11 | 294 | | public decimal Importe { get; set; } |
| | | 295 | | |
| | | 296 | | [MaxLength(500)] |
| | 4 | 297 | | public string? Observaciones { get; set; } |
| | | 298 | | |
| | 3 | 299 | | public DateTime? FechaComunicacion { get; set; } |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | public class EditarDescuentoPersonalRequest |
| | | 303 | | { |
| | | 304 | | [Required] |
| | | 305 | | public decimal Importe { get; set; } |
| | | 306 | | |
| | | 307 | | [MaxLength(500)] |
| | | 308 | | public string? Observaciones { get; set; } |
| | | 309 | | |
| | | 310 | | public DateTime? FechaComunicacion { get; set; } |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | public class ConfirmarDescuentosRequest |
| | | 314 | | { |
| | | 315 | | [Required] |
| | | 316 | | public long PeriodoId { get; set; } |
| | | 317 | | } |