| | | 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 | | |
| | 12 | 10 | | public IrpfRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 12 | 12 | | _context = context; |
| | 12 | 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<IEnumerable<FranjaIrpf>> GetFranjasPorFechaVigenciaAsync(DateTime vigenteDesde) |
| | | 27 | | { |
| | 0 | 28 | | return await _context.FranjasIrpf |
| | 0 | 29 | | .Where(f => f.VigenteDesde.Date == vigenteDesde.Date) |
| | 0 | 30 | | .OrderBy(f => f.NumeroFranja) |
| | 0 | 31 | | .ToListAsync(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<IEnumerable<FranjaIrpf>> GetHistorialFranjasAsync() |
| | | 35 | | { |
| | 0 | 36 | | return await _context.FranjasIrpf |
| | 0 | 37 | | .OrderByDescending(f => f.VigenteDesde) |
| | 0 | 38 | | .ThenBy(f => f.NumeroFranja) |
| | 0 | 39 | | .ToListAsync(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<FranjaIrpf?> GetFranjaByIdAsync(long id) |
| | | 43 | | { |
| | 4 | 44 | | return await _context.FranjasIrpf.FindAsync(id); |
| | 4 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<FranjaIrpf?> GetFranjaActivaByNumeroAsync(short numeroFranja) |
| | | 48 | | { |
| | 2 | 49 | | return await _context.FranjasIrpf |
| | 2 | 50 | | .Where(f => f.NumeroFranja == numeroFranja && f.VigenteHasta == null) |
| | 2 | 51 | | .FirstOrDefaultAsync(); |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task<DateTime?> ObtenerSiguienteFechaVigenciaAsync(DateTime vigenteDesde) |
| | | 55 | | { |
| | 0 | 56 | | return await _context.FranjasIrpf |
| | 0 | 57 | | .Where(f => f.VigenteDesde > vigenteDesde) |
| | 0 | 58 | | .OrderBy(f => f.VigenteDesde) |
| | 0 | 59 | | .Select(f => (DateTime?)f.VigenteDesde) |
| | 0 | 60 | | .FirstOrDefaultAsync(); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task<bool> ExisteFranjaAsync(short numeroFranja, DateTime vigenteDesde) |
| | | 64 | | { |
| | 2 | 65 | | return await _context.FranjasIrpf |
| | 2 | 66 | | .AnyAsync(f => f.NumeroFranja == numeroFranja && f.VigenteDesde.Date == vigenteDesde.Date); |
| | 2 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<FranjaIrpf> CreateFranjaAsync(FranjaIrpf franja) |
| | | 70 | | { |
| | 4 | 71 | | _context.FranjasIrpf.Add(franja); |
| | 4 | 72 | | await _context.SaveChangesAsync(); |
| | 4 | 73 | | return franja; |
| | 4 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task CreateFranjasAsync(IEnumerable<FranjaIrpf> franjas) |
| | | 77 | | { |
| | 0 | 78 | | _context.FranjasIrpf.AddRange(franjas); |
| | 0 | 79 | | await _context.SaveChangesAsync(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<FranjaIrpf> UpdateFranjaAsync(FranjaIrpf franja) |
| | | 83 | | { |
| | 1 | 84 | | _context.FranjasIrpf.Update(franja); |
| | 1 | 85 | | await _context.SaveChangesAsync(); |
| | 1 | 86 | | return franja; |
| | 1 | 87 | | } |
| | | 88 | | |
| | | 89 | | public async Task UpdateFranjasAsync(IEnumerable<FranjaIrpf> franjas) |
| | | 90 | | { |
| | 0 | 91 | | _context.FranjasIrpf.UpdateRange(franjas); |
| | 0 | 92 | | await _context.SaveChangesAsync(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public async Task DeleteFranjaAsync(long id) |
| | | 96 | | { |
| | 2 | 97 | | var franja = await _context.FranjasIrpf.FindAsync(id); |
| | 2 | 98 | | if (franja != null) |
| | | 99 | | { |
| | 1 | 100 | | _context.FranjasIrpf.Remove(franja); |
| | 1 | 101 | | await _context.SaveChangesAsync(); |
| | | 102 | | } |
| | 2 | 103 | | } |
| | | 104 | | |
| | | 105 | | } |