Imports System.Data.SqlClient
Imports System.Reflection

Public Class cWarenorte

    Property wo_id As Integer
    Property wo_warenort As Object = Nothing
    Property wo_bezeichnung As Object = Nothing
    Property wo_firma As Object = Nothing
    Property wo_aktiv As Boolean
    Property wo_reihenfolge As Object = Nothing
    Property wo_ort As Object = Nothing
    Property wo_cluster As Object = Nothing
    Property wo_grenzstelle As Object = Nothing
    Property wo_knnr As Object = Nothing
    Property wo_strasse As Object = Nothing
    Property wo_ex As Boolean
    Property wo_im As Boolean
    Property wo_zv As Boolean
    Property wo_ze As Boolean

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(wo_id)
        Me.wo_id = wo_id
        LOAD()
    End Sub

    Sub New(wo_warenort, wo_firma)
        Me.wo_warenort = wo_warenort
        Me.wo_firma = wo_firma
        LOAD()
    End Sub


    Sub New()
        LOADALL()
    End Sub



    Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
            Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_id", wo_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_warenort", wo_warenort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_bezeichnung", wo_bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_firma", wo_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_aktiv", wo_aktiv))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_reihenfolge", wo_reihenfolge))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_ort", wo_ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_cluster", wo_cluster))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_grenzstelle", wo_grenzstelle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_knnr", wo_knnr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_strasse", wo_strasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_ex", wo_ex))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_im", wo_im))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_zv", wo_zv))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wo_ze", wo_ze))



        Return list
        End Function



        Public Function SAVE() As Boolean
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM [tblWarenorte] WHERE wo_id=@wo_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "AVISO", , list)
    End Function

    Public Sub LOAD()

        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("Select * From [tblWarenorte] Where wo_id =@wo_id", conn)
                    cmd.Parameters.AddWithValue("@wo_id", wo_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If

                        Next
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Public Sub LOADALL()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM [tblWarenorte] ", conn)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If

                        Next
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub



    Public Function getUpdateCmd() As String
            Try
                Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

                Dim str As String = ""
                For Each i In list
                    If Not i.isPrimaryParam Then
                        str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                    End If
                Next
                str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblWarenorte] SET " & str & " WHERE wo_id=@wo_id ")

        Catch ex As Exception
                MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
            End Try
            Return ""
        End Function


        Public Function getInsertCmd() As String
            Try
                Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
                Dim str As String = ""
                Dim values As String = ""
                For Each i In list
                    If Not i.isPrimaryParam Then
                        str &= "[" & i.Text & "],"
                        values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                    End If
                Next
                str = str.Substring(0, str.Length - 1) 'wg. ','
                values = values.Substring(0, values.Length - 1) 'wg. ','
            Return (" INSERT INTO [tblWarenorte]  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
                MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
            End Try
            Return ""
        End Function

    Public Function deleteWarenort(ByVal id As Integer) As Boolean

        Try
            SQL.doSQL("DELETE FROM [tblWarenorte] WHERE wo_id = '" & id & "'", "AVISO")
            Return True
        Catch ex As SqlException
            MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
        End Try

        Return False

    End Function



End Class

