< Summary

Information
Class: FAU.Logica.Services.InstructivoBeneficiosService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/InstructivoBeneficiosService.cs
Line coverage
58%
Covered lines: 45
Uncovered lines: 32
Coverable lines: 77
Total lines: 131
Line coverage: 58.4%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ObtenerAsigFamiliarAsync()0%620%
RegistrarAsigFamiliarAsync()66.66%66100%
ObtenerPsfAsync()0%620%
RegistrarPsfAsync()83.33%6683.33%
ObtenerHogarAsync()0%620%
RegistrarHogarAsync()100%44100%

File(s)

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

#LineLine coverage
 1using FAU.DataAccess;
 2using FAU.Entidades;
 3using Microsoft.EntityFrameworkCore;
 4
 5namespace FAU.Logica.Services;
 6
 7public class InstructivoBeneficiosService : IInstructivoBeneficiosService
 8{
 9    private readonly ApplicationDbContext _context;
 10
 711    public InstructivoBeneficiosService(ApplicationDbContext context)
 712        => _context = context;
 13
 14    // ── Asignación Familiar ───────────────────────────────────────────────────
 15
 16    public async Task<IReadOnlyList<InstructivoAsigFamiliar>> ObtenerAsigFamiliarAsync(bool soloVigente = true)
 17    {
 018        var todos = await _context.InstructivosAsigFamiliar.AsNoTracking().ToListAsync();
 19
 20        // soloVigente=false: todos los registros ordenados más reciente primero (para la UI de admin)
 021        if (!soloVigente) return todos.OrderByDescending(i => i.VigenteDesde).ThenBy(i => i.TipoFranja).ToList();
 22
 23        // soloVigente=true: solo el vigente HOY (para cálculos internos)
 024        var hoy = DateTime.Now.Date;
 025        return todos
 026            .Where(i => i.VigenteDesde <= hoy)
 027            .GroupBy(i => i.TipoFranja)
 028            .Select(g => g.OrderByDescending(i => i.VigenteDesde).First())
 029            .OrderBy(i => i.TipoFranja)
 030            .ToList();
 031    }
 32
 33    public async Task<IReadOnlyList<InstructivoAsigFamiliar>> RegistrarAsigFamiliarAsync(
 34        DateTime vigenteDesde, decimal franja8, decimal franja16, decimal discapacidad)
 35    {
 336        if (franja8 <= 0) throw new ArgumentException("El importe franja 8% debe ser mayor a 0");
 137        if (franja16 <= 0) throw new ArgumentException("El importe franja 16% debe ser mayor a 0");
 138        if (discapacidad <= 0) throw new ArgumentException("El importe discapacidad debe ser mayor a 0");
 39
 140        var nuevas = new[]
 141        {
 142            new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Franja8,      Importe = franja8,      Vige
 143            new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Franja16,     Importe = franja16,     Vige
 144            new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Discapacidad, Importe = discapacidad, Vige
 145        };
 46
 147        await _context.InstructivosAsigFamiliar.AddRangeAsync(nuevas);
 148        await _context.SaveChangesAsync();
 149        return nuevas;
 150    }
 51
 52    // ── PSF ───────────────────────────────────────────────────────────────────
 53
 54    public async Task<IReadOnlyList<InstructivoPsf>> ObtenerPsfAsync(bool soloVigente = true)
 55    {
 056        var todos = await _context.InstructivosPsf.AsNoTracking().ToListAsync();
 057        if (!soloVigente) return todos.OrderByDescending(i => i.VigenteDesde).ThenBy(i => i.NumeroCategoria).ToList();
 58
 059        var hoy = DateTime.Now.Date;
 060        return todos
 061            .Where(i => i.VigenteDesde <= hoy)
 062            .GroupBy(i => (int)i.NumeroCategoria)
 063            .Select(g => g.OrderByDescending(i => i.VigenteDesde).First())
 064            .OrderBy(i => i.NumeroCategoria)
 065            .ToList();
 066    }
 67
 68    public async Task<IReadOnlyList<InstructivoPsf>> RegistrarPsfAsync(
 69        DateTime vigenteDesde,
 70        IReadOnlyList<(short Categoria, string Descripcion, decimal Importe)> categorias)
 71    {
 972        var categoriasDistintas = categorias.Select(c => c.Categoria).Distinct().ToList();
 773        if (categoriasDistintas.Count != 5 || !new[] { 1, 2, 3, 4, 5 }.All(n => categoriasDistintas.Contains((short)n)))
 174            throw new InvalidOperationException("El instructivo PSF debe incluir exactamente las 5 categorías (1-5)");
 75
 676        if (categorias.Any(c => c.Importe <= 0))
 077            throw new ArgumentException("Todos los importes deben ser mayores a 0");
 78
 679        var nuevas = categorias.Select(c => new InstructivoPsf
 680        {
 681            NumeroCategoria = c.Categoria,
 682            Descripcion = c.Descripcion,
 683            Importe = c.Importe,
 684            VigenteDesde = vigenteDesde
 685        }).ToList();
 86
 187        await _context.InstructivosPsf.AddRangeAsync(nuevas);
 188        await _context.SaveChangesAsync();
 189        return nuevas;
 190    }
 91
 92    // ── Hogar Constituido ─────────────────────────────────────────────────────
 93
 94    public async Task<IReadOnlyList<TablaHogarConstituido>> ObtenerHogarAsync(bool soloVigente = true)
 95    {
 096        var todos = await _context.TablasHogarConstituido.AsNoTracking().ToListAsync();
 097        if (!soloVigente)
 098            return todos.OrderByDescending(t => t.VigenteDesde).ThenBy(t => t.HastaHaberes ?? decimal.MaxValue).ToList()
 99
 0100        var hoy = DateTime.Now.Date;
 0101        return todos
 0102            .Where(t => t.VigenteDesde <= hoy)
 0103            .GroupBy(t => t.HastaHaberes)
 0104            .Select(g => g.OrderByDescending(t => t.VigenteDesde).First())
 0105            .OrderBy(t => t.HastaHaberes ?? decimal.MaxValue)
 0106            .ToList();
 0107    }
 108
 109    public async Task<IReadOnlyList<TablaHogarConstituido>> RegistrarHogarAsync(
 110        DateTime vigenteDesde,
 111        IReadOnlyList<(decimal? HastaHaberes, decimal Importe, string? RefPorcentaje)> tramos)
 112    {
 9113        if (!tramos.Any(t => t.HastaHaberes is null))
 1114            throw new InvalidOperationException("La tabla de Hogar Constituido debe incluir al menos un tramo sin tope (
 115
 5116        if (tramos.Any(t => t.Importe <= 0))
 1117            throw new ArgumentException("Todos los importes deben ser mayores a 0");
 118
 3119        var nuevas = tramos.Select(t => new TablaHogarConstituido
 3120        {
 3121            HastaHaberes = t.HastaHaberes,
 3122            Importe = t.Importe,
 3123            RefPorcentaje = t.RefPorcentaje,
 3124            VigenteDesde = vigenteDesde
 3125        }).ToList();
 126
 1127        await _context.TablasHogarConstituido.AddRangeAsync(nuevas);
 1128        await _context.SaveChangesAsync();
 1129        return nuevas;
 1130    }
 131}