| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class ReportePresupuestalRepository : IReportePresupuestalRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 0 | 10 | | public ReportePresupuestalRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 0 | 12 | | _context = context; |
| | 0 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<int> ObtenerVersionMaximaAsync(long periodoId) |
| | | 16 | | { |
| | 0 | 17 | | return await _context.LiquidacionItems |
| | 0 | 18 | | .AsNoTracking() |
| | 0 | 19 | | .Where(i => i.PeriodoId == periodoId) |
| | 0 | 20 | | .MaxAsync(i => (int?)i.Version) ?? 0; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task<List<ItemConDatos>> ObtenerItemsConDatosAsync(long periodoId, int version) |
| | | 24 | | { |
| | 0 | 25 | | var items = await _context.LiquidacionItems |
| | 0 | 26 | | .AsNoTracking() |
| | 0 | 27 | | .Where(i => i.PeriodoId == periodoId && i.Version == version) |
| | 0 | 28 | | .ToListAsync(); |
| | | 29 | | |
| | 0 | 30 | | if (items.Count == 0) |
| | 0 | 31 | | return new List<ItemConDatos>(); |
| | | 32 | | |
| | 0 | 33 | | var cedulas = items.Select(i => i.Cedula).ToHashSet(); |
| | | 34 | | |
| | | 35 | | // Cargar relaciones activas de las cédulas presentes |
| | 0 | 36 | | var relaciones = await _context.RelacionesLaborales |
| | 0 | 37 | | .AsNoTracking() |
| | 0 | 38 | | .Include(r => r.Persona) |
| | 0 | 39 | | .Include(r => r.Grado) |
| | 0 | 40 | | .Include(r => r.Escalafon) |
| | 0 | 41 | | .Include(r => r.Regimen) |
| | 0 | 42 | | .Include(r => r.Programa) |
| | 0 | 43 | | .Include(r => r.Unidad) |
| | 0 | 44 | | .Where(r => r.Estado == EstadoRelacion.Activo && cedulas.Contains(r.Persona.Cedula)) |
| | 0 | 45 | | .ToListAsync(); |
| | | 46 | | |
| | 0 | 47 | | var relacionPorCedula = relaciones |
| | 0 | 48 | | .GroupBy(r => r.Persona.Cedula) |
| | 0 | 49 | | .ToDictionary(g => g.Key, g => g.First()); |
| | | 50 | | |
| | 0 | 51 | | return items |
| | 0 | 52 | | .Select(i => |
| | 0 | 53 | | { |
| | 0 | 54 | | if (!relacionPorCedula.TryGetValue(i.Cedula, out var rel)) |
| | 0 | 55 | | return null; |
| | 0 | 56 | | |
| | 0 | 57 | | return new ItemConDatos( |
| | 0 | 58 | | Cedula: i.Cedula, |
| | 0 | 59 | | Nombre: rel.Persona.NombreCompleto, |
| | 0 | 60 | | Grado: rel.Grado.Denominacion, |
| | 0 | 61 | | Escalafon: rel.Escalafon.Denominacion, |
| | 0 | 62 | | Regimen: rel.Regimen.NumeroLey, |
| | 0 | 63 | | RegimenId: rel.RegimenId, |
| | 0 | 64 | | Programa: rel.Programa.Denominacion, |
| | 0 | 65 | | ProgramaId: rel.ProgramaId, |
| | 0 | 66 | | Unidad: rel.Unidad.Denominacion, |
| | 0 | 67 | | RubroContable: i.RubroContable, |
| | 0 | 68 | | Importe: i.Importe |
| | 0 | 69 | | ); |
| | 0 | 70 | | }) |
| | 0 | 71 | | .Where(x => x is not null) |
| | 0 | 72 | | .Select(x => x!) |
| | 0 | 73 | | .ToList(); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task<List<MovimientoConDatos>> ObtenerMovimientosAsync(long periodoId) |
| | | 77 | | { |
| | 0 | 78 | | var periodo = await _context.PeriodosLiquidacion |
| | 0 | 79 | | .AsNoTracking() |
| | 0 | 80 | | .FirstOrDefaultAsync(p => p.Id == periodoId); |
| | | 81 | | |
| | 0 | 82 | | if (periodo is null) |
| | 0 | 83 | | return new List<MovimientoConDatos>(); |
| | | 84 | | |
| | 0 | 85 | | var desde = new DateTime(periodo.Anio, periodo.Mes, 1); |
| | 0 | 86 | | var hasta = desde.AddMonths(1); |
| | | 87 | | |
| | 0 | 88 | | return await _context.MovimientosLaborales |
| | 0 | 89 | | .AsNoTracking() |
| | 0 | 90 | | .Include(m => m.RelacionLaboral) |
| | 0 | 91 | | .ThenInclude(r => r.Persona) |
| | 0 | 92 | | .Include(m => m.TipoMovimiento) |
| | 0 | 93 | | .Where(m => m.FechaMovimiento >= desde && m.FechaMovimiento < hasta) |
| | 0 | 94 | | .Select(m => new MovimientoConDatos( |
| | 0 | 95 | | m.RelacionLaboral.Persona.Cedula, |
| | 0 | 96 | | m.RelacionLaboral.Persona.NombreCompleto, |
| | 0 | 97 | | m.TipoMovimiento.Nombre, |
| | 0 | 98 | | m.FechaMovimiento, |
| | 0 | 99 | | m.Observaciones, |
| | 0 | 100 | | m.TipoMovimiento.EsAlta |
| | 0 | 101 | | )) |
| | 0 | 102 | | .ToListAsync(); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public async Task<Dictionary<string, DiferenciaFilaDto>?> ObtenerResumenPeriodoAnteriorAsync(long periodoId) |
| | | 106 | | { |
| | 0 | 107 | | var periodoActual = await _context.PeriodosLiquidacion |
| | 0 | 108 | | .AsNoTracking() |
| | 0 | 109 | | .FirstOrDefaultAsync(p => p.Id == periodoId); |
| | | 110 | | |
| | 0 | 111 | | if (periodoActual is null) |
| | 0 | 112 | | return null; |
| | | 113 | | |
| | | 114 | | // Buscar el período CERRADA más reciente anterior al actual (por anio/mes) |
| | 0 | 115 | | var periodoAnterior = await _context.PeriodosLiquidacion |
| | 0 | 116 | | .AsNoTracking() |
| | 0 | 117 | | .Where(p => p.Estado == Periodo.EstadoCerrada |
| | 0 | 118 | | && (p.Anio < periodoActual.Anio |
| | 0 | 119 | | || (p.Anio == periodoActual.Anio && p.Mes < periodoActual.Mes))) |
| | 0 | 120 | | .OrderByDescending(p => p.Anio) |
| | 0 | 121 | | .ThenByDescending(p => p.Mes) |
| | 0 | 122 | | .FirstOrDefaultAsync(); |
| | | 123 | | |
| | 0 | 124 | | if (periodoAnterior is null) |
| | 0 | 125 | | return null; |
| | | 126 | | |
| | 0 | 127 | | var versionAnterior = await _context.LiquidacionItems |
| | 0 | 128 | | .AsNoTracking() |
| | 0 | 129 | | .Where(i => i.PeriodoId == periodoAnterior.Id) |
| | 0 | 130 | | .MaxAsync(i => (int?)i.Version) ?? 0; |
| | | 131 | | |
| | 0 | 132 | | var items = await _context.LiquidacionItems |
| | 0 | 133 | | .AsNoTracking() |
| | 0 | 134 | | .Where(i => i.PeriodoId == periodoAnterior.Id && i.Version == versionAnterior) |
| | 0 | 135 | | .ToListAsync(); |
| | | 136 | | |
| | 0 | 137 | | if (items.Count == 0) |
| | 0 | 138 | | return new Dictionary<string, DiferenciaFilaDto>(); |
| | | 139 | | |
| | 0 | 140 | | var cedulas = items.Select(i => i.Cedula).ToHashSet(); |
| | | 141 | | |
| | | 142 | | // Sin filtro de estado: los funcionarios dados de baja ya no son Activo |
| | 0 | 143 | | var relaciones = await _context.RelacionesLaborales |
| | 0 | 144 | | .AsNoTracking() |
| | 0 | 145 | | .Include(r => r.Persona) |
| | 0 | 146 | | .Include(r => r.Grado) |
| | 0 | 147 | | .Include(r => r.Regimen) |
| | 0 | 148 | | .Include(r => r.Programa) |
| | 0 | 149 | | .Where(r => cedulas.Contains(r.Persona.Cedula)) |
| | 0 | 150 | | .ToListAsync(); |
| | | 151 | | |
| | 0 | 152 | | var relacionPorCedula = relaciones |
| | 0 | 153 | | .GroupBy(r => r.Persona.Cedula) |
| | 0 | 154 | | .ToDictionary(g => g.Key, g => g.First()); |
| | | 155 | | |
| | 0 | 156 | | return items |
| | 0 | 157 | | .GroupBy(i => i.Cedula) |
| | 0 | 158 | | .Select(g => |
| | 0 | 159 | | { |
| | 0 | 160 | | var cedula = g.Key; |
| | 0 | 161 | | var liquido = g.Sum(i => i.Importe); |
| | 0 | 162 | | if (!relacionPorCedula.TryGetValue(cedula, out var rel)) |
| | 0 | 163 | | return new DiferenciaFilaDto { Cedula = cedula, Liquido = liquido }; |
| | 0 | 164 | | |
| | 0 | 165 | | return new DiferenciaFilaDto |
| | 0 | 166 | | { |
| | 0 | 167 | | Cedula = cedula, |
| | 0 | 168 | | Nombre = rel.Persona.NombreCompleto, |
| | 0 | 169 | | Grado = rel.Grado.Denominacion, |
| | 0 | 170 | | Regimen = rel.Regimen.NumeroLey, |
| | 0 | 171 | | Programa = rel.Programa.Denominacion, |
| | 0 | 172 | | Liquido = liquido |
| | 0 | 173 | | }; |
| | 0 | 174 | | }) |
| | 0 | 175 | | .ToDictionary(f => f.Cedula); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | public async Task<List<NoDisponibleRaw>> ObtenerNoDisponiblesAsync(long periodoId, int version) |
| | | 179 | | { |
| | | 180 | | // Funcionarios en situaciones que no afectan cobro |
| | 0 | 181 | | var relaciones = await _context.RelacionesLaborales |
| | 0 | 182 | | .AsNoTracking() |
| | 0 | 183 | | .Include(r => r.Persona) |
| | 0 | 184 | | .Include(r => r.Situacion) |
| | 0 | 185 | | .Where(r => r.Estado == EstadoRelacion.Activo && !r.Situacion.AfectaCobro) |
| | 0 | 186 | | .ToListAsync(); |
| | | 187 | | |
| | 0 | 188 | | if (relaciones.Count == 0) |
| | 0 | 189 | | return new List<NoDisponibleRaw>(); |
| | | 190 | | |
| | 0 | 191 | | var cedulas = relaciones.Select(r => r.Persona.Cedula).ToHashSet(); |
| | | 192 | | |
| | | 193 | | // Calcular total liquidado por cédula en el período |
| | 0 | 194 | | var totales = await _context.LiquidacionItems |
| | 0 | 195 | | .AsNoTracking() |
| | 0 | 196 | | .Where(i => i.PeriodoId == periodoId && i.Version == version && cedulas.Contains(i.Cedula)) |
| | 0 | 197 | | .GroupBy(i => i.Cedula) |
| | 0 | 198 | | .Select(g => new { Cedula = g.Key, Total = g.Sum(i => i.Importe) }) |
| | 0 | 199 | | .ToDictionaryAsync(x => x.Cedula, x => x.Total); |
| | | 200 | | |
| | 0 | 201 | | return relaciones |
| | 0 | 202 | | .GroupBy(r => r.Persona.Cedula) |
| | 0 | 203 | | .Select(g => |
| | 0 | 204 | | { |
| | 0 | 205 | | var rel = g.First(); |
| | 0 | 206 | | var total = totales.TryGetValue(rel.Persona.Cedula, out var t) ? t : 0m; |
| | 0 | 207 | | return new NoDisponibleRaw( |
| | 0 | 208 | | Cedula: rel.Persona.Cedula, |
| | 0 | 209 | | Nombre: rel.Persona.NombreCompleto, |
| | 0 | 210 | | Situacion: rel.Situacion.Denominacion, |
| | 0 | 211 | | TotalLiquidado: total |
| | 0 | 212 | | ); |
| | 0 | 213 | | }) |
| | 0 | 214 | | .ToList(); |
| | 0 | 215 | | } |
| | | 216 | | } |