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.Data.Filtering
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Xpo
Imports DevExpress.Persistent.Base.Security
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Model

Public Class GLCassaVC
    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
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of GLCassaVC).aMakeEditable_GLCassa.Active.SetItemValue("", False)
        Frame.GetController(Of GLCassaVC).aPrintCassaDocument.Active.SetItemValue("", False)
        Frame.GetController(Of GLCassaVC).aRecalculateSelectedGLCassa.Active.SetItemValue("", False)
        Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", False)
        If View.Id = "GLCassa_ListView" Then '-- Lista nézete -----------------------------------------------------------------------------------
            Frame.GetController(Of GLCassaVC).aPrintCassaDocument.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", True)
            Frame.GetController(Of GLCassaVC).aMakeEditable_GLCassa.Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
        End If '---------------------------------------------------------------------------------------------------------------------------------

        If View.Id = "GLCassa_ListView_Editable" Then '-- Lista nézete -----------------------------------------------------------------------------------
            AddHandler Frame.GetController(Of DeleteObjectsViewController).DeleteAction.Executed, AddressOf GLCassa_DeleteAction_Executed
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "DocumentNumber=''"
        End If

        If View.Id Like "GLHeader_GLRows_ListView_Cassa" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().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

        If View.Id = "GLCassa_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Dim _GLCassa As GLCassa = TryCast(View.CurrentObject, GLCassa)
            If _GLCassa Is Nothing Then
            Else
                If _GLCassa.IsEditable = False Then
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aFinal_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aFinalAndNew_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aBackToRowEdit_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aToTable_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aToTableD_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", False)
                    If _GLCassa.DocumentNumber IsNot Nothing Then
                        If _GLCassa.DocumentNumber <> "" Then
                            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                        End If
                    End If
                End If
                If _GLCassa.IsStorno = True Then
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", False)
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "GLCassa_ListView" Then '-- Lista nézete -----------------------------------------------------------------------------------
            Frame.GetController(Of GLCassaVC).aPrintCassaDocument.Active.SetItemValue("", False)
            Frame.GetController(Of GLCassaVC).aRecalculateSelectedGLCassa.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", False)
            Frame.GetController(Of GLCassaVC).aMakeEditable_GLCassa.Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If '---------------------------------------------------------------------------------------------------------------------------------

        If View.Id = "GLCassa_ListView_Editable" Then '-- Lista nézete -----------------------------------------------------------------------------------
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)

        End If

        If View.Id Like "GLHeader_GLRows_ListView_Cassa" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)

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

        End If

        If View.Id = "GLCassa_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Dim _GLCassa As GLCassa = TryCast(View.CurrentObject, GLCassa)
            If _GLCassa Is Nothing Then
            Else
                If _GLCassa.IsEditable = False Then
                    Frame.GetController(Of GLCassaVC).aMakeEditable_GLCassa.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aFinal_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aFinalAndNew_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aBackToRowEdit_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aToTable_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aToTableD_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
                Else
                    Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", True)
                    If _GLCassa.DocumentNumber IsNot Nothing Then
                        If _GLCassa.DocumentNumber <> "" Then
                            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                        End If
                    End If
                End If
                If _GLCassa.IsStorno = True Then
                    Frame.GetController(Of GLCassaVC).aMakeEditable_GLCassa.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                    Frame.GetController(Of GLCassaVC).aStorno_GLCassa.Active.SetItemValue("", True)
                End If
            End If
        End If
    End Sub

    Private Sub aToTable_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_GLCassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _GLHeader As GLHeader = TryCast(View.SelectedObjects(0), GLHeader)
        Dim _GLRows As GLRows
        Dim _DebitChartOfAccounts As ChartOfAccounts
        Dim _CreditChartOfAccounts As ChartOfAccounts
        Dim _GLRowsTypeVAT As eGLRowsTypeVAT
        Dim _GLRowsGUID As Guid = Guid.NewGuid()
        Dim _AmountBruttoHUF As Double = 0
        Dim _AmountNettoHUF As Double = 0
        Dim _AmountVATHUF As Double = 0
        Dim _AmountBruttoDEV As Double = 0
        Dim _AmountNettoDEV As Double = 0
        Dim _AmountVATDEV As Double = 0
        Dim _CashType As eCashType
        Dim _TransactionType As eTransactionType
        Dim _PartnerType As ePartnerType


        If _GLCassa.row_PartnerType <> ePartnerType.eNotSet Then
            If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                If _GLCassa.CheckIfPartnerCashFlowsInMonthGreaterThan1500000(_ocur, _GLCassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _GLCassa.row_AmountHUF, 0) = False Then
                    If MsgBox(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "PartnerCashFlowsInMonthGreaterThan1500000")), _
                              MsgBoxStyle.DefaultButton2 + MsgBoxStyle.YesNoCancel + MsgBoxStyle.Exclamation, "") <> MsgBoxResult.Yes Then Exit Sub
                End If
            Else
                If _GLCassa.CheckIfPartnerCashFlowsInMonthGreaterThan1500000(_ocur, _GLCassa, _GLCassa.row_Customer, _GLCassa.DateExecution, 0, _GLCassa.row_AmountHUF) = False Then
                    If MsgBox(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "PartnerCashFlowsInMonthGreaterThan1500000")), _
                              MsgBoxStyle.DefaultButton2 + MsgBoxStyle.YesNoCancel + MsgBoxStyle.Exclamation, "") <> MsgBoxResult.Yes Then Exit Sub
                End If
            End If
        End If

        _GLRows = _ocur.CreateObject(Of GLRows)()

        If _GLCassa.CashMoveType = eCashMoveType.eIn Then
            _GLRows.InAmountHUF = _GLCassa.row_AmountHUF
            _GLRows.InAmountDEV = _GLCassa.row_AmountDEV
            _GLRows.InAmountDEV2 = _GLCassa.row_AmountDEV2
            _GLRows.CurrencyRate2 = _GLCassa.row_CurrencyRate2
            _DebitChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
            _TransactionType = eTransactionType.ePartnerCredit
            _GLRows.CreditAmountHUF = _GLCassa.row_AmountHUF
            _GLRows.CreditAmountDEV = _GLCassa.row_AmountDEV
            _GLRows.DebitAmountHUF = 0
            _GLRows.DebitAmountDEV = 0
            If _GLCassa.row_CashType = 0 Then 'Partner
                If _GLCassa.row_ConnectInfo <> "" Then
                    If _GLCassa.row_PartnerType = 0 Then
                        _PartnerType = _GLCassa.row_PartnerType
                        _CreditChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                    Else
                        _PartnerType = _GLCassa.row_PartnerType
                        _CreditChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                    End If
                Else
                    _CreditChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                End If
            Else
                _TransactionType = eTransactionType.eNormalCredit
                _PartnerType = ePartnerType.eNotSet
                _CreditChartOfAccounts = _GLCassa.row_ChartOfAccounts
            End If
        Else
            _GLRows.OutAmountHUF = _GLCassa.row_AmountHUF
            _GLRows.OutAmountDEV = _GLCassa.row_AmountDEV
            _GLRows.OutAmountDEV2 = _GLCassa.row_AmountDEV2
            _GLRows.CurrencyRate2 = _GLCassa.row_CurrencyRate2
            _DebitChartOfAccounts = Nothing
            _CreditChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
            _TransactionType = eTransactionType.ePartnerDebit
            _GLRows.CreditAmountHUF = 0
            _GLRows.CreditAmountDEV = 0
            _GLRows.DebitAmountHUF = _GLCassa.row_AmountHUF
            _GLRows.DebitAmountDEV = _GLCassa.row_AmountDEV
            If _GLCassa.row_CashType = 0 Then 'Partner
                If _GLCassa.row_ConnectInfo <> "" Then
                    If _GLCassa.row_PartnerType = 0 Then
                        _PartnerType = _GLCassa.row_PartnerType
                        _DebitChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                    Else
                        _PartnerType = _GLCassa.row_PartnerType
                        _DebitChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                    End If
                Else
                    _DebitChartOfAccounts = _GLCassa.row_Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                End If
            Else
                _TransactionType = eTransactionType.eNormalDebit
                _PartnerType = ePartnerType.eNotSet
                _DebitChartOfAccounts = _GLCassa.row_ChartOfAccounts
            End If
        End If

        _GLRowsGUID = Guid.NewGuid

        If _GLCassa.row_CashType = eCashType.ePartner Then
            _CashType = eCashType.ePartner
            _GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
            _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _GLCassa.row_AmountDEV, _
                      _GLHeader.row_AmountHUF, _GLCassa.LiquidAssets_Cassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _
                      Nothing, _GLHeader.row_NoteRows, Nothing, Nothing, _
                      Nothing, Nothing, Nothing, _GLCassa.row_ConnectInfo, Nothing, _
                      _GLRowsGUID, _TransactionType, _CashType, _
                      _GLCassa.row_PartnerType)
        ElseIf _GLCassa.row_CashType = eCashType.eNormal Or _GLCassa.row_CashType = eCashType.eCorrectionHUF Then
            _CashType = eCashType.eNormal
            If _GLCassa.row_VAT Is Nothing Then '--Nincs ÁFA sor
                _GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _GLCassa.row_AmountDEV, _
                          _GLHeader.row_AmountHUF, _GLCassa.LiquidAssets_Cassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _
                          _GLHeader.row_VAT, _GLHeader.row_NoteRows, _GLCassa.row_UniqueObjects, _GLHeader.row_Controlling, _
                          _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace, _GLCassa.row_CostHolder, _
                          _GLCassa.row_ConnectInfo, _GLRowsTypeVAT, _GLRowsGUID, _TransactionType, _CashType, _PartnerType)
            Else
                If _GLCassa.row_VAT.KeyValue = 0 Then '-- Nincs ÁFA sor
                    _GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _GLCassa.row_AmountDEV, _
                          _GLHeader.row_AmountHUF, _GLCassa.LiquidAssets_Cassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _
                          _GLHeader.row_VAT, _GLHeader.row_NoteRows, _GLCassa.row_UniqueObjects, _GLHeader.row_Controlling, _
                          _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace, _GLCassa.row_CostHolder, _
                          _GLCassa.row_ConnectInfo, _GLRowsTypeVAT, _GLRowsGUID, _TransactionType, _CashType, _PartnerType)
                Else '-- Van ÁFA sor !!!
                    '--1. Először a nettó sort kell megadni -----------------------------------------------------------------------

                    _AmountBruttoDEV = _GLCassa.row_AmountDEV
                    _AmountBruttoHUF = _GLCassa.row_AmountHUF

                    _AmountVATHUF = Math.Round((_AmountBruttoHUF / (_GLCassa.row_VAT.KeyValuePercent + 100)) * _GLCassa.row_VAT.KeyValuePercent, 0, MidpointRounding.AwayFromZero)
                    _AmountVATDEV = Math.Round((_AmountBruttoDEV / (_GLCassa.row_VAT.KeyValuePercent + 100)) * _GLCassa.row_VAT.KeyValuePercent, 0, MidpointRounding.AwayFromZero)

                    _AmountNettoDEV = _AmountBruttoDEV - _AmountVATDEV
                    _AmountNettoHUF = _AmountBruttoHUF - _AmountVATHUF

                    If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                        _GLRows.InAmountHUF = _AmountNettoHUF
                        _GLRows.InAmountDEV = _AmountNettoDEV

                    Else
                        _GLRows.OutAmountHUF = _AmountNettoHUF
                        _GLRows.OutAmountDEV = _AmountNettoDEV
                    End If

                    _GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _AmountNettoDEV, _
                          _AmountNettoHUF, _GLCassa.LiquidAssets_Cassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _
                          _GLHeader.row_VAT, _GLHeader.row_NoteRows, _GLCassa.row_UniqueObjects, _GLHeader.row_Controlling, _
                          _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace, _GLCassa.row_CostHolder, _
                          _GLCassa.row_ConnectInfo, _GLRowsTypeVAT, _GLRowsGUID, _TransactionType, _CashType, _PartnerType)
                    '--2. Most az ÁFA sort ----------------------------------------------------------------------------------------
                    '-- Kell egy új sor !
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                        _GLRows.InAmountHUF = _AmountVATHUF
                        _GLRows.InAmountDEV = _AmountVATDEV
                    Else
                        _GLRows.OutAmountHUF = _AmountVATHUF
                        _GLRows.OutAmountDEV = _AmountVATDEV
                    End If
                    _GLRowsTypeVAT = eGLRowsTypeVAT.eVATRow
                    If _GLCassa.row_VAT IsNot Nothing Then

                        If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                            _CreditChartOfAccounts = _GLCassa.row_VAT.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                            _DebitChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                        Else
                            _CreditChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                            _DebitChartOfAccounts = _GLCassa.row_VAT.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                        End If
                    End If
                    If _GLCassa.row_VAT.InversState = True Then
                        If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                        Else
                            _CreditChartOfAccounts = _GLCassa.row_VAT.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                            _DebitChartOfAccounts = _GLCassa.row_VAT.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                        End If
                    End If
                    _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _AmountVATDEV, _
                          _AmountVATHUF, _GLCassa.LiquidAssets_Cassa, _GLCassa.row_Customer, _GLCassa.DateExecution, _
                          _GLHeader.row_VAT, _GLHeader.row_NoteRows, _GLCassa.row_UniqueObjects, _GLHeader.row_Controlling, _
                          _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace, _GLCassa.row_CostHolder, _
                          _GLCassa.row_ConnectInfo, _GLRowsTypeVAT, _GLRowsGUID, _TransactionType, _CashType, _PartnerType)
                End If
            End If
        End If

        _ocur.CommitChanges()

        '-- Pontozás ellenőrzése GUID alapján ! ----------------------------------------------------------------------------------------------------------------------------------------
        Dim _GLRows_Collection As New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("GLRowsGUID=?", _GLRowsGUID))
        Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
        For Each _GLRows In _GLRows_Collection
            GLFunctions.ReconfigurePCI(_uow_pci, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        Next
        '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        '_GLCassa.row_NoteRows = ""
        _GLCassa.row_AmountDEV = 0
        _GLCassa.row_AmountHUF = 0
        _GLCassa.row_PCIGroup = Nothing
        _GLCassa.row_Customer = Nothing
        _GLCassa.row_ConnectInfo = ""
        '_GLCassa.row_ChartOfAccounts = Nothing
        _GLCassa.row_AmountDEV2 = 0
        _GLCassa.row_CurrencyRate2 = 0

        '// 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_GLCassa.Execute
        GLCassa_Final(e)
        View.Close(False)
    End Sub
    Private Sub aStorno_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStorno_GLCassa.Execute
        '-- Pénztári bizonylat stornírozása
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _GLRows As GLRows
        If _GLCassa IsNot Nothing Then

            For Each _GLRows In _GLCassa.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
                _GLRows.LiquidAssets = _GLCassa.LiquidAssets_Cassa
            Next


            If _GLCassa.IsStorno = False And _GLCassa.IsEditable = False Then
                Dim _IsNegativeDate As Date = _GLCassa.CheckIsNegativeifStorno(_ocur, _GLCassa)

                If _IsNegativeDate > #1/1/1980# Then
                    _ocur.Rollback()
                    Throw New Exception("*Ezzel a tranzakcióval a pénztár mínuszba menne a következő napon: " & Format(_IsNegativeDate, "yyyy.MM.dd"))
                    Exit Sub
                End If
                _GLCassa.IsStorno = True
                _GLCassa.TotalInAmountDEV = 0
                _GLCassa.TotalInAmountHUF = 0
                _GLCassa.TotalOutAmountDEV = 0
                _GLCassa.TotalOutAmountHUF = 0

                Dim _GLRows_Collection As New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("GLHeader.Oid=?", _GLCassa.Oid))
                _ocur.Delete(_GLRows_Collection)
                _ocur.CommitChanges()
                If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                End If
            End If
        End If
    End Sub
    Private Sub cs_MasterObjectChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim _GLCassa As GLCassa = TryCast((CType(sender, PropertyCollectionSource)).MasterObject, GLCassa)
        If _GLCassa.IsEditable = False Then
            Frame.GetController(Of GLCassaVC).aToTable_GLCassa.Active.SetItemValue("", False)
        End If

    End Sub
    Private Sub aMakeEditable_GLCassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMakeEditable_GLCassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLCassa_IsEditable As GLCassa
        If _GLCassa IsNot Nothing Then
            _GLCassa_IsEditable = _ocur.FindObject(Of GLCassa)(CriteriaOperator.Parse("IsEditable=True and DocumentNumber<>'' and LiquidAssets_Cassa=?", _GLCassa.LiquidAssets_Cassa), True)
            If _GLCassa_IsEditable IsNot Nothing Then
                Throw New Exception("*Nem lehet szerkesztésre beállítani, mert van már szerkeszthetésre vissza állított, korábban véglegesített bizonylat. Előbb ezt kell véglegesíteni !")
            End If

            _GLCassa.IsEditable = True
            _ocur.CommitChanges()

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLCassa_DetailView", True, _onew.GetObject(_GLCassa))
        End If
    End Sub
    Private Sub aFinalAndNew_GLCassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalAndNew_GLCassa.Execute
        GLCassa_Final(e)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLCassa_Finalized = _onew.GetObject(TryCast(e.SelectedObjects(0), GLCassa))
        Dim _GLCassa As GLCassa = _onew.CreateObject(Of GLCassa)()

        _GLCassa.CustomersFrom = _GLCassa_Finalized.CustomersFrom
        _GLCassa.LiquidAssets_Cassa = _GLCassa_Finalized.LiquidAssets_Cassa
        _GLCassa.CashHandler = _GLCassa_Finalized.CashHandler
        _GLCassa.DateExecution = _GLCassa_Finalized.DateExecution
        _GLCassa.DateCreated = _GLCassa_Finalized.DateExecution
        _GLCassa.DatePayment = _GLCassa_Finalized.DateExecution


        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, TryCast(View.Model, Model.IModelDetailView).Id, True, _GLCassa)
        e.ShowViewParameters.TargetWindow = TargetWindow.Current


    End Sub
#Region "SIS"
    Private Sub GLCassa_Final(ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        If View.SelectedObjects.Count > 0 Then
            Dim _GLCassa As GLCassa = TryCast(e.SelectedObjects(0), GLCassa)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _A As AuditedObjectWeakReference
            Dim AuditTrail As XPCollection(Of AuditDataItemPersistent)
            Dim _GLRows As GLRows
            AuditTrail = AuditedObjectWeakReference.GetAuditTrail(_ocur.Session, _GLCassa)



            If _GLCassa Is Nothing Then Exit Sub
            If _GLCassa.GLRows.Count <= 0 Then
                'Throw New Exception("*Nincs rögzített sor !")
                Throw New Exception(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "NoGLRows")))
            End If
            If _GLCassa.DocumentNumber Is Nothing Then _GLCassa.DocumentNumber = ""
            If _GLCassa.LiquidAssets_Cassa.NoTimeCheck <> True Then
                If _GLCassa.DocumentNumber = "" Then
                    Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("LiquidAssets_Cassa=? and CashMoveType=? and DateExecution > ? and IsEditable=False and GetYear(DateExecution)=?", _
                                                                                                                 _GLCassa.LiquidAssets_Cassa, _GLCassa.CashMoveType, DateAdd(DateInterval.Minute, 1439, _GLCassa.DateExecution.Date), _GLCassa.DateExecution.Date.Year))
                    If _GLCassa_Collection.Count > 0 Then
                        Throw New Exception(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "DateIsOlderThanLastDate")))
                    End If

                    If _GLCassa.DateExecution.Date > GetSQLTime(_ocur.Session).Date Then
                        Throw New Exception(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "DateIsNewerThanNow")))
                    End If
                End If

                '--Ellenőrzés, hogy nem megy e mínuszba !
                Dim _IsNegativeDate As Date = _GLCassa.CheckIsNegative(_ocur, _GLCassa.LiquidAssets_Cassa, _GLCassa)


                If _IsNegativeDate > #1/1/1980# Then
                    Throw New Exception("*Nem mehet a pénztár mínuszba ! Dátum : " & Format(_IsNegativeDate, "yyyy.MM.dd"))
                    Throw New Exception(String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\GLCassa", "CassaAmountGoToNegative"), _IsNegativeDate))
                End If
            End If

            If _GLCassa.DocumentNumber = "" And _GLCassa.LiquidAssets_Cassa.NoDocumentNumber <> True Then
                Dim _YearString As String = _GLCassa.DateExecution.Year.ToString
                If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                    _GLCassa.DocumentNumber = _GLCassa.LiquidAssets_Cassa.NumberGeneratorIn.GetNewNumber(_GLCassa.LiquidAssets_Cassa.NumberGeneratorIn, _YearString)
                Else
                    _GLCassa.DocumentNumber = _GLCassa.LiquidAssets_Cassa.NumberGeneratorOut.GetNewNumber(_GLCassa.LiquidAssets_Cassa.NumberGeneratorOut, _YearString)
                End If
            End If
            _GLCassa.IsEditable = False
            For Each _GLRows In _GLCassa.GLRows
                _GLRows.DateVAT = _GLCassa.DateExecution
            Next
            _ocur.CommitChanges()

            '// Az összes sorra vonatkozó pontozás újragenerálása, mert változhat a dátum !
            For Each _GLRows In _GLCassa.GLRows
                Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
            Next

            If _GLCassa.CurrencyName.ShortName <> "HUF" Then
                Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                GenerateCurrencyRateAverageStart(_uow, _GLCassa.DateExecution.Year, _GLCassa)
            End If

            '-- Bizonylat nyomtatása --------------------------------------------------------------------------------------
            _GLCassa.RecalculateTotal(_GLCassa, _ocur, True)
            _GLCassa.CreateRoundingRow(_GLCassa, _ocur)
            _GLCassa.RecalculateTotal(_GLCassa, _ocur, True)
            If _GLCassa.LiquidAssets_Cassa.NoPrint = False Then
                _GLCassa.PrintCassaDocument(_GLCassa, _ocur, Frame)
            End If
            '--------------------------------------------------------------------------------------------------------------
        Else
            '--- !!!!TESZT MIATT!!!! -------------------------------
            Throw New Exception("*A kifizetni kívánt összeg meghaladja a kasszában lévőt!!")
        End If
    End Sub
#End Region
    Private Sub aPrintCassaDocument_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintCassaDocument.Execute
        Dim _GLCassa As GLCassa
        Try
            If e.SelectedObjects.Count > 0 Then
                _GLCassa = TryCast(e.SelectedObjects.Item(0), GLCassa)
                _GLCassa.PrintCassaDocument(_GLCassa, TryCast(View.ObjectSpace, Xpo.XPObjectSpace), Frame)
            End If
        Catch

        End Try
    End Sub
    Private Sub aRecalculateSelectedGLCassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRecalculateSelectedGLCassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _GLCassa As GLCassa
        For Each _GLCassa In e.SelectedObjects
            _GLCassa.RecalculateTotal(_GLCassa, _ocur, True)
        Next
    End Sub
    Private Sub aBackToRowEdit_GLCassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_GLCassa.Execute
        'Visszatölti a sort editáláshoz
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        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

            _GLCassa.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _GLCassa.CurrencyName.ShortName))
            If _GLRows.CashType = eCashType.eNormal Then
                _GLCassa.row_CashType = eCashType.eNormal
                If _GLRows.InAmountHUF > 0 Then
                    _GLCassa.row_ChartOfAccounts = _GLRows.CreditChartOfAccounts
                Else
                    _GLCassa.row_ChartOfAccounts = _GLRows.DebitChartOfAccounts
                End If
            Else
                _GLCassa.row_CashType = eCashType.ePartner
                _PCIGroup = _ocur.FindObject(Of PCIGroup) _
                    (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and PartnerType=? and ConnectInfo=?", _
                                            _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _
                                            _GLRows.PartnerType, _GLRows.ConnectInfo), True)
                _GLCassa.row_PCIGroup = _PCIGroup
                If _PCIGroup.CurrencyName_Account IsNot Nothing Then
                    _GLCassa.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _PCIGroup.CurrencyName_Account.ShortName))
                    If _GLCassa.row_CurrencyName2 Is Nothing Then
                        _GLCassa.row_CurrencyName2 = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _GLCassa.CurrencyName.ShortName))
                    End If
                End If
            End If
            _GLCassa.row_Customer = _GLRows.Customer
            _GLCassa.row_ConnectInfo = _GLRows.ConnectInfo
            If _GLRows.PartnerType = ePartnerType.eReceivables Then 'Vevő
            ElseIf _GLRows.PartnerType = ePartnerType.ePayabels Then 'Szállító
            ElseIf _GLRows.PartnerType = ePartnerType.eNotSet Then 'Normál 

            End If

            _GLCassa.row_VAT = _GLRows.VAT
            If _GLRows.VAT IsNot Nothing Then
                If _GLRows.VAT.KeyValue > 0 Then

                End If
            End If
            _GLCassa.row_Controlling = _GLRows.Controlling
            _GLCassa.row_CostHolder = _GLRows.CostHolder
            _GLCassa.row_CostPlace = _GLRows.CostPlace
            _GLCassa.row_JobNumberObjects = _GLRows.JobNumberObjects
            _GLCassa.row_UniqueObjects = _GLRows.UniqueObjects
            _GLCassa.row_NoteRows = _GLRows.NoteRows

            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
            _GLCassa.row_AmountDEV = _AmountDEV
            _GLCassa.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 = _GLCassa.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

            View.Refresh()
        End If
    End Sub
    Private Sub aToTableD_GLCassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_GLCassa.Execute
        'Törli a táblázatban lévő sort
        Dim _GUID As Guid
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _GLRows_ListEditor As ListPropertyEditor
        Dim _GLRows As GLRows
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim i As Long

        Dim _CustomersFrom As CustomersFrom = Nothing
        Dim _Customers As Customers = Nothing
        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 _GLCassa.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 = _GLCassa.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
            View.Refresh()
        End If
    End Sub

    Private Sub aGLCassaDivideRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLCassaDivideRows.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 aGLCassaDivideRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLCassaDivideRows.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 aGenerateGLCassRateDiff_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateGLCassRateDiff.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa As GLCassa = TryCast(e.SelectedObjects(0), GLCassa)

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

    Private Sub GLCassa_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 _GLCassa As GLCassa
        Dim _GLRows As GLRows
        Dim _GLRows_Collection As XPCollection(Of GLRows)

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

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

    Private Sub aImportPCIGroupCassa_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImportPCIGroupCassa.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(PCIGroup))


        Dim _P As PCIGroup = Nothing
        If _GLCassa IsNot Nothing Then

            _CollectionSource.Criteria.Clear()
            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria("1") = CriteriaOperator.Parse("CurrencyName_Account.ShortName=?", _GLCassa.LiquidAssets_Cassa.CurrencyName.ShortName)
            _CollectionSource.Criteria("2") = CriteriaOperator.Parse("PartnerType=?", _GLCassa.row_PartnerType)
            _CollectionSource.Criteria("3") = CriteriaOperator.Parse("CustomersFrom.Oid=?", _GLCassa.CustomersFrom.Oid)
            If _GLCassa.LiquidAssets_Cassa.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 aImportPCIGroupCassa_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImportPCIGroupCassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGroup As PCIGroup
        Dim _PCIGroup_P As PCIGroup
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _GLHeader As GLHeader = TryCast(_GLCassa, 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 _GLCassa 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
                _DebitChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                _CreditChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                _TransactionType = eTransactionType.ePartnerCredit
            Else
                _DebitChartOfAccounts = _PCIGroup.Customers.CustomersGLParameters.GetChartOfAccounts(_PCIGroup.DateExecution_Account.Year, eVATMode.eIn)
                _CreditChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                _TransactionType = eTransactionType.ePartnerDebit
            End If

            Select Case _PCIGroup.PartnerType
                Case ePartnerType.eReceivables

                    _GLRows.CreditAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.CreditAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.DebitAmountHUF = 0
                    _GLRows.DebitAmountDEV = 0
                    _GLRows.InAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.InAmountDEV = _PCIGroup.BallanceDEV

                Case ePartnerType.ePayabels
                    _GLRows.DebitAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.DebitAmountDEV = _PCIGroup.BallanceDEV
                    _GLRows.CreditAmountDEV = 0
                    _GLRows.CreditAmountDEV = 0
                    _GLRows.OutAmountHUF = _PCIGroup.BallanceHUF
                    _GLRows.OutAmountDEV = _PCIGroup.BallanceDEV

            End Select
            _GLRows.PartnerType = _PCIGroup.PartnerType
            _GLRows.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, _
                              _PCIGroup.BallanceDEV, _PCIGroup.BallanceHUF, _GLCassa.LiquidAssets_Cassa, _
                              _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)

            _GLCassa.GetBallanceDEV(_GLCassa.row_LiquidAssets, _GLCassa.DateExecution)
            _GLCassa.GetBallanceHUF(_GLCassa.row_LiquidAssets, _GLCassa.DateExecution)

            _ocur.CommitChanges()

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

        Next
    End Sub

    Private Sub aGetCurrencyRateCassa_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGetCurrencyRateCassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa As GLCassa = TryCast(View.SelectedObjects(0), GLCassa)
        Dim _TransactionType As eTransactionType
        If _GLCassa IsNot Nothing Then
            If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                _TransactionType = eTransactionType.eNormalCredit
            ElseIf _GLCassa.CashMoveType = eCashMoveType.eOut Then
                _TransactionType = eTransactionType.eNormalDebit
            Else
                _TransactionType = eTransactionType.eNotSet
            End If
            _GLCassa.row_CurrencyRate = _GLCassa.LiquidAssets_Cassa.GetCurrencyRateDate(_ocur, _TransactionType, _GLCassa.LiquidAssets_Cassa, _GLCassa.DateExecution)
        End If
    End Sub

    Private Sub GenerateCurrencyRateAverageStart(_uow As UnitOfWork, _Year As Long, _GLCassa As GLCassa)


        Dim _GLRows_Collection As New XPCollection(Of GLRows) _
                    (PersistentCriteriaEvaluationBehavior.InTransaction, _uow, _
                    CriteriaOperator.Parse("LiquidAssets.Oid=? and GLHeader.IsEditable=False and GetDate(GLHeader.DateExecution)>=?", _
                    _GLCassa.LiquidAssets_Cassa.Oid, New Date(_Year, 1, 1)))

        Dim _GLRows As GLRows
        Dim _SumAmountDEV As Double = 0
        Dim _SumAmountHUF As Double = 0
        Dim _LiquidAssetsRate As LiquidAssetsRate = _uow.FindObject(Of LiquidAssetsRate)(CriteriaOperator.Parse("LiquidAssets.oid=? and GetYear(DateExecution)=?", _
                                                                                                                _GLCassa.LiquidAssets_Cassa.Oid, _
                                                                                                                _Year))
        If _LiquidAssetsRate IsNot Nothing Then
            _SumAmountDEV = _LiquidAssetsRate.AmountDEV
            _SumAmountHUF = _LiquidAssetsRate.AmountHUF
        Else
            '// HA NINCS BEÁLLÍTVA AKKOR NEM FUT LE !"
            Exit Sub
        End If

        _GLRows_Collection.Sorting.Add(New SortProperty("GetDate(GLHeader.DateExecution)", DB.SortingDirection.Ascending))
        _GLRows_Collection.Sorting.Add(New SortProperty("InAmountDEV", DB.SortingDirection.Descending))

        RaiseEvent InitWork(1, 1, _GLRows_Collection.Count)
        For Each _GLRows In _GLRows_Collection
            '// A deviza a fix és az MNB árfolyammal újra kell számolni mindent
            RaiseEvent DoWork()
            If _GLRows.InAmountDEV > 0 Then
                Dim _CurrencyRate_MNB_Collection = New XPCollection(Of CurrencyRate) _
                    (_uow, CriteriaOperator.Parse("CurrencyName.ShortName=? and IsMNB=True and GetDate(DateValid)<=?", _
                    _GLCassa.LiquidAssets_Cassa.CurrencyName.ShortName, _GLRows.GLHeader.DateExecution.Date))
                _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 = _GLRows.GLHeader.DateExecution.Date Then
                            _GLRows.InAmountHUF = Math.Round(_CurrencyRate_MNB.RateAverage * _GLRows.InAmountDEV, 2, MidpointRounding.AwayFromZero)
                            _GLRows.AmountHUF = _GLRows.InAmountHUF
                            Exit For
                        End If
                    Next
                End If
            End If
        Next
        RaiseEvent FinalWork
        _uow.CommitChanges()

        _GLRows_Collection = New XPCollection(Of GLRows) _
                    (PersistentCriteriaEvaluationBehavior.InTransaction, _uow, _
                    CriteriaOperator.Parse("LiquidAssets.Oid=? and GLHeader.IsEditable=False and GetDate(GLHeader.DateExecution)>=?", _
                    _GLCassa.LiquidAssets_Cassa.Oid, New Date(_Year, 1, 1)))

        _GLRows_Collection.Sorting.Add(New SortProperty("GLHeader.DateExecution", DB.SortingDirection.Ascending))

        RaiseEvent InitWork(1, 1, _GLRows_Collection.Count)
        For Each _GLRows In _GLRows_Collection
            RaiseEvent DoWork()

            '// Árfolyamok helyrerakása
            If _GLRows.OutAmountDEV > 0 Then
                If _SumAmountDEV <> 0 Then
                    _GLRows.OutAmountHUF = Math.Round(_GLRows.OutAmountDEV * (_SumAmountHUF / _SumAmountDEV), 2, MidpointRounding.AwayFromZero)
                    _GLRows.AmountHUF = _GLRows.OutAmountHUF
                    _GLRows.Save()
                    _uow.CommitChanges()

                    If _GLRows.PartnerType <> ePartnerType.eNotSet Then
                        GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
                        _uow.CommitChanges()
                    End If
                Else
                    Dim _CurrencyRate_MNB_Collection = New XPCollection(Of CurrencyRate) _
                                        (_uow, CriteriaOperator.Parse("CurrencyName.ShortName=? and IsMNB=True and GetDate(DateValid)<=?", _
                                        _GLCassa.LiquidAssets_Cassa.CurrencyName.ShortName, _GLRows.GLHeader.DateExecution.Date))
                    _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 = _GLRows.GLHeader.DateExecution.Date Then
                                _GLRows.OutAmountHUF = Math.Round(_CurrencyRate_MNB.RateAverage * _GLRows.OutAmountDEV, 2, MidpointRounding.AwayFromZero)
                                _GLRows.AmountHUF = _GLRows.OutAmountHUF
                                _GLRows.Save()
                                _uow.CommitChanges()
                                If _GLRows.PartnerType <> ePartnerType.eNotSet Then
                                    GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
                                    _uow.CommitChanges()
                                End If
                                Exit For
                            End If
                        Next
                    End If
                End If

                _SumAmountDEV -= Math.Round(_GLRows.OutAmountDEV, 2, MidpointRounding.AwayFromZero)
                _SumAmountHUF -= Math.Round(_GLRows.OutAmountHUF, 2, MidpointRounding.AwayFromZero)

                If _SumAmountDEV < 0 Then _SumAmountDEV = 0
                If _SumAmountHUF < 0 Then _SumAmountHUF = 0
            Else
                _SumAmountDEV += Math.Round(_GLRows.InAmountDEV, 2, MidpointRounding.AwayFromZero)
                _SumAmountHUF += Math.Round(_GLRows.InAmountHUF, 2, MidpointRounding.AwayFromZero)
            End If
        Next
        _uow.CommitChanges()

        RaiseEvent FinalWork
    End Sub

    Private Sub aGLCassaGenerateDocumentNumber_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGLCassaGenerateDocumentNumber.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(_ocur.Session, CriteriaOperator.Parse("IsEditable=False AND (DocumentNumber='' OR  isnull(DocumentNumber)=true)"))
        If _GLCassa_Collection.Count > 0 Then
            _GLCassa_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))

            RaiseEvent InitWork(1, 1, _GLCassa_Collection.Count)
            For Each _GLCassa As GLCassa In _GLCassa_Collection
                Dim _YearString As String = _GLCassa.DateExecution.Year.ToString
                RaiseEvent DoWork()
                If _GLCassa.CashMoveType = eCashMoveType.eIn Then
                    _GLCassa.DocumentNumber = _GLCassa.LiquidAssets_Cassa.NumberGeneratorIn.GetNewNumber(_GLCassa.LiquidAssets_Cassa.NumberGeneratorIn, _YearString)
                Else
                    _GLCassa.DocumentNumber = _GLCassa.LiquidAssets_Cassa.NumberGeneratorOut.GetNewNumber(_GLCassa.LiquidAssets_Cassa.NumberGeneratorOut, _YearString)
                End If
            Next

            _ocur.CommitChanges()
            RaiseEvent FinalWork
        End If
    End Sub
End Class


