

Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cKundenSDL
    Property KundenNr As Integer
    Property SDLNr As Integer
    Property Sperre As Integer
    Property Sperrdatum As Object = Nothing
    Property Vertretungsvollmacht As Object = Nothing
    Property Auszahlungsvollmacht As Object = Nothing
    Property Finanzamtbestätigung As Object = Nothing
    Property Vermerk As Object = Nothing
    Property Antragsdatum As Object = Nothing
    Property Erfassungsdatum As Object = Nothing
    Property Änderungsdatum As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property MautDE_FlottenPIN As Object = Nothing
    Property MautDE_MasterPIN As Object = Nothing
    Property MautDE_BenutzerNr As Object = Nothing
    Property MautAT_KartenNr As Object = Nothing


    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(KundenNr, SDLNr)
        Me.KundenNr = KundenNr
        Me.SDLNr = SDLNr
        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("SDLNr", SDLNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sperre", Sperre))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sperrdatum", Sperrdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vertretungsvollmacht", Vertretungsvollmacht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Auszahlungsvollmacht", Auszahlungsvollmacht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Finanzamtbestätigung", Finanzamtbestätigung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vermerk", Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Antragsdatum", Antragsdatum))
        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("MautDE_FlottenPIN", MautDE_FlottenPIN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MautDE_MasterPIN", MautDE_MasterPIN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MautDE_BenutzerNr", MautDE_BenutzerNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MautAT_KartenNr", MautAT_KartenNr))

        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 KundenSDL  WHERE KundenNr=@KundenNr and SDLNr=@SDLNr) " &
                       " 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 KundenSDL WHERE KundenNr=@KundenNr and SDLNr=@SDLNr  ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)
                    cmd.Parameters.AddWithValue("@SDLNr", SDLNr)
                    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 [KundenSDL] SET " & str & " WHERE KundenNr=@KundenNr and SDLNr=@SDLNr  ")

        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 KundenSDL  (" & 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

End Class
