


Imports System.Data.SqlClient
    Imports System.Reflection
Public Class cSDL_LKW
    Property KundenNr As Integer
    Property KfzKennzeichen As String
    Property Nationalität As Object = Nothing
    Property Verkauft As Object = Nothing
    Property Abgemeldet As Object = Nothing
    Property Vermerk As Object = Nothing
    Property Erfassungsdatum As Object = Nothing
    Property Änderungsdatum As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property KZAenderung As Object = Nothing
    Property ts As Object = Nothing
    Property Baujahr As Object = Nothing
    Property KzMiete As Object = Nothing
    Property KzLeasing As Object = Nothing
    Property KzFinanzierungBank As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(KundenNr, KfzKennzeichen)
        Me.KundenNr = KundenNr
        Me.KfzKennzeichen = KfzKennzeichen
        LOAD()
    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("KundenNr", KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KfzKennzeichen", KfzKennzeichen))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Nationalität", Nationalität))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Verkauft", Verkauft))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Abgemeldet", Abgemeldet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vermerk", Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Erfassungsdatum", Erfassungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Änderungsdatum", Änderungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachbearbeiter", Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KZAenderung", KZAenderung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Baujahr", Baujahr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KzMiete", KzMiete))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KzLeasing", KzLeasing))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KzFinanzierungBank", KzFinanzierungBank))

        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 LKW  WHERE KundenNr=@KundenNr and KfzKennzeichen=@KfzKennzeichen) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionSDL()
                Using cmd As New SqlCommand("SELECT * FROM LKW WHERE KundenNr=@KundenNr and KfzKennzeichen=@KfzKennzeichen  ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)
                    cmd.Parameters.AddWithValue("@KfzKennzeichen", KfzKennzeichen)
                    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 [LKW] SET " & str & " WHERE KundenNr=@KundenNr and KfzKennzeichen=@KfzKennzeichen  ")

        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 LKW  (" & 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 Shared Function GET_LIST_WEB(KundenNr As Integer) As List(Of cSDL_LKW)
        Dim LIST As New List(Of cSDL_LKW)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionSDL()

                Using cmd As New SqlCommand("SELECT * FROM LKW WHERE KundenNr=@KundenNr ORDER BY KfzKennzeichen ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)

                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim SDL_LKW As New cSDL_LKW()
                        For Each li In SDL_LKW.getParameterList()
                            Dim propInfo As PropertyInfo = SDL_LKW.GetType.GetProperty(li.Scalarvariable)

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

                        LIST.Add(SDL_LKW)
                        '  If loadPos Then LOAD_POSITIONEN()
                    End While
                    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
        Return LIST
    End Function


End Class
