Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenVollmachten

    Property kdvm_Id As Integer
    Property kdvm_KundenNr As Object = Nothing
    Property kdvm_kdvmaId As Object = Nothing
    Property kdvm_erhalten As Boolean = False
    Property kdvm_erhalten_Datum As Object = Nothing
    Property kdvm_Info As String
    Property kdvm_datenarchivId As Object = Nothing
    Property kdvm_datenarchivId_2 As Object = Nothing
    Property kdvm_archiviert As Boolean = False
    Property kdvm_gueltig_bis As Object = Nothing
    Property kdvm_gueltig_ab As Object = Nothing
    Property kdvm_vermerk As Object = Nothing
    Property kdvm_geloescht As Boolean = False
    Property kdvm_emailId As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

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

    Sub New()

    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("kdvm_Id", kdvm_Id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_KundenNr", kdvm_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_kdvmaId", kdvm_kdvmaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_erhalten", kdvm_erhalten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_erhalten_Datum", kdvm_erhalten_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_Info", kdvm_Info))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_datenarchivId", kdvm_datenarchivId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_datenarchivId_2", kdvm_datenarchivId_2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_archiviert", kdvm_archiviert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_gueltig_bis", kdvm_gueltig_bis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_gueltig_ab", kdvm_gueltig_ab))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_vermerk", kdvm_vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_geloescht", kdvm_geloescht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdvm_emailId", kdvm_emailId))

        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 tblKundenVollmachten  WHERE kdvm_Id=@kdvm_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Function SAVEVM() As Integer
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblKundenVollmachten  WHERE kdvm_Id=@kdvm_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        kdvm_Id = SQL.doSQLVarListID(kdvm_Id, sqlstr, "FMZOLL", , list)
        Return kdvm_Id
    End Function

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


