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

Public Enum eControllingDirection
    eIn = 0
    eOut = 1
    eNotSet = -1
End Enum
<DefaultClassOptions()> _
<NavigationItem("FinancialStock")> _
Public Class Controlling
    Inherits HCategory
    Private _ChartOfAccounts As ChartOfAccounts '5 ötös
    Private _ChartOfAccounts2 As ChartOfAccounts '8 nyócas
    Private _IsCOCRequired As Boolean = True 'Teljesítési igazolást igényel az adott számra teljesítéshez kötelező az aláírt nyomtatvány
    Private _COCAboveAmount As Double 'A teljesítési igazolás nyomtatása nem szükséges ezen összeg alatt
    Private _IsParent As Boolean = False
    Private _Controllingdirection As eControllingDirection ' Kontrollszám pénzmozgás iránya
    Private _IsReInvoiceReason As Boolean = False ' Továbbszámlázási jogcímként is használható?
    Private _ReInvoiceDescription As String ' Ha továbbszámlázási jogcím is akkor mi a jogcím megnevezése
    Private _TechnicalContractLimit As Double 'Adott CM-re vonatkozó összeghatár ameddig technikai szerződést enged létrehozni
    Private _Controlling1 As Controlling
    Private _Controlling2 As Controlling
    Private _Controlling3 As Controlling
    Private _Controlling4 As Controlling
    Private _Controlling5 As Controlling
    Private _IsAdvancePayment As Boolean 'Előleg, vagy foglaló kontrollszám
    Private _IsEstateStatistics As Boolean 'Ingatlan értékesítési statisztikába szűrés
    ' Nonpersistent --------------------------------------------
    Private _CollectionOfControlling As XPCollection(Of Controlling)
    Private _RequireCars As Boolean = False
    Private _RequireHRPerson As Boolean
    Private _RequirePhones As Boolean = False

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
        If session.IsObjectsLoading = False And session.IsObjectsSaving = False Then
            Me.Controllingdirection = eControllingDirection.eNotSet
        End If
    End Sub
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()
        If Me.Parent IsNot Nothing Then
            If Me.Parent.Children IsNot Nothing Then
                If Me.Parent.Children.Count = 1 Then
                    TryCast(Me.Parent, Controlling).IsParent = False
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        Dim _c As New XPCollection(Session, GetType(Controlling), CriteriaOperator.Parse("Children.Count=0 and IsParent=True"))
        Dim _Controlling As Controlling

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

        For Each _Controlling1 In _c1
            If _Controlling1.Children.Count <> 0 Then
                _Controlling1.IsParent = True
                _Controlling1.Save()
            End If
        Next
        Me.ReconfigureGroup(Nothing, Me, False)
    End Sub

    <ImmediatePostData(True), DataSourceProperty("CollectionOfControlling")>
    <NonPersistent>
    Property ParentObject As Controlling
        Get
            Return MyBase.Parent
        End Get
        Set(value As Controlling)
            MyBase.Parent = value
        End Set
    End Property
    <VisibleInListView(False), VisibleInDetailView(False), VisibleInLookupListView(False)>
    <Browsable(False)>
    <NonPersistent()>
    Private ReadOnly Property CollectionOfControlling As XPCollection(Of Controlling)
        Get
            _CollectionOfControlling = New XPCollection(Of Controlling)(Session, CriteriaOperator.Parse("Oid != ?", Me.Oid))
            Return _CollectionOfControlling
        End Get
    End Property

    <Browsable(False)>
    Property ChartOfAccounts As ChartOfAccounts
        Get
            Return (_ChartOfAccounts)
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts", _ChartOfAccounts, value)
        End Set
    End Property
    <Browsable(False)>
    Property ChartOfAccounts2 As ChartOfAccounts
        Get
            Return (_ChartOfAccounts2)
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts2", _ChartOfAccounts2, value)
        End Set
    End Property
    Property IsCOCRequired As Boolean
        Get
            Return _IsCOCRequired
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsCOCRequired", _IsCOCRequired, value)
        End Set
    End Property
    Property COCAboveAmount As Double
        Get
            Return _COCAboveAmount
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("COCAboveAmount", _COCAboveAmount, 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
    Property Controllingdirection As eControllingDirection
        Get
            Return _Controllingdirection
        End Get
        Set(ByVal value As eControllingDirection)
            SetPropertyValue("Controllingdirection", _Controllingdirection, value)
        End Set
    End Property
    Property IsReInvoiceReason As Boolean
        Get
            Return _IsReInvoiceReason
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsReInvoiceReason", _IsReInvoiceReason, value)
        End Set
    End Property
    Property ReInvoiceDescription As String
        Get
            Return _ReInvoiceDescription
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ReInvoiceDescription", _ReInvoiceDescription, value)
        End Set
    End Property
    Property TechnicalContractLimit As Double
        Get
            Return _TechnicalContractLimit
        End Get
        Set(value As Double)
            SetPropertyValue("TechnicalContractLimit", _TechnicalContractLimit, value)
        End Set
    End Property
    Property Controlling1 As Controlling
        Get
            Return _Controlling1
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling1", _Controlling1, value)
        End Set
    End Property
    Property Controlling2 As Controlling
        Get
            Return _Controlling2
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling2", _Controlling2, value)
        End Set
    End Property
    Property Controlling3 As Controlling
        Get
            Return _Controlling3
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling3", _Controlling3, value)
        End Set
    End Property
    Property Controlling4 As Controlling
        Get
            Return _Controlling4
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling4", _Controlling4, value)
        End Set
    End Property
    Property Controlling5 As Controlling
        Get
            Return _Controlling5
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling5", _Controlling5, value)
        End Set
    End Property
    Property IsAdvancePayment As Boolean
        Get
            Return _IsAdvancePayment
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsAdvancePayment", _IsAdvancePayment, value)
        End Set
    End Property
    Property IsEstateStatistics As Boolean
        Get
            Return _IsEstateStatistics
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsEstateStatistics", _IsEstateStatistics, value)
        End Set
    End Property
    Property RequireCars As Boolean
        Get
            Return _RequireCars
        End Get
        Set(value As Boolean)
            SetPropertyValue("RequireCars", _RequireCars, value)
        End Set
    End Property
    Property RequireHRPerson As Boolean
        Get
            Return _RequireHRPerson
        End Get
        Set(value As Boolean)
            SetPropertyValue("RequireHRPerson", _RequireHRPerson, value)
        End Set
    End Property
    Property RequirePhones As Boolean
        Get
            Return _RequirePhones
        End Get
        Set(value As Boolean)
            SetPropertyValue("RequirePhones", _RequirePhones, value)
        End Set
    End Property


    Sub ReconfigureGroup(_ObjectSpace As Xpo.XPObjectSpace, _Controlling As Controlling, _WithCommit As Boolean)
        Dim _Controlling5 As Controlling = Nothing
        Dim _Controlling4 As Controlling = Nothing
        Dim _Controlling3 As Controlling = Nothing
        Dim _Controlling2 As Controlling = Nothing
        Dim _Controlling1 As Controlling = Nothing

        _Controlling1 = _Controlling.Parent
        If _Controlling1 IsNot Nothing Then
            _Controlling2 = _Controlling1.Parent
            If _Controlling2 IsNot Nothing Then
                _Controlling3 = _Controlling2.Parent
                If _Controlling3 IsNot Nothing Then
                    _Controlling4 = _Controlling3.Parent
                    If _Controlling4 IsNot Nothing Then
                        _Controlling5 = _Controlling4.Parent
                    End If
                End If
            End If
        End If

        If _Controlling5 IsNot Nothing Then
            _Controlling._Controlling1 = _Controlling5
            _Controlling._Controlling2 = _Controlling4
            _Controlling._Controlling3 = _Controlling3
            _Controlling._Controlling4 = _Controlling2
            _Controlling._Controlling5 = _Controlling1
        ElseIf _Controlling4 IsNot Nothing Then
            _Controlling._Controlling1 = _Controlling4
            _Controlling._Controlling2 = _Controlling3
            _Controlling._Controlling3 = _Controlling2
            _Controlling._Controlling4 = _Controlling1
            _Controlling._Controlling5 = _Controlling
        ElseIf _Controlling3 IsNot Nothing Then
            _Controlling._Controlling1 = _Controlling3
            _Controlling._Controlling2 = _Controlling2
            _Controlling._Controlling3 = _Controlling1
            _Controlling._Controlling4 = _Controlling
            _Controlling._Controlling5 = Nothing
        ElseIf _Controlling2 IsNot Nothing Then
            _Controlling._Controlling1 = _Controlling2
            _Controlling._Controlling2 = _Controlling1
            _Controlling._Controlling3 = _Controlling
            _Controlling._Controlling4 = Nothing
            _Controlling._Controlling5 = Nothing
        ElseIf _Controlling1 IsNot Nothing Then
            _Controlling._Controlling1 = _Controlling1
            _Controlling._Controlling2 = _Controlling
            _Controlling._Controlling3 = Nothing
            _Controlling._Controlling4 = Nothing
            _Controlling._Controlling5 = Nothing
        ElseIf _Controlling1 Is Nothing Then
            _Controlling._Controlling1 = _Controlling
            _Controlling._Controlling2 = Nothing
            _Controlling._Controlling3 = Nothing
            _Controlling._Controlling4 = Nothing
            _Controlling._Controlling5 = Nothing
        End If
        _Controlling.Save()
        If _WithCommit Then _ObjectSpace.CommitChanges()
    End Sub
    Private Sub InContrlollingSet(ByVal _p_InControlling As Controlling)
        '----- Rekurzív gráf bejárás az összes gyerekelemnek a BEVÉTEL kontrollszám csoportban --------------
        _p_InControlling.Controllingdirection = eControllingDirection.eIn ' Gyökér elem beállítása
        If _p_InControlling.Children.Count = 0 Then ' Szülő elem beállítása
            _p_InControlling.Controllingdirection = eControllingDirection.eIn
        Else
            Dim _Controlling As Controlling

            For Each _Controlling In _p_InControlling.Children
                _Controlling.Controllingdirection = eControllingDirection.eIn ' Levél elemek beállítása
                If _Controlling.Children.Count <> 0 Then ' A rekurzió megakasztása
                    InContrlollingSet(_Controlling)
                End If
            Next
        End If
        _p_InControlling.Save()
    End Sub
    Private Sub OutContrlollingSet(ByVal _p_OutControlling As Controlling)
        '----- Rekurzív gráf bejárás az összes gyerekelemnek a KIADÁS kontrollszám csoportban --------------
        _p_OutControlling.Controllingdirection = eControllingDirection.eOut ' Gyökér elem beállítása
        If _p_OutControlling.Children.Count = 0 Then ' Szülő elem beállítása
            _p_OutControlling.Controllingdirection = eControllingDirection.eOut
        Else
            Dim _Controlling As Controlling

            For Each _Controlling In _p_OutControlling.Children
                _Controlling.Controllingdirection = eControllingDirection.eOut ' Levél elemek beállítása
                If _Controlling.Children.Count <> 0 Then ' A rekurzió megakasztása
                    OutContrlollingSet(_Controlling)
                End If
            Next
        End If
        _p_OutControlling.Save()
    End Sub

    <Association("Controlling-ControllingYear", GetType(ControllingYear)), VisibleInListView(False), VisibleInLookupListView(False)>
    ReadOnly Property ControllingYear As XPCollection(Of ControllingYear)
        Get
            Return GetCollection(Of ControllingYear)("ControllingYear")
        End Get
    End Property
    Public Function GetChartOfAccounts(_AccountYear As Long, _IsReinvoice As Boolean) As ChartOfAccounts
        Dim _ChartOfAccounts As ChartOfAccounts = Nothing
        Dim _ControllingYear As ControllingYear
        Select Case _IsReinvoice
            Case False
                _ChartOfAccounts = Me.ChartOfAccounts
                For Each _ControllingYear In Me.ControllingYear
                    If _ControllingYear.AccountYear = _AccountYear Then
                        _ChartOfAccounts = _ControllingYear.ChartOfAccounts
                        Exit For
                    End If
                Next
            Case True
                _ChartOfAccounts = Me.ChartOfAccounts2
                For Each _ControllingYear In Me.ControllingYear
                    If _ControllingYear.AccountYear = _AccountYear Then
                        _ChartOfAccounts = _ControllingYear.ChartOfAccounts2
                        Exit For
                    End If
                Next
        End Select
        Return _ChartOfAccounts
    End Function

End Class 'Kontroll számok
<DefaultClassOptions(), NavigationItem(False), CreatableItem(False)>
<RuleCriteria("RuleCriteria ControllingYear-ChartOfAccounts", DefaultContexts.Save, "ChartOfAccounts.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)>
<RuleCriteria("RuleCriteria ControllingYear-ChartOfAccounts2", DefaultContexts.Save, "ChartOfAccounts2.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)>
Public Class ControllingYear
    Inherits BaseObject

    Private _Controlling As Controlling
    Private _AccountYear As Long
    Private _ChartOfAccounts As ChartOfAccounts
    Private _ChartOfAccounts2 As ChartOfAccounts
    Public Sub New(ByVal session As Session)
        MyBase.New(session)

    End Sub

    <Association("Controlling-ControllingYear", GetType(Controlling))>
    Property Controlling As Controlling
        Get
            Return _Controlling
        End Get
        Set(value As Controlling)
            SetPropertyValue("Controlling", _Controlling, value)
        End Set
    End Property
    Property AccountYear As Long
        Get
            Return _AccountYear
        End Get
        Set(value As Long)
            SetPropertyValue("AccountYear", _AccountYear, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceCriteria("AccountYear='@This.AccountYear'")>
    Property ChartOfAccounts As ChartOfAccounts
        Get
            Return _ChartOfAccounts
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts", _ChartOfAccounts, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceCriteria("AccountYear='@This.AccountYear'")>
    Property ChartOfAccounts2 As ChartOfAccounts
        Get
            Return _ChartOfAccounts2
        End Get
        Set(value As ChartOfAccounts)
            SetPropertyValue("ChartOfAccounts2", _ChartOfAccounts2, value)
        End Set
    End Property
End Class
