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