| | | 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 | | |
| | 1 | 10 | | public ReciboRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 1 | 12 | | _context = context; |
| | 1 | 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 | | { |
| | 1 | 91 | | var relacion = await _context.RelacionesLaborales |
| | 1 | 92 | | .AsNoTracking() |
| | 1 | 93 | | .Include(r => r.Persona) |
| | 1 | 94 | | .ThenInclude(p => p.CuentasBancarias.Where(c => c.Estado == "activa")) |
| | 1 | 95 | | .ThenInclude(c => c.Banco) |
| | 1 | 96 | | .Include(r => r.Regimen) |
| | 1 | 97 | | .Include(r => r.Programa) |
| | 1 | 98 | | .Include(r => r.Unidad) |
| | 1 | 99 | | .Include(r => r.SubUnidad) |
| | 1 | 100 | | .Include(r => r.Grado) |
| | 1 | 101 | | .Include(r => r.Escalafon) |
| | 1 | 102 | | .FirstOrDefaultAsync(r => r.Id == relacionId); |
| | | 103 | | |
| | 1 | 104 | | if (relacion == null) return null; |
| | | 105 | | |
| | 1 | 106 | | var cedula = relacion.Persona.Cedula; |
| | | 107 | | |
| | 1 | 108 | | var items = await _context.LiquidacionItems |
| | 1 | 109 | | .AsNoTracking() |
| | 1 | 110 | | .Where(i => i.PeriodoId == periodoId && i.Version == version && i.Cedula == cedula) |
| | 1 | 111 | | .ToListAsync(); |
| | | 112 | | |
| | 1 | 113 | | if (!items.Any()) return null; |
| | | 114 | | |
| | 1 | 115 | | var periodo = await _context.PeriodosLiquidacion |
| | 1 | 116 | | .AsNoTracking() |
| | 1 | 117 | | .Where(p => p.Id == periodoId) |
| | 1 | 118 | | .Select(p => new { p.Anio, p.Mes }) |
| | 1 | 119 | | .FirstOrDefaultAsync(); |
| | | 120 | | |
| | 1 | 121 | | var situacionCodigo = await _context.Situaciones |
| | 1 | 122 | | .AsNoTracking() |
| | 1 | 123 | | .Where(s => s.Id == relacion.SituacionId) |
| | 1 | 124 | | .Select(s => s.Codigo) |
| | 1 | 125 | | .FirstOrDefaultAsync() ?? string.Empty; |
| | | 126 | | |
| | | 127 | | // Obtener tipos de catálogo para clasificar ítems |
| | 1 | 128 | | var codigosNumericos = items |
| | 4 | 129 | | .Select(i => i.CodigoConcepto) |
| | 4 | 130 | | .Where(c => int.TryParse(c, out _)) |
| | 1 | 131 | | .Select(int.Parse) |
| | 1 | 132 | | .ToHashSet(); |
| | | 133 | | |
| | 1 | 134 | | var catalogoPorCodigo = await _context.CatalogoItems |
| | 1 | 135 | | .AsNoTracking() |
| | 1 | 136 | | .Where(c => codigosNumericos.Contains(c.Codigo)) |
| | 1 | 137 | | .ToDictionaryAsync( |
| | 4 | 138 | | c => c.Codigo.ToString(), |
| | 5 | 139 | | c => new CatalogoReciboItem(c.Tipo, c.ItemParFictoId)); |
| | | 140 | | |
| | 1 | 141 | | var idsParesFictos = catalogoPorCodigo.Values |
| | 4 | 142 | | .Where(c => c.ItemParFictoId.HasValue) |
| | 2 | 143 | | .Select(c => c.ItemParFictoId!.Value) |
| | 1 | 144 | | .ToHashSet(); |
| | | 145 | | |
| | 1 | 146 | | var tiposPorId = idsParesFictos.Count == 0 |
| | 1 | 147 | | ? new Dictionary<long, string>() |
| | 1 | 148 | | : await _context.CatalogoItems |
| | 1 | 149 | | .AsNoTracking() |
| | 1 | 150 | | .Where(c => idsParesFictos.Contains(c.Id)) |
| | 5 | 151 | | .ToDictionaryAsync(c => c.Id, c => c.Tipo); |
| | | 152 | | |
| | 1 | 153 | | var haberes = new List<ReciboItemDto>(); |
| | 1 | 154 | | var beneficios = new List<ReciboItemDto>(); |
| | 1 | 155 | | var descuentosLegales = new List<ReciboItemDto>(); |
| | 1 | 156 | | var descuentosPersonales = new List<ReciboItemDto>(); |
| | | 157 | | |
| | 14 | 158 | | foreach (var item in items.OrderBy(i => i.CodigoConcepto)) |
| | | 159 | | { |
| | 4 | 160 | | var dto = new ReciboItemDto |
| | 4 | 161 | | { |
| | 4 | 162 | | Codigo = item.CodigoConcepto, |
| | 4 | 163 | | Descripcion = item.NombreConcepto, |
| | 4 | 164 | | RubroContable = item.RubroContable, |
| | 4 | 165 | | Monto = item.Importe |
| | 4 | 166 | | }; |
| | | 167 | | |
| | 4 | 168 | | if (item.Importe > 0) |
| | | 169 | | { |
| | 2 | 170 | | var catalogoItem = catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var t) ? t : null; |
| | 2 | 171 | | var tipo = catalogoItem?.Tipo ?? "haber_normal"; |
| | 2 | 172 | | if (string.Equals(tipo, "ajuste", StringComparison.OrdinalIgnoreCase)) |
| | | 173 | | { /* el ajuste se muestra por separado en columna 3, no suma a haberes */ } |
| | 2 | 174 | | else if (EsFictoParaRecibo(catalogoItem)) |
| | 1 | 175 | | beneficios.Add(dto); |
| | 1 | 176 | | else if (tipo.StartsWith("beneficio_social", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 177 | | beneficios.Add(dto); |
| | | 178 | | else |
| | 1 | 179 | | haberes.Add(dto); |
| | | 180 | | } |
| | 2 | 181 | | else if (catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var catalogoItem) && |
| | 2 | 182 | | EsDescuentoLegalParaRecibo(catalogoItem, tiposPorId)) |
| | | 183 | | { |
| | 2 | 184 | | descuentosLegales.Add(dto); |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | 0 | 188 | | descuentosPersonales.Add(dto); |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 2 | 192 | | var totalHaberes = haberes.Sum(i => i.Monto); |
| | 2 | 193 | | var totalBeneficios = beneficios.Sum(i => i.Monto); |
| | 3 | 194 | | var totalDescuentosLegales = Math.Abs(descuentosLegales.Sum(i => i.Monto)); |
| | 1 | 195 | | var totalDescuentosPersonales = Math.Abs(descuentosPersonales.Sum(i => i.Monto)); |
| | 1 | 196 | | var totalDescuentos = totalDescuentosLegales + totalDescuentosPersonales; |
| | | 197 | | |
| | 1 | 198 | | var nominal = totalHaberes + totalBeneficios; |
| | 1 | 199 | | var exactLiquid = nominal - totalDescuentosLegales - totalDescuentosPersonales; |
| | 1 | 200 | | var aCobrar = Math.Round(exactLiquid, 0, MidpointRounding.AwayFromZero); |
| | 1 | 201 | | var ajuste = aCobrar - exactLiquid; |
| | | 202 | | |
| | | 203 | | // Civil: SIT19 (presupuestado), SIT20 (baja civil), SIT21 (contratado) |
| | 1 | 204 | | var esCivil = situacionCodigo is "SIT19" or "SIT20" or "SIT21"; |
| | | 205 | | |
| | 1 | 206 | | var cuentaActiva = relacion.Persona.CuentasBancarias.FirstOrDefault(); |
| | | 207 | | |
| | 1 | 208 | | return new ReciboDto |
| | 1 | 209 | | { |
| | 1 | 210 | | Anio = periodo?.Anio ?? 0, |
| | 1 | 211 | | Mes = periodo?.Mes ?? 0, |
| | 1 | 212 | | RelacionLaboralId = relacion.Id, |
| | 1 | 213 | | Cedula = cedula, |
| | 1 | 214 | | PrimerNombre = relacion.Persona.PrimerNombre, |
| | 1 | 215 | | SegundoNombre = relacion.Persona.SegundoNombre ?? string.Empty, |
| | 1 | 216 | | PrimerApellido = relacion.Persona.PrimerApellido, |
| | 1 | 217 | | SegundoApellido = relacion.Persona.SegundoApellido ?? string.Empty, |
| | 1 | 218 | | Grado = relacion.Grado?.Denominacion ?? string.Empty, |
| | 1 | 219 | | Escalafon = relacion.Escalafon?.Denominacion ?? string.Empty, |
| | 1 | 220 | | Regimen = relacion.Regimen.NumeroLey, |
| | 1 | 221 | | Unidad = relacion.Unidad?.Denominacion ?? string.Empty, |
| | 1 | 222 | | Programa = relacion.Programa?.Denominacion ?? string.Empty, |
| | 1 | 223 | | UnidadCodigo = relacion.Unidad?.Codigo ?? string.Empty, |
| | 1 | 224 | | Compania = relacion.SubUnidad?.Codigo ?? string.Empty, |
| | 1 | 225 | | EsOficial = relacion.Grado?.EsOficial ?? false, |
| | 1 | 226 | | EsSubalterno = relacion.Grado?.EsSubalterno ?? false, |
| | 1 | 227 | | EsCivil = esCivil, |
| | 1 | 228 | | FechaInicio = relacion.FechaInicio, |
| | 1 | 229 | | FechaUltimoAscenso = relacion.FechaUltimoAscenso, |
| | 1 | 230 | | Haberes = haberes, |
| | 1 | 231 | | Beneficios = beneficios, |
| | 1 | 232 | | DescuentosLegales = descuentosLegales, |
| | 1 | 233 | | DescuentosPersonales = descuentosPersonales, |
| | 1 | 234 | | TotalHaberes = totalHaberes, |
| | 1 | 235 | | TotalBeneficios = totalBeneficios, |
| | 1 | 236 | | TotalDescuentosLegales = totalDescuentosLegales, |
| | 1 | 237 | | TotalDescuentosPersonales = totalDescuentosPersonales, |
| | 1 | 238 | | TotalDescuentos = totalDescuentos, |
| | 1 | 239 | | Nominal = nominal, |
| | 1 | 240 | | LiquidoPagable = aCobrar, |
| | 1 | 241 | | ACobrar = aCobrar, |
| | 1 | 242 | | Ajuste = ajuste, |
| | 1 | 243 | | DatosBancarios = cuentaActiva != null |
| | 1 | 244 | | ? new ReciboDatosBancariosDto |
| | 1 | 245 | | { |
| | 1 | 246 | | Banco = cuentaActiva.Banco?.Nombre, |
| | 1 | 247 | | Cuenta = cuentaActiva.NumeroCuenta |
| | 1 | 248 | | } |
| | 1 | 249 | | : new ReciboDatosBancariosDto { Observacion = "Sin cuenta registrada" } |
| | 1 | 250 | | }; |
| | 1 | 251 | | } |
| | | 252 | | |
| | | 253 | | private static bool EsDescuentoLegalParaRecibo( |
| | | 254 | | CatalogoReciboItem catalogoItem, |
| | | 255 | | IReadOnlyDictionary<long, string> tiposPorId) |
| | | 256 | | { |
| | 2 | 257 | | if (string.Equals(catalogoItem.Tipo, "descuento_legal", StringComparison.OrdinalIgnoreCase)) |
| | 1 | 258 | | return true; |
| | | 259 | | |
| | 1 | 260 | | return catalogoItem.ItemParFictoId is long itemParFictoId |
| | 1 | 261 | | && tiposPorId.TryGetValue(itemParFictoId, out var tipoPar) |
| | 1 | 262 | | && tipoPar.StartsWith("ficto_", StringComparison.OrdinalIgnoreCase); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private static bool EsFictoParaRecibo(CatalogoReciboItem? catalogoItem) => |
| | 2 | 266 | | catalogoItem is not null && catalogoItem.Tipo.StartsWith("ficto_", StringComparison.OrdinalIgnoreCase); |
| | | 267 | | |
| | 17 | 268 | | private sealed record CatalogoReciboItem(string Tipo, long? ItemParFictoId); |
| | | 269 | | } |