Imports System
Imports System.ComponentModel
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.Base.General
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.ExpressApp.ConditionalAppearance

<DefaultClassOptions()> _
<NavigationItem("FinancialTransactions")> _
<DeferredDeletion(False)> _
Public Class TrialBalanceHeader
    Inherits BaseObject
    Private _DateFrom As Date 'Dátumtól
    Private _DateTo As Date 'Dátumig
    Private _CustomersFrom As CustomersFrom 'Saját cég
    Private _ShortName As String 'Lekérdezés rövid megnevezése
    Private _QueryLevel As Long = 10
    Private _ObjectCreated As Date
    Private _UserCreated As User
    Private _GLReportDateExecutionMode As eDateExecutionMode = eDateExecutionMode.eHeader
    Private _IsGLOpenClose As Boolean = False ' Csak a főkönyvi nyitás és zárásnál látszik
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.DateFrom = FirstDayOfYear(GetSQLTime(Session))
        Me.DateTo = LastDayOfYear(GetSQLTime(Session))
    End Sub
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()
        Session.Delete(Me.TrialBalance)
        Session.Delete(Me.TrialBalanceOpenClose)
        Session.Delete(Me.TrialBalanceSum)
    End Sub

    <RuleRequiredField("TrialBalanceHeader-DateFrom", DefaultContexts.Save)> _
    Property DateFrom() As Date
        Get
            Return _DateFrom
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateFrom", _DateFrom, value)
        End Set
    End Property
    <RuleRequiredField("TrialBalanceHeader-DateTo", DefaultContexts.Save)> _
    Property DateTo() As Date
        Get
            Return _DateTo
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateTo", _DateTo, value)
        End Set
    End Property
    <RuleRequiredField("TrialBalanceHeader-CustomersFrom", DefaultContexts.Save)> _
    Property CustomersFrom() As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(ByVal value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <RuleRequiredField("TrialBalanceHeader-ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    Property QueryLevel As Long
        Get
            Return _QueryLevel
        End Get
        Set(value As Long)
            SetPropertyValue("QueryLevel", _QueryLevel, value)
        End Set
    End Property
    <NonCloneable> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    Property GLReportDateExecutionMode As eDateExecutionMode
        Get
            Return _GLReportDateExecutionMode
        End Get
        Set(value As eDateExecutionMode)
            SetPropertyValue("GLReportDateExecutionMode", _GLReportDateExecutionMode, value)
        End Set
    End Property

    '//XPCollections

    <Association("TrialBalanceHeader-TrialBalance", GetType(TrialBalance)), VisibleInListView(False)> _
    ReadOnly Property TrialBalance() As XPCollection(Of TrialBalance)
        Get
            Return GetCollection(Of TrialBalance)("TrialBalance")
        End Get
    End Property
    <Association("TrialBalanceHeader-TrialBalanceOpenClose", GetType(TrialBalanceOpenClose)), VisibleInListView(False)> _
    ReadOnly Property TrialBalanceOpenClose() As XPCollection(Of TrialBalanceOpenClose)
        Get
            Return GetCollection(Of TrialBalanceOpenClose)("TrialBalanceOpenClose")
        End Get
    End Property
    <Association("TrialBalanceHeader-TrialBalanceSum", GetType(TrialBalanceSum)), VisibleInListView(False)> _
    ReadOnly Property TrialBalanceSum() As XPCollection(Of TrialBalanceSum)
        Get
            Return GetCollection(Of TrialBalanceSum)("TrialBalanceSum")
        End Get
    End Property
    <Association("TrialBalanceHeader-TrialBalanceJobNumberObjects", GetType(TrialBalanceJobNumberObjects)), VisibleInListView(False)> _
    ReadOnly Property TrialBalanceJobNumberObjects As XPCollection(Of TrialBalanceJobNumberObjects)
        Get
            Return GetCollection(Of TrialBalanceJobNumberObjects)("TrialBalanceJobNumberObjects")
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property GLRowsStatictics As XPCollection(Of GLRowsStatistics)
        Get
            If Session.IsNewObject(Me) Then
                Return Nothing
            Else
                Return New XPCollection(Of GLRowsStatistics)(Session, CriteriaOperator.Parse("MasterKey=?", Me.Oid))
            End If
        End Get
    End Property
    <VisibleInListView(False), VisibleInDetailView(False), VisibleInLookupListView(False)> _
    Property IsGLOpenClose As Boolean
        Get
            Return _IsGLOpenClose
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsGLOpenClose", _IsGLOpenClose, value)
        End Set
    End Property
    Public Sub CreateGLRowsStatictics(_uow As UnitOfWork, _DateFrom As Date, _DateTo As Date, _CustomersFrom As CustomersFrom, _MasterKey As Guid)
        '// Előkészít egy GLRowsStatictics XPCollection azokra a tételekre, amelyek nincsenek feladva ezen időszakra !
        Dim _GLRowsStatistics As GLRowsStatistics
        Dim _GLRowsStatistics_Collection As New XPCollection(Of GLRowsStatistics)(_uow, CriteriaOperator.Parse("MasterKey=?", _MasterKey))
        _uow.Delete(_GLRowsStatistics_Collection)

        Dim _RegistryHeader_Collection As New XPCollection(Of RegistryHeader)(_uow, CriteriaOperator.Parse("CustomersFrom.Oid = ? And GetDate(F_DateExecution) Between(?, ?) And RegistryType.RegistryMainType = 'eAccount' And F_GLAccounts Is Null And IsStorno = False", _CustomersFrom.Oid, _DateFrom.Date, _DateTo.Date))
        Dim _RegistryHeader As RegistryHeader

        For Each _RegistryHeader In _RegistryHeader_Collection
            _GLRowsStatistics = New GLRowsStatistics(_uow)
            _GLRowsStatistics.MasterKey = _MasterKey
            _GLRowsStatistics.RegistryHeader = _RegistryHeader
        Next

        Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(_uow, CriteriaOperator.Parse("InvoiceType.InvoiceTypeBase <> 'eCustomRate' And getdate(DateExecution) Between(?, ?) And CustomersFrom.Oid = ? And GLAccounts Is Null", _DateFrom.Date, _DateTo.Date, _CustomersFrom.Oid))
        Dim _InvoiceHeader As InvoiceHeader

        For Each _InvoiceHeader In _InvoiceHeader_Collection
            _GLRowsStatistics = New GLRowsStatistics(_uow)
            _GLRowsStatistics.MasterKey = _MasterKey
            _GLRowsStatistics.InvoiceHeader = _InvoiceHeader
        Next
        _uow.CommitChanges()

    End Sub
End Class

