| | | 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> Items, int TotalCount)> ObtenerResumenesAsync(long periodoId, int version, |
| | | 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)) |
| | | 36 | | { |
| | 0 | 37 | | if (filtro.Cedula.Length >= 7) |
| | 0 | 38 | | query = query.Where(r => r.Persona.Cedula == filtro.Cedula); |
| | 0 | 39 | | else if (filtro.Cedula.Length >= 3) |
| | 0 | 40 | | query = query.Where(r => r.Persona.Cedula.Contains(filtro.Cedula)); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (filtro.ProgramaId.HasValue) |
| | 0 | 44 | | query = query.Where(r => r.ProgramaId == filtro.ProgramaId.Value); |
| | | 45 | | |
| | 0 | 46 | | if (filtro.UnidadId.HasValue) |
| | 0 | 47 | | query = query.Where(r => r.UnidadId == filtro.UnidadId.Value); |
| | | 48 | | |
| | 0 | 49 | | var relaciones = await query.ToListAsync(); |
| | | 50 | | |
| | | 51 | | // Obtener cédulas que tienen liquidación en el período/versión |
| | 0 | 52 | | var cedulasConLiquidacion = await _context.LiquidacionEstadosPersonal |
| | 0 | 53 | | .AsNoTracking() |
| | 0 | 54 | | .Where(e => e.PeriodoId == periodoId && e.Version == version) |
| | 0 | 55 | | .Select(e => e.Cedula) |
| | 0 | 56 | | .ToHashSetAsync(); |
| | | 57 | | |
| | | 58 | | // Calcular líquido pagable (suma de todos los ítems por cédula) |
| | 0 | 59 | | var liquidosPorCedula = await _context.LiquidacionItems |
| | 0 | 60 | | .AsNoTracking() |
| | 0 | 61 | | .Where(i => i.PeriodoId == periodoId && i.Version == version) |
| | 0 | 62 | | .GroupBy(i => i.Cedula) |
| | 0 | 63 | | .Select(g => new { Cedula = g.Key, Liquido = g.Sum(i => i.Importe) }) |
| | 0 | 64 | | .ToDictionaryAsync(x => x.Cedula, x => x.Liquido); |
| | | 65 | | |
| | 0 | 66 | | var resultados = relaciones |
| | 0 | 67 | | .Where(r => cedulasConLiquidacion.Contains(r.Persona.Cedula)) |
| | 0 | 68 | | .ToList(); |
| | | 69 | | |
| | 0 | 70 | | if (!string.IsNullOrWhiteSpace(filtro.Regimen)) |
| | | 71 | | { |
| | 0 | 72 | | var regimenNorm = filtro.Regimen.Replace('_', '-').ToLowerInvariant(); |
| | 0 | 73 | | resultados = regimenNorm switch |
| | 0 | 74 | | { |
| | 0 | 75 | | "ley-vieja" => resultados.Where(r => r.Regimen.EsLeyVieja).ToList(), |
| | 0 | 76 | | "ley-nueva" => resultados.Where(r => !r.Regimen.EsLeyVieja).ToList(), |
| | 0 | 77 | | _ => resultados.Where(r => r.Regimen.NumeroLey.Contains(filtro.Regimen, StringComparison.OrdinalIgnoreCa |
| | 0 | 78 | | }; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | var todos = resultados.Select(r => new ReciboResumenDto |
| | 0 | 82 | | { |
| | 0 | 83 | | RelacionLaboralId = r.Id, |
| | 0 | 84 | | Cedula = r.Persona.Cedula, |
| | 0 | 85 | | Nombre = r.Persona.PrimerNombre, |
| | 0 | 86 | | Apellido = r.Persona.PrimerApellido, |
| | 0 | 87 | | Regimen = r.Regimen.NumeroLey, |
| | 0 | 88 | | Programa = r.Programa?.Denominacion ?? string.Empty, |
| | 0 | 89 | | Unidad = r.Unidad?.Denominacion ?? string.Empty, |
| | 0 | 90 | | LiquidoPagable = liquidosPorCedula.TryGetValue(r.Persona.Cedula, out var liq) ? liq : 0m |
| | 0 | 91 | | }).ToList(); |
| | | 92 | | |
| | 0 | 93 | | var totalCount = todos.Count; |
| | | 94 | | |
| | 0 | 95 | | if (filtro.Page.HasValue && filtro.PageSize.HasValue) |
| | | 96 | | { |
| | 0 | 97 | | var skip = (filtro.Page.Value - 1) * filtro.PageSize.Value; |
| | 0 | 98 | | todos = todos.Skip(skip).Take(filtro.PageSize.Value).ToList(); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return (todos, totalCount); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | public async Task<ReciboDto?> ObtenerDatosReciboAsync(long periodoId, int version, long relacionId) |
| | | 105 | | { |
| | 1 | 106 | | var relacion = await _context.RelacionesLaborales |
| | 1 | 107 | | .AsNoTracking() |
| | 1 | 108 | | .Include(r => r.Persona) |
| | 1 | 109 | | .ThenInclude(p => p.CuentasBancarias.Where(c => c.Estado == "activa")) |
| | 1 | 110 | | .ThenInclude(c => c.Banco) |
| | 1 | 111 | | .Include(r => r.Regimen) |
| | 1 | 112 | | .Include(r => r.Programa) |
| | 1 | 113 | | .Include(r => r.Unidad) |
| | 1 | 114 | | .Include(r => r.SubUnidad) |
| | 1 | 115 | | .Include(r => r.Grado) |
| | 1 | 116 | | .Include(r => r.Escalafon) |
| | 1 | 117 | | .FirstOrDefaultAsync(r => r.Id == relacionId); |
| | | 118 | | |
| | 1 | 119 | | if (relacion == null) return null; |
| | | 120 | | |
| | 1 | 121 | | var cedula = relacion.Persona.Cedula; |
| | | 122 | | |
| | 1 | 123 | | var items = await _context.LiquidacionItems |
| | 1 | 124 | | .AsNoTracking() |
| | 1 | 125 | | .Where(i => i.PeriodoId == periodoId && i.Version == version && i.Cedula == cedula) |
| | 1 | 126 | | .ToListAsync(); |
| | | 127 | | |
| | 1 | 128 | | if (!items.Any()) return null; |
| | | 129 | | |
| | 1 | 130 | | var periodo = await _context.PeriodosLiquidacion |
| | 1 | 131 | | .AsNoTracking() |
| | 1 | 132 | | .Where(p => p.Id == periodoId) |
| | 1 | 133 | | .Select(p => new { p.Anio, p.Mes }) |
| | 1 | 134 | | .FirstOrDefaultAsync(); |
| | | 135 | | |
| | 1 | 136 | | var situacionCodigo = await _context.Situaciones |
| | 1 | 137 | | .AsNoTracking() |
| | 1 | 138 | | .Where(s => s.Id == relacion.SituacionId) |
| | 1 | 139 | | .Select(s => s.Codigo) |
| | 1 | 140 | | .FirstOrDefaultAsync() ?? string.Empty; |
| | | 141 | | |
| | | 142 | | // Obtener tipos de catálogo para clasificar ítems |
| | 1 | 143 | | var codigosNumericos = items |
| | 4 | 144 | | .Select(i => i.CodigoConcepto) |
| | 4 | 145 | | .Where(c => int.TryParse(c, out _)) |
| | 1 | 146 | | .Select(int.Parse) |
| | 1 | 147 | | .ToHashSet(); |
| | | 148 | | |
| | 1 | 149 | | var catalogoPorCodigo = await _context.CatalogoItems |
| | 1 | 150 | | .AsNoTracking() |
| | 1 | 151 | | .Where(c => codigosNumericos.Contains(c.Codigo)) |
| | 1 | 152 | | .ToDictionaryAsync( |
| | 4 | 153 | | c => c.Codigo.ToString(), |
| | 5 | 154 | | c => new CatalogoReciboItem(c.Tipo, c.ItemParFictoId)); |
| | | 155 | | |
| | 1 | 156 | | var idsParesFictos = catalogoPorCodigo.Values |
| | 4 | 157 | | .Where(c => c.ItemParFictoId.HasValue) |
| | 2 | 158 | | .Select(c => c.ItemParFictoId!.Value) |
| | 1 | 159 | | .ToHashSet(); |
| | | 160 | | |
| | 1 | 161 | | var tiposPorId = idsParesFictos.Count == 0 |
| | 1 | 162 | | ? new Dictionary<long, string>() |
| | 1 | 163 | | : await _context.CatalogoItems |
| | 1 | 164 | | .AsNoTracking() |
| | 1 | 165 | | .Where(c => idsParesFictos.Contains(c.Id)) |
| | 5 | 166 | | .ToDictionaryAsync(c => c.Id, c => c.Tipo); |
| | | 167 | | |
| | 1 | 168 | | var haberes = new List<ReciboItemDto>(); |
| | 1 | 169 | | var beneficios = new List<ReciboItemDto>(); |
| | 1 | 170 | | var descuentosLegales = new List<ReciboItemDto>(); |
| | 1 | 171 | | var descuentosPersonales = new List<ReciboItemDto>(); |
| | | 172 | | |
| | 14 | 173 | | foreach (var item in items.OrderBy(i => i.CodigoConcepto)) |
| | | 174 | | { |
| | 4 | 175 | | var dto = new ReciboItemDto |
| | 4 | 176 | | { |
| | 4 | 177 | | Codigo = item.CodigoConcepto, |
| | 4 | 178 | | Descripcion = item.NombreConcepto, |
| | 4 | 179 | | RubroContable = item.RubroContable, |
| | 4 | 180 | | Monto = item.Importe |
| | 4 | 181 | | }; |
| | | 182 | | |
| | 4 | 183 | | if (item.Importe > 0) |
| | | 184 | | { |
| | 2 | 185 | | var catalogoItem = catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var t) ? t : null; |
| | 2 | 186 | | var tipo = catalogoItem?.Tipo ?? "haber_normal"; |
| | 2 | 187 | | if (string.Equals(tipo, "ajuste", StringComparison.OrdinalIgnoreCase)) |
| | | 188 | | { /* el ajuste se muestra por separado en columna 3, no suma a haberes */ } |
| | 2 | 189 | | else if (EsFictoParaRecibo(catalogoItem)) |
| | 1 | 190 | | beneficios.Add(dto); |
| | 1 | 191 | | else if (tipo.StartsWith("beneficio_social", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 192 | | beneficios.Add(dto); |
| | | 193 | | else |
| | 1 | 194 | | haberes.Add(dto); |
| | | 195 | | } |
| | | 196 | | else |
| | | 197 | | { |
| | 2 | 198 | | catalogoPorCodigo.TryGetValue(item.CodigoConcepto, out var catalogoItem); |
| | 2 | 199 | | var tipoNeg = catalogoItem?.Tipo; |
| | 2 | 200 | | if (string.Equals(tipoNeg, "ajuste", StringComparison.OrdinalIgnoreCase)) |
| | | 201 | | { /* ajuste negativo: se muestra por separado en columna 3, no suma a descuentos */ } |
| | 2 | 202 | | else if (catalogoItem != null && EsDescuentoLegalParaRecibo(catalogoItem, tiposPorId)) |
| | 2 | 203 | | descuentosLegales.Add(dto); |
| | | 204 | | else |
| | 0 | 205 | | descuentosPersonales.Add(dto); |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | 2 | 209 | | var totalHaberes = haberes.Sum(i => i.Monto); |
| | 2 | 210 | | var totalBeneficios = beneficios.Sum(i => i.Monto); |
| | 3 | 211 | | var totalDescuentosLegales = Math.Abs(descuentosLegales.Sum(i => i.Monto)); |
| | 1 | 212 | | var totalDescuentosPersonales = Math.Abs(descuentosPersonales.Sum(i => i.Monto)); |
| | 1 | 213 | | var totalDescuentos = totalDescuentosLegales + totalDescuentosPersonales; |
| | | 214 | | |
| | 1 | 215 | | var nominal = totalHaberes + totalBeneficios; |
| | 1 | 216 | | var exactLiquid = nominal - totalDescuentosLegales - totalDescuentosPersonales; |
| | 1 | 217 | | var aCobrar = Math.Round(exactLiquid, 0, MidpointRounding.AwayFromZero); |
| | 1 | 218 | | var ajuste = aCobrar - exactLiquid; |
| | | 219 | | |
| | | 220 | | // Civil: SIT19 (presupuestado), SIT20 (baja civil), SIT21 (contratado) |
| | 1 | 221 | | var esCivil = situacionCodigo is "SIT19" or "SIT20" or "SIT21"; |
| | | 222 | | |
| | 1 | 223 | | var cuentaActiva = relacion.Persona.CuentasBancarias.FirstOrDefault(); |
| | | 224 | | |
| | 1 | 225 | | return new ReciboDto |
| | 1 | 226 | | { |
| | 1 | 227 | | Anio = periodo?.Anio ?? 0, |
| | 1 | 228 | | Mes = periodo?.Mes ?? 0, |
| | 1 | 229 | | RelacionLaboralId = relacion.Id, |
| | 1 | 230 | | Cedula = cedula, |
| | 1 | 231 | | PrimerNombre = relacion.Persona.PrimerNombre, |
| | 1 | 232 | | SegundoNombre = relacion.Persona.SegundoNombre ?? string.Empty, |
| | 1 | 233 | | PrimerApellido = relacion.Persona.PrimerApellido, |
| | 1 | 234 | | SegundoApellido = relacion.Persona.SegundoApellido ?? string.Empty, |
| | 1 | 235 | | Grado = relacion.Grado?.Denominacion ?? string.Empty, |
| | 1 | 236 | | Escalafon = relacion.Escalafon?.Denominacion ?? string.Empty, |
| | 1 | 237 | | Regimen = relacion.Regimen.NumeroLey, |
| | 1 | 238 | | Unidad = relacion.Unidad?.Denominacion ?? string.Empty, |
| | 1 | 239 | | Programa = relacion.Programa?.Denominacion ?? string.Empty, |
| | 1 | 240 | | UnidadCodigo = relacion.Unidad?.Codigo ?? string.Empty, |
| | 1 | 241 | | Compania = relacion.SubUnidad?.Codigo ?? string.Empty, |
| | 1 | 242 | | EsOficial = relacion.Grado?.EsOficial ?? false, |
| | 1 | 243 | | EsSubalterno = relacion.Grado?.EsSubalterno ?? false, |
| | 1 | 244 | | EsCivil = esCivil, |
| | 1 | 245 | | FechaInicio = relacion.FechaInicio, |
| | 1 | 246 | | FechaUltimoAscenso = relacion.FechaUltimoAscenso, |
| | 1 | 247 | | Haberes = haberes, |
| | 1 | 248 | | Beneficios = beneficios, |
| | 1 | 249 | | DescuentosLegales = descuentosLegales, |
| | 1 | 250 | | DescuentosPersonales = descuentosPersonales, |
| | 1 | 251 | | TotalHaberes = totalHaberes, |
| | 1 | 252 | | TotalBeneficios = totalBeneficios, |
| | 1 | 253 | | TotalDescuentosLegales = totalDescuentosLegales, |
| | 1 | 254 | | TotalDescuentosPersonales = totalDescuentosPersonales, |
| | 1 | 255 | | TotalDescuentos = totalDescuentos, |
| | 1 | 256 | | Nominal = nominal, |
| | 1 | 257 | | LiquidoPagable = aCobrar, |
| | 1 | 258 | | ACobrar = aCobrar, |
| | 1 | 259 | | Ajuste = ajuste, |
| | 1 | 260 | | DatosBancarios = cuentaActiva != null |
| | 1 | 261 | | ? new ReciboDatosBancariosDto |
| | 1 | 262 | | { |
| | 1 | 263 | | Banco = cuentaActiva.Banco?.Nombre, |
| | 1 | 264 | | Cuenta = cuentaActiva.NumeroCuenta |
| | 1 | 265 | | } |
| | 1 | 266 | | : new ReciboDatosBancariosDto { Observacion = "Sin cuenta registrada" } |
| | 1 | 267 | | }; |
| | 1 | 268 | | } |
| | | 269 | | |
| | | 270 | | private static bool EsDescuentoLegalParaRecibo( |
| | | 271 | | CatalogoReciboItem catalogoItem, |
| | | 272 | | IReadOnlyDictionary<long, string> tiposPorId) |
| | | 273 | | { |
| | 2 | 274 | | if (string.Equals(catalogoItem.Tipo, "descuento_legal", StringComparison.OrdinalIgnoreCase)) |
| | 1 | 275 | | return true; |
| | | 276 | | |
| | 1 | 277 | | return catalogoItem.ItemParFictoId is long itemParFictoId |
| | 1 | 278 | | && tiposPorId.TryGetValue(itemParFictoId, out var tipoPar) |
| | 1 | 279 | | && tipoPar.StartsWith("ficto_", StringComparison.OrdinalIgnoreCase); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | private static bool EsFictoParaRecibo(CatalogoReciboItem? catalogoItem) => |
| | 2 | 283 | | catalogoItem is not null && catalogoItem.Tipo.StartsWith("ficto_", StringComparison.OrdinalIgnoreCase); |
| | | 284 | | |
| | 19 | 285 | | private sealed record CatalogoReciboItem(string Tipo, long? ItemParFictoId); |
| | | 286 | | } |