| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using FAU.DataAccess; |
| | | 5 | | using FAU.DataAccess.Repositories; |
| | | 6 | | using FAU.Entidades; |
| | | 7 | | using FAU.Logica.DTOs; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | |
| | | 10 | | namespace FAU.Logica.Services; |
| | | 11 | | |
| | | 12 | | public class PeriodoService : IPeriodoService |
| | | 13 | | { |
| | | 14 | | private readonly IPeriodosRepository _periodosRepository; |
| | | 15 | | private readonly ISnapshotsRepository _snapshotsRepository; |
| | | 16 | | private readonly ApplicationDbContext _context; |
| | | 17 | | private readonly IAuditoriaService _auditoriaService; |
| | | 18 | | private readonly ICurrentUserContext _currentUser; |
| | | 19 | | private readonly IInsumoEvaluador _evaluador; |
| | | 20 | | |
| | 25 | 21 | | public PeriodoService( |
| | 25 | 22 | | IPeriodosRepository periodosRepository, |
| | 25 | 23 | | ISnapshotsRepository snapshotsRepository, |
| | 25 | 24 | | ApplicationDbContext context, |
| | 25 | 25 | | IAuditoriaService auditoriaService, |
| | 25 | 26 | | ICurrentUserContext currentUser, |
| | 25 | 27 | | IInsumoEvaluador evaluador) |
| | | 28 | | { |
| | 25 | 29 | | _periodosRepository = periodosRepository; |
| | 25 | 30 | | _snapshotsRepository = snapshotsRepository; |
| | 25 | 31 | | _context = context; |
| | 25 | 32 | | _auditoriaService = auditoriaService; |
| | 25 | 33 | | _currentUser = currentUser; |
| | 25 | 34 | | _evaluador = evaluador; |
| | 25 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<(bool Exito, Periodo? Periodo, string? Error)> AbrirPeriodoAsync(short anio, short mes) |
| | | 38 | | { |
| | 9 | 39 | | if (anio < 2000 || anio > 2100) |
| | | 40 | | { |
| | 0 | 41 | | return (false, null, "El año debe estar entre 2000 y 2100"); |
| | | 42 | | } |
| | | 43 | | |
| | 9 | 44 | | if (mes < 1 || mes > 12) |
| | | 45 | | { |
| | 0 | 46 | | return (false, null, "El mes debe estar entre 1 y 12"); |
| | | 47 | | } |
| | | 48 | | |
| | 9 | 49 | | var periodoExistente = await _periodosRepository.ObtenerPorAnioMesAsync(anio, mes); |
| | 9 | 50 | | if (periodoExistente is not null && periodoExistente.Estado == Periodo.EstadoAbierta && periodoExistente.Activo) |
| | | 51 | | { |
| | 1 | 52 | | return (true, periodoExistente, null); |
| | | 53 | | } |
| | | 54 | | |
| | 8 | 55 | | var periodoEnCurso = await _periodosRepository.ObtenerEnCursoAsync(); |
| | 8 | 56 | | if (periodoEnCurso is not null) |
| | | 57 | | { |
| | 1 | 58 | | return (false, null, $"Ya existe un período activo en estado {periodoEnCurso.Estado}"); |
| | | 59 | | } |
| | | 60 | | |
| | 7 | 61 | | var parametrosPeriodo = await _context.ParametrosPeriodo |
| | 7 | 62 | | .AsNoTracking() |
| | 7 | 63 | | .FirstOrDefaultAsync(p => p.Anio == anio && p.Mes == mes); |
| | | 64 | | |
| | 7 | 65 | | if (parametrosPeriodo is null) |
| | | 66 | | { |
| | 2 | 67 | | return (false, null, "No existen parámetros de liquidación para el período indicado"); |
| | | 68 | | } |
| | | 69 | | |
| | 5 | 70 | | var fechaInicioPeriodo = new DateTime(anio, mes, 1, 0, 0, 0, DateTimeKind.Utc); |
| | 5 | 71 | | var fechaFinPeriodo = fechaInicioPeriodo.AddMonths(1).AddTicks(-1); |
| | | 72 | | |
| | 5 | 73 | | var existeRemuneraciones = await _context.RemuneracionesGrado |
| | 5 | 74 | | .AsNoTracking() |
| | 5 | 75 | | .AnyAsync(r => r.VigenteDesde <= fechaInicioPeriodo && (r.VigenteHasta == null || r.VigenteHasta >= fechaIni |
| | | 76 | | |
| | 5 | 77 | | if (!existeRemuneraciones) |
| | | 78 | | { |
| | 0 | 79 | | return (false, null, "No existen remuneraciones de grado vigentes para el período"); |
| | | 80 | | } |
| | | 81 | | |
| | 5 | 82 | | var existeFranjaIrpf = await _context.FranjasIrpf |
| | 5 | 83 | | .AsNoTracking() |
| | 5 | 84 | | .AnyAsync(f => f.VigenteDesde <= fechaInicioPeriodo && (f.VigenteHasta == null || f.VigenteHasta >= fechaIni |
| | | 85 | | |
| | 5 | 86 | | if (!existeFranjaIrpf) |
| | | 87 | | { |
| | 0 | 88 | | return (false, null, "No existen franjas IRPF vigentes para el período"); |
| | | 89 | | } |
| | | 90 | | |
| | 5 | 91 | | var existeRelacionLaboral = await _context.RelacionesLaborales |
| | 5 | 92 | | .AsNoTracking() |
| | 5 | 93 | | .AnyAsync(r => r.Estado == EstadoRelacion.Activo); |
| | | 94 | | |
| | 5 | 95 | | if (!existeRelacionLaboral) |
| | | 96 | | { |
| | 0 | 97 | | return (false, null, "No hay funcionarios activos en el padrón"); |
| | | 98 | | } |
| | | 99 | | |
| | 5 | 100 | | var snapshotJson = await SerializarSnapshotAsync(anio, mes, parametrosPeriodo, fechaInicioPeriodo, fechaFinPerio |
| | 5 | 101 | | var hashSnapshot = CalcularHash(snapshotJson); |
| | | 102 | | |
| | 5 | 103 | | var snapshot = new SnapshotInsumos |
| | 5 | 104 | | { |
| | 5 | 105 | | ContenidoJson = snapshotJson, |
| | 5 | 106 | | HashIntegridad = hashSnapshot |
| | 5 | 107 | | }; |
| | | 108 | | |
| | 5 | 109 | | var snapshotCreado = await _snapshotsRepository.CrearAsync(snapshot); |
| | | 110 | | |
| | 5 | 111 | | var periodo = new Periodo |
| | 5 | 112 | | { |
| | 5 | 113 | | Anio = anio, |
| | 5 | 114 | | Mes = mes, |
| | 5 | 115 | | Estado = Periodo.EstadoAbierta, |
| | 5 | 116 | | SnapshotId = snapshotCreado.Id, |
| | 5 | 117 | | FechaApertura = DateTime.UtcNow, |
| | 5 | 118 | | UsuarioApertura = _currentUser.UserId ?? 0, |
| | 5 | 119 | | HashSnapshot = hashSnapshot, |
| | 5 | 120 | | Activo = true |
| | 5 | 121 | | }; |
| | | 122 | | |
| | 5 | 123 | | var periodoCreado = await _periodosRepository.CrearAsync(periodo); |
| | | 124 | | |
| | 5 | 125 | | await _auditoriaService.LogAuditoriaAsync( |
| | 5 | 126 | | _currentUser.UserId ?? 0, |
| | 5 | 127 | | AccionEnum.AbrirPeriodoLiquidacion, |
| | 5 | 128 | | ContextoEnum.Liquidaciones, |
| | 5 | 129 | | _currentUser.Host, |
| | 5 | 130 | | entidad: nameof(Periodo), |
| | 5 | 131 | | entidadId: periodoCreado.Id, |
| | 5 | 132 | | detalle: new |
| | 5 | 133 | | { |
| | 5 | 134 | | anio, |
| | 5 | 135 | | mes, |
| | 5 | 136 | | snapshotId = snapshotCreado.Id, |
| | 5 | 137 | | hashSnapshot |
| | 5 | 138 | | }); |
| | | 139 | | |
| | 5 | 140 | | return (true, periodoCreado, null); |
| | 9 | 141 | | } |
| | | 142 | | |
| | | 143 | | public Task<Periodo?> ObtenerActivoAsync() |
| | | 144 | | { |
| | 0 | 145 | | return _periodosRepository.ObtenerActivoAsync(); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public Task<Periodo?> ObtenerEnCursoAsync() |
| | | 149 | | { |
| | 0 | 150 | | return _periodosRepository.ObtenerEnCursoAsync(); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | public Task<Periodo?> ObtenerPorIdAsync(long id) |
| | | 154 | | { |
| | 0 | 155 | | return _periodosRepository.ObtenerPorIdAsync(id); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | public async Task<PrecondicionesDto?> ObtenerPrecondicionesAsync(long periodoId) |
| | | 159 | | { |
| | 0 | 160 | | var validacion = await ValidarInsumosAsync(periodoId); |
| | 0 | 161 | | if (validacion is null) return null; |
| | | 162 | | |
| | 0 | 163 | | bool Ok(string tipo) => validacion.ItemsIncompletos.All(i => i.Tipo != tipo); |
| | | 164 | | string Detail(string tipo, string okMsg, string failMsg) => |
| | 0 | 165 | | Ok(tipo) ? okMsg : (validacion.ItemsIncompletos.FirstOrDefault(i => i.Tipo == tipo)?.Descripcion ?? failMsg) |
| | | 166 | | |
| | 0 | 167 | | var lotesOk = Ok("lote_faltante"); |
| | 0 | 168 | | var novedadesOk = Ok("novedades_pendientes"); |
| | 0 | 169 | | var beneficiosOk = Ok("beneficios_faltantes"); |
| | 0 | 170 | | var remuneracionesOk = Ok("tabla_referencia") || |
| | 0 | 171 | | !validacion.ItemsIncompletos.Any(i => i.Clave == "SinRemuneraciones"); |
| | 0 | 172 | | var franjasOk = !validacion.ItemsIncompletos.Any(i => i.Clave == "SinFranjasIrpf"); |
| | 0 | 173 | | var funcionariosOk = !validacion.ItemsIncompletos.Any(i => i.Clave == "SinFuncionarios"); |
| | | 174 | | |
| | 0 | 175 | | return new PrecondicionesDto |
| | 0 | 176 | | { |
| | 0 | 177 | | LotesAprobados = new PrecondicionItemDto |
| | 0 | 178 | | { |
| | 0 | 179 | | Ok = lotesOk, |
| | 0 | 180 | | Detail = lotesOk ? "Lotes aprobados" : "Sin lotes aprobados para el período" |
| | 0 | 181 | | }, |
| | 0 | 182 | | NovedadesSinConfirmar = new PrecondicionItemDto |
| | 0 | 183 | | { |
| | 0 | 184 | | Ok = novedadesOk, |
| | 0 | 185 | | Detail = novedadesOk ? "Sin novedades pendientes" : |
| | 0 | 186 | | validacion.ItemsIncompletos.FirstOrDefault(i => i.Tipo == "novedades_pendientes")?.Descripcion ?? "N |
| | 0 | 187 | | }, |
| | 0 | 188 | | BeneficiosVigentes = new PrecondicionItemDto |
| | 0 | 189 | | { |
| | 0 | 190 | | Ok = beneficiosOk, |
| | 0 | 191 | | Detail = beneficiosOk ? "Beneficios sociales activos" : "Sin beneficios sociales activos vigentes" |
| | 0 | 192 | | }, |
| | 0 | 193 | | RemuneracionesVigentes = new PrecondicionItemDto |
| | 0 | 194 | | { |
| | 0 | 195 | | Ok = remuneracionesOk, |
| | 0 | 196 | | Detail = remuneracionesOk ? "Tablas de remuneración vigentes" : "Sin tablas de remuneración para el perí |
| | 0 | 197 | | }, |
| | 0 | 198 | | FranjasIrpf = new PrecondicionItemDto |
| | 0 | 199 | | { |
| | 0 | 200 | | Ok = franjasOk, |
| | 0 | 201 | | Detail = franjasOk ? "Franjas IRPF vigentes" : "Sin franjas IRPF para el período" |
| | 0 | 202 | | }, |
| | 0 | 203 | | FuncionariosActivos = new PrecondicionItemDto |
| | 0 | 204 | | { |
| | 0 | 205 | | Ok = funcionariosOk, |
| | 0 | 206 | | Detail = funcionariosOk ? "Funcionarios activos en el padrón" : "No hay funcionarios activos" |
| | 0 | 207 | | } |
| | 0 | 208 | | }; |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | public async Task<ValidacionInsumosDto?> ValidarInsumosAsync(long periodoId) |
| | | 212 | | { |
| | 0 | 213 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 0 | 214 | | if (periodo is null) return null; |
| | 0 | 215 | | return await _evaluador.EvaluarAsync(periodo); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | public async Task<(bool Exito, Periodo? Periodo, ValidacionInsumosDto? Validacion, string? Error)> CerrarInsumosAsyn |
| | | 219 | | long periodoId, List<string>? confirmaciones = null) |
| | | 220 | | { |
| | 8 | 221 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 8 | 222 | | if (periodo is null) |
| | 0 | 223 | | return (false, null, null, "Período no encontrado"); |
| | | 224 | | |
| | 8 | 225 | | if (periodo.Estado != Periodo.EstadoAbierta) |
| | 1 | 226 | | return (false, null, null, $"Solo se puede cerrar insumos desde estado ABIERTA. Estado actual: {periodo.Esta |
| | | 227 | | |
| | 7 | 228 | | var validacion = await _evaluador.EvaluarAsync(periodo); |
| | | 229 | | |
| | 7 | 230 | | if (!validacion.TodosCompletos) |
| | | 231 | | { |
| | 5 | 232 | | if (confirmaciones is null || confirmaciones.Count == 0) |
| | 4 | 233 | | return (false, null, validacion, "Hay insumos incompletos. Revisá el detalle y confirmá cada omisión par |
| | | 234 | | |
| | 2 | 235 | | var bloqueantes = validacion.ItemsIncompletos.Where(i => !i.EsConfirmable).ToList(); |
| | 1 | 236 | | if (bloqueantes.Count > 0) |
| | 0 | 237 | | return (false, null, validacion, "Existen problemas bloqueantes que no pueden omitirse."); |
| | | 238 | | |
| | 1 | 239 | | var clavesSinConfirmar = validacion.ItemsIncompletos |
| | 1 | 240 | | .Where(i => !confirmaciones.Contains(i.Clave)) |
| | 1 | 241 | | .ToList(); |
| | 1 | 242 | | if (clavesSinConfirmar.Count > 0) |
| | 0 | 243 | | return (false, null, new ValidacionInsumosDto |
| | 0 | 244 | | { |
| | 0 | 245 | | TodosCompletos = false, |
| | 0 | 246 | | PuedeConfirmar = true, |
| | 0 | 247 | | ItemsIncompletos = clavesSinConfirmar |
| | 0 | 248 | | }, "Faltan confirmar algunos items incompletos."); |
| | | 249 | | } |
| | | 250 | | |
| | 3 | 251 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 3 | 252 | | periodo.Estado = Periodo.EstadoListaCalculo; |
| | 3 | 253 | | periodo.FechaCierreInsumos = DateTime.UtcNow; |
| | 3 | 254 | | periodo.UsuarioCierreInsumosId = usuarioId; |
| | 3 | 255 | | await _periodosRepository.ActualizarAsync(periodo); |
| | | 256 | | |
| | 3 | 257 | | var omisiones = confirmaciones ?? new List<string>(); |
| | 3 | 258 | | await _auditoriaService.LogAuditoriaAsync( |
| | 3 | 259 | | usuarioId, |
| | 3 | 260 | | AccionEnum.CerrarInsumosPeriodo, |
| | 3 | 261 | | ContextoEnum.Liquidaciones, |
| | 3 | 262 | | _currentUser.Host, |
| | 3 | 263 | | entidad: nameof(Periodo), |
| | 3 | 264 | | entidadId: periodo.Id, |
| | 3 | 265 | | detalle: new |
| | 3 | 266 | | { |
| | 3 | 267 | | anio = periodo.Anio, |
| | 3 | 268 | | mes = periodo.Mes, |
| | 3 | 269 | | omisionesConfirmadas = omisiones.Select(c => |
| | 0 | 270 | | validacion.ItemsIncompletos.FirstOrDefault(i => i.Clave == c)?.Descripcion ?? c) |
| | 3 | 271 | | }); |
| | | 272 | | |
| | 3 | 273 | | return (true, periodo, null, null); |
| | 8 | 274 | | } |
| | | 275 | | |
| | | 276 | | public async Task<(bool Exito, Periodo? Periodo, string? Error)> ReabrirInsumosAsync(long periodoId) |
| | | 277 | | { |
| | 3 | 278 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 3 | 279 | | if (periodo is null) |
| | 0 | 280 | | return (false, null, "Período no encontrado"); |
| | | 281 | | |
| | 3 | 282 | | if (periodo.Estado != Periodo.EstadoListaCalculo) |
| | 1 | 283 | | return (false, null, $"Solo se puede reabrir desde estado LISTA_CALCULO. Estado actual: {periodo.Estado}"); |
| | | 284 | | |
| | 2 | 285 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 2 | 286 | | periodo.Estado = Periodo.EstadoAbierta; |
| | 2 | 287 | | periodo.FechaCierreInsumos = null; |
| | 2 | 288 | | periodo.UsuarioCierreInsumosId = null; |
| | 2 | 289 | | await _periodosRepository.ActualizarAsync(periodo); |
| | | 290 | | |
| | 2 | 291 | | await _auditoriaService.LogAuditoriaAsync( |
| | 2 | 292 | | usuarioId, |
| | 2 | 293 | | AccionEnum.ReabrirInsumosPeriodo, |
| | 2 | 294 | | ContextoEnum.Liquidaciones, |
| | 2 | 295 | | _currentUser.Host, |
| | 2 | 296 | | entidad: nameof(Periodo), |
| | 2 | 297 | | entidadId: periodo.Id, |
| | 2 | 298 | | detalle: new { anio = periodo.Anio, mes = periodo.Mes }); |
| | | 299 | | |
| | 2 | 300 | | return (true, periodo, null); |
| | 3 | 301 | | } |
| | | 302 | | |
| | | 303 | | public async Task<(bool Exito, Periodo? Periodo, string? Error)> IniciarCalculoAsync(long periodoId) |
| | | 304 | | { |
| | 2 | 305 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 2 | 306 | | if (periodo is null) |
| | 0 | 307 | | return (false, null, "Período no encontrado"); |
| | | 308 | | |
| | 2 | 309 | | if (periodo.Estado != Periodo.EstadoListaCalculo) |
| | 1 | 310 | | return (false, null, $"Solo se puede calcular desde estado LISTA_CALCULO. Estado actual: {periodo.Estado}"); |
| | | 311 | | |
| | 1 | 312 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 1 | 313 | | periodo.Estado = Periodo.EstadoEnCalculo; |
| | 1 | 314 | | periodo.FechaCalculo = DateTime.UtcNow; |
| | 1 | 315 | | periodo.UsuarioCalculoId = usuarioId; |
| | 1 | 316 | | await _periodosRepository.ActualizarAsync(periodo); |
| | | 317 | | |
| | 1 | 318 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 319 | | usuarioId, |
| | 1 | 320 | | AccionEnum.IniciarCalculoPeriodo, |
| | 1 | 321 | | ContextoEnum.Liquidaciones, |
| | 1 | 322 | | _currentUser.Host, |
| | 1 | 323 | | entidad: nameof(Periodo), |
| | 1 | 324 | | entidadId: periodo.Id, |
| | 1 | 325 | | detalle: new { anio = periodo.Anio, mes = periodo.Mes }); |
| | | 326 | | |
| | 1 | 327 | | return (true, periodo, null); |
| | 2 | 328 | | } |
| | | 329 | | |
| | | 330 | | public async Task<(bool Exito, Periodo? Periodo, string? Error)> FinalizarCalculoAsync(long periodoId, bool exitoso, |
| | | 331 | | { |
| | 2 | 332 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 2 | 333 | | if (periodo is null) |
| | 0 | 334 | | return (false, null, "Período no encontrado"); |
| | | 335 | | |
| | 2 | 336 | | if (periodo.Estado != Periodo.EstadoEnCalculo) |
| | 0 | 337 | | return (false, null, $"Solo se puede finalizar el cálculo desde estado EN_CALCULO. Estado actual: {periodo.E |
| | | 338 | | |
| | 2 | 339 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 2 | 340 | | if (exitoso) |
| | | 341 | | { |
| | 1 | 342 | | periodo.Estado = Periodo.EstadoCalculada; |
| | 1 | 343 | | periodo.FechaCalculo = DateTime.UtcNow; |
| | | 344 | | } |
| | | 345 | | else |
| | | 346 | | { |
| | 1 | 347 | | periodo.Estado = Periodo.EstadoListaCalculo; |
| | | 348 | | } |
| | | 349 | | |
| | 2 | 350 | | await _periodosRepository.ActualizarAsync(periodo); |
| | | 351 | | |
| | 2 | 352 | | await _auditoriaService.LogAuditoriaAsync( |
| | 2 | 353 | | usuarioId, |
| | 2 | 354 | | AccionEnum.IniciarCalculoPeriodo, |
| | 2 | 355 | | ContextoEnum.Liquidaciones, |
| | 2 | 356 | | _currentUser.Host, |
| | 2 | 357 | | entidad: nameof(Periodo), |
| | 2 | 358 | | entidadId: periodo.Id, |
| | 2 | 359 | | detalle: new { exitoso, mensajeError }); |
| | | 360 | | |
| | 2 | 361 | | return (true, periodo, null); |
| | 2 | 362 | | } |
| | | 363 | | |
| | | 364 | | public async Task<(bool Exito, Periodo? Periodo, string? Error)> CerrarAsync(long periodoId) |
| | | 365 | | { |
| | 3 | 366 | | var periodo = await _periodosRepository.ObtenerPorIdAsync(periodoId); |
| | 3 | 367 | | if (periodo is null) |
| | 0 | 368 | | return (false, null, "Período no encontrado"); |
| | | 369 | | |
| | 3 | 370 | | if (periodo.Estado != Periodo.EstadoCalculada) |
| | 1 | 371 | | return (false, null, $"Solo se puede cerrar desde estado CALCULADA. Estado actual: {periodo.Estado}"); |
| | | 372 | | |
| | 2 | 373 | | var usuarioId = _currentUser.UserId ?? 0; |
| | 2 | 374 | | periodo.Estado = Periodo.EstadoCerrada; |
| | 2 | 375 | | periodo.FechaCierre = DateTime.UtcNow; |
| | 2 | 376 | | periodo.UsuarioCierreId = usuarioId; |
| | 2 | 377 | | await _periodosRepository.ActualizarAsync(periodo); |
| | | 378 | | |
| | 2 | 379 | | await _auditoriaService.LogAuditoriaAsync( |
| | 2 | 380 | | usuarioId, |
| | 2 | 381 | | AccionEnum.CerrarPeriodo, |
| | 2 | 382 | | ContextoEnum.Liquidaciones, |
| | 2 | 383 | | _currentUser.Host, |
| | 2 | 384 | | entidad: nameof(Periodo), |
| | 2 | 385 | | entidadId: periodo.Id, |
| | 2 | 386 | | detalle: new { anio = periodo.Anio, mes = periodo.Mes }); |
| | | 387 | | |
| | 2 | 388 | | return (true, periodo, null); |
| | 3 | 389 | | } |
| | | 390 | | |
| | | 391 | | private async Task<string> SerializarSnapshotAsync( |
| | | 392 | | short anio, |
| | | 393 | | short mes, |
| | | 394 | | ParametroPeriodo parametrosPeriodo, |
| | | 395 | | DateTime fechaInicioPeriodo, |
| | | 396 | | DateTime fechaFinPeriodo) |
| | | 397 | | { |
| | 5 | 398 | | var lotes = await _context.LotesCompensacion |
| | 5 | 399 | | .AsNoTracking() |
| | 5 | 400 | | .Where(l => l.Periodo.Year == anio && l.Periodo.Month == mes && l.Estado == "aprobado") |
| | 5 | 401 | | .Select(l => new |
| | 5 | 402 | | { |
| | 5 | 403 | | l.Id, |
| | 5 | 404 | | l.TipoCompensacionId, |
| | 5 | 405 | | l.Periodo, |
| | 5 | 406 | | l.Estado, |
| | 5 | 407 | | l.Fuente, |
| | 5 | 408 | | l.NombreArchivo, |
| | 5 | 409 | | l.HashArchivo, |
| | 5 | 410 | | l.UsuarioId, |
| | 5 | 411 | | l.AprobadoPor, |
| | 5 | 412 | | l.AprobadoEn |
| | 5 | 413 | | }) |
| | 5 | 414 | | .ToListAsync(); |
| | | 415 | | |
| | 5 | 416 | | var beneficios = await _context.BeneficiosSociales |
| | 5 | 417 | | .AsNoTracking() |
| | 5 | 418 | | .Where(b => b.Estado == "activo" && b.VigenteDesde <= fechaFinPeriodo && (b.VigenteHasta == null || b.Vigent |
| | 5 | 419 | | .Select(b => new |
| | 5 | 420 | | { |
| | 5 | 421 | | b.Id, |
| | 5 | 422 | | b.PersonaId, |
| | 5 | 423 | | b.TipoBeneficioId, |
| | 5 | 424 | | b.Cantidad, |
| | 5 | 425 | | b.VigenteDesde, |
| | 5 | 426 | | b.VigenteHasta, |
| | 5 | 427 | | b.Estado, |
| | 5 | 428 | | b.ClaveEvento, |
| | 5 | 429 | | b.FechaEvento, |
| | 5 | 430 | | b.RetroactividadPendiente |
| | 5 | 431 | | }) |
| | 5 | 432 | | .ToListAsync(); |
| | | 433 | | |
| | 5 | 434 | | var remuneraciones = await _context.RemuneracionesGrado |
| | 5 | 435 | | .AsNoTracking() |
| | 5 | 436 | | .Where(r => r.VigenteDesde <= fechaInicioPeriodo && (r.VigenteHasta == null || r.VigenteHasta >= fechaInicio |
| | 5 | 437 | | .Select(r => new |
| | 5 | 438 | | { |
| | 5 | 439 | | r.Id, |
| | 5 | 440 | | r.GradoId, |
| | 5 | 441 | | r.ItemId, |
| | 5 | 442 | | r.Monto, |
| | 5 | 443 | | r.VigenteDesde, |
| | 5 | 444 | | r.VigenteHasta |
| | 5 | 445 | | }) |
| | 5 | 446 | | .ToListAsync(); |
| | | 447 | | |
| | 5 | 448 | | var franjasIrpf = await _context.FranjasIrpf |
| | 5 | 449 | | .AsNoTracking() |
| | 5 | 450 | | .Where(f => f.VigenteDesde <= fechaInicioPeriodo && (f.VigenteHasta == null || f.VigenteHasta >= fechaInicio |
| | 5 | 451 | | .Select(f => new |
| | 5 | 452 | | { |
| | 5 | 453 | | f.Id, |
| | 5 | 454 | | f.NumeroFranja, |
| | 5 | 455 | | f.DesdeBpc, |
| | 5 | 456 | | f.HastaBpc, |
| | 5 | 457 | | f.Tasa, |
| | 5 | 458 | | f.VigenteDesde, |
| | 5 | 459 | | f.VigenteHasta |
| | 5 | 460 | | }) |
| | 5 | 461 | | .ToListAsync(); |
| | | 462 | | |
| | 5 | 463 | | var snapshot = new |
| | 5 | 464 | | { |
| | 5 | 465 | | anio, |
| | 5 | 466 | | mes, |
| | 5 | 467 | | fechaCreacion = DateTime.UtcNow, |
| | 5 | 468 | | parametros = new |
| | 5 | 469 | | { |
| | 5 | 470 | | parametrosPeriodo.Anio, |
| | 5 | 471 | | parametrosPeriodo.Mes, |
| | 5 | 472 | | parametrosPeriodo.BpcAnual, |
| | 5 | 473 | | parametrosPeriodo.TasaMontepioLeyVieja, |
| | 5 | 474 | | parametrosPeriodo.TasaMontepioLeyNueva, |
| | 5 | 475 | | parametrosPeriodo.TasaFonasa, |
| | 5 | 476 | | parametrosPeriodo.TasaFrl, |
| | 5 | 477 | | parametrosPeriodo.UmbralDeduccionBpc, |
| | 5 | 478 | | parametrosPeriodo.TasaDeduccionAlta, |
| | 5 | 479 | | parametrosPeriodo.TasaDeduccionBaja, |
| | 5 | 480 | | parametrosPeriodo.PorcentajeMinimoDeficit |
| | 5 | 481 | | }, |
| | 5 | 482 | | lotesCompensacion = lotes, |
| | 5 | 483 | | beneficiosSociales = beneficios, |
| | 5 | 484 | | remuneracionesGrado = remuneraciones, |
| | 5 | 485 | | franjasIrpf = franjasIrpf |
| | 5 | 486 | | }; |
| | | 487 | | |
| | 5 | 488 | | return JsonSerializer.Serialize(snapshot, new JsonSerializerOptions |
| | 5 | 489 | | { |
| | 5 | 490 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 5 | 491 | | WriteIndented = false |
| | 5 | 492 | | }); |
| | 5 | 493 | | } |
| | | 494 | | |
| | | 495 | | private static string CalcularHash(string contenidoJson) |
| | | 496 | | { |
| | 5 | 497 | | using var sha256 = SHA256.Create(); |
| | 5 | 498 | | var bytes = Encoding.UTF8.GetBytes(contenidoJson); |
| | 5 | 499 | | return Convert.ToHexString(sha256.ComputeHash(bytes)).ToLowerInvariant(); |
| | 5 | 500 | | } |
| | | 501 | | } |