

Imports System.Data.SqlClient
Imports System.Reflection

Public Class cSDL
    Property KundenNr As Integer
    Property KfzKennzeichen As String = ""
    Property SDLNr As Integer
    Property History As Integer
    Property RefKundenNr As Object = Nothing
    Property MSE_Kd_Nr As Object = Nothing
    Property TELEPASS_Kd_Nr As Object = Nothing
    Property CLIENT_Nr As Object = Nothing
    Property BestellNr As Object = Nothing
    Property Bestelldatum As Object = Nothing
    Property Lieferdatum As Object = Nothing
    Property Ausstellungsdatum As Object = Nothing
    Property Umstellungsdatum As Object = Nothing
    Property KartenNr As Object = Nothing
    Property PAN_Nr As Object = Nothing
    Property VIACARD As Object = Nothing
    Property MOTORWAY_CARD_Nr As Object = Nothing
    Property CARD_Nr As Object = Nothing
    Property KartenSchlüsselNr As Object = Nothing
    Property FREJUS_CARD_Nr As Object = Nothing
    Property ECO As Object = Nothing
    Property GültigBis As Object = Nothing
    Property OBUID As Object = Nothing
    Property SERIE_Nr As Object = Nothing
    Property TELEPASS_Nr As Object = Nothing
    Property Vermerk As Object = Nothing
    Property Partner As Object = Nothing
    Property BenutzerNr As Object = Nothing
    Property PIN As Object = Nothing
    Property FlottenPIN As Object = Nothing
    Property MasterPIN As Object = Nothing
    Property VertragsNr As Object = Nothing
    Property Vertragsabschlussdatum As Object = Nothing
    Property Kategorie As Object = Nothing
    Property Schadstoffklasse As Object = Nothing
    Property Status As Object = Nothing
    Property Ersatzkarte As Object = Nothing
    Property Gesperrt As Object = Nothing
    Property Defekt As Object = Nothing
    Property Verloren As Object = Nothing
    Property Gestohlen As Object = Nothing
    Property Erfassungsdatum As Object = Nothing
    Property Änderungsdatum As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property Retourniert As Object = Nothing
    Property RetourniertAm As Object = Nothing

    Property GesperrtAm As Object = Nothing
    Property DefektAm As Object = Nothing
    Property VerlorenAm As Object = Nothing
    Property GestohlenAm As Object = Nothing
    Property ErsatzkarteAm As Object = Nothing
    Property TelepassErsatzkarte As Object = Nothing
    Property TelepassErsatzkarteAm As Object = Nothing
    Property Archiviert As Boolean = False
    Property LKWAbgemeldet As Boolean = False
    Property LKWAbgemeldetAM As Object = Nothing
    Property Fahrer As Object = Nothing
    Property Kaution As Object = Nothing
    Property BelegNr As Object = Nothing
    Property Nationalitaet As Object = Nothing
    Property CO2Kategorie As Object = Nothing
    Property CVCNr As Object = Nothing

    Public hasentry
    Dim SQL As New SQL

    Sub New()

    End Sub

    Sub New(KundenNr, KfzKennzeichen, SDLNr, History)
        Me.KundenNr = KundenNr
        Me.KfzKennzeichen = KfzKennzeichen
        Me.SDLNr = SDLNr
        Me.History = History
        LOAD()
    End Sub


    Sub New(KundenNr, KfzKennzeichen, SDLNr)
        Me.KundenNr = KundenNr
        Me.KfzKennzeichen = KfzKennzeichen
        Me.SDLNr = SDLNr
        Dim histroyTemp = SQL.getValueTxtBySql("SELECT TOP 1 History FROM SDL WHERE KundenNr='" & KundenNr & "' AND KfzKennzeichen='" & KfzKennzeichen & "' AND SDLNr='" & SDLNr & "' ORDER BY History DESC", "SDL")
        Me.History = IIf(histroyTemp <> "", histroyTemp, 0)
        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("SDLNr", SDLNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("History", History))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("RefKundenNr", RefKundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MSE-Kd-Nr", MSE_Kd_Nr, "MSE_Kd_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TELEPASS-Kd-Nr", TELEPASS_Kd_Nr, "TELEPASS_Kd_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CLIENT-Nr", CLIENT_Nr, "CLIENT_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BestellNr", BestellNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bestelldatum", Bestelldatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Lieferdatum", Lieferdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ausstellungsdatum", Ausstellungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Umstellungsdatum", Umstellungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KartenNr", KartenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PAN-Nr", PAN_Nr, "PAN_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VIACARD", VIACARD))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MOTORWAY-CARD-Nr", MOTORWAY_CARD_Nr, "MOTORWAY_CARD_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CARD-Nr", CARD_Nr, "CARD_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KartenSchlüsselNr", KartenSchlüsselNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FREJUS-CARD-Nr", FREJUS_CARD_Nr, "FREJUS_CARD_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ECO", ECO))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GültigBis", GültigBis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("OBUID", OBUID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SERIE-Nr", SERIE_Nr, "SERIE_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TELEPASS-Nr", TELEPASS_Nr, "TELEPASS_Nr"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vermerk", Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Partner", Partner))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BenutzerNr", BenutzerNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PIN", PIN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FlottenPIN", FlottenPIN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MasterPIN", MasterPIN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VertragsNr", VertragsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vertragsabschlussdatum", Vertragsabschlussdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kategorie", Kategorie))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Schadstoffklasse", Schadstoffklasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Status", Status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ersatzkarte", Ersatzkarte))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gesperrt", Gesperrt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Defekt", Defekt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Verloren", Verloren))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gestohlen", Gestohlen))
        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("Retourniert", Retourniert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("RetourniertAm", RetourniertAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GesperrtAm", GesperrtAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DefektAm", DefektAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VerlorenAm", VerlorenAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GestohlenAm", GestohlenAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErsatzkarteAm", ErsatzkarteAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TelepassErsatzkarte", TelepassErsatzkarte))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TelepassErsatzkarteAm", TelepassErsatzkarteAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Archiviert", Archiviert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKWAbgemeldet", LKWAbgemeldet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKWAbgemeldetAM", LKWAbgemeldetAM))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fahrer", Fahrer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kaution", Kaution))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BelegNr", BelegNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Nationalitaet", Nationalitaet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CO2Kategorie", CO2Kategorie))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CVCNr", CVCNr))

        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 SDL WITH(updlock,serializable) WHERE KundenNr=@KundenNr AND KfzKennzeichen=@KfzKennzeichen AND SDLNr=@SDLNr AND History=@History) " &
                       " 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 = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM SDL WHERE KundenNr=@KundenNr AND KfzKennzeichen=@KfzKennzeichen AND SDLNr=@SDLNr AND History=@History ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)
                    cmd.Parameters.AddWithValue("@KfzKennzeichen", KfzKennzeichen)
                    cmd.Parameters.AddWithValue("@SDLNr", SDLNr)
                    cmd.Parameters.AddWithValue("@History", History)
                    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 [SDL] SET " & str & " WHERE KundenNr=@KundenNr AND KfzKennzeichen=@KfzKennzeichen AND SDLNr=@SDLNr AND History=@History ")

        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 SDL  (" & 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, Optional SDLNr As Integer = -1) As List(Of cSDL)
        Dim LIST As New List(Of cSDL)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionSDL()

                Dim sqlWHERE = ""
                If SDLNr > 0 Then sqlWHERE &= " AND SDLNr =@SDLNr "

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

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

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

                        LIST.Add(SDL)
                        '  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

    Public Shared Function GET_LIST_WEB_LKW(KundenNr As Integer, Optional KfzKennzeichen As String = "") As List(Of cSDL)
        Dim LIST As New List(Of cSDL)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionSDL()

                Dim sqlWHERE = ""
                If KfzKennzeichen <> "" Then sqlWHERE &= " AND KfzKennzeichen =@KfzKennzeichen "

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

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

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

                        LIST.Add(SDL)
                        '  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
