| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Catálogo de acreedores para descuentos personales |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Route("api/acreedores")] |
| | | 15 | | [Authorize] |
| | | 16 | | public class AcreedoresController : ControllerBase |
| | | 17 | | { |
| | | 18 | | private readonly ApplicationDbContext _context; |
| | | 19 | | private readonly ILogger<AcreedoresController> _logger; |
| | | 20 | | |
| | 2 | 21 | | public AcreedoresController( |
| | 2 | 22 | | ApplicationDbContext context, |
| | 2 | 23 | | ILogger<AcreedoresController> logger) |
| | | 24 | | { |
| | 2 | 25 | | _context = context; |
| | 2 | 26 | | _logger = logger; |
| | 2 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Listar acreedores activos para selección en formularios |
| | | 31 | | /// GET /api/acreedores |
| | | 32 | | /// </summary> |
| | | 33 | | [HttpGet] |
| | | 34 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearDescuentosPersonales)] |
| | | 35 | | public async Task<IActionResult> GetAcreedores([FromQuery] bool? activo = true) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | 2 | 39 | | var query = _context.Acreedores.AsNoTracking(); |
| | | 40 | | |
| | 2 | 41 | | if (activo.HasValue) |
| | | 42 | | { |
| | 2 | 43 | | query = query.Where(acreedor => acreedor.Activo == activo.Value); |
| | | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | var acreedores = await query |
| | 2 | 47 | | .OrderBy(acreedor => acreedor.PrioridadDeficit) |
| | 2 | 48 | | .ThenBy(acreedor => acreedor.Nombre) |
| | 2 | 49 | | .Select(acreedor => new AcreedorDto |
| | 2 | 50 | | { |
| | 2 | 51 | | Id = acreedor.Id, |
| | 2 | 52 | | Codigo = acreedor.Codigo, |
| | 2 | 53 | | Nombre = acreedor.Nombre, |
| | 2 | 54 | | PrioridadDeficit = acreedor.PrioridadDeficit, |
| | 2 | 55 | | FormatoArchivo = acreedor.FormatoArchivo, |
| | 2 | 56 | | Contacto = acreedor.Contacto, |
| | 2 | 57 | | Activo = acreedor.Activo |
| | 2 | 58 | | }) |
| | 2 | 59 | | .ToListAsync(); |
| | | 60 | | |
| | 2 | 61 | | return Ok(ApiResponse<IEnumerable<AcreedorDto>>.SuccessResult(acreedores)); |
| | | 62 | | } |
| | 0 | 63 | | catch (Exception ex) |
| | | 64 | | { |
| | 0 | 65 | | _logger.LogError(ex, "Error al obtener acreedores"); |
| | 0 | 66 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 67 | | } |
| | 2 | 68 | | } |
| | | 69 | | } |