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