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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Model

Public Class GLCompensationVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected selection As ArrayList
    Public Sub New()
        MyBase.New()
        selection = New ArrayList()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of GLCompensationVC).aMakeEditable_GLCompensation.Active.SetItemValue("", False)

        If View.Id = "GLCompensation_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Dim _GLCompensation As GLCompensation = TryCast(View.CurrentObject, GLCompensation)
            If _GLCompensation Is Nothing Then
            Else
                If _GLCompensation.IsEditable = False Then
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aFinal_GLCompensation.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aFinalAndNew_GLCompensation.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aFinalAndNew_GLCompensation.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aToTable_GLCompensation.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aToTableD_GLCompensation.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aBackToRowEdit_GLCompensation.Active.SetItemValue("", False)
                End If
                If _GLCompensation.IsStorno = True Then
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of GLCompensationVC).aMakeEditable_GLCompensation.Active.SetItemValue("", False)
                End If
                If _GLCompensation.IsEditable = True And _GLCompensation.IsStorno = False Then

                End If
            End If
        End If

        If View.Id = "GLCompensation_ListView" Then
            Frame.GetController(Of GLCompensationVC).aMakeEditable_GLCompensation.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "GLCompensation_ListView_Editable" Then
            AddHandler Frame.GetController(Of DeleteObjectsViewController).DeleteAction.Executed, AddressOf GLCompensation_DeleteAction_Executed
        End If

        If View.Id Like "GLHeader_GLRows_ListView_Compensation" 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.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Dim nv As ListView = CType(View, ListView)
            Dim cs As PropertyCollectionSource = CType(nv.CollectionSource, PropertyCollectionSource)
            AddHandler cs.MasterObjectChanged, AddressOf cs_MasterObjectChanged

        End If

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "GLCompensation_DetailView" Then
            Frame.GetController(Of GLCompensationVC).aFinalAndNew_GLCompensation.Active.SetItemValue("", True)
            Frame.GetController(Of GLCompensationVC).aFinalAndNew_GLCompensation.Active.SetItemValue("", True)
            Frame.GetController(Of GLCompensationVC).aToTable_GLCompensation.Active.SetItemValue("", True)
            Frame.GetController(Of GLCompensationVC).aToTableD_GLCompensation.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aToTable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_GLCompensation.Execute
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _GLHeader As GLHeader = TryCast(_GLCompensation, GLHeader)
        Dim _GLRows As GLRows
        Dim _GUID As Guid = Guid.NewGuid()
        Dim _CashType As eCashType
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        _GLRows = _ocur.CreateObject(Of GLRows)()

        If _GLCompensation.row_TransactionType = eTransactionType.ePartnerCredit Or _GLCompensation.row_TransactionType = eTransactionType.ePartnerDebit Then
            _CashType = eCashType.ePartner
            If _GLCompensation.row_PartnerType = ePartnerType.eReceivables Then
                If _GLCompensation.row_TransactionType = eTransactionType.ePartnerCredit Then
                    _GLHeader.row_DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _GLHeader.row_CreditChartOfAccounts = _GLCompensation.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                ElseIf _GLCompensation.row_TransactionType = eTransactionType.ePartnerDebit Then
                    _GLHeader.row_DebitChartOfAccounts = _GLCompensation.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                    _GLHeader.row_CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                End If
            Else
                If _GLCompensation.row_TransactionType = eTransactionType.ePartnerCredit Then
                    _GLHeader.row_DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _GLHeader.row_CreditChartOfAccounts = _GLCompensation.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eIn)
                ElseIf _GLCompensation.row_TransactionType = eTransactionType.ePartnerDebit Then
                    _GLHeader.row_DebitChartOfAccounts = _GLCompensation.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eIn)
                    _GLHeader.row_CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                End If
            End If
        Else
            _CashType = eCashType.eNormal
            If _GLCompensation.row_TransactionType = eTransactionType.eNormalCredit Then
                _GLHeader.row_DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                _GLHeader.row_CreditChartOfAccounts = _GLCompensation.row_ChartOfAccounts
            Else
                _GLHeader.row_DebitChartOfAccounts = _GLCompensation.row_ChartOfAccounts
                _GLHeader.row_CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
            End If

        End If
        Select Case _GLCompensation.row_TransactionType
            Case eTransactionType.eNormalCredit, eTransactionType.ePartnerCredit
                _GLRows.CreditAmountHUF = _GLHeader.row_AmountHUF
                _GLRows.CreditAmountDEV = _GLHeader.row_AmountDEV
                _GLRows.InAmountHUF = _GLHeader.row_AmountHUF
                _GLRows.InAmountDEV = _GLHeader.row_AmountDEV
            Case eTransactionType.eNormalDebit, eTransactionType.ePartnerDebit
                _GLRows.DebitAmountHUF = _GLHeader.row_AmountHUF
                _GLRows.DebitAmountDEV = _GLHeader.row_AmountDEV
                _GLRows.OutAmountHUF = _GLHeader.row_AmountHUF
                _GLRows.OutAmountDEV = _GLHeader.row_AmountDEV
        End Select
        _GLRows.PartnerType = _GLCompensation.row_PartnerType

        _GLRows.AddGLRows(_GLHeader, _GLHeader.row_DebitChartOfAccounts, _GLHeader.row_CreditChartOfAccounts, _
                          _GLHeader.row_AmountDEV, _GLHeader.row_AmountHUF, _GLCompensation.LiquidAssets_Main, _
                          _GLHeader.row_Customer, _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, _
                          _GLCompensation.row_TransactionType, _CashType, _GLCompensation.row_PartnerType)
        _GLCompensation.RecalculateTotal()
        _ocur.CommitChanges()

        '--- Pontozás ellenőrzése !!!! ----------------------------------------------------------------------------------------------------------------
        Select Case _GLCompensation.row_TransactionType
            Case eTransactionType.ePartnerCredit, eTransactionType.ePartnerDebit
                Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
                GLFunctions.ReconfigurePCI(_uow_pci, _GLCompensation.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        End Select

        '----------------------------------------------------------------------------------------------------------------------------------------------

        _GLCompensation.row_NoteRows = ""
        _GLCompensation.row_AmountDEV = 0
        _GLCompensation.row_AmountHUF = 0
        _GLCompensation.row_PCIGroup = Nothing
        _GLCompensation.row_Customer = Nothing
        _GLCompensation.row_ConnectInfo = ""
        _GLCompensation.row_ChartOfAccounts = Nothing

        '// 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 Sub
    Private Sub aFinal_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinal_GLCompensation.Execute
        GLCompensation_Final(e)
        View.Close(False)
    End Sub
    Private Sub cs_MasterObjectChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim _GLCompensation As GLCompensation = TryCast((CType(sender, PropertyCollectionSource)).MasterObject, GLCompensation)
        If _GLCompensation.IsEditable = False Then
            Frame.GetController(Of GLCompensationVC).aToTable_GLCompensation.Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub aMakeEditable_GLCompensation_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMakeEditable_GLCompensation.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        If _GLCompensation IsNot Nothing Then

            _GLCompensation.IsEditable = True
            _ocur.CommitChanges()

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLCompensation_DetailView", True, _onew.GetObject(_GLCompensation))
        End If
    End Sub
    Private Sub aFinalAndNew_GLCompensation_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalAndNew_GLCompensation.Execute
        GLCompensation_Final(e)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLCompensation As GLCompensation = _onew.CreateObject(Of GLCompensation)()

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, TryCast(View.Model, Model.IModelDetailView).Id, True, _GLCompensation)
        e.ShowViewParameters.TargetWindow = TargetWindow.Current
    End Sub
#Region "SIS"
    Private Sub GLCompensation_Final(ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRows As GLRows
        If View.SelectedObjects.Count > 0 Then
            Dim _GLCompensation As GLCompensation = TryCast(e.SelectedObjects(0), GLCompensation)
            For Each _GLCompensation In e.SelectedObjects
                If _GLCompensation.GLRows.Count <= 0 Then
                    _ocur.Rollback()
                    Throw New Exception("*Nincs rögzített sor !")
                End If
                For Each _GLRows In _GLCompensation.GLRows
                    _GLRows.LiquidAssets = _GLCompensation.LiquidAssets_Main
                Next
                _GLCompensation.IsEditable = False
                If _GLCompensation.DocumentNumber Is Nothing Or _GLCompensation.DocumentNumber = "" Then
                    Dim _NumberGenerator As NumberGenerator = TryCast(_GLCompensation.LiquidAssets_Main.NumberGeneratorIn, NumberGenerator)
                    _GLCompensation.DocumentNumber = _NumberGenerator.GetNewNumber(_NumberGenerator).ToString
                End If
                _GLCompensation.Save()
                _ocur.CommitChanges()

                '// Az összes sorra vonatkozó pontozás újragenerálása, mert változhat a dátum !
                For Each _GLRows In _GLCompensation.GLRows
                    Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                    GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
                Next
            Next
        End If
    End Sub
#End Region
    Private Sub aToTableD_GLCompensation_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_GLCompensation.Execute
        'Törli a táblázatban lévő sort
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GUID As Guid
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _GLRows_ListEditor As ListPropertyEditor
        Dim _GLRows As GLRows
        Dim i As Long

        Dim _CustomersFrom As CustomersFrom
        Dim _Customers As Customers
        Dim _PartnerType As ePartnerType
        Dim _ConnectInfo As String

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

        _GLRows = TryCast(_GLRows_ListEditor.ListView.CurrentObject, GLRows)

        If _GLRows.GLRowsRateDiff IsNot Nothing Then
            Throw New Exception("*Nem lehet a sort törölni mert van árfolyam elszámolás !")
            Exit Sub
        End If

        If _GLRows IsNot Nothing Then
            _GUID = _GLRows.GLRowsGUID
            selection.Clear()
            For Each _GLRows In _GLCompensation.GLRows
                If _GLRows.GLRowsGUID = _GUID Then
                    selection.Add(_GLRows)
                End If
            Next
            For i = 0 To selection.Count - 1
                _GLRows = TryCast(selection(i), GLRows)
                If _GLRows.PartnerType = ePartnerType.ePayabels Or _GLRows.PartnerType = ePartnerType.eReceivables Then
                    _CustomersFrom = _GLCompensation.CustomersFrom
                    _Customers = _GLRows.Customer
                    _ConnectInfo = _GLRows.ConnectInfo
                    _PartnerType = _GLRows.PartnerType
                End If
                _GLRows.Delete()
                _ocur.CommitChanges()

                Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
                GLFunctions.ReconfigurePCI(_uow_pci, _CustomersFrom, _Customers, _PartnerType, _ConnectInfo)

            Next
            _GLCompensation.RecalculateTotal()
            View.Refresh()
        End If
    End Sub
    Private Sub aBackToRowEdit_GLCompensation_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_GLCompensation.Execute
        'Visszatölti a sort editáláshoz
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _GLRows_ListEditor As ListPropertyEditor
        Dim _GLRows As GLRows
        Dim _PCIGroup As PCIGroup

        Dim _CustomersFrom As CustomersFrom
        Dim _Customers As Customers
        Dim _PartnerType As ePartnerType
        Dim _ConnectInfo As String

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

        If _GLRows IsNot Nothing Then

            If _GLRows.GLRowsRateDiff IsNot Nothing Then
                Throw New Exception("*Nem lehet a sort törölni mert van árfolyam elszámolás !")
                Exit Sub
            End If

            _GLCompensation.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _GLCompensation.CurrencyName.ShortName))
            _PCIGroup = _ocur.FindObject(Of PCIGroup) _
            (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and PartnerType=? and ConnectInfo=? ", _GLRows.GLHeader.CustomersFrom, _
                                    _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo), True)
            If _PCIGroup IsNot Nothing Then
                _GLCompensation.row_PCIGroup = _PCIGroup
                _GLCompensation.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _PCIGroup.CurrencyName_Account.ShortName))
                If _GLCompensation.row_CurrencyName2 Is Nothing Then
                    _GLCompensation.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _GLCompensation.CurrencyName.ShortName))
                End If
            End If
            _GLCompensation.row_Customer = _GLRows.Customer
            _GLCompensation.row_ConnectInfo = _GLRows.ConnectInfo
            _GLCompensation.CurrencyName = _GLRows.GLHeader.CurrencyName
            _GLCompensation.CurrencyRate = _GLRows.GLHeader.CurrencyRate

            _GLCompensation.row_NoteRows = _GLRows.NoteRows
            _GLCompensation.CurrencyRate = _GLRows.GLHeader.CurrencyRate
            _GLCompensation.row_PartnerType = _GLRows.PartnerType
            _GLCompensation.row_TransactionType = _GLRows.TransactionType
            _GLCompensation.row_CurrencyRate2 = _GLRows.CurrencyRate2
            If _GLRows.InAmountHUF > 0 Then
                _GLCompensation.row_ChartOfAccounts = _GLRows.CreditChartOfAccounts
                _GLCompensation.row_AmountDEV2 = _GLRows.InAmountDEV2
            Else
                _GLCompensation.row_ChartOfAccounts = _GLRows.DebitChartOfAccounts
                _GLCompensation.row_AmountDEV2 = _GLRows.OutAmountDEV2
            End If

            Dim _GLRows_GUID_Collection As New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("GLRowsGUID=?", _GLRows.GLRowsGUID))
            Dim _AmountDEV As Double = 0
            Dim _AmountHUF As Double = 0
            For Each _GLRows In _GLRows_GUID_Collection
                _AmountDEV += _GLRows.AmountDEV
                _AmountHUF += _GLRows.AmountHUF
                selection.Add(_GLRows)
            Next
            _GLCompensation.row_AmountDEV = _AmountDEV
            _GLCompensation.row_AmountHUF = _AmountHUF

            For i = 0 To selection.Count - 1
                _GLRows = TryCast(selection(i), GLRows)
                If _GLRows.PartnerType = ePartnerType.ePayabels Or _GLRows.PartnerType = ePartnerType.eReceivables Then
                    _CustomersFrom = _GLCompensation.CustomersFrom
                    _Customers = _GLRows.Customer
                    _ConnectInfo = _GLRows.ConnectInfo
                    _PartnerType = _GLRows.PartnerType
                End If
                _GLRows.Delete()
                _ocur.CommitChanges()

                Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
                GLFunctions.ReconfigurePCI(_uow_pci, _CustomersFrom, _Customers, _PartnerType, _ConnectInfo)

            Next
        End If

        View.Refresh()
    End Sub

    Private Sub aPrintCompensation_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintCompensation.Execute
        Dim _GLCompensation As GLCompensation
        Try
            If e.SelectedObjects.Count > 0 Then
                _GLCompensation = TryCast(e.SelectedObjects.Item(0), GLCompensation)
                PrintCompensationDocument(_GLCompensation, TryCast(View.ObjectSpace, Xpo.XPObjectSpace))
            End If
        Catch

        End Try
    End Sub
    Sub PrintCompensationDocument(ByVal _GLCompensation As GLCompensation, ByVal _ocur As Xpo.XPObjectSpace)
        Dim _ReportData As ReportData
        Dim _GLHeader As GLHeader
        If _GLCompensation IsNot Nothing Then
            _GLHeader = TryCast(_GLCompensation, GLHeader)
            _ReportData = _GLCompensation.LiquidAssets_Main.ReportData
            If _ReportData IsNot Nothing Or _GLCompensation Is Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("GLHeader=?", _GLHeader), True)
            End If
        End If
    End Sub

    Private Sub aGLCompensationDivideRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLCompensationDivideRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRowsDivideH As GLRowsDivideH = _onew.CreateObject(Of GLRowsDivideH)()
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)

        If _GLRows IsNot Nothing Then
            If _GLRows.PartnerType = ePartnerType.ePayabels Or _GLRows.PartnerType = ePartnerType.eReceivables Then
                _GLRowsDivideH.GLRows_Oid = _GLRows.Oid
                _GLRowsDivideH.row_Customers = _onew.GetObject(_GLRows.Customer)
                _GLRowsDivideH.row_PartnerType = _GLRows.PartnerType
                e.View = Application.CreateDetailView(_onew, "GLRowsDivideH_DetailView_ConnectionDivide", False, _GLRowsDivideH)
            Else
                _GLRowsDivideH.GLRows_Oid = _GLRows.Oid
                e.View = Application.CreateDetailView(_onew, "GLRowsDivideH_DetailView", False, _GLRowsDivideH)
            End If
        End If
    End Sub

    Private Sub aGLCompensationDivideRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLCompensationDivideRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsDivideH As GLRowsDivideH = TryCast(e.PopupWindow.View.SelectedObjects(0), GLRowsDivideH)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)

        If _GLRows IsNot Nothing Then
            _GLRows.DivideRows(_ocur, _GLRows, _GLRowsDivideH, True)
        End If
    End Sub

    Private Sub aGenerateGLCompensationRateDiff_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateGLCompensationRateDiff.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCompensation As GLCompensation = TryCast(e.SelectedObjects(0), GLCompensation)

        For Each _GLCompensation In e.SelectedObjects
            If Not _GLCompensation.CurrencyName.ShortName = "HUF" Then
                _GLCompensation.Reconfigure_RateDiff(_ocur, _GLCompensation)
            End If
        Next
        _ocur.CommitChanges()
        View.Refresh()
    End Sub

    Private Sub GLCompensation_DeleteAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _GLCompensation As GLCompensation
        Dim _GLRows As GLRows
        Dim _GLRows_Collection As XPCollection(Of GLRows)

        For Each _GLCompensation In TryCast(e, SimpleActionExecuteEventArgs).SelectedObjects

            _GLRows_Collection = New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("PartnerType<>'eNotSet' and GLHeader.Oid=?", _GLCompensation.Oid), True)
            For Each _GLRows In _GLRows_Collection
                GLFunctions.ReconfigurePCI(_uow, _GLCompensation.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
            Next
        Next
    End Sub

    Private Sub aImportPCIGroupCompensation_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aImportPCIGroupCompensation.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(PCIGroup))


        Dim _P As PCIGroup
        If _GLCompensation IsNot Nothing Then

            _CollectionSource.Criteria.Clear()
            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria("1") = CriteriaOperator.Parse("CurrencyName_Account.ShortName=?", _GLCompensation.LiquidAssets_Main.CurrencyName.ShortName)
            _CollectionSource.Criteria("2") = CriteriaOperator.Parse("PartnerType=?", _GLCompensation.row_PartnerType)
            _CollectionSource.Criteria("3") = CriteriaOperator.Parse("CustomersFrom.Oid=?", _GLCompensation.CustomersFrom.Oid)
            If _GLCompensation.LiquidAssets_Main.CurrencyName.ShortName = "HUF" Then
                _CollectionSource.Criteria("4") = CriteriaOperator.Parse("BallanceHUF<>0")
            Else
                _CollectionSource.Criteria("4") = CriteriaOperator.Parse("BallanceDEV<>0")
            End If

            _CollectionSource.EndUpdateCriteria()

            e.View = Application.CreateListView("PCIGroup_ListView_Find", _CollectionSource, True)

        End If
    End Sub

    Private Sub aImportPCIGroupCompensation_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aImportPCIGroupCompensation.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGroup As PCIGroup
        Dim _PCIGroup_P As PCIGroup
        Dim _GLCompensation As GLCompensation = TryCast(View.SelectedObjects(0), GLCompensation)
        Dim _GLHeader As GLHeader = TryCast(_GLCompensation, GLHeader)
        Dim _GLRows As GLRows
        Dim _GUID As Guid
        Dim _CashType As eCashType
        Dim _DebitChartOfAccounts As ChartOfAccounts
        Dim _CreditChartOfAccounts As ChartOfAccounts
        Dim _TransactionType As eTransactionType
        If _GLCompensation Is Nothing Then Exit Sub


        For Each _PCIGroup_P In e.PopupWindow.View.SelectedObjects
            _PCIGroup = _ocur.GetObject(_PCIGroup_P)
            _GLRows = _ocur.CreateObject(Of GLRows)()
            _GUID = Guid.NewGuid()
            _CashType = eCashType.ePartner
            If _PCIGroup.PartnerType = ePartnerType.eReceivables Then
                If _PCIGroup.BallanceHUF > 0 Or _PCIGroup.BallanceDEV > 0 Then
                    _DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _CreditChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                    _TransactionType = eTransactionType.ePartnerCredit
                    _GLRows.CreditAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.CreditAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.DebitAmountHUF = 0
                    _GLRows.DebitAmountDEV = 0
                    _GLRows.InAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.InAmountDEV = _PCIGroup.BallanceDEV

                Else
                    _CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _DebitChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                    _TransactionType = eTransactionType.ePartnerDebit

                    _GLRows.DebitAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.DebitAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.CreditAmountHUF = 0
                    _GLRows.CreditAmountDEV = 0
                    _GLRows.OutAmountHUF = Math.Abs(_PCIGroup.BallanceHUF)
                    _GLRows.OutAmountDEV = Math.Abs(_PCIGroup.BallanceDEV)
                End If
            Else
                If _PCIGroup.BallanceHUF > 0 Or _PCIGroup.BallanceDEV > 0 Then
                    _DebitChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_PCIGroup.DateExecution_Account.Year, eVATMode.eIn)
                    _CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _TransactionType = eTransactionType.ePartnerDebit
                    _GLRows.DebitAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.DebitAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.CreditAmountDEV = 0
                    _GLRows.CreditAmountDEV = 0
                    _GLRows.OutAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.OutAmountDEV = _PCIGroup.BallanceDEV
                Else
                    _CreditChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_PCIGroup.DateExecution_Account.Year, eVATMode.eIn)
                    _DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    _TransactionType = eTransactionType.ePartnerCredit

                    _GLRows.CreditAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.CreditAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.DebitAmountHUF = 0
                    _GLRows.DebitAmountDEV = 0
                    _GLRows.InAmountHUF = Math.Abs(_PCIGroup.BallanceHUF)
                    _GLRows.InAmountDEV = Math.Abs(_PCIGroup.BallanceDEV)
                End If

            End If

            _GLRows.PartnerType = _PCIGroup.PartnerType
            _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _
                                Math.Abs(_PCIGroup.BallanceDEV), Math.Abs(_PCIGroup.BallanceHUF), _GLCompensation.LiquidAssets_Main, _
                                _PCIGroup.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, _
                                _PCIGroup.ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID, _
                                _TransactionType, _CashType, _PCIGroup.PartnerType)
            _GLCompensation.RecalculateTotal()
            _GLCompensation.GetBallanceDEV(_GLCompensation.row_LiquidAssets, _GLCompensation.DateExecution)
            _GLCompensation.GetBallanceHUF(_GLCompensation.row_LiquidAssets, _GLCompensation.DateExecution)

            _ocur.CommitChanges()

            '--- Pontozás ellenőrzése !!!! ----------------------------------------------------------------------------------------------------------------
            Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
            GLFunctions.ReconfigurePCI(_uow_pci, _GLCompensation.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        Next
    End Sub
End Class