Imports tsl5.Extensiones Partial Public Class ejercicioscontables Public Shared Function ObtieneidEjercicioAbierto(Fecha As Date) As Integer Dim bd = bdGestionAsegasa.gestionasegasaEntities.NuevoContexto Dim ej = bd.ejercicioscontables.FirstOrDefault(Function(x) x.FechaInicio <= Fecha And x.FechaFin >= Fecha And x.FechaCierre.HasValue = False) If ej Is Nothing Then ej = bd.ejercicioscontables.FirstOrDefault(Function(x) x.FechaInicio <= Fecha And x.FechaFin >= Fecha And x.FechaCierre.HasValue) If ej Is Nothing Then If Today.Subtract(Fecha).TotalDays > 365 OrElse Fecha.Subtract(Today).TotalDays > 60 Then Throw New Exception("La fecha del ejercicio " & Fecha.ToShortDateString & " parece incorrecta") Else ej = bdGestionAsegasa.ejercicioscontables.AbreEjercicio(bd, Fecha) Return ej.idEjercicio End If Else Throw New Exception("El ejercicio está cerrado") End If Else Return ej.idEjercicio End If End Function Public ReadOnly Property DescripcionExtendida As String Get If Me.FechaInicio.Month = 1 And Me.FechaInicio.Day = 1 And Me.FechaFin.Month = 12 And Me.FechaFin.Day = 31 Then Return Me.empresascontables.RazonSocial & " " & Me.FechaInicio.Year.ToString Else Return Me.empresascontables.RazonSocial & " " & Me.FechaInicio.ToString & " " & Me.FechaFin.ToString End If End Get End Property Public ReadOnly Property AsientoApertura As asientos Get Return Me.asientos.FirstOrDefault(Function(x) x.Tipo = bdGestionAsegasa.asientos.TipoAsiento.APERTURA) End Get End Property Public ReadOnly Property AsientoRegularizacion As asientos Get Return Me.asientos.FirstOrDefault(Function(x) x.Tipo = bdGestionAsegasa.asientos.TipoAsiento.REGULARIZACION) End Get End Property '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 Sub CopiaCuentas() Try Dim bd As gestionasegasaEntities = Me.ObtieneContexto Dim ea = bd.ejercicioscontables.OrderByDescending(Function(x) x.FechaInicio).FirstOrDefault(Function(x) x.FechaInicio < Me.FechaInicio) If ea IsNot Nothing Then Dim ctas1 = ea.cuentas.Where(Function(x) x.NumeroCuenta.Length = 1).ToList Dim ctas2 = ea.cuentas.Where(Function(x) x.NumeroCuenta.Length = 2).ToList Dim ctas3 = ea.cuentas.Where(Function(x) x.NumeroCuenta.Length = 3).ToList Dim ctas4 = ea.cuentas.Where(Function(x) x.NumeroCuenta.Length = 4).ToList Dim ctas8 = ea.cuentas.Where(Function(x) x.NumeroCuenta.Length = 8).ToList Dim Nc As Integer = 0 For Each cta In ctas1 Nc += CopiaCuenta(bd, Me, cta) Next For Each cta In ctas2 Nc += CopiaCuenta(bd, Me, cta) Next For Each cta In ctas3 Nc += CopiaCuenta(bd, Me, cta) Next For Each cta In ctas4 Nc += CopiaCuenta(bd, Me, cta) Next For Each cta In ctas8 Nc += CopiaCuenta(bd, Me, cta) Next bd.SaveChanges() End If Catch ex As Exception Throw New Exception(ex.Message, ex) End Try End Sub Private Function CopiaCuenta(bd As gestionasegasaEntities, Ejercicio As ejercicioscontables, CtaAnterior As cuentas) As Integer If Not Ejercicio.cuentas.Any(Function(X) X.NumeroCuenta = CtaAnterior.NumeroCuenta) Then Dim nc As New 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.AddTocuentas(nc) Return 1 Else Return 0 End If End Function Public Shared Function AbreEjercicio(bd As gestionasegasaEntities, Fecha As Date) As ejercicioscontables Dim ej As New ejercicioscontables() ej.FechaApertura = Now ej.FechaInicio = New Date(Fecha.Year, 1, 1) ej.FechaFin = New Date(Fecha.Year, 12, 31) ej.Descripcion = Fecha.Year.ToString bd.ejercicioscontables.AddObject(ej) bd.SaveChanges() ej.CopiaCuentas() Return ej End Function Public Shared Function CompruebaEjereciciosAbiertos(bd As gestionasegasaEntities, Fechas As List(Of Date)) As Boolean Dim ejeabiertos = bd.ejercicioscontables.Where(Function(x) x.FechaCierre.HasValue = False).ToList Return Fechas.All(Function(x) ejeabiertos.Any(Function(y) y.FechaInicio <= x And y.FechaFin >= x)) End Function End Class