Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Public Class ControllingVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "HCategory_Children_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Controlling_LookupListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "HCategory_Children_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "Controlling_LookupListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub

    Private Sub aReCreateControlling_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aReCreateControlling.Execute

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Controlling_Collection As New XPCollection(Of Controlling)(_ocur.Session)
        Dim _Controlling As Controlling

        RaiseEvent InitWork(1, 1, _Controlling_Collection.Count)
        For Each _Controlling In _Controlling_Collection
            RaiseEvent DoWork()
            _Controlling.ReconfigureGroup(_ocur, _Controlling, True)
        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)

    End Sub

    Private Sub aGenerateControllingfromChartOfAccounts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateControllingfromChartOfAccounts.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _ChartOfAccounts_Collection As New XPCollection(Of ChartOfAccounts)(_onew.Session, CriteriaOperator.Parse("AccountYear=?", Now.Date.Year))
        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _Controlling As Controlling
        Dim _Controlling_Parent As Controlling = Nothing
        Dim _ControllingYear As ControllingYear

        _ChartOfAccounts_Collection.Sorting.Add(New SortProperty("AccountNumber", DB.SortingDirection.Ascending))
        RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)
        For Each _ChartOfAccounts In _ChartOfAccounts_Collection
            RaiseEvent DoWork()
            _Controlling = _onew.FindObject(Of Controlling)(CriteriaOperator.Parse("Name=?", _ChartOfAccounts.Name))
            If _Controlling Is Nothing Then
                _Controlling = _onew.CreateObject(Of Controlling)()
            End If
            _Controlling.Name = _ChartOfAccounts.Name
            _Controlling_Parent = Nothing
            If _ChartOfAccounts.Parent IsNot Nothing Then
                _Controlling_Parent = _onew.FindObject(Of Controlling)(CriteriaOperator.Parse("Name=?", _ChartOfAccounts.Parent.Name))
            End If
            If _Controlling_Parent IsNot Nothing Then
                _Controlling.Parent = _Controlling_Parent
            End If
            _onew.CommitChanges()
            Dim _ControllingYear_Collection = New XPCollection(Of ControllingYear)(_onew.Session, CriteriaOperator.Parse("Controlling=?", _Controlling))
            If _ControllingYear_Collection.Count <= 0 Then
                _ControllingYear = _onew.CreateObject(Of ControllingYear)()
                _ControllingYear.Controlling = _Controlling
                _ControllingYear.AccountYear = Now.Date.Year
                _ControllingYear.ChartOfAccounts = _ChartOfAccounts
                _ControllingYear.ChartOfAccounts2 = _ChartOfAccounts
            End If

            Select Case Mid(_ChartOfAccounts.AccountNumber, 1, 1)
                Case "5", "8"
                    _Controlling.Controllingdirection = eControllingDirection.eOut
                Case "9"
                    _Controlling.Controllingdirection = eControllingDirection.eIn
                Case Else
                    _Controlling.Controllingdirection = eControllingDirection.eNotSet
            End Select
            _onew.CommitChanges()
        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork
    End Sub
End Class
