| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class ReciboRepository : IReciboRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 0 | 10 | | public ReciboRepository(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<ReciboResumenDto>> ObtenerResumenesAsync(long periodoId, int version, ReciboFiltroRequest fil |
| | | 24 | | { |
| | 0 | 25 | | var query = _context.RelacionesLaborales |
| | 0 | 26 | | .AsNoTracking() |
| | 0 | 27 | | .Include(r => r.Persona) |
| | 0 | 28 | | .ThenInclude(p => p.CuentasBancarias) |
| | 0 | 29 | | .ThenInclude(c => c.Banco) |
| | 0 | 30 | | .Include(r => r.Regimen) |
| | 0 | 31 | | .Include(r => r.Programa) |
| | 0 | 32 | | .Include(r => r.Unidad) |
| | 0 | 33 | | .Where(r => r.Estado == EstadoRelacion.Activo); |
| | | 34 | | |
| | 0 | 35 | | if (!string.IsNullOrWhiteSpace(filtro.Cedula)) |
| | 0 | 36 | | query = query.Where(r => r.Persona.Cedula == filtro.Cedula); |
| | | 37 | | |
| | 0 | 38 | | if (filtro.ProgramaId.HasValue) |
| | 0 | 39 | | query = query.Where(r => r.ProgramaId == filtro.ProgramaId.Value); |
| | | 40 | | |
| | 0 | 41 | | if (filtro.UnidadId.HasValue) |
| | 0 | 42 | | query = query.Where(r => r.UnidadId == filtro.UnidadId.Value); |
| | | 43 | | |
| | 0 | 44 | | var relaciones = await query.ToListAsync(); |
| | | 45 | | |
| | | 46 | | // Obtener cédulas que tienen liquidación en el período/versión |
| | 0 | 47 | | var cedulasConLiquidacion = await _context.LiquidacionEstadosPersonal |
| | 0 | 48 | | .AsNoTracking() |
| | 0 | 49 | | .Where(e => e.PeriodoId == periodoId && e.Version == version) |
| | 0 | 50 | | .Select(e => e.Cedula) |
| | 0 | 51 | | .ToHashSetAsync(); |
| | | 52 | | |
| | | 53 | | // Calcular líquido pagable (suma de todos los ítems por cédula) |
| | 0 | 54 | | var liquidosPorCedula = await _context.LiquidacionItems |
| | 0 | 55 | | .AsNoTracking() |
| | 0 | 56 | | .Where(i => i.PeriodoId == periodoId && i.Version == version) |
| | 0 | 57 | | .GroupBy(i => i.Cedula) |
| | 0 | 58 | | .Select(g => new { Cedula = g.Key, Liquido = g.Sum(i => i.Importe) }) |
| | 0 | 59 | | .ToDictionaryAsync(x => x.Cedula, x => x.Liquido); |
| | | 60 | | |
| | 0 | 61 | | var resultados = relaciones |
| | 0 | 62 | | .Where(r => cedulasConLiquidacion.Contains(r.Persona.Cedula)) |
| | 0 | 63 | | .ToList(); |
| | | 64 | | |
| | 0 | 65 | | if (!string.IsNullOrWhiteSpace(filtro.Regimen)) |
| | | 66 | | { |
| | 0 | 67 | | var regimenNorm = filtro.Regimen.Replace('_', '-').ToLowerInvariant(); |
| | 0 | 68 | | resultados = regimenNorm switch |
| | 0 | 69 | | { |
| | 0 | 70 | | "ley-vieja" => resultados.Where(r => r.Regimen.EsLeyVieja).ToList(), |
| | 0 | 71 | | "ley-nueva" => resultados.Where(r => !r.Regimen.EsLeyVieja).ToList(), |
| | 0 | 72 | | _ => resultados.Where(r => r.Regimen.NumeroLey.Contains(filtro.Regimen, StringComparison.OrdinalIgnoreCa |
| | 0 | 73 | | }; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return resultados.Select(r => new ReciboResumenDto |
| | 0 | 77 | | { |
| | 0 | 78 | | RelacionLaboralId = r.Id, |
| | 0 | 79 | | Cedula = r.Persona.Cedula, |
| | 0 | 80 | | Nombre = r.Persona.PrimerNombre, |
| | 0 | 81 | | Apellido = r.Persona.PrimerApellido, |
| | 0 | 82 | | Regimen = r.Regimen.NumeroLey, |
| | 0 | 83 | | Programa = r.Programa?.Denominacion ?? string.Empty, |
| | 0 | 84 | | Unidad = r.Unidad?.Denominacion ?? string.Empty, |
| | 0 | 85 | | LiquidoPagable = liquidosPorCedula.TryGetValue(r.Persona.Cedula, out var liq) ? liq : 0m |
| | 0 | 86 | | }).ToList(); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | public async Task<ReciboDto?> ObtenerDatosReciboAsync(long periodoId, int version, long relacionId) |
| | | 90 | | { |
| | 0 | 91 | | var relacion = await _context.RelacionesLaborales |
| | 0 | 92 | | .AsNoTracking() |
| | 0 | 93 | | .Include(r => r.Persona) |
| | 0 | 94 | | .ThenInclude(p => p.CuentasBancarias.Where(c => c.Estado == "activa")) |
| | 0 | 95 | | .ThenInclude(c => c.Banco) |
| | 0 | 96 | | .Include(r => r.Regimen) |
| | 0 | 97 | | .Include(r => r.Programa) |
| | 0 | 98 | | .Include(r => r.Unidad) |
| | 0 | 99 | | .Include(r => r.SubUnidad) |
| | 0 | 100 | | .Include(r => r.Grado) |
| | 0 | 101 | | .Include(r => r.Escalafon) |
| | 0 | 102 | | .FirstOrDefaultAsync(r => r.Id == relacionId); |
| | | 103 | | |
| | 0 | 104 | | if (relacion == null) return null; |
| | | 105 | | |
| | 0 | 106 | | var cedula = relacion.Persona.Cedula; |
| | | 107 | | |
| | 0 | 108 | | var items = await _context.LiquidacionItems |
| | 0 | 109 | | .AsNoTracking() |
| | 0 | 110 | | .Where(i => i.PeriodoId == periodoId && i.Version == version && i.Cedula == cedula) |
| | 0 | 111 | | .ToListAsync(); |
| | | 112 | | |
| | 0 | 113 | | if (!items.Any()) return null; |
| | | 114 | | |
| | 0 | 115 | | var periodo = await _context.PeriodosLiquidacion |
| | 0 | 116 | | .AsNoTracking() |
| | 0 | 117 | | .Where(p => p.Id == periodoId) |
| | 0 | 118 | | .Select(p => new { p.Anio, p.Mes }) |
| | 0 | 119 | | .FirstOrDefaultAsync(); |
| | | 120 | | |
| | 0 | 121 | | var situacionCodigo = await _context.Situaciones |
| | 0 | 122 | | .AsNoTracking() |
| | 0 | 123 | | .Where(s => s.Id == relacion.SituacionId) |
| | 0 | 124 | | .Select(s => s.Codigo) |
| | 0 | 125 | | .FirstOrDefaultAsync() ?? string.Empty; |
| | | 126 | | |
| | | 127 | | // Obtener tipos de catálogo para clasificar ítems |
| | 0 | 128 | | var codigosNumericos = items |
| | 0 | 129 | | .Select(i => i.CodigoConcepto) |
| | 0 | 130 | | .Where(c => int.TryParse(c, out _)) |
| | 0 | 131 | | .Select(int.Parse) |
| | 0 | 132 | | .ToHashSet(); |
| | | 133 | | |
| | 0 | 134 | | var catalogoPorCodigo = await _context.CatalogoItems |
| | 0 | 135 | | .AsNoTracking() |
| | 0 | 136 | | .Where(c => codigosNumericos.Contains(c.Codigo)) |
| | 0 | 137 | | .ToDictionaryAsync(c => c.Codigo.ToString(), c => c.Tipo); |
| | | 138 | | |
| | 0 | 139 | | var haberes = new List<ReciboItemDto>(); |
| | 0 | 140 | | var beneficios = new List<ReciboItemDto>(); |
| | 0 | 141 | | var descuentosLegales = new List<ReciboItemDto>(); |
| | 0 | 142 | | var descuentosPersonales = new List<ReciboItemDto>(); |
| | | 143 | | |
| | 0 | 144 | | foreach (var item in items.OrderBy(i => i.CodigoConcepto)) |
| | | 145 | | { |
| | 0 | 146 | | var dto = new ReciboItemDto |
| | 0 | 147 | | { |
| | 0 | 148 | | Codigo = item.CodigoConcepto, |
| | 0 | 149 | | Descripcion = item.NombreConcepto, |
| | 0 | 150 | | RubroContable = item.RubroContable, |
| | 0 | 151 | | Monto = item.Importe |
| | 0 | 152 | | }; |
| | | 153 | | |
| | 0 | 154 | | if (item.Importe > 0) |
| | | 155 | | { |
| | 0 | 156 | | var tipo = catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var t) ? t : "haber_normal"; |
| | 0 | 157 | | if (tipo.StartsWith("beneficio_social", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 158 | | beneficios.Add(dto); |
| | | 159 | | else |
| | 0 | 160 | | haberes.Add(dto); |
| | | 161 | | } |
| | 0 | 162 | | else if (catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var tipo2) && |
| | 0 | 163 | | string.Equals(tipo2, "descuento_legal", StringComparison.OrdinalIgnoreCase)) |
| | | 164 | | { |
| | 0 | 165 | | descuentosLegales.Add(dto); |
| | | 166 | | } |
| | | 167 | | else |
| | | 168 | | { |
| | 0 | 169 | | descuentosPersonales.Add(dto); |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | var totalHaberes = haberes.Sum(i => i.Monto); |
| | 0 | 174 | | var totalBeneficios = beneficios.Sum(i => i.Monto); |
| | 0 | 175 | | var totalDescuentosLegales = Math.Abs(descuentosLegales.Sum(i => i.Monto)); |
| | 0 | 176 | | var totalDescuentosPersonales = Math.Abs(descuentosPersonales.Sum(i => i.Monto)); |
| | 0 | 177 | | var totalDescuentos = totalDescuentosLegales + totalDescuentosPersonales; |
| | | 178 | | |
| | 0 | 179 | | var nominal = totalHaberes + totalBeneficios; |
| | 0 | 180 | | var exactLiquid = nominal - totalDescuentosLegales - totalDescuentosPersonales; |
| | 0 | 181 | | var aCobrar = Math.Round(exactLiquid, 0, MidpointRounding.AwayFromZero); |
| | 0 | 182 | | var ajuste = aCobrar - exactLiquid; |
| | | 183 | | |
| | | 184 | | // Civil: SIT19 (presupuestado), SIT20 (baja civil), SIT21 (contratado) |
| | 0 | 185 | | var esCivil = situacionCodigo is "SIT19" or "SIT20" or "SIT21"; |
| | | 186 | | |
| | 0 | 187 | | var cuentaActiva = relacion.Persona.CuentasBancarias.FirstOrDefault(); |
| | | 188 | | |
| | 0 | 189 | | return new ReciboDto |
| | 0 | 190 | | { |
| | 0 | 191 | | Anio = periodo?.Anio ?? 0, |
| | 0 | 192 | | Mes = periodo?.Mes ?? 0, |
| | 0 | 193 | | RelacionLaboralId = relacion.Id, |
| | 0 | 194 | | Cedula = cedula, |
| | 0 | 195 | | PrimerNombre = relacion.Persona.PrimerNombre, |
| | 0 | 196 | | SegundoNombre = relacion.Persona.SegundoNombre ?? string.Empty, |
| | 0 | 197 | | PrimerApellido = relacion.Persona.PrimerApellido, |
| | 0 | 198 | | SegundoApellido = relacion.Persona.SegundoApellido ?? string.Empty, |
| | 0 | 199 | | Grado = relacion.Grado?.Denominacion ?? string.Empty, |
| | 0 | 200 | | Escalafon = relacion.Escalafon?.Denominacion ?? string.Empty, |
| | 0 | 201 | | Regimen = relacion.Regimen.NumeroLey, |
| | 0 | 202 | | Unidad = relacion.Unidad?.Denominacion ?? string.Empty, |
| | 0 | 203 | | Programa = relacion.Programa?.Denominacion ?? string.Empty, |
| | 0 | 204 | | UnidadCodigo = relacion.Unidad?.Codigo ?? string.Empty, |
| | 0 | 205 | | Compania = relacion.SubUnidad?.Codigo ?? string.Empty, |
| | 0 | 206 | | EsOficial = relacion.Grado?.EsOficial ?? false, |
| | 0 | 207 | | EsSubalterno = relacion.Grado?.EsSubalterno ?? false, |
| | 0 | 208 | | EsCivil = esCivil, |
| | 0 | 209 | | FechaInicio = relacion.FechaInicio, |
| | 0 | 210 | | FechaUltimoAscenso = relacion.FechaUltimoAscenso, |
| | 0 | 211 | | Haberes = haberes, |
| | 0 | 212 | | Beneficios = beneficios, |
| | 0 | 213 | | DescuentosLegales = descuentosLegales, |
| | 0 | 214 | | DescuentosPersonales = descuentosPersonales, |
| | 0 | 215 | | TotalHaberes = totalHaberes, |
| | 0 | 216 | | TotalBeneficios = totalBeneficios, |
| | 0 | 217 | | TotalDescuentosLegales = totalDescuentosLegales, |
| | 0 | 218 | | TotalDescuentosPersonales = totalDescuentosPersonales, |
| | 0 | 219 | | TotalDescuentos = totalDescuentos, |
| | 0 | 220 | | Nominal = nominal, |
| | 0 | 221 | | LiquidoPagable = aCobrar, |
| | 0 | 222 | | ACobrar = aCobrar, |
| | 0 | 223 | | Ajuste = ajuste, |
| | 0 | 224 | | DatosBancarios = cuentaActiva != null |
| | 0 | 225 | | ? new ReciboDatosBancariosDto |
| | 0 | 226 | | { |
| | 0 | 227 | | Banco = cuentaActiva.Banco?.Nombre, |
| | 0 | 228 | | Cuenta = cuentaActiva.NumeroCuenta |
| | 0 | 229 | | } |
| | 0 | 230 | | : new ReciboDatosBancariosDto { Observacion = "Sin cuenta registrada" } |
| | 0 | 231 | | }; |
| | 0 | 232 | | } |
| | | 233 | | } |