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.ExpressApp.ConditionalAppearance

<DefaultClassOptions()> _
<NavigationItem("FinancialStock")> _
<Appearance("ChartOfAccounts 1", AppearanceItemType:="ViewItem", criteria:="AccountNumber in ('1','2','3','4','5','8','9')", Fontstyle:=Drawing.FontStyle.Bold, targetitems:="*", context:="*")> _
Public Class ChartOfAccounts
    Inherits HCategory
    Private _AccountYear As Long
    Private _AccountNumber As String
    Private _IsParent As Boolean = False
    Private _ChartOfAccounts1 As ChartOfAccounts
    Private _ChartOfAccounts2 As ChartOfAccounts
    Private _ChartOfAccounts3 As ChartOfAccounts
    Private _ChartOfAccounts4 As ChartOfAccounts
    Private _ChartOfAccounts5 As ChartOfAccounts
    Private _ChartOfAccounts6 As ChartOfAccounts
    Private _ChartOfAccounts7 As ChartOfAccounts
    Private _ChartOfAccounts8 As ChartOfAccounts

    Private _ChartOfAccounts_Parent As ChartOfAccounts
    Private _RecordCount As Long = 0 'Az erre a számlára könyvelt tételek száma

    Private _DebitAmountHUF As Double
    Private _CreditAmountHUF As Double

    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()
        If Me.Parent IsNot Nothing Then
            If Me.Parent.Children.Count = 1 Then
                TryCast(Me.Parent, ChartOfAccounts).IsParent = False
            End If
        End If
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If IsDeleted = False Then

            Dim _c As New XPCollection(PersistentCriteriaEvaluationBehavior.InTransaction, Session, GetType(ChartOfAccounts), CriteriaOperator.Parse("Children.Count=0 and IsParent=True"))
            Dim _ChartOfAccounts As ChartOfAccounts

            For Each _ChartOfAccounts In _c
                If _ChartOfAccounts.Children.Count = 0 Then
                    _ChartOfAccounts.IsParent = False
                    _ChartOfAccounts.Save()
                End If
            Next
            Dim _c1 As New XPCollection(PersistentCriteriaEvaluationBehavior.InTransaction, Session, GetType(ChartOfAccounts), CriteriaOperator.Parse("Children.Count<>0 and IsParent=False"))
            Dim _ChartOfAccountsX As ChartOfAccounts

            For Each _ChartOfAccountsX In _c1
                If _ChartOfAccountsX.Children.Count <> 0 Then
                    _ChartOfAccountsX.IsParent = True
                    _ChartOfAccountsX.Save()
                End If
            Next

            ' -- A szülő számlák begyűjtése a gyökérig
            Me.ChartOfAccounts1 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 1), Me.AccountYear))
            Me.ChartOfAccounts2 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 2), Me.AccountYear))
            Me.ChartOfAccounts3 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 3), Me.AccountYear))
            Me.ChartOfAccounts4 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 4), Me.AccountYear))
            Me.ChartOfAccounts5 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 5), Me.AccountYear))
            Me.ChartOfAccounts6 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 6), Me.AccountYear))
            Me.ChartOfAccounts7 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 7), Me.AccountYear))
            Me.ChartOfAccounts8 = Session.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(Me.AccountNumber, 1, 8), Me.AccountYear))
        End If
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        AccountYear = Year(Now)
    End Sub
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub

    <RuleRequiredField("ChartOfAccounts-AccountYear", DefaultContexts.Save)> _
    Property AccountYear As Long
        Get
            Return _AccountYear
        End Get
        Set(value As Long)
            SetPropertyValue("AccountYear", _AccountYear, value)
        End Set
    End Property
    Property AccountNumber() As String
        Get
            'If Session.IsNewObject(Me) And Not Session.IsObjectsSaving And Me.Name IsNot Nothing Then
            If Not Session.IsObjectsSaving Then
                If Name IsNot Nothing Then
                    _AccountNumber = Me.Name.Split(" ")(0)
                End If
            End If
            Return _AccountNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("AccountNumber", _AccountNumber, value)
        End Set
    End Property
    Property IsParent() As Boolean
        Get
            Return _IsParent
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsParent", _IsParent, value)
        End Set
    End Property
    <DataSourceCriteria("AccountYear='@This.AccountYear'")> _
    Property ChartOfAccounts_Parent As ChartOfAccounts
        Get
            Return TryCast(Me.Parent, ChartOfAccounts)
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts_Parent", _ChartOfAccounts_Parent, value)
            Me.Parent = TryCast(value, HCategory)
        End Set
    End Property

    Property ChartOfAccounts1 As ChartOfAccounts
        Get
            Return _ChartOfAccounts1
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts1", _ChartOfAccounts1, value)
        End Set
    End Property
    Property ChartOfAccounts2 As ChartOfAccounts
        Get
            Return _ChartOfAccounts2
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts2", _ChartOfAccounts2, value)
        End Set
    End Property
    Property ChartOfAccounts3 As ChartOfAccounts
        Get
            Return _ChartOfAccounts3
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts3", _ChartOfAccounts3, value)
        End Set
    End Property
    Property ChartOfAccounts4 As ChartOfAccounts
        Get
            Return _ChartOfAccounts4
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts4", _ChartOfAccounts4, value)
        End Set
    End Property
    Property ChartOfAccounts5 As ChartOfAccounts
        Get
            Return _ChartOfAccounts5
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts5", _ChartOfAccounts5, value)
        End Set
    End Property
    Property ChartOfAccounts6 As ChartOfAccounts
        Get
            Return _ChartOfAccounts6
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts6", _ChartOfAccounts6, value)
        End Set
    End Property
    Property ChartOfAccounts7 As ChartOfAccounts
        Get
            Return _ChartOfAccounts7
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts7", _ChartOfAccounts7, value)
        End Set
    End Property
    Property ChartOfAccounts8 As ChartOfAccounts
        Get
            Return _ChartOfAccounts8
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts8", _ChartOfAccounts8, value)
        End Set
    End Property

    Property RecordCount As Long
        Get
            Return _RecordCount
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("RecordCount", _RecordCount, value)
        End Set
    End Property

    Sub Reconfigure_ChartOfAccounts(ByVal _ObjectSpace As Xpo.XPObjectSpace) 'Az eljárás beállítja a szülő paramétereket
        Dim _ChartOfAccounts_Collection As New XPCollection(Of ChartOfAccounts)(_ObjectSpace.Session, CriteriaOperator.Parse(""))
        Dim _COA As ChartOfAccounts

        For Each _COA In _ChartOfAccounts_Collection
            _COA.ChartOfAccounts1 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 1), _COA.AccountYear))
            _COA.ChartOfAccounts2 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 2), _COA.AccountYear))
            _COA.ChartOfAccounts3 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 3), _COA.AccountYear))
            _COA.ChartOfAccounts4 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 4), _COA.AccountYear))
            _COA.ChartOfAccounts5 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 5), _COA.AccountYear))
            _COA.ChartOfAccounts6 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 6), _COA.AccountYear))
            _COA.ChartOfAccounts7 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 7), _COA.AccountYear))
            _COA.ChartOfAccounts8 = _ObjectSpace.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", Mid(_COA.AccountNumber, 1, 8), _COA.AccountYear))
            _COA.Save()
        Next
        _ObjectSpace.CommitChanges()
    End Sub

End Class 'Főkönyvi számlaszámok

