Imports System
Imports System.ComponentModel
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.Base.General
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq
Imports System.Linq.Expressions

Public Module GLFunctions
    Public Sub ReconfigurePCI(_uow As UnitOfWork, _CustomersFrom As CustomersFrom, _Customers As Customers, _PartnerType As ePartnerType, _ConnectInfo As String)
        Dim _PCIDetail As PCIDetail = Nothing
        Dim _PCIDetail1 As PCIDetail = Nothing
        Dim _PCIGroup As PCIGroup = Nothing
        Dim _GLAccounts As GLAccounts = Nothing
        Dim _GLRows As GLRows = Nothing
        Dim _InvoiceHeader As InvoiceHeader = Nothing
        Dim _RegistryHeader As RegistryHeader = Nothing
        Dim _DateExecution_Account As Date
        Dim _DatePayment_Account As Date
        Dim _CurrencyName_Account As CurrencyName = Nothing
        Dim _PCIDetail_Collection As XPCollection(Of PCIDetail)

        If _Customers Is Nothing Then Exit Sub
        If _CustomersFrom Is Nothing Then Exit Sub
        If _PartnerType = ePartnerType.eNotSet Then Exit Sub
        If _ConnectInfo Is Nothing Then _ConnectInfo = ""
        Try
            ' HIBAJAVÍTÁS MIATT
            Dim _GLAccount_Collection_X As New XPCollection(Of GLAccounts)(_uow, CriteriaOperator.Parse("IsNull(ConnectInfo)=True or ConnectInfo=''"))
            For Each _GLAccounts In _GLAccount_Collection_X
                Select Case GLOBAL_SQLType
                    Case eSQLType.PostgreSQL
                        _uow.ExecuteNonQuery("update ""GLAccounts"" set ""ConnectInfo""='" & _GLAccounts.DocumentNumber & "' Where ""Oid""='" & _GLAccounts.Oid.ToString & "'")
                    Case eSQLType.MSSQL, eSQLType.MySQL
                        _uow.ExecuteNonQuery("update GLAccounts set ConnectInfo='" & _GLAccounts.DocumentNumber & "' Where Oid='" & _GLAccounts.Oid.ToString & "'")
                End Select
            Next
            _uow.CommitChanges()

            '--- Itt kezdődik -----------------------------------------------------------------------------------------------------------------------------------
            Dim _PCIGroup_Collection As New XPCollection(Of PCIGroup)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                            _uow, CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and PartnerType=? and ConnectInfo=?", _
                                                         _CustomersFrom.Oid, _Customers.Oid, _PartnerType, _ConnectInfo))

            For Each _PCIGroup In _PCIGroup_Collection
                Select Case GLOBAL_SQLType
                    Case eSQLType.PostgreSQL
                        _uow.ExecuteNonQuery("update ""InvoiceHeader"" set ""PCIGroup""=Null Where ""PCIGroup""='" & _PCIGroup.Oid.ToString & "'")
                        _uow.ExecuteNonQuery("update ""PCIGroupAging"" set ""PCIGroup""=Null Where ""PCIGroup""='" & _PCIGroup.Oid.ToString & "'")
                        _uow.CommitChanges()
                    Case Else
                        _uow.ExecuteNonQuery("update InvoiceHeader set PCIGroup=Null Where PCIGroup='" & _PCIGroup.Oid.ToString & "'")
                        _uow.ExecuteNonQuery("update PCIGroupAging set PCIGroup=Null Where PCIGroup='" & _PCIGroup.Oid.ToString & "'")
                        _uow.CommitChanges()
                End Select
            Next

            Dim _SQL As String = ""
            Select Case GLOBAL_SQLType
                Case eSQLType.PostgreSQL
                    _SQL = "delete from ""PCIDetail"" where ""CustomersFrom""='" & _CustomersFrom.Oid.ToString & "' and"
                    _SQL += " ""Customers""='" & _Customers.Oid.ToString & "' and"
                    _SQL += " ""PartnerType""=" & _PartnerType & " and"
                    _SQL += " ""ConnectInfo""='" & _ConnectInfo & "'"
                    _uow.ExecuteNonQuery(_SQL)

                    _SQL = "delete from ""PCIGroup"" where ""CustomersFrom""='" & _CustomersFrom.Oid.ToString & "' and"
                    _SQL += " ""Customers""='" & _Customers.Oid.ToString & "' and"
                    _SQL += " ""PartnerType""=" & _PartnerType & " and"
                    _SQL += " ""ConnectInfo""='" & _ConnectInfo & "'"
                    _uow.ExecuteNonQuery(_SQL)
                Case Else
                    _SQL = "delete from PCIDetail where CustomersFrom='" & _CustomersFrom.Oid.ToString & "' and"
                    _SQL += " Customers='" & _Customers.Oid.ToString & "' and"
                    _SQL += " PartnerType=" & _PartnerType & " and"
                    _SQL += " ConnectInfo='" & _ConnectInfo & "'"
                    _uow.ExecuteNonQuery(_SQL)

                    _SQL = "delete from PCIGroup where CustomersFrom='" & _CustomersFrom.Oid.ToString & "' and"
                    _SQL += " Customers='" & _Customers.Oid.ToString & "' and"
                    _SQL += " PartnerType=" & _PartnerType & " and"
                    _SQL += " ConnectInfo='" & _ConnectInfo & "'"
                    _uow.ExecuteNonQuery(_SQL)
            End Select
            _uow.CommitChanges()
            _PCIGroup = Nothing
            _PCIDetail = Nothing
            'I. A lekönyvelt GLAccounts dokumentumok

            Dim _GLAccounts_Collection As New XPCollection(Of GLAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                _uow, CriteriaOperator.Parse("IsDeleted=False and CustomersFrom.Oid=? and Customers.Oid=? and PartnerType=? and ConnectInfo=? and IsEditable=False", _
                                            _CustomersFrom.Oid, _Customers.Oid, _PartnerType, _ConnectInfo))

            For Each _GLAccounts In _GLAccounts_Collection
                'If _PCIDetail Is Nothing Then

                _PCIDetail = New PCIDetail(_uow)
                _PCIDetail.GLAccounts = _GLAccounts
                _PCIDetail.MainType = "01-Account"
                _PCIDetail.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                _PCIDetail.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                _PCIDetail.PartnerType = _PartnerType
                _PCIDetail.ConnectInfo = _ConnectInfo
                _PCIDetail.DocumentNumber = _GLAccounts.DocumentNumber
                _PCIDetail.CurrencyName = _GLAccounts.CurrencyName
                _PCIDetail.DateExecution = _GLAccounts.DateExecution
                _PCIDetail.DateCreated = _GLAccounts.DateCreated
                _PCIDetail.DateExecution_Account = _GLAccounts.DateExecution
                _PCIDetail.DatePayment_Account = _GLAccounts.DatePayment
                If _GLAccounts.PaymentOption Is Nothing Then
                    _PCIDetail.Paymode_Account = ePayMode.InTransfer
                Else
                    _PCIDetail.Paymode_Account = _GLAccounts.PaymentOption.PayMode
                End If

                _PCIDetail.CurrencyName_Account = _GLAccounts.CurrencyName
                _PCIDetail.AmountDEV_Account = _GLAccounts.AmountDEVBrutto
                _PCIDetail.AmountHUF_Account = _GLAccounts.AmountHUFBrutto
                _CurrencyName_Account = _GLAccounts.CurrencyName
                _DateExecution_Account = _GLAccounts.DateExecution
                _DatePayment_Account = _GLAccounts.DatePayment



                If _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                    _PCIDetail.RegistryNumber = _GLAccounts.ImportedRegistryHeader.RegistryNumber
                Else
                    _PCIDetail.RegistryNumber = _GLAccounts.RegistryNumber
                End If
                If _PCIGroup Is Nothing Then
                    _PCIGroup = New PCIGroup(_uow)
                    _PCIGroup.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _PCIGroup.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                    _PCIGroup.PartnerType = _PartnerType
                    _PCIGroup.ConnectInfo = _ConnectInfo
                    _PCIGroup.DocumentNumber = _GLAccounts.DocumentNumber
                    _PCIGroup.Description = _GLAccounts.NoteHeader
                    _PCIGroup.GLAccounts = _GLAccounts

                    _PCIGroup.CurrencyName = _CurrencyName_Account
                    _PCIGroup.CurrencyName_Account = _CurrencyName_Account
                    _PCIGroup.DateCreated_Account = _GLAccounts.DateCreated
                    _PCIGroup.DateExecution_Account = _DateExecution_Account
                    _PCIGroup.DatePayment_Account = _GLAccounts.DatePayment

                    If _GLAccounts.PaymentOption Is Nothing Then
                        _PCIGroup.Paymode_Account = ePayMode.InTransfer
                    Else
                        _PCIGroup.Paymode_Account = _GLAccounts.PaymentOption.PayMode
                    End If

                    _PCIGroup.AmountDEV_Account = _GLAccounts.AmountDEVBrutto
                    _PCIGroup.AmountHUF_Account = _GLAccounts.AmountHUFBrutto

                    If _GLAccounts.ImportedRegistryHeader IsNot Nothing Then
                        _PCIGroup.RegistryNumber = _GLAccounts.ImportedRegistryHeader.RegistryNumber
                    Else
                        _PCIGroup.RegistryNumber = _GLAccounts.RegistryNumber
                    End If

                    If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                        _PCIGroup.RegistryHeader = _GLAccounts.ImportedRegistryHeader
                    Else
                        _PCIGroup.InvoiceHeader = _GLAccounts.ImportedInvoice
                    End If

                End If
                _PCIDetail.PCIGroup = _PCIGroup
                'End If
                _PCIDetail.BallanceDEV += _GLAccounts.AmountDEVBrutto
                _PCIDetail.BallanceHUF += _GLAccounts.AmountHUFBrutto
                _PCIGroup.BallanceDEV += _GLAccounts.AmountDEVBrutto
                _PCIGroup.BallanceHUF += _GLAccounts.AmountHUFBrutto

                If _GLAccounts.ImportedInvoice IsNot Nothing Then
                    _GLAccounts.ImportedInvoice.PCIGroup = _PCIGroup
                End If
            Next
            _uow.CommitChanges()
            _PCIDetail = Nothing
            'II. A kimenő számla, ami nincs feladva !
            If _PartnerType = ePartnerType.eReceivables Then
                Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                    _uow, CriteriaOperator.Parse("IsDeleted=False and isnull(DocumentNumber)=False and CustomersFrom.Oid=? and Customers.Oid=? and ConnectInfo=? and IsEditable=False and isnull(GLAccounts)=True", _
                                                _CustomersFrom.Oid, _Customers.Oid, _ConnectInfo))

                For Each _InvoiceHeader In _InvoiceHeader_Collection
                    'If _PCIDetail Is Nothing Then
                    _PCIDetail = New PCIDetail(_uow)
                    _PCIDetail.InvoiceHeader = _InvoiceHeader
                    _PCIDetail.MainType = "01-Account"
                    _PCIDetail.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _PCIDetail.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                    _PCIDetail.PartnerType = _PartnerType
                    _PCIDetail.ConnectInfo = _ConnectInfo
                    _PCIDetail.DocumentNumber = _InvoiceHeader.DocumentNumber

                    _PCIDetail.CurrencyName = _InvoiceHeader.CurrencyName
                    _PCIDetail.DateExecution = _InvoiceHeader.DateExecution
                    _PCIDetail.DateCreated = _InvoiceHeader.DateCreated
                    _PCIDetail.DateExecution_Account = _InvoiceHeader.DateExecution
                    _PCIDetail.DatePayment_Account = _InvoiceHeader.DatePayment
                    _PCIDetail.Paymode_Account = _InvoiceHeader.PaymentOption.PayMode
                    _PCIDetail.CurrencyName_Account = _InvoiceHeader.CurrencyName
                    _PCIDetail.AmountDEV_Account = _InvoiceHeader.TotalBruttoDEV
                    _PCIDetail.AmountHUF_Account = _InvoiceHeader.TotalBruttoHUF
                    _PCIDetail.Description = _InvoiceHeader.DocumentNumber
                    _CurrencyName_Account = _InvoiceHeader.CurrencyName
                    _DateExecution_Account = _InvoiceHeader.DateExecution
                    _DatePayment_Account = _InvoiceHeader.DatePayment
                    If _PCIGroup Is Nothing Then
                        _PCIGroup = New PCIGroup(_uow)
                        _PCIGroup.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                        _PCIGroup.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                        _PCIGroup.PartnerType = _PartnerType
                        _PCIGroup.ConnectInfo = _ConnectInfo
                        _PCIGroup.DocumentNumber = _InvoiceHeader.DocumentNumber
                        _PCIGroup.Description = _InvoiceHeader.Description
                        _PCIGroup.CurrencyName = _CurrencyName_Account
                        _PCIGroup.CurrencyName_Account = _CurrencyName_Account
                        _PCIGroup.DateCreated_Account = _InvoiceHeader.DateCreated
                        _PCIGroup.DateExecution_Account = _DateExecution_Account
                        _PCIGroup.DatePayment_Account = _InvoiceHeader.DatePayment
                        _PCIGroup.Paymode_Account = _InvoiceHeader.PaymentOption.PayMode
                        _PCIGroup.InvoiceHeader = _InvoiceHeader

                        _PCIGroup.AmountDEV_Account = _InvoiceHeader.TotalBruttoDEV
                        _PCIGroup.AmountHUF_Account = _InvoiceHeader.TotalBruttoHUF
                    End If
                    _PCIDetail.PCIGroup = _PCIGroup
                    'End If
                    _PCIDetail.BallanceDEV += _InvoiceHeader.TotalBruttoDEV
                    _PCIDetail.BallanceHUF += _InvoiceHeader.TotalBruttoHUF
                    _PCIGroup.BallanceDEV += _InvoiceHeader.TotalBruttoDEV
                    _PCIGroup.BallanceHUF += _InvoiceHeader.TotalBruttoHUF

                    _InvoiceHeader.PCIGroup = _PCIGroup
                Next
            End If
            _uow.CommitChanges()
            _PCIDetail = Nothing
            'IV. A rögzített számla iktatás, ami nincs feladva !
            If _PartnerType = ePartnerType.ePayabels Then
                Dim _RegistryHeader_Collection As New XPCollection(Of RegistryHeader)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                    _uow, CriteriaOperator.Parse("IsStorno=False and IsDeleted=False and RegistryAcceptState=0 and RegistryType.RegistryMainType in (0,2,4) and CustomersFrom.Oid=? and Customers.Oid=? and F_ConnectInfo=? and IsEditable=False and isnull(F_GLAccounts)=True", _
                                                _CustomersFrom.Oid, _Customers.Oid, _ConnectInfo))

                For Each _RegistryHeader In _RegistryHeader_Collection
                    'If _PCIDetail Is Nothing Then
                    _PCIDetail = New PCIDetail(_uow)
                    _PCIDetail.RegistryHeader = _RegistryHeader
                    _PCIDetail.MainType = "01-Account"
                    _PCIDetail.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _PCIDetail.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                    _PCIDetail.PartnerType = _PartnerType
                    _PCIDetail.ConnectInfo = _ConnectInfo
                    _PCIDetail.DocumentNumber = _RegistryHeader.DocumentNumber
                    _PCIDetail.CurrencyName = _RegistryHeader.F_CurrencyName
                    _PCIDetail.DateExecution = _RegistryHeader.F_DateExecution
                    _PCIDetail.DateCreated = _RegistryHeader.DateCreated
                    _PCIDetail.DatePayment_Account = _RegistryHeader.F_DatePayment
                    _PCIDetail.Paymode_Account = _RegistryHeader.F_PayMode
                    _PCIDetail.DateExecution_Account = _RegistryHeader.F_DateExecution
                    _PCIDetail.CurrencyName_Account = _RegistryHeader.F_CurrencyName
                    _PCIDetail.AmountDEV_Account = _RegistryHeader.F_AmountBruttoDEV
                    _PCIDetail.AmountHUF_Account = _RegistryHeader.F_AmountBruttoHUF
                    _CurrencyName_Account = _RegistryHeader.F_CurrencyName
                    _DateExecution_Account = _RegistryHeader.F_DateExecution
                    _DatePayment_Account = _RegistryHeader.F_DatePayment
                    _PCIDetail.RegistryNumber = _RegistryHeader.RegistryNumber
                    If _PCIGroup Is Nothing Then
                        _PCIGroup = New PCIGroup(_uow)
                        _PCIGroup.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                        _PCIGroup.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                        _PCIGroup.PartnerType = _PartnerType
                        _PCIGroup.ConnectInfo = _ConnectInfo
                        _PCIGroup.DocumentNumber = _RegistryHeader.DocumentNumber
                        _PCIGroup.Description = _RegistryHeader.ShortName
                        _PCIGroup.RegistryHeader = _RegistryHeader

                        _PCIGroup.CurrencyName = _CurrencyName_Account
                        _PCIGroup.CurrencyName_Account = _CurrencyName_Account
                        _PCIGroup.DateCreated_Account = _RegistryHeader.F_DateCreated
                        _PCIGroup.DateExecution_Account = _DateExecution_Account
                        _PCIGroup.DatePayment_Account = _RegistryHeader.F_DatePayment
                        _PCIGroup.Paymode_Account = _RegistryHeader.F_PayMode
                        _PCIGroup.AmountDEV_Account = _RegistryHeader.F_AmountBruttoDEV
                        _PCIGroup.AmountHUF_Account = _RegistryHeader.F_AmountBruttoHUF

                        _PCIGroup.RegistryNumber = _RegistryHeader.RegistryNumber
                    End If
                    _PCIDetail.PCIGroup = _PCIGroup
                    'End If
                    _PCIDetail.BallanceDEV += _RegistryHeader.F_AmountBruttoDEV
                    _PCIDetail.BallanceHUF += _RegistryHeader.F_AmountBruttoHUF
                    _PCIGroup.BallanceDEV += _RegistryHeader.F_AmountBruttoDEV
                    _PCIGroup.BallanceHUF += _RegistryHeader.F_AmountBruttoHUF
                Next
            End If
            _uow.CommitChanges()
            _PCIDetail = Nothing

            'II A pénzügyi teljesítések (soronként !)
            Dim _GLRows_Collection As New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                _uow, CriteriaOperator.Parse("GLHeader.IsDeleted=False and GLHeader.CustomersFrom.Oid=? and Customer.Oid=? and PartnerType=? and ConnectInfo=? and GLHeader.GLDocumentType in (0,2,3,4,5)", _
                                            _CustomersFrom.Oid, _Customers.Oid, _PartnerType, _ConnectInfo))

            For Each _GLRows In _GLRows_Collection
                If _GLRows.GLHeader.GLDocumentType = eGLDocumentType.eGLMixed And _GLRows.GLRowsType <> eGLRowsType.eNormal Then

                Else
                    _PCIDetail = New PCIDetail(_uow)
                    _PCIDetail.GLRows = _GLRows
                    If _PCIGroup Is Nothing Then
                        _PCIGroup = New PCIGroup(_uow)
                        _PCIGroup.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                        _PCIGroup.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                        _PCIGroup.PartnerType = _PartnerType
                        _PCIGroup.ConnectInfo = _ConnectInfo


                        _PCIGroup.CurrencyName = _GLRows.GLHeader.CurrencyName
                        _PCIGroup.DateCreated_Account = Nothing
                        _PCIGroup.CurrencyName_Account = Nothing
                        _PCIGroup.DateExecution_Account = Nothing
                        _PCIGroup.DatePayment_Account = Nothing
                        _PCIGroup.Paymode_Account = ePayMode.NotSet
                        _PCIGroup.AmountDEV_Account = 0
                        _PCIGroup.AmountHUF_Account = 0
                    End If
                    _PCIDetail.PCIGroup = _PCIGroup
                    Select Case _GLRows.GLHeader.GLDocumentType
                        Case eGLDocumentType.eGLMixed
                            _PCIDetail.MainType = "06-Mixed"
                        Case eGLDocumentType.eGLBank
                            _PCIDetail.MainType = "02-Bank"
                        Case eGLDocumentType.eGLCassa
                            _PCIDetail.MainType = "03-Cassa"
                        Case eGLDocumentType.eGLCompensation
                            _PCIDetail.MainType = "04-Compensation"
                        Case eGLDocumentType.eGLRateDiff
                            _PCIDetail.MainType = "05-Ratediff"
                        Case eGLDocumentType.eLateCharge
                            _PCIDetail.MainType = "06-Latecharge"
                    End Select

                    _PCIDetail.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _PCIDetail.Customers = _uow.GetObjectByKey(Of Customers)(_Customers.Oid)
                    _PCIDetail.PartnerType = _PartnerType
                    _PCIDetail.ConnectInfo = _ConnectInfo
                    _PCIDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber
                    _PCIDetail.CurrencyName = _GLRows.GLHeader.CurrencyName
                    _PCIDetail.DateExecution = _GLRows.GLHeader.DateExecution
                    _PCIDetail.DateCreated = _GLRows.GLHeader.DateCreated
                    _PCIDetail.AmountDEV_Account = 0
                    _PCIDetail.AmountHUF_Account = 0

                    If _GLRows.LiquidAssets IsNot Nothing Then
                        _PCIDetail.Description = _GLRows.LiquidAssets.ShortName & ", " & _GLRows.GLHeader.DocumentNumber
                    Else
                        _PCIDetail.Description = "<NINCS>, " & _GLRows.GLHeader.DocumentNumber
                    End If

                    If _CurrencyName_Account IsNot Nothing Then
                        _PCIDetail.DateExecution_Account = _DateExecution_Account
                        _PCIDetail.CurrencyName_Account = _CurrencyName_Account
                        _PCIDetail.DatePayment_Account = _DatePayment_Account
                        Select Case _PartnerType
                            Case ePartnerType.ePayabels
                                If _GLRows.InAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.InAmountHUF
                                If _GLRows.OutAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.OutAmountHUF * -1

                                If _GLRows.InAmountDEV <> 0 And _GLRows.GLHeader.CurrencyName Is _CurrencyName_Account Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV
                                If _GLRows.OutAmountDEV <> 0 And _GLRows.GLHeader.CurrencyName Is _CurrencyName_Account Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV * -1
                                If _GLRows.InAmountDEV2 <> 0 And _GLRows.GLHeader.CurrencyName IsNot _CurrencyName_Account And _CurrencyName_Account.ShortName <> "HUF" Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV2
                                If _GLRows.OutAmountDEV2 <> 0 And _GLRows.GLHeader.CurrencyName IsNot _CurrencyName_Account And _CurrencyName_Account.ShortName <> "HUF" Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV2 * -1

                            Case ePartnerType.eReceivables
                                If _GLRows.InAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.InAmountHUF * -1
                                If _GLRows.OutAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.OutAmountHUF

                                If _GLRows.InAmountDEV <> 0 And _GLRows.GLHeader.CurrencyName Is _CurrencyName_Account Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV * -1
                                If _GLRows.OutAmountDEV <> 0 And _GLRows.GLHeader.CurrencyName Is _CurrencyName_Account Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV
                                If _GLRows.InAmountDEV2 <> 0 And _GLRows.GLHeader.CurrencyName IsNot _CurrencyName_Account And _CurrencyName_Account.ShortName <> "HUF" Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV2 * -1
                                If _GLRows.OutAmountDEV2 <> 0 And _GLRows.GLHeader.CurrencyName IsNot _CurrencyName_Account And _CurrencyName_Account.ShortName <> "HUF" Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV2
                        End Select
                    Else
                        _PCIDetail.DateExecution_Account = Nothing
                        _PCIDetail.CurrencyName_Account = Nothing
                        Select Case _PartnerType
                            Case ePartnerType.ePayabels
                                If _GLRows.InAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.InAmountHUF
                                If _GLRows.OutAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.OutAmountHUF * -1
                                If _GLRows.InAmountDEV <> 0 Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV
                                If _GLRows.OutAmountDEV <> 0 Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV * -1
                            Case ePartnerType.eReceivables
                                If _GLRows.InAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.InAmountHUF * -1
                                If _GLRows.OutAmountHUF <> 0 Then _PCIDetail.BallanceHUF = _GLRows.OutAmountHUF
                                If _GLRows.InAmountDEV <> 0 Then _PCIDetail.BallanceDEV = _GLRows.InAmountDEV * -1
                                If _GLRows.OutAmountDEV <> 0 Then _PCIDetail.BallanceDEV = _GLRows.OutAmountDEV
                        End Select
                    End If
                    _PCIGroup.BallanceDEV += _PCIDetail.BallanceDEV
                    _PCIGroup.BallanceHUF += _PCIDetail.BallanceHUF
                End If
            Next
            _uow.CommitChanges()
            If _PCIGroup IsNot Nothing Then


                _PCIDetail_Collection = New XPCollection(Of PCIDetail)(_uow, CriteriaOperator.Parse("PCIGroup.Oid=?", _PCIGroup.Oid))
                _PCIDetail_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Descending))
                Dim _IsDateLastConnected As Boolean = False
                For Each _PCIDetail In _PCIDetail_Collection
                    _PCIDetail.BallanceRateDiff = _PCIGroup.BallanceRateDiff
                    _PCIDetail.BallanceState = _PCIGroup.BallanceState
                    _PCIDetail.Save()
                    _uow.CommitChanges()
                    If _IsDateLastConnected = False Then
                        Select Case _PCIDetail.MainType
                            Case "02-Bank", "03-Cassa", "04-Compensation"
                                _PCIDetail.PCIGroup.DateLastConnected = _PCIDetail.DateExecution
                                _IsDateLastConnected = True
                                _uow.CommitChanges()
                        End Select
                    End If
                Next


                If _PCIGroup.InvoiceHeader IsNot Nothing Then
                    If _PCIGroup.InvoiceHeader.InvoiceHold IsNot Nothing Then
                        If _PCIGroup.InvoiceHeader.InvoiceHold.Count > 0 Then
                            If _PCIGroup.CurrencyName_Account.ShortName = "HUF" Then
                                Dim _InvoiceHold As InvoiceHold
                                Dim _AmountHUF As Double = _PCIGroup.AmountHUF_Account - _PCIGroup.BallanceHUF
                                If _AmountHUF > 0 Then
                                    _PCIGroup.InvoiceHeader.InvoiceHold.Sorting.Add(New SortProperty("DatePayment", DB.SortingDirection.Ascending))
                                    For Each _InvoiceHold In _InvoiceHeader.InvoiceHold
                                        _InvoiceHold.BallanceHUF = 0
                                    Next
                                    For Each _InvoiceHold In _InvoiceHeader.InvoiceHold
                                        If _AmountHUF <= _InvoiceHold.AmountHUF Then
                                            _InvoiceHold.BallanceHUF = _AmountHUF
                                            Exit For
                                        Else
                                            _InvoiceHold.BallanceHUF = _InvoiceHold.AmountHUF
                                            _AmountHUF -= _InvoiceHold.AmountHUF
                                        End If
                                    Next
                                    For Each _InvoiceHold In _InvoiceHeader.InvoiceHold
                                        _InvoiceHold.BallanceHUF = _InvoiceHold.AmountHUF - _InvoiceHold.BallanceHUF
                                    Next
                                End If
                            Else

                            End If
                        End If
                    End If
                End If
                _uow.CommitChanges()
                If _PCIGroup.RegistryHeader IsNot Nothing Then
                    If _PCIGroup.RegistryHeader.RegistryBankTransfer IsNot Nothing Then
                        If _PCIGroup.RegistryHeader.RegistryBankTransfer.Count > 0 Then
                            If _PCIGroup.CurrencyName_Account.ShortName = "HUF" Then
                                Dim _RegistryBankTransfer As RegistryBankTransfer
                                Dim _AmountHUF As Double = _PCIGroup.AmountHUF_Account - _PCIGroup.BallanceHUF
                                If _AmountHUF > 0 Then
                                    _PCIGroup.RegistryHeader.RegistryBankTransfer.Sorting.Add(New SortProperty("DatePayment", DB.SortingDirection.Ascending))
                                    For Each _RegistryBankTransfer In _PCIGroup.RegistryHeader.RegistryBankTransfer
                                        _RegistryBankTransfer.BallanceHUF = 0
                                    Next
                                    For Each _RegistryBankTransfer In _PCIGroup.RegistryHeader.RegistryBankTransfer
                                        If _AmountHUF <= _RegistryBankTransfer.AmountHUF Then
                                            _RegistryBankTransfer.BallanceHUF = _AmountHUF
                                            Exit For
                                        Else
                                            _RegistryBankTransfer.BallanceHUF = _RegistryBankTransfer.AmountHUF
                                            _AmountHUF -= _RegistryBankTransfer.AmountHUF
                                        End If
                                    Next
                                    For Each _RegistryBankTransfer In _PCIGroup.RegistryHeader.RegistryBankTransfer
                                        _RegistryBankTransfer.BallanceHUF = _RegistryBankTransfer.AmountHUF - _RegistryBankTransfer.BallanceHUF
                                    Next
                                End If
                            Else

                            End If
                        End If
                    End If
                End If
                _uow.CommitChanges()
            End If
            _uow.CommitChanges()
        Catch exp As Exception

            Dim _uow_exception As New UnitOfWork(_uow.DataLayer)
            Dim _ErrorLog As ErrorLog = New ErrorLog(_uow_exception)
            _ErrorLog.Description = exp.Message.ToString
            _ErrorLog.Description += Chr(13) + Chr(10) + _ConnectInfo
            _ErrorLog.Save()
            _uow_exception.CommitChanges()

        Finally

        End Try
    End Sub
    Public Sub CorrectPCI(ByVal _uow As UnitOfWork, ByVal _Object As Object)
        Dim _AuditDataItemPersistent_Collection As XPCollection(Of AuditDataItemPersistent)
        Dim _AuditDataItemPersistent As AuditDataItemPersistent

        If TryCast(_Object, RegistryHeader) IsNot Nothing Then
            _AuditDataItemPersistent_Collection = AuditedObjectWeakReference.GetAuditTrail(_uow, _Object)
            For Each _AuditDataItemPersistent In _AuditDataItemPersistent_Collection
                If _AuditDataItemPersistent.PropertyName = "DocumentNumber" Then

                End If
            Next
        End If
    End Sub
   
End Module
