| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class InstructivoBeneficiosService : IInstructivoBeneficiosService |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 16 | 11 | | public InstructivoBeneficiosService(ApplicationDbContext context) |
| | 16 | 12 | | => _context = context; |
| | | 13 | | |
| | | 14 | | // ── Asignación Familiar ─────────────────────────────────────────────────── |
| | | 15 | | |
| | | 16 | | public async Task<IReadOnlyList<InstructivoAsigFamiliar>> ObtenerAsigFamiliarAsync(bool soloVigente = true) |
| | | 17 | | { |
| | 2 | 18 | | var todos = await _context.InstructivosAsigFamiliar.AsNoTracking().ToListAsync(); |
| | | 19 | | |
| | | 20 | | // soloVigente=false: todos los registros ordenados más reciente primero (para la UI de admin) |
| | 7 | 21 | | 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) |
| | 1 | 24 | | var hoy = DateTime.Now.Date; |
| | 1 | 25 | | return todos |
| | 4 | 26 | | .Where(i => i.VigenteDesde <= hoy) |
| | 4 | 27 | | .GroupBy(i => i.TipoFranja) |
| | 7 | 28 | | .Select(g => g.OrderByDescending(i => i.VigenteDesde).First()) |
| | 3 | 29 | | .OrderBy(i => i.TipoFranja) |
| | 1 | 30 | | .ToList(); |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<IReadOnlyList<InstructivoAsigFamiliar>> RegistrarAsigFamiliarAsync( |
| | | 34 | | DateTime vigenteDesde, decimal franja8, decimal franja16, decimal discapacidad) |
| | | 35 | | { |
| | 5 | 36 | | if (franja8 <= 0) throw new ArgumentException("El importe franja 8% debe ser mayor a 0"); |
| | 4 | 37 | | if (franja16 <= 0) throw new ArgumentException("El importe franja 16% debe ser mayor a 0"); |
| | 3 | 38 | | if (discapacidad <= 0) throw new ArgumentException("El importe discapacidad debe ser mayor a 0"); |
| | | 39 | | |
| | 1 | 40 | | var nuevas = new[] |
| | 1 | 41 | | { |
| | 1 | 42 | | new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Franja8, Importe = franja8, Vige |
| | 1 | 43 | | new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Franja16, Importe = franja16, Vige |
| | 1 | 44 | | new InstructivoAsigFamiliar { TipoFranja = TipoFranjaAsigFamiliar.Discapacidad, Importe = discapacidad, Vige |
| | 1 | 45 | | }; |
| | | 46 | | |
| | 1 | 47 | | await _context.InstructivosAsigFamiliar.AddRangeAsync(nuevas); |
| | 1 | 48 | | await _context.SaveChangesAsync(); |
| | 1 | 49 | | return nuevas; |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | // ── PSF ─────────────────────────────────────────────────────────────────── |
| | | 53 | | |
| | | 54 | | public async Task<IReadOnlyList<InstructivoPsf>> ObtenerPsfAsync(bool soloVigente = true) |
| | | 55 | | { |
| | 2 | 56 | | var todos = await _context.InstructivosPsf.AsNoTracking().ToListAsync(); |
| | 7 | 57 | | if (!soloVigente) return todos.OrderByDescending(i => i.VigenteDesde).ThenBy(i => i.NumeroCategoria).ToList(); |
| | | 58 | | |
| | 1 | 59 | | var hoy = DateTime.Now.Date; |
| | 1 | 60 | | return todos |
| | 3 | 61 | | .Where(i => i.VigenteDesde <= hoy) |
| | 3 | 62 | | .GroupBy(i => (int)i.NumeroCategoria) |
| | 5 | 63 | | .Select(g => g.OrderByDescending(i => i.VigenteDesde).First()) |
| | 2 | 64 | | .OrderBy(i => i.NumeroCategoria) |
| | 1 | 65 | | .ToList(); |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<IReadOnlyList<InstructivoPsf>> RegistrarPsfAsync( |
| | | 69 | | DateTime vigenteDesde, |
| | | 70 | | IReadOnlyList<(short Categoria, string Descripcion, decimal Importe)> categorias) |
| | | 71 | | { |
| | 15 | 72 | | var categoriasDistintas = categorias.Select(c => c.Categoria).Distinct().ToList(); |
| | 13 | 73 | | if (categoriasDistintas.Count != 5 || !new[] { 1, 2, 3, 4, 5 }.All(n => categoriasDistintas.Contains((short)n))) |
| | 1 | 74 | | throw new InvalidOperationException("El instructivo PSF debe incluir exactamente las 5 categorías (1-5)"); |
| | | 75 | | |
| | 8 | 76 | | if (categorias.Any(c => c.Importe <= 0)) |
| | 1 | 77 | | throw new ArgumentException("Todos los importes deben ser mayores a 0"); |
| | | 78 | | |
| | 6 | 79 | | var nuevas = categorias.Select(c => new InstructivoPsf |
| | 6 | 80 | | { |
| | 6 | 81 | | NumeroCategoria = c.Categoria, |
| | 6 | 82 | | Descripcion = c.Descripcion, |
| | 6 | 83 | | Importe = c.Importe, |
| | 6 | 84 | | VigenteDesde = vigenteDesde |
| | 6 | 85 | | }).ToList(); |
| | | 86 | | |
| | 1 | 87 | | await _context.InstructivosPsf.AddRangeAsync(nuevas); |
| | 1 | 88 | | await _context.SaveChangesAsync(); |
| | 1 | 89 | | return nuevas; |
| | 1 | 90 | | } |
| | | 91 | | |
| | | 92 | | // ── Hogar Constituido ───────────────────────────────────────────────────── |
| | | 93 | | |
| | | 94 | | public async Task<IReadOnlyList<TablaHogarConstituido>> ObtenerHogarAsync(bool soloVigente = true) |
| | | 95 | | { |
| | 2 | 96 | | var todos = await _context.TablasHogarConstituido.AsNoTracking().ToListAsync(); |
| | 2 | 97 | | if (!soloVigente) |
| | 5 | 98 | | return todos.OrderByDescending(t => t.VigenteDesde).ThenBy(t => t.HastaHaberes ?? decimal.MaxValue).ToList() |
| | | 99 | | |
| | 1 | 100 | | var hoy = DateTime.Now.Date; |
| | 1 | 101 | | return todos |
| | 3 | 102 | | .Where(t => t.VigenteDesde <= hoy) |
| | 3 | 103 | | .GroupBy(t => t.HastaHaberes) |
| | 5 | 104 | | .Select(g => g.OrderByDescending(t => t.VigenteDesde).First()) |
| | 2 | 105 | | .OrderBy(t => t.HastaHaberes ?? decimal.MaxValue) |
| | 1 | 106 | | .ToList(); |
| | 2 | 107 | | } |
| | | 108 | | |
| | | 109 | | public async Task<IReadOnlyList<TablaHogarConstituido>> RegistrarHogarAsync( |
| | | 110 | | DateTime vigenteDesde, |
| | | 111 | | IReadOnlyList<(decimal? HastaHaberes, decimal Importe, string? RefPorcentaje)> tramos) |
| | | 112 | | { |
| | 9 | 113 | | if (!tramos.Any(t => t.HastaHaberes is null)) |
| | 1 | 114 | | throw new InvalidOperationException("La tabla de Hogar Constituido debe incluir al menos un tramo sin tope ( |
| | | 115 | | |
| | 5 | 116 | | if (tramos.Any(t => t.Importe <= 0)) |
| | 1 | 117 | | throw new ArgumentException("Todos los importes deben ser mayores a 0"); |
| | | 118 | | |
| | 3 | 119 | | var nuevas = tramos.Select(t => new TablaHogarConstituido |
| | 3 | 120 | | { |
| | 3 | 121 | | HastaHaberes = t.HastaHaberes, |
| | 3 | 122 | | Importe = t.Importe, |
| | 3 | 123 | | RefPorcentaje = t.RefPorcentaje, |
| | 3 | 124 | | VigenteDesde = vigenteDesde |
| | 3 | 125 | | }).ToList(); |
| | | 126 | | |
| | 1 | 127 | | await _context.TablasHogarConstituido.AddRangeAsync(nuevas); |
| | 1 | 128 | | await _context.SaveChangesAsync(); |
| | 1 | 129 | | return nuevas; |
| | 1 | 130 | | } |
| | | 131 | | } |