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.Data.Filtering
Imports DevExpress.Xpo
Imports System.Data.SqlClient
Imports System.Configuration
Imports DevExpress.ExpressApp.Editors
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Model
Imports DevExpress.ExpressApp.Utils


Public Class GLAccountsVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected selection As ArrayList
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork()
    Public Sub New()
        MyBase.New()
        selection = New ArrayList()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Sub DisableAll()
        Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aToTable_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aToTableD_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aBackToRowEdit_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aFinal_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aFinalAndNew_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aViewPCIDW_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of GLAccountsVC).aGetRegistryNumber_GLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aPCICheckGLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of GLAccountsVC).aCloneGLAccounts.Active.SetItemValue("", False)
        If View.Id = "GLAccounts_DetailView" Then
            DisableAll()
            Dim _GLRows As GLRows
            Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
            AddHandler _GLAccounts.Changed, AddressOf GLAccounts_Changed
            For Each _GLRows In _GLAccounts.GLRows
                _GLAccounts.row_ConnectInfo = _GLRows.ConnectInfo
                _GLAccounts.row_Customer = _GLRows.Customer
                _GLAccounts.row_DateVAT = _GLRows.DateVAT
                Exit For
            Next
            If _GLAccounts Is Nothing Then
            Else
                If _GLAccounts.IsEditable = True And _GLAccounts.IsStorno = False Then
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aToTable_GLAccounts.Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aToTableD_GLAccounts.Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aBackToRowEdit_GLAccounts.Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aFinal_GLAccounts.Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aFinalAndNew_GLAccounts.Active.SetItemValue("", True)
                    If _GLAccounts.RegistryNumber = "" Then
                        Frame.GetController(Of GLAccountsVC).aGetRegistryNumber_GLAccounts.Active.SetItemValue("", True)
                    End If
                ElseIf _GLAccounts.IsEditable = False And _GLAccounts.IsStorno = False Then
                    Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", True)
                    Frame.GetController(Of GLAccountsVC).aGetRegistryNumber_GLAccounts.Active.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "GLAccounts_ListView_Editable" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False) 'Akkor sem klónozható a gyári cloneactionnal, ha még szerkeszthető?
            Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", False)
        End If
        If View.Id = "Vivaldi_GLAccounts_ListView" Or View.Id = "Corner_GLAccounts_ListView" Then
            Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", False)

        End If
        If View.Id = "GLAccounts_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aViewPCIDW_GLAccounts.Active.SetItemValue("", True)
            Frame.GetController(Of GLAccountsVC).aPCICheckGLAccounts.Active.SetItemValue("", True)
            Frame.GetController(Of GLAccountsVC).aCloneGLAccounts.Active.SetItemValue("", True)
        End If
        '------------------- LISTVIEW -----------------------------------------------------------------------------------------------------
        If View.Id Like "GLHeader_GLRows_ListView_Accounts" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "GLAccounts_ListView_Deleted" Then
            Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", False)
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If

            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", True)
        Frame.GetController(Of GLAccountsVC).aToTable_GLAccounts.Active.SetItemValue("", True)
        Frame.GetController(Of GLAccountsVC).aToTableD_GLAccounts.Active.SetItemValue("", True)
        Frame.GetController(Of GLAccountsVC).aBackToRowEdit_GLAccounts.Active.SetItemValue("", True)
        Frame.GetController(Of GLAccountsVC).aFinal_GLAccounts.Active.SetItemValue("", True)
        Frame.GetController(Of GLAccountsVC).aFinalAndNew_GLAccounts.Active.SetItemValue("", True)

        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", True)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", True)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)

        Frame.GetController(Of GLAccountsVC).aCloneGLAccounts.Active.SetItemValue("", False)
        If View.Id = "GLAccounts_DetailView" Then
            Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
            RemoveHandler _GLAccounts.Changed, AddressOf GLAccounts_Changed
        End If

    End Sub
    Sub GLAccounts_Changed(sender As Object, e As DevExpress.Xpo.ObjectChangeEventArgs)

        If e.PropertyName = "CurrencyRate" Then
            Dim _GLAccounts As GLAccounts = TryCast(sender, GLAccounts)
            _GLAccounts.CurrencyRateVAT = e.NewValue
        End If

    End Sub
    Private Sub aToTable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_GLAccounts.Execute

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _GLHeader As GLHeader = TryCast(View.SelectedObjects(0), GLHeader)
        Dim _GLRows As GLRows
        Dim _NettoHUF As Double = 0
        Dim _NettoDEV As Double = 0
        Dim _BruttoHUF As Double = 0
        Dim _VATHUF As Double = 0
        Dim _VATDEV As Double = 0
        Dim _BruttoDEV As Double
        Dim _GUID As Guid = Guid.NewGuid

        If _GLAccounts IsNot Nothing Then
            ' -- PartnerType vizsgálata majd ez alapján a főkönyvi tartozik/kövezel beállítása ------------------------
            If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
                _GLAccounts.PartnerType = ePartnerType.eReceivables
                _GLHeader.row_CreditChartOfAccounts = _GLAccounts.row_ChartOfAccounts
                _GLHeader.row_DebitChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
            Else
                _GLAccounts.PartnerType = ePartnerType.ePayabels
                _GLHeader.row_CreditChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
                _GLHeader.row_DebitChartOfAccounts = _GLAccounts.row_ChartOfAccounts
            End If

            ' -- Deviza vizsgálat, ha nem Forint akkor átváltja ---------------------------------------------------------
            If _GLHeader.CurrencyName.ShortName = "HUF" Then
                _BruttoHUF = _GLHeader.row_AmountHUF
                If _GLAccounts.row_VAT IsNot Nothing Then
                    If _GLAccounts.row_VAT.InversState = True Then
                        _VATHUF = Math.Round(_BruttoHUF * _GLHeader.row_VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero)
                        _NettoHUF = _BruttoHUF
                        _BruttoHUF = _NettoHUF + _VATHUF
                    Else
                        _VATHUF = Math.Round(_BruttoHUF / (1 + _GLHeader.row_VAT.KeyValue) * _GLHeader.row_VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                        _NettoHUF = _BruttoHUF - _VATHUF
                    End If
                Else
                    _VATHUF = 0
                    _NettoHUF = _BruttoHUF - _VATHUF
                End If
            Else
                _BruttoDEV = _GLHeader.row_AmountDEV
                If _GLAccounts.row_VAT IsNot Nothing Then
                    If _GLAccounts.row_VAT.InversState = True Then
                        _VATDEV = Math.Round(_BruttoDEV * _GLHeader.row_VAT.InversKeyValue, 2, MidpointRounding.AwayFromZero)
                        _NettoDEV = _BruttoDEV
                        _BruttoDEV = _NettoDEV + _VATDEV
                    Else
                        _VATDEV = Math.Round(_BruttoDEV / (1 + _GLHeader.row_VAT.KeyValue) * _GLHeader.row_VAT.KeyValue, 2, MidpointRounding.AwayFromZero)
                        _NettoDEV = _BruttoDEV - _VATDEV
                    End If
                Else
                    _VATDEV = 0
                    _NettoDEV = _BruttoDEV - _VATDEV
                End If

                _VATHUF = Math.Round(_VATDEV * _GLAccounts.CurrencyRateVAT, 0, MidpointRounding.AwayFromZero)
                _NettoHUF = Math.Round(_NettoDEV * _GLHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)
                _BruttoHUF = _NettoHUF + _VATHUF

            End If

            ' -- ÁFA vizsgálata --------------------------------------------------------------------------------------------
            ' -- Létrehoz egy sort a nettó összegre ------------------------------------------------------------------------------------------

            _GLRows = _ocur.CreateObject(Of GLRows)()
            _GLRows.PartnerType = _GLAccounts.PartnerType
            If _GLRows.PartnerType = ePartnerType.eReceivables Then
                _GLRows.DebitAmountHUF = _NettoHUF
                _GLRows.DebitAmountDEV = _NettoDEV
            Else
                _GLRows.CreditAmountHUF = _NettoHUF
                _GLRows.CreditAmountDEV = _NettoDEV
            End If
            _GLRows.AddGLRows(_GLHeader, _GLHeader.row_DebitChartOfAccounts, _GLHeader.row_CreditChartOfAccounts,
                              _NettoDEV, _NettoHUF, _GLHeader.row_LiquidAssets,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _GLHeader.row_VAT,
                              _GLHeader.row_NoteRows, _GLHeader.row_UniqueObjects,
                              _GLHeader.row_Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
            '-- Létrehoz egy sort az ÁFÁ-ra ---------------------------------------------------------------------------------------------------
            If _GLAccounts.row_VAT IsNot Nothing Then
                If _GLAccounts.row_VAT IsNot Nothing Then
                    If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
                        _GLHeader.row_CreditChartOfAccounts = _GLAccounts.row_VAT.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        _GLHeader.row_DebitChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                    Else
                        _GLHeader.row_CreditChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
                        _GLHeader.row_DebitChartOfAccounts = _GLAccounts.row_VAT.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
                    End If
                End If
                If _GLAccounts.row_VAT.InversState = True Then
                    If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
                    Else
                        _GLHeader.row_CreditChartOfAccounts = _GLAccounts.row_VAT.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        _GLHeader.row_DebitChartOfAccounts = _GLAccounts.row_VAT.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
                    End If
                End If
                _GLRows = _ocur.CreateObject(Of GLRows)()
                _GLRows.PartnerType = _GLAccounts.PartnerType
                If _GLRows.PartnerType = ePartnerType.eReceivables Then
                    _GLRows.DebitAmountHUF = _VATHUF
                    _GLRows.DebitAmountDEV = _VATDEV
                Else
                    _GLRows.CreditAmountHUF = _VATHUF
                    _GLRows.CreditAmountDEV = _VATDEV
                End If

                _GLRows.AddGLRows(_GLHeader, _GLHeader.row_DebitChartOfAccounts, _GLHeader.row_CreditChartOfAccounts,
                                  _VATDEV, _VATHUF, _GLHeader.row_LiquidAssets,
                                  _GLAccounts.Customers, _GLHeader.row_DateVAT, _GLHeader.row_VAT, _GLHeader.row_NoteRows,
                                  _GLHeader.row_UniqueObjects, _GLHeader.row_Controlling, _GLHeader.row_JobNumberObjects,
                                  _GLHeader.row_CostPlace, _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo,
                                  eGLRowsTypeVAT.eVATRow, _GUID, eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
            End If
            _ocur.CommitChanges()

            '// Fókusz beállítása !!!!!!!!!!!!!!!!!!!!!!----------------------------------------------------------------
            Dim _Action As IModelAction
            Dim _IModelActionExtender As IModelActionExtender
            For Each _Action In Application.Model.ActionDesign.Actions
                If _Action.Id = e.Action.Id Then
                    _IModelActionExtender = TryCast(_Action, IModelActionExtender)
                    If _IModelActionExtender IsNot Nothing Then
                        If String.IsNullOrEmpty(_IModelActionExtender.FocusItem) = False Then
                            Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem(_IModelActionExtender.FocusItem)
                            If ItemToFocus IsNot Nothing Then
                                Dim ItemControl As Object
                                ItemControl = ItemToFocus.Control
                                ItemControl.Focus()
                            End If
                        End If
                    End If
                    Exit For
                End If
            Next
            '--------------------------------------------------------------------------------------------------------
            View.Refresh()
        End If

    End Sub
    Private Sub aFinal_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinal_GLAccounts.Execute
        GLAccounts_Final(e)
        View.Close(False)
    End Sub
    Private Sub aMakeEditable_GLAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMakeEditable_GLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Van As Boolean = False
        Dim _GLAccounts As GLAccounts = View.SelectedObjects(0)

        If _GLAccounts IsNot Nothing Then
            If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
                If _GLAccounts.ImportedInvoice Is Nothing Then
                    _GLAccounts.IsEditable = True
                End If
            End If
            If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                If _GLAccounts.ImportedRegistryHeader IsNot Nothing And _GLAccounts.IsRegistry = False Then

                Else
                    _GLAccounts.IsEditable = True
                End If

            End If
            _ocur.CommitChanges()
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLAccounts_DetailView", True, _onew.GetObject(_GLAccounts))
        End If
    End Sub
    Private Sub aFinalAndNew_GLAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalAndNew_GLAccounts.Execute
        GLAccounts_Final(e)

        Dim _GLAccounts_Old As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLAccounts As GLAccounts = _onew.CreateObject(Of GLAccounts)()

        If _GLAccounts_Old IsNot Nothing Then
            _GLAccounts.PartnerType = _GLAccounts_Old.PartnerType
        End If

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, TryCast(View.Model, Model.IModelDetailView).Id, True, _GLAccounts)
        e.ShowViewParameters.TargetWindow = TargetWindow.Current
    End Sub
#Region "SIS"
    Private Sub GLAccounts_Final(ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RegistryHeader As RegistryHeader = Nothing
        Dim _GLRows As GLRows

        If View.SelectedObjects.Count > 0 Then
            Dim _GLAccounts As GLAccounts = TryCast(e.SelectedObjects(0), GLAccounts)
            For Each _GLAccounts In e.SelectedObjects

                If _GLAccounts.CurrencyName.ShortName <> "HUF" Then
                    If _GLAccounts.CurrencyRate <= 1 Then
                        Throw New Exception("*Nincs megadva az árfolyam !")
                    End If
                    If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                        If _GLAccounts.CurrencyRateVAT <= 1 Then
                            Throw New Exception("*Nincs megadva az ÁFA árfolyam !")
                        End If
                    End If
                End If
                If _GLAccounts.GLRows.Count <= 0 Then
                    Throw New Exception("*Nincs rögzített sor !")
                End If
                _GLAccounts.IsEditable = False
                For Each _GLRows In _GLAccounts.GLRows
                    _GLRows.ConnectInfo = _GLAccounts.row_ConnectInfo
                    _GLRows.DateVAT = _GLAccounts.row_DateVAT
                    If _GLRows.GLHeader.CurrencyName.ShortName <> "HUF" Then
                        If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                            If _GLRows.GLRowsTypeVAT <> eGLRowsTypeVAT.eVATRow Then
                                _GLRows.AmountHUF = Math.Round(Math.Round(_GLAccounts.CurrencyRate, 2, MidpointRounding.AwayFromZero) * Math.Round(_GLRows.AmountDEV, 2, MidpointRounding.AwayFromZero), 0, MidpointRounding.AwayFromZero)
                            End If
                            If _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eVATRow Then
                                _GLRows.AmountHUF = Math.Round(Math.Round(_GLAccounts.CurrencyRateVAT, 2, MidpointRounding.AwayFromZero) * Math.Round(_GLRows.AmountDEV, 2, MidpointRounding.AwayFromZero), 0, MidpointRounding.AwayFromZero)
                            End If
                        Else
                            _GLRows.AmountHUF = Math.Round(Math.Round(_GLAccounts.CurrencyRate, 2, MidpointRounding.AwayFromZero) * Math.Round(_GLRows.AmountDEV, 2, MidpointRounding.AwayFromZero), 0, MidpointRounding.AwayFromZero)
                        End If
                    End If
                Next
                _GLAccounts.RecalculateTotal()
                If _GLAccounts.IsRegistry Then
                    If _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                        _RegistryHeader = _GLAccounts.ImportedRegistryHeader
                    End If
                    _RegistryHeader = _GLAccounts.CreateRegistry(_ocur, _RegistryHeader)
                    If _RegistryHeader IsNot Nothing Then
                        _GLAccounts.RegistryNumber = _RegistryHeader.RegistryNumber
                        _GLAccounts.ImportedRegistryHeader = _RegistryHeader
                        _RegistryHeader.F_GLAccounts = _GLAccounts
                    End If
                End If

                If _GLAccounts.IsControlling_InCash Then
                    '// Vegyes könyveléssel egy pénzügyi sor könyvelése !
                    Update_InCash_Mixed(_ocur, _GLAccounts)
                End If

                _ocur.CommitChanges()
                GLFunctions.ReconfigurePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.PartnerType, _GLAccounts.ConnectInfo)
            Next
            _ocur.CommitChanges()
        End If
    End Sub
    Private Sub Update_InCash_Mixed(_ocur As Xpo.XPObjectSpace, _GLAccounts As GLAccounts)
        'Beállítja a kp. számláknál a pénztári fizetést is egy vegyes bizonylattal
        Dim _GLMixed As GLMixed = _ocur.CreateObject(Of GLMixed)()
        Dim _GLRows As GLRows = Nothing
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts As ChartOfAccounts = Nothing

        If _GLAccounts.GLMixed_InCash IsNot Nothing Then
            _ocur.Delete(_GLAccounts.GLMixed_InCash.GLRows)
            _ocur.Delete(_GLAccounts.GLMixed_InCash)
        End If

        _GLMixed.DateCreated = _GLAccounts.DateCreated
        _GLMixed.CustomersFrom = _GLAccounts.CustomersFrom
        _GLMixed.DateExecution = _GLAccounts.DateExecution
        _GLMixed.DocumentNumber = _GLAccounts.DocumentNumber & "#PAY"
        _GLMixed.IsEditable = False
        _GLMixed.DatePayment = _GLAccounts.DatePayment

        _GLRows = _ocur.CreateObject(Of GLRows)()
        _GLRows.GLHeader = TryCast(_GLMixed, GLHeader)

        _GLRows.AmountHUF = _GLAccounts.AmountHUFBrutto
        _GLRows.AmountDEV = _GLAccounts.AmountDEVBrutto
        _GLRows.Customer = _GLAccounts.Customers
        _GLRows.ConnectInfo = _GLAccounts.ConnectInfo
        _GLRows.PartnerType = _GLAccounts.PartnerType

        If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
            _CreditChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
            _DebitChartOfAccounts = _GLAccounts.Controlling_InCash.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
            _GLRows.InAmountHUF = _GLRows.AmountHUF
            _GLRows.InAmountDEV = _GLRows.AmountDEV
        ElseIf _GLAccounts.PartnerType = ePartnerType.ePayabels Then
            _DebitChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
            _CreditChartOfAccounts = _GLAccounts.Controlling_InCash.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
            _GLRows.OutAmountHUF = _GLRows.AmountHUF
            _GLRows.OutAmountDEV = _GLRows.AmountDEV
        End If
        _GLRows.DebitChartOfAccounts = _DebitChartOfAccounts
        _GLRows.CreditChartOfAccounts = _CreditChartOfAccounts
        _GLAccounts.GLMixed_InCash = _GLMixed
    End Sub

#End Region
    Private Sub aToTableD_GLAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_GLAccounts.Execute
        'Törli a táblázatban lévő sort
        Dim _GUID As Guid
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _GLRows_ListEditor As ListPropertyEditor
        Dim _GLRows As GLRows
        Dim i As Long
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _GLRows_ListEditor = TryCast(View, DetailView).FindItem("GLRows")

        _GLRows = TryCast(_GLRows_ListEditor.ListView.CurrentObject, GLRows)
        If _GLRows IsNot Nothing Then
            _GUID = _GLRows.GLRowsGUID
            selection.Clear()
            For Each _GLRows In _GLAccounts.GLRows
                If _GLRows.GLRowsGUID = _GUID Then
                    selection.Add(_GLRows)
                End If
            Next
            For i = 0 To selection.Count - 1
                _GLRows = TryCast(selection(i), GLRows)
                _GLRows.Delete()
            Next
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aImportReceivables_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aImportReceivables.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceVATRows As InvoiceVATRows
        Dim _GLRows As GLRows
        Dim _GLAccounts As GLAccounts

        For Each _InvoiceHeader In View.SelectedObjects
            _GLRows = _ocur.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customers=? and GLHeader.DocumentNumber=? and PartnerType=0",
                                                                       _InvoiceHeader.CustomersFrom, _InvoiceHeader.Customers, _InvoiceHeader.DocumentNumber), True)
            If _GLRows Is Nothing Then
                _GLAccounts = _ocur.CreateObject(Of GLAccounts)()
                '-- ÁFA sorok könyvelése
                For Each _InvoiceVATRows In _InvoiceHeader.InvoiceVATRows

                Next

                '-- Nettó sorok könyvleése LINQ-kel !!!
                Dim invoices = New XPQuery(Of InvoiceRows)(_ocur.Session)
                Dim query = From invoice In invoices Where invoice.InvoiceHeader.Oid = _InvoiceHeader.Oid
                            Group invoice By invoice.Controlling, invoice.CostHolder, invoice.UniqueObjects,
                                             invoice.JobNumberObjects, invoice.Controlling.ChartOfAccounts Into g = Group
                            Select New With {Key .Controlling = Controlling,
                                             Key .CostHolder = CostHolder,
                                             Key .UniqueObjects = UniqueObjects,
                                             Key .JobNumberObjects = JobNumberObjects,
                                             Key .AmountNettoHUF = g.Sum(Function(inv) (inv.ListPriceHUF - inv.DiscountPriceHUF) * inv.QTT),
                                             Key .AmountNettoDEV = g.Sum(Function(inv) (inv.ListPriceDEV - inv.DiscountPriceDEV) * inv.QTT)}
                For Each item In query

                Next

            End If



        Next

    End Sub
    Private Sub aBackToRowEdit_GLAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_GLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _GLRows_ListEditor As ListPropertyEditor
        Dim _GLRows As GLRows
        Dim _GUID As Guid

        _GLRows_ListEditor = TryCast(View, DetailView).FindItem("GLRows")
        _GLRows = TryCast(_GLRows_ListEditor.ListView.CurrentObject, GLRows)

        _GUID = _GLRows.GLRowsGUID
        _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow

        If _GLRows IsNot Nothing Then
            _GLRows = _ocur.FindObject(Of GLRows)(CriteriaOperator.Parse("GLRowsGUID=? and GLRowsTypeVAT=0", _GUID))
            If _GLRows IsNot Nothing Then
                If _GLRows.PartnerType = ePartnerType.ePayabels Then
                    _GLAccounts.row_ChartOfAccounts = _GLRows.DebitChartOfAccounts
                Else
                    _GLAccounts.row_ChartOfAccounts = _GLRows.CreditChartOfAccounts
                End If
                _GLAccounts.row_UniqueObjects = _GLRows.UniqueObjects
                _GLAccounts.row_CostHolder = _GLRows.CostHolder
                _GLAccounts.row_CostPlace = _GLRows.CostPlace
                _GLAccounts.row_Controlling = _GLRows.Controlling
                _GLAccounts.row_NoteRows = _GLRows.NoteRows
                _GLAccounts.row_VAT = _GLRows.VAT
                _GLAccounts.row_ConnectInfo = _GLAccounts.ConnectInfo
                Dim _GLRows_Collection As New XPCollection(Of GLRows)(_ocur.Session, CriteriaOperator.Parse("GLRowsGUID=?", _GUID))
                _GLAccounts.row_AmountDEV = 0
                _GLAccounts.row_AmountHUF = 0
                For Each _GLRows In _GLRows_Collection
                    _GLAccounts.row_AmountDEV += _GLRows.AmountDEV
                Next
                For Each _GLRows In _GLRows_Collection
                    _GLAccounts.row_AmountHUF += _GLRows.AmountHUF
                Next
                View.Refresh()
            End If
        End If
    End Sub

    Private Sub aViewPCIDW_GLAccounts_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPCIDW_GLAccounts.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)

        If _GLAccounts IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _GLAccounts.CustomersFrom,
                              _GLAccounts.Customers, _GLAccounts.PartnerType,
                              _GLAccounts.DocumentNumber)
        End If
    End Sub

    Private Sub aGetRegistryNumber_GLAccounts_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGetRegistryNumber_GLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _RegistryHeader As RegistryHeader

        If _GLAccounts IsNot Nothing Then
            If _GLAccounts.RegistryNumber = "" Then
                _GLAccounts.RecalculateTotal()
                If _GLAccounts.AmountHUFBrutto <> 0 Then
                    _RegistryHeader = _GLAccounts.CreateRegistry(_ocur, _RegistryHeader)
                    If _RegistryHeader IsNot Nothing Then
                        _GLAccounts.RegistryNumber = _RegistryHeader.RegistryNumber
                        _GLAccounts.ImportedRegistryHeader = _RegistryHeader
                        _RegistryHeader.F_GLAccounts = _GLAccounts
                    End If
                End If
                _ocur.CommitChanges()
            Else
                Throw New Exception("*0 összeggel nem lehet iktatni !")
            End If
        End If
    End Sub

    Private Sub aOpenAccountsPdf_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenAccountsPdf.Execute
        If View.SelectedObjects.Count > 0 Then
            Try
                Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
                Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)

                If _GLAccounts.DocumentFile Is Nothing Then
                    If _GLAccounts.ImportedInvoice IsNot Nothing Then
                        ViewAttachments.ViewAttachments(_ocur, Frame, _GLAccounts.ImportedInvoice.CustomersFrom, _GLAccounts.ImportedInvoice.Customers, _GLAccounts.ImportedInvoice.DocumentNumber, "")
                    ElseIf _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                        ViewAttachments.ViewAttachments(_ocur, Frame, _GLAccounts.ImportedRegistryHeader.CustomersFrom, _GLAccounts.ImportedRegistryHeader.Customers, _GLAccounts.ImportedRegistryHeader.DocumentNumber, "")
                    Else
                        Throw New Exception("*Nincs dokumentum !")
                    End If
                Else
                    Dim _FileData As FileData = _ocur.CreateObject(Of FileData)()

                    _FileData = _GLAccounts.DocumentFile

                    Dim _FileName As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempPath, _FileData.FileName)
                    Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)

                    _FileData.SaveToStream(_StreamFile)
                    _StreamFile.Close()
                    Process.Start(_FileName)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Throw New Exception("*Nincs egy kijelölt sor sem!")
        End If
    End Sub

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

        Dim _GLAccounts As GLAccounts

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLAccounts In View.SelectedObjects
            RaiseEvent DoWork()
            If _GLAccounts IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.PartnerType, _GLAccounts.ConnectInfo)
            End If
        Next
        RaiseEvent FinalWork()

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

    Private Sub aCloneGLAccounts_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloneGLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLAccounts As GLAccounts = _onew.GetObject(TryCast(View.SelectedObjects(0), GLAccounts))
        Dim _OldGuid As Guid
        Dim _GUID As Guid = Guid.NewGuid

        If _GLAccounts Is Nothing Then
            Throw New Exception("*Nincs kiválasztott elem!")
        End If

        Dim _GLAccounts_New As GLAccounts = _onew.CreateObject(Of GLAccounts)()

        _GLAccounts_New.GLDocumentType = eGLDocumentType.eGLAccount
        _GLAccounts_New.CustomersFrom = _GLAccounts.CustomersFrom
        _GLAccounts_New.CurrencyName = _GLAccounts.CurrencyName
        _GLAccounts_New.CurrencyRate = _GLAccounts.CurrencyRate
        _GLAccounts_New.DateCreated = _GLAccounts.DateCreated
        _GLAccounts_New.DateExecution = _GLAccounts.DateExecution
        _GLAccounts_New.DatePayment = _GLAccounts.DatePayment
        _GLAccounts_New.NoteHeader = _GLAccounts.NoteHeader
        _GLAccounts_New.IsEditable = True
        _GLAccounts_New.IsStorno = False
        _GLAccounts_New.PartnerType = _GLAccounts.PartnerType
        _GLAccounts_New.PaymentOption = _GLAccounts.PaymentOption
        _GLAccounts_New.CurrencyRateVAT = _GLAccounts.CurrencyRateVAT
        _GLAccounts_New.Customers = _GLAccounts.Customers
        _GLAccounts_New.CustomerBankAccounts = _GLAccounts.CustomerBankAccounts
        _GLAccounts_New.HeaderInfo = _GLAccounts.HeaderInfo
        _GLAccounts_New.ConnectInfo = _GLAccounts.ConnectInfo

        If _GLAccounts.GLRows.Count > 0 Then
            _GLAccounts.GLRows.Sorting().Add(New SortProperty("GLRowsGUID", DB.SortingDirection.Ascending))

            For Each _GLRows As GLRows In _GLAccounts.GLRows
                Dim _GLRows_New As GLRows = _onew.CreateObject(Of GLRows)()

                _GLRows_New.GLHeader = _GLAccounts_New
                _GLRows_New.DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
                _GLRows_New.CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
                _GLRows_New.AmountHUF = _GLRows.AmountHUF
                _GLRows_New.AmountDEV = _GLRows.AmountDEV
                _GLRows_New.Customer = _GLRows.Customer
                _GLRows_New.DateVAT = _GLRows.DateVAT
                _GLRows_New.VAT = _GLRows.VAT
                _GLRows_New.NoteRows = _GLRows.NoteRows
                _GLRows_New.UniqueObjects = _GLRows.UniqueObjects
                _GLRows_New.Controlling = _GLRows.Controlling
                _GLRows_New.JobNumberObjects = _GLRows.JobNumberObjects
                _GLRows_New.CostPlace = _GLRows.CostPlace
                _GLRows_New.CostHolder = _GLRows.CostHolder
                _GLRows_New.ConnectInfo = _GLRows.ConnectInfo
                _GLRows_New.PartnerType = _GLRows.PartnerType
                _GLRows_New.GLRowsTypeVAT = _GLRows.GLRowsTypeVAT

                If _OldGuid <> _GLRows.GLRowsGUID Then
                    _GUID = Guid.NewGuid
                End If
                _GLRows_New.GLRowsGUID = _GUID
                _OldGuid = _GLRows.GLRowsGUID

            Next
        End If

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLAccounts_DetailView", True, _GLAccounts_New)

    End Sub

    Private Sub aCreateMixedPCI_GLAccount_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aCreateMixedPCI_GLAccount.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _GLAccountMixedPCI As GLAccountMixedPCI = _onew.CreateObject(Of GLAccountMixedPCI)()

        e.View = Application.CreateDetailView(_onew, "GLAccountMixedPCI_DetailView", True, _GLAccountMixedPCI)
    End Sub

    Private Sub aCreateMixedPCI_GLAccount_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aCreateMixedPCI_GLAccount.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)

        Dim _GLAccountMixedPCI As GLAccountMixedPCI = e.PopupWindow.View.SelectedObjects(0)
        Dim _GLAccounts As GLAccounts = Nothing
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing
        Dim _ChartOfAccounts As ChartOfAccounts = Nothing
        Dim _GLRows As GLRows = Nothing
        Dim _PCIGroup As PCIGroup = Nothing
        Dim _GLMixed As GLMixed = Nothing
        Dim _GLMixedRows As GLRows = Nothing
        Dim _GLHeader As GLHeader
        Dim _GUID As Guid = Nothing

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLAccounts In View.SelectedObjects
            RaiseEvent DoWork()
            _GLAccounts = _onew.GetObjectByKey(Of GLAccounts)(_GLAccounts.Oid)
            If _GLAccounts.PartnerType = ePartnerType.eReceivables And
               _GLAccounts.PaymentOption.PayMode = ePayMode.InCash And
               _GLAccountMixedPCI.ChartOfAccounts.Children.Count = 0 Then

                _PCIGroup = _onew.FindObject(Of PCIGroup)(CriteriaOperator.Parse("PartnerType=? and CustomersFrom=? and Customers=? and ConnectInfo=?", _GLAccounts.PartnerType, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.ConnectInfo))
                If _PCIGroup IsNot Nothing And _PCIGroup.BallanceHUF <> 0 Then
                    For Each _GLRows In _GLAccounts.GLRows

                        _GLMixed = _onew.CreateObject(Of GLMixed)()
                        _GLMixed.DateCreated = _GLAccounts.DateExecution
                        _GLMixed.DateExecution = _GLAccounts.DateExecution
                        _GLMixed.CustomersFrom = _GLAccounts.CustomersFrom
                        _GLMixed.DatePayment = _GLAccounts.DateExecution
                        _GLMixed.CurrencyName = _GLAccounts.CurrencyName
                        _GLMixed.CurrencyRate = _GLAccounts.CurrencyRate
                        _GLMixed.IsEditable = False
                        _GLMixed.DocumentNumber = _GLAccounts.ConnectInfo & " -> technikai pontozása"
                        _GLMixed.IsStorno = False
                        _GLMixed.NoteHeader = "Technikai pontozás "

                        _GLHeader = TryCast(_GLMixed, GLHeader)
                        _GLMixedRows = _onew.CreateObject(Of GLRows)()
                        _GLMixedRows.InAmountHUF = _GLAccounts.AmountHUFBrutto
                        _GLMixedRows.InAmountDEV = _GLAccounts.AmountDEVBrutto
                        _GUID = Guid.NewGuid
                        _GLMixedRows.AddGLRows(_GLHeader, _onew.GetObject(_GLAccountMixedPCI.ChartOfAccounts), _GLRows.DebitChartOfAccounts,
                                          _GLAccounts.AmountDEVBrutto, _GLAccounts.AmountHUFBrutto, Nothing,
                                          _GLAccounts.Customers, _GLAccounts.DateExecution, Nothing,
                                          "", Nothing, Nothing, Nothing, Nothing, Nothing,
                                          _GLAccounts.ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID, eTransactionType.ePartnerCredit, eCashType.eNormal, ePartnerType.eReceivables)
                        _onew.CommitChanges()

                        '// Pontozás újra !!!

                        GLFunctions.ReconfigurePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, ePartnerType.eReceivables, _GLAccounts.ConnectInfo)

                        Exit For

                    Next
                End If
            End If
        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork()
    End Sub

    Private Sub aToTable_GLAccounts_Telephone_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aToTable_GLAccounts_Telephone.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLAccounts As GLAccounts = _onew.GetObject(TryCast(View.SelectedObjects(0), GLAccounts))
        If _GLAccounts Is Nothing Then Exit Sub


        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(RegistryTelephone))
        Dim _RegistryTelephone As RegistryTelephone
        Dim _Phones_Collection As New XPCollection(Of Phones)(_onew.Session, CriteriaOperator.Parse(""))

        Dim _Phones As Phones
        Dim _RegistryTelephoneParameters As RegistryTelephoneParameters = _onew.FindObject(Of RegistryTelephoneParameters)(CriteriaOperator.Parse("CustomersFrom.Oid=?", _GLAccounts.CustomersFrom.Oid))



        For Each _Phones In _Phones_Collection
            _RegistryTelephone = _onew.FindObject(Of RegistryTelephone)(CriteriaOperator.Parse("GLAccounts=? and UniqueObjects=?", _GLAccounts, _Phones.UniqueObjects))
            If _RegistryTelephone Is Nothing Then
                _RegistryTelephone = _onew.CreateObject(Of RegistryTelephone)()
            End If
            _RegistryTelephone.UniqueObjects = _Phones.UniqueObjects
            _RegistryTelephone.RegistryTelephoneParameters = _RegistryTelephoneParameters
            _RegistryTelephone.GLAccounts = _GLAccounts
            _CollectionSource.Add(_RegistryTelephone)
        Next
        _onew.CommitChanges()   'ezt át kell nézni!!!!!!

        _CollectionSource.BeginUpdateCriteria()
        _CollectionSource.Criteria("ByTelephone") = CriteriaOperator.Parse("GLAccounts.Oid=?", _GLAccounts.Oid) 'PersistentCriteriaEvaluationBehavior.InTransaction
        _CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("RegistryTelephone_ListView", _CollectionSource, False)
    End Sub

    Private Sub aToTable_GLAccounts_Telephone_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aToTable_GLAccounts_Telephone.Execute
        '//Telefonszámla rögzítése
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _RegistryTelephone As RegistryTelephone

        Dim _ListView As ListView = TryCast(e.PopupWindow.View, ListView)

        '// Ellenőrzések
        If _GLAccounts Is Nothing Then Exit Sub
        If _GLAccounts.PartnerType = ePartnerType.eReceivables Then Exit Sub


        Dim _GLHeader As GLHeader = TryCast(_GLAccounts, GLHeader)
        Dim _GLRows As GLRows
        Dim _CreditChartOfAccounts As ChartOfAccounts = _GLAccounts.Customers.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eIn)
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing

        Dim _NettoHUF As Double = 0
        Dim _BruttoHUF As Double = 0
        Dim _VATHUF As Double = 0
        Dim _VAT As VAT
        Dim _GUID As Guid
        Dim _UniqueObjects As UniqueObjects
        Dim _Controlling As Controlling

        If _ListView IsNot Nothing Then
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)


            For Each _RegistryTelephone In _ListView.CollectionSource.List
                ''//Először az ÁFÁ-s tételek rögzítése
                If _RegistryTelephone.Amount_Brutto_INTERNET > 0 Then
                    _GUID = New Guid
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT)

                    _BruttoHUF = _RegistryTelephone.Amount_Brutto_INTERNET
                    _VATHUF = Math.Round(_BruttoHUF / (1 + _VAT.KeyValue) * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _NettoHUF = _BruttoHUF - _VATHUF

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_INTERNET)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    '//ÁFA sor
                    _DebitChartOfAccounts = _VAT.GetChartOfAccounts(_GLAccounts.row_DateVAT.Year, eVATMode.eIn)
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _VATHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, Nothing,
                              Nothing, Nothing, Nothing,
                              Nothing, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eVATRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
                End If
                If _RegistryTelephone.Amount_Brutto_OTHER_DEFAULTVAT > 0 Then
                    _GUID = New Guid
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT)

                    _BruttoHUF = _RegistryTelephone.Amount_Brutto_OTHER_DEFAULTVAT
                    _VATHUF = Math.Round(_BruttoHUF / (1 + _VAT.KeyValue) * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _NettoHUF = _BruttoHUF - _VATHUF

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_OTHER_DEFAULTVAT)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    '//ÁFA sor
                    _DebitChartOfAccounts = _VAT.GetChartOfAccounts(_GLAccounts.row_DateVAT.Year, eVATMode.eIn)
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _VATHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, Nothing,
                              Nothing, Nothing, Nothing,
                              Nothing, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eVATRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
                End If
                If _RegistryTelephone.Amount_Brutto_OTHER_NOVAT > 0 Then
                    _GUID = New Guid
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT)

                    _BruttoHUF = _RegistryTelephone.Amount_Brutto_OTHER_NOVAT
                    _VATHUF = Math.Round(_BruttoHUF / (1 + _VAT.KeyValue) * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _NettoHUF = _BruttoHUF - _VATHUF

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_OTHER_NOVAT)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    '//ÁFA sor
                    _DebitChartOfAccounts = _VAT.GetChartOfAccounts(_GLAccounts.row_DateVAT.Year, eVATMode.eIn)
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _VATHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, Nothing,
                              Nothing, Nothing, Nothing,
                              Nothing, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eVATRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
                End If
                If _RegistryTelephone.Amount_Brutto_PARKING > 0 Then
                    _GUID = New Guid
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT)

                    _BruttoHUF = _RegistryTelephone.Amount_Brutto_PARKING
                    _VATHUF = Math.Round(_BruttoHUF / (1 + _VAT.KeyValue) * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _NettoHUF = _BruttoHUF - _VATHUF

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_PARKING)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    '//ÁFA sor
                    _DebitChartOfAccounts = _VAT.GetChartOfAccounts(_GLAccounts.row_DateVAT.Year, eVATMode.eIn)
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _VATHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, Nothing,
                              Nothing, Nothing, Nothing,
                              Nothing, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eVATRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
                End If
                If _RegistryTelephone.Amount_Brutto_TELEPHON > 0 Then
                    _GUID = New Guid
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT)

                    _BruttoHUF = Math.Round(_RegistryTelephone.Amount_Brutto_TELEPHON * 0.7, 0, MidpointRounding.AwayFromZero)
                    _VATHUF = Math.Round(_BruttoHUF / (1 + _VAT.KeyValue) * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _NettoHUF = _BruttoHUF - _VATHUF

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _DebitChartOfAccounts = _VAT.GetChartOfAccounts(_GLAccounts.row_DateVAT.Year, eVATMode.eIn)
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _VATHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, Nothing,
                              Nothing, Nothing, Nothing,
                              Nothing, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eVATRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT)
                    _NettoHUF = _RegistryTelephone.AmountNetto_TELEPHON_80

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_TELEPHON_80)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)

                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _VAT = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT)
                    _NettoHUF = _RegistryTelephone.AmountNetto_TELEPHON_20

                    _UniqueObjects = _ocur.GetObject(_RegistryTelephone.UniqueObjects)
                    _Controlling = _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_TELEPHON_20)
                    _DebitChartOfAccounts = _Controlling.GetChartOfAccounts(_GLAccounts.DateExecution.Year, False)
                    '// Nettó sor
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _NettoHUF, Nothing,
                              _GLAccounts.Customers, _GLHeader.row_DateVAT, _VAT,
                              _GLHeader.row_NoteRows, _UniqueObjects,
                              _Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace,
                              _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID,
                              eTransactionType.eNotSet, eCashType.ePartner, _GLAccounts.PartnerType)
                End If
            Next
        End If

        _ocur.CommitChanges()
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        View.Refresh()
    End Sub

    Private Sub aGenerateOpenClosePCIRecord_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGenerateOpenClosePCIRecord.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLAccountsOpenClosePCIPopup As GLAccountsOpenClosePCIPopup = _onew.CreateObject(Of GLAccountsOpenClosePCIPopup)()

        e.View = Application.CreateDetailView(_onew, "GLAccountsOpenClosePCIPopup_DetailView", True, _GLAccountsOpenClosePCIPopup)

    End Sub

    Private Sub aGenerateOpenClosePCIRecord_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGenerateOpenClosePCIRecord.Execute
        Dim _GLAccountsOpenClosePCIPopup As GLAccountsOpenClosePCIPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), GLAccountsOpenClosePCIPopup)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _GLAccounts As GLAccounts

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)

        For Each _GLAccounts In View.SelectedObjects
            RaiseEvent DoWork()
            MakeOpenClosePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.PartnerType, _GLAccounts.ConnectInfo, _GLAccountsOpenClosePCIPopup.YearClose, _GLAccountsOpenClosePCIPopup.GLRowsTypeSelect)
        Next
        RaiseEvent FinalWork()
    End Sub

    Public Sub MakeOpenClosePCI(_uow As UnitOfWork, _CustomersFrom As CustomersFrom, _Customers As Customers, _PartnerType As ePartnerType, _ConnectInfo As String, _YearClose As Long, _GLRowsTypeSelect As eGLRowsTypeSelect)
        '// Az eljárás elkészít egy záró és egy nyitó tételt az adott pontozásra vonatkozóan 491 -> Partner illetve 492->partner szinten
        '// ConnectInfo +"-" + záróév +" ZÁRÁS"
        '// ConnectInfo +"-" + záróév +" NYITÁS"
        '// Az adatokat a kartonból veszi !!!

        Dim _AccountNumber_Partner As String
        Dim _GLMixed_Close As GLMixed = Nothing
        Dim _GLMixed_Open As GLMixed = Nothing

        Dim _DebitChartOfAccounts_Open As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts_Open As ChartOfAccounts = Nothing

        Dim _DebitChartOfAccounts_Close As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts_Close As ChartOfAccounts = Nothing

        Dim _DebitAmountHUF As Double = 0
        Dim _CreditAmountHUF As Double = 0
        Dim _BallanceHUF As Double = 0

        Dim _GLRows_Open As GLRows = Nothing
        Dim _GLRows_Close As GLRows = Nothing

        _CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
        _Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)


        '// Ellenőrzés. Ha már van, ne csináljon semmit.
        _GLRows_Close = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customer=? and GLRowsType=2 and ConnectInfo=? and PartnerType=? and GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _CustomersFrom, _Customers, _ConnectInfo, _PartnerType, _YearClose))
        If _GLRows_Close IsNot Nothing Then
            Exit Sub
        End If


        If _PartnerType = ePartnerType.ePayabels Then 'Szállító
            _AccountNumber_Partner = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eIn).AccountNumber
            _CreditChartOfAccounts_Close = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='492' and AccountYear=?", _YearClose))
            _DebitChartOfAccounts_Close = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eIn)
            _DebitChartOfAccounts_Open = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491' and AccountYear=?", _YearClose + 1))
            _CreditChartOfAccounts_Open = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eIn)
        Else 'Vevő
            _AccountNumber_Partner = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eOut).AccountNumber
            _DebitChartOfAccounts_Close = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='492' and AccountYear=?", _YearClose))
            _CreditChartOfAccounts_Close = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eOut)
            _CreditChartOfAccounts_Open = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491' and AccountYear=?", _YearClose + 1))
            _DebitChartOfAccounts_Open = _Customers.CustomersGLParameters.GetChartOfAccounts(_YearClose, eVATMode.eOut)
        End If

        Dim _GLRows_xpquery = New XPQuery(Of GLRows)(_uow)

        Dim query = From _GLRows In _GLRows_xpquery
                    Where _GLRows.GLHeader.CustomersFrom.Oid = _CustomersFrom.Oid And
                          _GLRows.Customer.Oid = _Customers.Oid And
                          _GLRows.ConnectInfo = _ConnectInfo And
                          _GLRows.PartnerType = _PartnerType And
                          _GLRows.GLHeader.DateExecution.Year = _YearClose And
                          _GLRows.GLHeader.IsEditable = False And
                          _GLRows.GLRowsType <> eGLRowsType.eClose And
                          _GLRows.GLHeader IsNot Nothing And
                          (_GLRows.DebitChartOfAccounts.AccountNumber = _AccountNumber_Partner Or
                           _GLRows.CreditChartOfAccounts.AccountNumber = _AccountNumber_Partner)
                    Select New With {
                         Key .Customers = _GLRows.Customer,
                         Key .PartnerType = _GLRows.PartnerType,
                         Key .ConnectInfo = _GLRows.ConnectInfo,
                         Key .DebitChartOfAccount = _GLRows.DebitChartOfAccounts,
                         Key .CreditChartOfAccount = _GLRows.CreditChartOfAccounts,
                         Key .AmountHUF = _GLRows.AmountHUF}

        For Each item In query
            If item.DebitChartOfAccount.AccountNumber = _AccountNumber_Partner Then
                _DebitAmountHUF += item.AmountHUF
            End If
            If item.CreditChartOfAccount.AccountNumber = _AccountNumber_Partner Then
                _CreditAmountHUF += item.AmountHUF
            End If
        Next
        _BallanceHUF = _DebitAmountHUF - _CreditAmountHUF

        If Math.Round(_BallanceHUF) <> 0 Then
            '// ZÁRÓ TÉTEL beszúrása
            If _GLRowsTypeSelect = eGLRowsTypeSelect.eBoth Or _GLRowsTypeSelect = eGLRowsTypeSelect.eClose Then
                _GLRows_Close = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customer=? and GLRowsType=2 and ConnectInfo=? and PartnerType=? and GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _CustomersFrom, _Customers, _ConnectInfo, _PartnerType, _YearClose))
                If _GLRows_Close IsNot Nothing Then
                    _GLMixed_Close = _GLRows_Close.GLHeader
                Else
                    _GLMixed_Close = New GLMixed(_uow)
                    _GLMixed_Close.DateExecution = New Date(_YearClose, 12, 31)
                    _GLMixed_Close.GLDocumentType = eGLDocumentType.eGLMixed
                    _GLMixed_Close.DateCreated = _GLMixed_Close.DateExecution
                    _GLMixed_Close.CustomersFrom = _CustomersFrom
                    _GLMixed_Close.IsEditable = False
                End If
                If _GLRows_Close Is Nothing Then _GLRows_Close = New GLRows(_uow)
                _GLMixed_Close.DocumentNumber = _AccountNumber_Partner & " karton zárása " & _YearClose.ToString
                _GLMixed_Close.NoteHeader = _Customers.FullName & " partner " & _AccountNumber_Partner & " főkönyvi karton zárása a " & _YearClose.ToString & " évre."

                _GLRows_Close.NoteRows = _GLMixed_Close.NoteHeader
                _GLRows_Close.GLHeader = _GLMixed_Close
                _GLRows_Close.GLRowsType = eGLRowsType.eClose
                _GLRows_Close.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow

                Select Case _PartnerType
                    Case ePartnerType.ePayabels
                        _GLRows_Close.AmountHUF = _BallanceHUF * -1
                    Case ePartnerType.eReceivables
                        _GLRows_Close.AmountHUF = _BallanceHUF
                End Select

                _GLRows_Close.DebitChartOfAccounts = _DebitChartOfAccounts_Close
                _GLRows_Close.CreditChartOfAccounts = _CreditChartOfAccounts_Close
                _GLRows_Close.Customer = _Customers
                _GLRows_Close.PartnerType = _PartnerType
                _GLRows_Close.ConnectInfo = _ConnectInfo
            End If

            '//Nyitó tétel beszúrása
            If _GLRowsTypeSelect = eGLRowsTypeSelect.eBoth Or _GLRowsTypeSelect = eGLRowsTypeSelect.eOpen Then
                _GLRows_Open = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customer=? and GLRowsType=1 and ConnectInfo=? and PartnerType=? and GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _CustomersFrom, _Customers, _ConnectInfo, _PartnerType, _YearClose + 1))
                If _GLRows_Open IsNot Nothing Then
                    _GLMixed_Open = _GLRows_Open.GLHeader
                Else
                    _GLMixed_Open = New GLMixed(_uow)
                    _GLMixed_Open.DateExecution = New Date(_YearClose + 1, 1, 1)
                    _GLMixed_Open.GLDocumentType = eGLDocumentType.eGLMixed
                    _GLMixed_Open.DateCreated = _GLMixed_Open.DateExecution
                    _GLMixed_Open.CustomersFrom = _CustomersFrom
                    _GLMixed_Open.IsEditable = False

                End If
                If _GLRows_Open Is Nothing Then _GLRows_Open = New GLRows(_uow)
                _GLMixed_Open.DocumentNumber = _AccountNumber_Partner & " karton nyitása " & (_YearClose + 1).ToString
                _GLMixed_Open.NoteHeader = _Customers.FullName & " partner " & _AccountNumber_Partner & " főkönyvi karton nyitása a " & (_YearClose + 1).ToString & " évre."

                _GLRows_Open.NoteRows = _GLMixed_Open.NoteHeader
                _GLRows_Open.GLHeader = _GLMixed_Open
                _GLRows_Open.GLRowsType = eGLRowsType.eOpen
                _GLRows_Open.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow

                Select Case _PartnerType
                    Case ePartnerType.ePayabels
                        _GLRows_Open.AmountHUF = _BallanceHUF * -1
                    Case ePartnerType.eReceivables
                        _GLRows_Open.AmountHUF = _BallanceHUF
                End Select

                _GLRows_Open.DebitChartOfAccounts = _DebitChartOfAccounts_Open
                _GLRows_Open.CreditChartOfAccounts = _CreditChartOfAccounts_Open
                _GLRows_Open.Customer = _Customers
                _GLRows_Open.PartnerType = _PartnerType
                _GLRows_Open.ConnectInfo = _ConnectInfo
            End If
            _uow.CommitChanges()
        End If

    End Sub
    Public Sub RemoveOpenClosePCI(_uow As UnitOfWork, _CustomersFrom As Customers, _Customers As Customers, _PartnerType As ePartnerType, _ConnectInfo As String, _YearClose As Long)
        '// Az eljárás megkeresi és törli a nyitó-záró tételt az adott évre
        Dim _GLMixed_Close As GLMixed = Nothing
        Dim _GLMixed_Open As GLMixed = Nothing

        Dim _GLRows_Open As GLRows = Nothing
        Dim _GLRows_Close As GLRows = Nothing

        _CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
        _Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)

        _GLRows_Close = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customer=? and GLRowsType=2 and ConnectInfo=? and PartnerType=? and GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _CustomersFrom, _Customers, _ConnectInfo, _PartnerType, _YearClose))
        If _GLRows_Close IsNot Nothing Then
            _GLMixed_Close = _GLRows_Close.GLHeader
        End If
        _GLRows_Open = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? and Customer=? and GLRowsType=1 and ConnectInfo=? and PartnerType=? and GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _CustomersFrom, _Customers, _ConnectInfo, _PartnerType, _YearClose + 1))
        If _GLRows_Open IsNot Nothing Then
            _GLMixed_Open = _GLRows_Open.GLHeader
        End If

        If _GLMixed_Open IsNot Nothing Then
            _uow.Delete(_GLMixed_Open.GLRows)
            _uow.Delete(_GLMixed_Open)
        End If
        If _GLMixed_Close IsNot Nothing Then
            _uow.Delete(_GLMixed_Close.GLRows)
            _uow.Delete(_GLMixed_Close)
        End If
        _uow.CommitChanges()
    End Sub

    Private Sub aGenerateOpenClosePCIRecord_Storno_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGenerateOpenClosePCIRecord_Storno.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLAccountsOpenClosePCIPopup As GLAccountsOpenClosePCIPopup = _onew.CreateObject(Of GLAccountsOpenClosePCIPopup)()

        e.View = Application.CreateDetailView(_onew, "GLAccountsOpenClosePCIPopup_DetailView", True, _GLAccountsOpenClosePCIPopup)
    End Sub

    Private Sub aGenerateOpenClosePCIRecord_Storno_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGenerateOpenClosePCIRecord_Storno.Execute
        Dim _GLAccountsOpenClosePCIPopup As GLAccountsOpenClosePCIPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), GLAccountsOpenClosePCIPopup)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _GLAccounts As GLAccounts

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)

        For Each _GLAccounts In View.SelectedObjects
            RaiseEvent DoWork()
            RemoveOpenClosePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.PartnerType, _GLAccounts.ConnectInfo, _GLAccountsOpenClosePCIPopup.YearClose)
        Next
        RaiseEvent FinalWork()
    End Sub

    Private Sub aRestoreDeletedRecord_GLAccounts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aRestoreDeletedRecord_GLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLAccounts In View.SelectedObjects
            RaiseEvent DoWork()

            _GLAccounts.SetMemberValue("GCRecord", Nothing)
        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork()
    End Sub

    Private Sub aGetCurrencyRateGLAccounts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGetCurrencyRateGLAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLAccounts As GLAccounts = TryCast(View.SelectedObjects(0), GLAccounts)
        Dim _LiquidAssets As LiquidAssets = _ocur.CreateObject(Of LiquidAssets)

        If _GLAccounts IsNot Nothing Then
            Dim _CurrencyRate_MNB_Collection = New XPCollection(Of CurrencyRate)(_ocur.Session,
                            CriteriaOperator.Parse("CurrencyName=? and IsMNB=True and GetDate(DateValid)<=?",
                             _GLAccounts.CurrencyName, _GLAccounts.DateExecution))
            _CurrencyRate_MNB_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))
            If _CurrencyRate_MNB_Collection.Count > 0 Then
                For Each _CurrencyRate_MNB In _CurrencyRate_MNB_Collection
                    If _CurrencyRate_MNB.DateValid.Date = _GLAccounts.DateExecution.Date Then
                        _GLAccounts.CurrencyRate = _CurrencyRate_MNB.RateAverage
                        Exit For
                    End If
                    Exit For
                Next
            End If
        End If
    End Sub
End Class
