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.Xpo
Imports System.IO
Imports DevExpress.Data.Filtering
Imports System.Globalization
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.ExpressApp.Win.SystemModule
Imports DevExpress.ExpressApp.SystemModule

Public Class BankImportVC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "BankImportHeader_BankImportHeaderSum_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "BankImportHeaderSum_BankImportHeaderSumDetail_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "BankImportHeaderSum_GLRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "BankImportHeaderSum_BankImportNameParameters_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            'Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "BankImportHeaderSum_BankImportPopup_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub Ondeactivated()
        MyBase.OnActivated()
        If View.Id = "BankImportHeader_BankImportHeaderSum_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "BankImportHeaderSum_BankImportHeaderSumDetail_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "BankImportHeaderSum_GLRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "BankImportHeaderSum_BankImportNameParameters_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            'Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "BankImportHeaderSum_BankImportPopup_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub BankImportVC_ViewControlsCreated(sender As System.Object, e As System.EventArgs)
        'If View.Id = "BankImportHeaderSum_BankImportNameParameters_ListView" Then ' Az inline edit beállítása.
        '    Dim _ListView As ListView = TryCast(View, ListView)
        '    Dim _GridListEditor As GridListEditor = TryCast(_ListView.Editor, GridListEditor)

        '    If _GridListEditor IsNot Nothing Then
        '        _GridListEditor.AllowEdit = True

        '        For i As Integer = 0 To _GridListEditor.Columns.Count - 1
        '            If _GridListEditor.GridView.Columns(i).FieldName <> "Controlling!" And _GridListEditor.GridView.Columns(i).FieldName <> "ChartOfAccounts!" Then
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
        '            End If
        '        Next i

        '    End If
        'End If
    End Sub
    Private Sub aBankImport_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBankImport.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _BankImportHeader As BankImportHeader
        Dim _BankImportHeaderSum As BankImportHeaderSum = Nothing
        Dim _BankImportHeaderSumDetail As BankImportHeaderSumDetail
        Dim _BankImportNameParameters As BankImportNameParameters
        Dim _BankInformation As BankInformation
        Dim _GLBank As GLBank

        Dim _BIH As BankImportHeader = TryCast(View.SelectedObjects(0), BankImportHeader)
        _BIH.Save()
        View.ObjectSpace.CommitChanges()
        _BankImportHeader = _uow.GetObjectByKey(Of BankImportHeader)(_BIH.Oid)

        Dim _MemoryStream As New MemoryStream
        Dim _StreamReader As StreamReader
        Dim _Line As String
        Dim _LineCount As Int32

        For Each _BankImportHeaderSum In _BankImportHeader.BankImportHeaderSum
            If _BankImportHeaderSum.GLBank IsNot Nothing Then
                If _BankImportHeaderSum.GLBank.IsEditable = False Then
                    Throw New Exception("*Nem lehet véglegesített kivonatot feldolgozni !")
                    Exit Sub
                End If
            End If
        Next

        For Each _BankImportHeaderSum In _BankImportHeader.BankImportHeaderSum
            If _BankImportHeaderSum.GLBank IsNot Nothing Then
                _GLBank = _uow.FindObject(Of GLBank)(CriteriaOperator.Parse("Oid=?", _BankImportHeaderSum.GLBank.Oid))
                _uow.Delete(_GLBank.GLRows)
                _GLBank.BankImportHeaderSum = Nothing
                _GLBank.Save()
                _uow.Delete(_GLBank)
            End If
        Next
        _uow.CommitChanges()

        Dim _BankImportHeaderSumDetail_Collection As New XPCollection(_uow, GetType(BankImportHeaderSumDetail), _
             CriteriaOperator.Parse("BankImportHeader.Oid=?", _BankImportHeader.Oid))
        _uow.Delete(_BankImportHeaderSumDetail_Collection)
        _uow.CommitChanges()

        Dim _BankImportHeaderSum_Collection As New XPCollection(_uow, GetType(BankImportHeaderSum), _
             CriteriaOperator.Parse("BankImportHeader.Oid=?", _BankImportHeader.Oid))
        _uow.Delete(_BankImportHeaderSum_Collection)
        _uow.CommitChanges()

        _BankImportHeader.FileData.SaveToStream(_MemoryStream)
        _MemoryStream.Position = 0
        _StreamReader = New StreamReader(_MemoryStream, System.Text.Encoding.Default) ' Esetleg: System.Text.Encoding.GetEncoding(1250)

        Do While _StreamReader.Peek <> -1
            _Line = _StreamReader.ReadLine
            _LineCount = _LineCount + 1
        Loop
        _MemoryStream.Position = 0
        _StreamReader = New StreamReader(_MemoryStream, System.Text.Encoding.Default) ' Esetleg: System.Text.Encoding.GetEncoding(1250)

        _WaitFormClass.Initwork(1, 1, _LineCount)
        Do While _StreamReader.Peek <> -1
            _WaitFormClass.DoWork()
            _Line = _StreamReader.ReadLine

            If Left(_Line, 2) = "11" Then
                _BankImportHeaderSum = New BankImportHeaderSum(_uow)
                _GLBank = New GLBank(_uow)

                _BankImportHeaderSum.BankImportHeader = _BankImportHeader

                _BankImportHeaderSum.DateOpen = Convert.ToDateTime(_Line.Substring(85, 4) & "." & _Line.Substring(89, 2) & "." & _Line.Substring(91, 2)) 'Nyitó dátum
                _BankImportHeaderSum.DateClose = Convert.ToDateTime(_Line.Substring(93, 4) & "." & _Line.Substring(97, 2) & "." & _Line.Substring(100, 2))  'Záró dátum
                _BankImportHeaderSum.AccountNumber = _Line.Substring(10, 24) 'Bankszámlaszám amire a kivonat szól (saját számláink egyike)
                _BankImportHeaderSum.CurrencyName = _uow.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _Line.Substring(34, 3))) 'Deviza
                _BankImportHeaderSum.AmountOpen = Convert.ToDouble(_Line.Substring(101, 17)) + (Convert.ToDouble(_Line.Substring(118, 2)) / 100) 'Nyitó összeg
                _BankImportHeaderSum.AmountClose = Convert.ToDouble(_Line.Substring(120, 17)) + (Convert.ToDouble(_Line.Substring(136, 2)) / 100) 'Záró összeg
                _BankImportHeaderSum.DocumentNumber = _Line.Substring(2, 8).Trim 'Kiovnat száma
                _BankInformation = _uow.FindObject(Of BankInformation)(CriteriaOperator.Parse("AccountNumber=?", Left(_BankImportHeaderSum.AccountNumber, 8) & "-" & _
                                   _BankImportHeaderSum.AccountNumber.Substring(8, 8) & "-" & _BankImportHeaderSum.AccountNumber.Substring(16, 8)))
                Try
                    _BankImportHeaderSum.LiquidAssets = _BankInformation.LiquidAssests ' Saját pénzeszközünk
                Catch ex As Exception
                End Try

                _GLBank.BankImportHeaderSum = _BankImportHeaderSum
                _GLBank.CurrencyName = _BankImportHeaderSum.CurrencyName
                _GLBank.CustomersFrom = _BankInformation.LiquidAssests.CustomerFrom
                _GLBank.DocumentNumber = _BankImportHeaderSum.DocumentNumber
                _GLBank.DateExecution = _BankImportHeaderSum.DateOpen
                _GLBank.DateCreated = _BankImportHeaderSum.DateOpen
                _GLBank.DatePayment = _BankImportHeaderSum.DateOpen
                _GLBank.NoteHeader = _BankImportHeader.FileData.FileName 'Fájl neve, melyikből töltötte be
                _GLBank.LiquidAssets_Main = _BankInformation.LiquidAssests

                _BankImportHeaderSum.Save()
                _GLBank.Save()
            End If

            If Left(_Line, 2) = "12" Then
                _BankImportHeaderSumDetail = New BankImportHeaderSumDetail(_uow)
                _BankImportHeaderSumDetail.BankImportHeader = _BankImportHeader
                _BankImportHeaderSumDetail.BankImportHeaderSum = _BankImportHeaderSum
                '_BankImportHeaderSumDetail.NoteRow = _Line.Substring(356, 140).Trim
                _BankImportHeaderSumDetail.NoteRow = _Line.Substring(356, 35).Trim & " " & _Line.Substring(391, 35).Trim & " " & _Line.Substring(426, 35).Trim & " " & _Line.Substring(461, 35).Trim

                Try
                    _BankImportHeaderSumDetail.CreditAmount = Convert.ToDouble(_Line.Substring(851, 14)) + Convert.ToDouble(_Line.Substring(865, 2) / 100)
                Catch ex As Exception
                End Try

                Try
                    _BankImportHeaderSumDetail.DebitAmount = Convert.ToDouble(_Line.Substring(902, 14)) + Convert.ToDouble(_Line.Substring(916, 2) / 100)
                Catch ex As Exception
                End Try

                If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                    _BankImportHeaderSumDetail.PartnerString = _Line.Substring(182, 35).Trim & " " & _Line.Substring(218, 35).Trim & " " & _Line.Substring(252, 35).Trim & " " & _Line.Substring(287, 35).Trim
                    _BankImportHeaderSumDetail.AccountNumber = _Line.Substring(322, 34).Trim
                Else
                    _BankImportHeaderSumDetail.PartnerString = _Line.Substring(636, 35).Trim & " " & _Line.Substring(671, 35).Trim & " " & _Line.Substring(706, 35).Trim & " " & _Line.Substring(741, 35).Trim
                    _BankImportHeaderSumDetail.AccountNumber = _Line.Substring(776, 34).Trim
                End If

                If IsNumeric(_BankImportHeaderSumDetail.AccountNumber) Then
                    _BankImportHeaderSumDetail.AccountNumber = _BankImportHeaderSumDetail.AccountNumber.PadRight(24, "0")
                    _BankImportHeaderSumDetail.AccountNumber = Left(_BankImportHeaderSumDetail.AccountNumber, 8) & "-" & _BankImportHeaderSumDetail.AccountNumber.Substring(8, 8) & "-" & _BankImportHeaderSumDetail.AccountNumber.Substring(16)
                End If

                _BankImportHeaderSumDetail.Save()

                If _BankImportHeaderSumDetail.AccountNumber.Trim.Length = 0 Then
                    _BankImportNameParameters = _uow.FindObject(Of BankImportNameParameters)(CriteriaOperator.Parse("NoteRow=?", _BankImportHeaderSumDetail.NoteRow))
                    If _BankImportNameParameters Is Nothing Then

                        _BankImportNameParameters = New BankImportNameParameters(_uow)

                        _BankImportNameParameters.NoteRow = _BankImportHeaderSumDetail.NoteRow
                        '_BankImportNameParameters.ChartOfAccounts =
                        '_BankImportNameParameters.Controlling =

                        _BankImportNameParameters.Save()
                        _uow.CommitChanges()
                    End If
                End If

            End If

        Loop
        _uow.CommitChanges()
        _Waitformclass.FinalWork()
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub
    Private Sub aCreateGLBankRows_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateGLBankRows.CustomizePopupWindowParams
        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _BankImportHeaderSumDetail As BankImportHeaderSumDetail = _onew.GetObjectByKey(Of BankImportHeaderSumDetail)(TryCast(View.SelectedObjects(0), BankImportHeaderSumDetail).Oid)
        Dim _BankImportPopup As New BankImportPopup(_onew.Session)
        'Az AccountNumber tagolva van tárolva a táblában?
        Dim _CustomerBankAccounts As CustomerBankAccounts = _onew.FindObject(Of CustomerBankAccounts)(CriteriaOperator.Parse("AccountNumber=?", _BankImportHeaderSumDetail.AccountNumber))

        If _CustomerBankAccounts IsNot Nothing Then
            _BankImportPopup.Customers = _CustomerBankAccounts.Customers
        End If
        _BankImportPopup.NoteRows = _BankImportHeaderSumDetail.NoteRow
        If _BankImportHeaderSumDetail.BankImportHeaderSum.CurrencyName.ShortName = "HUF" Then
            If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                _BankImportPopup.AmountHUF = _BankImportHeaderSumDetail.CreditAmount
            End If
            If _BankImportHeaderSumDetail.DebitAmount <> 0 Then
                _BankImportPopup.AmountHUF = _BankImportHeaderSumDetail.DebitAmount
            End If
        Else
            If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                _BankImportPopup.AmountDEV = _BankImportHeaderSumDetail.CreditAmount
            End If
            If _BankImportHeaderSumDetail.DebitAmount <> 0 Then
                _BankImportPopup.AmountDEV = _BankImportHeaderSumDetail.DebitAmount
            End If
        End If

        '_BankImportPopup.TransactionType
        'ChartOfAccounts As ChartOfAccounts
        'Controlling As Controlling
        'ConnectInfo As String
        'CurrencyName2 As CurrencyName
        '_BankImportPopup.PCIGroup = ????

        e.View = Application.CreateDetailView(_onew, _BankImportPopup, False)
    End Sub
    Private Sub aCreateGLBankRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateGLBankRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _BankImportHeaderSumDetail As BankImportHeaderSumDetail = _ocur.GetObject(TryCast(View.SelectedObjects(0), BankImportHeaderSumDetail))
        Dim _BankImportPopup As BankImportPopup = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), BankImportPopup))
        Dim _ChartOfAccounts As ChartOfAccounts = _ocur.GetObject(TryCast(_BankImportPopup.ChartOfAccounts, ChartOfAccounts))

        Dim _GLRows As GLRows
        ' Dim _BankImportNameParameters As BankImportNameParameters

        _GLRows = New GLRows(_ocur.Session)

        '_GLRows.GLHeader = _BankImportHeaderSumDetail.BankImportHeaderSum.GLBank

        If _BankImportPopup.TransactionType = eTransactionType.eNormalCredit Or _BankImportPopup.TransactionType = eTransactionType.ePartnerCredit Then
            _GLRows.CreditChartOfAccounts = _ChartOfAccounts
            _GLRows.DebitChartOfAccounts = _BankImportPopup.Customers.CustomersGLParameters.GetChartOfAccounts(_BankImportHeaderSumDetail.BankImportHeaderSum.GLBank.DateExecution.Year, eVATMode.eIn)
        End If
        If _BankImportPopup.TransactionType = eTransactionType.eNormalCredit Or _BankImportPopup.TransactionType = eTransactionType.ePartnerCredit Then
            _GLRows.CreditChartOfAccounts = _BankImportPopup.Customers.CustomersGLParameters.GetChartOfAccounts(_BankImportHeaderSumDetail.BankImportHeaderSum.GLBank.DateExecution.Year, eVATMode.eOut)
            _GLRows.DebitChartOfAccounts = _ChartOfAccounts
        End If

        _GLRows.AmountDEV = _BankImportPopup.AmountDEV
        _GLRows.AmountHUF = _BankImportPopup.AmountHUF
        _GLRows.Controlling = _BankImportPopup.Controlling
        _GLRows.ConnectInfo = _BankImportPopup.ConnectInfo

        _GLRows.Save()


        '_BankImportNameParameters = _ocur.FindObject(Of BankImportNameParameters)(CriteriaOperator.Parse("NoteRow=?", _BankImportPopup.NoteRows))
        'If _BankImportNameParameters Is Nothing Then
        '    _BankImportNameParameters = New BankImportNameParameters(_ocur.Session)

        '    _BankImportNameParameters.NoteRow = _BankImportPopup.NoteRows
        '    _BankImportNameParameters.ChartOfAccounts = _ChartOfAccounts
        '    _BankImportNameParameters.Controlling = _BankImportPopup.Controlling

        '    _BankImportNameParameters.Save()
        'End If

        _ocur.CommitChanges()
        'Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub
    Private Sub aCreateGLRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateGLRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _BankImportHeaderSum As BankImportHeaderSum = _uow.GetObjectByKey(Of BankImportHeaderSum)(TryCast(View.SelectedObjects(0), BankImportHeaderSum).Oid)
        Dim _BankImportHeaderSumDetail As BankImportHeaderSumDetail
        Dim _GLRows As GLRows
        'Dim _Customers As Customers
        Dim _BankImportNameParameters As BankImportNameParameters
        Dim _CustomerBankAccounts As CustomerBankAccounts
        Dim _CustomersGLParameters As CustomersGLParameters = _uow.FindObject(Of CustomersGLParameters)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("1=1")) 'Az utolsó elemet kapjuk meg a táblából. A ChartofAccountNo mindig ugyan az!

        _WaitFormClass.Initwork(1, 1, _BankImportHeaderSum.BankImportHeaderSumDetail.Count)
        For Each _BankImportHeaderSumDetail In _BankImportHeaderSum.BankImportHeaderSumDetail

            _WaitFormClass.DoWork()
            _GLRows = New GLRows(_uow)
            _GLRows.GLHeader = _BankImportHeaderSum.GLBank

            If _BankImportHeaderSumDetail.AccountNumber IsNot Nothing Then 'Valószínűleg beazonosítható a tranzakcióhoz tartozó partner

                _CustomerBankAccounts = _uow.FindObject(Of CustomerBankAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                 CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _
                                                                                _BankImportHeaderSumDetail.AccountNumber, _GLRows.GLHeader.DateExecution.Year))
                If _CustomerBankAccounts IsNot Nothing Then ' Sikerült a beazonosítás
                    _GLRows.Customer = _CustomerBankAccounts.Customers
                End If
            End If

            If _GLRows.Customer Is Nothing Then 'Nem sikerült partnert azonosítani
                If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                    _GLRows.TransactionType = eTransactionType.eNormalCredit 'Követel típusú tranzakció lesz

                    _GLRows.DebitChartOfAccounts = _BankImportHeaderSum.LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0) 'Tartozik a bank(számla)
                    _GLRows.CreditChartOfAccounts = _CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut) 'Követel az elszámolási számla
                    '_GLRows.CreditChartOfAccounts = _Customers.CustomersGLParameters.ChartOfAccountsNo

                End If
                If _BankImportHeaderSumDetail.DebitAmount <> 0 Then 'Tartozik típusú tranzakció lesz
                    _GLRows.TransactionType = eTransactionType.eNormalDebit

                    '_GLRows.DebitChartOfAccounts = _Customers.CustomersGLParameters.ChartOfAccountsNo
                    _GLRows.DebitChartOfAccounts = _CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut) 'Tartozik az elszámolási számla
                    _GLRows.CreditChartOfAccounts = _BankImportHeaderSum.LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0) 'Követel a bank(számla)

                End If

                _GLRows.PartnerType = ePartnerType.eNotSet 'Ha nincs beazonosított partner, akkor így kell beállítani??????
                _GLRows.CashType = eCashType.eNormal
            Else                                            'Van azonosított partnerünk
                If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                    _GLRows.TransactionType = eTransactionType.ePartnerCredit 'Követel típusú tranzakció
                    _GLRows.PartnerType = ePartnerType.eReceivables 'Vevői számla 

                    _GLRows.DebitChartOfAccounts = _BankImportHeaderSum.LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0) 'Tartozik a bank(számla)
                    _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut) 'Követel a partnerhez tartozó számla

                End If
                If _BankImportHeaderSumDetail.DebitAmount <> 0 Then
                    _GLRows.TransactionType = eTransactionType.ePartnerDebit 'Tartozik típusú tranzakció
                    _GLRows.PartnerType = ePartnerType.ePayabels 'Szállítói számla

                    _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn) 'Tartozik a partnerhez tartozó számla
                    _GLRows.CreditChartOfAccounts = _BankImportHeaderSum.LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0) 'Követel a bank(számla)

                End If

                _GLRows.CashType = eCashType.ePartner
            End If

            If _BankImportHeaderSum.CurrencyName.ShortName = "HUF" Then 'Forintos tranzakció
                If _BankImportHeaderSumDetail.CreditAmount <> 0 Then 'Követel
                    _GLRows.AmountHUF = _BankImportHeaderSumDetail.CreditAmount
                    _GLRows.InAmountHUF = _BankImportHeaderSumDetail.CreditAmount
                Else
                    _GLRows.AmountHUF = _BankImportHeaderSumDetail.DebitAmount
                    _GLRows.OutAmountHUF = _BankImportHeaderSumDetail.DebitAmount
                End If
            Else                                                        'Devizás tranzakció
                If _BankImportHeaderSumDetail.CreditAmount <> 0 Then
                    _GLRows.AmountDEV = _BankImportHeaderSumDetail.CreditAmount 'Követel
                    _GLRows.InAmountDEV = _BankImportHeaderSumDetail.CreditAmount
                Else
                    _GLRows.AmountDEV = _BankImportHeaderSumDetail.DebitAmount
                    _GLRows.OutAmountDEV = _BankImportHeaderSumDetail.DebitAmount
                End If
            End If


            _GLRows.LiquidAssets = _BankImportHeaderSum.LiquidAssets
            _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
            _GLRows.GLRowsGUID = Guid.NewGuid
            '_GLRows.ConnectInfo = _BankImportHeaderSumDetail.NoteRow
            _GLRows.NoteRows = _BankImportHeaderSumDetail.NoteRow
            _GLRows.BankImportHeaderSumDetail = _BankImportHeaderSumDetail


            '_GLRows.Save()

            If _BankImportHeaderSumDetail.AccountNumber.Trim.Length = 0 Then
                _BankImportNameParameters = _uow.FindObject(Of BankImportNameParameters)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("NoteRow=?", _BankImportHeaderSumDetail.NoteRow))
                If _BankImportNameParameters Is Nothing Then

                    _BankImportNameParameters = New BankImportNameParameters(_uow)

                    _BankImportNameParameters.NoteRow = _BankImportHeaderSumDetail.NoteRow
                    '_BankImportNameParameters.ChartOfAccounts =
                    '_BankImportNameParameters.Controlling =

                    '        _BankImportNameParameters.Save()

                End If
            End If

        Next
        _uow.CommitChanges()
        _Waitformclass.FinalWork()
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub
    Private Sub aFinalGLBank_Import_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalGLBank_Import.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        'Dim _BankImportHeaderSum As BankImportHeaderSum = _ocur.GetObject(TryCast(View.SelectedObjects(0), BankImportHeaderSum))
        Dim _GLRows As GLRows = _ocur.GetObject(TryCast(View.SelectedObjects(0), GLRows))

        _GLRows.BankImportHeaderSumDetail.BankImportHeaderSum.GLBank.IsEditable = False
        _ocur.CommitChanges()

        Windows.Forms.MessageBox.Show("Az aktuális banki kivonathoz tartozó főkönyvi bejegyzés végledesítésre került.")


    End Sub

    

    Private Sub aGLBankImportsDivideRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGLBankImportsDivideRows.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 aGLBankImportsDivideRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGLBankImportsDivideRows.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

End Class
