| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.DataAccess.Repositories; |
| | | 3 | | using FAU.Entidades; |
| | | 4 | | using FAU.Logica.DTOs; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace FAU.Logica.Services; |
| | | 8 | | |
| | | 9 | | public class AguinaldoBajaService : IAguinaldoBajaService |
| | | 10 | | { |
| | | 11 | | private readonly IAguinaldoBajaRepository _aguinaldoBajaRepository; |
| | | 12 | | private readonly ApplicationDbContext _context; |
| | | 13 | | private readonly AguinaldoBajaCalculator _calculator; |
| | | 14 | | private readonly IAuditoriaService _auditoriaService; |
| | | 15 | | private readonly ICurrentUserContext _currentUser; |
| | | 16 | | |
| | 16 | 17 | | public AguinaldoBajaService( |
| | 16 | 18 | | IAguinaldoBajaRepository aguinaldoBajaRepository, |
| | 16 | 19 | | ApplicationDbContext context, |
| | 16 | 20 | | AguinaldoBajaCalculator calculator, |
| | 16 | 21 | | IAuditoriaService auditoriaService, |
| | 16 | 22 | | ICurrentUserContext currentUser) |
| | | 23 | | { |
| | 16 | 24 | | _aguinaldoBajaRepository = aguinaldoBajaRepository; |
| | 16 | 25 | | _context = context; |
| | 16 | 26 | | _calculator = calculator; |
| | 16 | 27 | | _auditoriaService = auditoriaService; |
| | 16 | 28 | | _currentUser = currentUser; |
| | 16 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<AguinaldoValidacionDto> ValidarHistoricoAsync(CalcularAguinaldoBajaRequest request) |
| | | 32 | | { |
| | 8 | 33 | | var resultado = new AguinaldoValidacionDto(); |
| | | 34 | | |
| | 8 | 35 | | if (!EsMesProcesable(request.Mes)) |
| | | 36 | | { |
| | 2 | 37 | | resultado.PersonasConHistoricoIncompleto = 1; |
| | 2 | 38 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 2 | 39 | | { |
| | 2 | 40 | | Tipo = "MES_INVALIDO", |
| | 2 | 41 | | Cedula = string.Empty, |
| | 2 | 42 | | NombreCompleto = string.Empty, |
| | 2 | 43 | | Descripcion = "El cálculo de aguinaldo por baja no aplica en los meses 6 o 12 (cubiertos por el proceso |
| | 2 | 44 | | }); |
| | 2 | 45 | | return resultado; |
| | | 46 | | } |
| | | 47 | | |
| | 6 | 48 | | var parametros = await ObtenerParametrosPeriodoAsync(request); |
| | 6 | 49 | | if (parametros is null) |
| | | 50 | | { |
| | 1 | 51 | | resultado.PersonasConHistoricoIncompleto = 1; |
| | 1 | 52 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 1 | 53 | | { |
| | 1 | 54 | | Tipo = "PARAMETROS_INEXISTENTES", |
| | 1 | 55 | | Cedula = string.Empty, |
| | 1 | 56 | | NombreCompleto = string.Empty, |
| | 1 | 57 | | Descripcion = "No existe configuración de parámetros para el período solicitado." |
| | 1 | 58 | | }); |
| | 1 | 59 | | return resultado; |
| | | 60 | | } |
| | | 61 | | |
| | 5 | 62 | | var bajas = (await ObtenerBajasParaPeriodoAsync(request)).ToList(); |
| | 5 | 63 | | resultado.TotalPersonasActivas = bajas.Count; |
| | | 64 | | |
| | 5 | 65 | | var semestre = CalcularSemestre(request.Mes); |
| | 9 | 66 | | var personaIds = bajas.Select(b => b.RelacionLaboral.PersonaId).ToList(); |
| | | 67 | | |
| | | 68 | | // Pre-cargar historico de todo el período para evitar N+1 |
| | 5 | 69 | | var historicosPorPersona = await CargarHistoricoSemestreAsync(request, personaIds); |
| | | 70 | | |
| | 18 | 71 | | foreach (var baja in bajas) |
| | | 72 | | { |
| | 4 | 73 | | var personaId = baja.RelacionLaboral.PersonaId; |
| | | 74 | | |
| | 4 | 75 | | var yaProcesado = await _aguinaldoBajaRepository.GetByPersonaFechaBajaAsync( |
| | 4 | 76 | | personaId, baja.FechaMovimiento); |
| | | 77 | | |
| | 4 | 78 | | if (yaProcesado is not null) |
| | | 79 | | { |
| | 1 | 80 | | resultado.PersonasConHistoricoIncompleto++; |
| | 1 | 81 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 1 | 82 | | { |
| | 1 | 83 | | Tipo = "YA_PROCESADO", |
| | 1 | 84 | | Cedula = baja.RelacionLaboral.Persona.Cedula, |
| | 1 | 85 | | NombreCompleto = baja.RelacionLaboral.Persona.NombreCompleto, |
| | 1 | 86 | | Descripcion = "Ya existe un aguinaldo por baja para esta fecha de baja." |
| | 1 | 87 | | }); |
| | 1 | 88 | | continue; |
| | | 89 | | } |
| | | 90 | | |
| | 3 | 91 | | var cubiertoPorSemestral = await _context.Set<Aguinaldo>() |
| | 3 | 92 | | .AnyAsync(a => a.PersonaId == personaId |
| | 3 | 93 | | && a.Anio == request.Anio |
| | 3 | 94 | | && a.Semestre == semestre); |
| | | 95 | | |
| | 3 | 96 | | if (cubiertoPorSemestral) |
| | | 97 | | { |
| | 1 | 98 | | resultado.PersonasConHistoricoIncompleto++; |
| | 1 | 99 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 1 | 100 | | { |
| | 1 | 101 | | Tipo = "CUBIERTO_POR_SEMESTRAL", |
| | 1 | 102 | | Cedula = baja.RelacionLaboral.Persona.Cedula, |
| | 1 | 103 | | NombreCompleto = baja.RelacionLaboral.Persona.NombreCompleto, |
| | 1 | 104 | | Descripcion = $"El semestre {semestre}/{request.Anio} ya fue cubierto por el proceso de aguinaldo se |
| | 1 | 105 | | }); |
| | 1 | 106 | | continue; |
| | | 107 | | } |
| | | 108 | | |
| | 2 | 109 | | var mesesComputados = CalcularMesesComputados(request, baja.RelacionLaboral.FechaInicio); |
| | 2 | 110 | | if (mesesComputados <= 0) |
| | | 111 | | { |
| | 0 | 112 | | resultado.PersonasConHistoricoIncompleto++; |
| | 0 | 113 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 0 | 114 | | { |
| | 0 | 115 | | Tipo = "MESES_INVALIDOS", |
| | 0 | 116 | | Cedula = baja.RelacionLaboral.Persona.Cedula, |
| | 0 | 117 | | NombreCompleto = baja.RelacionLaboral.Persona.NombreCompleto, |
| | 0 | 118 | | Descripcion = "No se pudo calcular el período de meses válidos para la baja." |
| | 0 | 119 | | }); |
| | 0 | 120 | | continue; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Verificar meses faltantes en historico_liquidaciones |
| | 2 | 124 | | var mesesEsperados = ObtenerMesesEsperados(request, baja.RelacionLaboral.FechaInicio); |
| | 2 | 125 | | var encontrados = historicosPorPersona.GetValueOrDefault(personaId) ?? new HashSet<(short, short)>(); |
| | 8 | 126 | | var faltantes = mesesEsperados.Where(m => !encontrados.Contains(m)).ToList(); |
| | | 127 | | |
| | 2 | 128 | | if (faltantes.Any()) |
| | | 129 | | { |
| | 1 | 130 | | resultado.PersonasConHistoricoIncompleto++; |
| | 1 | 131 | | resultado.Incidencias.Add(new AguinaldoIncidenciaDto |
| | 1 | 132 | | { |
| | 1 | 133 | | Tipo = "HistoricoIncompleto", |
| | 1 | 134 | | Cedula = baja.RelacionLaboral.Persona.Cedula, |
| | 1 | 135 | | NombreCompleto = baja.RelacionLaboral.Persona.NombreCompleto, |
| | 1 | 136 | | MesesFaltantes = faltantes.Select(m => FormatearMesAnio(m.Anio, m.Mes)).ToList(), |
| | 1 | 137 | | Descripcion = "Faltan meses en el histórico de liquidaciones para el período de baja." |
| | 1 | 138 | | }); |
| | | 139 | | } |
| | | 140 | | else |
| | | 141 | | { |
| | 1 | 142 | | resultado.PersonasListasParaCalcular++; |
| | | 143 | | } |
| | 2 | 144 | | } |
| | | 145 | | |
| | 5 | 146 | | return resultado; |
| | 8 | 147 | | } |
| | | 148 | | |
| | | 149 | | public async Task<(bool Success, IEnumerable<AguinaldoBajaDto> Resultados, string? Error)> CalcularAguinaldoBajaAsyn |
| | | 150 | | { |
| | 5 | 151 | | if (!EsMesProcesable(request.Mes)) |
| | | 152 | | { |
| | 1 | 153 | | return (false, Array.Empty<AguinaldoBajaDto>(), "El cálculo de aguinaldo por baja no aplica en los meses 6 o |
| | | 154 | | } |
| | | 155 | | |
| | 4 | 156 | | var parametros = await ObtenerParametrosPeriodoAsync(request); |
| | 4 | 157 | | if (parametros is null) |
| | | 158 | | { |
| | 1 | 159 | | return (false, Array.Empty<AguinaldoBajaDto>(), "No existe configuración de parámetros para el período solic |
| | | 160 | | } |
| | | 161 | | |
| | 3 | 162 | | var bajas = await ObtenerBajasParaPeriodoAsync(request); |
| | 3 | 163 | | var resultados = new List<AguinaldoBajaDto>(); |
| | | 164 | | |
| | 3 | 165 | | var semestre = CalcularSemestre(request.Mes); |
| | 3 | 166 | | var esPrimerSemestre = request.Mes <= 6; |
| | 3 | 167 | | var mesInicioPeriodo = esPrimerSemestre ? (short)12 : (short)6; |
| | 3 | 168 | | var mesFinPeriodo = (short)request.Mes; |
| | 3 | 169 | | var mesLiquidacion = (short)request.Mes; |
| | | 170 | | |
| | 12 | 171 | | foreach (var baja in bajas) |
| | | 172 | | { |
| | 3 | 173 | | var existe = await _aguinaldoBajaRepository.GetByPersonaFechaBajaAsync( |
| | 3 | 174 | | baja.RelacionLaboral.PersonaId, |
| | 3 | 175 | | baja.FechaMovimiento); |
| | | 176 | | |
| | 3 | 177 | | if (existe is not null) |
| | | 178 | | continue; |
| | | 179 | | |
| | | 180 | | // D6: No procesar si el semestre ya fue cubierto por aguinaldo semestral |
| | 2 | 181 | | var cubiertoPorSemestral = await _context.Set<Aguinaldo>() |
| | 2 | 182 | | .AnyAsync(a => a.PersonaId == baja.RelacionLaboral.PersonaId |
| | 2 | 183 | | && a.Anio == request.Anio |
| | 2 | 184 | | && a.Semestre == semestre); |
| | | 185 | | |
| | 2 | 186 | | if (cubiertoPorSemestral) |
| | | 187 | | continue; |
| | | 188 | | |
| | 1 | 189 | | var mesesComputados = CalcularMesesComputados(request, baja.RelacionLaboral.FechaInicio); |
| | 1 | 190 | | if (mesesComputados <= 0) |
| | | 191 | | continue; |
| | | 192 | | |
| | 1 | 193 | | var totalHaberesGravados = await ObtenerTotalHaberesGravadosAsync(request, baja.RelacionLaboral.PersonaId); |
| | 1 | 194 | | var tasaMontepio = baja.RelacionLaboral.Regimen.EsLeyVieja |
| | 1 | 195 | | ? parametros.TasaMontepioLeyVieja |
| | 1 | 196 | | : parametros.TasaMontepioLeyNueva; |
| | 1 | 197 | | var tasaSanidad = parametros.TasaFonasa; |
| | | 198 | | |
| | 1 | 199 | | var resultadoCalculo = _calculator.Calcular( |
| | 1 | 200 | | totalHaberesGravados, |
| | 1 | 201 | | mesesComputados, |
| | 1 | 202 | | tasaMontepio, |
| | 1 | 203 | | tasaSanidad); |
| | | 204 | | |
| | 1 | 205 | | var estado = totalHaberesGravados == 0m |
| | 1 | 206 | | ? "SIN_DERECHO" |
| | 1 | 207 | | : resultadoCalculo.Liquido <= 0m |
| | 1 | 208 | | ? "INCIDENCIA" |
| | 1 | 209 | | : "VALIDO"; |
| | | 210 | | |
| | 1 | 211 | | var aguinaldo = new AguinaldoBaja |
| | 1 | 212 | | { |
| | 1 | 213 | | PersonaId = baja.RelacionLaboral.PersonaId, |
| | 1 | 214 | | Cedula = baja.RelacionLaboral.Persona.Cedula, |
| | 1 | 215 | | NombreCompleto = baja.RelacionLaboral.Persona.NombreCompleto, |
| | 1 | 216 | | FechaBaja = baja.FechaMovimiento, |
| | 1 | 217 | | MotivoBajaCodigo = baja.RelacionLaboral.MotivoBaja?.Codigo, |
| | 1 | 218 | | Anio = (short)request.Anio, |
| | 1 | 219 | | Mes = (short)request.Mes, |
| | 1 | 220 | | Semestre = semestre, |
| | 1 | 221 | | TotalHaberesGravados = totalHaberesGravados, |
| | 1 | 222 | | MesesComputados = mesesComputados, |
| | 1 | 223 | | MesInicioPeriodo = mesInicioPeriodo, |
| | 1 | 224 | | MesFinPeriodo = mesFinPeriodo, |
| | 1 | 225 | | MesLiquidacion = mesLiquidacion, |
| | 1 | 226 | | Nominal = resultadoCalculo.Nominal, |
| | 1 | 227 | | Montepio = resultadoCalculo.Montepio, |
| | 1 | 228 | | Sanidad = resultadoCalculo.Sanidad, |
| | 1 | 229 | | TotalDescuentos = resultadoCalculo.TotalDescuentos, |
| | 1 | 230 | | Liquido = resultadoCalculo.Liquido, |
| | 1 | 231 | | Redondeo = resultadoCalculo.Redondeo, |
| | 1 | 232 | | Estado = estado, |
| | 1 | 233 | | Items = new List<AguinaldoBajaItem> |
| | 1 | 234 | | { |
| | 1 | 235 | | new AguinaldoBajaItem |
| | 1 | 236 | | { |
| | 1 | 237 | | CodigoConcepto = "AGUINALDO_NOMINAL", |
| | 1 | 238 | | NombreConcepto = "Nominal aguinaldo por baja", |
| | 1 | 239 | | Importe = resultadoCalculo.Nominal, |
| | 1 | 240 | | Tipo = "haber" |
| | 1 | 241 | | }, |
| | 1 | 242 | | new AguinaldoBajaItem |
| | 1 | 243 | | { |
| | 1 | 244 | | CodigoConcepto = "AGUINALDO_MONTEPIO", |
| | 1 | 245 | | NombreConcepto = "Montepío aguinaldo por baja", |
| | 1 | 246 | | Importe = -resultadoCalculo.Montepio, |
| | 1 | 247 | | Tipo = "descuento" |
| | 1 | 248 | | }, |
| | 1 | 249 | | new AguinaldoBajaItem |
| | 1 | 250 | | { |
| | 1 | 251 | | CodigoConcepto = "AGUINALDO_DNS", |
| | 1 | 252 | | NombreConcepto = "D.N.S.FF.AA. aguinaldo por baja", |
| | 1 | 253 | | Importe = -resultadoCalculo.Sanidad, |
| | 1 | 254 | | Tipo = "descuento" |
| | 1 | 255 | | } |
| | 1 | 256 | | } |
| | 1 | 257 | | }; |
| | | 258 | | |
| | 1 | 259 | | var creado = await _aguinaldoBajaRepository.CreateAsync(aguinaldo); |
| | 1 | 260 | | resultados.Add(AguinaldoBajaDto.FromEntity(creado)); |
| | | 261 | | |
| | | 262 | | try |
| | | 263 | | { |
| | 1 | 264 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 265 | | _currentUser.UserId ?? 0, |
| | 1 | 266 | | AccionEnum.GenerarLiquidacion, |
| | 1 | 267 | | ContextoEnum.Liquidaciones, |
| | 1 | 268 | | entidad: "aguinaldo_baja", |
| | 1 | 269 | | entidadId: creado.Id, |
| | 1 | 270 | | detalle: new { Resultado = resultadoCalculo, Estado = estado }); |
| | 1 | 271 | | } |
| | 0 | 272 | | catch |
| | | 273 | | { |
| | | 274 | | // No bloquear el proceso por fallos en auditoría |
| | 0 | 275 | | } |
| | 1 | 276 | | } |
| | | 277 | | |
| | 3 | 278 | | return (true, resultados, null); |
| | 5 | 279 | | } |
| | | 280 | | |
| | | 281 | | public async Task<IEnumerable<AguinaldoBajaDto>> ObtenerPorPeriodoAsync(int anio, int mes) |
| | | 282 | | { |
| | 3 | 283 | | if (anio <= 0 || mes < 1 || mes > 12) |
| | 2 | 284 | | return Enumerable.Empty<AguinaldoBajaDto>(); |
| | | 285 | | |
| | 1 | 286 | | var registros = await _aguinaldoBajaRepository.GetByPeriodoAsync((short)anio, (short)mes); |
| | 1 | 287 | | return registros.Select(AguinaldoBajaDto.FromEntity).ToList(); |
| | 3 | 288 | | } |
| | | 289 | | |
| | | 290 | | private async Task<IEnumerable<MovimientoLaboral>> ObtenerBajasParaPeriodoAsync(CalcularAguinaldoBajaRequest request |
| | | 291 | | { |
| | 8 | 292 | | return await _context.MovimientosLaborales |
| | 8 | 293 | | .Include(m => m.RelacionLaboral) |
| | 8 | 294 | | .ThenInclude(rl => rl.Persona) |
| | 8 | 295 | | .Include(m => m.RelacionLaboral) |
| | 8 | 296 | | .ThenInclude(rl => rl.Regimen) |
| | 8 | 297 | | .Include(m => m.RelacionLaboral) |
| | 8 | 298 | | .ThenInclude(rl => rl.Situacion) |
| | 8 | 299 | | .Include(m => m.RelacionLaboral) |
| | 8 | 300 | | .ThenInclude(rl => rl.MotivoBaja) |
| | 8 | 301 | | .Include(m => m.TipoMovimiento) |
| | 8 | 302 | | .Where(m => m.FechaMovimiento.Year == request.Anio |
| | 8 | 303 | | && m.FechaMovimiento.Month == request.Mes |
| | 8 | 304 | | && !m.TipoMovimiento.EsAlta) |
| | 8 | 305 | | .ToListAsync(); |
| | 8 | 306 | | } |
| | | 307 | | |
| | | 308 | | // D3: el cálculo de baja aplica en todos los meses EXCEPTO junio y diciembre, |
| | | 309 | | // que quedan cubiertos por el proceso semestral. |
| | | 310 | | private static bool EsMesProcesable(int mes) |
| | | 311 | | { |
| | 13 | 312 | | return mes != 6 && mes != 12; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | private static short CalcularSemestre(int mes) |
| | | 316 | | { |
| | 8 | 317 | | return mes <= 6 ? (short)1 : (short)2; |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | private static short CalcularMesesComputados(CalcularAguinaldoBajaRequest request, DateTime fechaInicio) |
| | | 321 | | { |
| | | 322 | | // S1: comienza en diciembre del año anterior |
| | | 323 | | // S2: comienza en junio del año actual |
| | 3 | 324 | | var esPrimerSemestre = request.Mes <= 6; |
| | 3 | 325 | | var inicioSemestre = esPrimerSemestre |
| | 3 | 326 | | ? new DateTime(request.Anio - 1, 12, 1) |
| | 3 | 327 | | : new DateTime(request.Anio, 6, 1); |
| | | 328 | | |
| | 3 | 329 | | var fechaInicioValida = fechaInicio > inicioSemestre ? fechaInicio : inicioSemestre; |
| | 3 | 330 | | var fechaFin = new DateTime(request.Anio, request.Mes, 1); |
| | | 331 | | |
| | 3 | 332 | | if (fechaInicioValida > fechaFin) |
| | 0 | 333 | | return 0; |
| | | 334 | | |
| | 3 | 335 | | return (short)((fechaFin.Year - fechaInicioValida.Year) * 12 + fechaFin.Month - fechaInicioValida.Month + 1); |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | private async Task<decimal> ObtenerTotalHaberesGravadosAsync(CalcularAguinaldoBajaRequest request, long personaId) |
| | | 339 | | { |
| | 1 | 340 | | var esPrimerSemestre = request.Mes <= 6; |
| | | 341 | | |
| | 1 | 342 | | if (esPrimerSemestre) |
| | | 343 | | { |
| | 1 | 344 | | return await _context.HistoricoLiquidaciones |
| | 1 | 345 | | .Where(h => h.PersonaId == personaId |
| | 1 | 346 | | && ((h.Anio == request.Anio - 1 && h.Mes == 12) |
| | 1 | 347 | | || (h.Anio == request.Anio && h.Mes >= 1 && h.Mes <= request.Mes))) |
| | 1 | 348 | | .SumAsync(h => (decimal?)h.HaberesGravados) ?? 0m; |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | return await _context.HistoricoLiquidaciones |
| | 0 | 352 | | .Where(h => h.PersonaId == personaId |
| | 0 | 353 | | && h.Anio == request.Anio |
| | 0 | 354 | | && h.Mes >= 6 && h.Mes <= request.Mes) |
| | 0 | 355 | | .SumAsync(h => (decimal?)h.HaberesGravados) ?? 0m; |
| | 1 | 356 | | } |
| | | 357 | | |
| | | 358 | | private async Task<ParametroPeriodo?> ObtenerParametrosPeriodoAsync(CalcularAguinaldoBajaRequest request) |
| | | 359 | | { |
| | 10 | 360 | | return await _context.ParametrosPeriodo |
| | 10 | 361 | | .Where(p => p.Anio < request.Anio || (p.Anio == request.Anio && p.Mes <= request.Mes)) |
| | 10 | 362 | | .OrderByDescending(p => p.Anio) |
| | 10 | 363 | | .ThenByDescending(p => p.Mes) |
| | 10 | 364 | | .FirstOrDefaultAsync(); |
| | 10 | 365 | | } |
| | | 366 | | |
| | | 367 | | private async Task<Dictionary<long, HashSet<(short Anio, short Mes)>>> CargarHistoricoSemestreAsync( |
| | | 368 | | CalcularAguinaldoBajaRequest request, List<long> personaIds) |
| | | 369 | | { |
| | 5 | 370 | | if (personaIds.Count == 0) |
| | 1 | 371 | | return new Dictionary<long, HashSet<(short Anio, short Mes)>>(); |
| | | 372 | | |
| | | 373 | | IQueryable<HistoricoLiquidacion> query; |
| | | 374 | | |
| | 4 | 375 | | if (request.Mes <= 6) |
| | | 376 | | { |
| | | 377 | | // 1er semestre: Dic año anterior + Ene hasta mes de baja del año actual |
| | 4 | 378 | | var anioAnterior = (short)(request.Anio - 1); |
| | 4 | 379 | | var anioActual = (short)request.Anio; |
| | 4 | 380 | | var mesHasta = (short)request.Mes; |
| | 4 | 381 | | query = _context.HistoricoLiquidaciones |
| | 4 | 382 | | .Where(h => personaIds.Contains(h.PersonaId) |
| | 4 | 383 | | && ((h.Anio == anioAnterior && h.Mes == 12) |
| | 4 | 384 | | || (h.Anio == anioActual && h.Mes >= 1 && h.Mes <= mesHasta))); |
| | | 385 | | } |
| | | 386 | | else |
| | | 387 | | { |
| | | 388 | | // 2do semestre: Jun hasta mes de baja del año actual |
| | 0 | 389 | | var anioActual = (short)request.Anio; |
| | 0 | 390 | | var mesHasta = (short)request.Mes; |
| | 0 | 391 | | query = _context.HistoricoLiquidaciones |
| | 0 | 392 | | .Where(h => personaIds.Contains(h.PersonaId) |
| | 0 | 393 | | && h.Anio == anioActual |
| | 0 | 394 | | && h.Mes >= 6 && h.Mes <= mesHasta); |
| | | 395 | | } |
| | | 396 | | |
| | 4 | 397 | | var registros = await query |
| | 4 | 398 | | .Select(h => new { h.PersonaId, h.Anio, h.Mes }) |
| | 4 | 399 | | .ToListAsync(); |
| | | 400 | | |
| | 4 | 401 | | return registros |
| | 11 | 402 | | .GroupBy(h => h.PersonaId) |
| | 4 | 403 | | .ToDictionary( |
| | 4 | 404 | | g => g.Key, |
| | 19 | 405 | | g => g.Select(h => (h.Anio, h.Mes)).ToHashSet()); |
| | 5 | 406 | | } |
| | | 407 | | |
| | | 408 | | private static List<(short Anio, short Mes)> ObtenerMesesEsperados( |
| | | 409 | | CalcularAguinaldoBajaRequest request, DateTime fechaInicio) |
| | | 410 | | { |
| | 2 | 411 | | var esPrimerSemestre = request.Mes <= 6; |
| | 2 | 412 | | var inicioSemestre = esPrimerSemestre |
| | 2 | 413 | | ? new DateTime(request.Anio - 1, 12, 1) |
| | 2 | 414 | | : new DateTime(request.Anio, 6, 1); |
| | | 415 | | |
| | 2 | 416 | | var fechaInicioValida = fechaInicio > inicioSemestre ? fechaInicio : inicioSemestre; |
| | 2 | 417 | | var fechaFin = new DateTime(request.Anio, request.Mes, 1); |
| | | 418 | | |
| | 2 | 419 | | if (fechaInicioValida > fechaFin) |
| | 0 | 420 | | return new List<(short Anio, short Mes)>(); |
| | | 421 | | |
| | 2 | 422 | | var meses = new List<(short Anio, short Mes)>(); |
| | 2 | 423 | | var current = new DateTime(fechaInicioValida.Year, fechaInicioValida.Month, 1); |
| | 8 | 424 | | while (current <= fechaFin) |
| | | 425 | | { |
| | 6 | 426 | | meses.Add(((short)current.Year, (short)current.Month)); |
| | 6 | 427 | | current = current.AddMonths(1); |
| | | 428 | | } |
| | 2 | 429 | | return meses; |
| | | 430 | | } |
| | | 431 | | |
| | 1 | 432 | | private static string FormatearMesAnio(short anio, short mes) => $"{mes:D2}/{anio:D4}"; |
| | | 433 | | } |