| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class IrpfRepository : IIrpfRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 25 | 10 | | public IrpfRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 25 | 12 | | _context = context; |
| | 25 | 13 | | } |
| | | 14 | | |
| | | 15 | | // ── Franjas ────────────────────────────────────────────────────────────── |
| | | 16 | | |
| | | 17 | | public async Task<IEnumerable<FranjaIrpf>> GetFranjasVigentesAsync(DateTime? fecha = null) |
| | | 18 | | { |
| | 2 | 19 | | var fechaRef = fecha?.Date ?? DateTime.UtcNow.Date; |
| | 2 | 20 | | return await _context.FranjasIrpf |
| | 2 | 21 | | .Where(f => f.VigenteDesde <= fechaRef && (f.VigenteHasta == null || f.VigenteHasta >= fechaRef)) |
| | 2 | 22 | | .OrderBy(f => f.NumeroFranja) |
| | 2 | 23 | | .ToListAsync(); |
| | 2 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<FranjaIrpf?> GetFranjaByIdAsync(long id) |
| | | 27 | | { |
| | 4 | 28 | | return await _context.FranjasIrpf.FindAsync(id); |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<FranjaIrpf?> GetFranjaActivaByNumeroAsync(short numeroFranja) |
| | | 32 | | { |
| | 2 | 33 | | return await _context.FranjasIrpf |
| | 2 | 34 | | .Where(f => f.NumeroFranja == numeroFranja && f.VigenteHasta == null) |
| | 2 | 35 | | .FirstOrDefaultAsync(); |
| | 2 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<bool> ExisteFranjaAsync(short numeroFranja, DateTime vigenteDesde) |
| | | 39 | | { |
| | 2 | 40 | | return await _context.FranjasIrpf |
| | 2 | 41 | | .AnyAsync(f => f.NumeroFranja == numeroFranja && f.VigenteDesde.Date == vigenteDesde.Date); |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<FranjaIrpf> CreateFranjaAsync(FranjaIrpf franja) |
| | | 45 | | { |
| | 4 | 46 | | _context.FranjasIrpf.Add(franja); |
| | 4 | 47 | | await _context.SaveChangesAsync(); |
| | 4 | 48 | | return franja; |
| | 4 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<FranjaIrpf> UpdateFranjaAsync(FranjaIrpf franja) |
| | | 52 | | { |
| | 1 | 53 | | _context.FranjasIrpf.Update(franja); |
| | 1 | 54 | | await _context.SaveChangesAsync(); |
| | 1 | 55 | | return franja; |
| | 1 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task DeleteFranjaAsync(long id) |
| | | 59 | | { |
| | 2 | 60 | | var franja = await _context.FranjasIrpf.FindAsync(id); |
| | 2 | 61 | | if (franja != null) |
| | | 62 | | { |
| | 1 | 63 | | _context.FranjasIrpf.Remove(franja); |
| | 1 | 64 | | await _context.SaveChangesAsync(); |
| | | 65 | | } |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | // ── Deducciones ────────────────────────────────────────────────────────── |
| | | 69 | | |
| | | 70 | | public async Task<(IEnumerable<DeduccionIrpf> Items, int TotalCount)> GetDeduccionesPagedAsync( |
| | | 71 | | int page, int pageSize, long? personaId = null, string? tipo = null, bool? activo = null) |
| | | 72 | | { |
| | 4 | 73 | | var query = _context.DeduccionesIrpf |
| | 4 | 74 | | .Include(d => d.Persona) |
| | 4 | 75 | | .AsQueryable(); |
| | | 76 | | |
| | 4 | 77 | | if (personaId.HasValue) |
| | 1 | 78 | | query = query.Where(d => d.PersonaId == personaId.Value); |
| | | 79 | | |
| | 4 | 80 | | if (!string.IsNullOrWhiteSpace(tipo)) |
| | 1 | 81 | | query = query.Where(d => d.Tipo == tipo); |
| | | 82 | | |
| | 4 | 83 | | if (activo.HasValue) |
| | 1 | 84 | | query = query.Where(d => d.Activo == activo.Value); |
| | | 85 | | |
| | 4 | 86 | | var totalCount = await query.CountAsync(); |
| | 4 | 87 | | var items = await query |
| | 4 | 88 | | .OrderBy(d => d.PersonaId) |
| | 4 | 89 | | .ThenBy(d => d.Tipo) |
| | 4 | 90 | | .Skip((page - 1) * pageSize) |
| | 4 | 91 | | .Take(pageSize) |
| | 4 | 92 | | .ToListAsync(); |
| | | 93 | | |
| | 4 | 94 | | return (items, totalCount); |
| | 4 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<DeduccionIrpf?> GetDeduccionByIdAsync(long id) |
| | | 98 | | { |
| | 3 | 99 | | return await _context.DeduccionesIrpf |
| | 3 | 100 | | .Include(d => d.Persona) |
| | 3 | 101 | | .FirstOrDefaultAsync(d => d.Id == id); |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | public async Task<bool> ExisteDeduccionActivaAsync(long personaId, string tipo) |
| | | 105 | | { |
| | 3 | 106 | | return await _context.DeduccionesIrpf |
| | 3 | 107 | | .AnyAsync(d => d.PersonaId == personaId && d.Tipo == tipo && d.Activo); |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task<int> ContarDeduccionesActivasAsync(long personaId) |
| | | 111 | | { |
| | 2 | 112 | | return await _context.DeduccionesIrpf |
| | 2 | 113 | | .CountAsync(d => d.PersonaId == personaId && d.Activo); |
| | 2 | 114 | | } |
| | | 115 | | |
| | | 116 | | public async Task<DeduccionIrpf> CreateDeduccionAsync(DeduccionIrpf deduccion) |
| | | 117 | | { |
| | 3 | 118 | | _context.DeduccionesIrpf.Add(deduccion); |
| | 3 | 119 | | await _context.SaveChangesAsync(); |
| | 3 | 120 | | return deduccion; |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | public async Task<DeduccionIrpf> UpdateDeduccionAsync(DeduccionIrpf deduccion) |
| | | 124 | | { |
| | 1 | 125 | | _context.DeduccionesIrpf.Update(deduccion); |
| | 1 | 126 | | await _context.SaveChangesAsync(); |
| | 1 | 127 | | return deduccion; |
| | 1 | 128 | | } |
| | | 129 | | } |