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 System.Windows.Forms
Imports System.Data.OleDb

Public Class GIROInfo_VC
    Inherits DevExpress.ExpressApp.ViewController

    Dim _WaitFormClass As New WaitFormClass

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Private Sub aGIROInfo_ImportXLS_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGIROInfo_ImportXLS.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _cs As String = ""
        Dim _GIROID_Array As New ArrayList
        Dim _ShortName_Array As New ArrayList
        Dim _ZIPCode_Array As New ArrayList
        Dim _Address_Array As New ArrayList

        Dim _OpenFileDialog As New OpenFileDialog
        _OpenFileDialog.Filter = "xls Files (.xls)|*.xls|xlsx Files (.xlsx)|*.xlsx|All Files (*.*)|*.*"
        _OpenFileDialog.FilterIndex = 1
        If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK And (Not String.IsNullOrEmpty(_OpenFileDialog.FileName)) Then
            Dim _xlsFile As String = _OpenFileDialog.FileName
            _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
            _cs += "Data Source=" & _xlsFile.ToString & ";"
            _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
            Dim _UpdatedCount As Long = 0

            Try
                mSQL = "select * from [" + IO.Path.GetFileNameWithoutExtension(_xlsFile) + "$]" '[Munka1$]"
                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
                        If Not String.IsNullOrEmpty(dr(0).ToString) Then

                            _GIROID_Array.Add(dr(0).ToString) 'ID
                            _ShortName_Array.Add(dr(1).ToString) 'shortname
                            _ZIPCode_Array.Add(dr(2).ToString) 'zipcode
                            _Address_Array.Add(dr(3).ToString) 'adddress
                        End If
                    End While

                    dr.Close()
                End If
                conn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
                conn.Close()
            End Try
        End If
        _WaitFormClass.InitWork(1, 1, _GIROID_Array.Count)
        Dim _ActIndex As Long = 0
        For Each _ID As String In _GIROID_Array
            _WaitFormClass.DoWork
            Dim _GIROInfo As GIROInfo = _ocur.FindObject(Of GIROInfo)(DevExpress.Data.Filtering.CriteriaOperator.Parse("GIROID=?", _GIROID_Array(_ActIndex)))
            If _GIROInfo Is Nothing Then
                _GIROInfo = New GIROInfo(_ocur.Session)
                With _GIROInfo
                    _GIROInfo.GIROID = _GIROID_Array(_ActIndex)
                    _GIROInfo.ShortName = _ShortName_Array(_ActIndex)
                    _GIROInfo.Address = _ZIPCode_Array(_ActIndex) + " " + _Address_Array(_ActIndex)
                End With
            Else
                With _GIROInfo
                    _GIROInfo.ShortName = _ShortName_Array(_ActIndex)
                    _GIROInfo.Address = _ZIPCode_Array(_ActIndex) + " " + _Address_Array(_ActIndex)
                End With
            End If
            _ActIndex += 1
        Next
        _ocur.CommitChanges()
        _Waitformclass.FinalWork
    End Sub

    Private Sub aGIROInfo_ImportCSV_Execute(sender As Object, e As SimpleActionExecuteEventArgs)

    End Sub
End Class
