< Summary

Information
Class: FAU.Logica.Services.LiquidacionService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/LiquidacionService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 102
Coverable lines: 102
Total lines: 149
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GenerarLiquidacionAsync()0%7280%
ObtenerEstadosAsync()100%210%
ObtenerItemsAsync()100%210%
ObtenerResumenAsync()100%210%

File(s)

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

#LineLine coverage
 1using FAU.DataAccess;
 2using FAU.Entidades;
 3using FAU.Logica.DTOs;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace FAU.Logica.Services;
 7
 8public class LiquidacionService : ILiquidacionService
 9{
 10    private readonly ApplicationDbContext _context;
 11    private readonly IAuditoriaService _auditoriaService;
 12    private readonly ICurrentUserContext _currentUser;
 13
 014    public LiquidacionService(
 015        ApplicationDbContext context,
 016        IAuditoriaService auditoriaService,
 017        ICurrentUserContext currentUser)
 18    {
 019        _context = context;
 020        _auditoriaService = auditoriaService;
 021        _currentUser = currentUser;
 022    }
 23
 24    public async Task<(bool Exito, string? Error)> GenerarLiquidacionAsync(long periodoId, int version)
 25    {
 026        var periodo = await _context.PeriodosLiquidacion
 027            .AsNoTracking()
 028            .FirstOrDefaultAsync(p => p.Id == periodoId && p.Activo);
 29
 030        if (periodo is null)
 31        {
 032            return (false, "Período de liquidación no encontrado o no está activo");
 33        }
 34
 035        if (version <= 0)
 36        {
 037            return (false, "La versión debe ser mayor a cero");
 38        }
 39
 040        var relacionesActivas = await _context.RelacionesLaborales
 041            .AsNoTracking()
 042            .Where(r => r.Estado == EstadoRelacion.Activo)
 043            .Join(
 044                _context.Personas.AsNoTracking(),
 045                r => r.PersonaId,
 046                p => p.Id,
 047                (r, p) => new { p.Cedula }
 048            )
 049            .Distinct()
 050            .ToListAsync();
 51
 052        if (!relacionesActivas.Any())
 53        {
 054            return (false, "No se encontraron relaciones laborales activas para generar la liquidación");
 55        }
 56
 057        var existItems = await _context.LiquidacionItems
 058            .AnyAsync(li => li.PeriodoId == periodoId && li.Version == version);
 59
 060        if (existItems)
 61        {
 062            return (false, "Ya existe una liquidación generada para ese período y versión");
 63        }
 64
 065        var estados = relacionesActivas.Select(r => new LiquidacionEstadoPersonal
 066        {
 067            PeriodoId = periodoId,
 068            Cedula = r.Cedula,
 069            Version = version,
 070            Estado = "PENDIENTE",
 071            FechaActualizacion = DateTime.UtcNow
 072        }).ToList();
 73
 074        await _context.LiquidacionEstadosPersonal.AddRangeAsync(estados);
 75
 076        var resumen = new LiquidacionResumen
 077        {
 078            PeriodoId = periodoId,
 079            Version = version,
 080            TotalImporte = 0m,
 081            Rubro083 = false,
 082            Descripcion = "Resumen inicial de liquidación"
 083        };
 84
 085        await _context.LiquidacionResumenes.AddAsync(resumen);
 086        await _context.SaveChangesAsync();
 87
 088        await _auditoriaService.LogAuditoriaAsync(
 089            _currentUser.UserId ?? 0,
 090            AccionEnum.GenerarLiquidacion,
 091            ContextoEnum.Liquidaciones,
 092            _currentUser.Host,
 093            entidad: nameof(LiquidacionEstadoPersonal),
 094            entidadId: periodoId,
 095            detalle: new { periodoId, version, registros = estados.Count });
 96
 097        return (true, null);
 098    }
 99
 100    public async Task<IEnumerable<CalculoEstadoDto>> ObtenerEstadosAsync(long periodoId, int version)
 101    {
 0102        return await _context.LiquidacionEstadosPersonal
 0103            .AsNoTracking()
 0104            .Where(e => e.PeriodoId == periodoId && e.Version == version)
 0105            .Select(e => new CalculoEstadoDto
 0106            {
 0107                Cedula = e.Cedula,
 0108                Version = e.Version,
 0109                Estado = e.Estado,
 0110                MensajeError = e.MensajeError
 0111            })
 0112            .ToListAsync();
 0113    }
 114
 115    public async Task<IEnumerable<CalculoItemDto>> ObtenerItemsAsync(long periodoId, int version)
 116    {
 0117        return await _context.LiquidacionItems
 0118            .AsNoTracking()
 0119            .Where(i => i.PeriodoId == periodoId && i.Version == version)
 0120            .Select(i => new CalculoItemDto
 0121            {
 0122                Cedula = i.Cedula,
 0123                Version = i.Version,
 0124                CodigoConcepto = i.CodigoConcepto,
 0125                NombreConcepto = i.NombreConcepto,
 0126                Importe = i.Importe,
 0127                RubroContable = i.RubroContable,
 0128                ObjetoGasto = i.ObjetoGasto
 0129            })
 0130            .ToListAsync();
 0131    }
 132
 133    public async Task<IEnumerable<LiquidacionResumenDto>> ObtenerResumenAsync(long periodoId, int version)
 134    {
 0135        return await _context.LiquidacionResumenes
 0136            .AsNoTracking()
 0137            .Where(r => r.PeriodoId == periodoId && r.Version == version)
 0138            .Select(r => new LiquidacionResumenDto
 0139            {
 0140                Version = r.Version,
 0141                ProgramaId = r.ProgramaId,
 0142                RegimenId = r.RegimenId,
 0143                Rubro083 = r.Rubro083,
 0144                TotalImporte = r.TotalImporte,
 0145                Descripcion = r.Descripcion
 0146            })
 0147            .ToListAsync();
 0148    }
 149}