Imports System.Data.SqlClient
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.IO
Imports DevExpress.Persistent.BaseImpl

Module BaseImport

    Public Sub Import_GLAccounts(ByVal _ID As Long, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        Dim dr_r As SqlDataReader

        Dim mSQL As String
        Dim _Customers As Customers
        Dim _GLRows As GLRows
        Dim _GUID As Guid
        Dim _GLAccounts As GLAccounts
        Dim _PaymentOption As PaymentOption

        Dim _UniqueObjects As UniqueObjects
        Dim _JobNumberObjects As JobNumberObjects
        Dim _CostHolder As CostHolder
        Dim _CostPlace As CostPlace
        Dim _Controlling As Controlling


        mSQL = "select Ügyfélszám=t2.CégAlany,t1.BizTipus,Bizonylatszám=isnull(t1.Bizonylatszám,''),t1.TD,t1.ED,t1.KD,t1.ÁFADátum,"
        mSQL = mSQL & " Árfolyam=isnull(t2.Árfolyam,1),IktatóSzám=isnull(t1.IktatóSzám,''),"
        mSQL = mSQL & " S_Ügyfél=case when t1.BizTipus=0 then isnull(t6.Számlaszám,'') else isnull(t5.Számlaszám,'') end,"
        mSQL = mSQL & " t2.Storno,Megjegyzés=isnull(t1.Megjegyzés,''),Deviza=isnull(t1.Deviza,''),"
        mSQL = mSQL & " ID_Dokument_Header=isnull(t2.ID_Dokument_Header,-1),"

        mSQL = mSQL & " FizetésiMód=isnull(t1.FizetésiMód,'')"

        mSQL = mSQL & " from sql_Cassa..vv_ÖsszLista t1 left join sql_Cassa..Folyó_Bizonylatok t2 on"
        mSQL = mSQL & " t1.ID=t2.ID left join sql_Main..Ügyféltörzs t3 on"
        mSQL = mSQL & " t2.CégAlany=t3.Ügyfélszám left join Ügyféltörzs_Mode t4 on"
        mSQL = mSQL & " t3.Mode=t4.Mode left join sql_Cassa..Költségek t5 on"
        mSQL = mSQL & " t4.KöltségIDIn=t5.ID left join sql_Cassa..Költségek t6 on"
        mSQL = mSQL & " t4.KöltségIDOut=t6.ID"

        mSQL = mSQL & " Where t1.ID=" & _ID

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                _PaymentOption = _uow.FindObject(Of PaymentOption)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_h("FizetésiMód")))
                If _PaymentOption Is Nothing Then
                    _PaymentOption = New PaymentOption(_uow)
                    If UCase(dr_h("FizetésiMód")) = "KÉSZPÉNZ" Then
                        _PaymentOption.PayMode = ePayMode.InCash
                        _PaymentOption.PayDay = 0
                        _PaymentOption.ShortName = dr_h("FizetésiMód")
                        _PaymentOption.Description = dr_h("FizetésiMód")
                    Else
                        _PaymentOption.PayMode = ePayMode.InTransfer
                        _PaymentOption.PayDay = 8
                        _PaymentOption.ShortName = dr_h("FizetésiMód")
                        _PaymentOption.Description = dr_h("FizetésiMód")
                    End If
                End If
                _Customers = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                If _Customers IsNot Nothing Then

                    _GLAccounts = _uow.FindObject(Of GLAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and PartnerType=? and DocumentNumber=?", _
                                                       _CustomersFrom.Oid, _Customers.Oid, dr_h("BizTipus"), dr_h("Bizonylatszám")))
                    If _GLAccounts Is Nothing Then _GLAccounts = New GLAccounts(_uow)

                    _GLAccounts.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _GLAccounts.Customers = _Customers
                    _GLAccounts.PaymentOption = _PaymentOption

                    _GLAccounts.CurrencyName = _uow.FindObject(Of CurrencyName)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_h("Deviza")))
                    If dr_h("Deviza") = "HUF" Then
                        _GLAccounts.CurrencyRate = 1
                        _GLAccounts.CurrencyRateVAT() = 1
                    Else
                        _GLAccounts.CurrencyRate = dr_h("Árfolyam")
                        _GLAccounts.CurrencyRateVAT() = dr_h("Árfolyam")
                    End If
                    '_GLAccounts.CustomerBankAccounts
                    _GLAccounts.DateCreated = dr_h("KD")
                    _GLAccounts.DateExecution = dr_h("TD")
                    _GLAccounts.DatePayment = dr_h("ED")
                    _GLAccounts.DocumentNumber = dr_h("Bizonylatszám")
                    _GLAccounts.GLDocumentType = eGLDocumentType.eGLAccount
                    _GLAccounts.IsEditable = False
                    _GLAccounts.IsStorno = dr_h("Storno")
                    _GLAccounts.NoteHeader = dr_h("Megjegyzés")
                    _GLAccounts.PartnerType = dr_h("BizTipus")
                    '_GLAccounts.PaymentOption
                    _GLAccounts.RegistryNumber = dr_h("IktatóSzám")
                    _GLAccounts.IsRegistry = False

                    If dr_h("ID_Dokument_Header") > 0 Then
                        Dim _FS As FileStream
                        Dim _FilePath As String

                        _FilePath = LoadFileFromSQL(dr_h("ID_Dokument_Header"), _ConnectionString)
                        If System.IO.File.Exists(_FilePath) Then
                            _FS = New FileStream(_FilePath, FileMode.Open)

                            _GLAccounts.DocumentFile = New FileData(_uow)
                            _GLAccounts.DocumentFile.LoadFromStream(_FilePath, _FS)
                            _GLAccounts.DocumentFile.FileName = System.IO.Path.GetFileName(_FilePath)

                            _FS.Close()
                            _FS = Nothing
                        End If
                    End If

                    mSQL = "select Áfahiv=isnull(t1.Áfahiv,''),Bizonylatszám_P=isnull(t2.Bizonylatszám_P,''),t1.ÖsszegNetto,t1.ÖsszegNettoDeviza,"
                    mSQL = mSQL & " t1.ÖsszegAFADeviza,t1.ÖsszegAFA,"
                    mSQL = mSQL & " S_Költség=isnull(t3.Számlaszám,''),t1.Megjegyzés,"
                    mSQL = mSQL & " S_ÁFA=case when t2.BizTipus=0 then isnull(t5.Számlaszám,'') else isnull(t6.Számlaszám,'') end,"

                    mSQL = mSQL & " M_Munkaszám=isnull(t20.Megnevezés,''),"
                    mSQL = mSQL & " M_Egyedi=isnull(t21.Megnevezés,''),"
                    mSQL = mSQL & " M_Költséghely=isnull(t23.Megnevezés,''),"
                    mSQL = mSQL & " M_Költségviselő=isnull(t24.Megnevezés,''),"
                    mSQL = mSQL & " M_Controlling=isnull(t25.Megnevezés,'')"

                    mSQL = mSQL & " from Folyó_Bizonylatok_Sorok t1 join sql_Cassa..Folyó_Bizonylatok t2 on"
                    mSQL = mSQL & " t1.ParentID=t2.ID left join sql_Cassa..Költségek t3 on"
                    mSQL = mSQL & " t1.KöltségID=t3.ID left join sql_Cassa..VAT t4 on"
                    mSQL = mSQL & " t1.Áfahiv=t4.ShortName left join sql_Cassa..Költségek t5 on"
                    mSQL = mSQL & " t4.KöltségIDOut=t5.ID left join sql_Cassa..Költségek t6 on"
                    mSQL = mSQL & " t4.KöltségIDIn=t6.ID left join Munkaszám t20 on"
                    mSQL = mSQL & " t1.MunkaszámID=t20.ID left join Egyedi t21 on"
                    mSQL = mSQL & " t1.EgyediID=t21.ID left join RögzítőKód t22 on"
                    mSQL = mSQL & " t1.RögzítőKódID=t22.ID left join Költséghely t23 on"
                    mSQL = mSQL & " t22.KöltséghelyID=t23.ID left join Költségviselő t24 on"
                    mSQL = mSQL & " t22.KöltségviselőID=t24.ID left join Költségviselő t25 on"
                    mSQL = mSQL & " t1.ControllingID=t25.ID"

                    mSQL = mSQL & " Where t1.ParentID=" & _ID
                    mSQL = mSQL & " order by t1.ID"

                    cmd_r.CommandTimeout = 60
                    cmd_r.Connection = conn_r
                    cmd_r.CommandType = CommandType.Text
                    cmd_r.CommandText = mSQL
                    conn_r.Open()

                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLAccounts.Oid))
                    _uow.Delete(_GLRows_Collection)

                    If conn_r.State = ConnectionState.Open Then
                        dr_r = cmd_r.ExecuteReader(CommandBehavior.SingleResult)
                        While dr_r.Read
                            _GUID = Guid.NewGuid
                            _GLRows = New GLRows(_uow)
                            _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)
                            _GLRows.AmountDEV = dr_r("ÖsszegNettoDeviza")
                            _GLRows.AmountHUF = dr_r("ÖsszegNetto")
                            If LTrim(RTrim(dr_r("Bizonylatszám_P"))) = "" Then
                                _GLRows.ConnectInfo = _GLAccounts.DocumentNumber
                            Else
                                _GLRows.ConnectInfo = dr_r("Bizonylatszám_P")
                            End If
                            _GLRows.Controlling = Nothing
                            _GLRows.CostHolder = Nothing
                            _GLRows.CostPlace = Nothing
                            _GLRows.PartnerType = _GLAccounts.PartnerType
                            If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_h("S_Ügyfél"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_r("S_Költség"), _GLRows.GLHeader.DateExecution.Year))
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_h("S_Ügyfél"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_r("S_Költség"), _GLRows.GLHeader.DateExecution.Year))
                            End If

                            _GLRows.GLRowsGUID = _GUID
                            _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                            _GLRows.JobNumberObjects = Nothing
                            _GLRows.NoteRows = dr_r("Megjegyzés")
                            _GLRows.TransactionType = eTransactionType.eNotSet
                            _GLRows.UniqueObjects = Nothing
                            _GLRows.Customer = _GLAccounts.Customers
                            _GLRows.VAT = _uow.FindObject(Of VAT)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_r("Áfahiv")))
                            _GLRows.DateVAT = dr_h("ÁfaDátum")

                            '-- Jelölőadatok beállítása
                            _UniqueObjects = _uow.FindObject(Of UniqueObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Egyedi")))
                            If _UniqueObjects IsNot Nothing Then _GLRows.UniqueObjects = _UniqueObjects
                            _JobNumberObjects = _uow.FindObject(Of JobNumberObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Munkaszám")))
                            If _JobNumberObjects IsNot Nothing Then _GLRows.JobNumberObjects = _JobNumberObjects
                            _CostPlace = _uow.FindObject(Of CostPlace)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Költséghely")))
                            If _CostPlace IsNot Nothing Then _GLRows.CostPlace = _CostPlace
                            _CostHolder = _uow.FindObject(Of CostHolder)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Költségviselő")))
                            If _CostHolder IsNot Nothing Then _GLRows.CostHolder = _CostHolder
                            _Controlling = _uow.FindObject(Of Controlling)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Controlling")))
                            If _Controlling IsNot Nothing Then _GLRows.Controlling = _Controlling

                            If dr_r("Áfahiv") <> "" Then
                                _GLRows = New GLRows(_uow)
                                _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)
                                _GLRows.VAT = _uow.FindObject(Of VAT)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_r("Áfahiv")))
                                _GLRows.DateVAT = dr_h("ÁfaDátum")
                                _GLRows.AmountDEV = dr_r("ÖsszegAFADeviza")
                                _GLRows.AmountHUF = dr_r("ÖsszegAFA")
                                _GLRows.ConnectInfo = dr_r("Bizonylatszám_P")
                                _GLRows.Controlling = Nothing
                                _GLRows.CostHolder = Nothing
                                _GLRows.CostPlace = Nothing
                                _GLRows.PartnerType = _GLAccounts.PartnerType
                                If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                                    _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear", dr_h("S_Ügyfél"), _GLRows.GLHeader.DateExecution.Year))
                                    _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_r("S_ÁFA"), _GLRows.GLHeader.DateExecution.Year))
                                    If _GLRows.VAT IsNot Nothing Then
                                        If _GLRows.VAT.InversState = True Then
                                            _GLRows.DebitChartOfAccounts = _GLRows.VAT.GetChartOfAccounts(_GLRows.DateVAT.Year, eVATMode.eOut)
                                            _GLRows.CreditChartOfAccounts = _GLRows.VAT.GetChartOfAccounts(_GLRows.DateVAT.Year, eVATMode.eIn)
                                            If dr_h("Deviza") = "HUF" Then
                                                _GLRows.AmountDEV = 0
                                                _GLRows.AmountHUF = Math.Round(dr_r("ÖsszegNetto") * _GLRows.VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero)
                                            Else
                                                _GLRows.AmountDEV = Math.Round(dr_r("ÖsszegNettoDeviza") * _GLRows.VAT.InversKeyValue, 2, MidpointRounding.AwayFromZero)
                                                _GLRows.AmountHUF = Math.Round(dr_r("ÖsszegNettoDeviza") * Math.Round(dr_h("Árfolyam"), 2, MidpointRounding.AwayFromZero) * _GLRows.VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero)
                                            End If
                                            
                                        End If
                                    End If
                                Else
                                    _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_h("S_Ügyfél"), _GLRows.GLHeader.DateExecution.Year))
                                    _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_r("S_ÁFA"), _GLRows.GLHeader.DateExecution.Year))
                                End If

                                _GLRows.GLRowsGUID = _GUID
                                _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eVATRow
                                _GLRows.JobNumberObjects = Nothing
                                _GLRows.NoteRows = dr_r("Megjegyzés")
                                _GLRows.TransactionType = eTransactionType.eNotSet
                                _GLRows.UniqueObjects = Nothing
                                _GLRows.Customer = _GLAccounts.Customers

                            End If
                        End While
                        conn_r.Close()
                        _GLAccounts.RecalculateTotal(True)
                    End If
                End If
            End While
        End If
        conn_h.Close()


    End Sub
    Public Sub Import_GLBank(ByVal _Cassa As Long, ByVal _LiquidAssets As LiquidAssets, ByVal _Date As String, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, ByVal _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand

        Dim mSQL As String
        Dim i As Long = 1

        Dim _GLBank As GLBank = Nothing
        Dim _GLRows As GLRows = Nothing
        Dim _GLBank_Collection As New XPCollection(Of GLBank)(_uow, CriteriaOperator.Parse("DateExecution=? and CustomersFrom=? and LiquidAssets_Main=?", _Date, _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid), _uow.GetObjectByKey(Of LiquidAssets)(_LiquidAssets.Oid)))

        For Each _GLBank In _GLBank_Collection
            _uow.Delete(_GLBank.GLRows)
        Next
        _uow.Delete(_GLBank_Collection)

        mSQL = "select t1.Date,t1.ID,TömbSorszám=isnull(t1.TömbSorszám,''),t1.Date,"
        mSQL = mSQL & " Megjegyzés=isnull(t1.Megjegyzés,''),t2.QTT_Sum,t2.DQTT_Sum,"
        mSQL = mSQL & " Bizonylatszám_P=isnull(t2.Bizonylatszám_P,''),"
        mSQL = mSQL & " Comment=isnull(t3.Comment,''),SorMegjegyzés=isnull(t2.Comment,''),"
        mSQL = mSQL & " Ügyfélszám=isnull(t2.Ügyfélszám,0),"
        mSQL = mSQL & " TranType=t2.TranType,t2.Mode,"

        mSQL = mSQL & " M_Költség=isnull(t4.Megnevezés,'')"

        mSQL = mSQL & " from Cassa_Sum t1 join Cassa_Detail t2 on"
        mSQL = mSQL & " t1.LinkNumber=t2.LinkNumber left join Cassa t3 on"
        mSQL = mSQL & " t1.Cassa=t3.Cassa left join Költségek t4 on"
        mSQL = mSQL & " t2.KöltségID=t4.ID"

        mSQL = mSQL & " Where t1.Cassa=" & _Cassa & " and "
        mSQL = mSQL & "   t1.Date ='" & _Date & "'"
        mSQL = mSQL & " Order by t1.ID,t2.ID"

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                If i = 1 Then
                    _GLBank = New GLBank(_uow)
                    _GLBank.LiquidAssets_Main = _LiquidAssets
                    _GLBank.CustomersFrom = _CustomersFrom
                    _GLBank.DateCreated = dr_h("Date")
                    _GLBank.DateExecution = dr_h("Date")
                    _GLBank.DatePayment = dr_h("Date")
                    _GLBank.DocumentNumber = dr_h("TömbSorszám")
                    _GLBank.GLDocumentType = eGLDocumentType.eGLBank
                    _GLBank.IsEditable = False
                    _GLBank.IsStorno = False
                    _GLBank.NoteHeader = dr_h("Megjegyzés")
                    i += 1
                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLBank.Oid))
                    _uow.Delete(_GLRows_Collection)
                End If
                If _GLBank IsNot Nothing Then
                    _GLRows = New GLRows(_uow)
                    _GLRows.GLHeader = TryCast(_GLBank, GLHeader)
                    _GLRows.AmountHUF = dr_h("QTT_Sum")
                    _GLRows.AmountDEV = dr_h("DQTT_Sum")
                    _GLRows.ConnectInfo = dr_h("Bizonylatszám_P")
                    _GLRows.NoteRows = dr_h("SorMegjegyzés")
                    _GLRows.LiquidAssets = _LiquidAssets
                    _GLRows.GLRowsGUID = Guid.NewGuid
                    Select Case dr_h("TranType")
                        Case "Szállítói jóváírás"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                        Case "Szállítói terhelés"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case "Vevői jóváírás"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                        Case "Vevői terhelés"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case Else
                            _GLRows.PartnerType = ePartnerType.eNotSet
                            If dr_h("Mode") = 0 Then
                                _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            End If
                    End Select
                    If dr_h("Mode") = 0 Then 'Jóváírás
                        _GLRows.InAmountHUF = dr_h("QTT_Sum")
                        _GLRows.InAmountDEV = dr_h("DQTT_Sum")
                    Else                     'Terhelés
                        _GLRows.OutAmountHUF = dr_h("QTT_Sum")
                        _GLRows.OutAmountDEV = dr_h("DQTT_Sum")
                    End If

                End If
            End While
            If _GLBank IsNot Nothing Then _GLBank.RecalculateTotal()
        End If
        conn_h.Close()
    End Sub
    Public Sub Import_GLCassa(ByVal _Cassa As Long, ByVal _LiquidAssets As LiquidAssets, ByVal _LinkNumber As String, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, ByVal _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        Dim dr_r As SqlDataReader = Nothing

        Dim mSQL As String
        Dim i As Long = 1


        Dim _Customers As Customers = Nothing
        Dim _GLCassa As GLCassa = Nothing
        Dim _GLRows As GLRows
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(_uow, CriteriaOperator.Parse("DocumentNumber=? and CustomersFrom=? and LiquidAssets_Cassa=?", _
                                                                                             _LinkNumber, _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid), _
                                                                                             _uow.GetObjectByKey(Of LiquidAssets)(_LiquidAssets.Oid)))
        For Each _GLCassa In _GLCassa_Collection
            _uow.Delete(_GLCassa.GLRows)
        Next
        _uow.Delete(_GLCassa_Collection)

        mSQL = "select t1.Date,t1.ID,TömbSorszám=isnull(t1.TömbSorszám,''),t1.Date,"
        mSQL = mSQL & " Megjegyzés=isnull(t1.Megjegyzés,''),t2.QTT_Sum,t2.DQTT_Sum,"
        mSQL = mSQL & " Bizonylatszám_P=isnull(t2.Bizonylatszám_P,''),"
        mSQL = mSQL & " Comment=isnull(t3.Comment,''),SorMegjegyzés=isnull(t2.Comment,''),"
        mSQL = mSQL & " Ügyfélszám=isnull(t2.Ügyfélszám,0),"
        mSQL = mSQL & " TranType=t2.TranType,t2.Mode"

        mSQL = mSQL & " M_Költség=isnull(t4.Megnevezés,'')"

        mSQL = mSQL & " from Cassa_Sum t1 join Cassa_Detail t2 on"
        mSQL = mSQL & " t1.LinkNumber=t2.LinkNumber left join Cassa t3 on"
        mSQL = mSQL & " t1.Cassa=t3.Cassa left join Költségek t4 on"
        mSQL = mSQL & " t2.KöltségID=t4.ID"

        mSQL = mSQL & " Where t1.Cassa=" & _Cassa & " and "
        mSQL = mSQL & "   t1.LinkNumber =" & _LinkNumber
        mSQL = mSQL & " Order by t1.ID,t2.ID"
        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                If i = 1 Then
                    _GLCassa = New GLCassa(_uow)
                    _GLCassa.LiquidAssets_Cassa = _LiquidAssets
                    _GLCassa.CustomersFrom = _CustomersFrom
                    _GLCassa.DateCreated = dr_h("Date")
                    _GLCassa.DateExecution = dr_h("Date")
                    _GLCassa.DatePayment = dr_h("Date")
                    _GLCassa.DocumentNumber = _LinkNumber
                    _GLCassa.GLDocumentType = eGLDocumentType.eGLCassa
                    _GLCassa.IsEditable = False
                    _GLCassa.IsStorno = False
                    _GLCassa.NoteHeader = dr_h("Megjegyzés")

                    i += 1
                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLCassa.Oid))
                    _uow.Delete(_GLRows_Collection)
                End If
                If _GLCassa IsNot Nothing Then
                    _GLRows = New GLRows(_uow)
                    _GLRows.GLHeader = TryCast(_GLCassa, GLHeader)
                    _GLRows.AmountHUF = dr_h("QTT_Sum")
                    _GLRows.AmountDEV = dr_h("DQTT_Sum")
                    _GLRows.ConnectInfo = dr_h("Bizonylatszám_P")
                    _GLRows.NoteRows = dr_h("SorMegjegyzés")
                    _GLRows.GLRowsGUID = Guid.NewGuid
                    Select Case dr_h("TranType")
                        Case "Szállítói jóváírás"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                        Case "Szállítói terhelés"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case "Vevői jóváírás"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                        Case "Vevői terhelés"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case Else
                            _GLRows.PartnerType = ePartnerType.eNotSet
                            If dr_h("Mode") = 0 Then
                                _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            End If
                    End Select
                    If dr_h("Mode") = 0 Then 'Jóváírás
                        _GLRows.InAmountHUF = dr_h("QTT_Sum")
                        _GLRows.InAmountDEV = dr_h("DQTT_Sum")
                    Else                     'Terhelés
                        _GLRows.OutAmountHUF = dr_h("QTT_Sum")
                        _GLRows.OutAmountDEV = dr_h("DQTT_Sum")
                    End If

                End If
            End While
            If _GLCassa IsNot Nothing Then _GLCassa.RecalculateTotal(_GLCassa, Nothing, False)
        End If
        conn_h.Close()

    End Sub
    Public Sub Import_GLCompensation(ByVal _Cassa As Long, ByVal _LiquidAssets As LiquidAssets, ByVal _LinkNumber As String, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, ByVal _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        '   Dim dr_r As SqlDataReader

        Dim mSQL As String
        Dim i As Long = 1


        'Dim _Customers As Customers
        Dim _GLCompensation As GLCompensation = Nothing
        Dim _GLRows As GLRows
        Dim _GLCompensation_Collection As New XPCollection(Of GLCompensation)(_uow, CriteriaOperator.Parse("DocumentNumber=? and CustomersFrom=? and LiquidAssets_Main=?", _
                                                                                             "", _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid), _
                                                                                             _uow.GetObjectByKey(Of LiquidAssets)(_LiquidAssets.Oid)))

        For Each _GLCompensation In _GLCompensation_Collection
            _uow.Delete(_GLCompensation.GLRows)
        Next
        _uow.Delete(_GLCompensation_Collection)
        _GLCompensation_Collection = New XPCollection(Of GLCompensation)(_uow, CriteriaOperator.Parse("DocumentNumber=? and CustomersFrom=? and LiquidAssets_Main=?", _
                                                                                             _LinkNumber, _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid), _
                                                                                             _uow.GetObjectByKey(Of LiquidAssets)(_LiquidAssets.Oid)))

        For Each _GLCompensation In _GLCompensation_Collection
            _uow.Delete(_GLCompensation.GLRows)
        Next
        _uow.Delete(_GLCompensation_Collection)

        mSQL = "select t1.Date,t1.ID,TömbSorszám=isnull(t1.TömbSorszám,'" & _LinkNumber & "'),t1.Date,"
        mSQL = mSQL & " Megjegyzés=isnull(t1.Megjegyzés,''),t2.QTT_Sum,t2.DQTT_Sum,"
        mSQL = mSQL & " Bizonylatszám_P=isnull(t2.Bizonylatszám_P,t2.Bizonylatszám),"
        mSQL = mSQL & " Comment=isnull(t3.Comment,''),SorMegjegyzés=isnull(t2.Comment,''),"
        mSQL = mSQL & " Ügyfélszám=isnull(t2.Ügyfélszám,0),"

        mSQL = mSQL & " TranType=t2.TranType,t2.Mode,"

        mSQL = mSQL & " M_Költség=isnull(t4.Megnevezés,'')"

        mSQL = mSQL & " from Cassa_Sum t1 join Cassa_Detail t2 on"
        mSQL = mSQL & " t1.LinkNumber=t2.LinkNumber left join Cassa t3 on"
        mSQL = mSQL & " t1.Cassa=t3.Cassa left join Költségek t4 on"
        mSQL = mSQL & " t2.KöltségID=t4.ID"

        mSQL = mSQL & " Where t1.Cassa=" & _Cassa & " and "
        mSQL = mSQL & "   t1.LinkNumber =" & _LinkNumber
        mSQL = mSQL & " Order by t1.ID,t2.ID"
        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                If i = 1 Then
                    _GLCompensation = New GLCompensation(_uow)
                    _GLCompensation.LiquidAssets_Main = _LiquidAssets
                    _GLCompensation.CustomersFrom = _CustomersFrom
                    _GLCompensation.DateCreated = dr_h("Date")
                    _GLCompensation.DateExecution = dr_h("Date")
                    _GLCompensation.DatePayment = dr_h("Date")
                    _GLCompensation.BallanceDEV = 0
                    _GLCompensation.BallanceHUF = 0
                    If dr_h("TömbSorszám") = "" Then
                        _GLCompensation.DocumentNumber = _LinkNumber
                    Else
                        _GLCompensation.DocumentNumber = dr_h("TömbSorszám")
                    End If
                    _GLCompensation.GLDocumentType = eGLDocumentType.eGLCompensation
                    _GLCompensation.IsEditable = False
                    _GLCompensation.IsStorno = False
                    _GLCompensation.NoteHeader = dr_h("Megjegyzés")
                    i += 1
                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLCompensation.Oid))
                    _uow.Delete(_GLRows_Collection)
                End If
                If _GLCompensation IsNot Nothing Then
                    _GLRows = New GLRows(_uow)
                    _GLRows.GLHeader = TryCast(_GLCompensation, GLHeader)
                    _GLRows.AmountHUF = dr_h("QTT_Sum")
                    _GLRows.AmountDEV = dr_h("DQTT_Sum")
                    _GLRows.ConnectInfo = dr_h("Bizonylatszám_P")
                    _GLRows.NoteRows = dr_h("SorMegjegyzés")
                    _GLRows.GLRowsGUID = Guid.NewGuid
                    Select Case dr_h("TranType")
                        Case "Szállítói jóváírás"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                        Case "Szállítói terhelés"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case "Vevői jóváírás"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                        Case "Vevői terhelés"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut)
                            _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                        Case Else
                            _GLRows.PartnerType = ePartnerType.eNotSet
                            If dr_h("Mode") = 0 Then
                                _GLRows.DebitChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), _GLRows.GLHeader.DateExecution.Year))
                                _GLRows.CreditChartOfAccounts = _LiquidAssets.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, 0)
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            End If
                    End Select
                    If dr_h("Mode") = 0 Then 'Jóváírás
                        _GLRows.InAmountHUF = dr_h("QTT_Sum")
                        _GLRows.InAmountDEV = dr_h("DQTT_Sum")
                    Else                     'Terhelés
                        _GLRows.OutAmountHUF = dr_h("QTT_Sum")
                        _GLRows.OutAmountDEV = dr_h("DQTT_Sum")
                    End If
                    '-- Jelölő adatok ????

                End If
            End While
        End If
        conn_h.Close()

    End Sub
    Public Sub Import_GLMixed(ByVal _FK_ID As Long, _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, ByVal _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        ' Dim dr_r As SqlDataReader

        Dim mSQL As String
        Dim i As Long = 1



        Dim _GLMixed As GLMixed = Nothing
        Dim _GLRows As GLRows = Nothing

        Dim _UniqueObjects As UniqueObjects = Nothing
        Dim _JobNumberObjects As JobNumberObjects = Nothing
        Dim _CostHolder As CostHolder = Nothing
        Dim _CostPlace As CostPlace = Nothing
        Dim _Controlling As Controlling = Nothing

        mSQL = "select KSorszám=isnull(t3.KSorszám,''),t3.ED,t3.Bizonylatszám,"
        mSQL = mSQL & " Megjegyzés=isnull(t3.Megjegyzés,''),IktatóSzám=isnull(t3.IktatóSzámK,''),"
        mSQL = mSQL & " M_Debit=isnull(t5.Számlaszám,''),M_Credit=isnull(t6.Számlaszám,''),"
        mSQL = mSQL & " t1.Összeg,S_Megjegyzés=isnull(t2.Megjegyzés,''),TD=t3.ED,"

        mSQL = mSQL & " M_Munkaszám=isnull(t20.Megnevezés,''),"
        mSQL = mSQL & " M_Egyedi=isnull(t21.Megnevezés,''),"
        mSQL = mSQL & " M_Költséghely=isnull(t23.Megnevezés,''),"
        mSQL = mSQL & " M_Költségviselő=isnull(t24.Megnevezés,''),"
        mSQL = mSQL & " M_Controlling=isnull(t25.Megnevezés,'')"

        mSQL = mSQL & " from sql_Cassa..FKBizonylatok_Részletes t1 join sql_Cassa..FKBizonylatok_Sorok t2 on"
        mSQL = mSQL & " t1.ParentID1=t2.ID join sql_Cassa..FKBizonylatok t3 on"
        mSQL = mSQL & " t1.ParentID=t3.ID left join sql_Cassa..Költségek t5 on"
        mSQL = mSQL & " t1.KöltségIDSzámla=t5.ID left join sql_Cassa..Költségek t6 on"
        mSQL = mSQL & " t1.KöltségIDEllenszámla=t6.ID left join Munkaszám t20 on"
        mSQL = mSQL & " t2.MunkaszámID=t20.ID left join Egyedi t21 on"
        mSQL = mSQL & " t2.EgyediID=t21.ID left join RögzítőKód t22 on"
        mSQL = mSQL & " t2.RögzítőKódID=t22.ID left join Költséghely t23 on"
        mSQL = mSQL & " t22.KöltséghelyID=t23.ID left join Költségviselő t24 on"
        mSQL = mSQL & " t22.KöltségviselőID=t24.ID left join Költségviselő t25 on"
        mSQL = mSQL & " t2.ControllingID=t25.ID"

        mSQL = mSQL & " Where t1.Oldal='K' and t3.ID=" & _FK_ID
        mSQL = mSQL & " Order by t1.ID,t2.ID"

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                If i = 1 Then
                    _GLMixed = _uow.FindObject(Of GLMixed)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                        CriteriaOperator.Parse("DocumentNumber=? and CustomersFrom.oid=?", dr_h("KSorszám") & "->" & dr_h("Bizonylatszám"), _CustomersFrom.Oid))
                    If _GLMixed Is Nothing Then _GLMixed = New GLMixed(_uow)

                    _GLMixed.CurrencyName = _uow.FindObject(Of CurrencyName)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName='HUF'"))
                    _GLMixed.CurrencyRate = 1
                    _GLMixed.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _GLMixed.DateCreated = dr_h("ED")
                    _GLMixed.DateExecution = dr_h("ED")
                    _GLMixed.DatePayment = dr_h("ED")
                    _GLMixed.DocumentNumber = dr_h("KSorszám") & "->" & dr_h("Bizonylatszám")
                    _GLMixed.GLDocumentType = eGLDocumentType.eGLMixed
                    _GLMixed.IsEditable = False
                    _GLMixed.IsStorno = False
                    _GLMixed.RegistryNumber = dr_h("IktatóSzám")
                    _GLMixed.NoteHeader = dr_h("Megjegyzés")
                    _uow.Delete(_GLMixed.GLRows)
                    i += 1
                End If

                _GLRows = New GLRows(_uow)
                _GLRows.GLHeader = TryCast(_GLMixed, GLHeader)
                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                  CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_h("M_Debit"), _GLRows.GLHeader.DateExecution.Year))
                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                  CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", dr_h("M_Credit"), _GLRows.GLHeader.DateExecution.Year))
                _GLRows.AmountDEV = 0
                _GLRows.AmountHUF = dr_h("Összeg")

                _GLRows.GLRowsGUID = Guid.NewGuid
                _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                _GLRows.NoteRows = dr_h("S_Megjegyzés")
                _GLRows.PartnerType = ePartnerType.eNotSet
                _GLRows.TransactionType = eTransactionType.eNotSet

                '-- Jelölőadatok beállítása
                _UniqueObjects = _uow.FindObject(Of UniqueObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_h("M_Egyedi")))
                If _UniqueObjects IsNot Nothing Then _GLRows.UniqueObjects = _UniqueObjects
                _JobNumberObjects = _uow.FindObject(Of JobNumberObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_h("M_Munkaszám")))
                If _JobNumberObjects IsNot Nothing Then _GLRows.JobNumberObjects = _JobNumberObjects
                _CostPlace = _uow.FindObject(Of CostPlace)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_h("M_Költséghely")))
                If _CostPlace IsNot Nothing Then _GLRows.CostPlace = _CostPlace
                _CostHolder = _uow.FindObject(Of CostHolder)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_h("M_Költségviselő")))
                If _CostHolder IsNot Nothing Then _GLRows.CostHolder = _CostHolder
                _Controlling = _uow.FindObject(Of Controlling)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_h("M_Controlling")))
                If _Controlling IsNot Nothing Then _GLRows.Controlling = _Controlling

            End While
        End If
        conn_h.Close()

    End Sub
    Public Sub Import_Invoice(ByVal conn As SqlConnection, ByVal ID As Long, ByVal _InvoiceHeader As InvoiceHeader, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom)
        'Dim cmd_h As New SqlCommand
        'Dim dr_h As SqlDataReader

        'Dim cmd_r As New SqlCommand
        'Dim dr_r As SqlDataReader

        'Dim _Customers As Customers
        'Dim mSQL As String
        'Dim _InvoiceRows As InvoiceRows
        'mSQL = "select t1.Ügyfélszám "
        'mSQL = mSQL & " from sql_Main..Számlaössz t1"
        'mSQL = mSQL & " Where t1.ID=" & ID

        'cmd_h.CommandTimeout = 60
        'cmd_h.Connection = conn
        'cmd_h.CommandType = CommandType.Text
        'cmd_h.CommandText = mSQL
        'conn.Open()
        'If conn.State = ConnectionState.Open Then
        '    dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
        '    While dr_h.Read
        '        _Customers = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
        '                     CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
        '        If _Customers IsNot Nothing Then

        '            _InvoiceHeader = _uow.FindObject(Of InvoiceHeader)(PersistentCriteriaEvaluationBehavior.InTransaction, _
        '                        CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and DocumentNumber=?", _
        '                                               _CustomersFrom.Oid, _Customers.Oid, dr_h("Bizonylatszám")))
        '            If _InvoiceHeader Is Nothing Then _InvoiceHeader = New InvoiceHeader(_uow)

        '            _InvoiceHeader.BankInformation
        '            _InvoiceHeader.BookEntryErrorString
        '            _InvoiceHeader.ConnectInfo
        '            _InvoiceHeader.CurrencyName
        '            _InvoiceHeader.Customers = _Customers
        '            _InvoiceHeader.CustomersFrom = _CustomersFrom
        '            _InvoiceHeader.DateCreated
        '            _InvoiceHeader.DateExecution
        '            _InvoiceHeader.DatePayment
        '            _InvoiceHeader.Description
        '            _InvoiceHeader.DocumentNumber
        '            _InvoiceHeader.HUNumberToText
        '            _InvoiceHeader.InvoiceType
        '            _InvoiceHeader.IsEditable = False
        '            _InvoiceHeader.IsStorno
        '            _InvoiceHeader.IsImported
        '            _InvoiceHeader.IsProforma
        '            _InvoiceHeader.PaymentOption
        '            _InvoiceHeader.QualityOption
        '            _InvoiceHeader.ReadyforBookEntry = True
        '            _InvoiceHeader.Saved_Customers_Address_City
        '            _InvoiceHeader.Saved_Customers_Address_Country
        '            _InvoiceHeader.Saved_Customers_Address_StateProvince
        '            _InvoiceHeader.Saved_Customers_Address_Street
        '            _InvoiceHeader.Saved_Customers_Address_ZipPostal
        '            _InvoiceHeader.Saved_Customers_FullAddress
        '            _InvoiceHeader.Saved_Customers_Name
        '            _InvoiceHeader.Saved_Customers_VATNumber
        '            _InvoiceHeader.Saved_Customers_VATNumberEU
        '            _InvoiceHeader.Saved_CustomersFrom_Address_City
        '            _InvoiceHeader.Saved_CustomersFrom_Address_Country
        '            _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince
        '            _InvoiceHeader.Saved_CustomersFrom_Address_Street
        '            _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal
        '            _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber
        '            _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN
        '            _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName
        '            _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT
        '            _InvoiceHeader.Saved_CustomersFrom_FullAddress
        '            _InvoiceHeader.Saved_CustomersFrom_Name
        '            _InvoiceHeader.Saved_CustomersFrom_VATNumber
        '            _InvoiceHeader.Saved_CustomersFrom_VATNumberEU
        '            _InvoiceHeader.ShipmentOption
        '            _InvoiceHeader.TotalBruttoDEV
        '            _InvoiceHeader.TotalBruttoHUF

        '            mSQL = "select Áfahiv=isnull(t1.Áfahiv,''),t2.Bizonylatszám_P,"
        '            mSQL = mSQL & " from Folyó_Bizonylatok_Sorok t1 join Folyó_Bizonylatok t2 on"
        '            mSQL = mSQL & " t1.ParentID=t2.ID"
        '            mSQL = mSQL & " Where t1.ParentID=" & ID
        '            mSQL = mSQL & " order by t1.ID"

        '            cmd_r.CommandTimeout = 60
        '            cmd_r.Connection = conn
        '            cmd_r.CommandType = CommandType.Text
        '            cmd_r.CommandText = mSQL
        '            conn.Open()

        '            Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GlHeader.Oid=?", _GLAccounts.Oid))
        '            _uow.Delete(_GLRows_Collection)

        '            If conn.State = ConnectionState.Open Then
        '                dr_r = cmd_r.ExecuteReader(CommandBehavior.SingleResult)
        '                While dr_r.Read
        '                    _InvoiceRows.ChartOfAccounts
        '                    _InvoiceRows.ContractRows
        '                    _InvoiceRows.Controlling
        '                    _InvoiceRows.CostHolder
        '                    _InvoiceRows.CostPlace
        '                    _InvoiceRows.CustomsTariffs
        '                    _InvoiceRows.Description
        '                    _InvoiceRows.DiscountPriceDEV
        '                    _InvoiceRows.DiscountPriceHUF
        '                    _InvoiceRows.InvoiceHeader = _InvoiceHeader
        '                    _InvoiceRows.InvoiceRowtype = eInvoiceRowType.FromManual
        '                    _InvoiceRows.JobNumberObjects
        '                    _InvoiceRows.ListPriceDEV
        '                    _InvoiceRows.ListPriceHUF
        '                    _InvoiceRows.PriceDEV
        '                    _InvoiceRows.PriceHUF
        '                    _InvoiceRows.QTT
        '                    _InvoiceRows.QTT
        '                    _InvoiceRows.QualityOption
        '                    _InvoiceRows.ReadyforBookEntryRows
        '                    _InvoiceRows.RowIndex
        '                    _InvoiceRows.ShortName
        '                    _InvoiceRows.UniqueObjects
        '                    _InvoiceRows.Units
        '                    _InvoiceRows.VAT
        '                    _InvoiceRows.WorkflowSystemSteps
        '        End If
        '    End While
        'End If
    End Sub
    Public Sub Import_Invoice_E(ByVal conn As SqlConnection, ByVal ID As Long, ByVal _InvoiceHeader As InvoiceHeader, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom)
        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        Dim dr_r As SqlDataReader

        Dim _Customers As Customers
        Dim mSQL As String
        Dim _InvoiceRows As InvoiceRows
        mSQL = "select t1.Ügyfélszám,Bankszámlaszám=isnull(t1.Bankszámlaszám,''),Bizonylatszám=isnull(t1.Bizonylatszám,''),"
        mSQL = mSQL & ""
        mSQL = mSQL & " from sql_Cassa..Bill_Sum t1"
        mSQL = mSQL & " Where t1.ID=" & ID

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn.Open()
        If conn.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                _Customers = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                             CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                If _Customers IsNot Nothing Then

                End If
                _InvoiceHeader = _uow.FindObject(Of InvoiceHeader)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                            CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and DocumentNumber=?", _
                                                   _CustomersFrom.Oid, _Customers.Oid, dr_h("Bizonylatszám")))
                If _InvoiceHeader Is Nothing Then _InvoiceHeader = New InvoiceHeader(_uow)

                _InvoiceHeader.BankInformation = _uow.FindObject(Of BankInformation)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=?", dr_h("Bankszámlaszám")))
                _InvoiceHeader.ConnectInfo = dr_h("Bizonylatszám")
                _InvoiceHeader.CurrencyName = _uow.FindObject(Of CurrencyName)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_h("Deviza")))
                _InvoiceHeader.Customers = _Customers
                _InvoiceHeader.CustomersFrom = _CustomersFrom
                _InvoiceHeader.DateCreated = dr_h("KD")
                _InvoiceHeader.DateExecution = dr_h("TD")
                _InvoiceHeader.DatePayment = dr_h("ED")
                _InvoiceHeader.Description = dr_h("Note")
                _InvoiceHeader.DocumentNumber = dr_h("Bizonylatszám")
                _InvoiceHeader.HUNumberToText = dr_h("AzazStr")
                _InvoiceHeader.IsEditable = False
                _InvoiceHeader.IsImported = True
                _InvoiceHeader.IsProforma = False
                _InvoiceHeader.ReadyforBookEntry = False
                _InvoiceHeader.Saved_Customers_Address_City = _Customers.Address1.City
                _InvoiceHeader.Saved_Customers_Address_Country = _Customers.Address1.Country.Name
                _InvoiceHeader.Saved_Customers_Address_StateProvince = _Customers.Address1.StateProvince
                _InvoiceHeader.Saved_Customers_Address_Street = _Customers.Address1.Street
                _InvoiceHeader.Saved_Customers_Address_ZipPostal = _Customers.Address1.ZipPostal
                _InvoiceHeader.Saved_Customers_Name = _Customers.Name
                _InvoiceHeader.Saved_Customers_VATNumber = _Customers.VATNumber
                _InvoiceHeader.Saved_Customers_VATNumberEU = _Customers.VATNumberEU

                _InvoiceHeader.Saved_CustomersFrom_Address_City = _CustomersFrom.Address1.City
                _InvoiceHeader.Saved_CustomersFrom_Address_Country = _CustomersFrom.Address1.Country.Name
                _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _CustomersFrom.Address1.StateProvince
                _InvoiceHeader.Saved_CustomersFrom_Address_Street = _CustomersFrom.Address1.Street
                _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _CustomersFrom.Address1.ZipPostal
                _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber
                _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN
                _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = ""
                _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT

                _InvoiceHeader.Saved_CustomersFrom_Name = _CustomersFrom.Name
                _InvoiceHeader.Saved_CustomersFrom_VATNumber = _CustomersFrom.VATNumber
                _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _CustomersFrom.VATNumberEU

                '_InvoiceHeader.TotalBruttoDEV
                '_InvoiceHeader.TotalBruttoHUF

                mSQL = "select D_Name=isnull(t1.D_Name,''),"
                mSQL = mSQL & " from sql_Cassa..Bill_Detail t1 join sql_Cassa..Bill_Sum t2 on"
                mSQL = mSQL & " t1.Bizonylatszám=t2.Bizonylatszám"
                mSQL = mSQL & " Where t2.ID=" & ID
                mSQL = mSQL & " order by t1.ID"

                cmd_r.CommandTimeout = 60
                cmd_r.Connection = conn
                cmd_r.CommandType = CommandType.Text
                cmd_r.CommandText = mSQL
                conn.Open()

                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_uow, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid))
                _uow.Delete(_InvoiceRows_Collection)
                Dim _RowIndex As Long = 1
                If conn.State = ConnectionState.Open Then
                    dr_r = cmd_r.ExecuteReader(CommandBehavior.SingleResult)
                    While dr_r.Read
                        _InvoiceRows = New InvoiceRows(_uow)
                        '_InvoiceRows.ChartOfAccounts
                        '_InvoiceRows.ContractRows
                        '_InvoiceRows.Controlling
                        '_InvoiceRows.CostHolder
                        '_InvoiceRows.CostPlace
                        '_InvoiceRows.CustomsTariffs
                        _InvoiceRows.Description = dr_r("")
                        _InvoiceRows.DiscountPriceDEV = 0
                        _InvoiceRows.DiscountPriceHUF = 0
                        _InvoiceRows.InvoiceHeader = _InvoiceHeader
                        _InvoiceRows.InvoiceRowtype = eInvoiceRowType.FromManual
                        '_InvoiceRows.JobNumberObjects
                        '_InvoiceRows.ListPriceDEV
                        '_InvoiceRows.ListPriceHUF
                        '_InvoiceRows.PriceDEV
                        '_InvoiceRows.PriceHUF
                        '_InvoiceRows.QTT
                        '_InvoiceRows.QTT
                        '_InvoiceRows.QualityOption
                        _InvoiceRows.ReadyforBookEntryRows = False
                        '_InvoiceRows.RowIndex
                        '_InvoiceRows.ShortName
                        '_InvoiceRows.UniqueObjects
                        '_InvoiceRows.Units
                        '_InvoiceRows.VAT
                        '_InvoiceRows.WorkflowSystemSteps

                    End While
                End If
            end while
        End If
    End Sub

    Public Sub Import_FN(ByVal _uow As UnitOfWork, ByVal _LinkNumber As String, ByVal _CustomersFrom As CustomersFrom, ByVal _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        ' Dim dr_r As SqlDataReader

        Dim mSQL As String
        Dim i As Long = 1

        Dim _ChartOfAccounts_Open As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491'"))
        If _ChartOfAccounts_Open Is Nothing Then Exit Sub

        'Dim _Customers As Customers
        Dim _GLMixed As GLMixed
        Dim _GLRows As GLRows
        Dim _GLMixed_Collection As New XPCollection(Of GLMixed)(_uow, CriteriaOperator.Parse("DocumentNumber=? and CustomersFrom=?", _
                                                                                             _LinkNumber, _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)))
        _uow.Delete(_GLMixed_Collection)

        mSQL = "select t1.Date,t1.ID,TömbSorszám=isnull(t1.TömbSorszám,''),t1.Date,"
        mSQL = mSQL & " Megjegyzés=isnull(t1.Megjegyzés,''),t2.QTT_Sum,t2.DQTT_Sum,"
        mSQL = mSQL & " Bizonylatszám_P=isnull(t2.Bizonylatszám_P,''),"
        mSQL = mSQL & " Comment=isnull(t3.Comment,''),SorMegjegyzés=isnull(t2.Comment,''),"
        mSQL = mSQL & " Ügyfélszám=isnull(t2.Ügyfélszám,0),"
        mSQL = mSQL & " TranType=t1.TranType,t2.Mode,"

        mSQL = mSQL & " M_Költség=isnull(t4.Megnevezés,'')"

        mSQL = mSQL & " from Cassa_Sum t1 join Cassa_Detail t2 on"
        mSQL = mSQL & " t1.LinkNumber=t2.LinkNumber left join Cassa t3 on"
        mSQL = mSQL & " t1.Cassa=t3.Cassa left join Költségek t4 on"
        mSQL = mSQL & " t2.KöltségID=t4.ID"

        mSQL = mSQL & " Where t2.PBizonylatszám='" & _LinkNumber & "'"

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                If i = 1 Then
                    _GLMixed = New GLMixed(_uow)
                    _GLMixed.CustomersFrom = _CustomersFrom
                    _GLMixed.DateCreated = dr_h("Date")
                    _GLMixed.DateExecution = dr_h("Date")
                    _GLMixed.DatePayment = dr_h("Date")
                    _GLMixed.DocumentNumber = _LinkNumber
                    _GLMixed.GLDocumentType = eGLDocumentType.eGLMixed
                    _GLMixed.IsEditable = False
                    _GLMixed.IsStorno = False
                    _GLMixed.NoteHeader = dr_h("Megjegyzés")

                    i += 1
                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLMixed.Oid))
                    _uow.Delete(_GLRows_Collection)
                End If
                If _GLMixed IsNot Nothing Then
                    _GLRows = New GLRows(_uow)
                    _GLRows.GLHeader = TryCast(_GLMixed, GLHeader)
                    _GLRows.AmountHUF = dr_h("QTT_Sum")
                    _GLRows.AmountDEV = dr_h("DQTT_Sum")
                    _GLRows.ConnectInfo = dr_h("Bizonylatszám_P")
                    _GLRows.NoteRows = dr_h("SorMegjegyzés")
                    Select Case dr_h("TranType")
                        Case "Szállítói jóváírás"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(Year(dr_h("Date")), eVATMode.eIn)
                        Case "Szállítói terhelés"
                            _GLRows.PartnerType = ePartnerType.ePayabels
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(Year(dr_h("Date")), eVATMode.eIn)
                            _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                        Case "Vevői jóváírás"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                            _GLRows.CreditChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(Year(dr_h("Date")), eVATMode.eOut)
                        Case "Vevői terhelés"
                            _GLRows.PartnerType = ePartnerType.eReceivables
                            _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            _GLRows.DebitChartOfAccounts = _GLRows.Customer.CustomersGLParameters.GetChartOfAccounts(Year(dr_h("Date")), eVATMode.eOut)
                            _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                        Case Else
                            _GLRows.PartnerType = ePartnerType.eNotSet
                            If dr_h("Mode") = 0 Then
                                _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), Year(dr_h("Date"))))
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                                                                                    CriteriaOperator.Parse("Name=? and AccountYear=?", dr_h("M_Költség"), Year(dr_h("Date"))))
                                _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                                _GLRows.Customer = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                            End If
                    End Select
                    If dr_h("Mode") = 0 Then 'Jóváírás
                        _GLRows.InAmountHUF = dr_h("QTT_Sum")
                        _GLRows.InAmountDEV = dr_h("DQTT_Sum")
                    Else                     'Terhelés
                        _GLRows.OutAmountHUF = dr_h("QTT_Sum")
                        _GLRows.OutAmountDEV = dr_h("DQTT_Sum")
                    End If

                End If
            End While

        End If
        conn_h.Close()


    End Sub
    Public Sub Import_FN_1(ByVal _ID As Long, ByVal _uow As UnitOfWork, ByVal _CustomersFrom As CustomersFrom, _ConnectionString As String)
        '// MS SQL régi rendszerből importál Folyószámlából
        Dim conn_h As New SqlConnection(_ConnectionString)
        Dim conn_r As New SqlConnection(_ConnectionString)

        Dim cmd_h As New SqlCommand
        Dim dr_h As SqlDataReader

        Dim cmd_r As New SqlCommand
        Dim dr_r As SqlDataReader

        Dim mSQL As String
        Dim _Customers As Customers
        Dim _GLRows As GLRows
        Dim _GUID As Guid
        Dim _GLAccounts As GLAccounts
        Dim _PaymentOption As PaymentOption

        Dim _UniqueObjects As UniqueObjects
        Dim _JobNumberObjects As JobNumberObjects
        Dim _CostHolder As CostHolder
        Dim _CostPlace As CostPlace
        Dim _Controlling As Controlling

        Dim _ChartOfAccounts_Open As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491'"))
        If _ChartOfAccounts_Open Is Nothing Then Exit Sub

        mSQL = "select Ügyfélszám=t2.CégAlany,t1.BizTipus,Bizonylatszám=isnull(t1.Bizonylatszám,''),t1.TD,t1.ED,t1.KD,t1.ÁFADátum,"
        mSQL = mSQL & " Árfolyam=isnull(t2.Árfolyam,1),IktatóSzám=isnull(t1.IktatóSzám,''),"
        mSQL = mSQL & " S_Ügyfél=case when t1.BizTipus=0 then isnull(t6.Számlaszám,'') else isnull(t5.Számlaszám,'') end,"
        mSQL = mSQL & " t2.Storno,Megjegyzés=isnull(t1.Megjegyzés,''),Deviza=isnull(t1.Deviza,''),"
        mSQL = mSQL & " ID_Dokument_Header=isnull(t2.ID_Dokument_Header,-1),"

        mSQL = mSQL & " FizetésiMód=isnull(t1.FizetésiMód,'')"

        mSQL = mSQL & " from sql_Cassa..vv_ÖsszLista t1 left join sql_Cassa..Folyó_Bizonylatok t2 on"
        mSQL = mSQL & " t1.ID=t2.ID left join sql_Main..Ügyféltörzs t3 on"
        mSQL = mSQL & " t2.CégAlany=t3.Ügyfélszám left join Ügyféltörzs_Mode t4 on"
        mSQL = mSQL & " t3.Mode=t4.Mode left join sql_Cassa..Költségek t5 on"
        mSQL = mSQL & " t4.KöltségIDIn=t5.ID left join sql_Cassa..Költségek t6 on"
        mSQL = mSQL & " t4.KöltségIDOut=t6.ID"

        mSQL = mSQL & " Where t1.ID=" & _ID

        cmd_h.CommandTimeout = 60
        cmd_h.Connection = conn_h
        cmd_h.CommandType = CommandType.Text
        cmd_h.CommandText = mSQL
        conn_h.Open()
        If conn_h.State = ConnectionState.Open Then
            dr_h = cmd_h.ExecuteReader(CommandBehavior.SingleResult)
            While dr_h.Read
                _PaymentOption = _uow.FindObject(Of PaymentOption)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_h("FizetésiMód")))
                If _PaymentOption Is Nothing Then
                    _PaymentOption = New PaymentOption(_uow)
                    If UCase(dr_h("FizetésiMód")) = "KÉSZPÉNZ" Then
                        _PaymentOption.PayMode = ePayMode.InCash
                        _PaymentOption.PayDay = 0
                        _PaymentOption.ShortName = dr_h("FizetésiMód")
                        _PaymentOption.Description = dr_h("FizetésiMód")
                    Else
                        _PaymentOption.PayMode = ePayMode.InTransfer
                        _PaymentOption.PayDay = 8
                        _PaymentOption.ShortName = dr_h("FizetésiMód")
                        _PaymentOption.Description = dr_h("FizetésiMód")
                    End If
                End If
                _Customers = _uow.FindObject(Of Customers)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                 CriteriaOperator.Parse("CustomerNumber=?", dr_h("Ügyfélszám").ToString))
                If _Customers IsNot Nothing Then

                    _GLAccounts = _uow.FindObject(Of GLAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and PartnerType=? and DocumentNumber=?", _
                                                       _CustomersFrom.Oid, _Customers.Oid, dr_h("BizTipus"), dr_h("Bizonylatszám")))
                    If _GLAccounts Is Nothing Then _GLAccounts = New GLAccounts(_uow)

                    _GLAccounts.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_CustomersFrom.Oid)
                    _GLAccounts.Customers = _Customers
                    _GLAccounts.PaymentOption = _PaymentOption

                    _GLAccounts.CurrencyName = _uow.FindObject(Of CurrencyName)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_h("Deviza")))
                    If dr_h("Deviza") = "HUF" Then
                        _GLAccounts.CurrencyRate = 1
                        _GLAccounts.CurrencyRateVAT() = 1
                    Else
                        _GLAccounts.CurrencyRate = dr_h("Árfolyam")
                        _GLAccounts.CurrencyRateVAT() = dr_h("Árfolyam")
                    End If
                    '_GLAccounts.CustomerBankAccounts
                    _GLAccounts.DateCreated = dr_h("KD")
                    _GLAccounts.DateExecution = dr_h("TD")
                    _GLAccounts.DatePayment = dr_h("ED")
                    _GLAccounts.DocumentNumber = dr_h("Bizonylatszám")
                    _GLAccounts.GLDocumentType = eGLDocumentType.eGLAccount
                    _GLAccounts.IsEditable = False
                    _GLAccounts.IsStorno = dr_h("Storno")
                    _GLAccounts.NoteHeader = dr_h("Megjegyzés")
                    _GLAccounts.PartnerType = dr_h("BizTipus")
                    '_GLAccounts.PaymentOption
                    _GLAccounts.RegistryNumber = dr_h("IktatóSzám")
                    _GLAccounts.IsRegistry = False

                    If dr_h("ID_Dokument_Header") > 0 Then
                        Dim _FS As FileStream
                        Dim _FilePath As String

                        _FilePath = LoadFileFromSQL(dr_h("ID_Dokument_Header"), _ConnectionString)
                        If System.IO.File.Exists(_FilePath) Then
                            _FS = New FileStream(_FilePath, FileMode.Open)

                            _GLAccounts.DocumentFile = New FileData(_uow)
                            _GLAccounts.DocumentFile.LoadFromStream(_FilePath, _FS)
                            _GLAccounts.DocumentFile.FileName = System.IO.Path.GetFileName(_FilePath)

                            _FS.Close()
                            _FS = Nothing
                        End If
                    End If

                    mSQL = "select Áfahiv=isnull(t1.Áfahiv,''),Bizonylatszám_P=isnull(t2.Bizonylatszám_P,''),t1.ÖsszegNetto,t1.ÖsszegNettoDeviza,"
                    mSQL = mSQL & " t1.ÖsszegAFADeviza,t1.ÖsszegAFA,"
                    mSQL = mSQL & " S_Költség=isnull(t3.Számlaszám,''),t1.Megjegyzés,"
                    mSQL = mSQL & " S_ÁFA=case when t2.BizTipus=0 then isnull(t5.Számlaszám,'') else isnull(t6.Számlaszám,'') end,"

                    mSQL = mSQL & " M_Munkaszám=isnull(t20.Megnevezés,''),"
                    mSQL = mSQL & " M_Egyedi=isnull(t21.Megnevezés,''),"
                    mSQL = mSQL & " M_Költséghely=isnull(t23.Megnevezés,''),"
                    mSQL = mSQL & " M_Költségviselő=isnull(t24.Megnevezés,''),"
                    mSQL = mSQL & " M_Controlling=isnull(t25.Megnevezés,'')"

                    mSQL = mSQL & " from Folyó_Bizonylatok_Sorok t1 join sql_Cassa..Folyó_Bizonylatok t2 on"
                    mSQL = mSQL & " t1.ParentID=t2.ID left join sql_Cassa..Költségek t3 on"
                    mSQL = mSQL & " t1.KöltségID=t3.ID left join sql_Cassa..VAT t4 on"
                    mSQL = mSQL & " t1.Áfahiv=t4.ShortName left join sql_Cassa..Költségek t5 on"
                    mSQL = mSQL & " t4.KöltségIDOut=t5.ID left join sql_Cassa..Költségek t6 on"
                    mSQL = mSQL & " t4.KöltségIDIn=t6.ID left join Munkaszám t20 on"
                    mSQL = mSQL & " t1.MunkaszámID=t20.ID left join Egyedi t21 on"
                    mSQL = mSQL & " t1.EgyediID=t21.ID left join RögzítőKód t22 on"
                    mSQL = mSQL & " t1.RögzítőKódID=t22.ID left join Költséghely t23 on"
                    mSQL = mSQL & " t22.KöltséghelyID=t23.ID left join Költségviselő t24 on"
                    mSQL = mSQL & " t22.KöltségviselőID=t24.ID left join Költségviselő t25 on"
                    mSQL = mSQL & " t1.ControllingID=t25.ID"

                    mSQL = mSQL & " Where t1.ParentID=" & _ID
                    mSQL = mSQL & " order by t1.ID"

                    cmd_r.CommandTimeout = 60
                    cmd_r.Connection = conn_r
                    cmd_r.CommandType = CommandType.Text
                    cmd_r.CommandText = mSQL
                    conn_r.Open()

                    Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLHeader.Oid=?", _GLAccounts.Oid))
                    _uow.Delete(_GLRows_Collection)

                    If conn_r.State = ConnectionState.Open Then
                        dr_r = cmd_r.ExecuteReader(CommandBehavior.SingleResult)
                        While dr_r.Read
                            _GUID = Guid.NewGuid
                            _GLRows = New GLRows(_uow)
                            _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)
                            _GLRows.AmountDEV = dr_r("ÖsszegNettoDeviza")
                            _GLRows.AmountHUF = dr_r("ÖsszegNetto")
                            If LTrim(RTrim(dr_r("Bizonylatszám_P"))) = "" Then
                                _GLRows.ConnectInfo = _GLAccounts.DocumentNumber
                            Else
                                _GLRows.ConnectInfo = dr_r("Bizonylatszám_P")
                            End If
                            _GLRows.Controlling = Nothing
                            _GLRows.CostHolder = Nothing
                            _GLRows.CostPlace = Nothing
                            _GLRows.PartnerType = _GLAccounts.PartnerType
                            If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                                _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=?", dr_h("S_Ügyfél")))
                                _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                            Else
                                _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=?", dr_h("S_Ügyfél")))
                                _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                            End If

                            _GLRows.GLRowsGUID = _GUID
                            _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                            _GLRows.JobNumberObjects = Nothing
                            _GLRows.NoteRows = dr_r("Megjegyzés")
                            _GLRows.TransactionType = eTransactionType.eNotSet
                            _GLRows.UniqueObjects = Nothing
                            _GLRows.Customer = _GLAccounts.Customers
                            _GLRows.VAT = _uow.FindObject(Of VAT)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_r("Áfahiv")))
                            _GLRows.DateVAT = dr_h("ÁfaDátum")

                            '-- Jelölőadatok beállítása
                            _UniqueObjects = _uow.FindObject(Of UniqueObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Egyedi")))
                            If _UniqueObjects IsNot Nothing Then _GLRows.UniqueObjects = _UniqueObjects
                            _JobNumberObjects = _uow.FindObject(Of JobNumberObjects)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Munkaszám")))
                            If _JobNumberObjects IsNot Nothing Then _GLRows.JobNumberObjects = _JobNumberObjects
                            _CostPlace = _uow.FindObject(Of CostPlace)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Költséghely")))
                            If _CostPlace IsNot Nothing Then _GLRows.CostPlace = _CostPlace
                            _CostHolder = _uow.FindObject(Of CostHolder)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Költségviselő")))
                            If _CostHolder IsNot Nothing Then _GLRows.CostHolder = _CostHolder
                            _Controlling = _uow.FindObject(Of Controlling)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("Name=?", dr_r("M_Controlling")))
                            If _Controlling IsNot Nothing Then _GLRows.Controlling = _Controlling

                            If dr_r("Áfahiv") <> "" Then
                                _GLRows = New GLRows(_uow)
                                _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)
                                _GLRows.VAT = _uow.FindObject(Of VAT)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("ShortName=?", dr_r("Áfahiv")))
                                _GLRows.DateVAT = dr_h("ÁfaDátum")
                                _GLRows.AmountDEV = dr_r("ÖsszegAFADeviza")
                                _GLRows.AmountHUF = dr_r("ÖsszegAFA")
                                _GLRows.ConnectInfo = dr_r("Bizonylatszám_P")
                                _GLRows.Controlling = Nothing
                                _GLRows.CostHolder = Nothing
                                _GLRows.CostPlace = Nothing
                                _GLRows.PartnerType = _GLAccounts.PartnerType
                                If _GLAccounts.PartnerType = ePartnerType.ePayabels Then
                                    _GLRows.CreditChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=?", dr_h("S_Ügyfél")))
                                    _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                                    If _GLRows.VAT IsNot Nothing Then
                                        If _GLRows.VAT.InversState = True Then
                                            _GLRows.DebitChartOfAccounts = _ChartOfAccounts_Open
                                            _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                                            If dr_h("Deviza") = "HUF" Then
                                                _GLRows.AmountDEV = 0
                                                _GLRows.AmountHUF = Math.Round(dr_r("ÖsszegNetto") * _GLRows.VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero)
                                            Else
                                                _GLRows.AmountDEV = Math.Round(dr_r("ÖsszegNettoDeviza") * _GLRows.VAT.InversKeyValue, 2, MidpointRounding.AwayFromZero)
                                                _GLRows.AmountHUF = Math.Round(dr_r("ÖsszegNettoDeviza") * Math.Round(dr_h("Árfolyam"), 2, MidpointRounding.AwayFromZero) * _GLRows.VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero)
                                            End If

                                        End If
                                    End If
                                Else
                                    _GLRows.DebitChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("AccountNumber=?", dr_h("S_Ügyfél")))
                                    _GLRows.CreditChartOfAccounts = _ChartOfAccounts_Open
                                End If

                                _GLRows.GLRowsGUID = _GUID
                                _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eVATRow
                                _GLRows.JobNumberObjects = Nothing
                                _GLRows.NoteRows = dr_r("Megjegyzés")
                                _GLRows.TransactionType = eTransactionType.eNotSet
                                _GLRows.UniqueObjects = Nothing
                                _GLRows.Customer = _GLAccounts.Customers

                            End If
                        End While
                        conn_r.Close()
                        _GLAccounts.RecalculateTotal(True)
                    End If
                End If
            End While
        End If
        conn_h.Close()


    End Sub

#Region "FileHandling"
    Public Function LoadFileFromSQL(_ID_File As Long, _ConnectionString As String) As String
        '/==========================================================================================\
        '|VERSION:         1                                                                        |
        '|AUTHOR:          SISSERVER : ivan.szabo(2010.1.15 9:5)                                    |
        '|LAST MODIFYING:  SISSERVER : ivan.szabo                                                   |
        '|DATE:            2010.1.15 9:5                                                            |
        '|----------------------------------------DESCRIPTION---------------------------------------|
        '|Az eljárás dokumantum azonosító alapján létrehoz egy filet és visszatér a teljes útvonalla|
        '|l.                                                                                        |
        '\==========================================================================================/


        Dim conn As New SqlConnection(_ConnectionString)

        Dim mSQL As String
        mSQL = "select t1.Data,t2.FileName "
        mSQL = mSQL & " from sql_Work..Dokumentum_Rows t1 join sql_Work..Dokumentum_Header t2 on"
        mSQL = mSQL & " t1.ID_Dokumentum_Header=t2.ID"
        mSQL = mSQL & " where t1.ID_Dokumentum_Header = " & _ID_File
        mSQL = mSQL & " order by t1.Idx asc"

        Dim cmd As New SqlCommand


        cmd.CommandTimeout = 60
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = mSQL

        Dim _FileName As String = ""
        Dim _StreamFile As FileStream = Nothing
        LoadFileFromSQL = ""

        Dim dr As SqlDataReader
        Dim _Buffer As String
        Dim _Record As Byte
        Dim I As Long = 0
        Dim J As Long = 0
        conn.Open()
        If conn.State = ConnectionState.Open Then
            dr = cmd.ExecuteReader(CommandBehavior.SingleResult)

            While (dr.Read)
                _Buffer = dr("Data")
                If J = 0 Then
                    _FileName = Path.GetTempPath & System.IO.Path.GetFileName(dr("FileName"))
                    If System.IO.File.Exists(_FileName) Then System.IO.File.Delete(_FileName)
                    _StreamFile = New FileStream(_FileName, FileMode.Create, FileAccess.ReadWrite)
                    J += 1
                End If
                For I = 1 To Len(_Buffer)
                    _Record = Asc(Mid(_Buffer, I, 1))
                    _StreamFile.WriteByte(_Record)
                Next
            End While
            If _StreamFile IsNot Nothing Then
                _StreamFile.Close()
            End If
        End If
        conn.Close()
        Return _FileName
    End Function
#End Region
End Module
