< Summary

Information
Class: FAU.Logica.Services.AcreedorService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/AcreedorService.cs
Line coverage
93%
Covered lines: 125
Uncovered lines: 8
Coverable lines: 133
Total lines: 196
Line coverage: 93.9%
Branch coverage
80%
Covered branches: 29
Total branches: 36
Branch coverage: 80.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
GetByIdAsync()100%11100%
GetPagedAsync()83.33%66100%
CreateAsync()83.33%121293.54%
UpdateAsync()77.77%181889.28%

File(s)

/home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/AcreedorService.cs

#LineLine coverage
 1using FAU.DataAccess.Repositories;
 2using FAU.Entidades;
 3using FAU.Logica.DTOs;
 4
 5namespace FAU.Logica.Services;
 6
 7public class AcreedorService : IAcreedorService
 8{
 9    private readonly IAcreedorRepository _acreedorRepository;
 10    private readonly IAuditoriaService _auditoriaService;
 11    private readonly ICurrentUserContext _currentUser;
 12
 113    private static readonly HashSet<string> _categoriasValidas = new(StringComparer.Ordinal)
 114    {
 115        CategoriaDeficitDescuento.RetencionJudicialAlimenticia,
 116        CategoriaDeficitDescuento.GarantiaAlquiler,
 117        CategoriaDeficitDescuento.CuotaSindicalPartido,
 118        CategoriaDeficitDescuento.CreditoSocialBrou,
 119        CategoriaDeficitDescuento.Vivienda,
 120        CategoriaDeficitDescuento.SeguroVida,
 121        CategoriaDeficitDescuento.SaludPrepaga,
 122        CategoriaDeficitDescuento.CreditoNominaCooperativa,
 123        CategoriaDeficitDescuento.FacilidadPagoTributaria,
 124        CategoriaDeficitDescuento.OtrasRetenciones,
 125    };
 26
 1527    public AcreedorService(
 1528        IAcreedorRepository acreedorRepository,
 1529        IAuditoriaService auditoriaService,
 1530        ICurrentUserContext currentUser)
 31    {
 1532        _acreedorRepository = acreedorRepository;
 1533        _auditoriaService = auditoriaService;
 1534        _currentUser = currentUser;
 1535    }
 36
 37    public async Task<Acreedor?> GetByIdAsync(long id)
 38    {
 239        return await _acreedorRepository.GetByIdAsync(id);
 240    }
 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    {
 549        if (page < 1) page = 1;
 450        if (pageSize < 1) pageSize = 20;
 551        if (pageSize > 100) pageSize = 100;
 52
 453        var (items, totalCount) = await _acreedorRepository.GetPagedAsync(page, pageSize, searchQuery, activo, categoria
 54
 855        var dtos = items.Select(a => new AcreedorDto
 856        {
 857            Id = a.Id,
 858            Codigo = a.Codigo,
 859            Nombre = a.Nombre,
 860            CategoriaDeficit = a.CategoriaDeficit,
 861            OrdenLegalDeficit = a.OrdenLegalDeficit,
 862            FormatoArchivo = a.FormatoArchivo,
 863            Contacto = a.Contacto,
 864            Activo = a.Activo,
 865        }).ToList();
 66
 467        return new PagedResult<AcreedorDto>
 468        {
 469            Items = dtos,
 470            Page = page,
 471            PageSize = pageSize,
 472            TotalCount = totalCount,
 473        };
 474    }
 75
 76    public async Task<(bool Success, Acreedor? Acreedor, string? Error)> CreateAsync(CreateAcreedorRequest request)
 77    {
 78        try
 79        {
 680            if (string.IsNullOrWhiteSpace(request.Codigo))
 181                return (false, null, "El código es requerido");
 82
 583            if (string.IsNullOrWhiteSpace(request.Nombre))
 184                return (false, null, "El nombre es requerido");
 85
 486            if (!_categoriasValidas.Contains(request.CategoriaDeficit))
 187                return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida");
 88
 389            if (await _acreedorRepository.ExistsCodigoAsync(request.Codigo))
 190                return (false, null, "Ya existe un acreedor con ese código");
 91
 292            var acreedor = new Acreedor
 293            {
 294                Codigo = request.Codigo.Trim().ToUpper(),
 295                Nombre = request.Nombre.Trim(),
 296                CategoriaDeficit = request.CategoriaDeficit,
 297                OrdenLegalDeficit = request.OrdenLegalDeficit,
 298                FormatoArchivo = request.FormatoArchivo ?? "txt",
 299                Contacto = request.Contacto?.Trim(),
 2100                Activo = true,
 2101            };
 102
 2103            var creado = await _acreedorRepository.CreateAsync(acreedor);
 104
 2105            await _auditoriaService.LogAuditoriaAsync(
 2106                usuarioId: _currentUser.UserId ?? 0,
 2107                AccionEnum.CrearAcreedor,
 2108                ContextoEnum.Acreedores,
 2109                host: _currentUser.Host,
 2110                entidad: nameof(Acreedor),
 2111                entidadId: creado.Id,
 2112                detalle: new { operacion = "crear", id = creado.Id, codigo = creado.Codigo, nombre = creado.Nombre });
 113
 2114            return (true, creado, null);
 115        }
 0116        catch (Exception ex)
 117        {
 0118            return (false, null, $"Error al crear acreedor: {ex.Message}");
 119        }
 6120    }
 121
 122    public async Task<(bool Success, Acreedor? Acreedor, string? Error)> UpdateAsync(long id, UpdateAcreedorRequest requ
 123    {
 124        try
 125        {
 3126            var acreedor = await _acreedorRepository.GetByIdAsync(id);
 3127            if (acreedor == null)
 1128                return (false, null, "Acreedor no encontrado");
 129
 2130            if (request.CategoriaDeficit != null && !_categoriasValidas.Contains(request.CategoriaDeficit))
 1131                return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida");
 132
 1133            var snapshotAntes = new
 1134            {
 1135                id = acreedor.Id,
 1136                codigo = acreedor.Codigo,
 1137                nombre = acreedor.Nombre,
 1138                categoriaDeficit = acreedor.CategoriaDeficit,
 1139                ordenLegalDeficit = acreedor.OrdenLegalDeficit,
 1140                formatoArchivo = acreedor.FormatoArchivo,
 1141                contacto = acreedor.Contacto,
 1142                activo = acreedor.Activo,
 1143            };
 144
 1145            if (!string.IsNullOrWhiteSpace(request.Nombre))
 1146                acreedor.Nombre = request.Nombre.Trim();
 147
 1148            if (request.CategoriaDeficit != null)
 0149                acreedor.CategoriaDeficit = request.CategoriaDeficit;
 150
 1151            if (request.OrdenLegalDeficit.HasValue)
 0152                acreedor.OrdenLegalDeficit = request.OrdenLegalDeficit.Value;
 153
 1154            if (request.FormatoArchivo != null)
 0155                acreedor.FormatoArchivo = request.FormatoArchivo;
 156
 1157            if (request.Contacto != null)
 0158                acreedor.Contacto = request.Contacto.Trim();
 159
 1160            if (request.Activo.HasValue)
 1161                acreedor.Activo = request.Activo.Value;
 162
 1163            var actualizado = await _acreedorRepository.UpdateAsync(acreedor);
 164
 1165            await _auditoriaService.LogAuditoriaAsync(
 1166                usuarioId: _currentUser.UserId ?? 0,
 1167                AccionEnum.ActualizarAcreedor,
 1168                ContextoEnum.Acreedores,
 1169                host: _currentUser.Host,
 1170                entidad: nameof(Acreedor),
 1171                entidadId: actualizado.Id,
 1172                detalle: new
 1173                {
 1174                    operacion = "editar",
 1175                    antes = snapshotAntes,
 1176                    despues = new
 1177                    {
 1178                        id = actualizado.Id,
 1179                        codigo = actualizado.Codigo,
 1180                        nombre = actualizado.Nombre,
 1181                        categoriaDeficit = actualizado.CategoriaDeficit,
 1182                        ordenLegalDeficit = actualizado.OrdenLegalDeficit,
 1183                        formatoArchivo = actualizado.FormatoArchivo,
 1184                        contacto = actualizado.Contacto,
 1185                        activo = actualizado.Activo,
 1186                    }
 1187                });
 188
 1189            return (true, actualizado, null);
 190        }
 0191        catch (Exception ex)
 192        {
 0193            return (false, null, $"Error al actualizar acreedor: {ex.Message}");
 194        }
 3195    }
 196}