< Summary

Information
Class: FAU.Logica.Services.DescuentoPersonalService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/DescuentoPersonalService.cs
Line coverage
88%
Covered lines: 64
Uncovered lines: 8
Coverable lines: 72
Total lines: 126
Line coverage: 88.8%
Branch coverage
66%
Covered branches: 24
Total branches: 36
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CrearAsync()77.77%181896.87%
EditarAsync()66.66%6690%
EliminarAsync()75%44100%
ConfirmarAsync()75%44100%
ListarAsync()0%2040%
ConfirmarBorradoresAsync(...)100%11100%

File(s)

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

#LineLine coverage
 1using FAU.DataAccess;
 2using FAU.DataAccess.Repositories;
 3using FAU.Entidades;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace FAU.Logica.Services;
 7
 8public class DescuentoPersonalService : IDescuentoPersonalService
 9{
 10    private readonly IDescuentoPersonalRepository _repo;
 11    private readonly IPersonalRepository          _personalRepo;
 12    private readonly IPeriodosRepository          _periodosRepo;
 13    private readonly ApplicationDbContext         _context;
 14
 1215    public DescuentoPersonalService(
 1216        IDescuentoPersonalRepository repo,
 1217        IPersonalRepository personalRepo,
 1218        IPeriodosRepository periodosRepo,
 1219        ApplicationDbContext context)
 20    {
 1221        _repo         = repo;
 1222        _personalRepo = personalRepo;
 1223        _periodosRepo = periodosRepo;
 1224        _context      = context;
 1225    }
 26
 27    public async Task<DescuentoPersonalPeriodo> CrearAsync(
 28        long periodoId, string cedulaTitular, int codigoConcepto, long acreedorId,
 29        decimal importe, string? observaciones, long usuarioId)
 30    {
 531        if (importe <= 0)
 132            throw new ArgumentException("El importe debe ser mayor a cero.");
 33
 434        if (acreedorId <= 0)
 035            throw new ArgumentException("El acreedor es requerido.");
 36
 437        var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular)
 438            ?? throw new ArgumentException($"Funcionario {cedulaTitular} no encontrado.");
 39
 440        var periodo = await _periodosRepo.ObtenerPorIdAsync(periodoId)
 441            ?? throw new ArgumentException($"Período {periodoId} no encontrado.");
 42
 443        if (periodo.Estado != "ABIERTA" && periodo.Estado != "LISTA_CALCULO")
 144            throw new InvalidOperationException($"El período está en estado {periodo.Estado} y no admite modificaciones.
 45
 346        var catalogoItem = await _context.CatalogoItems
 347            .FirstOrDefaultAsync(c => c.Codigo == codigoConcepto && c.Vigente)
 348            ?? throw new ArgumentException($"Concepto con código {codigoConcepto} no encontrado o no vigente.");
 49
 350        if (catalogoItem.Tipo != "descuento_personal")
 151            throw new InvalidOperationException($"El concepto {codigoConcepto} (tipo '{catalogoItem.Tipo}') no es un des
 52
 253        var acreedor = await _context.Acreedores
 254            .AsNoTracking()
 255            .FirstOrDefaultAsync(a => a.Id == acreedorId && a.Activo)
 256            ?? throw new ArgumentException($"Acreedor {acreedorId} no encontrado o no activo.");
 57
 158        var descuento = new DescuentoPersonalPeriodo
 159        {
 160            PeriodoId         = periodoId,
 161            PersonaId         = relacion.PersonaId,
 162            AcreedorId        = acreedor.Id,
 163            CatalogoItemId    = catalogoItem.Id,
 164            Importe           = importe,
 165            Estado            = "borrador",
 166            Observaciones     = observaciones,
 167            UsuarioCreacionId = usuarioId
 168        };
 69
 170        return await _repo.CreateAsync(descuento);
 171    }
 72
 73    public async Task<DescuentoPersonalPeriodo> EditarAsync(long id, decimal importe, string? observaciones)
 74    {
 275        if (importe <= 0)
 076            throw new ArgumentException("El importe debe ser mayor a cero.");
 77
 278        var d = await _repo.GetByIdAsync(id)
 279            ?? throw new ArgumentException($"Descuento {id} no encontrado.");
 80
 281        if (d.Estado != "borrador")
 182            throw new InvalidOperationException("Solo se pueden editar descuentos en estado borrador.");
 83
 184        d.Importe      = importe;
 185        d.Observaciones = observaciones;
 186        return await _repo.UpdateAsync(d);
 187    }
 88
 89    public async Task EliminarAsync(long id)
 90    {
 291        var d = await _repo.GetByIdAsync(id)
 292            ?? throw new ArgumentException($"Descuento {id} no encontrado.");
 93
 294        if (d.Estado != "borrador")
 195            throw new InvalidOperationException("Solo se pueden eliminar descuentos en estado borrador.");
 96
 197        await _repo.DeleteAsync(id);
 198    }
 99
 100    public async Task<DescuentoPersonalPeriodo> ConfirmarAsync(long id)
 101    {
 2102        var descuento = await _repo.GetByIdAsync(id)
 2103            ?? throw new ArgumentException($"Descuento {id} no encontrado.");
 104
 2105        if (descuento.Estado != "borrador")
 1106            throw new InvalidOperationException("Solo se pueden confirmar descuentos en estado borrador.");
 107
 1108        descuento.Estado = "confirmado";
 1109        return await _repo.UpdateAsync(descuento);
 1110    }
 111
 112    public async Task<IEnumerable<DescuentoPersonalPeriodo>> ListarAsync(
 113        long periodoId, string? cedulaTitular, int? codigoConcepto, string? estado)
 114    {
 0115        long? personaId = null;
 0116        if (!string.IsNullOrEmpty(cedulaTitular))
 117        {
 0118            var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular);
 0119            personaId = relacion?.PersonaId;
 120        }
 0121        return await _repo.GetByPeriodoAsync(periodoId, personaId, codigoConcepto, estado);
 0122    }
 123
 124    public Task<int> ConfirmarBorradoresAsync(long periodoId) =>
 1125        _repo.ConfirmarBorradoresAsync(periodoId);
 126}