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

Imports DevExpress.Xpo
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Filtering
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports SISBusiness.Module.RegistryVC
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation

Public Class BookEntryVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected selection As ArrayList
    Protected noselect As Boolean = False
    Public Sub New()
        MyBase.New()
        selection = New ArrayList()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", False)
        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsDivide.Active.SetItemValue("", False)

        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", False)

        Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aGLAccountsReadyforBookEntry.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aVATDivide.Active.SetItemValue("", False)
        Frame.GetController(Of BookEntryVC).aVATMerge.Active.SetItemValue("", False)
        If View.Id = "GLAccountRowsforBookEntry_ListView_ManagerCheck" Then
            Dim _GLAccountRowsforBookEntry_ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _CurrentUser As User = _ocur.Session.GetObjectByKey(Of User)(_ocur.Session.GetKeyValue(SecuritySystem.CurrentUser))
            If _CurrentUser.UserName <> "SysAdmin" Then
                _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Remove("Criteria1")
                _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Add("Criteria1", _
                    CriteriaOperator.Parse("WorkflowSystemSteps.StepFinancialType=4 and WorkflowSystemSteps.HRResponsible.User.Oid=?", _CurrentUser.Oid))
            Else
                _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Remove("Criteria1")
                _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Add("Criteria1", _
                    CriteriaOperator.Parse("WorkflowSystemSteps.StepFinancialType=4"))
            End If
            Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "GLAccountRowsforBookEntry_ListView" Then
            Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aGLAccountsReadyforBookEntry.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aVATDivide.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aVATMerge.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", True)
            Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
            GenerateInvoiceWorkflowStep(_uow)


            Dim _GLAccountRowsforBookEntry_ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Remove("Criteria1")
            _GLAccountRowsforBookEntry_ListView.CollectionSource.Criteria.Add("Criteria1", CriteriaOperator.Parse("WorkflowSystemSteps.IsReadyForImport=True"))

            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged

        End If

        If View.Id = "InvoiceHeader_ListView_GLAccounts" 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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", True)
        End If
        If View.Id = "RegistryHeader_ListView_GLAccounts" 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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", True)
        End If
        If View.Id = "GLRows_ListView_Account" Or View.Id = "GLRows_ListView_All" Or View.Id = "GLRows_ListView_Bank" Or _
           View.Id = "GLRows_ListView_Cassa" Or View.Id = "GLRows_ListView_Compensation" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", True)

            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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id Like "GLRows_ListView_Error*" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", True)
        End If
        If View.Id = "GLRows_ListView_Bank" Or _
           View.Id = "GLRows_ListView_Cassa" Or View.Id = "GLRows_ListView_Compensation" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsDivide.Active.SetItemValue("", True)
        End If

        If View.Id = "GLRows_ListView_Ratediff" Or _
           View.Id = "GLRows_ListView_Mixed" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", True)
        End If

        If View.Id Like "GLAccountRowsforBookEntry*" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderInHeader_PCIDetail_ListView" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsDivide.Active.SetItemValue("", True)
        End If

        AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", True)
        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsDivide.Active.SetItemValue("", True)

        Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", True)
        Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", True)
        Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", True)

        Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", True)
        Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", True)
        If View.Id = "GLAccountRowsforBookEntry_ListView_ManagerCheck" Then
            Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "GLAccountRowsforBookEntry_ListView" Then
            Frame.GetController(Of BookEntryVC).aBookEntryCheckGotoNextStep.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aGLAccountsforBookEntryCheck.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aViewRegistry_BEC.Active.SetItemValue("", False)
            Frame.GetController(Of BookEntryVC).aViewAttachments.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            RemoveHandler View.SelectionChanged, AddressOf view_SelectionChanged

        End If

        If View.Id = "InvoiceHeader_ListView_GLAccounts" 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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", False)
        End If
        If View.Id = "RegistryHeader_ListView_GLAccounts" 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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLAccountsforBookEntryRemove.Active.SetItemValue("", False)
        End If
        If View.Id = "GLRows_ListView_Account" Or View.Id = "GLRows_ListView_Bank" Or _
           View.Id = "GLRows_ListView_Cassa" Or View.Id = "GLRows_ListView_Compensation" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsCheck.Active.SetItemValue("", False)

            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.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "GLRows_ListView_Bank" Or _
           View.Id = "GLRows_ListView_Cassa" Or View.Id = "GLRows_ListView_Compensation" Then
            Frame.GetController(Of SISBusiness.Module.BookEntryVC).aGLRowsDivide.Active.SetItemValue("", False)
        End If

        If View.Id Like "GLAccountRowsforBookEntry*" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        RemoveHandler View.SelectionChanged, AddressOf view_SelectionChanged

    End Sub
    Private Sub view_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _GLRows As GLRows
        Dim _GLAccounts As GLAccounts
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry
        If View Is Nothing Then Exit Sub
        If noselect Then Exit Sub
        If View.Id Like "GLRows_ListView*" And noselect = False Then
            selection.Clear()
            For Each _GLRows In View.SelectedObjects
                selection.Add(_GLRows)
            Next
        End If
        If View.Id = "GLAccounts_ListView" Then
            selection.Clear()
            For Each _GLAccounts In View.SelectedObjects
                selection.Add(_GLAccounts)
            Next
        End If
        If View.Id = "GLAccountRowsforBookEntry_ListView" And noselect = False Then
            selection.Clear()
            For Each _GLAccountRowsforBookEntry In View.SelectedObjects
                selection.Add(_GLAccountRowsforBookEntry)
            Next
            If selection.Count = 1 Then
                Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
                Dim _RegistryRowsFinancial As RegistryRowsFinancial = _ocur.GetObjectByKey(Of RegistryRowsFinancial)(_GLAccountRowsforBookEntry.Oid)
                If _RegistryRowsFinancial IsNot Nothing Then
                    Frame.GetController(Of BookEntryVC)().aVATDivide.Enabled.SetItemValue("", True)
                Else
                    Frame.GetController(Of BookEntryVC)().aVATDivide.Enabled.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "GLAccountRowsforBookEntry_ListView_ManagerCheck" And noselect = False Then
            selection.Clear()
            For Each _GLAccountRowsforBookEntry In View.SelectedObjects
                selection.Add(_GLAccountRowsforBookEntry)
            Next
        End If
    End Sub

    Private Sub aBookEntryPayables_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBookEntryPayables.Execute
        '//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        '/
        '/ Szállítói számlák iktatásból feladása
        '/
        '//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RegistryHeader As RegistryHeader
        Dim I As Long
        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _RegistryHeader In View.SelectedObjects
            I += 1
            _RegistryHeader.BookEntry(0, _RegistryHeader, _uow, False)
            If I Mod 100 = 0 Then _uow.CommitChanges()
            RaiseEvent DoWork()
        Next
        RaiseEvent FinalWork
        _uow.CommitChanges()
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If

    End Sub
    Private Sub aBookEntryReceiveables_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBookEntryReceiveables.Execute
        '//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        '/
        '/ Vevői számlák feladása
        '/
        '//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _InvoiceHeader As InvoiceHeader
        Dim I As Long = 0

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _InvoiceHeader In View.SelectedObjects
            I += 1
            _InvoiceHeader.BookEntry(0, _InvoiceHeader, _uow, False)
            RaiseEvent DoWork()
            If I Mod 100 = 0 Then _uow.CommitChanges()
        Next
        RaiseEvent FinalWork
        _uow.CommitChanges()
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aGLRowsCheck_Cancel(sender As Object, e As System.EventArgs) Handles aGLRowsCheck.Cancel
        noselect = False
    End Sub
    Private Sub aGLRowsCheck_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLRowsCheck.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRowsCheck As GLRowsCheck = _onew.CreateObject(Of GLRowsCheck)()
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)
        If _GLRows IsNot Nothing Then
            _GLRowsCheck.ChartOfAccountsYear = _onew.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", _GLRows.GLHeader.DateExecution.Year))
        Else
            _GLRowsCheck.ChartOfAccountsYear = _onew.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", GetSQLTime(_onew.Session).Year))
        End If

        e.View = Application.CreateDetailView(_onew, "GLRowsCheck_DetailView", False, _GLRowsCheck)
    End Sub
    Private Sub aGLRowsCheck_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLRowsCheck.Execute
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace 'TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsCheck As GLRowsCheck = TryCast(e.PopupWindow.View.SelectedObjects(0), GLRowsCheck)
        Dim _GLRows As GLRows
        Dim I As Long = 0
        Dim _Controlling As Controlling
        Dim _JobNumberObjects As JobNumberObjects
        Dim _CostHolder As CostHolder
        Dim _CostPlace As CostPlace
        Dim _UniqueObjects As UniqueObjects
        Dim _DebitChartOfAccounts As ChartOfAccounts
        Dim _CreditChartOfAccounts As ChartOfAccounts
        If _GLRowsCheck.Controlling IsNot Nothing Then
            _Controlling = _onew.FindObject(Of Controlling)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.Controlling.Oid), True)
            If _Controlling.Children.Count > 0 Then
                Throw New Exception(String.Format("Nem gyermekobjektum. <<{0}>> Csak gyermekobjektum választható ki !", _Controlling.Name))
                Exit Sub
            End If
        Else
            _Controlling = Nothing
        End If
        If _GLRowsCheck.JobNumberObjects IsNot Nothing Then
            _JobNumberObjects = _onew.FindObject(Of JobNumberObjects)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.JobNumberObjects.Oid), True)
        Else
            _JobNumberObjects = Nothing
        End If
        If _GLRowsCheck.CostHolder IsNot Nothing Then
            _CostHolder = _onew.FindObject(Of CostHolder)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.CostHolder.Oid), True)
        Else
            _CostHolder = Nothing
        End If
        If _GLRowsCheck.CostPlace IsNot Nothing Then
            _CostPlace = _onew.FindObject(Of CostPlace)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.CostPlace.Oid), True)
        Else
            _CostPlace = Nothing
        End If
        If _GLRowsCheck.UniqueObjects IsNot Nothing Then
            _UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.UniqueObjects.Oid), True)
        Else
            _UniqueObjects = Nothing
        End If

        If _GLRowsCheck.DebitChartOfAccounts IsNot Nothing Then
            _DebitChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.DebitChartOfAccounts.Oid), True)
            If _DebitChartOfAccounts.Children.Count > 0 Then
                Throw New Exception("*A tartozik számlaszám NEM lehet szülő !")
                Exit Sub
            End If
        Else
            _DebitChartOfAccounts = Nothing
        End If
        If _GLRowsCheck.CreditChartOfAccounts IsNot Nothing Then
            _CreditChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("Oid=?", _GLRowsCheck.CreditChartOfAccounts.Oid), True)
            If _CreditChartOfAccounts.Children.Count > 0 Then
                Throw New Exception("*A követel számlaszám NEM lehet szülő !")
                Exit Sub
            End If
        Else
            _CreditChartOfAccounts = Nothing
        End If

        If _DebitChartOfAccounts IsNot Nothing And _CreditChartOfAccounts IsNot Nothing Then
            If _DebitChartOfAccounts.Oid = _CreditChartOfAccounts.Oid Then
                Throw New Exception("*A tartozik és a követel számlaszám NEM lehet azonos !")
                Exit Sub
            End If
        End If
        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLRows In View.SelectedObjects
            _GLRows = _onew.GetObject(_GLRows)
            I += 1
            RaiseEvent DoWork()
            If _Controlling IsNot Nothing Then
                _GLRows.Controlling = _Controlling
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.InvoiceRows.Controlling = _Controlling
                End If
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.Controlling = _Controlling
                End If
            End If

            If _JobNumberObjects IsNot Nothing Then
                _GLRows.JobNumberObjects = _JobNumberObjects
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.InvoiceRows.JobNumberObjects = _JobNumberObjects
                End If
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.JobNumberObjects = _JobNumberObjects
                End If
            End If

            If _CostHolder IsNot Nothing Then
                _GLRows.CostHolder = _CostHolder
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.InvoiceRows.CostHolder = _CostHolder
                End If
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.CostHolder = _CostHolder
                End If
            End If

            If _CostPlace IsNot Nothing Then
                _GLRows.CostPlace = _CostPlace
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.InvoiceRows.CostPlace = _CostPlace
                End If
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.CostPlace = _CostPlace
                End If
            End If

            If _UniqueObjects IsNot Nothing Then
                _GLRows.UniqueObjects = _UniqueObjects
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.InvoiceRows.UniqueObjects = _UniqueObjects
                End If
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.UniqueObjects = _UniqueObjects
                End If
            End If

            If _DebitChartOfAccounts IsNot Nothing Then
                If _GLRows.GLHeader.DateExecution.Year = _GLRowsCheck.ChartOfAccountsYear.AccountYear Then
                    _GLRows.DebitChartOfAccounts = _DebitChartOfAccounts
                End If
            End If

            If _CreditChartOfAccounts IsNot Nothing Then
                If _GLRows.GLHeader.DateExecution.Year = _GLRowsCheck.ChartOfAccountsYear.AccountYear Then
                    _GLRows.CreditChartOfAccounts = _CreditChartOfAccounts
                End If
            End If

            If _GLRowsCheck.IsDateExecution = True Then
                _GLRows.DateExecution = _GLRowsCheck.DateExecution
                If _GLRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    _GLRows.RegistryRowsFinancialControlling.DateExecution = _GLRowsCheck.DateExecution
                End If
                If _GLRows.InvoiceRows IsNot Nothing Then
                    _GLRows.DateExecution = _GLRowsCheck.DateExecution
                End If
            End If
            If _GLRows.GLRowsType <> _GLRowsCheck.GLRowsType Then
                _GLRows.GLRowsType = _GLRowsCheck.GLRowsType
            End If
            If I Mod 100 = 0 Then _onew.CommitChanges()
        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork

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

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aGLRowsDivide_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLRowsDivide.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRowsDivide As GLRowsDivide = _onew.CreateObject(Of GLRowsDivide)()
        e.View = Application.CreateDetailView(_onew, "GLRowsDivide_DetailView", False, _GLRowsDivide)
    End Sub
    Private Sub aGLRowsDivide_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLRowsDivide.Execute
        '-- Sor bontása
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsDivide As GLRowsDivide = TryCast(e.PopupWindow.View.SelectedObjects(0), GLRowsDivide)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)
        Dim _GLRows_New As GLRows = Nothing
        Dim _Rate As Double
        Dim _AmountHUF As Double
        Dim _AmountDEV As Double

        If _GLRows Is Nothing Then
            Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)
            If _PCIDetail Is Nothing Then
                Exit Sub
            Else
                _GLRows = _PCIDetail.GLRows
            End If

        End If


        If _GLRows.AmountDEV <> 0 Then _Rate = _GLRows.AmountHUF / _GLRows.AmountHUF

        If _GLRowsDivide.AmountPercent > 0 Then
            '-- GLRowsNew
            _GLRows_New = _ocur.CreateObject(Of GLRows)()
            _GLRows_New.AmountDEV = Math.Round(_GLRows.AmountDEV * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.AmountHUF = Math.Round(_GLRows.AmountHUF * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.CashType = _GLRows.CashType
            _GLRows_New.ConnectInfo = _GLRows.ConnectInfo
            _GLRows_New.Controlling = _GLRows.Controlling
            _GLRows_New.CostHolder = _GLRows.CostHolder
            _GLRows_New.CostPlace = _GLRows.CostPlace

            _GLRows_New.CreditAmountDEV = Math.Round(_GLRows.CreditAmountDEV * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.CreditAmountHUF = Math.Round(_GLRows.CreditAmountHUF * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)

            _GLRows_New.CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
            _GLRows_New.Customer = _GLRows.Customer
            _GLRows_New.DateVAT = _GLRows.DateVAT

            _GLRows_New.DebitAmountDEV = Math.Round(_GLRows.DebitAmountDEV * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.DebitAmountHUF = Math.Round(_GLRows.DebitAmountHUF * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)

            _GLRows_New.DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
            _GLRows_New.GLHeader = _GLRows.GLHeader
            _GLRows_New.GLRowsGUID = _GLRows.GLRowsGUID
            _GLRows_New.GLRowsTypeVAT = _GLRows.GLRowsTypeVAT
            _GLRows_New.ImportID = _GLRows.ImportID

            _GLRows_New.InAmountDEV = Math.Round(_GLRows.InAmountDEV * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.InAmountHUF = Math.Round(_GLRows.InAmountHUF * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)

            _GLRows_New.JobNumberObjects = _GLRows.JobNumberObjects
            _GLRows_New.LiquidAssets = _GLRows.LiquidAssets
            _GLRows_New.NoteRows = _GLRows.NoteRows

            _GLRows_New.OutAmountDEV = Math.Round(_GLRows.OutAmountDEV * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)
            _GLRows_New.OutAmountHUF = Math.Round(_GLRows.OutAmountHUF * (_GLRowsDivide.AmountPercent / 100), 2, MidpointRounding.AwayFromZero)

            _GLRows_New.PartnerType = _GLRows.PartnerType
            _GLRows_New.TransactionType = _GLRows.TransactionType
            _GLRows_New.UniqueObjects = _GLRows.UniqueObjects
            _GLRows_New.VAT = _GLRows.VAT
            _GLRows_New.Save()

            '-- GLRows

            _GLRows.AmountDEV = Math.Round(_GLRows.AmountDEV * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.AmountHUF = Math.Round(_GLRows.AmountHUF * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.CreditAmountDEV = Math.Round(_GLRows.CreditAmountDEV * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.CreditAmountHUF = Math.Round(_GLRows.CreditAmountHUF * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.DebitAmountDEV = Math.Round(_GLRows.DebitAmountDEV * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.DebitAmountHUF = Math.Round(_GLRows.DebitAmountHUF * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.InAmountDEV = Math.Round(_GLRows.InAmountDEV * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.InAmountHUF = Math.Round(_GLRows.InAmountHUF * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.OutAmountDEV = Math.Round(_GLRows.OutAmountDEV * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)
            _GLRows.OutAmountHUF = Math.Round(_GLRows.OutAmountHUF * (1 - (_GLRowsDivide.AmountPercent / 100)), 2, MidpointRounding.AwayFromZero)

            _GLRows.Save()
        ElseIf _GLRowsDivide.AmountHUF > 0 And _GLRows.GLHeader.CurrencyName.ShortName = "HUF" Then
            If Math.Abs(_GLRowsDivide.AmountHUF) < Math.Abs(_GLRows.AmountHUF) Then
                '-- GLRowsNew
                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                _GLRows_New.AmountHUF = _GLRowsDivide.AmountHUF
                _GLRows_New.CashType = _GLRows.CashType
                _GLRows_New.ConnectInfo = _GLRows.ConnectInfo
                _GLRows_New.Controlling = _GLRows.Controlling
                _GLRows_New.CostHolder = _GLRows.CostHolder
                _GLRows_New.CostPlace = _GLRows.CostPlace
                If _GLRows.CreditAmountHUF <> 0 Then
                    _GLRows_New.CreditAmountHUF = _GLRowsDivide.AmountHUF
                End If
                _GLRows_New.CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
                _GLRows_New.Customer = _GLRows.Customer
                _GLRows_New.DateVAT = _GLRows.DateVAT
                If _GLRows.DebitAmountHUF <> 0 Then
                    _GLRows_New.DebitAmountHUF = _GLRowsDivide.AmountHUF
                End If
                _GLRows_New.DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
                _GLRows_New.GLHeader = _GLRows.GLHeader
                _GLRows_New.GLRowsGUID = _GLRows.GLRowsGUID
                _GLRows_New.GLRowsTypeVAT = _GLRows.GLRowsTypeVAT
                _GLRows_New.ImportID = _GLRows.ImportID
                If _GLRows.InAmountHUF <> 0 Then
                    _GLRows_New.InAmountHUF = _GLRowsDivide.AmountHUF
                End If
                _GLRows_New.JobNumberObjects = _GLRows.JobNumberObjects
                _GLRows_New.LiquidAssets = _GLRows.LiquidAssets
                _GLRows_New.NoteRows = _GLRows.NoteRows
                If _GLRows.OutAmountHUF <> 0 Then
                    _GLRows_New.OutAmountHUF = _GLRowsDivide.AmountHUF
                End If
                _GLRows_New.PartnerType = _GLRows.PartnerType
                _GLRows_New.TransactionType = _GLRows.TransactionType
                _GLRows_New.UniqueObjects = _GLRows.UniqueObjects
                _GLRows_New.VAT = _GLRows.VAT
                _GLRows_New.Save()

                '-- GLRows
                _AmountHUF = _GLRows.AmountHUF - _GLRowsDivide.AmountHUF
                _GLRows.AmountHUF = _AmountHUF
                If _GLRows.CreditAmountHUF <> 0 Then _GLRows.CreditAmountHUF = _AmountHUF
                If _GLRows.DebitAmountHUF <> 0 Then _GLRows.DebitAmountHUF = _AmountHUF
                If _GLRows.InAmountHUF <> 0 Then _GLRows.InAmountHUF = _AmountHUF
                If _GLRows.OutAmountHUF <> 0 Then _GLRows.OutAmountHUF = _AmountHUF

                _GLRows.Save()
            End If
        ElseIf _GLRowsDivide.AmountDEV > 0 Then
            If Math.Abs(_GLRowsDivide.AmountDEV) < Math.Abs(_GLRows.AmountDEV) Then
                '-- GLRowsNew
                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                _GLRows_New.AmountDEV = _GLRowsDivide.AmountDEV
                _GLRows_New.AmountHUF = Math.Round(_GLRowsDivide.AmountDEV * _Rate, 2, MidpointRounding.AwayFromZero)
                _GLRows_New.CashType = _GLRows.CashType
                _GLRows_New.ConnectInfo = _GLRows.ConnectInfo
                _GLRows_New.Controlling = _GLRows.Controlling
                _GLRows_New.CostHolder = _GLRows.CostHolder
                _GLRows_New.CostPlace = _GLRows.CostPlace
                If _GLRows.CreditAmountDEV <> 0 Then
                    _GLRows_New.CreditAmountDEV = _GLRowsDivide.AmountDEV
                    _GLRows_New.CreditAmountHUF = _GLRows_New.AmountHUF
                End If
                _GLRows_New.CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
                _GLRows_New.Customer = _GLRows.Customer
                _GLRows_New.DateVAT = _GLRows.DateVAT
                If _GLRows.DebitAmountDEV <> 0 Then
                    _GLRows_New.DebitAmountDEV = _GLRowsDivide.AmountDEV
                    _GLRows_New.DebitAmountHUF = _GLRows_New.AmountHUF
                End If
                _GLRows_New.DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
                _GLRows_New.GLHeader = _GLRows.GLHeader
                _GLRows_New.GLRowsGUID = _GLRows.GLRowsGUID
                _GLRows_New.GLRowsTypeVAT = _GLRows.GLRowsTypeVAT
                _GLRows_New.ImportID = _GLRows.ImportID
                If _GLRows.InAmountDEV <> 0 Then
                    _GLRows_New.InAmountDEV = _GLRowsDivide.AmountDEV
                    _GLRows_New.InAmountHUF = _GLRows_New.AmountHUF
                End If
                _GLRows_New.JobNumberObjects = _GLRows.JobNumberObjects
                _GLRows_New.LiquidAssets = _GLRows.LiquidAssets
                _GLRows_New.NoteRows = _GLRows.NoteRows
                If _GLRows.OutAmountDEV <> 0 Then
                    _GLRows_New.OutAmountDEV = _GLRowsDivide.AmountDEV
                    _GLRows_New.OutAmountHUF = _GLRows_New.AmountHUF
                End If
                _GLRows_New.PartnerType = _GLRows.PartnerType
                _GLRows_New.TransactionType = _GLRows.TransactionType
                _GLRows_New.UniqueObjects = _GLRows.UniqueObjects
                _GLRows_New.VAT = _GLRows.VAT
                _GLRows_New.Save()

                '-- GLRows
                _AmountDEV = _GLRows.AmountDEV - _GLRowsDivide.AmountDEV
                _AmountHUF = Math.Round(_GLRowsDivide.AmountDEV * _Rate, 2, MidpointRounding.AwayFromZero)
                _GLRows.AmountDEV = _AmountDEV
                _GLRows.AmountHUF = _AmountHUF
                If _GLRows.CreditAmountDEV <> 0 Then
                    _GLRows.CreditAmountDEV = _AmountDEV
                    _GLRows.CreditAmountHUF = _AmountHUF
                End If
                If _GLRows.DebitAmountDEV <> 0 Then
                    _GLRows.DebitAmountDEV = _AmountDEV
                    _GLRows.DebitAmountHUF = _AmountHUF
                End If
                If _GLRows.InAmountDEV <> 0 Then
                    _GLRows.InAmountDEV = _AmountDEV
                    _GLRows.InAmountHUF = _AmountHUF
                End If
                If _GLRows.OutAmountDEV <> 0 Then
                    _GLRows.OutAmountDEV = _AmountDEV
                    _GLRows.OutAmountHUF = _AmountHUF
                End If
                _GLRows.Save()
            End If
        End If
        _ocur.CommitChanges()
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        GLFunctions.ReconfigurePCI(_uow, _GLRows_New.GLHeader.CustomersFrom, _GLRows_New.Customer, _GLRows_New.PartnerType, _GLRows_New.ConnectInfo)
        If TryCast(Frame, NestedFrame) IsNot Nothing Then
            TryCast(Frame, NestedFrame).ViewItem.View.ObjectSpace.Refresh()
        Else
            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        End If
    End Sub
    Private Sub aBookEntryRemove_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBookEntryRemove.Execute
        noselect = True
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _GLAccounts As GLAccounts
        Dim _InvoiceHeader As InvoiceHeader
        Dim _RegistryHeader As RegistryHeader

        Dim _InvoiceHeader_Collection As New ArrayList
        Dim _RegistryHeader_Collection As New ArrayList
        Dim I As Long
        RaiseEvent InitWork(1, 1, selection.Count)
        For Each _GLAccounts In selection
            If _GLAccounts.ImportedInvoice IsNot Nothing Then
                _InvoiceHeader_Collection.Add(_GLAccounts.ImportedInvoice)
            End If

            If _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                If _GLAccounts.IsRegistry = False Then
                    _RegistryHeader_Collection.Add(_GLAccounts.ImportedRegistryHeader)
                End If
            End If

            RaiseEvent DoWork()
        Next
        RaiseEvent FinalWork

        RaiseEvent InitWork(1, 1, _InvoiceHeader_Collection.Count + _RegistryHeader_Collection.Count)
        For Each _InvoiceHeader In _InvoiceHeader_Collection
            I += 1
            _InvoiceHeader.BookEntryRemove(_InvoiceHeader, _uow, False)
            RaiseEvent DoWork()
            If I Mod 100 = 0 Then _uow.CommitChanges()
        Next
        For Each _RegistryHeader In _RegistryHeader_Collection
            I += 1
            _RegistryHeader.BookEntryRemove(_RegistryHeader, _uow, False)
            RaiseEvent DoWork()
            If I Mod 100 = 0 Then _uow.CommitChanges()
        Next
        RaiseEvent FinalWork
        _uow.CommitChanges()

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        noselect = False
    End Sub

    Private Sub aGLAccountsforBookEntryCheck_Cancel(sender As Object, e As System.EventArgs) Handles aGLAccountsforBookEntryCheck.Cancel
        noselect = False
    End Sub
    Private Sub aGLAccountsforBookEntryCheck_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLAccountsforBookEntryCheck.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _BookEntryCheck As BookEntryCheck = _onew.CreateObject(Of BookEntryCheck)()
        _BookEntryCheck.IsManagerCheck = False
        _BookEntryCheck.ChartOfAccountsYear = _onew.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", GetSQLTime(_onew.Session).Year))
        If View.Id = "GLAccountRowsforBookEntry_ListView_ManagerCheck" Then _BookEntryCheck.IsManagerCheck = True
        e.View = Application.CreateDetailView(_onew, "BookEntryCheck_DetailView", False, _BookEntryCheck)
    End Sub
    Private Sub aGLAccountsforBookEntryCheck_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLAccountsforBookEntryCheck.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)

        Dim _BookEntryCheck As BookEntryCheck = TryCast(e.PopupWindow.View.SelectedObjects(0), BookEntryCheck)

        Dim _BookEntryCheck_ChartOfAccounts As ChartOfAccounts
        Dim _BookEntryCheck_JobNumberObjects As JobNumberObjects
        Dim _BookEntryCheck_UniqueObjects As UniqueObjects
        Dim _BookEntryCheck_CostHolder As CostHolder
        Dim _BookEntryCheck_CostPlace As CostPlace
        Dim _BookEntryCheck_Controlling As Controlling

        Dim i As Long = 0
        If e.PopupWindow.View.SelectedObjects(0).ChartOfAccounts Is Nothing Then
            _BookEntryCheck_ChartOfAccounts = Nothing
        Else
            _BookEntryCheck_ChartOfAccounts = _uow.GetObjectByKey(Of ChartOfAccounts)(e.PopupWindow.View.SelectedObjects(0).ChartOfAccounts.Oid)
        End If
        If e.PopupWindow.View.SelectedObjects(0).JobNumberObjects Is Nothing Then
            _BookEntryCheck_JobNumberObjects = Nothing
        Else
            _BookEntryCheck_JobNumberObjects = _uow.GetObjectByKey(Of JobNumberObjects)(e.PopupWindow.View.SelectedObjects(0).JobNumberObjects.Oid)
        End If
        If e.PopupWindow.View.SelectedObjects(0).UniqueObjects Is Nothing Then
            _BookEntryCheck_UniqueObjects = Nothing
        Else
            _BookEntryCheck_UniqueObjects = _uow.GetObjectByKey(Of UniqueObjects)(e.PopupWindow.View.SelectedObjects(0).UniqueObjects.Oid)
        End If
        If e.PopupWindow.View.SelectedObjects(0).CostHolder Is Nothing Then
            _BookEntryCheck_CostHolder = Nothing
        Else
            _BookEntryCheck_CostHolder = _uow.GetObjectByKey(Of CostHolder)(e.PopupWindow.View.SelectedObjects(0).CostHolder.Oid)
        End If
        If e.PopupWindow.View.SelectedObjects(0).CostPlace Is Nothing Then
            _BookEntryCheck_CostPlace = Nothing
        Else
            _BookEntryCheck_CostPlace = _uow.GetObjectByKey(Of CostPlace)(e.PopupWindow.View.SelectedObjects(0).CostPlace.Oid)
        End If
        If e.PopupWindow.View.SelectedObjects(0).Controlling Is Nothing Then
            _BookEntryCheck_Controlling = Nothing
        Else
            _BookEntryCheck_Controlling = _uow.GetObjectByKey(Of Controlling)(e.PopupWindow.View.SelectedObjects(0).Controlling.Oid)
            If _BookEntryCheck_Controlling.Children.Count > 0 Then
                Throw New Exception(String.Format("Nem gyermekobjektum. <<{0}>> Csak gyermekobjektum választható ki !", _BookEntryCheck_Controlling.Name))
                Exit Sub
            End If
        End If
        e.PopupWindow.Close(False)

        Dim _InvoiceRows As InvoiceRows
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry


        RaiseEvent InitWork(1, 1, selection.Count)
        For Each _GLAccountRowsforBookEntry In selection
            i += 1
            RaiseEvent DoWork()
            If _GLAccountRowsforBookEntry.PartnerType = ePartnerType.eReceivables Then
                _InvoiceRows = _uow.GetObjectByKey(Of InvoiceRows)(_GLAccountRowsforBookEntry.Oid)
                If _InvoiceRows IsNot Nothing Then

                    If _BookEntryCheck_ChartOfAccounts IsNot Nothing Then _InvoiceRows.ChartOfAccounts = _BookEntryCheck_ChartOfAccounts
                    If _BookEntryCheck_CostHolder IsNot Nothing Then
                        _InvoiceRows.CostHolder = _BookEntryCheck_CostHolder
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.CostHolder = _BookEntryCheck_CostHolder
                        End If
                    End If

                    If _BookEntryCheck_CostPlace IsNot Nothing Then
                        _InvoiceRows.CostPlace = _BookEntryCheck_CostPlace
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.CostPlace = _BookEntryCheck_CostPlace
                        End If
                    End If

                    If _BookEntryCheck_UniqueObjects IsNot Nothing Then
                        _InvoiceRows.UniqueObjects = _BookEntryCheck_UniqueObjects
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.UniqueObjects = _BookEntryCheck_UniqueObjects
                        End If
                    End If

                    If _BookEntryCheck_JobNumberObjects IsNot Nothing Then
                        _InvoiceRows.JobNumberObjects = _BookEntryCheck_JobNumberObjects
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.JobNumberObjects = _BookEntryCheck_JobNumberObjects
                        End If
                    End If

                    If _BookEntryCheck_Controlling IsNot Nothing Then
                        _InvoiceRows.Controlling = _BookEntryCheck_Controlling
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.Controlling = _BookEntryCheck_Controlling
                        End If
                    End If
                    If _BookEntryCheck.IsDateExecution = True Then
                        _InvoiceRows.DateExecution = _BookEntryCheck.DateExecution
                        If _InvoiceRows.ContractRows IsNot Nothing Then
                            _InvoiceRows.ContractRows.DateExecution = _BookEntryCheck.DateExecution
                        End If
                    End If

                End If
            End If
            If _GLAccountRowsforBookEntry.PartnerType = ePartnerType.ePayabels Then
                _RegistryRowsFinancialControlling = _uow.GetObjectByKey(Of RegistryRowsFinancialControlling)(_GLAccountRowsforBookEntry.Oid)
                If _RegistryRowsFinancialControlling IsNot Nothing Then
                    If _BookEntryCheck_ChartOfAccounts IsNot Nothing Then _RegistryRowsFinancialControlling.ChartOfAccounts = _BookEntryCheck_ChartOfAccounts
                    If _BookEntryCheck_CostHolder IsNot Nothing Then _RegistryRowsFinancialControlling.CostHolder = _BookEntryCheck_CostHolder
                    If _BookEntryCheck_CostPlace IsNot Nothing Then _RegistryRowsFinancialControlling.CostPlace = _BookEntryCheck_CostPlace
                    If _BookEntryCheck_UniqueObjects IsNot Nothing Then _RegistryRowsFinancialControlling.UniqueObjects = _BookEntryCheck_UniqueObjects
                    If _BookEntryCheck_JobNumberObjects IsNot Nothing Then _RegistryRowsFinancialControlling.JobNumberObjects = _BookEntryCheck_JobNumberObjects
                    If _BookEntryCheck_Controlling IsNot Nothing Then _RegistryRowsFinancialControlling.Controlling = _BookEntryCheck_Controlling
                    If _BookEntryCheck.IsDateExecution = True Then _RegistryRowsFinancialControlling.DateExecution = _BookEntryCheck.DateExecution
                End If
            End If
            If i Mod 100 = 0 Then _uow.CommitChanges()
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aGLAccountsforBookEntryRemove_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLAccountsforBookEntryRemove.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim I As Long = 0

        If View.Id = "InvoiceHeader_ListView_GLAccounts" Then
            Dim _InvoiceHeader As InvoiceHeader
            Dim _InvoiceRows As InvoiceRows
            Dim _InvoiceVATRows As InvoiceVATRows
            RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
            For Each _InvoiceHeader In View.SelectedObjects
                I += 1
                If _InvoiceHeader.GLAccounts Is Nothing Then
                    _InvoiceHeader.ReadyforBookEntry = False
                    For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                        _InvoiceRows.ReadyforBookEntryRows = False
                    Next
                    For Each _InvoiceVATRows In _InvoiceHeader.InvoiceVATRows
                        _InvoiceVATRows.ReadyforBookEntryRows = False

                    Next
                End If
                If I Mod 100 = 0 Then _ocur.CommitChanges()
                RaiseEvent DoWork()
            Next
            _ocur.CommitChanges()
            RaiseEvent FinalWork

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If

        ElseIf View.Id = "RegistryHeader_ListView_GLAccounts" Then
            Dim _RegistryHeader As RegistryHeader
            Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
            Dim _RegistryRowsFinancial As RegistryRowsFinancial
            RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
            For Each _RegistryHeader In View.SelectedObjects
                I += 1
                If _RegistryHeader.F_GLAccounts Is Nothing Then
                    _RegistryHeader.F_ReadyforBookEntry = False
                    For Each _RegistryRowsFinancialControlling In _RegistryHeader.RegistryRowsFinancialControlling
                        _RegistryRowsFinancialControlling.F_ReadyforBookEntryRows = False
                    Next
                    For Each _RegistryRowsFinancial In _RegistryHeader.RegistryRowsFinancial
                        _RegistryRowsFinancial.F_ReadyforBookEntryRows = False
                    Next
                End If
                If I Mod 100 = 0 Then _ocur.CommitChanges()
                RaiseEvent DoWork()
            Next
            _ocur.CommitChanges()
            RaiseEvent FinalWork

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If

        End If
    End Sub

    Private Sub aBookEntryCheckGotoNextStep_Cancel(sender As Object, e As System.EventArgs) Handles aBookEntryCheckGotoNextStep.Cancel
        noselect = False
    End Sub

    Private Sub aBookEntryCheckGotoNextStep_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aBookEntryCheckGotoNextStep.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RegistryChoiceNextStep As RegistryChoiceNextStep
        Dim _InvoiceChoiceNextStep As InvoiceChoiceNextStep
        Dim _WorkflowSystemSteps As WorkflowSystemSteps
        Dim _WorkflowSystem As WorkflowSystem
        Dim _Step As Long
        Dim _IsGroup As Boolean = False
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry
        Dim _InvoiceHeader As InvoiceHeader

        If View.SelectedObjects.Count = 0 Then Exit Sub


        If View.Id = "GLAccountRowsforBookEntry_ListView" Or View.Id = "GLAccountRowsforBookEntry_ListView_ManagerCheck" Then
            _GLAccountRowsforBookEntry = TryCast(View.SelectedObjects(0), GLAccountRowsforBookEntry)
            If _GLAccountRowsforBookEntry.InvoiceHeader IsNot Nothing Then
                _InvoiceChoiceNextStep = _onew.CreateObject(Of InvoiceChoiceNextStep)()

                _WorkflowSystem = _onew.GetObjectByKey(Of WorkflowSystem)(_GLAccountRowsforBookEntry.InvoiceHeader.InvoiceType.WorkflowSystem.Oid)
                _WorkflowSystemSteps = _onew.FindObject(Of WorkflowSystemSteps)(CriteriaOperator.Parse("WorkflowSystem.Oid=?", _WorkflowSystem.Oid), True)
                _InvoiceChoiceNextStep.InvoiceHeader = _onew.GetObject(_GLAccountRowsforBookEntry.InvoiceHeader)
                _InvoiceChoiceNextStep.HRPerson = _WorkflowSystemSteps.HRResponsible
                _InvoiceChoiceNextStep.WorkflowSystemSteps = _WorkflowSystemSteps

                noselect = True

                e.View = Application.CreateDetailView(_onew, "InvoiceChoiceNextStep_DetailView", False, _InvoiceChoiceNextStep)
            Else
                _RegistryChoiceNextStep = _onew.CreateObject(Of RegistryChoiceNextStep)()
                _RegistryChoiceNextStep.RegistryHeader = _onew.GetObject(_GLAccountRowsforBookEntry.RegistryHeader)
                _WorkflowSystem = _RegistryChoiceNextStep.RegistryHeader.CurrentWorkflowSystemSteps.WorkflowSystem
                _Step = _RegistryChoiceNextStep.RegistryHeader.CurrentWorkflowSystemSteps.StepIndex + 1
                _WorkflowSystemSteps = _onew.FindObject(Of WorkflowSystemSteps)(CriteriaOperator.Parse("WorkflowSystem.Oid=? and StepIndex=?", _WorkflowSystem.Oid, _Step), True)
                _RegistryChoiceNextStep.HRPerson = _RegistryChoiceNextStep.RegistryHeader.HRResponsible
                _RegistryChoiceNextStep.WorkflowSystemSteps = _WorkflowSystemSteps

                noselect = True

                e.View = Application.CreateDetailView(_onew, "RegistryChoiceNextStep_DetailView", False, _RegistryChoiceNextStep)
            End If
        End If
    End Sub
    Private Sub aBookEntryCheckGotoNextStep_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aBookEntryCheckGotoNextStep.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RegistryHeader As RegistryHeader
        Dim _CurrentWorkflowSystemStepsFirstShortName As String
        Dim _RegistryTask As RegistryTasks
        Dim _RegistryTask_New As RegistryTasks

        Dim _Faild As Boolean = False
        Dim _OK_NewRegistryTask As Boolean = False
        Dim _NewVC As New SISBusiness.Module.RegistryVC 'A CheckNextStep public function meghívása miatt szükséges egy példány ebből a viewcontrollerből

        Dim _RegistryHeaderCollection As New ArrayList
        Dim _InvoiceHeaderCollection As New ArrayList

        Dim _InvoiceHeader As InvoiceHeader = Nothing
        Dim _InvoiceRows As InvoiceRows = Nothing
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry = Nothing
        Dim _RegistryChoiceNextStep As RegistryChoiceNextStep = Nothing
        Dim _InvoiceChoiceNextStep As InvoiceChoiceNextStep = Nothing
        Dim _GLAccountRowsforBookEntry_First As GLAccountRowsforBookEntry = Nothing

        Dim _InvoiceOrRegistry As Long = 0

        _InvoiceHeaderCollection.Clear()
        _RegistryHeaderCollection.Clear()
        Dim i As Long = 0
        For Each _GLAccountRowsforBookEntry In selection
            If i = 0 Then
                _GLAccountRowsforBookEntry_First = _GLAccountRowsforBookEntry
                If _GLAccountRowsforBookEntry.RegistryHeader IsNot Nothing Then
                    _InvoiceOrRegistry = -1
                    _RegistryHeader = _uow.GetObjectByKey(Of RegistryHeader)(_GLAccountRowsforBookEntry.RegistryHeader.Oid)
                    _CurrentWorkflowSystemStepsFirstShortName = TryCast(_RegistryHeader.CurrentWorkflowSystemSteps, WorkflowSystemSteps).ShortName
                Else
                    _InvoiceOrRegistry = 1
                    _InvoiceRows = _uow.GetObjectByKey(Of InvoiceRows)(_GLAccountRowsforBookEntry.Oid)
                    _CurrentWorkflowSystemStepsFirstShortName = TryCast(_InvoiceRows.WorkflowSystemSteps, WorkflowSystemSteps).ShortName
                End If
                i += 1
            End If


            If _GLAccountRowsforBookEntry.RegistryHeader IsNot Nothing And _InvoiceOrRegistry = 1 Then
                Throw New Exception("*Van kijelölt szállítói sor is !")
                Exit Sub
            End If
            If _GLAccountRowsforBookEntry.InvoiceHeader IsNot Nothing And _InvoiceOrRegistry = -1 Then
                Throw New Exception("*Van kijelölt vevői számla sor is !")
                Exit Sub
            End If


            If _GLAccountRowsforBookEntry.RegistryHeader IsNot Nothing Then

                If _GLAccountRowsforBookEntry.RegistryHeader.CurrentWorkflowSystemSteps.ShortName <> _CurrentWorkflowSystemStepsFirstShortName Then
                    Throw New Exception("*Minden kijelölt sornak azonos lépésen kell állnia !")
                    Exit Sub
                End If

                _RegistryChoiceNextStep = TryCast(e.PopupWindow.View.SelectedObjects(0), RegistryChoiceNextStep)
                If _RegistryHeaderCollection.Contains(_uow.GetObjectByKey(Of RegistryHeader)(_GLAccountRowsforBookEntry.RegistryHeader.Oid)) = False Then
                    _RegistryHeaderCollection.Add(_uow.GetObjectByKey(Of RegistryHeader)(_GLAccountRowsforBookEntry.RegistryHeader.Oid))
                End If

            End If
            If _GLAccountRowsforBookEntry.InvoiceHeader IsNot Nothing Then
                For Each _InvoiceRows In _GLAccountRowsforBookEntry.InvoiceHeader.InvoiceRows
                    If _InvoiceRows.WorkflowSystemSteps.ShortName <> _CurrentWorkflowSystemStepsFirstShortName Then
                        Throw New Exception("*Minden kijelölt sornak azonos lépésen kell állnia !")
                        Exit Sub
                    End If
                Next
                _InvoiceChoiceNextStep = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceChoiceNextStep)
                If _InvoiceHeaderCollection.Contains(_uow.GetObjectByKey(Of InvoiceHeader)(_GLAccountRowsforBookEntry.InvoiceHeader.Oid)) = False Then
                    _InvoiceHeaderCollection.Add(_uow.GetObjectByKey(Of InvoiceHeader)(_GLAccountRowsforBookEntry.InvoiceHeader.Oid))
                End If
            End If
        Next


        If _RegistryChoiceNextStep IsNot Nothing Then
            If _RegistryHeaderCollection.Count > 0 Then
                RaiseEvent InitWork(1, 1, _RegistryHeaderCollection.Count)
                For Each _RegistryHeader In _RegistryHeaderCollection
                    RaiseEvent DoWork()
                    If CheckNextStep(_uow, _RegistryHeader, _RegistryChoiceNextStep.WorkflowSystemSteps) Then
                        _OK_NewRegistryTask = True
                    Else
                        _OK_NewRegistryTask = False
                    End If

                    _RegistryHeader.CurrentWorkflowSystemSteps = _uow.GetObjectByKey(Of WorkflowSystemSteps)(_RegistryChoiceNextStep.WorkflowSystemSteps.Oid)
                    If _RegistryHeader.CurrentWorkflowSystemSteps.StepIndex = 1 Then
                        _RegistryHeader.IsEditable = True
                    End If
                    _RegistryTask = _uow.FindObject(Of RegistryTasks)(CriteriaOperator.Parse("RegistryHeader.Oid=? AND Status=?", _RegistryHeader.Oid, 0))

                    If _RegistryTask IsNot Nothing Then
                        _RegistryTask.Status = General.TaskStatus.Completed
                    End If

                    If _RegistryChoiceNextStep.WorkflowSystemSteps.IsLastStep = False Then
                        If _OK_NewRegistryTask = True Then
                            _RegistryTask_New = New RegistryTasks(_uow) '--- Egy új feladat példányosítása
                            _RegistryTask_New.RegistryHeader = _RegistryHeader '--- Az új feladatnak az iktatás fejlécét átadjuk
                            _RegistryTask_New.CurrentWorkflowSystemSteps = _uow.GetObjectByKey(Of WorkflowSystemSteps)(_RegistryHeader.CurrentWorkflowSystemSteps.Oid)
                            _RegistryTask_New.StartDate = GetSQLTime(_ocur.Session)
                            _RegistryTask_New.Status = General.TaskStatus.NotStarted
                            _RegistryTask_New.HRPerson = _uow.GetObjectByKey(Of HRPerson)(_RegistryChoiceNextStep.HRPerson.Oid)
                            _RegistryTask_New.Description = _RegistryChoiceNextStep.Description
                            _RegistryTask_New.Subject = _RegistryHeader.ShortName
                            _RegistryTask_New.DueDate = DateAdd(DateInterval.Day, _RegistryChoiceNextStep.WorkflowSystemSteps.ExpirationDayofFailure, Now)
                        End If
                    Else
                        For Each _RegistryRowsFinancialControlling In _RegistryHeader.RegistryRowsFinancialControlling
                            _RegistryRowsFinancialControlling.WorkflowMessage = _RegistryChoiceNextStep.Description
                        Next
                    End If
                Next
                RaiseEvent FinalWork
            End If
        End If

        If _InvoiceChoiceNextStep IsNot Nothing Then
            If _InvoiceHeaderCollection.Count > 0 Then
                Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
                RaiseEvent InitWork(1, 1, _InvoiceHeaderCollection.Count)
                For Each _InvoiceHeader In _InvoiceHeaderCollection
                    For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                        RaiseEvent DoWork()
                        _InvoiceRows.WorkflowSystemSteps = _uow.GetObjectByKey(Of WorkflowSystemSteps)(_InvoiceChoiceNextStep.WorkflowSystemSteps.Oid)
                    Next
                Next
                RaiseEvent FinalWork
                Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
            End If
        End If

        _uow.CommitChanges()
        noselect = False

        If _Faild = True Then
            '---- Teszt Miatt-------------------
            e.PopupWindow.Close()
            noselect = False
            Throw New Exception("*A kijelölt elemek között volt nem megfelelő lépésen álló feladat, ami nem került léptetésre!")
            _OK_NewRegistryTask = False
        End If

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aViewAttachments_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments.Execute
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry = TryCast(View.SelectedObjects(0), GLAccountRowsforBookEntry)
        If _GLAccountRowsforBookEntry.RegistryHeader IsNot Nothing Then

            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _GLAccountRowsforBookEntry.RegistryHeader.DocumentFile.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _GLAccountRowsforBookEntry.RegistryHeader.DocumentFile.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        ElseIf _GLAccountRowsforBookEntry.InvoiceHeader IsNot Nothing Then
            Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), GLAccountRowsforBookEntry).InvoiceHeader
            Dim _ReportData As ReportData
            Dim _ReportData_DEV As ReportData
            Dim _ReportData_DEV_EU As ReportData
            Dim _CurrencyName As CurrencyName
            _CurrencyName = _InvoiceHeader.CurrencyName
            If _InvoiceHeader IsNot Nothing Then
                _ReportData = _InvoiceHeader.InvoiceType.ReportData
                _ReportData_DEV = _InvoiceHeader.InvoiceType.ReportData_DEV
                _ReportData_DEV_EU = _InvoiceHeader.InvoiceType.ReportData_DEV_EU
                If _ReportData IsNot Nothing Or _InvoiceHeader Is Nothing Then
                    If _CurrencyName.ShortName = "HUF" Then
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                    Else
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_DEV, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                    End If

                End If
            End If
        End If
    End Sub
    Private Sub aViewAttachments_BEP_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_BEP.Execute
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _CustomersFrom As CustomersFrom = _RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RegistryHeader.Customers
        Dim _DocumentNumber As String = _RegistryHeader.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")

    End Sub
    Private Sub aViewRegistry_BEP_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_BEP.Execute
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _CustomersFrom As CustomersFrom = _RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RegistryHeader.Customers
        Dim _DocumentNumber As String = _RegistryHeader.DocumentNumber
        ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
    End Sub
    Public Function CheckNextStep(ByVal p_uow As UnitOfWork, ByVal p_RegistryHeader As RegistryHeader, ByVal p_StepToJump As WorkflowSystemSteps)
        'Ha ezt módosítod akkor a bookentryVC-ben is módosíts!
        Dim _WorkflowSystem As WorkflowSystem = p_uow.FindObject(Of WorkflowSystem)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", p_RegistryHeader.RegistryType.WorkflowSystem.ShortName))
        Dim _CurrentStepIndex As Long = p_RegistryHeader.CurrentWorkflowSystemSteps.StepIndex - 1
        Dim _StepToJumpIndex As Long = p_StepToJump.StepIndex - 2

        Dim _sortCollection As SortingCollection = New SortingCollection()
        _sortCollection.Add(New SortProperty("StepIndex", DB.SortingDirection.Ascending))
        _WorkflowSystem.WorkflowSystemSteps.Sorting = _sortCollection

        For i = _CurrentStepIndex To _StepToJumpIndex
            If TryCast(_WorkflowSystem.WorkflowSystemSteps(i), WorkflowSystemSteps).StepFinancialType = eStepFinancialType.eCheckVAT Then
                If p_RegistryHeader.BruttoAmountRemaining <> 0 Then
                    '---- TESZT MIATT-----------------------------
                    Throw New Exception("*Nincs Áfára teljesen lebontva, nem lehet tovább léptetni")
                End If
            End If
            If TryCast(_WorkflowSystem.WorkflowSystemSteps(i), WorkflowSystemSteps).StepFinancialType = eStepFinancialType.eCheckControlling Then
                Dim _ParentControllings As String = "" 'Azon kontrollszámok nevét tartalmazó string amelyek focsoportok
                If p_RegistryHeader.NettoAmountRemaining <> 0 Then
                    '--- TESZT MIATT---------------------
                    Throw New Exception("*Nincs a számla teljesen leigazolva, nem lehet tovább léptetni")
                End If
                If p_RegistryHeader.BruttoAmountRemainingBank <> 0 Then
                    Throw New Exception("*Nincs a teljes összeg lebontva a banki csomagokra")
                End If
                'Az iktatás összes számlaigazolási sorának vizsgálata, hogy van e bennük hivatkozás focsoport cm-re
                For Each _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling In p_RegistryHeader.RegistryRowsFinancialControlling
                    If _RegistryRowsFinancialControlling.Controlling.IsParent = True Then
                        _ParentControllings += _RegistryRowsFinancialControlling.Controlling.Name & ", "
                    End If
                Next
                If Not _ParentControllings = "" Then

                    Throw New Exception("*Figyelem a felhasznált CM számok focsoportok: " & _ParentControllings & "! Kérem válasszon egy nem főcsoport CM számot")
                End If
            End If
            If TryCast(_WorkflowSystem.WorkflowSystemSteps(i), WorkflowSystemSteps).StepFinancialType = eStepFinancialType.eCheckCOCScanned Then
                'Aktu�lis iktat�shoz rendelt teljes�t�sre bont�s sorokat ellenoriz. A teljes leigazoland� nett� �sszegre vonatkoz�an a CM sz�moknak be�ll�tott hat�r�rt�ket vizsg�l
                'miszerint k�teles visszacsatolt al��rt teles�t�si igazol�s nyomtatv�nyt visszacsatolni

                '�sszegyujti az iktat�shoz tartoz� teljes�t�sre bont�s sorokat
                Dim _RegistryRowsFinancialControllingCollection As XPCollection(Of RegistryRowsFinancialControlling) = New XPCollection(Of RegistryRowsFinancialControlling)(PersistentCriteriaEvaluationBehavior.InTransaction, p_uow, CriteriaOperator.Parse("RegistryHeader=?", p_RegistryHeader))
                Dim sortCollection As SortingCollection = New SortingCollection()
                Dim j As Long = 0
                Dim _WhileNextStep As Boolean = True
                Dim _AmountonCM As Double

                'Sorba rendezi a kollekci�t cm sz�m alapj�n
                sortCollection.Add(New SortProperty("Controlling.Name", DB.SortingDirection.Ascending))
                _RegistryRowsFinancialControllingCollection.Sorting = sortCollection

                Do While _WhileNextStep
                    ' Az aktu�lis CM sz�m egyenlege null�r�l indul
                    _AmountonCM = 0
                    Do
                        'A CM egyenleg�t n�velj�k addig am�g az aktu�lis soron r� hivatkoznak
                        _AmountonCM += _RegistryRowsFinancialControllingCollection(j).Amount

                        'Ha a CM -nek be van �ll�tva, hogy k�telesek vagyunk elv�gezni a vizsg�latot
                        '�s a CM aktu�lis egyenlege meghaladja a megengedett �sszeget �s nincs f�jl visszacsatolva
                        'akkor nem lehet tov�bbengedni a folyamatot.
                        If _RegistryRowsFinancialControllingCollection(j).Controlling.IsCOCRequired And _
                            _AmountonCM > _RegistryRowsFinancialControllingCollection(j).Controlling.COCAboveAmount And _
                            _RegistryRowsFinancialControllingCollection(j).File Is Nothing Then
                            '--- TESZT MIATT---------------------
                            Throw New Exception("*Nincs feltöltve a beszkennelt aláírt teljesítési igazolás!")
                        End If

                        If j = _RegistryRowsFinancialControllingCollection.Count - 1 Then
                            _WhileNextStep = False
                            Exit Do
                        End If

                        j += 1
                        'Ez a kitétel azért kell, hogy addig gyujtse egy adott CM egyenleg�t am�g az aktu�lis �s a k�vetkezo sor ugyanarra a CM-re hivatkozik
                    Loop While _RegistryRowsFinancialControllingCollection(j - 1).Controlling.Name = _RegistryRowsFinancialControllingCollection(j).Controlling.Name
                Loop
            End If
        Next
        Return True
    End Function
    Private Sub aViewRegistry_BEC_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_BEC.Execute
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry = TryCast(View.SelectedObjects(0), GLAccountRowsforBookEntry)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        If _GLAccountRowsforBookEntry IsNot Nothing Then
            If _GLAccountRowsforBookEntry.RegistryHeader IsNot Nothing Then
                Dim _CustomersFrom As CustomersFrom = _GLAccountRowsforBookEntry.RegistryHeader.CustomersFrom
                Dim _Customers As Customers = _GLAccountRowsforBookEntry.RegistryHeader.Customers
                Dim _DocumentNumber As String = _GLAccountRowsforBookEntry.RegistryHeader.DocumentNumber
                ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
            End If
        End If

    End Sub

    Public Sub GenerateInvoiceWorkflowStep(ByVal _uow As UnitOfWork)
        Dim _InvoiceType_Collection As New XPCollection(Of InvoiceType)(_uow, CriteriaOperator.Parse("isnull(WorkflowSystem)=False"))
        Dim _InvoiceType As InvoiceType

        Dim _InvoiceRows_Collection As XPCollection(Of InvoiceRows)
        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceVATRows As InvoiceVATRows
        Dim _InvoiceVATRows_Collection As XPCollection(Of InvoiceVATRows)

        Dim _WorkflowSystemSteps As WorkflowSystemSteps
        Dim _IsCheckManager As Boolean
        For Each _InvoiceType In _InvoiceType_Collection
            _IsCheckManager = False
            For Each _WorkflowSystemSteps In _InvoiceType.WorkflowSystem.WorkflowSystemSteps
                If _WorkflowSystemSteps.StepFinancialType = eStepFinancialType.eCheckManager Then
                    _IsCheckManager = True
                    _InvoiceRows_Collection = New XPCollection(Of InvoiceRows)(_uow, CriteriaOperator.Parse("InvoiceHeader.InvoiceType.Oid=? and isnull(WorkflowSystemSteps)=True", _InvoiceType.Oid))
                    For Each _InvoiceRows In _InvoiceRows_Collection
                        _InvoiceRows.WorkflowSystemSteps = _WorkflowSystemSteps
                    Next
                End If
                If _IsCheckManager = False And _WorkflowSystemSteps.IsLastStep = True Then
                    _InvoiceRows_Collection = New XPCollection(Of InvoiceRows)(_uow, CriteriaOperator.Parse("InvoiceHeader.InvoiceType.Oid=? and isnull(WorkflowSystemSteps)=True", _InvoiceType.Oid))
                    For Each _InvoiceRows In _InvoiceRows_Collection
                        _InvoiceRows.WorkflowSystemSteps = _WorkflowSystemSteps
                    Next
                End If
            Next
        Next
        _InvoiceVATRows_Collection = New XPCollection(Of InvoiceVATRows)(_uow, CriteriaOperator.Parse("ReadyforBookEntryRows=False"))
        For Each _InvoiceVATRows In _InvoiceVATRows_Collection
            _InvoiceVATRows.ReadyforBookEntryRows = True
        Next
        _uow.CommitChanges()
    End Sub
    Private Sub aVATDivide_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aVATDivide.Cancel

    End Sub
    Private Sub aVATDivide_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aVATDivide.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccountRowsforBookEntry = TryCast(View.SelectedObjects(0), GLAccountRowsforBookEntry)
        Dim _DivideVAT As DivideVAT
        Dim _RegistryRowsFinancial As RegistryRowsFinancial

        _RegistryRowsFinancial = _ocur.GetObjectByKey(Of RegistryRowsFinancial)(_GLAccountRowsforBookEntry.Oid)
        If _RegistryRowsFinancial IsNot Nothing Then
            _DivideVAT = _onew.CreateObject(Of DivideVAT)()
            _DivideVAT.RegistryRowsFinancial = _onew.GetObjectByKey(Of RegistryRowsFinancial)(_RegistryRowsFinancial.Oid)
            _DivideVAT.DateVAT = _RegistryRowsFinancial.DateVAT
            _DivideVAT.VAT = _onew.GetObjectByKey(Of VAT)(_RegistryRowsFinancial.VAT.Oid)
            e.View = Application.CreateDetailView(_onew, "DivideVAT_DetailView", True, _DivideVAT)
        End If

    End Sub
    Private Sub aVATDivide_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aVATDivide.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DivideVAT = TryCast(e.PopupWindow.View.SelectedObjects(0), DivideVAT)
        Dim _RegistryRowsFinancial As RegistryRowsFinancial = _ocur.GetObjectByKey(Of RegistryRowsFinancial)(_DivideVAT.RegistryRowsFinancial.Oid)

        Try
            If _DivideVAT.BruttoAmount = 0 Then
                _RegistryRowsFinancial.DateVAT = _DivideVAT.DateVAT
                If _RegistryRowsFinancial.VAT.Oid <> _DivideVAT.VAT.Oid Then
                    _RegistryRowsFinancial.VAT = _ocur.GetObjectByKey(Of VAT)(_DivideVAT.VAT.Oid)
                End If
                _ocur.CommitChanges()
                _RegistryRowsFinancial.RegistryHeader.RecalculateVAT(_ocur, _RegistryRowsFinancial.RegistryHeader)
                _ocur.CommitChanges()
                If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                End If
            Else
                Dim _RegistryRowsFinancial_NEW As RegistryRowsFinancial = _ocur.CreateObject(Of RegistryRowsFinancial)()
                _RegistryRowsFinancial_NEW.BruttoAmount = _RegistryRowsFinancial.BruttoAmount - _DivideVAT.BruttoAmount
                _RegistryRowsFinancial_NEW.DateVAT = _DivideVAT.DateVAT
                _RegistryRowsFinancial_NEW.Note = _DivideVAT.Note
                _RegistryRowsFinancial_NEW.RegistryHeader = _ocur.GetObjectByKey(Of RegistryHeader)(_RegistryRowsFinancial.RegistryHeader.Oid)
                _RegistryRowsFinancial_NEW.VAT = _ocur.GetObjectByKey(Of VAT)(_DivideVAT.VAT.Oid)

                _RegistryRowsFinancial.BruttoAmount -= _RegistryRowsFinancial_NEW.BruttoAmount
                _RegistryRowsFinancial.DateVAT = _DivideVAT.DateVAT
                _RegistryRowsFinancial.Note = _DivideVAT.Note
                _RegistryRowsFinancial.VAT = _ocur.GetObjectByKey(Of VAT)(_DivideVAT.VAT.Oid)


                _ocur.CommitChanges()
                _RegistryRowsFinancial.RegistryHeader.RecalculateVAT(_ocur, _RegistryRowsFinancial.RegistryHeader)
                _ocur.CommitChanges()

                If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                End If
            End If


        Catch
            _ocur.Rollback()
            '------------------------------------TESZT MIATT ----------------------------------------->>
            Throw New Exception("*Hiba történt az ÁFA bontás során, a művelet nem hajtódott végre. A kellemetlenségért elnézést kérünk!")
            '------------------------------------TESZT MIATT -----------------------------------------<<
        End Try
    End Sub

    Private Sub aGLAccountsReadyforBookEntry_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLAccountsReadyforBookEntry.Execute
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceVATRows As InvoiceVATRows

        Dim _RegistryRowsFinancial As RegistryRowsFinancial
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim i As Long = 0

        RaiseEvent InitWork(1, 1, e.SelectedObjects.Count)
        For Each _GLAccountRowsforBookEntry In e.SelectedObjects
            RaiseEvent DoWork()
            _InvoiceRows = _uow.GetObjectByKey(Of InvoiceRows)(_GLAccountRowsforBookEntry.Oid)
            If _InvoiceRows IsNot Nothing Then
                _InvoiceRows.ReadyforBookEntryRows = True
                _InvoiceRows.InvoiceHeader.ReadyforBookEntry = True
            End If
            _InvoiceVATRows = _uow.GetObjectByKey(Of InvoiceVATRows)(_GLAccountRowsforBookEntry.Oid)
            If _InvoiceVATRows IsNot Nothing Then
                _InvoiceVATRows.ReadyforBookEntryRows = True
                _InvoiceVATRows.InvoiceHeader.ReadyforBookEntry = True
            End If
            _RegistryRowsFinancial = _uow.GetObjectByKey(Of RegistryRowsFinancial)(_GLAccountRowsforBookEntry.Oid)
            If _RegistryRowsFinancial IsNot Nothing Then
                _RegistryRowsFinancial.F_ReadyforBookEntryRows = True
                _RegistryRowsFinancial.RegistryHeader.F_ReadyforBookEntry = True
            End If
            _RegistryRowsFinancialControlling = _uow.GetObjectByKey(Of RegistryRowsFinancialControlling)(_GLAccountRowsforBookEntry.Oid)
            If _RegistryRowsFinancialControlling IsNot Nothing Then
                _RegistryRowsFinancialControlling.F_ReadyforBookEntryRows = True
                _RegistryRowsFinancialControlling.RegistryHeader.F_ReadyforBookEntry = True
            End If
            If i Mod 100 = 0 Then _uow.CommitChanges()
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If

    End Sub

    Private Sub aVATMerge_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aVATMerge.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccountRowsforBookEntry As GLAccountRowsforBookEntry

        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RegistryRowsFinancial As RegistryRowsFinancial = Nothing
        Dim _RegistryRowsFinancial_Base As RegistryRowsFinancial = Nothing
        Dim i As Long = 0


        Dim _RegistryRowsFinancial_Collection As New ArrayList

        For Each _GLAccountRowsforBookEntry In View.SelectedObjects
            _RegistryRowsFinancial = _uow.GetObjectByKey(Of RegistryRowsFinancial)(_GLAccountRowsforBookEntry.Oid)
            If _RegistryRowsFinancial IsNot Nothing Then
                If i = 0 Then
                    _RegistryRowsFinancial_Base = _RegistryRowsFinancial
                    i += 1
                Else
                    If _RegistryRowsFinancial.RegistryHeader.Oid = _RegistryRowsFinancial_Base.RegistryHeader.Oid And _
                        _RegistryRowsFinancial.VAT.Oid = _RegistryRowsFinancial_Base.VAT.Oid Then
                        _RegistryRowsFinancial_Collection.Add(_RegistryRowsFinancial)
                    End If
                End If
            End If
        Next

        For Each _RegistryRowsFinancial In _RegistryRowsFinancial_Collection
            _RegistryRowsFinancial_Base.BruttoAmount += _RegistryRowsFinancial.BruttoAmount
        Next
        For Each _RegistryRowsFinancial In _RegistryRowsFinancial_Collection
            _uow.Delete(_RegistryRowsFinancial)
        Next

        _uow.CommitChanges()

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub

End Class
