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 System.IO
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Xpo
Imports Nuvolar.Module
Public Class BankVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected _selection As ArrayList
    Protected _selection_group As ArrayList
    Protected _noselect As Boolean = False
    Public Sub New()
        MyBase.New()
        _selection = New ArrayList()
        _selection_group = New ArrayList
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of BankVC).aViewAttachments_Bank.Active.SetItemValue("", False)
        Frame.GetController(Of BankVC).aViewRegistry.Active.SetItemValue("", False)
        Frame.GetController(Of BankVC).aViewPDCI_Bank.Active.SetItemValue("", False)
        Frame.GetController(Of BankVC).aBankDivide.Active.SetItemValue("", False)
        If View.Id Like "RegistryBankTransfer_ListView*" Then

            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).NewObjectAction.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)

            Frame.GetController(Of BankVC).aViewAttachments_Bank.Active.SetItemValue("", True)
            Frame.GetController(Of BankVC).aViewRegistry.Active.SetItemValue("", True)
            Frame.GetController(Of BankVC).aViewPDCI_Bank.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
        End If
        If View.Id = "GLBankDivide_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "BankParameterDEV_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "RegistryBankTransfer_ListView_Not_Transfered" Then
            Frame.GetController(Of BankVC).aBankDivide.Active.SetItemValue("", True)
            Frame.GetController(Of BankVC).aBankMerge.Active.SetItemValue("", True)
        End If

        If View.Id = "BankNoTransferPopup" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id Like "RegistryBankTransfer_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            RemoveHandler View.SelectionChanged, AddressOf view_SelectionChanged
        End If
        If View.Id = "GLBankDivide_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "BankParameterDEV_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "RegistryBankTransfer_ListView" Then
            Frame.GetController(Of BankVC).aBankDivide.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).NewObjectAction.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", True)
        End If
    End Sub
#Region "Bank save" 'Banki kommunikáció
    Private Sub DownloadToMKBHUF(ByVal _View As View, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs)
       
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _BankParameterDEV As BankParameterDEV = TryCast(e.PopupWindow.View.SelectedObjects(0), BankParameterDEV)
        Dim _Bank_AccountNumber_CustomersFrom As String = _BankParameterDEV.BankInformation.AccountNumber
        Dim _RegistryBankTransfer As RegistryBankTransfer
        Dim _DocumentNumber As String
        Dim _Note As String
        Dim I As Long
        Dim _BankString As String
        Dim _Today As String
        Dim _Customers_Name As String
        Dim _CustomersFrom_Name As String
        Dim _Bank_AccountNumber_Customers As String
        Dim _Amount As String
        Dim _FileName As String


        If _selection Is Nothing Or _selection.Count <= 0 Then Exit Sub

        '-------------------------------------------------------------------------------------------
        _Today = Format(Year(_BankParameterDEV.DateExecution), "0000")
        _Today = _Today & Format(Month(_BankParameterDEV.DateExecution), "00")
        _Today = _Today & Format(Day(_BankParameterDEV.DateExecution), "00")

        _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(_selection(0), RegistryBankTransferView).Oid)

        If _BankParameterDEV.CustomFileName = "" Then
            _FileName = Replace(Replace(Mid(_RegistryBankTransfer.RegistryHeader.CustomersFrom.FullName, 1, 10), ".", "_"), " ", "_") & _
                "_" & _Today & Replace(Format(Now, "hh:mm"), ":", "") & ".HUF"
        Else
            _FileName = _BankParameterDEV.CustomFileName
        End If

        Dim _FileStream As New FileStream(_BankParameterDEV.FolderName & "/" & _FileName, FileMode.Create, FileAccess.ReadWrite)

        Dim _StreamWriter As New StreamWriter(_FileStream, System.Text.Encoding.ASCII)

        For I = 0 To _selection.Count - 1
            _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(_selection(I), RegistryBankTransferView).Oid)
            If _RegistryBankTransfer.IsBlocked = False Then
                _CustomersFrom_Name = _RegistryBankTransfer.RegistryHeader.CustomersFrom.Name
                If _RegistryBankTransfer.RegistryHeader.Customers.Name = "" Then
                    _RegistryBankTransfer.RegistryHeader.Customers.Name = _RegistryBankTransfer.RegistryHeader.Customers.FullName
                    _ocur.Session.FlushChanges()
                End If
                _Customers_Name = _RegistryBankTransfer.RegistryHeader.Customers.Name
                _Bank_AccountNumber_Customers = Replace(_RegistryBankTransfer.RegistryHeader.F_CustomerBankAccounts.AccountNumber, "-", "")
                _Bank_AccountNumber_Customers = AlignStr(_Bank_AccountNumber_Customers, eDirection.Right, "0", 24)
                _Bank_AccountNumber_Customers = AlignStr(Replace(_Bank_AccountNumber_Customers, "-", ""), 1, " ", 47)

                '-- Letöltésre kerülő összeg !

                _Amount = LTrim(RTrim(CStr(Math.Round((TryCast(_selection(I), RegistryBankTransferView).AmountHUF), 0, MidpointRounding.AwayFromZero)))) & ",00"

                _DocumentNumber = _RegistryBankTransfer.RegistryHeader.DocumentNumber
                _Note = _RegistryBankTransfer.Note
                _Customers_Name = AlignStr(Replace(_Customers_Name, Chr(13) & Chr(10), " "), 1, " ", 32)
                _Amount = AlignStr(Replace(_Amount, ".", ""), 0, "0", 13)

                _BankString = "PAYORD"
                _BankString = _BankString & "DO"
                _BankString = _BankString & "20000101000000"

                _BankString = _BankString & Replace(AlignStr(Replace(_Bank_AccountNumber_CustomersFrom, "-", ""), 1, " ", 47), "00000000", "        ")
                _BankString = _BankString & "0"
                _BankString = _BankString & ANSITo852(AlignStr(Replace(_CustomersFrom_Name, Chr(13) & Chr(10), " "), 1, " ", 32))
                _BankString = _BankString & AlignStr(" ", 0, " ", 117)

                _BankString = _BankString & _Bank_AccountNumber_Customers
                _BankString = _BankString & "0"
                _BankString = _BankString & AlignStr(" ", 0, " ", 64)
                _BankString = _BankString & ANSITo852(AlignStr(Replace(_Customers_Name, Chr(13) & Chr(10), " "), 1, " ", 32))

                _BankString = _BankString & AlignStr(" ", 0, " ", 229)

                _BankString = _BankString & ANSITo852(AlignStr(_DocumentNumber, 1, " ", 32))
                _BankString = _BankString & ANSITo852(AlignStr(_Note, 1, " ", 64))
                _BankString = _BankString & ANSITo852(AlignStr(" ", 0, " ", 6))

                _BankString = _BankString & AlignStr(" ", 0, " ", 90)
                _BankString = _BankString & "1111"
                _BankString = _BankString & AlignStr(" ", 0, " ", 17)

                _BankString = _BankString & "HUF"
                _BankString = _BankString & "0"
                _BankString = _BankString & AlignStr(_Amount, 0, " ", 13)

                _BankString = _BankString & AlignStr(" ", 0, " ", 12)

                _BankString = _BankString & AlignStr(_Today, 0, " ", 8)

                _BankString = _BankString & AlignStr(" ", 0, " ", 95)

                _BankString = _BankString & "00" & Chr(13) & Chr(10)

                _StreamWriter.Write(_BankString)
                _RegistryBankTransfer.BankSavedFileName = _FileName
                _RegistryBankTransfer.Save()
            End If
        Next I
        _StreamWriter.Close()
        _BankParameterDEV.TransferTotalDEV = 0
        _BankParameterDEV.TransferCount = 0
        _BankParameterDEV.TransferTotalHUF = 0
        _BankParameterDEV.BankInformation = Nothing
        _BankParameterDEV.Save()
        e.PopupWindow.View.ObjectSpace.CommitChanges()
        _ocur.CommitChanges()
    End Sub
    Private Sub DownloadToMKBDEV(ByVal _View As View, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _BankParameterDEV As BankParameterDEV = TryCast(e.PopupWindow.View.SelectedObjects(0), BankParameterDEV)
        Dim _Bank_AccountNumber_CustomersFrom As String
        Dim _Bank_AccountNumber_Customers As String
        Dim _RegistryBankTransfer As RegistryBankTransfer
        Dim _DocumentNumber As String
        Dim _Note As String
        Dim I As Long
        Dim _BankString As String
        Dim _Today As String
        Dim _Customers_Name As String
        Dim _CustomersFrom_Name As String
        Dim _FileName As String
        Dim _Amount As String

        If _selection Is Nothing Or _selection.Count <= 0 Then Exit Sub

        '-------------------------------------------------------------------------------------------
        _Today = Format(Year(_BankParameterDEV.DateExecution), "0000") & Format(Month(_BankParameterDEV.DateExecution), "00") & Format(Day(_BankParameterDEV.DateExecution), "00")
        _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(_selection(0), RegistryBankTransferView).Oid)
        _FileName = Replace(Replace(Mid(_RegistryBankTransfer.RegistryHeader.CustomersFrom.FullName, 1, 10), ".", "_"), " ", "_") & _
            "_" & _Today & Replace(Format(Now, "hh:mm"), ":", "") & ".DEV"

        _CustomersFrom_Name = _RegistryBankTransfer.RegistryHeader.CustomersFrom.FullName
        _Bank_AccountNumber_CustomersFrom = Replace(_BankParameterDEV.BankInformation.AccountNumber, "-", "")
        _Bank_AccountNumber_CustomersFrom = AlignStr(_Bank_AccountNumber_CustomersFrom, eDirection.Right, "0", 24)
        _Bank_AccountNumber_CustomersFrom = AlignStr(Replace(_Bank_AccountNumber_CustomersFrom, "-", ""), 1, " ", 47)

        Dim _FileStream As New FileStream(_BankParameterDEV.FolderName & "/" & _FileName, FileMode.Create, FileAccess.ReadWrite)
        Dim _StreamWriter As New StreamWriter(_FileStream, System.Text.Encoding.ASCII)

        For I = 0 To _selection.Count - 1
            _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(_selection(I), RegistryBankTransferView).Oid)
            If _RegistryBankTransfer.IsBlocked = False Then
                If _RegistryBankTransfer.RegistryHeader.Customers.Name = "" Then
                    _RegistryBankTransfer.RegistryHeader.Customers.Name = _RegistryBankTransfer.RegistryHeader.Customers.FullName
                    _ocur.Session.FlushChanges()
                End If
                _Customers_Name = _RegistryBankTransfer.RegistryHeader.Customers.Name
                _Bank_AccountNumber_Customers = Replace(_RegistryBankTransfer.RegistryHeader.F_CustomerBankAccounts.AccountNumber, "-", "")
                _Bank_AccountNumber_Customers = AlignStr(_Bank_AccountNumber_Customers, eDirection.Right, "0", 24)
                _Bank_AccountNumber_Customers = AlignStr(Replace(_Bank_AccountNumber_Customers, "-", ""), 1, " ", 47)

                '_Amount = Replace(LTrim(RTrim(CStr(Math.Round((TryCast(_selection(I), RegistryBankTransferView).AmountDEV), 2, MidpointRounding.AwayFromZero)))), ".", "")

                _Amount = Replace(LTrim(RTrim(Format(Math.Round((TryCast(_selection(I), RegistryBankTransferView).AmountDEV), 2, MidpointRounding.AwayFromZero), "0.00"))), ",", "")

                _DocumentNumber = _RegistryBankTransfer.RegistryHeader.DocumentNumber
                _Note = _RegistryBankTransfer.Note
                _Customers_Name = AlignStr(Replace(_Customers_Name, Chr(13) & Chr(10), " "), 1, " ", 32)
                _Amount = AlignStr(Replace(_Amount, ".", ""), 0, "0", 13) ' A replace-re nem biztos, hogy szükség van.

                'Tranzakció
                _BankString = "PAYORD" 'Rekord típus.
                _BankString = _BankString & "IN" 'Devizaátutalás.
                _BankString = _BankString & _Today & "000000" 'Azonosító.

                'Fealadó
                _BankString = _BankString & _Bank_AccountNumber_CustomersFrom 'Számlaszám.
                _BankString = _BankString & "0" 'Számlaszám típusa: 24 jegyű GIRO számlaszám esetén 0, nemzetközi számlaszám esetén 9.
                _BankString = _BankString & ANSITo852(AlignStr(Replace(_CustomersFrom_Name, Chr(13) & Chr(10), " "), 1, " ", 140)) 'Feladó neve.

                _BankString = _BankString & AlignStr(" ", 1, " ", 9) 'A következő mezőnek a 220. pozíción kell kezdődnie.

                'Címzett
                _BankString = _BankString & _Bank_AccountNumber_Customers 'Számlaszám.
                _BankString = _BankString & "0" 'Számlaszám típusa: 24 jegyű GIRO számlaszám esetén 0, nemzetközi számlaszám esetén 9.
                _BankString = _BankString & AlignStr(_RegistryBankTransfer.RegistryHeader.F_CustomerBankAccounts.ShortName, 1, " ", 64) 'Bank neve.
                _BankString = _BankString & ANSITo852(AlignStr(Replace(_Customers_Name, Chr(13) & Chr(10), " "), 1, " ", 140)) 'Címzett neve.
                _BankString = _BankString & "HU" 'Országkód - a régi rendszerben is így volt...
                _BankString = _BankString & AlignStr(_RegistryBankTransfer.RegistryHeader.F_CustomerBankAccounts.SWIFT, 0, " ", 15) 'SWIFT kód.
                _BankString = _BankString & AlignStr(" ", 1, " ", 35) 'Levelező bank.

                'Megjegyzés.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'Rezidens (Y/N).
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'SWIFT üzenetről másolatot kér (Y/N).
                _BankString = _BankString & AlignStr(" ", 1, " ", 15) 'Fax szám, ahova a SWIFT másolatot kéri.
                _BankString = _BankString & AlignStr(" ", 1, " ", 16) 'Kitöltő karakterek.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1)  'Nem kér egyedi árfolyamot (Y/N).
                _BankString = _BankString & AlignStr(" ", 1, " ", 35) 'Banknak szóló közlemény, nem használt.
                _BankString = _BankString & AlignStr(" ", 1, " ", 95) 'Közlemény.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'Kitöltő karakter.

                'Stat. információ.
                _BankString = _BankString & AlignStr(" ", 1, " ", 4) 'Jogcímkód.
                _BankString = _BankString & AlignStr(" ", 1, " ", 60) 'Kitöltő karakterekek.
                _BankString = _BankString & AlignStr(" ", 1, " ", 24) 'Dev. eng. száma.
                _BankString = _BankString & AlignStr(" ", 1, " ", 8) 'Dev. eng. dátuma.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'Sürgős teljesítés használata.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'Sürgős teljesítés.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'VIBER.
                _BankString = _BankString & AlignStr(" ", 1, " ", 1) 'Kitöltő karakter.

                'Összeg.
                _BankString = _BankString & AlignStr(_BankParameterDEV.CurrencyName.ShortName, 1, " ", 3) 'Kifizetendő deviza - a popupról vesszük.
                _BankString = _BankString & AlignStr(" ", 1, " ", 14) 'Kitöltő karakterekek.
                _BankString = _BankString & AlignStr(_RegistryBankTransfer.RegistryHeader.F_CurrencyName.ShortName, 1, " ", 3) 'Átutalandó deviza.
                _BankString = _BankString & "2" 'Tizedesjegyek száma.
                _BankString = _BankString & AlignStr(_Amount, 0, "0", 13) 'Átutalandó összeg.
                _BankString = _BankString & AlignStr(" ", 1, " ", 12) 'Kitöltő karakterekek.

                'Dátum.
                _BankString = _BankString & _Today 'Valutanap.
                _BankString = _BankString & AlignStr(" ", 1, " ", 12) 'Kitöltő karakterekek.

                'Egyéb.
                _BankString = _BankString & _BankParameterDEV.BankCost 'Költségviselés.
                _BankString = _BankString & AlignStr(" ", 1, " ", 82) 'Kitöltő karakterekek.
                _BankString = _BankString & "00" 'Státusz.

                'Rekordelválasztó
                _BankString = _BankString & Chr(13) & Chr(10)

                _StreamWriter.Write(_BankString)
                _RegistryBankTransfer.BankSavedFileName = _FileName
                _RegistryBankTransfer.Save()
            End If
        Next I
        _StreamWriter.Close()
        _BankParameterDEV.TransferTotalDEV = 0
        _BankParameterDEV.TransferCount = 0
        _BankParameterDEV.TransferTotalHUF = 0
        _BankParameterDEV.BankInformation = Nothing
        _BankParameterDEV.Save()
        e.PopupWindow.View.ObjectSpace.CommitChanges()
        _ocur.CommitChanges()
    End Sub
    Private Function ANSITo852(ByVal S As String) As String
        '[ü]  129  08/01  201  81  u diaeresis
        '[é]  130  08/02  202  82  e acute
        '[Ő]  138  08/10  212  8A  O double acute accent
        '[ő]  139  08/11  213  8B  o double acute accent
        '[É]  144  09/00  220  90  E acute
        '[ĺ]  146  09/02  222  92  l acute
        '[ö]  148  09/04  224  94  o diaeresis
        '[Ö]  153  09/09  231  99  O diaeresis
        '[Ü]  154  09/10  232  9A  U diaeresis
        '[á]  160  10/00  240  A0  a acute
        '[í]  161  10/01  241  A1  i acute
        '[ó]  162  10/02  242  A2  o acute
        '[ú]  163  10/03  243  A3  u acute
        '[Á]  181  11/05  265  B5  A acute
        '[Í]  214  13/06  326  D6  I acute
        '[Ó]  224  14/00  340  E0  O acute
        '[Ú]  233  14/09  351  E9  U acute
        '[Ű]  235  14/11  353  EB  U double acute
        '[ű]  251  15/11  373  FB  u double acute

        Dim I As Integer
        Dim seged As String
        Dim ch As String

        seged = ""
        For I = 1 To Len(S)
            ch = Mid(S, I, 1)
            Select Case ch
                'Case "ó" : ch = Chr(162)
                'Case "ű" : ch = Chr(251)
                'Case "ú" : ch = Chr(163)
                'Case "ő" : ch = Chr(139)
                'Case "á" : ch = Chr(160)
                'Case "é" : ch = Chr(130)
                'Case "í" : ch = Chr(161)
                'Case "ü" : ch = Chr(129)
                'Case "ű" : ch = Chr(129)
                'Case "Ó" : ch = Chr(224)
                'Case "Ű" : ch = Chr(235)
                'Case "Ú" : ch = Chr(233)
                'Case "Ő" : ch = Chr(138)
                'Case "Á" : ch = Chr(181)
                'Case "É" : ch = Chr(144)
                'Case "Í" : ch = Chr(214)
                'Case "Ü" : ch = Chr(154)
                'Case "Ű" : ch = Chr(154)
                Case "ö" : ch = "o"
                Case "ó" : ch = "o"
                Case "ű" : ch = "u"
                Case "ú" : ch = "u"
                Case "ő" : ch = "o"
                Case "á" : ch = "a"
                Case "é" : ch = "e"
                Case "í" : ch = "i"
                Case "ü" : ch = "u"
                Case "ű" : ch = "u"
                Case "Ó" : ch = "O"
                Case "Ű" : ch = "u"
                Case "Ú" : ch = "U"
                Case "Ő" : ch = "O"
                Case "Á" : ch = "A"
                Case "É" : ch = "E"
                Case "Í" : ch = "I"
                Case "Ü" : ch = "U"
                Case "Ű" : ch = "U"
                Case "Ö" : ch = "O"
            End Select
            seged = seged & ch
        Next I
        ANSITo852 = seged
    End Function
#End Region

    Private Sub aSaveElectraHUF_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aSaveElectraHUF.Cancel
        _noselect = False
    End Sub
    Private Sub aSaveElectraHUF_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSaveElectraHUF.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _BankParameterDEV As BankParameterDEV
        Dim _TransferTotalDEV As Double = 0
        Dim _TransferTotalHUF As Double = 0
        Dim _RegistryBankTransferView As RegistryBankTransferView
        Dim I As Long

        If IsSameCustomerFrom(_selection) = False Then
            Throw New UserFriendlyException("Csak azonos Saját cég esetén választható ez a funkció!")
        End If

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        _BankParameterDEV = _ocur.FindObject(Of BankParameterDEV)(CriteriaOperator.Parse("CurrencyName.ShortName='HUF'"), True)
        If _BankParameterDEV Is Nothing Then _BankParameterDEV = _ocur.CreateObject(Of BankParameterDEV)()

        _BankParameterDEV.DateExecution = Now
        _BankParameterDEV.TransferTotalDEV = 0
        _BankParameterDEV.TransferCount = 0
        _BankParameterDEV.TransferTotalHUF = 0
        _BankParameterDEV.TransferCount = _selection.Count
        _BankParameterDEV.CustomFileName = ""
        For I = 0 To _selection.Count - 1
            _RegistryBankTransferView = TryCast(_selection(I), RegistryBankTransferView)
            If _RegistryBankTransferView.IsBlocked = False Then
                If I = 0 Then _BankParameterDEV.CustomersFrom = _RegistryBankTransferView.RegistryHeader.CustomersFrom
                _TransferTotalHUF += _RegistryBankTransferView.AmountHUF
                _TransferTotalDEV += _RegistryBankTransferView.AmountDEV
            End If
        Next I
        _BankParameterDEV.TransferTotalDEV = _TransferTotalDEV
        _BankParameterDEV.TransferTotalHUF = _TransferTotalHUF

        _noselect = True
        _BankParameterDEV.Save()
        _ocur.CommitChanges()

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)

        Dim _BankParameterDEV_New As BankParameterDEV = _onew.GetObject(_BankParameterDEV)
        e.View = Application.CreateDetailView(_onew, "BankParameterDEV_DetailView", False, _BankParameterDEV_New)

    End Sub
    Private Sub aSaveElectraHUF_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSaveElectraHUF.Execute
        Me.DownloadToMKBHUF(View, e)
        _selection.Clear()
        _noselect = False
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub

    Private Sub aSaveElectraDEV_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSaveElectraDEV.Execute
        Me.DownloadToMKBDEV(View, e)
        _selection.Clear()
        _noselect = False
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub

    Private Function IsSameCustomerFrom(ByVal _selection As ArrayList) As Boolean
        Dim _RegistryBankTransferView = TryCast(_selection(0), RegistryBankTransferView)

        For I As Int32 = 1 To _selection.Count - 1
            If _RegistryBankTransferView.RegistryHeader.CustomersFrom.Oid <> _selection(I).RegistryHeader.CustomersFrom.Oid Then
                Return False
            End If
        Next

        Return True
    End Function

    Private Sub aSaveElectraDEV_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aSaveElectraDEV.Cancel
        _noselect = False
    End Sub

    Private Sub aSaveElectraDEV_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSaveElectraDEV.CustomizePopupWindowParams
        aSaveElectraHUF_CustomizePopupWindowParams(sender, e)
    End Sub

    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub

    Private Sub view_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _RegistryBankTransferView As RegistryBankTransferView
        If View.Id Like "RegistryBankTransfer_ListView*" And _noselect = False Then
            _selection.Clear()
            For Each _RegistryBankTransferView In View.SelectedObjects
                _selection.Add(_RegistryBankTransferView)
            Next
        End If
    End Sub

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

        Dim _RegistryBankTransfer As RegistryBankTransfer

        For I = 0 To View.SelectedObjects.Count - 1
            _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(View.SelectedObjects(I), RegistryBankTransferView).Oid)
            _RegistryBankTransfer.BankSavedFileName = ""
            _RegistryBankTransfer.Save()
        Next I
        View.ObjectSpace.CommitChanges()
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub

    Private Sub aViewRegistry_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RegistryBankTransferView As RegistryBankTransferView = TryCast(View.SelectedObjects(0), RegistryBankTransferView)

        If _RegistryBankTransferView IsNot Nothing Then
            Dim _RegistryBankTransfer As RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(_RegistryBankTransferView.Oid)
            If _RegistryBankTransfer IsNot Nothing Then
                Dim _RegistryHeader As RegistryHeader = _onew.GetObject(_RegistryBankTransfer.RegistryHeader)
                e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "RegistryHeader_DetailView_BankTransfer", False, _RegistryHeader)
            End If

        End If

    End Sub

    Private Sub aBankDivide_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aBankDivide.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _GLBankDivide As GLBankDivide = _onew.CreateObject(Of GLBankDivide)()
        Dim _RegistryBankTransfer As RegistryBankTransfer = _onew.GetObjectByKey(Of RegistryBankTransfer)(TryCast(View.SelectedObjects(0), RegistryBankTransferView).Oid)
        _GLBankDivide.RegistryBankTransfer_AmountDEV = _RegistryBankTransfer.AmountDEV
        _GLBankDivide.RegistryBankTransfer_AmountHUF = _RegistryBankTransfer.AmountHUF
        _GLBankDivide.RegistryBankTransfer_RegistryHeader_CurrencyName = _RegistryBankTransfer.RegistryHeader.F_CurrencyName.ShortName
        _GLBankDivide.Note = _RegistryBankTransfer.Note
        e.View = Application.CreateDetailView(_onew, "GLBankDivide_DetailView", False, _GLBankDivide)
    End Sub

    Private Sub aBankDivide_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aBankDivide.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLBankDivide As GLBankDivide = TryCast(e.PopupWindow.View.SelectedObjects(0), GLBankDivide)

        Dim _oid_RegistryBankTransfer As Guid = TryCast(View.SelectedObjects(0), RegistryBankTransferView).Oid
        Dim _RegistryBankTransfer As RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(_oid_RegistryBankTransfer)

        If Not _RegistryBankTransfer.RegistryHeader.BruttoAmountRemainingBank = 0 Then
            Throw New Exception("*A számla banki csomagjai nem fedik a számla teljes összegét, kérem ellenőrizze!")
        End If

        If _GLBankDivide.AmountDEV > _RegistryBankTransfer.AmountDEV Or _GLBankDivide.AmountHUF > _RegistryBankTransfer.AmountHUF Then
            Throw New Exception("*A megadott adatok meghaladják a csomag értékét!")
        End If

        If _GLBankDivide.AmountPercent > 0 Then
            Dim _RegistryBankTransfer_New As RegistryBankTransfer = _ocur.CreateObject(Of RegistryBankTransfer)()
            _RegistryBankTransfer_New.RegistryHeader = _RegistryBankTransfer.RegistryHeader
            _RegistryBankTransfer_New.AmountHUF = Math.Round((_RegistryBankTransfer.AmountHUF * (_GLBankDivide.AmountPercent / 100)), 0, MidpointRounding.AwayFromZero)
            If Not _RegistryBankTransfer.RegistryHeader.F_CurrencyName.ShortName = "HUF" Then
                _RegistryBankTransfer_New.AmountDEV = Math.Round(_RegistryBankTransfer.AmountDEV * (_GLBankDivide.AmountPercent / 100), 0, MidpointRounding.AwayFromZero)
                _RegistryBankTransfer_New.AmountHUF = Math.Round((_RegistryBankTransfer_New.AmountDEV * _RegistryBankTransfer.RegistryHeader.F_CurrencyRate), 0, MidpointRounding.AwayFromZero)
            End If
            _RegistryBankTransfer_New.DatePayment = _GLBankDivide.DatePayment
            _RegistryBankTransfer_New.Note = _GLBankDivide.Note
            _RegistryBankTransfer_New.Save()

            _RegistryBankTransfer.AmountHUF -= _RegistryBankTransfer_New.AmountHUF
            If Not _RegistryBankTransfer.RegistryHeader.F_CurrencyName.ShortName = "HUF" Then
                _RegistryBankTransfer.AmountDEV -= _RegistryBankTransfer_New.AmountDEV
            End If
            _RegistryBankTransfer.Save()

        ElseIf _RegistryBankTransfer.RegistryHeader.F_CurrencyName.ShortName = "HUF" And _GLBankDivide.AmountHUF > 0 Then

            Dim _RegistryBankTransfer_New As RegistryBankTransfer = _ocur.CreateObject(Of RegistryBankTransfer)()
            _RegistryBankTransfer_New.RegistryHeader = _RegistryBankTransfer.RegistryHeader
            _RegistryBankTransfer_New.AmountHUF = _GLBankDivide.AmountHUF

            _RegistryBankTransfer_New.DatePayment = _GLBankDivide.DatePayment
            _RegistryBankTransfer_New.Note = _GLBankDivide.Note
            _RegistryBankTransfer_New.Save()

            _RegistryBankTransfer.AmountHUF -= _RegistryBankTransfer_New.AmountHUF

            _RegistryBankTransfer.Save()

        Else

            Dim _RegistryBankTransfer_New As RegistryBankTransfer = _ocur.CreateObject(Of RegistryBankTransfer)()
            _RegistryBankTransfer_New.RegistryHeader = _RegistryBankTransfer.RegistryHeader
            _RegistryBankTransfer_New.AmountDEV = _GLBankDivide.AmountDEV
            _RegistryBankTransfer_New.AmountHUF = _RegistryBankTransfer_New.AmountDEV * _RegistryBankTransfer.RegistryHeader.F_CurrencyRate

            _RegistryBankTransfer_New.DatePayment = _GLBankDivide.DatePayment
            _RegistryBankTransfer_New.Note = _GLBankDivide.Note
            _RegistryBankTransfer_New.Save()

            _RegistryBankTransfer.AmountHUF -= _RegistryBankTransfer_New.AmountHUF
            _RegistryBankTransfer.AmountDEV -= _RegistryBankTransfer_New.AmountDEV
            _RegistryBankTransfer.Save()

        End If

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

    Private Sub aBankMerge_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBankMerge.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryBankTransfer As RegistryBankTransfer = Nothing
        Dim _RegistryBankTransfer_Base As RegistryBankTransfer = Nothing

        Dim _DelArray As New ArrayList
        Dim _AmountHUF As Double = 0
        Dim _AmountDEV As Double = 0

        Dim _oid As Guid
        _DelArray.Clear()
        If _selection Is Nothing Or _selection.Count <= 0 Then Exit Sub
        For I = 0 To _selection.Count - 1
            _oid = TryCast(_selection(I), RegistryBankTransferView).Oid
            If I = 0 Then
                _RegistryBankTransfer_Base = _ocur.GetObjectByKey(Of RegistryBankTransfer)(_oid)
            Else
                _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(_oid)
                If _RegistryBankTransfer.RegistryHeader.Oid = _RegistryBankTransfer_Base.RegistryHeader.Oid Then
                    _AmountDEV += _RegistryBankTransfer.AmountDEV
                    _AmountHUF += _RegistryBankTransfer.AmountHUF
                    _DelArray.Add(_RegistryBankTransfer)
                End If
            End If

        Next
        _RegistryBankTransfer_Base.AmountHUF += _AmountHUF
        _RegistryBankTransfer_Base.AmountDEV += _AmountDEV

        For i = 0 To _DelArray.Count - 1
            _ocur.Delete(_DelArray(i))
        Next

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

    Private Sub aViewAttachments_Bank_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_Bank.Execute
        Dim _RegistryBankTransferView As RegistryBankTransferView = TryCast(View.SelectedObjects(0), RegistryBankTransferView)
        Dim _CustomersFrom As CustomersFrom = _RegistryBankTransferView.RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RegistryBankTransferView.RegistryHeader.Customers
        Dim _DocumentNumber As String = _RegistryBankTransferView.RegistryHeader.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")

    End Sub

    Private Sub aBankPrepareName_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aBankPrepareName.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RegistryBankTransferView As RegistryBankTransferView = TryCast(View.SelectedObjects(0), RegistryBankTransferView)
        Dim _GLBankPrepareName As GLBankPrepareName = _onew.CreateObject(Of GLBankPrepareName)()
        If _RegistryBankTransferView.BankPrepareName IsNot Nothing Then
            _GLBankPrepareName.BankPrepareName = _RegistryBankTransferView.BankPrepareName
        End If
        e.View = Application.CreateDetailView(_onew, "GLBankPrepareName_DetailView", False, _GLBankPrepareName)
    End Sub

    Private Sub aBankPrepareName_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aBankPrepareName.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLBankPrepareName As GLBankPrepareName = TryCast(e.PopupWindow.View.SelectedObjects(0), GLBankPrepareName)
        Dim _RegistryBankTransfer As RegistryBankTransfer

        For I = 0 To View.SelectedObjects.Count - 1
            _RegistryBankTransfer = _ocur.GetObjectByKey(Of RegistryBankTransfer)(TryCast(View.SelectedObjects(I), RegistryBankTransferView).Oid)
            _RegistryBankTransfer.BankPrepareName = _GLBankPrepareName.BankPrepareName
            _RegistryBankTransfer.Save()
        Next I
        View.ObjectSpace.CommitChanges()

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

    Private Sub aBankNoTransfer_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aBankNoTransfer.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _BankNoTransferPopup As BankNoTransferPopup = _onew.CreateObject(Of BankNoTransferPopup)()
        e.View = Application.CreateDetailView(_onew, "BankNoTransferPopup_DetailView", False, _BankNoTransferPopup)
    End Sub

    Private Sub aBankNoTransfer_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aBankNoTransfer.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RegistryBankTransfer As RegistryBankTransfer
        Dim i As Long = 0
        Dim _BankNoTransferPopup As BankNoTransferPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), BankNoTransferPopup)

        If _selection Is Nothing Or _selection.Count <= 0 Then Exit Sub
        For i = 0 To _selection.Count - 1
            _RegistryBankTransfer = _uow.GetObjectByKey(Of RegistryBankTransfer)(TryCast(_selection(i), RegistryBankTransferView).Oid)
            _RegistryBankTransfer.BankSavedFileName = _BankNoTransferPopup.Description
        Next i
        _uow.CommitChanges()
        _selection.Clear()
        _noselect = False
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub

    Private Sub aViewPDCI_Bank_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPDCI_Bank.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RegistryBankTransferView As RegistryBankTransferView = TryCast(View.SelectedObjects(0), RegistryBankTransferView)
        Dim _CustomersFrom As CustomersFrom = _RegistryBankTransferView.RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RegistryBankTransferView.RegistryHeader.Customers
        Dim _DocumentNumber As String = _RegistryBankTransferView.RegistryHeader.DocumentNumber

        ViewPDCI.ViewPDCIDetail(_onew, Application, e, _CustomersFrom, _Customers, 1, _DocumentNumber)

    End Sub
End Class

'ReadOnly Property BallanceHUF As Double
'    Get
'        Dim _PCIGroup As PCIGroup = _
'            Session.FindObject(Of PCIGroup) _
'            (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and PartnerType=1 and ConnectInfo=?", _
'            Me.RegistryHeader.CustomersFrom, Me.RegistryHeader.Customers, Me.RegistryHeader.DocumentNumber), True)
'        If _PCIGroup IsNot Nothing Then
'            Return _PCIGroup.BallanceHUF
'        Else
'            Return Me.RegistryHeader.F_AmountBruttoHUF
'        End If
'    End Get
'End Property