| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class DescuentoPersonalRepository : IDescuentoPersonalRepository |
| | | 7 | | { |
| | 1 | 8 | | private static readonly string[] EstadosAplicables = |
| | 1 | 9 | | [ |
| | 1 | 10 | | "confirmado", |
| | 1 | 11 | | "aplicado", |
| | 1 | 12 | | "parcial", |
| | 1 | 13 | | "deficit" |
| | 1 | 14 | | ]; |
| | | 15 | | |
| | | 16 | | private readonly ApplicationDbContext _context; |
| | | 17 | | |
| | 40 | 18 | | public DescuentoPersonalRepository(ApplicationDbContext context) => _context = context; |
| | | 19 | | |
| | | 20 | | public async Task<DescuentoPersonalPeriodo?> GetByIdAsync(long id) => |
| | 2 | 21 | | await _context.DescuentosPersonalesPeriodo |
| | 2 | 22 | | .Include(d => d.Persona) |
| | 2 | 23 | | .Include(d => d.Acreedor) |
| | 2 | 24 | | .Include(d => d.CatalogoItem) |
| | 2 | 25 | | .FirstOrDefaultAsync(d => d.Id == id); |
| | | 26 | | |
| | | 27 | | public async Task<IEnumerable<DescuentoPersonalPeriodo>> GetByPeriodoAsync( |
| | | 28 | | long periodoId, long? personaId = null, int? codigoConcepto = null, string? estado = null) |
| | | 29 | | { |
| | 1 | 30 | | var query = _context.DescuentosPersonalesPeriodo |
| | 1 | 31 | | .Include(d => d.Persona) |
| | 1 | 32 | | .Include(d => d.Acreedor) |
| | 1 | 33 | | .Include(d => d.CatalogoItem) |
| | 1 | 34 | | .Where(d => d.PeriodoId == periodoId); |
| | | 35 | | |
| | 1 | 36 | | if (personaId.HasValue) query = query.Where(d => d.PersonaId == personaId.Value); |
| | 1 | 37 | | if (codigoConcepto.HasValue) query = query.Where(d => d.CatalogoItem.Codigo == codigoConcepto.Value); |
| | 1 | 38 | | if (!string.IsNullOrEmpty(estado)) query = query.Where(d => d.Estado == estado); |
| | | 39 | | |
| | 1 | 40 | | return await query.OrderBy(d => d.Persona.PrimerApellido).ToListAsync(); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task<IEnumerable<DescuentoPersonalPeriodo>> GetConfirmadosPorPersonaAsync(long periodoId, long personaI |
| | 2 | 44 | | await _context.DescuentosPersonalesPeriodo |
| | 2 | 45 | | .Include(d => d.Acreedor) |
| | 2 | 46 | | .Include(d => d.CatalogoItem) |
| | 2 | 47 | | .Where(d => d.PeriodoId == periodoId && d.PersonaId == personaId && EstadosAplicables.Contains(d.Estado)) |
| | 2 | 48 | | .ToListAsync(); |
| | | 49 | | |
| | | 50 | | public async Task<bool> TieneBorradoresAsync(long periodoId) => |
| | 2 | 51 | | await _context.DescuentosPersonalesPeriodo |
| | 2 | 52 | | .AnyAsync(d => d.PeriodoId == periodoId && d.Estado == "borrador"); |
| | | 53 | | |
| | | 54 | | public async Task<DescuentoPersonalPeriodo> CreateAsync(DescuentoPersonalPeriodo descuento) |
| | | 55 | | { |
| | 1 | 56 | | _context.DescuentosPersonalesPeriodo.Add(descuento); |
| | 1 | 57 | | await _context.SaveChangesAsync(); |
| | 1 | 58 | | return descuento; |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task<DescuentoPersonalPeriodo> UpdateAsync(DescuentoPersonalPeriodo descuento) |
| | | 62 | | { |
| | 1 | 63 | | _context.DescuentosPersonalesPeriodo.Update(descuento); |
| | 1 | 64 | | await _context.SaveChangesAsync(); |
| | 1 | 65 | | return descuento; |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task DeleteAsync(long id) |
| | | 69 | | { |
| | 2 | 70 | | var d = await _context.DescuentosPersonalesPeriodo.FindAsync(id); |
| | 2 | 71 | | if (d != null) |
| | | 72 | | { |
| | 1 | 73 | | _context.DescuentosPersonalesPeriodo.Remove(d); |
| | 1 | 74 | | await _context.SaveChangesAsync(); |
| | | 75 | | } |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async Task<int> ConfirmarBorradoresAsync(long periodoId) |
| | | 79 | | { |
| | 1 | 80 | | var borradores = await _context.DescuentosPersonalesPeriodo |
| | 1 | 81 | | .Where(d => d.PeriodoId == periodoId && d.Estado == "borrador") |
| | 1 | 82 | | .ToListAsync(); |
| | | 83 | | |
| | 8 | 84 | | foreach (var d in borradores) d.Estado = "confirmado"; |
| | 1 | 85 | | await _context.SaveChangesAsync(); |
| | 1 | 86 | | return borradores.Count; |
| | 1 | 87 | | } |
| | | 88 | | |
| | | 89 | | public async Task<int> EliminarBorradoresPorAcreedorAsync(long periodoId, long acreedorId) |
| | | 90 | | { |
| | 3 | 91 | | var borradores = await _context.DescuentosPersonalesPeriodo |
| | 3 | 92 | | .Where(d => d.PeriodoId == periodoId && d.AcreedorId == acreedorId && d.Estado == "borrador") |
| | 3 | 93 | | .ToListAsync(); |
| | | 94 | | |
| | 3 | 95 | | _context.DescuentosPersonalesPeriodo.RemoveRange(borradores); |
| | 3 | 96 | | await _context.SaveChangesAsync(); |
| | 3 | 97 | | return borradores.Count; |
| | 3 | 98 | | } |
| | | 99 | | |
| | | 100 | | public async Task CrearVariosAsync(IEnumerable<DescuentoPersonalPeriodo> descuentos) |
| | | 101 | | { |
| | 3 | 102 | | _context.DescuentosPersonalesPeriodo.AddRange(descuentos); |
| | 3 | 103 | | await _context.SaveChangesAsync(); |
| | 3 | 104 | | } |
| | | 105 | | } |