| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class AcreedorService : IAcreedorService |
| | | 8 | | { |
| | | 9 | | private readonly IAcreedorRepository _acreedorRepository; |
| | | 10 | | private readonly IAuditoriaService _auditoriaService; |
| | | 11 | | private readonly ICurrentUserContext _currentUser; |
| | | 12 | | |
| | 0 | 13 | | private static readonly HashSet<string> _categoriasValidas = new(StringComparer.Ordinal) |
| | 0 | 14 | | { |
| | 0 | 15 | | CategoriaDeficitDescuento.RetencionJudicialAlimenticia, |
| | 0 | 16 | | CategoriaDeficitDescuento.GarantiaAlquiler, |
| | 0 | 17 | | CategoriaDeficitDescuento.CuotaSindicalPartido, |
| | 0 | 18 | | CategoriaDeficitDescuento.CreditoSocialBrou, |
| | 0 | 19 | | CategoriaDeficitDescuento.Vivienda, |
| | 0 | 20 | | CategoriaDeficitDescuento.SeguroVida, |
| | 0 | 21 | | CategoriaDeficitDescuento.SaludPrepaga, |
| | 0 | 22 | | CategoriaDeficitDescuento.CreditoNominaCooperativa, |
| | 0 | 23 | | CategoriaDeficitDescuento.FacilidadPagoTributaria, |
| | 0 | 24 | | CategoriaDeficitDescuento.OtrasRetenciones, |
| | 0 | 25 | | }; |
| | | 26 | | |
| | 0 | 27 | | public AcreedorService( |
| | 0 | 28 | | IAcreedorRepository acreedorRepository, |
| | 0 | 29 | | IAuditoriaService auditoriaService, |
| | 0 | 30 | | ICurrentUserContext currentUser) |
| | | 31 | | { |
| | 0 | 32 | | _acreedorRepository = acreedorRepository; |
| | 0 | 33 | | _auditoriaService = auditoriaService; |
| | 0 | 34 | | _currentUser = currentUser; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<Acreedor?> GetByIdAsync(long id) |
| | | 38 | | { |
| | 0 | 39 | | return await _acreedorRepository.GetByIdAsync(id); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<PagedResult<AcreedorDto>> GetPagedAsync( |
| | | 43 | | int page, |
| | | 44 | | int pageSize, |
| | | 45 | | string? searchQuery = null, |
| | | 46 | | bool? activo = null, |
| | | 47 | | string? categoria = null) |
| | | 48 | | { |
| | 0 | 49 | | if (page < 1) page = 1; |
| | 0 | 50 | | if (pageSize < 1) pageSize = 20; |
| | 0 | 51 | | if (pageSize > 100) pageSize = 100; |
| | | 52 | | |
| | 0 | 53 | | var (items, totalCount) = await _acreedorRepository.GetPagedAsync(page, pageSize, searchQuery, activo, categoria |
| | | 54 | | |
| | 0 | 55 | | var dtos = items.Select(a => new AcreedorDto |
| | 0 | 56 | | { |
| | 0 | 57 | | Id = a.Id, |
| | 0 | 58 | | Codigo = a.Codigo, |
| | 0 | 59 | | Nombre = a.Nombre, |
| | 0 | 60 | | CategoriaDeficit = a.CategoriaDeficit, |
| | 0 | 61 | | OrdenLegalDeficit = a.OrdenLegalDeficit, |
| | 0 | 62 | | FormatoArchivo = a.FormatoArchivo, |
| | 0 | 63 | | Contacto = a.Contacto, |
| | 0 | 64 | | Activo = a.Activo, |
| | 0 | 65 | | }).ToList(); |
| | | 66 | | |
| | 0 | 67 | | return new PagedResult<AcreedorDto> |
| | 0 | 68 | | { |
| | 0 | 69 | | Items = dtos, |
| | 0 | 70 | | Page = page, |
| | 0 | 71 | | PageSize = pageSize, |
| | 0 | 72 | | TotalCount = totalCount, |
| | 0 | 73 | | }; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task<(bool Success, Acreedor? Acreedor, string? Error)> CreateAsync(CreateAcreedorRequest request) |
| | | 77 | | { |
| | | 78 | | try |
| | | 79 | | { |
| | 0 | 80 | | if (string.IsNullOrWhiteSpace(request.Codigo)) |
| | 0 | 81 | | return (false, null, "El código es requerido"); |
| | | 82 | | |
| | 0 | 83 | | if (string.IsNullOrWhiteSpace(request.Nombre)) |
| | 0 | 84 | | return (false, null, "El nombre es requerido"); |
| | | 85 | | |
| | 0 | 86 | | if (!_categoriasValidas.Contains(request.CategoriaDeficit)) |
| | 0 | 87 | | return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida"); |
| | | 88 | | |
| | 0 | 89 | | if (await _acreedorRepository.ExistsCodigoAsync(request.Codigo)) |
| | 0 | 90 | | return (false, null, "Ya existe un acreedor con ese código"); |
| | | 91 | | |
| | 0 | 92 | | var acreedor = new Acreedor |
| | 0 | 93 | | { |
| | 0 | 94 | | Codigo = request.Codigo.Trim().ToUpper(), |
| | 0 | 95 | | Nombre = request.Nombre.Trim(), |
| | 0 | 96 | | CategoriaDeficit = request.CategoriaDeficit, |
| | 0 | 97 | | OrdenLegalDeficit = request.OrdenLegalDeficit, |
| | 0 | 98 | | FormatoArchivo = request.FormatoArchivo ?? "txt", |
| | 0 | 99 | | Contacto = request.Contacto?.Trim(), |
| | 0 | 100 | | Activo = true, |
| | 0 | 101 | | }; |
| | | 102 | | |
| | 0 | 103 | | var creado = await _acreedorRepository.CreateAsync(acreedor); |
| | | 104 | | |
| | 0 | 105 | | await _auditoriaService.LogAuditoriaAsync( |
| | 0 | 106 | | usuarioId: _currentUser.UserId ?? 0, |
| | 0 | 107 | | AccionEnum.CrearAcreedor, |
| | 0 | 108 | | ContextoEnum.Acreedores, |
| | 0 | 109 | | host: _currentUser.Host, |
| | 0 | 110 | | entidad: nameof(Acreedor), |
| | 0 | 111 | | entidadId: creado.Id, |
| | 0 | 112 | | detalle: new { operacion = "crear", id = creado.Id, codigo = creado.Codigo, nombre = creado.Nombre }); |
| | | 113 | | |
| | 0 | 114 | | return (true, creado, null); |
| | | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 0 | 118 | | return (false, null, $"Error al crear acreedor: {ex.Message}"); |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | public async Task<(bool Success, Acreedor? Acreedor, string? Error)> UpdateAsync(long id, UpdateAcreedorRequest requ |
| | | 123 | | { |
| | | 124 | | try |
| | | 125 | | { |
| | 0 | 126 | | var acreedor = await _acreedorRepository.GetByIdAsync(id); |
| | 0 | 127 | | if (acreedor == null) |
| | 0 | 128 | | return (false, null, "Acreedor no encontrado"); |
| | | 129 | | |
| | 0 | 130 | | if (request.CategoriaDeficit != null && !_categoriasValidas.Contains(request.CategoriaDeficit)) |
| | 0 | 131 | | return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida"); |
| | | 132 | | |
| | 0 | 133 | | var snapshotAntes = new |
| | 0 | 134 | | { |
| | 0 | 135 | | id = acreedor.Id, |
| | 0 | 136 | | codigo = acreedor.Codigo, |
| | 0 | 137 | | nombre = acreedor.Nombre, |
| | 0 | 138 | | categoriaDeficit = acreedor.CategoriaDeficit, |
| | 0 | 139 | | ordenLegalDeficit = acreedor.OrdenLegalDeficit, |
| | 0 | 140 | | formatoArchivo = acreedor.FormatoArchivo, |
| | 0 | 141 | | contacto = acreedor.Contacto, |
| | 0 | 142 | | activo = acreedor.Activo, |
| | 0 | 143 | | }; |
| | | 144 | | |
| | 0 | 145 | | if (!string.IsNullOrWhiteSpace(request.Nombre)) |
| | 0 | 146 | | acreedor.Nombre = request.Nombre.Trim(); |
| | | 147 | | |
| | 0 | 148 | | if (request.CategoriaDeficit != null) |
| | 0 | 149 | | acreedor.CategoriaDeficit = request.CategoriaDeficit; |
| | | 150 | | |
| | 0 | 151 | | if (request.OrdenLegalDeficit.HasValue) |
| | 0 | 152 | | acreedor.OrdenLegalDeficit = request.OrdenLegalDeficit.Value; |
| | | 153 | | |
| | 0 | 154 | | if (request.FormatoArchivo != null) |
| | 0 | 155 | | acreedor.FormatoArchivo = request.FormatoArchivo; |
| | | 156 | | |
| | 0 | 157 | | if (request.Contacto != null) |
| | 0 | 158 | | acreedor.Contacto = request.Contacto.Trim(); |
| | | 159 | | |
| | 0 | 160 | | if (request.Activo.HasValue) |
| | 0 | 161 | | acreedor.Activo = request.Activo.Value; |
| | | 162 | | |
| | 0 | 163 | | var actualizado = await _acreedorRepository.UpdateAsync(acreedor); |
| | | 164 | | |
| | 0 | 165 | | await _auditoriaService.LogAuditoriaAsync( |
| | 0 | 166 | | usuarioId: _currentUser.UserId ?? 0, |
| | 0 | 167 | | AccionEnum.ActualizarAcreedor, |
| | 0 | 168 | | ContextoEnum.Acreedores, |
| | 0 | 169 | | host: _currentUser.Host, |
| | 0 | 170 | | entidad: nameof(Acreedor), |
| | 0 | 171 | | entidadId: actualizado.Id, |
| | 0 | 172 | | detalle: new |
| | 0 | 173 | | { |
| | 0 | 174 | | operacion = "editar", |
| | 0 | 175 | | antes = snapshotAntes, |
| | 0 | 176 | | despues = new |
| | 0 | 177 | | { |
| | 0 | 178 | | id = actualizado.Id, |
| | 0 | 179 | | codigo = actualizado.Codigo, |
| | 0 | 180 | | nombre = actualizado.Nombre, |
| | 0 | 181 | | categoriaDeficit = actualizado.CategoriaDeficit, |
| | 0 | 182 | | ordenLegalDeficit = actualizado.OrdenLegalDeficit, |
| | 0 | 183 | | formatoArchivo = actualizado.FormatoArchivo, |
| | 0 | 184 | | contacto = actualizado.Contacto, |
| | 0 | 185 | | activo = actualizado.Activo, |
| | 0 | 186 | | } |
| | 0 | 187 | | }); |
| | | 188 | | |
| | 0 | 189 | | return (true, actualizado, null); |
| | | 190 | | } |
| | 0 | 191 | | catch (Exception ex) |
| | | 192 | | { |
| | 0 | 193 | | return (false, null, $"Error al actualizar acreedor: {ex.Message}"); |
| | | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | } |