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 System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo.DB

Public Class GLBaseVC
    Inherits DevExpress.ExpressApp.ViewController
    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()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        '-- Sor módosító letiltása
        Frame.GetController(Of GLBaseVC).aGLBaseDivideRows.Active.SetItemValue("", False)
        Frame.GetController(Of GLBaseVC).aGLBaseOpenDocument.Active.SetItemValue("", False)
        Frame.GetController(Of GLBaseVC).aGLBaseViewPCIDetail.Active.SetItemValue("", False)
        Frame.GetController(Of GLBaseVC).aGLBaseDivideRows.TargetObjectType = GetType(Nuvolar.Module.GLRows)
        Frame.GetController(Of GLBaseVC).aGLBaseDivideRows.TargetObjectsCriteria = "GLHeader.GLDocumentType='eGLBank' or GLHeader.GLDocumentType='eGLCassa' or GLHeader.GLDocumentType='eGLCompensation' or GLHeader.GLDocumentType='eGLMixed'"

        Frame.GetController(Of GLBaseVC).aGLBaseOpenAttachments.Active.SetItemValue("", False)



        If View.Id = "GLRows_ListView_Bank" Or _
            View.Id = "GLRows_ListView_Cassa" Or _
            View.Id = "GLRows_ListView_Mixed" Or _
            View.Id = "GLRows_ListView_All" Or _
            View.Id = "GLRows_ListView_Compensation" Then
            Frame.GetController(Of GLBaseVC).aGLBaseDivideRows.Active.SetItemValue("", True)
            Frame.GetController(Of GLBaseVC).aGLBaseOpenDocument.Active.SetItemValue("", True)
            Frame.GetController(Of GLBaseVC).aGLBaseOpenAttachments.Active.SetItemValue("", True)
            Frame.GetController(Of GLBaseVC).aGLBaseViewPCIDetail.Active.SetItemValue("", True)
        End If
        If View.Id Like "GLRows_ListView_*" Then
            Frame.GetController(Of GLBaseVC).aGLBaseOpenDocument.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            'Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "GLRows_ListView_Error_NoDetails" Then
            Dim _ListView As ListView = TryCast(View, ListView)

            _ListView.CollectionSource.BeginUpdateCriteria()

            _ListView.CollectionSource.Criteria("First") = CriteriaOperator.Parse(GetFilterforNoDetails())
            _ListView.CollectionSource.EndUpdateCriteria()

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id Like "GLRows_ListView_*" Then
            Frame.GetController(Of GLBaseVC).aGLBaseOpenDocument.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aGLBaseDivideRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLBaseDivideRows.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
                _GLRowsDivideH.row_PartnerType = ePartnerType.eNotSet
                e.View = Application.CreateDetailView(_onew, "GLRowsDivideH_DetailView", False, _GLRowsDivideH)
            End If

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

        If _GLRows IsNot Nothing Then

            _GLRows.DivideRows(_ocur, _GLRows, _GLRowsDivideH, True)

        End If
    End Sub
    Private Sub aGLBaseOpenDocument_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLBaseOpenDocument.Execute
        '-- Karton sorához tartozó eredeti dokumentum megnyitása
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)
        If _GLRows IsNot Nothing Then
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _onew.GetObject(_GLRows.GLHeader))
        End If
    End Sub

    Private Sub aGLBaseOpenAttachments_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLBaseOpenAttachments.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)


        If _GLRows IsNot Nothing Then
            If _GLRows.GLHeader IsNot Nothing Then
                Select Case _GLRows.GLHeader.GLDocumentType
                    Case eGLDocumentType.eGLAccount
                        Dim _GLAccounts As GLAccounts = _ocur.GetObjectByKey(Of GLAccounts)(_GLRows.GLHeader.Oid)
                        If _GLAccounts IsNot Nothing Then
                            If _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                                Dim _RegistryHeader As RegistryHeader = _ocur.GetObjectByKey(Of RegistryHeader)(_GLAccounts.ImportedRegistryHeader.Oid)
                                If _RegistryHeader IsNot Nothing Then
                                    If _RegistryHeader.DocumentFile IsNot Nothing Then
                                        ProcessDocument(_RegistryHeader.DocumentFile)
                                    End If
                                End If
                            End If
                            If _GLAccounts.ImportedInvoice IsNot Nothing Then
                                Dim _InvoiceHeader As InvoiceHeader = _ocur.GetObjectByKey(Of InvoiceHeader)(_GLAccounts.ImportedInvoice.Oid)
                                If _InvoiceHeader IsNot Nothing Then
                                    If _InvoiceHeader.PDF_Copy IsNot Nothing Then
                                        ProcessDocument(_InvoiceHeader.PDF_Copy)
                                    End If
                                    If _InvoiceHeader.PDF_Copy Is Nothing Then
                                        ViewAttachments.ViewAttachments(_ocur, Frame, _InvoiceHeader.CustomersFrom, _InvoiceHeader.Customers, _InvoiceHeader.DocumentNumber, "")
                                    End If
                                End If
                            End If
                        End If
                    Case eGLDocumentType.eGLBank
                        Dim _GLBank As GLBank = _ocur.GetObjectByKey(Of GLBank)(_GLRows.GLHeader.Oid)
                    Case eGLDocumentType.eGLCassa
                        Dim _GLCassa As GLCassa = _ocur.GetObjectByKey(Of GLCassa)(_GLRows.GLHeader.Oid)
                        _GLCassa.PrintCassaDocument(_GLCassa, _ocur, Frame)
                    Case (eGLDocumentType.eGLCompensation)
                        Dim _GLCompensation As GLCompensation = _ocur.GetObjectByKey(Of GLCompensation)(_GLRows.GLHeader.Oid)

                    Case eGLDocumentType.eGLMixed
                        Dim _GLMixed As GLMixed = _ocur.GetObjectByKey(Of GLMixed)(_GLRows.GLHeader.Oid)
                        If _GLMixed IsNot Nothing Then
                            If _GLMixed.DocumentFile IsNot Nothing Then
                                ProcessDocument(_GLMixed.DocumentFile)
                            End If
                        End If
                End Select
            End If
        End If

    End Sub
    Private Sub ProcessDocument(_DocumentFile As FileData)
        Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _DocumentFile.FileName)
        Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
        _DocumentFile.SaveToStream(_Stream)
        _Stream.Close()
        Process.Start(_FilePath)
    End Sub

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

        Dim _GLRows As GLRows

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLRows In View.SelectedObjects
            If _GLRows.PartnerType <> ePartnerType.eNotSet Then
                RaiseEvent DoWork()
                ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
            End If
        Next
        RaiseEvent FinalWork
    End Sub

    Private Sub aViewGLDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewGLDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)
        If _GLRows IsNot Nothing Then
            Dim _CS As New CollectionSource(_onew, GetType(GLDetails))
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("First") = CriteriaOperator.Parse("GLRows.Oid=?", _GLRows.Oid)
            _CS.EndUpdateCriteria()
            e.ShowViewParameters.CreatedView = Application.CreateListView("GLDetails_ListView", _CS, False)
        End If
    End Sub

    Private Sub aReconfigureGLDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aReconfigureGLDetail.Execute
        '// Csak SysAdmin használhatja
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _CurrentUser As User = _ocur.Session.GetObjectByKey(Of User)(_ocur.Session.GetKeyValue(SecuritySystem.CurrentUser))
        If _CurrentUser.UserName <> "SysAdmin" Then
            Exit Sub
        End If



        Dim _GLDetail_Collection As New XPCollection(Of GLDetails)(_uow)
        _uow.Delete(_GLDetail_Collection)

        Dim _GLDetail As GLDetails
        Dim _GLRows As GLRows
        Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow)


        RaiseEvent InitWork(1, 1, _GLRows_Collection.Count)
        For Each _GLRows In _GLRows_Collection
            RaiseEvent DoWork()
            _GLDetail = New GLDetails(_uow)
            _GLDetail.GLRows = _GLRows
            _GLDetail.AmountDEV = _GLRows.AmountDEV
            _GLDetail.AmountHUF = _GLRows.AmountHUF
            _GLDetail.Side = "T"
            _GLDetail.ChartOfAccounts = _GLRows.DebitChartOfAccounts

            _GLDetail = New GLDetails(_uow)
            _GLDetail.GLRows = _GLRows
            _GLDetail.AmountDEV = _GLRows.AmountDEV
            _GLDetail.AmountHUF = _GLRows.AmountHUF
            _GLDetail.Side = "K"
            _GLDetail.ChartOfAccounts = _GLRows.CreditChartOfAccounts
            _uow.CommitChanges()
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
    End Sub

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


        'Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)

        'Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_uow, CriteriaOperator.Parse("isnull(InvoiceHeader)=False and isnull(DateExecution)=True"))
        'Dim _InvoiceRows As InvoiceRows

        'RaiseEvent InitWork(1, 1, _InvoiceRows_Collection.Count)
        'For Each _InvoiceRows In _InvoiceRows_Collection
        '    RaiseEvent DoWork()
        '    _InvoiceRows.DateExecution = _InvoiceRows.InvoiceHeader.DateExecution
        'Next
        'RaiseEvent FinalWork
        '_uow.CommitChanges()

        'Dim _RegistryRowsFinancialControlling_Collection As New XPCollection(Of RegistryRowsFinancialControlling)(_uow, CriteriaOperator.Parse("isnull(DateExecution)=True and isnull(RegistryHeader)=False and RegistryHeader.IsEditable=False and RegistryHeader.RegistryType.RegistryMainType in ('eAccount')"))
        'Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling

        'For Each _RegistryRowsFinancialControlling In _RegistryRowsFinancialControlling_Collection
        '    RaiseEvent DoWork()
        '    _RegistryRowsFinancialControlling.DateExecution = _RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution

        'Next
        'RaiseEvent FinalWork
        '_uow.CommitChanges()

        'Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("isnull(GLHeader)=False and isnull(DateExecution)=True"))
        'Dim _GLRows As GLRows

        'RaiseEvent InitWork(1, 1, _GLRows_Collection.Count)
        'For Each _GLRows In _GLRows_Collection
        '    RaiseEvent DoWork()
        '    _GLRows.DateExecution = _GLRows.GLHeader.DateExecution
        'Next
        'RaiseEvent FinalWork
        '_uow.CommitChanges()



        'Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
    End Sub

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

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

        Dim _GLAccountsRevaluations As GLAccountsRevaluations

        For Each _GLRows In View.SelectedObjects


            _CustomersFrom = _GLRows.GLHeader.CustomersFrom
            _Customers = _GLRows.Customer
            _PartnerType = _GLRows.PartnerType
            _ConnectInfo = _GLRows.ConnectInfo

            _GLAccountsRevaluations = _ocur.FindObject(Of GLAccountsRevaluations)(CriteriaOperator.Parse("GLRows.Oid=?", _GLRows.Oid)
                                                                                )
            If _GLAccountsRevaluations IsNot Nothing Then
                _ocur.Delete(_GLAccountsRevaluations)
            End If

            _ocur.Delete(_GLRows)
            _ocur.CommitChanges()

            GLFunctions.ReconfigurePCI(_uow, _CustomersFrom, _Customers, _PartnerType, _ConnectInfo)
            _uow.CommitChanges()
        Next
    End Sub

    Private Sub aGLRowsNormalToPartner_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGLRowsNormalToPartner.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRowsNormalToPartnerPopup As GLRowsNormalToPartnerPopup
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)

        If _GLRows IsNot Nothing Then
            _GLRowsNormalToPartnerPopup = _onew.CreateObject(Of GLRowsNormalToPartnerPopup)()
            _GLRowsNormalToPartnerPopup.PartnerType = ePartnerType.eNotSet
            _GLRowsNormalToPartnerPopup.Customers = _onew.GetObject(_GLRows.Customer)
            _GLRowsNormalToPartnerPopup.ConnectInfo = _GLRows.ConnectInfo

            e.View = Application.CreateDetailView(_onew, "GLRowsNormalToPartnerPopup_DetailView", True, _GLRowsNormalToPartnerPopup)

        End If

    End Sub

    Private Sub aGLRowsNormalToPartner_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGLRowsNormalToPartner.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsNormalToPartnerPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), GLRowsNormalToPartnerPopup)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)

        Dim _GLBank As GLBank = TryCast(_GLRows.GLHeader, GLBank)
        Dim _GLCassa As GLCassa = TryCast(_GLRows.GLHeader, GLCassa)
        Dim _GLCompensation As GLCompensation = TryCast(_GLRows.GLHeader, GLCompensation)

        If _GLRows IsNot Nothing Then

            _GLRows.PartnerType = _GLRowsNormalToPartnerPopup.PartnerType
            _GLRows.Customer = _ocur.GetObject(_GLRowsNormalToPartnerPopup.Customers)
            _GLRows.ConnectInfo = _GLRowsNormalToPartnerPopup.ConnectInfo
            _GLRows.TransactionType = _GLRowsNormalToPartnerPopup.TransactionType

            If _GLBank IsNot Nothing Then
                If _GLRows.PartnerType = ePartnerType.eReceivables Then
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLBank.LiquidAssets_Main.GetChartOfAccounts(_GLBank.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLBank.DateExecution.Year, eVATMode.eOut)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLBank.DateExecution.Year, eVATMode.eOut)
                        _GLRows.CreditChartOfAccounts = _GLBank.LiquidAssets_Main.GetChartOfAccounts(_GLBank.DateExecution.Year, 0)
                    End If
                Else
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLBank.LiquidAssets_Main.GetChartOfAccounts(_GLBank.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLBank.DateExecution.Year, eVATMode.eIn)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLBank.DateExecution.Year, eVATMode.eIn)
                        _GLRows.CreditChartOfAccounts = _GLBank.LiquidAssets_Main.GetChartOfAccounts(_GLBank.DateExecution.Year, 0)
                    End If
                End If
            End If

            If _GLCassa IsNot Nothing Then
                If _GLRows.PartnerType = ePartnerType.eReceivables Then
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eOut)
                        _GLRows.CreditChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                    End If
                Else
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLCassa.LiquidAssets_Cassa.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCassa.DateExecution.Year, eVATMode.eIn)
                        _GLRows.CreditChartOfAccounts = _GLBank.LiquidAssets_Main.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                    End If
                End If
            End If

            If _GLCompensation IsNot Nothing Then
                If _GLRows.PartnerType = ePartnerType.eReceivables Then
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eOut)
                        _GLRows.CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    End If
                Else
                    If _GLRows.TransactionType = eTransactionType.ePartnerCredit Then
                        _GLRows.DebitChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                        _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eIn)
                    ElseIf _GLRows.TransactionType = eTransactionType.ePartnerDebit Then
                        _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLCompensation.DateExecution.Year, eVATMode.eIn)
                        _GLRows.CreditChartOfAccounts = _GLCompensation.LiquidAssets_Main.GetChartOfAccounts(_GLCompensation.DateExecution.Year, 0)
                    End If
                End If
            End If
            _ocur.CommitChanges()
            GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        End If
    End Sub

    Private Sub aGLRowsRepairYear_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGLRowsRepairYear.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLRows As GLRows
        Dim _ChartOfAccounts As ChartOfAccounts
        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
        For Each _GLRows In View.SelectedObjects
            RaiseEvent DoWork()
            _GLRows = _onew.GetObject(_GLRows)

            If _GLRows.GLHeader IsNot Nothing Then
                If _GLRows.GLHeader.DateExecution.Year <> _GLRows.DebitChartOfAccounts.AccountYear Then
                    _ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _GLRows.DebitChartOfAccounts.AccountNumber, _GLRows.GLHeader.DateExecution.Year))
                    If _ChartOfAccounts IsNot Nothing Then
                        _GLRows.DebitChartOfAccounts = _ChartOfAccounts
                    End If
                End If
                If _GLRows.GLHeader.DateExecution.Year <> _GLRows.CreditChartOfAccounts.AccountYear Then
                    _ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _GLRows.CreditChartOfAccounts.AccountNumber, _GLRows.GLHeader.DateExecution.Year))
                    If _ChartOfAccounts IsNot Nothing Then
                        _GLRows.CreditChartOfAccounts = _ChartOfAccounts
                    End If
                End If
            End If
        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
    End Sub

    Private Function GetFilterforNoDetails() As String
        Dim _SQL As String = ""
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _return As String = ""
        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL, eSQLType.MSSQL
                _SQL = " SELECT t1.Oid,COUNT(t2.Oid)"
                _SQL += " FROM GLRows t1 LEFT JOIN GLDetails t2 ON"
                _SQL += " t1.Oid=t2.GLRows JOIN GLHeader t3 ON"
                _SQL += " t1.GLHeader = t3.Oid"
                _SQL += " WHERE t3.IsEditable = 0"
                _SQL += " GROUP BY t1.Oid"
                _SQL += " HAVING COUNT(t2.Oid) <> 2"
            Case eSQLType.PostgreSQL
                _SQL = " SELECT t1.""Oid"",COUNT(t2.""Oid"")"
                _SQL += " FROM ""GLRows"" t1 LEFT JOIN ""GLDetails"" t2 ON"
                _SQL += " t1.""Oid""=t2.""GLRows"" JOIN ""GLHeader"" t3 ON"
                _SQL += " t1.""GLHeader"" = t3.""Oid"""
                _SQL += " WHERE t3.""IsEditable"" = 0"
                _SQL += " GROUP BY t1.""Oid"""
                _SQL += " HAVING COUNT(t2.""Oid"") <> 2"
        End Select
        _SelectedData = _uow.ExecuteQuery(_SQL)
        If _SelectedData.ResultSet.Length > 0 Then
            For Each _SelectStatementResultRow In _SelectedData.ResultSet(0).Rows

                If _return = "" Then
                    _return = "'" & _SelectStatementResultRow.Values(0).ToString & "'"
                Else
                    _return += ",'" & _SelectStatementResultRow.Values(0).ToString & "'"
                End If
            Next
        End If
        Return "Oid in (" & _return & ")"

    End Function

    Private Sub aRepairNoDetails_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aRepairNoDetails.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _GLRows As GLRows

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _GLRows In View.SelectedObjects
            RaiseEvent DoWork()
            Dim _GLDetail_Collection As New XPCollection(Of GLDetails)(_uow, CriteriaOperator.Parse("GLRows.Oid=?", _GLRows.Oid.ToString))
            _uow.Delete(_GLDetail_Collection)

            Dim _GLDetail As GLDetails
            _GLDetail = New GLDetails(_uow)
            _GLDetail.GLRows = _uow.GetObjectByKey(Of GLRows)(_GLRows.Oid)
            _GLDetail.AmountDEV = _GLRows.AmountDEV
            _GLDetail.AmountHUF = _GLRows.AmountHUF
            _GLDetail.Side = "T"
            _GLDetail.ChartOfAccounts = _uow.GetObjectByKey(Of ChartOfAccounts)(_GLRows.DebitChartOfAccounts.Oid)

            _GLDetail = New GLDetails(_uow)
            _GLDetail.GLRows = _uow.GetObjectByKey(Of GLRows)(_GLRows.Oid)
            _GLDetail.AmountDEV = _GLRows.AmountDEV
            _GLDetail.AmountHUF = _GLRows.AmountHUF
            _GLDetail.Side = "K"
            _GLDetail.ChartOfAccounts = _uow.GetObjectByKey(Of ChartOfAccounts)(_GLRows.CreditChartOfAccounts.Oid)

            _uow.CommitChanges()
        Next
        RaiseEvent FinalWork
    End Sub

#Region "ROW INDEX"
    Private Sub aDown_GLRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aDown_GLRows.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLHeader As GLHeader = TryCast(View.CurrentObject, GLRows).GLHeader
        Dim _GLRowsSelected As GLRows
        Dim _GLRowsPlus As GLRows = Nothing
        Dim _GLRows As GLRows
        
        Me.ReorderRows(_ocur, _GLHeader)

        Try
            _GLRowsSelected = TryCast(View.SelectedObjects(0), GLRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _GLRowsSelected.RowIndex < _GLHeader.GLRows.Count - 1 Then
            For Each _GLRows In _GLHeader.GLRows
                If _GLRows.RowIndex = _GLRowsSelected.RowIndex + 1 Then
                    _GLRowsPlus = _GLRows
                    Exit For
                End If
            Next

            If _GLRowsPlus IsNot Nothing Then
                _GLRowsSelected.RowIndex += 1
                _GLRowsPlus.RowIndex -= 1
            End If
            _ocur.CommitChanges()
            TryCast(View, ListView).CollectionSource.Reload()
        End If
    End Sub
    Private Sub aUp_GLRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aUp_GLRows.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLHeader As GLHeader = TryCast(View.SelectedObjects(0), GLRows).GLHeader
        Dim _GLRowsSelected As GLRows
        Dim _GLRowsMinus As GLRows = Nothing
        Dim _GLRows As GLRows

        Me.ReorderRows(_ocur, _GLHeader)

        Try
            _GLRowsSelected = TryCast(View.SelectedObjects(0), GLRows) ' Az editor egy listview, kiveszi az első kijelölt sorát
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _GLRowsSelected.RowIndex > 0 Then
            For Each _GLRows In _GLHeader.GLRows
                If _GLRows.RowIndex = _GLRowsSelected.RowIndex - 1 Then
                    _GLRowsMinus = _GLRows
                    Exit For
                End If
            Next

            If _GLRowsMinus IsNot Nothing Then
                _GLRowsSelected.RowIndex -= 1
                _GLRowsMinus.RowIndex += 1
            End If
            _ocur.CommitChanges()
            TryCast(View, ListView).CollectionSource.Reload()
        End If
    End Sub
    Private Sub ReorderRows(_ocur As Xpo.XPObjectSpace, _GLHeader As GLHeader)
        Dim _NoOrder As Boolean = False
        Dim _RowIndex As Long = 0
        Dim _GLRows As GLRows

        '//------------------------------------------------------------------------------------------
        _GLHeader.GLRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Descending))
        For Each _GLRows In _GLHeader.GLRows
            If _GLRows.RowIndex <= 0 Then
                _NoOrder = True
                Exit For
            End If
            Exit For
        Next
        _GLHeader.GLRows.Sorting.RemoveAt(_GLHeader.GLRows.Sorting.Count - 1)
        If _NoOrder Then
            For Each _GLRows In _GLHeader.GLRows
                _GLRows.RowIndex = _RowIndex
                _RowIndex += 1
            Next
        End If
        _ocur.CommitChanges()
        '----------------------------------------------------------------------------------------------
    End Sub
#End Region
    
    Private Sub aGLBaseViewPCIDetail_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGLBaseViewPCIDetail.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRows As GLRows = TryCast(View.SelectedObjects(0), GLRows)

        If _GLRows IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _GLRows.GLHeader.CustomersFrom, _
                              _GLRows.Customer, _GLRows.PartnerType, _
                              _GLRows.ConnectInfo)
        End If
    End Sub
End Class
