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
<DefaultClassOptions()> _
<NavigationItem("FinancialTransactions")> _
<CreatableItem(False)> _
Public Class GLRows
    Inherits BaseObject
    Private _GLHeader As GLHeader
    Private _GLAccounts As GLAccounts
    Private _DebitChartOfAccounts As ChartOfAccounts 'Tartozik számlaszám
    Private _CreditChartOfAccounts As ChartOfAccounts ' Követel számlaszám
    Private _AmountHUF As Double ' könyvelt összeg forintban
    Private _AmountDEV As Double ' könyvelt összeg devizában
    Private _AmountDEVFIFO As Double = 0 'FIFO értékeléshez felhasznált deviza összege
    Private _LiquidAssets As LiquidAssets ' pénzeszköz
    Private _Customer As Customers ' partner
    Private _DateVAT As Date 'ÁFA idõszak
    Private _DateExecution As Date 'Teljesítési idõszak
    Private _VAT As VAT 'ÁFA kód
    Private _NoteRows As String 'Sor megjegyzés


    Private _Controlling As Controlling 'Kontrolling szám
    Private _CostPlace As CostPlace 'Költséghely
    Private _CostHolder As CostHolder 'Költségviselõ
    Private _JobNumberObjects As JobNumberObjects 'Munkaszám
    Private _UniqueObjects As UniqueObjects 'Egyedi azonosító


    Private _ConnectInfo As String = "" 'Analitikus kötési szám folyószámlához
    Private _ConnectInfoAdvanced As String = "" 'Analitikus kötési szám az előlegek pontozásához

    Private _PartnerType As ePartnerType
    Private _DebitAmountHUF As Double = 0
    Private _CreditAmountHUF As Double = 0
    Private _DebitAmountDEV As Double = 0
    Private _CreditAmountDEV As Double = 0
    Private _GLRowsGUID As Guid
    Private _GLRowsTypeVAT As eGLRowsTypeVAT
    Private _InAmountHUF As Double = 0 'Pénztár miatt befizetés
    Private _OutAmountHUF As Double = 0 'Pénztár miatt kifizetés
    Private _InAmountDEV As Double = 0 'Pénztár miatt befizetés
    Private _OutAmountDEV As Double = 0 'Pénztár miatt kifizetés
    Private _ImportID As String = "" 'Külsõ rendszerbõl importált sor
    Private _CashType As eCashType
    Private _TransactionType As eTransactionType
    Private _CurrencyRate2 As Double = 1
    Private _InAmountDEV2 As Double = 0 'Másodlagos deviza !
    Private _OutAmountDEV2 As Double = 0 'Másodlagos deviza
    Private _CurrencyName2 As CurrencyName 'Másodlagos devizanem
    Private _BankImportHeaderSumDetail As BankImportHeaderSumDetail
    Private _GLRowsRateDiff As GLRows

    Private _InvoiceRows As InvoiceRows
    Private _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
    Private _GLRowsType As eGLRowsType '0 normál 1 Nyitó, 2 Záró

    Private _RowIndex As Long 'Sorrend
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        If Me.GLHeader IsNot Nothing Then
            If Me.GLHeader.DateExecution = Nothing Then
            Else
                Me.DateExecution = Me.GLHeader.DateExecution
            End If
        End If
    End Sub

    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Me.ConnectInfo Is Nothing Then
            Me.ConnectInfo = ""
        End If

        Dim _GLDetail_Collection As New XPCollection(Of GLDetails)(Session, CriteriaOperator.Parse("GLRows.Oid=?", Me.Oid))
        Session.Delete(_GLDetail_Collection)
        Dim _GLDetail As GLDetails

        If Session.IsObjectToDelete(Me) = False Then
            _GLDetail = New GLDetails(Session)
            _GLDetail.GLRows = Me
            _GLDetail.AmountDEV = Me.AmountDEV
            _GLDetail.AmountHUF = Me.AmountHUF
            _GLDetail.Side = "T"
            _GLDetail.ChartOfAccounts = Me.DebitChartOfAccounts



            _GLDetail = New GLDetails(Session)
            _GLDetail.GLRows = Me
            _GLDetail.AmountDEV = Me.AmountDEV
            _GLDetail.AmountHUF = Me.AmountHUF
            _GLDetail.Side = "K"
            _GLDetail.ChartOfAccounts = Me.CreditChartOfAccounts

            GLRowsType = eGLRowsType.eNormal
            If CreditChartOfAccounts IsNot Nothing AndAlso DebitChartOfAccounts IsNot Nothing Then

                If CreditChartOfAccounts.AccountNumber Like "491*" Or DebitChartOfAccounts.AccountNumber Like "491*" Then
                    GLRowsType = eGLRowsType.eOpen
                End If
                If CreditChartOfAccounts.AccountNumber Like "492*" Or DebitChartOfAccounts.AccountNumber Like "492*" Then
                    GLRowsType = eGLRowsType.eClose
                End If
            End If
            If Me.DateExecution = Nothing Then
                If Me.GLHeader IsNot Nothing Then
                    Me.DateExecution = Me.GLHeader.DateExecution
                End If
            End If

            '    '// Sorrend beállítása
            '    If Session.IsNewObject(Me) Then
            '        Me.RowIndex = Me.GLHeader.GLRows.Count
            '    End If
        End If
    End Sub
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()

        Dim _GLDetail_Collection As New XPCollection(Of GLDetails)(Session, CriteriaOperator.Parse("GLRows.Oid=?", Me.Oid))
        Session.Delete(_GLDetail_Collection)
        Dim _GLDetail As GLDetails


        Dim _GLRowsRateDiff_Collection As New XPCollection(Of GLRows)(Session, CriteriaOperator.Parse("GLRowsRateDiff=?", Me))
        For Each _GLRows As GLRows In _GLRowsRateDiff_Collection
            _GLRows.GLRowsRateDiff = Nothing
            _GLRows.Save()
        Next

    End Sub
    Protected Overrides Sub OnDeleted()
        MyBase.OnDeleted()

        '// PARASZTLENGŐS ÚJRAPONTOZÁS TÖRLÉS ESETÉN !!!!
        On Error Resume Next

        If Me.PartnerType <> ePartnerType.eNotSet Then
            'Dim _uow As New UnitOfWork(Session.DataLayer)
            'Dim _CustomersFrom_Collection As New XPCollection(Of CustomersFrom)(_uow)

            'For Each _CustomersFrom As CustomersFrom In _CustomersFrom_Collection
            '    GLFunctions.ReconfigurePCI(_uow, _CustomersFrom, Me.Customer, Me.PartnerType, Me.ConnectInfo)
            '    _uow.CommitChanges()
            'Next
        End If

        '// Sorrend újraépítése !!!
    End Sub


    <Association("GLHeader", GetType(GLHeader))> _
    Property GLHeader() As GLHeader
        Get
            Return _GLHeader
        End Get
        Set(ByVal value As GLHeader)
            SetPropertyValue("GLHeader", _GLHeader, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)>
    <ImmediatePostData(True), DataSourceCriteria("isnull(Children)=True")>
    Property DebitChartOfAccounts() As ChartOfAccounts
        Get
            Return _DebitChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("DebitChartOfAccounts", _DebitChartOfAccounts, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)>
    Property CreditChartOfAccounts() As ChartOfAccounts
        Get
            Return _CreditChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("CreditChartOfAccounts", _CreditChartOfAccounts, value)
        End Set
    End Property

    <RuleRequiredField("", DefaultContexts.Save)>
    Property AmountHUF() As Double
        Get
            Return Math.Round(_AmountHUF, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AmountHUF", _AmountHUF, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property AmountDEV() As Double
        Get
            Return Math.Round(_AmountDEV, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AmountDEV", _AmountDEV, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    <VisibleInDetailView(False)>
    Property AmountDEVFIFO As Double
        Get
            Return Math.Round(_AmountDEVFIFO, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("AmountDEVFIFO", _AmountDEVFIFO, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property

    Property LiquidAssets() As LiquidAssets
        Get
            Return _LiquidAssets
        End Get
        Set(ByVal value As LiquidAssets)
            SetPropertyValue("LiquidAssets", _LiquidAssets, value)
        End Set
    End Property
    Property Customer() As Customers
        Get
            Return _Customer
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customer", _Customer, value)
        End Set
    End Property
    Property DateVAT() As Date
        Get
            Return _DateVAT
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateCreated", _DateVAT, value)
        End Set
    End Property
    Property DateExecution As Date
        Get
            Return _DateExecution
        End Get
        Set(value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
        End Set
    End Property
    Property VAT() As VAT
        Get
            Return _VAT
        End Get
        Set(ByVal value As VAT)
            SetPropertyValue("VAT", _VAT, value)
        End Set
    End Property
    <Size(-1)>
    Property NoteRows() As String
        Get
            Return _NoteRows
        End Get
        Set(ByVal value As String)
            SetPropertyValue("NoteRows", _NoteRows, value)
        End Set
    End Property

    Property RowIndex As Long
        Get
            Return _RowIndex
        End Get
        Set(value As Long)
            SetPropertyValue("RowIndex", _RowIndex, value)
        End Set
    End Property
    'Rule5DMatrix ...

    <ImmediatePostData(True), DataSourceCriteria("IsParent=False")>
    <RuleRequiredField("GLRows-Controlling", DefaultContexts.Save, TargetCriteria:="Rule5DMatrix.RequireControlling=True and Require5DValidation=True")> _
    Property Controlling() As Controlling
        Get
            Return _Controlling
        End Get
        Set(ByVal value As Controlling)
            SetPropertyValue("Controlling", _Controlling, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then

            End If
        End Set
    End Property
    <RuleRequiredField("GLRows-JobNumberObjects", DefaultContexts.Save, TargetCriteria:="Rule5DMatrix.RequireJobNumberObjects=True and Require5DValidation=True")> _
    Property JobNumberObjects() As JobNumberObjects
        Get
            Return _JobNumberObjects
        End Get
        Set(ByVal value As JobNumberObjects)
            SetPropertyValue("JobNumberObjects", _JobNumberObjects, value)
        End Set
    End Property
    <RuleRequiredField("GLRows-UniqueObjects", DefaultContexts.Save, TargetCriteria:="Rule5DMatrix.RequireUniqueObjects=True and Require5DValidation=True")> _
    Property UniqueObjects() As UniqueObjects
        Get
            Return _UniqueObjects
        End Get
        Set(ByVal value As UniqueObjects)
            SetPropertyValue("UniqueObjects", _UniqueObjects, value)
        End Set
    End Property
    <RuleRequiredField("GLRows-CostPlace", DefaultContexts.Save, TargetCriteria:="Rule5DMatrix.RequireCostPlace=True and Require5DValidation=True")> _
    Property CostPlace() As CostPlace
        Get
            Return _CostPlace
        End Get
        Set(ByVal value As CostPlace)
            SetPropertyValue("CostPlace", _CostPlace, value)
        End Set
    End Property
    <RuleRequiredField("GLRows-CostHolder", DefaultContexts.Save, TargetCriteria:="Rule5DMatrix.RequireCostHolder=True and Require5DValidation=True")> _
    Property CostHolder() As CostHolder
        Get
            Return _CostHolder
        End Get
        Set(ByVal value As CostHolder)
            SetPropertyValue("CostHolder", _CostHolder, value)
        End Set
    End Property

    <Browsable(False)> _
    ReadOnly Property Rule5DMatrix As Rule5DMatrix
        Get
            If GLHeader Is Nothing Then
                Return Nothing
            Else
                Dim _Rule5DforGL As Rule5DforGL = Session.FindObject(Of Rule5DforGL)(CriteriaOperator.Parse("GLDocumentType=?", GLHeader.GLDocumentType))
                If _Rule5DforGL IsNot Nothing Then
                    Return _Rule5DforGL.Rule5DMatrix
                Else
                    Return Nothing
                End If
            End If
        End Get
    End Property
    <Browsable(False)> _
    ReadOnly Property Require5DValidation As Boolean
        Get
            Dim _Require5DValidation As Boolean = True
            If _Require5DValidation Then
                Select Case Me.CreditChartOfAccounts.AccountNumber
                    Case "599", "491", "492", "493"
                        _Require5DValidation = False
                    Case Else
                        _Require5DValidation = True
                End Select
            End If
            If _Require5DValidation Then
                Select Case Me.DebitChartOfAccounts.AccountNumber
                    Case "599", "491", "492", "493"
                        _Require5DValidation = False
                    Case Else
                        _Require5DValidation = True
                End Select
            End If
            Return _Require5DValidation
        End Get
    End Property
    Property ConnectInfo() As String
        Get
            Return _ConnectInfo
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ConnectInfo", _ConnectInfo, LTrim(RTrim(value)))
        End Set
    End Property
    Property ConnectInfoAdvanced As String
        Get
            Return _ConnectInfoAdvanced
        End Get
        Set(value As String)
            SetPropertyValue("ConnectInfoAdvanced", _ConnectInfoAdvanced, value)
        End Set
    End Property
    Property PartnerType() As ePartnerType
        Get
            Return _PartnerType
        End Get
        Set(ByVal value As ePartnerType)
            SetPropertyValue("PartnerType", _PartnerType, value)
        End Set
    End Property
    Property DebitAmountHUF() As Double
        Get
            Return Math.Round(_DebitAmountHUF, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DebitAmountHUF", _DebitAmountHUF, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property CreditAmountHUF() As Double
        Get
            Return Math.Round(_CreditAmountHUF, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("CreditAmountHUF", _CreditAmountHUF, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property DebitAmountDEV() As Double
        Get
            Return _DebitAmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DebitAmountDEV", _DebitAmountDEV, value)
        End Set
    End Property
    Property CreditAmountDEV() As Double
        Get
            Return _CreditAmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("CreditAmountDEV", _CreditAmountDEV, value)
        End Set
    End Property
    Property GLRowsTypeVAT As eGLRowsTypeVAT
        Get
            Return _GLRowsTypeVAT
        End Get
        Set(ByVal value As eGLRowsTypeVAT)
            SetPropertyValue("GLRowsTypeVAT", _GLRowsTypeVAT, value)
        End Set
    End Property
    Property GLRowsGUID As Guid
        Get
            Return _GLRowsGUID
        End Get
        Set(ByVal value As Guid)
            SetPropertyValue("GLRowsGUID", _GLRowsGUID, value)
        End Set
    End Property
    '-- Összegek !!!
    Property InAmountHUF As Double
        Get
            Return Math.Round(_InAmountHUF, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("InAmountHUF", _InAmountHUF, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property OutAmountHUF As Double
        Get
            Return Math.Round(_OutAmountHUF, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("OutAmountHUF", _OutAmountHUF, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property InAmountDEV As Double
        Get
            Return Math.Round(_InAmountDEV, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("InAmountDEV", _InAmountDEV, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property OutAmountDEV As Double
        Get
            Return Math.Round(_OutAmountDEV, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("OutAmountDEV", _OutAmountDEV, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property

    Property ImportID As String
        Get
            Return _ImportID
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ImportID", _ImportID, value)
        End Set
    End Property
    Property CashType As eCashType
        Get
            Return _CashType
        End Get
        Set(ByVal value As eCashType)
            SetPropertyValue("CashType", _CashType, value)
        End Set
    End Property
    Property TransactionType As eTransactionType
        Get
            Return _TransactionType
        End Get
        Set(ByVal value As eTransactionType)
            SetPropertyValue("TransactionType", _TransactionType, value)
        End Set
    End Property
    Property CurrencyRate2 As Double
        Get
            Return _CurrencyRate2
        End Get
        Set(value As Double)
            SetPropertyValue("CurrencyRate2", _CurrencyRate2, value)
        End Set
    End Property
    Property InAmountDEV2 As Double
        Get
            Return Math.Round(_InAmountDEV2, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("InAmountDEV2", _InAmountDEV2, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property OutAmountDEV2 As Double
        Get
            Return Math.Round(_OutAmountDEV2, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("OutAmountDEV2", _OutAmountDEV2, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property

    Property GLRowsType As eGLRowsType
        Get
            Return _GLRowsType
        End Get
        Set(value As eGLRowsType)
            SetPropertyValue("GLRowsType", _GLRowsType, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
    <VisibleInListView(False)> _
    <VisibleInLookupListView(False)> _
    Property GLRowsRateDiff As GLRows
        Get
            Return _GLRowsRateDiff
        End Get
        Set(value As GLRows)
            SetPropertyValue("GLRowsRateDiff", _GLRowsRateDiff, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
    <VisibleInListView(False)> _
    <VisibleInLookupListView(False)> _
    Property InvoiceRows As InvoiceRows
        Get
            Return _InvoiceRows
        End Get
        Set(value As InvoiceRows)
            SetPropertyValue("InvoiceRows", _InvoiceRows, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
    <VisibleInListView(False)> _
    <VisibleInLookupListView(False)> _
    Property RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Get
            Return _RegistryRowsFinancialControlling
        End Get
        Set(value As RegistryRowsFinancialControlling)
            SetPropertyValue("RegistryRowsFinancialControlling", _RegistryRowsFinancialControlling, value)
        End Set
    End Property
    Property BankImportHeaderSumDetail As BankImportHeaderSumDetail
        Get
            Return _BankImportHeaderSumDetail
        End Get
        Set(value As BankImportHeaderSumDetail)
            SetPropertyValue("BankImportHeaderSumDetail", _BankImportHeaderSumDetail, value)
        End Set
    End Property
    ReadOnly Property CurrencyRate As Double
        Get
            If AmountDEV <> 0 Then
                Return AmountHUF / AmountDEV
            Else
                Return 1
            End If
        End Get
    End Property
    Public Sub AddGLRows(ByVal p_GLHeader As GLHeader, ByVal p_DebitChartOfAccounts As ChartOfAccounts, ByVal p_CreditChartOfAccounts As ChartOfAccounts, _
                         ByVal p_AmountDEV As Double, ByVal p_AmountHUF As Double, ByVal p_LiquidAssets As LiquidAssets, _
                         ByVal p_Customer As Customers, ByVal p_DateVAT As Date, _
                         ByVal p_VAT As VAT, ByVal p_NoteRows As String, ByVal p_UniqueObjects As UniqueObjects, _
                         ByVal p_Controlling As Controlling, ByVal p_JobNumberOBjects As JobNumberObjects, _
                         ByVal p_CostPlace As CostPlace, ByVal p_CostHolder As CostHolder, ByVal p_ConnectInfo As String, _
                         ByVal p_GLRowsTypeVAT As eGLRowsTypeVAT, ByVal p_GLRowsGUID As Guid, Optional ByVal p_TransactionType As eTransactionType = eTransactionType.eNotSet, _
                         Optional ByVal p_CashType As eCashType = eCashType.eNormal, Optional ByVal p_PartnerType As ePartnerType = ePartnerType.eNotSet)
        TransactionType = p_TransactionType
        GLHeader = p_GLHeader
        DebitChartOfAccounts = p_DebitChartOfAccounts
        CreditChartOfAccounts = p_CreditChartOfAccounts
        AmountDEV = p_AmountDEV
        AmountHUF = p_AmountHUF
        LiquidAssets = p_LiquidAssets
        Customer = p_Customer
        DateVAT = p_DateVAT
        VAT = p_VAT
        NoteRows = p_NoteRows
        UniqueObjects = p_UniqueObjects
        Controlling = p_Controlling
        JobNumberObjects = p_JobNumberOBjects
        CostPlace = p_CostPlace
        CostHolder = p_CostHolder
        ConnectInfo = p_ConnectInfo
        GLRowsTypeVAT = p_GLRowsTypeVAT
        GLRowsGUID = p_GLRowsGUID
        CashType = p_CashType
        PartnerType = p_PartnerType

    End Sub
    Public Sub DivideRows(_ocur As Xpo.XPObjectSpace, p_GLRowsBase As GLRows, p_GLRowsDivideH As GLRowsDivideH, Optional p_WithCommit As Boolean = False)
        '-- Az eljárás megcsinálja a megfelelõ fõkönyvi sor sorbontását
        Dim _GLRowsDivideR As GLRowsDivideR
        Dim _GLHeader As GLHeader
        Dim _GLBank As GLBank
        Dim _GLCassa As GLCassa
        Dim _GLCompensation As GLCompensation
        Dim _GLRows As GLRows
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts As ChartOfAccounts = Nothing
        Dim _LiquidAssets As LiquidAssets = Nothing

        Dim _CustomersFrom_Base As CustomersFrom = Nothing
        Dim _Customers_Base As Customers = Nothing
        Dim _ConnectInfo_Base As String = ""
        Dim _PartnerType_Base As ePartnerType = ePartnerType.eNotSet

        If p_GLRowsBase IsNot Nothing Then
            _GLHeader = p_GLRowsBase.GLHeader
            If p_GLRowsBase.PartnerType = ePartnerType.ePayabels Or p_GLRowsBase.PartnerType = ePartnerType.eReceivables Then
                _CustomersFrom_Base = p_GLRowsBase.GLHeader.CustomersFrom
                _Customers_Base = p_GLRowsBase.Customer
                _PartnerType_Base = p_GLRowsBase.PartnerType
                _ConnectInfo_Base = p_GLRowsBase.ConnectInfo
            End If
            If p_GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLBank Or _
               p_GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLCassa Or _
               p_GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLCompensation Or _
               p_GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLMixed Then ' Bank esetén mi a helyzet !

                Select Case p_GLRowsBase.GLHeader.GLDocumentType
                    Case eGLDocumentType.eGLBank
                        _GLBank = _ocur.FindObject(Of GLBank)(CriteriaOperator.Parse("Oid=?", p_GLRowsBase.GLHeader.Oid), True)
                        _LiquidAssets = _GLBank.LiquidAssets_Main
                    Case eGLDocumentType.eGLCassa
                        _GLCassa = _ocur.FindObject(Of GLCassa)(CriteriaOperator.Parse("Oid=?", p_GLRowsBase.GLHeader.Oid), True)
                        _LiquidAssets = _GLCassa.LiquidAssets_Cassa
                    Case eGLDocumentType.eGLCompensation
                        _GLCompensation = _ocur.FindObject(Of GLCompensation)(CriteriaOperator.Parse("Oid=?", p_GLRowsBase.GLHeader.Oid), True)
                        _LiquidAssets = _GLCompensation.LiquidAssets_Main
                End Select

                For Each _GLRowsDivideR In p_GLRowsDivideH.GLRowsDivideR
                    _GLRows = _ocur.CreateObject(Of GLRows)()
                    _GLRows.GLHeader = _GLHeader
                    If p_GLRowsBase.GLHeader.GLDocumentType <> eGLDocumentType.eGLMixed Then
                        If _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0).AccountNumber = p_GLRowsBase.DebitChartOfAccounts.AccountNumber Then
                            '-- Bankba jóváírás érkezett
                            _DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            If _GLRowsDivideR.ChartOfAccounts IsNot Nothing Then
                                _CreditChartOfAccounts = _GLRowsDivideR.ChartOfAccounts
                            Else
                                _CreditChartOfAccounts = p_GLRowsDivideH.GLRowsBase.CreditChartOfAccounts
                            End If
                            _GLRows.InAmountDEV = _GLRowsDivideR.AmountDEV
                            _GLRows.InAmountHUF = _GLRowsDivideR.AmountHUF
                        ElseIf _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0).AccountNumber = p_GLRowsBase.CreditChartOfAccounts.AccountNumber Then
                            '-- Banki terhelés
                            _CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            If _GLRowsDivideR.ChartOfAccounts IsNot Nothing Then
                                _DebitChartOfAccounts = _GLRowsDivideR.ChartOfAccounts
                            Else
                                _DebitChartOfAccounts = p_GLRowsDivideH.GLRowsBase.DebitChartOfAccounts
                            End If
                            _GLRows.OutAmountDEV = _GLRowsDivideR.AmountDEV
                            _GLRows.OutAmountHUF = _GLRowsDivideR.AmountHUF

                        End If
                    End If
                    If p_GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLMixed Then
                        _DebitChartOfAccounts = _GLRowsDivideR.DebitChartOfAccounts
                        _CreditChartOfAccounts = _GLRowsDivideR.CreditChartOfAccounts
                        _GLRows.OutAmountDEV = _GLRowsDivideR.AmountDEV
                        _GLRows.OutAmountHUF = _GLRowsDivideR.AmountHUF
                        _LiquidAssets = Nothing
                    End If
                    _GLRows.AddGLRows(_GLHeader, _ocur.GetObject(_DebitChartOfAccounts), _ocur.GetObject(CreditChartOfAccounts), _GLRowsDivideR.AmountDEV, _GLRowsDivideR.AmountHUF, _
                                      _LiquidAssets, _ocur.GetObject(_GLRowsDivideR.Customers), _GLHeader.DateExecution, Nothing, _ocur.GetObject(_GLRowsDivideR.NoteRows), _
                                      _ocur.GetObject(_GLRowsDivideR.UniqueObjects), _ocur.GetObject(_GLRowsDivideR.Controlling), _ocur.GetObject(_GLRowsDivideR.JobNumberObjects), _ocur.GetObject(_GLRowsDivideR.CostPlace), _
                                      _ocur.GetObject(_GLRowsDivideR.CostHolder), _GLRowsDivideR.ConnectInfo, eGLRowsTypeVAT.eNettoRow, p_GLRowsBase.GLRowsGUID, _
                                      p_GLRowsBase.TransactionType, p_GLRowsBase.CashType, p_GLRowsBase.PartnerType)
                    If p_WithCommit Then
                        _ocur.CommitChanges()
                        If _GLRows.PartnerType = ePartnerType.ePayabels Or _GLRows.PartnerType = ePartnerType.eReceivables Then
                            Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                            GLFunctions.ReconfigurePCI(_uow, _GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
                        End If
                    End If
                Next
                _ocur.Delete(p_GLRowsBase)
            End If
            If p_WithCommit Then
                _ocur.CommitChanges()
                If _PartnerType_Base = ePartnerType.ePayabels Or _PartnerType_Base = ePartnerType.eReceivables Then
                    Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                    GLFunctions.ReconfigurePCI(_uow, _CustomersFrom_Base, _Customers_Base, _PartnerType_Base, _ConnectInfo_Base)
                End If
            End If
        End If
    End Sub
End Class
