
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cKundenVertraege
    Property kv_Id As Integer
    Property kv_KundenNr As Integer
    Property kv_erhalten_Datum As Date
    Property kv_vertragArtId As Integer
    Property kv_Info As Object = Nothing
    Property kv_datenarchivId As Object = Nothing
    Property kv_gueltig As Boolean = True


    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(kv_Id)
        Me.kv_Id = kv_Id
        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("kv_Id", kv_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_KundenNr", kv_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_erhalten_Datum", kv_erhalten_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_vertragArtId", kv_vertragArtId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_Info", kv_Info))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_datenarchivId", kv_datenarchivId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kv_gueltig", kv_gueltig))

        Return list
    End Function

    Public Shared Function LOAD_LIST(KundenNr As Integer) As DataTable
        Return (New SQL).loadDgvBySql("SELECT [kv_Id],[kv_KundenNr],[kv_erhalten_Datum],[kv_vertragArtId],kva_Bezeichnung,[kv_Info],[kv_datenarchivId],[kv_gueltig] FROM tblKundenVertraege INNER JOIN tblKundenVertraegeArten ON kv_vertragArtId=kva_id WHERE kv_KundenNr=" & KundenNr, "FMZOLL")
    End Function

    Public Shared Function ENTRY_Exists(KundenNr As Integer, kv_vertragArtId As Integer) As Boolean
        Return (CInt((New SQL).getValueTxtBySql("SELECT count(*) FROM tblKundenVertraege WHERE kv_KundenNr=" & KundenNr & " AND kv_vertragArtId='" & kv_vertragArtId & "'", "FMZOLL")) > 0)
    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 tblKundenVertraege With(updlock, serializable) WHERE kv_Id=@kv_Id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select * FROM tblKundenVertraege WHERE kv_Id=@kv_Id  ", conn)
                    cmd.Parameters.AddWithValue("@kv_Id", kv_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

                    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 [tblKundenVertraege] SET " & str & " WHERE kv_Id=@kv_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 tblKundenVertraege  (" & 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 DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblKundenVertraege] WITH(updlock,serializable) WHERE kv_Id=" & Me.kv_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


End Class
