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

Public Class GeneralLedgerVC
	Inherits DevExpress.ExpressApp.ViewController
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        'Me.Activate_GL_Accounting()
        Frame.GetController(Of GeneralLedgerVC).aToTable.Active.SetItemValue("", False) 'Folyószámla táblázatba akciója
        Frame.GetController(Of GeneralLedgerVC).aFinalizeAll.Active.SetItemValue("", False) 'Fólyószámla véglegesítési akciója
        Frame.GetController(Of GeneralLedgerVC).aStorno.Active.SetItemValue("", False) 'Folyószámla stornó akciója
        Frame.GetController(Of GeneralLedgerVC).aGLStorno.Active.SetItemValue("", False) 'Főkönyv stornó akciója
        Frame.GetController(Of GeneralLedgerVC).aGLFinal.Active.SetItemValue("", False) 'Főkönyv véglegesítési akciója
        Frame.GetController(Of GeneralLedgerVC).aGLToTable.Active.SetItemValue("", False) 'Főkönyv táblázatba akciója
        Frame.GetController(Of GeneralLedgerVC).aGLFinalAndNew.Active.SetItemValue("", False) 'Főkönyv véglegesítési és új akciója
        Frame.GetController(Of GeneralLedgerVC).aFinalizeAllAndNew.Active.SetItemValue("", False) 'Folyószámla véglegesítés és új akciója
        Frame.GetController(Of GeneralLedgerVC).MakeEditable.Active.SetItemValue("", False)

        If View.Id = "GLHeader_ListView" And SecuritySystemHelper.IsUserInRoleCurrentUser("GLMaintener") Then
            Frame.GetController(Of GeneralLedgerVC).MakeEditable.Active.SetItemValue("", True)
        End If

        'Folyószámla - NestedListView controll-ok letiltása, asszociáció létrehozása csak az itt megjelenített akcióval
        If View.Id Like "GLAccountsHeader_GLAccountsRows_ListView" Then

            Frame.GetController(Of GeneralLedgerVC).aToTable.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)

            Dim nv As ListView = CType(View, ListView)
            Dim cs As PropertyCollectionSource = CType(nv.CollectionSource, PropertyCollectionSource)
            AddHandler cs.MasterObjectChanged, AddressOf cs_MasterObjectChanged

        End If

        'Főkönyv - NestedListView controll-ok letiltása, asszociáció létrehozása csak az itt megjelenített akcióval
        If View.Id Like "GLHeader_GLRows_ListView" Then

            Frame.GetController(Of GeneralLedgerVC).aGLToTable.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)

            Dim GLnv As ListView = CType(View, ListView)
            Dim GLcs As PropertyCollectionSource = CType(GLnv.CollectionSource, PropertyCollectionSource)
            AddHandler GLcs.MasterObjectChanged, AddressOf GLcs_MasterObjectChanged

        End If

        'Folyószámla - egyedi akciók megjelenítése DetailView-ban, majd Editable vizsgálata alapján letiltja a szerkesztő controllereket
        If View.Id = "GLAccountsHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of GeneralLedgerVC).aFinalizeAll.Active.SetItemValue("", True)
            Frame.GetController(Of GeneralLedgerVC).aStorno.Active.SetItemValue("", True)
            Frame.GetController(Of GeneralLedgerVC).aFinalizeAllAndNew.Active.SetItemValue("", True)
        End If

        'Főkönyv - egyedi akciók megjelenítése DetailView-ban, majd Editable vizsgálata alapján letiltja a szerkesztő controllereket
        If View.Id = "GLHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of GeneralLedgerVC).aGLFinal.Active.SetItemValue("", True)
            Frame.GetController(Of GeneralLedgerVC).aGLStorno.Active.SetItemValue("", True)
            Frame.GetController(Of GeneralLedgerVC).aGLFinalAndNew.Active.SetItemValue("", True)
            Dim _GLHeader As GLHeader = TryCast(View.CurrentObject, GLHeader)
            If _GLHeader Is Nothing Then
            Else
                If _GLHeader.IsEditable = False Then
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of GeneralLedgerVC).aGLFinal.Active.SetItemValue("", False)
                    Frame.GetController(Of GeneralLedgerVC).aGLFinalAndNew.Active.SetItemValue("", False)

                Else
                    Frame.GetController(Of GeneralLedgerVC).aGLStorno.Active.SetItemValue("", False)
                End If
                If _GLHeader.IsStorno = True Then
                    Frame.GetController(Of GeneralLedgerVC).aGLStorno.Active.SetItemValue("", False)
                    Frame.GetController(Of GeneralLedgerVC).aGLFinal.Active.SetItemValue("", False)
                End If
            End If
            If SecuritySystemHelper.IsUserInRoleCurrentUser("GLMaintener") Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", True)
                Frame.GetController(Of GeneralLedgerVC).MakeEditable.Active.SetItemValue("", True)
            End If
        End If

    End Sub
    'Folyószámla MasterObject megkeresése a NestedListView controlljából
    Private Sub cs_MasterObjectChanged(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    'Főkönyv MasterObject megkeresése a NestedListView controlljából
    Private Sub GLcs_MasterObjectChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim _GLHeader As GLHeader = TryCast((CType(sender, PropertyCollectionSource)).MasterObject, GLHeader)
        If _GLHeader.IsEditable = False Then
            'DisableActions()
            Frame.GetController(Of GeneralLedgerVC).aGLToTable.Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub aToTable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable.Execute

    End Sub
    Private Sub aStorno_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStorno.Execute
    End Sub
    Private Sub aFinalizeAll_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeAll.Execute, aFinalizeAll.Execute
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
    End Sub
    Private Sub aGLFinal_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLFinal.Execute
        sis_aGLFinal(e)
        View.Close(False)
    End Sub
    Private Sub aGLToTable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLToTable.Execute
    End Sub
    Private Sub aGLStorno_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLStorno.Execute
    End Sub
    Private Sub aGLFinalAndNew_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLFinalAndNew.Execute
        If View.Id = "GLHeader_DetailView" Then
            sis_aGLFinal(e)
            Dim objectSpaceInternal As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim obj As GLHeader = objectSpaceInternal.CreateObject(Of GLHeader)()
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(objectSpaceInternal, obj)
            e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow
        End If
    End Sub
#Region "SIS-Rendszerhaz"
    Private Sub sis_aGLFinal(ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        If View Is Nothing Then Exit Sub
        If TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.IsObjectsLoading = False And TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.IsObjectsSaving = False Then
            Dim _GLHeader As GLHeader

            For Each _GLHeader In e.SelectedObjects
                _GLHeader.IsEditable = False
                _GLHeader.Save()
            Next
            TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.CommitTransaction()
            'View.Close(False)
        End If

    End Sub
#End Region

    Private Sub MakeEditable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles MakeEditable.Execute
        Dim _GLHeader As GLHeader
        For Each _GLHeader In e.SelectedObjects
            _GLHeader.IsEditable = True
            _GLHeader.Save()
            TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.CommitTransaction()
            If View.Id = "GLHeader_DetailView" Then
                View.Close(False)
            End If
        Next
    End Sub
End Class