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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Configuration
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Utils

Public Class XLSImportsVC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        'Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        'Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    '--- XLSX file-ból történõ importálások-----------------------------------------------------
    Private Sub Import_XLS_Customers_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_Customers.Execute
        '--- Ügyfelek importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _CustomersFrom As CustomersFrom
        Dim _CountryExtend As CountryExtend
        Dim _Address As Address
        Dim _Customers As Customers

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim dr2 As OleDbDataReader
        Dim mSQL As String
        Dim _PhoneNumber As PhoneNumber
        Dim _Count As Long = 0
        Dim _UpdatedCount As Long = 0
        Dim _TempName As String
        Dim _QueryString As String


        Try
            'mSQL = "select DB=count(*)"
            mSQL = " select * from [Ügyfelek$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            If conn.State = ConnectionState.Open Then

                dr2 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                While dr2.Read
                    If dr2("Név").ToString.Trim.Length > 0 Then
                        _Count += 1
                    End If
                End While
                dr2.Close()

                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    _WaitFormClass.Initwork(1, 1, _Count)

                    If dr("Név").ToString.Trim <> "" Then

                        If dr("Ügyfél tipus") = 4 Then
                            _QueryString = dr("Név").ToString.Replace("'", "''")
                            _QueryString = dr("Név").ToString.Replace("%", "'%")
                            _QueryString = dr("Név").ToString.Replace("_", "'_")
                            _QueryString = dr("Név").ToString.Replace("\", "'\")
                            _QueryString = dr("Név").ToString.Replace("/", "'/")
                            _TempName = "ShortName Like '%" & _QueryString.ToString.Trim & "%'"
                            _CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse(_TempName))
                            '_CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("CustomerNumber=?", dr("Ügyfélszám").ToString.Trim), True)
                            If _CustomersFrom Is Nothing Then
                                _CustomersFrom = New CustomersFrom(_uow)
                                If dr("Ügyfélszám").ToString.Trim.Length > 0 Then
                                    _CustomersFrom.CustomerNumber = dr("Ügyfélszám").ToString.Trim
                                End If
                                _CustomersFrom.Name = dr("Név").ToString.Trim
                                _CustomersFrom.FullName = dr("Név").ToString.Trim

                                _CountryExtend = _uow.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", dr("Ország")), True)
                                _Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? and ZipPostal=? and Street=? and Country=?", _
                                    dr("Város"), dr("Irányítószám"), dr("Utca házszám"), TryCast(_CountryExtend, Country)))
                                If _Address Is Nothing Then _Address = New Address(_uow)
                                _Address.Country = TryCast(_CountryExtend, Country)
                                _Address.City = dr("Város").ToString.Trim
                                _Address.ZipPostal = dr("Irányítószám").ToString.Trim
                                _Address.Street = dr("Utca házszám").ToString.Trim

                                _CustomersFrom.Address1 = _Address
                                _CustomersFrom.VATNumber = dr("Adószám").ToString.Trim
                                _CustomersFrom.VATNumberEU = dr("EU adószám").ToString.Trim

                                _CustomersFrom.Email = dr("Email").ToString.Trim
                                _CustomersFrom.WebSite = dr("WEB").ToString.Trim

                                If dr("Telefon1").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon1")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon1"
                                    _PhoneNumber.Number = dr("Telefon1").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Telefon2").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon2")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon2"
                                    _PhoneNumber.Number = dr("Telefon2").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Fax").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Fax")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Fax"
                                    _PhoneNumber.Number = dr("Fax").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If

                                _CustomersFrom.Save()

                            Else
                                _Count += 1
                                If dr("Ügyfélszám").ToString.Trim.Length > 0 Then
                                    _CustomersFrom.CustomerNumber = dr("Ügyfélszám").ToString.Trim
                                End If
                                _CustomersFrom.Name = dr("Név").ToString.Trim
                                _CustomersFrom.FullName = dr("Név").ToString.Trim

                                _CountryExtend = _uow.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", dr("Ország")), True)
                                _Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? and ZipPostal=? and Street=? and Country=?", _
                                    dr("Város"), dr("Irányítószám"), dr("Utca házszám"), TryCast(_CountryExtend, Country)))
                                If _Address Is Nothing Then _Address = New Address(_uow)
                                _Address.Country = TryCast(_CountryExtend, Country)
                                _Address.City = dr("Város").ToString.Trim
                                _Address.ZipPostal = dr("Irányítószám").ToString.Trim
                                _Address.Street = dr("Utca házszám").ToString.Trim

                                _CustomersFrom.Address1 = _Address
                                _CustomersFrom.VATNumber = dr("Adószám").ToString.Trim
                                _CustomersFrom.VATNumberEU = dr("EU adószám").ToString.Trim

                                _CustomersFrom.Email = dr("Email").ToString.Trim
                                _CustomersFrom.WebSite = dr("WEB").ToString.Trim

                                If dr("Telefon1").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon1")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon1"
                                    _PhoneNumber.Number = dr("Telefon1").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Telefon2").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon2")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon2"
                                    _PhoneNumber.Number = dr("Telefon2").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Fax").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Fax")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Fax"
                                    _PhoneNumber.Number = dr("Fax").ToString.Trim
                                    _CustomersFrom.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If

                                _CustomersFrom.Save()
                            End If
                        Else
                            _QueryString = dr("Név").ToString.Replace("'", "''")
                            _QueryString = _QueryString.Replace("%", "'%")
                            '_QueryString = dr("Név").ToString.Replace("_", "'_")
                            '_QueryString = dr("Név").ToString.Replace("\", "'\")
                            '_QueryString = dr("Név").ToString.Replace("/", "'/")
                            _TempName = "FullName Like '%" & _QueryString.ToString.Trim & "%'"
                            If _TempName Like "*Aquaterv*" Then

                            End If
                            _Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse(_TempName))
                            '_Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", dr("Ügyfélszám")), True)
                            If _Customers Is Nothing Then
                                _Customers = New Customers(_uow)
                                If dr("Ügyfélszám").ToString.Trim.Length > 0 Then
                                    _Customers.CustomerNumber = dr("Ügyfélszám").ToString.Trim
                                End If
                                _Customers.Name = dr("Név").ToString.Trim
                                _Customers.FullName = dr("Név").ToString.Trim

                                _CountryExtend = _uow.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", dr("Ország")))
                                _Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? and ZipPostal=? and Street=? and Country=?", _
                                    dr("Város"), dr("Irányítószám"), dr("Utca házszám"), TryCast(_CountryExtend, Country)))
                                If _Address Is Nothing Then _Address = New Address(_uow)
                                _Address.Country = TryCast(_CountryExtend, Country)
                                _Address.City = dr("Város").ToString.Trim
                                _Address.ZipPostal = dr("Irányítószám").ToString.Trim
                                _Address.Street = dr("Utca házszám").ToString.Trim

                                _Customers.Address1 = _Address
                                _Customers.VATNumber = dr("Adószám").ToString.Trim
                                _Customers.VATNumberEU = dr("EU adószám").ToString.Trim

                                _Customers.Email = dr("Email").ToString.Trim
                                _Customers.WebSite = dr("WEB").ToString.Trim

                                If dr("Telefon1").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon1")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon1"
                                    _PhoneNumber.Number = dr("Telefon1").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Telefon2").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon2")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon2"
                                    _PhoneNumber.Number = dr("Telefon2").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Fax").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Fax")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Fax"
                                    _PhoneNumber.Number = dr("Fax").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If

                                _Customers.Save()

                            Else
                                _Count += 1
                                If dr("Ügyfélszám").ToString.Trim.Length > 0 Then
                                    _Customers.CustomerNumber = dr("Ügyfélszám").ToString.Trim
                                End If
                                _Customers.Name = dr("Név").ToString.Trim
                                _Customers.FullName = dr("Név").ToString.Trim

                                _CountryExtend = _uow.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", dr("Ország")))
                                _Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? and ZipPostal=? and Street=? and Country=?", _
                                    dr("Város"), dr("Irányítószám"), dr("Utca házszám"), TryCast(_CountryExtend, Country)))
                                If _Address Is Nothing Then _Address = New Address(_uow)
                                _Address.Country = TryCast(_CountryExtend, Country)
                                _Address.City = dr("Város").ToString.Trim
                                _Address.ZipPostal = dr("Irányítószám").ToString.Trim
                                _Address.Street = dr("Utca házszám").ToString.Trim

                                _Customers.Address1 = _Address
                                _Customers.VATNumber = dr("Adószám").ToString.Trim
                                _Customers.VATNumberEU = dr("EU adószám").ToString.Trim

                                _Customers.Email = dr("Email").ToString.Trim
                                _Customers.WebSite = dr("WEB").ToString.Trim

                                If dr("Telefon1").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon1")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon1"
                                    _PhoneNumber.Number = dr("Telefon1").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Telefon2").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon2")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Telefon2"
                                    _PhoneNumber.Number = dr("Telefon2").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If
                                If dr("Fax").ToString <> "" Then
                                    _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Fax")), True)
                                    If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                                    _PhoneNumber.PhoneType = "Fax"
                                    _PhoneNumber.Number = dr("Fax").ToString.Trim
                                    _Customers.PhoneNumbers.Add(_PhoneNumber)
                                    _PhoneNumber.Save()
                                End If

                                _Customers.Save()

                            End If
                        End If
                        _WaitFormClass.DoWork
                        _uow.CommitChanges()
                        System.Windows.Forms.Application.DoEvents()
                    Else
                        _Count -= _UpdatedCount
                        MsgBox(_Count.ToString & " darab új ügyfél importálása és " & _UpdatedCount.ToString & " darab ügyfél frissítése sikeresen bejefeződött!")
                        dr.Close()
                        Exit Try
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub Import_XLS_Contacts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_Contacts.Execute
        '--- Kapcsolatok importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)

        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _Customers As Customers
        Dim _PhoneNumber As PhoneNumber
        Dim _Contacts As Contacts

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Kapcsolatok$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    _WaitFormClass.Initwork(1, 1, 4986)
                    '_Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", dr("Ügyfélszám")), True)
                    _Contacts = _uow.FindObject(Of Contacts)(CriteriaOperator.Parse("PhoneNumbers[Number=?]", dr("Mobil")), True)

                    If _Contacts Is Nothing Then
                        _Contacts = New Contacts(_uow)
                    End If

                    '_Contacts.Customers = _Customers
                    '_Contacts.ContactNumber = dr("Ügyfélszám").ToString.Trim
                    _Contacts.FirstName = dr("Keresztnév").ToString.Trim
                    _Contacts.LastName = dr("Vezetéknév").ToString.Trim
                    _Contacts.Email = dr("Email").ToString.Trim
                    _Contacts.AccountName = dr("Account Name").ToString.Trim

                    If Not dr("Születésnap").ToString.ToUpper = "" Then
                        _Contacts.Birthday = CDate("#" & dr("Születésnap") & "#")
                    End If

                    _Contacts.Save()

                    If dr("Mobil").ToString <> "" Then
                        _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Mobil")), True)
                        If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                        _PhoneNumber.PhoneType = "Mobil"
                        _PhoneNumber.Number = dr("Mobil").ToString.Trim
                        _Contacts.PhoneNumbers.Add(_PhoneNumber)
                        _PhoneNumber.Save()
                    End If
                    If dr("Telefon1").ToString <> "" Then
                        _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon1")), True)
                        If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                        _PhoneNumber.PhoneType = "Telefon1"
                        _PhoneNumber.Number = dr("Telefon1").ToString.Trim
                        _Contacts.PhoneNumbers.Add(_PhoneNumber)
                        _PhoneNumber.Save()
                    End If
                    If dr("Telefon2").ToString <> "" Then
                        _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Telefon2")), True)
                        If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                        _PhoneNumber.PhoneType = "Telefon2"
                        _PhoneNumber.Number = dr("Telefon2").ToString.Trim
                        _Contacts.PhoneNumbers.Add(_PhoneNumber)
                        _PhoneNumber.Save()
                    End If
                    If dr("Fax").ToString <> "" Then
                        _PhoneNumber = _uow.FindObject(Of PhoneNumber)(CriteriaOperator.Parse("Number=?", dr("Fax")), True)
                        If _PhoneNumber Is Nothing Then _PhoneNumber = New PhoneNumber(_uow)
                        _PhoneNumber.PhoneType = "Fax"
                        _PhoneNumber.Number = dr("Fax").ToString.Trim
                        _Contacts.PhoneNumbers.Add(_PhoneNumber)
                        _PhoneNumber.Save()
                    End If
                    _WaitFormClass.DoWork()
                    _uow.CommitChanges()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_Products_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_Products.Execute
        '--- Termékek importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _ProductsGroups As ProductsGroups
        Dim _Products As Products
        Dim _CustomsTariffs As CustomsTariffs

        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim dr2 As OleDbDataReader
        Dim mSQL As String
        Dim _Count As Long = 0
        Dim _UpdatedCount As Long = 0


        Try

            mSQL = "select * from [Termékek$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()

            If conn.State = ConnectionState.Open Then
                dr2 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                While dr2.Read And dr2("Cikkszám").ToString <> ""
                    _Count += 1
                End While
                dr2.Close()

                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read

                    If dr("Cikkszám").ToString <> "" Then

                        _WaitFormClass.Initwork(1, 1, _Count)

                        _ProductsGroups = _uow.FindObject(Of ProductsGroups)(CriteriaOperator.Parse("ShortName=?", dr("Termékcsoport")), True)

                        _Products = _uow.FindObject(Of Products)(CriteriaOperator.Parse("ProductNumber=?", dr("Cikkszám")), True)
                        If _Products Is Nothing Then
                            _Products = New Products(_uow)
                        Else
                            _UpdatedCount += 1
                        End If

                        If _ProductsGroups Is Nothing And dr("Termékcsoport").ToString.Trim <> Nothing Then
                            _ProductsGroups = New ProductsGroups(_uow)
                            _Products.ProductGroups = _ProductsGroups
                            _ProductsGroups.ShortName = dr("Termékcsoport").ToString.Trim
                            _ProductsGroups.Description = dr("Termékcsoport").ToString.Trim '& " Termék importból létrehozva"
                        End If

                        _Products.ProductGroups = _ProductsGroups
                        _Products.ProductNumber = dr("Cikkszám").ToString.Trim
                        _Products.Description = dr("Leírás").ToString.Trim
                        _Products.Description_De = dr("LeírásNémet").ToString.Trim
                        _Products.Description_Gb = dr("LeírásAngol").ToString.Trim
                        _Products.CountryFrom = _uow.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", dr("Származási hely").ToString.Trim))
                        _Products.CurrencyName = _uow.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", dr("Devizanem").ToString.Trim))
                        _CustomsTariffs = _uow.FindObject(Of CustomsTariffs)(CriteriaOperator.Parse("CustomsTariffNumber=?", dr("VTSZ").ToString.Trim))
                        If _CustomsTariffs Is Nothing Then
                            _CustomsTariffs = New CustomsTariffs(_uow)
                            _CustomsTariffs.CustomsTariffNumber = dr("VTSZ").ToString.Trim
                            _CustomsTariffs.Description = "-"
                        End If
                        _Products.CustomsTariff = _CustomsTariffs
                        _Products.ListPriceOutDEV = dr("Lista ár (DEV)")
                        _Products.ListPriceOutHUF = dr("Lista ár (HUF)")
                        _Products.ShortName = dr("Megnevezés").ToString.Trim
                        _Products.ShortName_De = dr("MegnevezésNémet").ToString.Trim
                        _Products.ShortName_Gb = dr("MegnevezésAngol").ToString.Trim
                        _Products.VAT = _uow.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName=?", dr("ÁFA kód").ToString.Trim))
                        _Products.Units = _uow.FindObject(Of Units)(CriteriaOperator.Parse("ShortName=?", dr("Megys").ToString.Trim))
                        _Products.Parameter_01 = dr("Paraméter_01").ToString.Trim
                        _Products.Parameter_02 = dr("Paraméter_02").ToString.Trim
                        _Products.Parameter_03 = dr("Paraméter_03").ToString.Trim
                        _Products.Parameter_04 = dr("Paraméter_04").ToString.Trim
                        _Products.Parameter_05 = dr("Paraméter_05").ToString.Trim
                        _Products.Parameter_06 = dr("Paraméter_06").ToString.Trim
                        _Products.Parameter_07 = dr("Paraméter_07").ToString.Trim
                        _Products.Parameter_08 = dr("Paraméter_08").ToString.Trim
                        _Products.Parameter_09 = dr("Paraméter_09").ToString.Trim
                        _Products.Parameter_10 = dr("Paraméter_10").ToString.Trim
                        _Products.ListPriceOutHUF = Math.Round(_Products.ListPriceOutHUF, 0, MidpointRounding.AwayFromZero)
                        _Products.ListPriceOutDEV = Math.Round(_Products.ListPriceOutDEV, 2, MidpointRounding.AwayFromZero)

                        _WaitFormClass.DoWork()
                        _uow.CommitChanges()
                        System.Windows.Forms.Application.DoEvents()
                    Else
                        _Count -= _UpdatedCount
                        MsgBox(_Count.ToString & " darab új termék importálása és " & _UpdatedCount.ToString & " darab termék frissítése sikeresen bejefeződött!")
                        dr.Close()
                        Exit Try
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_ProductGroups_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_ProductGroups.Execute
        '--- Termék csoportok importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _ProductGroups As ProductsGroups

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try
            'mSQL = "select DB=count(*)"
            mSQL = "select * from [Termékcsoportok$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            _WaitFormClass.Initwork(1, 1, 100)
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read

                    _ProductGroups = _uow.FindObject(Of ProductsGroups)(CriteriaOperator.Parse("ShortName=?", dr("Megnevezés")), True)

                    If _ProductGroups Is Nothing Then _ProductGroups = New ProductsGroups(_uow)

                    _ProductGroups.ShortName = dr("Megnevezés").ToString.Trim
                    _ProductGroups.Description = dr("Leírás").ToString.Trim

                    _ProductGroups.Save()

                    _WaitFormClass.DoWork()
                    _uow.CommitChanges()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_CustomersBankAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_CustomersBankAccounts.Execute
        '--- Bankszámlák importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _CustomerBankAccounts As CustomerBankAccounts
        Dim _Customers As Customers

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Bankszámlák$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            _WaitFormClass.Initwork(1, 1, 1000)
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    _CustomerBankAccounts = _uow.FindObject(Of CustomerBankAccounts)(CriteriaOperator.Parse("AccountNumber=?", dr("Bankszámlaszám")), True)
                    _Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", dr("Ügyfélszám")), True)

                    If _CustomerBankAccounts Is Nothing Then _CustomerBankAccounts = New CustomerBankAccounts(_uow)

                    If _Customers IsNot Nothing Then _CustomerBankAccounts.Customers = _Customers

                    _CustomerBankAccounts.ShortName = dr("Bank megnevezése").ToString.Trim
                    If dr("Bankszámlaszám").ToString.Trim.Length = 16 Then
                        _CustomerBankAccounts.AccountNumber = dr("Bankszámlaszám").ToString.Trim & "00000000"
                    Else
                        _CustomerBankAccounts.AccountNumber = dr("Bankszámlaszám").ToString.Trim
                    End If
                    _CustomerBankAccounts.IBAN = dr("IBAN").ToString.Trim
                    _CustomerBankAccounts.SWIFT = dr("SWIFT").ToString.Trim

                    _CustomerBankAccounts.Save()

                    _WaitFormClass.DoWork()
                    _uow.CommitChanges()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_LiquidAssets_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_LiquidAssets.Execute
        '--- Pénzeszközök importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _LiquidAssets As LiquidAssets
        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _CurrencyName As CurrencyName

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Pénzeszközök$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            _WaitFormClass.Initwork(1, 1, 1000)
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read


                    _LiquidAssets = _uow.FindObject(Of LiquidAssets)(CriteriaOperator.Parse("ShortName=?", dr("Megnevezés")), True)

                    If _LiquidAssets Is Nothing Then _LiquidAssets = New LiquidAssets(_uow)
                    _LiquidAssets.ShortName = dr("Megnevezés").ToString.Trim

                    Select Case dr("Tipus")
                        Case 0
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.NormalCheckout
                        Case 1
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.NormalBank
                        Case 2
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.DepositCheckout
                        Case 3
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.Compensatory
                        Case 4
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.CurrencyCheckout
                        Case 5
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.SpecialTransaction
                        Case 6
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.TechnicalTransaction
                        Case 7
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.SimpleReceipt
                        Case 8
                            _LiquidAssets.LiquidAssetsType = eLiquidAssetsType.Virtual
                    End Select

                    If dr("FK számlaszám").ToString <> "" Then
                        _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", dr("FK számlaszám")), True)
                        If _ChartOfAccounts Is Nothing Then _ChartOfAccounts = New ChartOfAccounts(_uow)
                        _ChartOfAccounts.AccountNumber = dr("FK számlaszám").ToString.Trim
                        '_LiquidAssets.ChartOfAccounts = _ChartOfAccounts
                        _ChartOfAccounts.Save()
                    End If
                    If dr("Deviza").ToString <> "" Then
                        _CurrencyName = _uow.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", dr("Deviza")), True)
                        If _CurrencyName Is Nothing Then _CurrencyName = New CurrencyName(_uow)
                        _CurrencyName.ShortName = dr("Deviza").ToString.Trim
                        _LiquidAssets.CurrencyName = _CurrencyName
                        _CurrencyName.Save()
                    End If

                    _LiquidAssets.Save()

                    _WaitFormClass.DoWork()
                    _uow.CommitChanges()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_ChartsOfAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_ChartsOfAccounts.Execute
        '--- Számlatükrök importálása XLSX file-ból----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _ChartOfAccounts_Parent As ChartOfAccounts

        Dim _AccountNumber As String
        Dim _AccountNumber_Parent As String

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Számlatükör$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            _WaitFormClass.Initwork(1, 1, 1000)
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    _
                    _AccountNumber = dr("Számlaszám")
                    _AccountNumber_Parent = Mid(_AccountNumber, 1, Len(_AccountNumber) - 1)

                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber), True)

                    _ChartOfAccounts_Parent = Nothing
                    If _AccountNumber_Parent <> "" Then _ChartOfAccounts_Parent = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber_Parent), True)

                    If _ChartOfAccounts Is Nothing Then _ChartOfAccounts = New ChartOfAccounts(_uow)
                    _ChartOfAccounts.AccountNumber = dr("Számlaszám").ToString.Trim
                    _ChartOfAccounts.Name = dr("Számlaszám").ToString.Trim & " " & dr("Megnevezés").ToString.Trim
                    _ChartOfAccounts.Parent = _ChartOfAccounts_Parent
                    _WaitFormClass.DoWork()
                    _uow.CommitChanges()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_Contracts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_Contracts.Execute
        '--- Mennyiségi egységek importálása XLSX file-ból----------------------------------------------
        Dim _Ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _uow As UnitOfWork = New UnitOfWork(_Ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _ContractTemplate As ContractTemplate

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Szerzõdések$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    _WaitFormClass.Initwork(1, 1, 10)

                    Dim _CustomersFrom As CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("CustomerNumber=?", dr("Vállalat").ToString.Trim))
                    Dim _Customers As Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", dr("Szállító szám").ToString.Trim))
                    Dim _Contracts As Contracts = _uow.FindObject(Of Contracts)(CriteriaOperator.Parse("ShortName=? AND Customers=? AND CustomersFrom=?", dr("Megnevezés").ToString.Trim, _Customers, _CustomersFrom))
                    If _Customers IsNot Nothing And _CustomersFrom IsNot Nothing Then
                        'szerzõdési sablon meghatározása a szerzõdés típus szerint
                        If dr("Szerzõdés típus").ToString.Trim = "OS" Then
                            _ContractTemplate = _uow.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("CustomersFrom=? And PartnerType=1 And ShortName LIKE '%szállítói%'", _CustomersFrom))
                        Else
                            _ContractTemplate = _uow.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("CustomersFrom=? And PartnerType=0", _CustomersFrom))
                        End If
                        'Ha a szerzõdés még nem létezik akkor létrehozzuk
                        If _Contracts Is Nothing Then
                            Dim _Contracts_New As Contracts = New Contracts(_uow)
                            _Contracts_New.ContractTemplate = _ContractTemplate
                            _Contracts_New.State = State.Signed
                            _Contracts_New.ShortName = dr("Megnevezés").ToString.Trim
                            _Contracts_New.CustomersFrom = _CustomersFrom
                            _Contracts_New.Customers = _Customers
                            If Not dr("Szerzõdéskötés dátuma").ToString.Trim = "" Then
                                Try
                                    _Contracts_New.DateCreated = CType(CDate("#" & dr("Szerzõdéskötés dátuma").ToString.Trim & "#"), Date)
                                Catch

                                End Try
                            End If
                            If Not dr("Szerzõdés lejárat").ToString.Trim = "" Then
                                Try
                                    _Contracts_New.DateExpectClose = CType(CDate("#" & dr("Szerzõdés lejárat").ToString.Trim & "#"), Date)
                                Catch

                                End Try
                            End If
                            _ContractTemplate.NumberGenerator.Reload()
                            _Contracts_New.Save()
                            _uow.CommitChanges()
                        End If
                    End If
                    _WaitFormClass.DoWork()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_GLAccounts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_GLAccounts.Execute
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        Dim _Ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_Ocur.Session.DataLayer)
        Dim _cs As String
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)
        Dim _MissingCustomers As String = ""

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim mSQL As String


        Try

            mSQL = "select * from [Folyószámla nyitó$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()
            _WaitFormClass.Initwork(1, 1, 1000)
            If conn.State = ConnectionState.Open Then
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read

                    Dim _GLAccounts As GLAccounts = New GLAccounts(_uow)
                    Dim _GLHeader As GLHeader = TryCast(_GLAccounts, GLHeader)
                    Dim _GLRows As GLRows = New GLRows(_uow)
                    Dim _CustomersFrom As CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("CustomerNumber=?", dr("Sajátcég").ToString.Trim))
                    Dim _Customers As Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", dr("Partner sorszáma").ToString.Trim))
                    Dim _ChartOfAccounts As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491'"))
                    Dim _PaymentOption As PaymentOption = _uow.FindObject(Of PaymentOption)(CriteriaOperator.Parse("PayMode = 1"))
                    Dim _CurrencyName As CurrencyName = _uow.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", dr("Deviza").ToString.Trim))
                    Dim _PartnerType As ePartnerType = dr("Partner típusa").ToString.Trim
                    Dim _ExistingGLRows As GLRows = _uow.FindObject(Of GLRows)(CriteriaOperator.Parse("GLHeader.CustomersFrom=? AND Customer=? AND PartnerType=? AND GLHeader.DocumentNumber=?", _CustomersFrom, _Customers, _PartnerType, dr("Pontozási Információ").ToString.Trim))

                    If _Customers IsNot Nothing And _CustomersFrom IsNot Nothing And _ExistingGLRows Is Nothing Then

                        _GLAccounts.CustomersFrom = _CustomersFrom
                        _GLAccounts.Customers = _Customers
                        _GLAccounts.row_ConnectInfo = dr("Pontozási információ").ToString.Trim
                        _GLAccounts.row_AmountHUF = dr("Egyenleg HUF").ToString.Trim
                        _GLAccounts.row_AmountDEV = dr("Egyenleg DEV").ToString.Trim
                        _GLAccounts.DateCreated = CType(CDate("#" & dr("Kiállítás dátuma").ToString.Trim & "#"), Date)
                        _GLAccounts.DateExecution = CType(CDate("#" & dr("Teljesítés dátuma").ToString.Trim & "#"), Date)
                        _GLAccounts.DatePayment = CType(CDate("#" & dr("Fizetési határidõ").ToString.Trim & "#"), Date)
                        _GLAccounts.PaymentOption = _PaymentOption
                        _GLAccounts.CurrencyName = _CurrencyName

                        If _PartnerType = ePartnerType.eReceivables Then
                            _GLAccounts.PartnerType = ePartnerType.eReceivables
                        ElseIf _PartnerType = ePartnerType.ePayabels Then
                            _GLAccounts.PartnerType = ePartnerType.ePayabels
                        End If
                        _GLHeader = TryCast(_GLAccounts, GLHeader)
                        _GLHeader.Save()

                        ' -- Könyvelési sor hozzáadása
                        ' -- PartnerType vizsgálata majd ez alapján a fõkönyvi tartozik/kövezel beállítása ------------------------

                        Dim _NettoHUF As Double
                        Dim _BruttoHUF As Double
                        Dim _VATHUF As Double
                        Dim _BruttoDEV As Double
                        Dim _GUID As Guid = Guid.NewGuid

                        If _GLAccounts.PartnerType = ePartnerType.eReceivables Then
                            _GLAccounts.PartnerType = ePartnerType.eReceivables
                            _GLHeader.row_CreditChartOfAccounts = _ChartOfAccounts
                            '_GLHeader.row_DebitChartOfAccounts = _Customers.CustomersGLParameters.ChartOfAccountsOut
                        Else
                            _GLAccounts.PartnerType = ePartnerType.ePayabels
                            '_GLHeader.row_CreditChartOfAccounts = _Customers.CustomersGLParameters.ChartOfAccountsIn
                            _GLHeader.row_DebitChartOfAccounts = _ChartOfAccounts
                        End If
                        ' -- Deviza vizsgálat, ha nem Forint akkor átváltja ---------------------------------------------------------
                        If _GLHeader.CurrencyName.ShortName = "HUF" Then
                            _BruttoHUF = _GLHeader.row_AmountHUF
                            _BruttoDEV = 0
                        Else
                            _BruttoDEV = _GLHeader.row_AmountDEV
                            _BruttoHUF = Math.Round(_GLHeader.row_AmountDEV * _GLHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)
                            _GLHeader.row_AmountHUF = _BruttoHUF
                        End If

                        ' -- ÁFA vizsgálata --------------------------------------------------------------------------------------------
                        ' -- Létrehoz egy sort a nettó összegre ------------------------------------------------------------------------------------------

                        _GLRows = New GLRows(_uow)
                        _GLRows.PartnerType = _GLAccounts.PartnerType
                        If _GLRows.PartnerType = ePartnerType.eReceivables Then
                            _GLRows.DebitAmountHUF = _GLHeader.row_AmountHUF
                            _GLRows.DebitAmountDEV = _GLHeader.row_AmountDEV
                        Else
                            _GLRows.CreditAmountHUF = _GLHeader.row_AmountHUF
                            _GLRows.CreditAmountDEV = _GLHeader.row_AmountDEV
                        End If
                        _GLRows.AddGLRows(_GLHeader, _GLHeader.row_DebitChartOfAccounts, _GLHeader.row_CreditChartOfAccounts, _
                                          _GLHeader.row_AmountDEV, _GLHeader.row_AmountHUF, _GLHeader.row_LiquidAssets, _
                                          _Customers, _GLHeader.DateExecution, Nothing, _
                                          _GLHeader.row_NoteRows, _GLHeader.row_UniqueObjects, _
                                          _GLHeader.row_Controlling, _GLHeader.row_JobNumberObjects, _GLHeader.row_CostPlace, _
                                          _GLHeader.row_CostHolder, _GLHeader.row_ConnectInfo, eGLRowsTypeVAT.eNettoRow, _GUID, _
                                          eTransactionType.eNotSet, eCashType.eNormal, _GLAccounts.PartnerType)

                        ' -- Véglegesítés -----
                        _GLAccounts.IsEditable = False
                        _GLAccounts.RecalculateTotal()
                        _GLAccounts.Save()
                        _GLHeader.Save()
                        _uow.CommitChanges()
                        ' -- Véglegesítés vége -----
                        ' -- Könyvelési sor hozzáadása vége
                    End If
                    _WaitFormClass.DoWork()
                    System.Windows.Forms.Application.DoEvents()
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub Import_XLS_Services_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_Services.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _Services As Services
        Dim _XLSImports As XLSImports = TryCast(View.SelectedObjects(0), XLSImports)

        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=Excels\XLSImports.xlsx;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim dr2 As OleDbDataReader
        Dim mSQL As String
        Dim _Count As Long = 0
        Dim _UpdatedCount As Long = 0


        Try

            mSQL = "select * from [Szolgáltatások$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()

            If conn.State = ConnectionState.Open Then
                dr2 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                While dr2.Read And dr2("Megnevezés").ToString.Length > 0
                    _Count += 1
                End While
                dr2.Close()
                _WaitFormClass.Initwork(1, 1, _Count)
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    If dr("Megnevezés").ToString <> "" Then

                        _Services = _uow.FindObject(Of Services)(CriteriaOperator.Parse("ShortName=? and Description=? and ListPriceHUF=? and ListPriceDEV=?", dr("Megnevezés").ToString.Trim, dr("Leírás").ToString.Trim, dr("Lista ár (HUF)").ToString.Trim, dr("Lista ár (DEV)").ToString.Trim), True)

                        If _Services Is Nothing Then
                            _Services = New Services(_uow)
                        Else
                            _UpdatedCount += 1
                        End If

                        _Services.Description = dr("Leírás").ToString.Trim
                        _Services.ListPriceDEV = dr("Lista ár (DEV)")
                        _Services.ListPriceHUF = dr("Lista ár (HUF)")
                        _Services.RowGroup1 = dr("Csoport 1").ToString.Trim
                        _Services.RowGroup2 = dr("Csoport 2").ToString.Trim
                        _Services.ShortName = dr("Megnevezés").ToString.Trim
                        _Services.Units = _uow.FindObject(Of Units)(CriteriaOperator.Parse("ShortName=?", dr("ME").ToString.Trim))
                        _Services.VAT = _uow.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName=?", dr("ÁFA kód").ToString.Trim))

                        If dr("Recept sorszáma").ToString.Trim <> "" Then
                            _Services.Recipe = _uow.FindObject(Of Recipe)(CriteriaOperator.Parse("DocumentNumber=?", dr("Recept sorszáma").ToString.Trim))
                        End If

                        If dr("VTSZ").ToString.Trim <> "" Then
                            _Services.CustomsTariffs = _uow.FindObject(Of CustomsTariffs)(CriteriaOperator.Parse("CustomsTariffNumber=?", dr("VTSZ").ToString.Trim))
                        End If

                        _Services.Save()
                        _WaitFormClass.DoWork()
                        _uow.CommitChanges()
                        System.Windows.Forms.Application.DoEvents()

                    Else
                        _Count -= _UpdatedCount
                        MsgBox(_Count.ToString & " darab új szolgáltatás importálása és " & _UpdatedCount.ToString & " darab szolgáltatás frissítése sikeresen bejefeződött!")
                        dr.Close()
                        Exit Try
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_XLS_KSZ_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles Import_XLS_KSZ.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)


        Dim _cs As String
        _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
        _cs += "Data Source=c:\data\fetools\R20110101.xls;"
        _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

        Dim conn As New OleDbConnection(_cs)
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim dr2 As OleDbDataReader
        Dim mSQL As String
        Dim _Count As Long = 0
        Dim _UpdatedCount As Long = 0
        Dim _CustomerNumber As String

        Dim _CustomersFrom As CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("Oid=Oid"))

        If _CustomersFrom Is Nothing Then Exit Sub

        Dim connstr As String
        connstr = " User ID=sa; "
        connstr &= " Password=pcl718;"
        connstr &= " Pooling=false;"
        connstr &= " Data Source=192.168.1.143,1777;"
        connstr &= " Initial Catalog=sql_Cassa"

        Dim conn_sql As New SqlConnection(connstr)
        Dim cmd_sql As New SqlCommand
        Dim dr_sql As SqlDataReader

        'P40001038-11
        Import_FN(_uow, "P40001038-11", _CustomersFrom, connstr)
        _uow.CommitChanges()

        Try

            mSQL = "select * from [Munkafüzet$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()

            If conn.State = ConnectionState.Open Then
                dr2 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                While dr2.Read
                    If dr2(2).ToString.Length > 0 Then
                        _Count += 1
                    End If
                End While
                dr2.Close()
                _WaitFormClass.Initwork(1, 1, _Count)
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                While dr.Read
                    '_CustomerNumber=dr(
                    If dr(1) IsNot Nothing Then
                        If dr(1).ToString <> "" And Mid(dr(1).ToString, 1, 1) = "F" Then


                            If Val(Mid(dr(1), 3, 20)) > 0 Then
                                Import_FN_1(Val(Mid(dr(1).ToString, 3, 20)), _uow, _CustomersFrom, connstr)
                                _uow.CommitChanges()
                            End If
                            _WaitFormClass.DoWork()

                            System.Windows.Forms.Application.DoEvents()
                        ElseIf dr(2).ToString <> "" And Mid(dr(1).ToString, 1, 1) = "P" Then


                            Import_FN(_uow, dr(4), _CustomersFrom, connstr)

                            _WaitFormClass.DoWork()
                            _uow.CommitChanges()
                            System.Windows.Forms.Application.DoEvents()
                        End If
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _WaitFormClass.Finalwork()
    End Sub
    Private Sub Import_PH_Immovables_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles Import_PH_Immovables.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Import_PH_Immovables_PopUp As New Import_PH_Immovables_PopUp(_onew.Session)

            e.View = Application.CreateDetailView(_onew, "Import_PH_Immovables_PopUp_DetailView", True, _Import_PH_Immovables_PopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub Import_PH_Immovables_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles Import_PH_Immovables.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Import_PH_Immovables_PopUp As Import_PH_Immovables_PopUp = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), Import_PH_Immovables_PopUp))
            If _Import_PH_Immovables_PopUp Is Nothing Then Throw New Exception("*Váratlan hiba történt a folyamat megszakadt!")
            If String.IsNullOrEmpty(_Import_PH_Immovables_PopUp.FileName) Then Throw New Exception("*Nem lett kiválasztva egyetlen file sem, melyből importálni lehetne!")
            If Not System.IO.File.Exists(_Import_PH_Immovables_PopUp.FileName) Then Throw New Exception("*A kiválsztott file nem létezik!")

            Dim _cs As String
            _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
            _cs += "Data Source=" & _Import_PH_Immovables_PopUp.FileName & ";"
            _cs += "Extended Properties=""Excel 12.0;HDR=YES;"""
            Dim conn As New OleDbConnection(_cs)
            Dim cmd As New OleDbCommand
            Dim dr As OleDbDataReader
            Dim mSQL As String
            Dim _UpdatedCount As Long = 0

            Dim _WorksheetName As String = ""
            Select Case _Import_PH_Immovables_PopUp.ImportPHType
                Case eImportPHType.eApartments
                    _WorksheetName = "Lakas"
                Case eImportPHType.eContainers
                    _WorksheetName = "Tarolo"
                Case eImportPHType.eGarages
                    _WorksheetName = "Garazs"
                Case eImportPHType.eOffices
                    _WorksheetName = "Uzlet"
                Case eImportPHType.eInvoiceRows
                    _WorksheetName = "Szerzodeses sorok"
                    If _Import_PH_Immovables_PopUp.Customers Is Nothing Then Throw New Exception("*Nincs beállítva ügyfél!")
                    If _Import_PH_Immovables_PopUp.CustomersFrom Is Nothing Then Throw New Exception("*Nincs beállítva saját cég!")
            End Select

            mSQL = "select * from [" + _WorksheetName + "$]"
            cmd.CommandTimeout = 60
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = mSQL
            conn.Open()

            If conn.State = ConnectionState.Open Then
                Select Case _Import_PH_Immovables_PopUp.ImportPHType
                    Case eImportPHType.eApartments
                        dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                        While dr.Read
                            Dim _UniqueObjectString As String = dr(0)
                            Dim _UniqueObjectStringArray As String() = _UniqueObjectString.Split((New [Char]() {"-"}))
                            Dim _UniqueObjects As UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjectString))
                            If _UniqueObjects Is Nothing Then
                                Dim _ImmovablesGroupsString As String = _UniqueObjectStringArray(0)
                                If _UniqueObjectStringArray.Length > 3 Then
                                    For _ik = 1 To _UniqueObjectStringArray.Length - 3
                                        _ImmovablesGroupsString += "-" + _UniqueObjectStringArray(_ik)
                                    Next
                                End If

                                Dim _ImmovablesGroups As ImmovablesGroups = _onew.FindObject(Of ImmovablesGroups)(CriteriaOperator.Parse("UniqueTag=?", _ImmovablesGroupsString))
                                Dim _Stairways As Stairways = _onew.FindObject(Of Stairways)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 2)))
                                Dim _Floors As Floors = _onew.FindObject(Of Floors)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _
                                                                                                           _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Remove(2, (_UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Length - 2))))
                                _UniqueObjects = New UniqueObjects(_onew.Session)
                                With (_UniqueObjects)
                                    .Name = _UniqueObjectString
                                End With

                                Dim _Apartments As New Apartments(_onew.Session)
                                With _Apartments
                                    .UniqueObjects = _UniqueObjects
                                    Select Case dr(1).ToString
                                        Case "Eladva"
                                            .ImmovableSoldState = eImmovableSoldState.eSold
                                            .CanForRent = False
                                            .CanForSale = False
                                        Case "Érdeklődő"

                                        Case Else
                                    End Select
                                    .ImmovableGroups = _ImmovablesGroups
                                    .Stairway = _Stairways
                                    .Floor = _Floors
                                    .UniqueTag = "*" 'Nincs UniqueObject generálás
                                    .ImmovablesTypes = eImmovablesTypes.eApartment
                                    If Not String.IsNullOrEmpty(dr(4).ToString) Then .RoomString = dr(4).ToString
                                    If Not String.IsNullOrEmpty(dr(5).ToString) Then .AreaNetto = Convert.ToDouble(dr(5).ToString)
                                    If Not String.IsNullOrEmpty(dr(6).ToString) Then .AreaNettoUnder190 = Convert.ToDouble(dr(6).ToString)
                                    If Not String.IsNullOrEmpty(dr(7).ToString) Then .AreaTerrace = Convert.ToDouble(dr(7).ToString)
                                    If .AreaTerrace > 0 Then .IsTerrace = True
                                    If Not String.IsNullOrEmpty(dr(9).ToString) Then .AreaNettoForSale = Convert.ToDouble(dr(9).ToString)
                                    If Not String.IsNullOrEmpty(dr(11).ToString) Then .PriceNettoHUF = Convert.ToDouble(dr(11).ToString)
                                End With
                            End If
                        End While
                    Case eImportPHType.eContainers
                        dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                        While dr.Read
                            Dim _UniqueObjectString As String = dr(0)
                            Dim _UniqueObjectStringArray As String() = _UniqueObjectString.Split((New [Char]() {"-"}))
                            Dim _UniqueObjects As UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjectString))
                            If _UniqueObjects Is Nothing Then
                                Dim _ImmovablesGroupsString As String = _UniqueObjectStringArray(0)
                                If _UniqueObjectStringArray.Length > 2 Then
                                    For _ik = 1 To _UniqueObjectStringArray.Length - 2
                                        _ImmovablesGroupsString += "-" + _UniqueObjectStringArray(_ik)
                                    Next
                                End If

                                Dim _ImmovablesGroups As ImmovablesGroups = _onew.FindObject(Of ImmovablesGroups)(CriteriaOperator.Parse("UniqueTag=?", _ImmovablesGroupsString))
                                Dim _Floors As Floors = _onew.FindObject(Of Floors)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _
                                                                                                           _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Remove(1, (_UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Length - 1))))
                                _UniqueObjects = New UniqueObjects(_onew.Session)
                                With (_UniqueObjects)
                                    .Name = _UniqueObjectString
                                End With

                                Dim _Container As New Container(_onew.Session)
                                With _Container
                                    .UniqueObjects = _UniqueObjects
                                    .ImmovableGroups = _ImmovablesGroups
                                    .Floor = _Floors
                                    .ImmovablesTypes = eImmovablesTypes.eContainer
                                    .UniqueTag = "*" 'Nincs UniqueObject generálás
                                    Select Case dr(1).ToString
                                        Case "Eladva"
                                            .ImmovableSoldState = eImmovableSoldState.eSold
                                        Case "Érdeklődő"

                                        Case Else
                                    End Select

                                    'If Not String.IsNullOrEmpty(dr(2).ToString) Then .UniqueTag = (dr(2).ToString).Remove(0, 1)
                                    If Not String.IsNullOrEmpty(dr(3).ToString) Then .PriceNettoHUF = Convert.ToDouble(dr(3).ToString)
                                End With
                            End If
                        End While
                    Case eImportPHType.eGarages
                        dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                        While dr.Read
                            Dim _UniqueObjectString As String = dr(0)
                            Dim _UniqueObjectStringArray As String() = _UniqueObjectString.Split((New [Char]() {"-"}))
                            Dim _UniqueObjects As UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjectString))
                            If _UniqueObjects Is Nothing Then
                                Dim _ImmovablesGroupsString As String = _UniqueObjectStringArray(0)
                                If _UniqueObjectStringArray.Length > 2 Then
                                    For _ik = 1 To _UniqueObjectStringArray.Length - 2
                                        _ImmovablesGroupsString += "-" + _UniqueObjectStringArray(_ik)
                                    Next
                                End If

                                Dim _ImmovablesGroups As ImmovablesGroups = _onew.FindObject(Of ImmovablesGroups)(CriteriaOperator.Parse("UniqueTag=?", _ImmovablesGroupsString))
                                Dim _Floors As Floors = _onew.FindObject(Of Floors)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _
                                                                                                           _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Remove(1, (_UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Length - 1))))
                                _UniqueObjects = New UniqueObjects(_onew.Session)
                                With (_UniqueObjects)
                                    .Name = _UniqueObjectString
                                End With

                                Dim _Garages As New Garages(_onew.Session)
                                With _Garages
                                    .UniqueObjects = _UniqueObjects
                                    .ImmovableGroups = _ImmovablesGroups
                                    .Floor = _Floors
                                    .ImmovablesTypes = eImmovablesTypes.eGarage
                                    .UniqueTag = "*" 'Nincs UniqueObject generálás
                                    Select Case dr(1).ToString
                                        Case "Eladva"
                                            .ImmovableSoldState = eImmovableSoldState.eSold
                                        Case "Érdeklődő"

                                        Case Else
                                    End Select
                                    ' If Not String.IsNullOrEmpty(dr(2).ToString) Then .UniqueTag = (dr(2).ToString).Remove(0, 1)
                                    If Not String.IsNullOrEmpty(dr(3).ToString) Then .PriceNettoHUF = Convert.ToDouble(dr(3).ToString)
                                End With
                            End If
                        End While
                    Case eImportPHType.eOffices
                        dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                        While dr.Read
                            Dim _UniqueObjectString As String = dr(0)
                            Dim _UniqueObjectStringArray As String() = _UniqueObjectString.Split((New [Char]() {"-"}))
                            Dim _UniqueObjects As UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjectString))
                            If _UniqueObjects Is Nothing Then
                                Dim _ImmovablesGroupsString As String = _UniqueObjectStringArray(0)
                                If _UniqueObjectStringArray.Length > 3 Then
                                    For _ik = 1 To _UniqueObjectStringArray.Length - 3
                                        _ImmovablesGroupsString += "-" + _UniqueObjectStringArray(_ik)
                                    Next
                                End If

                                Dim _ImmovablesGroups As ImmovablesGroups = _onew.FindObject(Of ImmovablesGroups)(CriteriaOperator.Parse("UniqueTag=?", _ImmovablesGroupsString))
                                Dim _Stairways As Stairways = _onew.FindObject(Of Stairways)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 2)))
                                Dim _Floors As Floors = _onew.FindObject(Of Floors)(CriteriaOperator.Parse("ImmovablesGroups=? AND UniqueTag=?", _ImmovablesGroups, _
                                                                                                           _UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Remove(1, (_UniqueObjectStringArray(_UniqueObjectStringArray.Length - 1).Length - 1))))
                                _UniqueObjects = New UniqueObjects(_onew.Session)
                                With (_UniqueObjects)
                                    .Name = _UniqueObjectString
                                End With

                                Dim _Offices As New Offices(_onew.Session)
                                With _Offices
                                    .UniqueObjects = _UniqueObjects
                                    .ImmovableGroups = _ImmovablesGroups
                                    .Stairway = _Stairways
                                    .Floor = _Floors
                                    .ImmovablesTypes = eImmovablesTypes.eOffice
                                    .UniqueTag = "*" 'Nincs UniqueObject generálás
                                    Select Case dr(1).ToString
                                        Case "Eladva"
                                            .ImmovableSoldState = eImmovableSoldState.eSold
                                        Case "Érdeklődő"

                                        Case Else
                                    End Select
                                    'If Not String.IsNullOrEmpty(dr(2).ToString) Then .UniqueTag = (dr(2).ToString).Remove(0, 1)
                                    If Not String.IsNullOrEmpty(dr(3).ToString) Then .AreaNettoForSale = Convert.ToDouble(dr(3).ToString)
                                    If Not String.IsNullOrEmpty(dr(4).ToString) Then .AreaPriceNettoHUF = Convert.ToDouble(dr(4).ToString)
                                    If Not String.IsNullOrEmpty(dr(5).ToString) Then .PriceNettoHUF = Convert.ToDouble(dr(5).ToString)
                                End With
                            End If
                        End While
                    Case eImportPHType.eInvoiceRows
                        Dim _Contracts As Contracts = _onew.FindObject(Of Contracts)(CriteriaOperator.Parse("Customers=? AND CustomersFrom=?", _
                                                                                _onew.GetObject(_Import_PH_Immovables_PopUp.Customers), _onew.GetObject(_Import_PH_Immovables_PopUp.CustomersFrom)))

                        If _Contracts Is Nothing Then
                            _Contracts = New Contracts(_onew.Session)
                            With _Contracts
                                .Customers = _onew.GetObject(_Import_PH_Immovables_PopUp.Customers)
                                .CustomersFrom = _onew.GetObject(_Import_PH_Immovables_PopUp.CustomersFrom)
                                .CurrencyName = _onew.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
                                .ShortName = System.IO.Path.GetFileName(_Import_PH_Immovables_PopUp.FileName)
                            End With
                        End If

                        dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                        While dr.Read
                            Dim _UniqueObjectString As String = dr(0)
                            Dim _UniqueObjectStringArray As String() = _UniqueObjectString.Split((New [Char]() {"-"}))
                            Dim _UniqueObjects As UniqueObjects = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjectString))
                            If _UniqueObjects IsNot Nothing Then
                                Dim _Immovables As Immovables = _onew.FindObject(Of Apartments)(CriteriaOperator.Parse("UniqueObjects=?", _UniqueObjects))
                                If _Immovables IsNot Nothing Then
                                    Dim _ContractRows As New ContractRows(_onew.Session)
                                    With _ContractRows
                                        .Contracts = _Contracts


                                    End With

                                    Select Case _Immovables.ImmovablesTypes
                                        Case eImmovablesTypes.eApartment

                                        Case eImmovablesTypes.eContainer
                                        Case eImmovablesTypes.eGarage
                                        Case eImmovablesTypes.eOffice
                                    End Select
                                End If

                            End If
                        End While

                End Select
                dr.Close()
            End If
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aOpenFileDialog_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOpenFileDialog.Execute
        Try
            Dim _OpenFileDialog As New System.Windows.Forms.OpenFileDialog
            _OpenFileDialog.Filter = "Xls Files (.xls)|*.xls|Xlsx Files (.xlsx)|*.xlsx|All Files (*.*)|*.*"
            Dim _Import_PH_Immovables_PopUp As Import_PH_Immovables_PopUp = TryCast(View.SelectedObjects(0), Import_PH_Immovables_PopUp)
            If _Import_PH_Immovables_PopUp Is Nothing Then Throw New Exception("*Váratlan hiba történt a folyamat megszakadt!")

            If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                _Import_PH_Immovables_PopUp.FileName = _OpenFileDialog.FileName
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
