using bdAsegasa; using bdAsegasa.db; using bdAsegasa.dbcontext; using System; using System.Collections.Generic; using System.Linq; namespace Servicio_Gestion_Asegasa.Procesos { public class ProcesosContabilidad { public static void CompruebaContabilidad() { using var bd = tscgestionasegasa.NuevoContexto(); var cta = bd.cuentascorreo.First(x => x.Codigo == "SEG.GENERALES"); string sDestinatarios = "sevilla@tecnosis.net"; var asis = bd.asientos.Where(x => x.Fecha < x.idEjercicioNavigation.FechaInicio || x.Fecha > x.idEjercicioNavigation.FechaFin || x.apuntes.Any(y => y.idCuentaNavigation.idEjercicio != x.idEjercicio) ).ToList(); if (asis.Count > 0) { var lFechaErronea = new List(); foreach (var asi in asis) { var ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaCierre == null && (x.FechaInicio <= asi.Fecha && x.FechaFin >= asi.Fecha) ); if (ej != null) { lFechaErronea.Add(asi); } else { if (asi.idEjercicio != ej.idEjercicio) { asi.idEjercicio = ej.idEjercicio; } foreach (var ap in asi.apuntes) { ap.idCuenta = bd.cuentas.First(x => x.NumeroCuenta == ap.idCuentaNavigation.NumeroCuenta && x.idEjercicio == asi.idEjercicio).idCuenta; } } } bd.SaveChanges(); if (lFechaErronea.Count > 0) { var lae = lFechaErronea.Select(x => new { NumeroAsiento = x.idAsiento, Fecha = x.Fecha, Ejercicio = x.idEjercicioNavigation.Descripcion }).ToList(); byte[] fe = tsUtilidades.Excel.IEnumerableAExcel(lae); bdAsegasa.db.correos.GeneraRegistroCorreoConAdjunto(bd, "Existen apuntes con cuentas de un ejercicio diferente al del asiento (No se corrige el asiento)", "Existen apuntes con cuentas de un ejercicio diferente al del asiento (Se corrige el asiento)", cta, fe, "AsientosFechaErronea-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx", "Asientos Con Fecha Erronea " + DateTime.Now.ToString("yyyy-MM-dd"), sDestinatarios); var idsErrores = lae.Select(y => y.NumeroAsiento).ToList(); asis = asis.Where(x => !idsErrores.Contains(x.idAsiento)).ToList(); } var la = asis.Select(x => new { NumeroAsiento = x.idAsiento, Fecha = x.Fecha, Ejercicio = x.idEjercicioNavigation.Descripcion }).ToList(); byte[] f = tsUtilidades.Excel.IEnumerableAExcel(la); bdAsegasa.db.correos.GeneraRegistroCorreoConAdjunto(bd, "Existen apuntes con cuentas de un ejercicio diferente al del asiento (Se corrige el asiento)", "Existen apuntes con cuentas de un ejercicio diferente al del asiento (Se corrige el asiento)", cta, f, "ApuntesIncoherentes-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx", "Apuntes Incoherentes " + DateTime.Now.ToString("yyyy-MM-dd"), sDestinatarios); } asis = bd.asientos.Where(x => x.Fecha < x.idEjercicioNavigation.FechaInicio || x.Fecha > x.idEjercicioNavigation.FechaFin).ToList(); if (asis.Count > 0) { var la = asis.Select(x => new { NumeroAsiento = x.idAsiento, Fecha = x.Fecha, Ejercicio = x.idEjercicioNavigation.Descripcion }).ToList(); byte[] f = tsUtilidades.Excel.IEnumerableAExcel(la); bdAsegasa.db.correos.GeneraRegistroCorreoConAdjunto(bd, "Existen asientos con fecha incoherente al del ejercicio", "Existen asientos con fecha incoherente al del ejercicio", cta, f, "AsientosIncoherentes-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx", "Asientos Incoherentes " + DateTime.Now.ToString("yyyy-MM-dd"), sDestinatarios); } asis = bd.asientos.Where(x => Math.Round((double)x.apuntes.Sum(y => y.Debe ), 2) != Math.Round((double)x.apuntes.Sum(y => y.Haber), 2)).ToList(); if (asis.Count > 0) { var la = asis.Select(x => new { NumeroAsiento = x.idAsiento, Fecha = x.Fecha, Ejercicio = x.idEjercicioNavigation.Descripcion, Debe = Math.Round((double)x.apuntes.Sum(y => y.Debe ), 2, MidpointRounding.AwayFromZero), Haber = Math.Round((double)x.apuntes.Sum(y => y.Haber ), 2, MidpointRounding.AwayFromZero) }).ToList(); byte[] f = tsUtilidades.Excel.IEnumerableAExcel(la); bdAsegasa.db.correos.GeneraRegistroCorreoConAdjunto(bd, "Existen asientos descuadrados", "Existen asientos descuadrados", cta, f, "AsientosDescuadrados-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx", "Asientos Descuadrados " + DateTime.Now.ToString("yyyy-MM-dd"), sDestinatarios); } asis = bd.asientos.Where(x => x.Fecha < x.idEjercicioNavigation.FechaInicio || x.Fecha > x.idEjercicioNavigation.FechaFin).ToList(); } } }