< 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
0%
Covered lines: 0
Uncovered lines: 133
Coverable lines: 133
Total lines: 196
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 36
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor(...)100%210%
GetByIdAsync()100%210%
GetPagedAsync()0%4260%
CreateAsync()0%156120%
UpdateAsync()0%342180%

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
 013    private static readonly HashSet<string> _categoriasValidas = new(StringComparer.Ordinal)
 014    {
 015        CategoriaDeficitDescuento.RetencionJudicialAlimenticia,
 016        CategoriaDeficitDescuento.GarantiaAlquiler,
 017        CategoriaDeficitDescuento.CuotaSindicalPartido,
 018        CategoriaDeficitDescuento.CreditoSocialBrou,
 019        CategoriaDeficitDescuento.Vivienda,
 020        CategoriaDeficitDescuento.SeguroVida,
 021        CategoriaDeficitDescuento.SaludPrepaga,
 022        CategoriaDeficitDescuento.CreditoNominaCooperativa,
 023        CategoriaDeficitDescuento.FacilidadPagoTributaria,
 024        CategoriaDeficitDescuento.OtrasRetenciones,
 025    };
 26
 027    public AcreedorService(
 028        IAcreedorRepository acreedorRepository,
 029        IAuditoriaService auditoriaService,
 030        ICurrentUserContext currentUser)
 31    {
 032        _acreedorRepository = acreedorRepository;
 033        _auditoriaService = auditoriaService;
 034        _currentUser = currentUser;
 035    }
 36
 37    public async Task<Acreedor?> GetByIdAsync(long id)
 38    {
 039        return await _acreedorRepository.GetByIdAsync(id);
 040    }
 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    {
 049        if (page < 1) page = 1;
 050        if (pageSize < 1) pageSize = 20;
 051        if (pageSize > 100) pageSize = 100;
 52
 053        var (items, totalCount) = await _acreedorRepository.GetPagedAsync(page, pageSize, searchQuery, activo, categoria
 54
 055        var dtos = items.Select(a => new AcreedorDto
 056        {
 057            Id = a.Id,
 058            Codigo = a.Codigo,
 059            Nombre = a.Nombre,
 060            CategoriaDeficit = a.CategoriaDeficit,
 061            OrdenLegalDeficit = a.OrdenLegalDeficit,
 062            FormatoArchivo = a.FormatoArchivo,
 063            Contacto = a.Contacto,
 064            Activo = a.Activo,
 065        }).ToList();
 66
 067        return new PagedResult<AcreedorDto>
 068        {
 069            Items = dtos,
 070            Page = page,
 071            PageSize = pageSize,
 072            TotalCount = totalCount,
 073        };
 074    }
 75
 76    public async Task<(bool Success, Acreedor? Acreedor, string? Error)> CreateAsync(CreateAcreedorRequest request)
 77    {
 78        try
 79        {
 080            if (string.IsNullOrWhiteSpace(request.Codigo))
 081                return (false, null, "El código es requerido");
 82
 083            if (string.IsNullOrWhiteSpace(request.Nombre))
 084                return (false, null, "El nombre es requerido");
 85
 086            if (!_categoriasValidas.Contains(request.CategoriaDeficit))
 087                return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida");
 88
 089            if (await _acreedorRepository.ExistsCodigoAsync(request.Codigo))
 090                return (false, null, "Ya existe un acreedor con ese código");
 91
 092            var acreedor = new Acreedor
 093            {
 094                Codigo = request.Codigo.Trim().ToUpper(),
 095                Nombre = request.Nombre.Trim(),
 096                CategoriaDeficit = request.CategoriaDeficit,
 097                OrdenLegalDeficit = request.OrdenLegalDeficit,
 098                FormatoArchivo = request.FormatoArchivo ?? "txt",
 099                Contacto = request.Contacto?.Trim(),
 0100                Activo = true,
 0101            };
 102
 0103            var creado = await _acreedorRepository.CreateAsync(acreedor);
 104
 0105            await _auditoriaService.LogAuditoriaAsync(
 0106                usuarioId: _currentUser.UserId ?? 0,
 0107                AccionEnum.CrearAcreedor,
 0108                ContextoEnum.Acreedores,
 0109                host: _currentUser.Host,
 0110                entidad: nameof(Acreedor),
 0111                entidadId: creado.Id,
 0112                detalle: new { operacion = "crear", id = creado.Id, codigo = creado.Codigo, nombre = creado.Nombre });
 113
 0114            return (true, creado, null);
 115        }
 0116        catch (Exception ex)
 117        {
 0118            return (false, null, $"Error al crear acreedor: {ex.Message}");
 119        }
 0120    }
 121
 122    public async Task<(bool Success, Acreedor? Acreedor, string? Error)> UpdateAsync(long id, UpdateAcreedorRequest requ
 123    {
 124        try
 125        {
 0126            var acreedor = await _acreedorRepository.GetByIdAsync(id);
 0127            if (acreedor == null)
 0128                return (false, null, "Acreedor no encontrado");
 129
 0130            if (request.CategoriaDeficit != null && !_categoriasValidas.Contains(request.CategoriaDeficit))
 0131                return (false, null, $"La categoría '{request.CategoriaDeficit}' no es válida");
 132
 0133            var snapshotAntes = new
 0134            {
 0135                id = acreedor.Id,
 0136                codigo = acreedor.Codigo,
 0137                nombre = acreedor.Nombre,
 0138                categoriaDeficit = acreedor.CategoriaDeficit,
 0139                ordenLegalDeficit = acreedor.OrdenLegalDeficit,
 0140                formatoArchivo = acreedor.FormatoArchivo,
 0141                contacto = acreedor.Contacto,
 0142                activo = acreedor.Activo,
 0143            };
 144
 0145            if (!string.IsNullOrWhiteSpace(request.Nombre))
 0146                acreedor.Nombre = request.Nombre.Trim();
 147
 0148            if (request.CategoriaDeficit != null)
 0149                acreedor.CategoriaDeficit = request.CategoriaDeficit;
 150
 0151            if (request.OrdenLegalDeficit.HasValue)
 0152                acreedor.OrdenLegalDeficit = request.OrdenLegalDeficit.Value;
 153
 0154            if (request.FormatoArchivo != null)
 0155                acreedor.FormatoArchivo = request.FormatoArchivo;
 156
 0157            if (request.Contacto != null)
 0158                acreedor.Contacto = request.Contacto.Trim();
 159
 0160            if (request.Activo.HasValue)
 0161                acreedor.Activo = request.Activo.Value;
 162
 0163            var actualizado = await _acreedorRepository.UpdateAsync(acreedor);
 164
 0165            await _auditoriaService.LogAuditoriaAsync(
 0166                usuarioId: _currentUser.UserId ?? 0,
 0167                AccionEnum.ActualizarAcreedor,
 0168                ContextoEnum.Acreedores,
 0169                host: _currentUser.Host,
 0170                entidad: nameof(Acreedor),
 0171                entidadId: actualizado.Id,
 0172                detalle: new
 0173                {
 0174                    operacion = "editar",
 0175                    antes = snapshotAntes,
 0176                    despues = new
 0177                    {
 0178                        id = actualizado.Id,
 0179                        codigo = actualizado.Codigo,
 0180                        nombre = actualizado.Nombre,
 0181                        categoriaDeficit = actualizado.CategoriaDeficit,
 0182                        ordenLegalDeficit = actualizado.OrdenLegalDeficit,
 0183                        formatoArchivo = actualizado.FormatoArchivo,
 0184                        contacto = actualizado.Contacto,
 0185                        activo = actualizado.Activo,
 0186                    }
 0187                });
 188
 0189            return (true, actualizado, null);
 190        }
 0191        catch (Exception ex)
 192        {
 0193            return (false, null, $"Error al actualizar acreedor: {ex.Message}");
 194        }
 0195    }
 196}