< 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
87%
Covered lines: 51
Uncovered lines: 7
Coverable lines: 58
Total lines: 105
Line coverage: 87.9%
Branch coverage
64%
Covered branches: 18
Total branches: 28
Branch coverage: 64.2%
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()78.57%1414100%
EditarAsync()66.66%6690%
EliminarAsync()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
 915    public DescuentoPersonalService(
 916        IDescuentoPersonalRepository repo,
 917        IPersonalRepository personalRepo,
 918        IPeriodosRepository periodosRepo,
 919        ApplicationDbContext context)
 20    {
 921        _repo         = repo;
 922        _personalRepo = personalRepo;
 923        _periodosRepo = periodosRepo;
 924        _context      = context;
 925    }
 26
 27    public async Task<DescuentoPersonalPeriodo> CrearAsync(
 28        long periodoId, string cedulaTitular, int codigoConcepto,
 29        decimal importe, string? observaciones, long usuarioId)
 30    {
 431        if (importe <= 0)
 132            throw new ArgumentException("El importe debe ser mayor a cero.");
 33
 334        var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular)
 335            ?? throw new ArgumentException($"Funcionario {cedulaTitular} no encontrado.");
 36
 337        var periodo = await _periodosRepo.ObtenerPorIdAsync(periodoId)
 338            ?? throw new ArgumentException($"Período {periodoId} no encontrado.");
 39
 340        if (periodo.Estado != "ABIERTA" && periodo.Estado != "LISTA_CALCULO")
 141            throw new InvalidOperationException($"El período está en estado {periodo.Estado} y no admite modificaciones.
 42
 243        var catalogoItem = await _context.CatalogoItems
 244            .FirstOrDefaultAsync(c => c.Codigo == codigoConcepto && c.Vigente)
 245            ?? throw new ArgumentException($"Concepto con código {codigoConcepto} no encontrado o no vigente.");
 46
 247        if (catalogoItem.Tipo != "descuento_personal")
 148            throw new InvalidOperationException($"El concepto {codigoConcepto} (tipo '{catalogoItem.Tipo}') no es un des
 49
 150        var descuento = new DescuentoPersonalPeriodo
 151        {
 152            PeriodoId         = periodoId,
 153            PersonaId         = relacion.PersonaId,
 154            CatalogoItemId    = catalogoItem.Id,
 155            Importe           = importe,
 156            Estado            = "borrador",
 157            Observaciones     = observaciones,
 158            UsuarioCreacionId = usuarioId
 159        };
 60
 161        return await _repo.CreateAsync(descuento);
 162    }
 63
 64    public async Task<DescuentoPersonalPeriodo> EditarAsync(long id, decimal importe, string? observaciones)
 65    {
 266        if (importe <= 0)
 067            throw new ArgumentException("El importe debe ser mayor a cero.");
 68
 269        var d = await _repo.GetByIdAsync(id)
 270            ?? throw new ArgumentException($"Descuento {id} no encontrado.");
 71
 272        if (d.Estado != "borrador")
 173            throw new InvalidOperationException("Solo se pueden editar descuentos en estado borrador.");
 74
 175        d.Importe      = importe;
 176        d.Observaciones = observaciones;
 177        return await _repo.UpdateAsync(d);
 178    }
 79
 80    public async Task EliminarAsync(long id)
 81    {
 282        var d = await _repo.GetByIdAsync(id)
 283            ?? throw new ArgumentException($"Descuento {id} no encontrado.");
 84
 285        if (d.Estado != "borrador")
 186            throw new InvalidOperationException("Solo se pueden eliminar descuentos en estado borrador.");
 87
 188        await _repo.DeleteAsync(id);
 189    }
 90
 91    public async Task<IEnumerable<DescuentoPersonalPeriodo>> ListarAsync(
 92        long periodoId, string? cedulaTitular, int? codigoConcepto, string? estado)
 93    {
 094        long? personaId = null;
 095        if (!string.IsNullOrEmpty(cedulaTitular))
 96        {
 097            var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular);
 098            personaId = relacion?.PersonaId;
 99        }
 0100        return await _repo.GetByPeriodoAsync(periodoId, personaId, codigoConcepto, estado);
 0101    }
 102
 103    public Task<int> ConfirmarBorradoresAsync(long periodoId) =>
 1104        _repo.ConfirmarBorradoresAsync(periodoId);
 105}