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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.Linq
Imports DevExpress.ExpressApp.Editors

Public Class PartnerAccountConnectionInfoQueryVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected selection As ArrayList
    Protected noselect As Boolean = False
    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()
        Frame.GetController(Of PartnerAccountConnectionInfoQueryVC).aSelectCustomers.Active.SetItemValue("", False)
        Frame.GetController(Of PartnerAccountConnectionInfoQueryVC).aSelectCustomersFrom.Active.SetItemValue("", False)
        If View.Id = "PartnerAccountConnectionInfoQueryHeader_PartnerAccountConnectionInfoQuerySelectedCustomers_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of PartnerAccountConnectionInfoQueryVC).aSelectCustomers.Active.SetItemValue("", True)
        End If
        If View.Id = "PartnerAccountConnectionInfoQueryHeader_PartnerAccountConnectionInfoQuerySelectedCustomersFrom_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of PartnerAccountConnectionInfoQueryVC).aSelectCustomersFrom.Active.SetItemValue("", True)
        End If
        If View.Id = "Customers_ListView_Select" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)

        End If
        If View.Id = "CustomersFrom_ListView_Select" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub aGenerateReport_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateReport.Execute
        '-- Az adott záródátumra vonatkozó partner connection infot adja meg
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim i As Long = 0
        Dim _Need As Boolean = False
        Dim _Crs As String = ""
        Dim _Crs1 As String = ""
        Dim _Cri As Long = 0
        Dim _PartnerAccountConnectionInfoQuerySelectedCustomers As PartnerAccountConnectionInfoQuerySelectedCustomers
        Dim _PartnerAccountConnectionInfoQuerySelectedCustomersFrom As PartnerAccountConnectionInfoQuerySelectedCustomersFrom
        Dim _PCIDetail As PCIDetail
        Dim _PCIDetail_Collection As XPCollection(Of PCIDetail)
        Dim _PartnerAccountConnectionInfoQueryRows As PartnerAccountConnectionInfoQueryRows
        Dim _PartnerAccountConnectionInfoQueryPartner As PartnerAccountConnectionInfoQueryPartner
        Dim _PartnerAccountConnectionInfoQueryHeader As PartnerAccountConnectionInfoQueryHeader = _uow.GetObjectByKey(Of PartnerAccountConnectionInfoQueryHeader) _
            (TryCast(View.SelectedObjects(0), PartnerAccountConnectionInfoQueryHeader).Oid)

        If _PartnerAccountConnectionInfoQueryHeader Is Nothing Then Exit Sub

        If _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQuerySelectedCustomersFrom.Count <= 0 Then
            Throw New Exception("*Legalább egy saját céget ki kell választani !")
            Exit Sub
        End If

        _PartnerAccountConnectionInfoQueryHeader.Save()

        Dim _PartnerAccountConnectionInfoQueryRows_Collection As New XPCollection(Of PartnerAccountConnectionInfoQueryRows) _
            (_uow, CriteriaOperator.Parse("PartnerAccountConnectionInfoQueryHeader=?", _PartnerAccountConnectionInfoQueryHeader))
        _uow.Delete(_PartnerAccountConnectionInfoQueryRows_Collection)

        Dim _PartnerAccountConnectionInfoQueryPartner_Collection As New XPCollection(Of PartnerAccountConnectionInfoQueryPartner) _
            (_uow, CriteriaOperator.Parse("PartnerAccountConnectionInfoQueryHeader=?", _PartnerAccountConnectionInfoQueryHeader))
        _uow.Delete(_PartnerAccountConnectionInfoQueryPartner_Collection)

        '-- Lekérdezés, adott teljesítési dátumig történõ lekérdezés 
        If _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQuerySelectedCustomers.Count > 0 Then
            _Crs += " (Customers.Oid in ("
            For Each _PartnerAccountConnectionInfoQuerySelectedCustomers In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQuerySelectedCustomers
                If _Cri = 0 Then
                    _Crs += "'" & _PartnerAccountConnectionInfoQuerySelectedCustomers.Customers.Oid.ToString & "'"
                    _Cri += 1
                Else
                    _Crs += ",'" & _PartnerAccountConnectionInfoQuerySelectedCustomers.Customers.Oid.ToString & "'"
                End If
            Next
            _Crs += ")) "
        End If
        _Cri = 0
        _Crs1 += " (CustomersFrom.Oid in ("
        For Each _PartnerAccountConnectionInfoQuerySelectedCustomersFrom In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQuerySelectedCustomersFrom
            If _Cri = 0 Then
                _Crs1 += "'" & _PartnerAccountConnectionInfoQuerySelectedCustomersFrom.CustomersFrom.Oid.ToString & "'"
                _Cri += 1
            Else
                _Crs1 += ",'" & _PartnerAccountConnectionInfoQuerySelectedCustomersFrom.CustomersFrom.Oid.ToString & "'"
            End If
        Next
        _Crs1 += ")) "

        If _Crs <> "" Then
            _PCIDetail_Collection = New XPCollection(Of PCIDetail) _
             (_uow, CriteriaOperator.Parse(_Crs1 & " and GetDate(DateExecution) <=? and PartnerType=? and " & _Crs, _PartnerAccountConnectionInfoQueryHeader.DateClose.Date, _
              _PartnerAccountConnectionInfoQueryHeader.PartnerType))
        Else
            _PCIDetail_Collection = New XPCollection(Of PCIDetail) _
            (_uow, CriteriaOperator.Parse(_Crs1 & " and GetDate(DateExecution) <=? and PartnerType=?", _PartnerAccountConnectionInfoQueryHeader.DateClose.Date, _
             _PartnerAccountConnectionInfoQueryHeader.PartnerType))

        End If


        RaiseEvent InitWork(1, 1, _PCIDetail_Collection.Count)
        For Each _PCIDetail In _PCIDetail_Collection
            RaiseEvent DoWork()
            i += 1

            _PartnerAccountConnectionInfoQueryRows = New PartnerAccountConnectionInfoQueryRows(_uow)
            _PartnerAccountConnectionInfoQueryRows.PartnerAccountConnectionInfoQueryHeader = _PartnerAccountConnectionInfoQueryHeader
            If _PCIDetail.DateExecution.Date < _PartnerAccountConnectionInfoQueryHeader.DateOpen.Date Then
                _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eOpen
                _PartnerAccountConnectionInfoQueryRows.ForDelete = True
                If _PCIDetail.CurrencyName_Account Is Nothing Then
                    _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName
                Else
                    _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName_Account
                End If
            Else
                Select Case _PCIDetail.MainType
                    Case "01-Account"
                        _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eAccounts
                        _PartnerAccountConnectionInfoQueryRows.ForDelete = True
                        _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName_Account
                        _PartnerAccountConnectionInfoQueryRows.DateExecution = _PCIDetail.DateExecution_Account
                        _PartnerAccountConnectionInfoQueryRows.DatePayment = _PCIDetail.DatePayment_Account
                        _PartnerAccountConnectionInfoQueryRows.AmountDEV = _PCIDetail.AmountDEV_Account
                        _PartnerAccountConnectionInfoQueryRows.AmountHUF = _PCIDetail.AmountHUF_Account
                        _PartnerAccountConnectionInfoQueryRows.DocumentNumber = _PCIDetail.DocumentNumber
                    Case Else
                        If _PCIDetail.CurrencyName_Account Is Nothing Then
                            _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eFinancialNoConnect
                            _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName
                            _PartnerAccountConnectionInfoQueryRows.DateExecution = _PCIDetail.DateExecution
                            _PartnerAccountConnectionInfoQueryRows.DocumentNumber = _PCIDetail.DocumentNumber
                        Else
                            If _PCIDetail.DateExecution_Account.Date < _PartnerAccountConnectionInfoQueryHeader.DateOpen.Date Then
                                _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eFinancialBefore
                                _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName_Account
                                _PartnerAccountConnectionInfoQueryRows.DateExecution = _PCIDetail.DateExecution_Account
                                _PartnerAccountConnectionInfoQueryRows.DatePayment = _PCIDetail.DatePayment_Account
                                _PartnerAccountConnectionInfoQueryRows.DocumentNumber = _PCIDetail.DocumentNumber
                            Else
                                _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eAccounts
                                _PartnerAccountConnectionInfoQueryRows.ForDelete = True
                                _PartnerAccountConnectionInfoQueryRows.CurrencyName = _PCIDetail.CurrencyName_Account
                                _PartnerAccountConnectionInfoQueryRows.DateExecution = _PCIDetail.DateExecution_Account
                                _PartnerAccountConnectionInfoQueryRows.DatePayment = _PCIDetail.DatePayment_Account
                                _PartnerAccountConnectionInfoQueryRows.DocumentNumber = _PCIDetail.DocumentNumber
                            End If
                        End If


                End Select
            End If
            _PartnerAccountConnectionInfoQueryRows.CustomersFrom = _PCIDetail.CustomersFrom
            _PartnerAccountConnectionInfoQueryRows.Customers = _PCIDetail.Customers
            _PartnerAccountConnectionInfoQueryRows.PartnerType = _PartnerAccountConnectionInfoQueryHeader.PartnerType
            _PartnerAccountConnectionInfoQueryRows.ConnectInfo = _PCIDetail.ConnectInfo
            _PartnerAccountConnectionInfoQueryRows.BallanceHUF = _PCIDetail.BallanceHUF
            _PartnerAccountConnectionInfoQueryRows.BallanceDEV = _PCIDetail.BallanceDEV
            _PartnerAccountConnectionInfoQueryRows.DocumentNumber = _PCIDetail.DocumentNumber

            If i Mod 1000 Then _uow.CommitChanges()

        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork

        '-- Most kell az XPQuery csoportosítás !!!
        '-- Nyitók helyrerakása
        Dim query = From _Open In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQueryRows
                    Where _Open.RowType = ePACIQRows.eOpen And
                          _Open.PartnerAccountConnectionInfoQueryHeader Is _PartnerAccountConnectionInfoQueryHeader
            Group _Open By _Open.CustomersFrom, _
                           _Open.Customers, _
                           _Open.PartnerType, _
                           _Open.CurrencyName Into g = Group _
            Select New With {Key .CustomersFrom = CustomersFrom, _
                             Key .PartnerType = PartnerType, _
                             Key .Customers = Customers, _
                             Key .CurrencyName = CurrencyName, _
                             Key .BallanceHUF = g.Sum(Function(inv) inv.BallanceHUF), _
                             Key .BallanceDEV = g.Sum(Function(inv) inv.BallanceDEV)}

        For Each item In query
            _PartnerAccountConnectionInfoQueryRows = New PartnerAccountConnectionInfoQueryRows(_uow)
            _PartnerAccountConnectionInfoQueryRows.ForDelete = False
            _PartnerAccountConnectionInfoQueryRows.PartnerAccountConnectionInfoQueryHeader = _PartnerAccountConnectionInfoQueryHeader
            _PartnerAccountConnectionInfoQueryRows.CustomersFrom = item.CustomersFrom
            _PartnerAccountConnectionInfoQueryRows.Customers = item.Customers
            _PartnerAccountConnectionInfoQueryRows.PartnerType = item.PartnerType
            _PartnerAccountConnectionInfoQueryRows.CurrencyName = item.CurrencyName
            _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eOpen
            _PartnerAccountConnectionInfoQueryRows.BallanceHUF = item.BallanceHUF
            _PartnerAccountConnectionInfoQueryRows.BallanceDEV = item.BallanceDEV
        Next

        _uow.CommitChanges()

        _PartnerAccountConnectionInfoQueryRows_Collection = New XPCollection(Of PartnerAccountConnectionInfoQueryRows) _
                    (_uow, CriteriaOperator.Parse("PartnerAccountConnectionInfoQueryHeader=? and RowType=0 and ForDelete=True", _PartnerAccountConnectionInfoQueryHeader))
        _uow.Delete(_PartnerAccountConnectionInfoQueryRows_Collection)


        _uow.CommitChanges()


        '-- Számlák helyrerakása
        Dim query1 = From _Open In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQueryRows
                    Where _Open.RowType = ePACIQRows.eAccounts And
                          _Open.PartnerAccountConnectionInfoQueryHeader Is _PartnerAccountConnectionInfoQueryHeader
            Group _Open By _Open.CustomersFrom, _
                           _Open.Customers, _
                           _Open.PartnerType, _
                           _Open.CurrencyName, _
                           _Open.ConnectInfo Into g = Group _
            Select New With {Key .CustomersFrom = CustomersFrom, _
                             Key .PartnerType = PartnerType, _
                             Key .Customers = Customers, _
                             Key .CurrencyName = CurrencyName, _
                             Key .ConnectInfo = ConnectInfo, _
                             Key .DocumentNumber = g.Max(Function(inv) inv.DocumentNumber), _
                             Key .DateExecution = g.Max(Function(inv) inv.DateExecution), _
                             Key .DatePayment = g.Max(Function(inv) inv.DatePayment), _
                             Key .BallanceHUF = g.Sum(Function(inv) inv.BallanceHUF), _
                             Key .BallanceDEV = g.Sum(Function(inv) inv.BallanceDEV)}

        For Each item In query1
            _Need = False
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eAll Then
                _Need = True
            End If
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eOpen Then
                If item.CurrencyName.ShortName = "HUF" Then
                    If Math.Round(item.BallanceHUF, 0, MidpointRounding.AwayFromZero) <> 0 Then
                        _Need = True
                    End If
                Else
                    If Math.Round(item.BallanceDEV, 2, MidpointRounding.AwayFromZero) <> 0 Then
                        _Need = True
                    End If
                End If
            End If
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eClose Then
                If item.CurrencyName.ShortName = "HUF" Then
                    If Math.Round(item.BallanceHUF, 0, MidpointRounding.AwayFromZero) = 0 Then
                        _Need = True
                    End If
                Else
                    If Math.Round(item.BallanceDEV, 2, MidpointRounding.AwayFromZero) = 0 Then
                        _Need = True
                    End If
                End If
            End If

            If _Need Then
                _PartnerAccountConnectionInfoQueryRows = New PartnerAccountConnectionInfoQueryRows(_uow)
                _PartnerAccountConnectionInfoQueryRows.ForDelete = False
                _PartnerAccountConnectionInfoQueryRows.PartnerAccountConnectionInfoQueryHeader = _PartnerAccountConnectionInfoQueryHeader
                _PartnerAccountConnectionInfoQueryRows.CustomersFrom = item.CustomersFrom
                _PartnerAccountConnectionInfoQueryRows.Customers = item.Customers
                _PartnerAccountConnectionInfoQueryRows.PartnerType = item.PartnerType
                _PartnerAccountConnectionInfoQueryRows.CurrencyName = item.CurrencyName
                _PartnerAccountConnectionInfoQueryRows.ConnectInfo = item.ConnectInfo
                _PartnerAccountConnectionInfoQueryRows.DocumentNumber = item.DocumentNumber
                _PartnerAccountConnectionInfoQueryRows.RowType = ePACIQRows.eAccounts
                _PartnerAccountConnectionInfoQueryRows.DatePayment = item.DatePayment
                _PartnerAccountConnectionInfoQueryRows.DateExecution = item.DateExecution
                _PartnerAccountConnectionInfoQueryRows.BallanceHUF = item.BallanceHUF
                _PartnerAccountConnectionInfoQueryRows.BallanceDEV = item.BallanceDEV
            End If
        Next

        _uow.CommitChanges()

        _PartnerAccountConnectionInfoQueryRows_Collection = New XPCollection(Of PartnerAccountConnectionInfoQueryRows) _
                    (_uow, CriteriaOperator.Parse("PartnerAccountConnectionInfoQueryHeader=? and RowType=1 and ForDelete=True", _PartnerAccountConnectionInfoQueryHeader))
        _uow.Delete(_PartnerAccountConnectionInfoQueryRows_Collection)

        _uow.CommitChanges()

        '-- Most kell a partner összesítés
        Dim query_partner = From _Open In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQueryRows
                    Where _Open.PartnerAccountConnectionInfoQueryHeader Is _PartnerAccountConnectionInfoQueryHeader
            Group _Open By _Open.CustomersFrom, _
                           _Open.Customers, _
                           _Open.PartnerType, _
                           _Open.CurrencyName Into g = Group _
            Select New With {Key .CustomersFrom = CustomersFrom, _
                             Key .PartnerType = PartnerType, _
                             Key .Customers = Customers, _
                             Key .CurrencyName = CurrencyName, _
                             Key .BallanceHUF = g.Sum(Function(inv) inv.BallanceHUF), _
                             Key .BallanceDEV = g.Sum(Function(inv) inv.BallanceDEV)}

        RaiseEvent InitWork(1, 1, query_partner.Count)
        For Each item In query_partner
            RaiseEvent DoWork()
            _Need = False
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eAll Then
                _Need = True
            End If
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eOpen Then
                If item.CurrencyName.ShortName = "HUF" Then
                    If Math.Round(item.BallanceHUF, 0, MidpointRounding.AwayFromZero) <> 0 Then
                        _Need = True
                    End If
                Else
                    If Math.Round(item.BallanceDEV, 2, MidpointRounding.AwayFromZero) <> 0 Then
                        _Need = True
                    End If
                End If
            End If
            If _PartnerAccountConnectionInfoQueryHeader.InfoType = eInfoType.eClose Then
                If item.CurrencyName.ShortName = "HUF" Then
                    If Math.Round(item.BallanceHUF, 0, MidpointRounding.AwayFromZero) = 0 Then
                        _Need = True
                    End If
                Else
                    If Math.Round(item.BallanceDEV, 2, MidpointRounding.AwayFromZero) = 0 Then
                        _Need = True
                    End If
                End If
            End If

            If _Need Then
                _PartnerAccountConnectionInfoQueryPartner = New PartnerAccountConnectionInfoQueryPartner(_uow)
                _PartnerAccountConnectionInfoQueryPartner.PartnerAccountConnectionInfoQueryHeader = _PartnerAccountConnectionInfoQueryHeader
                _PartnerAccountConnectionInfoQueryPartner.CustomersFrom = item.CustomersFrom
                _PartnerAccountConnectionInfoQueryPartner.Customers = item.Customers
                _PartnerAccountConnectionInfoQueryPartner.PartnerType = item.PartnerType
                _PartnerAccountConnectionInfoQueryPartner.CurrencyName_Account = item.CurrencyName
                _PartnerAccountConnectionInfoQueryPartner.BallanceDEV = item.BallanceDEV
                _PartnerAccountConnectionInfoQueryPartner.BallanceHUF = item.BallanceHUF
            End If
        Next

        _uow.CommitChanges()
        RaiseEvent FinalWork

        '// Egyéb infok egyenként kigenerálva !
        RaiseEvent InitWork(1, 1, _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQueryRows.Count)
        For Each _PartnerAccountConnectionInfoQueryRows In _PartnerAccountConnectionInfoQueryHeader.PartnerAccountConnectionInfoQueryRows
            RaiseEvent DoWork()
            If _PartnerAccountConnectionInfoQueryRows.PartnerType = ePartnerType.eReceivables Then
                Dim _InvoiceHeader As InvoiceHeader = _uow.FindObject(Of InvoiceHeader)(CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and DocumentNumber=?", _PartnerAccountConnectionInfoQueryRows.CustomersFrom.Oid, _PartnerAccountConnectionInfoQueryRows.Customers.Oid, _PartnerAccountConnectionInfoQueryRows.DocumentNumber))
                If _InvoiceHeader IsNot Nothing Then
                    _PartnerAccountConnectionInfoQueryRows.AmountNettoHUF = 0
                    _PartnerAccountConnectionInfoQueryRows.AmountVATHUF = 0
                    Dim _InvoiceVATRows As InvoiceVATRows
                    For Each _InvoiceVATRows In _InvoiceHeader.InvoiceVATRows
                        _PartnerAccountConnectionInfoQueryRows.AmountNettoHUF += _InvoiceVATRows.AmountNettoHUF
                        _PartnerAccountConnectionInfoQueryRows.AmountVATHUF += _InvoiceVATRows.AmountVATHUF
                    Next
                End If

            End If
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Function Query1() As System.Linq.IQueryable

    End Function

    Private Sub aSelectCustomers_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSelectCustomers.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace


        Dim _CS As New CollectionSource(_onew, GetType(Customers))

        '_CS.BeginUpdateCriteria()
        '_CS.Criteria.Clear()
        '_CS.Criteria("First") = ""
        '_CS.EndUpdateCriteria()

        e.View = Application.CreateListView("Customers_ListView_Select", _CS, False)

    End Sub

    Private Sub aSelectCustomers_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSelectCustomers.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _Customers As Customers
        Dim _PartnerAccountConnectionInfoQueryHeader As PartnerAccountConnectionInfoQueryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
        Dim _PartnerAccountConnectionInfoQuerySelectedCustomers As PartnerAccountConnectionInfoQuerySelectedCustomers

        If _PartnerAccountConnectionInfoQueryHeader Is Nothing Then Exit Sub

        If e.PopupWindow.View.SelectedObjects.Count > 0 Then

            For Each _Customers In e.PopupWindow.View.SelectedObjects
                _PartnerAccountConnectionInfoQuerySelectedCustomers = New PartnerAccountConnectionInfoQuerySelectedCustomers(_uow)
                _PartnerAccountConnectionInfoQuerySelectedCustomers.PartnerAccountConnectionInfoQueryHeader = _uow.GetObjectByKey(Of PartnerAccountConnectionInfoQueryHeader)(_PartnerAccountConnectionInfoQueryHeader.Oid)
                _PartnerAccountConnectionInfoQuerySelectedCustomers.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
            Next
            _uow.CommitChanges()
        End If

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If

    End Sub

    Private Sub aSelectCustomersFrom_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSelectCustomersFrom.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace


        Dim _CS As New CollectionSource(_onew, GetType(CustomersFrom))

        '_CS.BeginUpdateCriteria()
        '_CS.Criteria.Clear()
        '_CS.Criteria("First") = ""
        '_CS.EndUpdateCriteria()

        e.View = Application.CreateListView("CustomersFrom_ListView_Select", _CS, False)
    End Sub

    Private Sub aSelectCustomersFrom_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSelectCustomersFrom.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _Customersfrom As CustomersFrom
        Dim _PartnerAccountConnectionInfoQueryHeader As PartnerAccountConnectionInfoQueryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
        Dim _PartnerAccountConnectionInfoQuerySelectedCustomersfrom As PartnerAccountConnectionInfoQuerySelectedCustomersFrom

        If _PartnerAccountConnectionInfoQueryHeader Is Nothing Then Exit Sub

        If e.PopupWindow.View.SelectedObjects.Count > 0 Then

            For Each _Customersfrom In e.PopupWindow.View.SelectedObjects
                _PartnerAccountConnectionInfoQuerySelectedCustomersfrom = New PartnerAccountConnectionInfoQuerySelectedCustomersFrom(_uow)
                _PartnerAccountConnectionInfoQuerySelectedCustomersfrom.PartnerAccountConnectionInfoQueryHeader = _uow.GetObjectByKey(Of PartnerAccountConnectionInfoQueryHeader)(_PartnerAccountConnectionInfoQueryHeader.Oid)
                _PartnerAccountConnectionInfoQuerySelectedCustomersfrom.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_Customersfrom.Oid)
            Next
            _uow.CommitChanges()
        End If

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If

    End Sub

    Private Sub aViewPCID_PACIQ_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPCID_PACIQ.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PartnerAccountConnectionInfoQueryRows As PartnerAccountConnectionInfoQueryRows = TryCast(View.SelectedObjects(0), PartnerAccountConnectionInfoQueryRows)

        ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PartnerAccountConnectionInfoQueryRows.CustomersFrom, _
                          _PartnerAccountConnectionInfoQueryRows.Customers, _PartnerAccountConnectionInfoQueryRows.PartnerType, _
                          _PartnerAccountConnectionInfoQueryRows.ConnectInfo)
    End Sub

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

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _PartnerAccountConnectionInfoQueryRows In View.SelectedObjects
            RaiseEvent DoWork()
            GLFunctions.ReconfigurePCI(_uow, _PartnerAccountConnectionInfoQueryRows.CustomersFrom, _
                                       _PartnerAccountConnectionInfoQueryRows.Customers, _
                                       _PartnerAccountConnectionInfoQueryRows.PartnerType, _
                                       _PartnerAccountConnectionInfoQueryRows.ConnectInfo)

        Next
        RaiseEvent FinalWork
    End Sub

    Private Sub aCreateRounding_PACIQ_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateRounding_PACIQ.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RateDiffPopUp As RateDiffPopUp = _onew.CreateObject(Of RateDiffPopUp)()

        noselect = True
        _RateDiffPopUp.DateExecution = Now
        e.View = Application.CreateDetailView(_onew, "RateDiffPopUp_DetailView", False, _RateDiffPopUp)
    End Sub

    Private Sub aCreateRounding_PACIQ_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateRounding_PACIQ.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RateDiffPopUp As RateDiffPopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), RateDiffPopUp)
        Dim _PartnerAccountConnectionInfoQueryRows As PartnerAccountConnectionInfoQueryRows
        Dim _GLHeader_Rounding As GLMixed = Nothing
        Dim _GLRows_Rounding As GLRows
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts As ChartOfAccounts = Nothing
        Dim _Controlling As Controlling = Nothing
        Dim _GLMixedNG As GLMixedNG
        Dim _Rate As Double = 1


        Dim _GLHeader_RoundingParameter = _ocur.FindObject(Of GLHeader_RateDiffParameter)(CriteriaOperator.Parse("1=1"))
        If _GLHeader_RoundingParameter Is Nothing Then
            Throw New Exception("*Nincs beállítva az elengedési paraméter !")
            Exit Sub
        End If


        Dim _ChartOfAccounts_Rounding_Win As ChartOfAccounts = _GLHeader_RoundingParameter.GetChartOfAccounts(_RateDiffPopUp.DateExecution.Year, eWinLoss.eRoundingWin)
        Dim _ChartOfAccounts_Rounding_Loss As ChartOfAccounts = _GLHeader_RoundingParameter.GetChartOfAccounts(_RateDiffPopUp.DateExecution.Year, eWinLoss.eRoundingLoss)
        Dim _Controlling_Rounding_Win As Controlling = _GLHeader_RoundingParameter.Controlling_Rounding_Win
        Dim _Controlling_Rounding_Loss As Controlling = _GLHeader_RoundingParameter.Controlling_Rounding_Loss


        If _ChartOfAccounts_Rounding_Loss Is Nothing Or _
           _ChartOfAccounts_Rounding_Win Is Nothing Then
            Throw New Exception("*Nincs beállítva az elengedési paraméter !")
            Exit Sub
        End If

        Dim _CurrencyName As CurrencyName = Nothing
        Dim _CustomersFrom As CustomersFrom = Nothing
        Dim i As Long = 0
        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0
        Dim _InAmountDEV As Double = 0
        Dim _OutAmountDEV As Double = 0

        If selection.Count <= 0 Then Exit Sub

        For Each _PartnerAccountConnectionInfoQueryRows In selection
            If i = 0 Then
                _CustomersFrom = _PartnerAccountConnectionInfoQueryRows.CustomersFrom
                _CurrencyName = _PartnerAccountConnectionInfoQueryRows.CurrencyName
                If _PartnerAccountConnectionInfoQueryRows.AmountDEV <> 0 Then
                    _Rate = _PartnerAccountConnectionInfoQueryRows.AmountHUF / _PartnerAccountConnectionInfoQueryRows.AmountDEV
                End If
                i = 1
            End If
            If _PartnerAccountConnectionInfoQueryRows.CustomersFrom.Oid <> _CustomersFrom.Oid Then
                Throw New Exception("*Csak azonos saját cégű sorok jelölhető ki egyszerre !")
                Exit For
            End If

            If _PartnerAccountConnectionInfoQueryRows.CurrencyName.Oid <> _CurrencyName.Oid Then
                Throw New Exception("*Csak azonos devizával rendelkező sorok jelölhető ki egyszerre !")
                Exit For
            End If
        Next

        _GLHeader_Rounding = _ocur.CreateObject(Of GLMixed)()
        _GLHeader_Rounding.CustomersFrom = _CustomersFrom
        _GLMixedNG = _ocur.FindObject(Of GLMixedNG)(CriteriaOperator.Parse("CustomersFrom=?", _GLHeader_Rounding.CustomersFrom), True)
        If _GLMixedNG IsNot Nothing Then
            If _GLHeader_Rounding.DocumentNumber = "" Then
                _GLHeader_Rounding.DocumentNumber = _GLMixedNG.NumberGenerator.GetNewNumber(_GLMixedNG.NumberGenerator_Rounding)
            End If
        End If
        _GLHeader_Rounding.DateExecution = _RateDiffPopUp.DateExecution
        _GLHeader_Rounding.DateCreated = _RateDiffPopUp.DateExecution
        _GLHeader_Rounding.NoteHeader = _RateDiffPopUp.Description



        _GLHeader_Rounding.GLDocumentType = eGLDocumentType.eGLRateDiff
        _GLHeader_Rounding.IsEditable = False
        _GLHeader_Rounding.IsStorno = False


        For Each _PartnerAccountConnectionInfoQueryRows In selection
            _GLHeader_Rounding.CurrencyName = _PartnerAccountConnectionInfoQueryRows.CurrencyName
            _OutAmountDEV = 0
            _OutAmountHUF = 0
            _InAmountDEV = 0
            _InAmountHUF = 0
            If _PartnerAccountConnectionInfoQueryRows.CurrencyName.ShortName = "HUF" Then
                Select Case _PartnerAccountConnectionInfoQueryRows.PartnerType
                    Case ePartnerType.ePayabels
                        If _PartnerAccountConnectionInfoQueryRows.BallanceHUF > 0 Then 'Elengedés nyereség
                            _DebitChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eIn)
                            _CreditChartOfAccounts = _ChartOfAccounts_Rounding_Win
                            _Controlling = _Controlling_Rounding_Win
                            _OutAmountHUF = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceHUF)
                        Else 'Elengedés veszteség
                            _DebitChartOfAccounts = _ChartOfAccounts_Rounding_Loss
                            _CreditChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eIn)
                            _Controlling = _Controlling_Rounding_Loss
                            _InAmountHUF = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceHUF)
                        End If
                    Case ePartnerType.eReceivables
                        If _PartnerAccountConnectionInfoQueryRows.BallanceHUF > 0 Then ' Elengedés veszteség
                            _DebitChartOfAccounts = _ChartOfAccounts_Rounding_Loss
                            _CreditChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eOut)
                            _Controlling = _Controlling_Rounding_Loss
                            _InAmountHUF = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceHUF)
                        Else 'Elengedés nyereség
                            _DebitChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eOut)
                            _CreditChartOfAccounts = _ChartOfAccounts_Rounding_Win
                            _Controlling = _Controlling_Rounding_Win
                            _OutAmountHUF = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceHUF)
                        End If
                End Select
            Else
                Select Case _PartnerAccountConnectionInfoQueryRows.PartnerType

                    Case ePartnerType.ePayabels
                        If _PartnerAccountConnectionInfoQueryRows.BallanceDEV > 0 Then 'Elengedés nyereség szállító
                            _DebitChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eIn)
                            _CreditChartOfAccounts = _ChartOfAccounts_Rounding_Win
                            _Controlling = _Controlling_Rounding_Win
                            _OutAmountDEV = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV)
                        Else 'Elengedés veszteség szállító
                            _DebitChartOfAccounts = _ChartOfAccounts_Rounding_Loss
                            _CreditChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eIn)
                            _Controlling = _Controlling_Rounding_Loss
                            _InAmountDEV = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV)
                        End If
                    Case ePartnerType.eReceivables
                        If _PartnerAccountConnectionInfoQueryRows.BallanceDEV > 0 Then ' Elengedés veszteség vevő
                            _DebitChartOfAccounts = _ChartOfAccounts_Rounding_Loss
                            _CreditChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eOut)
                            _Controlling = _Controlling_Rounding_Loss
                            _InAmountDEV = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV)
                        Else 'Elengedés nyereség vevő
                            _DebitChartOfAccounts = _PartnerAccountConnectionInfoQueryRows.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Rounding.DateExecution.Year, eVATMode.eOut)
                            _CreditChartOfAccounts = _ChartOfAccounts_Rounding_Win
                            _Controlling = _Controlling_Rounding_Win
                            _OutAmountDEV = Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV)
                        End If
                End Select

            End If

            _GLRows_Rounding = _ocur.CreateObject(Of GLRows)()
            _GLRows_Rounding.GLHeader = _GLHeader_Rounding
            If _PartnerAccountConnectionInfoQueryRows.CurrencyName.ShortName = "HUF" Then
                _GLRows_Rounding.InAmountHUF = _InAmountHUF
                _GLRows_Rounding.OutAmountHUF = _OutAmountHUF
                _GLRows_Rounding.InAmountDEV = 0
                _GLRows_Rounding.OutAmountDEV = 0

                _GLRows_Rounding.AddGLRows(_GLHeader_Rounding, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceHUF), Nothing, _
                                            _PartnerAccountConnectionInfoQueryRows.Customers, Nothing, Nothing, "", Nothing, _Controlling, Nothing, Nothing, Nothing, _PartnerAccountConnectionInfoQueryRows.ConnectInfo, _
                                             eGLRowsTypeVAT.eNettoRow, Guid.NewGuid, eTransactionType.eNotSet, eCashType.ePartner, _PartnerAccountConnectionInfoQueryRows.PartnerType)
            Else

                _GLHeader_Rounding.CurrencyRate = _Rate
                _GLRows_Rounding.InAmountHUF = Math.Round(_InAmountDEV * _Rate, 0, MidpointRounding.AwayFromZero)
                _GLRows_Rounding.OutAmountHUF = Math.Round(_OutAmountDEV * _Rate, 0, MidpointRounding.AwayFromZero)
                _GLRows_Rounding.InAmountDEV = _InAmountDEV
                _GLRows_Rounding.OutAmountDEV = _OutAmountDEV

                _GLRows_Rounding.AddGLRows(_GLHeader_Rounding, _DebitChartOfAccounts, _CreditChartOfAccounts, Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV), Math.Round(Math.Abs(_PartnerAccountConnectionInfoQueryRows.BallanceDEV) * _Rate, 0, MidpointRounding.AwayFromZero), Nothing, _
                                        _PartnerAccountConnectionInfoQueryRows.Customers, Nothing, Nothing, "", Nothing, _Controlling, Nothing, Nothing, Nothing, _PartnerAccountConnectionInfoQueryRows.ConnectInfo, _
                                         eGLRowsTypeVAT.eNettoRow, Guid.NewGuid, eTransactionType.eNotSet, eCashType.ePartner, _PartnerAccountConnectionInfoQueryRows.PartnerType)

            End If
            _ocur.CommitChanges()

            GLFunctions.ReconfigurePCI(_uow, _PartnerAccountConnectionInfoQueryRows.CustomersFrom, _PartnerAccountConnectionInfoQueryRows.Customers, _PartnerAccountConnectionInfoQueryRows.PartnerType, _PartnerAccountConnectionInfoQueryRows.ConnectInfo)

        Next

        _ocur.CommitChanges()

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        noselect = False
    End Sub

End Class
