using bdGrupoSanchoToro; using System; using System.Collections.Generic; using System.Data; using System.Linq; using tsUtilidades.Extensiones; namespace bdGrupoSanchoToro.db { public partial class ejercicioscontables { public static int ObtieneidEjercicioAbierto(DateOnly Fecha) { var bd = tscGrupoSanchoToro.NuevoContexto(); var ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaInicio <= Fecha & x.FechaFin >= Fecha & x.FechaCierre.HasValue == false); if (ej is null) { ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaInicio <= Fecha & x.FechaFin >= Fecha & x.FechaCierre.HasValue); if (ej is null) { if (DateOnly.FromDateTime(DateTime.Today).DayNumber - Fecha.DayNumber > 365d || Fecha.DayNumber - DateOnly.FromDateTime(DateTime.Today).DayNumber > 60d) { throw new Exception("La fecha del ejercicio " + Fecha.ToShortDateString() + " parece incorrecta"); } else { ej = ejercicioscontables.AbreEjercicio(bd, Fecha); return ej.idEjercicio; } } else { throw new Exception("El ejercicio está cerrado"); } } else { return ej.idEjercicio; } } public string DescripcionExtendida { get { if (this.FechaInicio.Month == 1 & this.FechaInicio.Day == 1 & this.FechaFin.Month == 12 & this.FechaFin.Day == 31) { return this.idEmpresaNavigation.RazonSocial + " " + this.FechaInicio.Year.ToString(); } else { return this.idEmpresaNavigation.RazonSocial + " " + this.FechaInicio.ToString() + " " + this.FechaFin.ToString(); } } } public bdGrupoSanchoToro.db.asientos AsientoApertura { get { return this.asientos.FirstOrDefault(x => x.Tipo == (int)bdGrupoSanchoToro.db.asientos.TipoAsiento.APERTURA); } } public bdGrupoSanchoToro.db.asientos AsientoRegularizacion { get { return this.asientos.FirstOrDefault(x => x.Tipo == (int)bdGrupoSanchoToro.db.asientos.TipoAsiento.REGULARIZACION); } } // Private _CuentasPlanContable As List(Of cuentasplancontable) // Public ReadOnly Property CuentasPlanContable As List(Of cuentasplancontable) // Get // If _CuentasPlanContable Is Nothing Then // _CuentasPlanContable = Me.plancontable.gruposplancontable.SelectMany(Function(x) x.cuentasplancontable).ToList // End If // Return _CuentasPlanContable // End Get // End Property public void CopiaCuentas() { try { tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto(); var ea = bd.ejercicioscontables.OrderByDescending(x => x.FechaInicio).FirstOrDefault(x => x.FechaInicio < this.FechaInicio); if (ea is not null) { var ctas1 = ea.cuentas.Where(x => x.NumeroCuenta.Length == 1).ToList(); var ctas2 = ea.cuentas.Where(x => x.NumeroCuenta.Length == 2).ToList(); var ctas3 = ea.cuentas.Where(x => x.NumeroCuenta.Length == 3).ToList(); var ctas4 = ea.cuentas.Where(x => x.NumeroCuenta.Length == 4).ToList(); var ctas8 = ea.cuentas.Where(x => x.NumeroCuenta.Length == 8).ToList(); int Nc = 0; foreach (var cta in ctas1) Nc += this.CopiaCuenta(bd, this, cta); foreach (var cta in ctas2) Nc += this.CopiaCuenta(bd, this, cta); foreach (var cta in ctas3) Nc += this.CopiaCuenta(bd, this, cta); foreach (var cta in ctas4) Nc += this.CopiaCuenta(bd, this, cta); foreach (var cta in ctas8) Nc += this.CopiaCuenta(bd, this, cta); bd.SaveChanges(); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } } private int CopiaCuenta(tscGrupoSanchoToro bd, ejercicioscontables Ejercicio, bdGrupoSanchoToro.db.cuentas CtaAnterior) { if (!Ejercicio.cuentas.Any(X => (X.NumeroCuenta ?? "") == (CtaAnterior.NumeroCuenta ?? ""))) { var nc = new bdGrupoSanchoToro.db.cuentas(); nc.Denominacion = CtaAnterior.Denominacion; nc.EsCuentaFinal = CtaAnterior.EsCuentaFinal; nc.Mote = CtaAnterior.Mote; nc.Observaciones = CtaAnterior.Observaciones; nc.NumeroCuenta = CtaAnterior.NumeroCuenta; nc.idEjercicio = Ejercicio.idEjercicio; nc.idEmpresaAmortizacion = CtaAnterior.idEmpresaAmortizacion; nc.PresupuestoEnero = CtaAnterior.PresupuestoEnero; nc.PresupuestoFebrero = CtaAnterior.PresupuestoFebrero; nc.PresupuestoMarzo = CtaAnterior.PresupuestoMarzo; nc.PresupuestoAbril = CtaAnterior.PresupuestoAbril; nc.PresupuestoMayo = CtaAnterior.PresupuestoMayo; nc.PresupuestoJunio = CtaAnterior.PresupuestoJunio; nc.PresupuestoJulio = CtaAnterior.PresupuestoJulio; nc.PresupuestoAgosto = CtaAnterior.PresupuestoAgosto; nc.PresupuestoSeptiembre = CtaAnterior.PresupuestoSeptiembre; nc.PresupuestoOctubre = CtaAnterior.PresupuestoOctubre; nc.PresupuestoNoviembre = CtaAnterior.PresupuestoNoviembre; nc.PresupuestoDiciembre = CtaAnterior.PresupuestoDiciembre; bd.cuentas.Add(nc); return 1; } else { return 0; } } public static ejercicioscontables AbreEjercicio(tscGrupoSanchoToro bd, DateOnly Fecha) { var ej = new ejercicioscontables(); ej.FechaApertura = DateOnly.FromDateTime(DateTime.Now); ej.FechaInicio = DateOnly.FromDateTime(new DateTime(Fecha.Year, 1, 1)); ej.FechaFin = DateOnly.FromDateTime( new DateTime(Fecha.Year, 12, 31)); ej.Descripcion = Fecha.Year.ToString(); bd.ejercicioscontables.Add(ej); bd.SaveChanges(); ej.CopiaCuentas(); return ej; } public static bool CompruebaEjereciciosAbiertos(tscGrupoSanchoToro bd, List Fechas) { var ejeabiertos = bd.ejercicioscontables.Where(x => x.FechaCierre.HasValue == false).ToList(); return Fechas.All(x => ejeabiertos.Any(y => y.FechaInicio <= x & y.FechaFin >= x)); } } }