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)> _
<Appearance("Disable UserCreated,ObjectCreated-GLCardsHeader", AppearanceItemType:="ViewItem", Enabled:=False, TargetItems:="ObjectCreated,UserCreated", Criteria:="1=1")> _
<RuleCriteria("RuleCriteria GLCardsHeader-ChartOfAccounts", DevExpress.Persistent.Validation.DefaultContexts.Save, "ChartOfAccount.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)> _
Public Class GLCardsHeader
    Inherits BaseObject
    Private _ShortName As String 'Lekérdezés rövid megnevezése
    Private _DateFrom As Date
    Private _DateTo As Date
    Private _CustomersFrom As CustomersFrom
    Private _ChartOfAccounts As ChartOfAccounts
    Private _IsOpen As Boolean = True
    Private _IsClose As Boolean = False
    
    Private _ObjectCreated As Date
    Private _UserCreated As User
    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
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()
        Session.Delete(Me.GLCardsRows)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        DateFrom = CDate(Year(Now) & "/01/01")
        DateTo = Now
    End Sub
    <RuleRequiredField("GLCardsHeader-ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <RuleRequiredField("GLCardsHeader-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("GLCardsHeader-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("GLCardsHeader-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("GLCardsHeader-ChartOfAccount", DefaultContexts.Save)> _
    <DataSourceCriteria("AccountYear=GetYear('@This.DateFrom')")> _
    Property ChartOfAccount() As ChartOfAccounts
        Get
            Return _ChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccount", _ChartOfAccounts, value)
        End Set
    End Property
    Property IsOpen As Boolean
        Get
            Return _IsOpen
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsOpen", _IsOpen, value)
        End Set
    End Property
    Property IsClose As Boolean
        Get
            Return _IsClose
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsClose", _IsClose, value)
        End Set
    End Property
    '-- XP collections
    <Association("GLCardsHeader-GLCardsRows", GetType(GLCardsRows)), VisibleInListView(False)> _
    ReadOnly Property GLCardsRows() As XPCollection(Of GLCardsRows)
        Get
            Return GetCollection(Of GLCardsRows)("GLCardsRows")
        End Get
    End Property
    <Association("GLCardsHeader-GLCardsRowsPCI", GetType(GLCardsRowsPCI)), VisibleInListView(False)> _
    ReadOnly Property GLCardsRowsPCI() As XPCollection(Of GLCardsRowsPCI)
        Get
            Return GetCollection(Of GLCardsRowsPCI)("GLCardsRowsPCI")
        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), VisibleInLookupListView(False)> _
    ReadOnly Property GLCardsRowsPivot As XPCollection(Of GLCardsRows)
        Get
            Return New XPCollection(Of GLCardsRows)(Session, CriteriaOperator.Parse("GLCardsHeader=?", Me))
        End Get
    End Property

    ReadOnly Property ABRQueryRows As XPCollection(Of ABRQueryRows)
        Get
            Return New XPCollection(Of ABRQueryRows)(Session, CriteriaOperator.Parse("GLCardsHeader=?", Me))
        End Get
    End Property
    ReadOnly Property ABRQueryRowsDetail As XPCollection(Of ABRQueryRowsDetail)
        Get
            Return New XPCollection(Of ABRQueryRowsDetail)(Session, CriteriaOperator.Parse("GLCardsHeader=?", Me))
        End Get
    End Property

    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property IsCustomersGLParameters As Boolean
        Get
            If Me.ChartOfAccount Is Nothing Then
                Return False
                Exit Property
            Else
                Dim _IsC As Boolean = False
                Dim _CustomersGLParametersYear_Collection As New XPCollection(Of CustomersGLParametersYear)(Session)
                Dim _CustomersGLParametersYear As CustomersGLParametersYear

                For Each _CustomersGLParametersYear In _CustomersGLParametersYear_Collection
                    If _CustomersGLParametersYear.ChartOfAccountsIn IsNot Nothing Then
                        If _CustomersGLParametersYear.ChartOfAccountsIn.Oid = Me.ChartOfAccount.Oid Then
                            Return True
                            Exit Property
                        End If
                    End If
                    If _CustomersGLParametersYear.ChartOfAccountsOut IsNot Nothing Then
                        If _CustomersGLParametersYear.ChartOfAccountsOut.Oid = Me.ChartOfAccount.Oid Then
                            Return True
                            Exit Property
                        End If
                    End If
                    If _CustomersGLParametersYear.ChartOfAccountsNo IsNot Nothing Then
                        If _CustomersGLParametersYear.ChartOfAccountsNo.Oid = Me.ChartOfAccount.Oid Then
                            Return True
                            Exit Property
                        End If
                    End If
                Next
            End If
        End Get
    End Property
    '-- Szűrők
    <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


    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

