Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenVermerke
    Property LIST As New List(Of cKundenVermerkItem)
    Dim kvm_KundenNr As Integer

    Shared SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Sub New(kvm_KundenNr)
        LOAD_LIST(kvm_KundenNr)
    End Sub

    Sub New(kvm_KundenNr, kvm_art)
        LOAD_LIST(kvm_KundenNr, kvm_art)
    End Sub


    Public Shared Function getDT(kvm_KundenNr As Integer, Optional kvm_art() As String = Nothing) As DataTable
        Try
            Dim art As String = ""
            If kvm_art IsNot Nothing Then
                For Each s In kvm_art
                    art &= "'" & s & "',"
                Next
                If art.EndsWith(",") Then art = art.Substring(0, art.Length - 1)
            End If

            Dim myTable = New DataTable()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL
                Using cmd As New SqlCommand("SELECT * FROM [tblKundenVermerke] WHERE [kvm_KundenNr] =@kvm_KundenNr " & If(art <> "", "AND kvm_art IN (" & art & ") ", "") & " ORDER BY [kvm_datum] desc ", conn)
                    cmd.Parameters.AddWithValue("@kvm_KundenNr", kvm_KundenNr)
                    Dim dr As SqlDataReader = cmd.ExecuteReader()
                    myTable.Load(dr)
                    dr.Close()
                End Using
            End Using
            Return myTable  'While Schleife wird hier verlassen
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message)
        End Try
        Return Nothing
    End Function

    Public Sub LOAD_LIST(kvm_KundenNr As Integer, Optional kvm_art() As String = Nothing)
        Try
            Dim art As String = ""
            If kvm_art IsNot Nothing Then
                For Each s In kvm_art
                    art &= "'" & art & "',"
                Next
                If art.EndsWith(",") Then art = art.Substring(0, art.Length - 1)
            End If
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblKundenVermerke] WHERE [kvm_KundenNr] =@kvm_KundenNr " & If(art <> "", "AND kvm_art IN (" & art & ") ", "") & " ORDER BY [kvm_datum] desc ", conn)
                    cmd.Parameters.AddWithValue("@kvm_KundenNr", kvm_KundenNr)

                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cKundenVermerkItem()
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next

                        LIST.Add(l)

                    End While
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub

    Shared Function INSERT(kvm_KundenNr, kvm_art, kvm_text) As Boolean
        Try
            Dim KDV As New cKundenVermerkItem
            KDV.kvm_art = kvm_art
            KDV.kvm_text = kvm_text
            KDV.kvm_KundenNr = kvm_KundenNr
            KDV.kvm_datum = Now
            KDV.kvm_sb = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            KDV.kvm_sbId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
            Return KDV.SAVE()
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function

    Public Function ADD(kvm_art, kvm_text) As Boolean
        Try
            Dim KDV As New cKundenVermerkItem
            KDV.kvm_art = kvm_art
            KDV.kvm_text = kvm_text
            KDV.kvm_datum = Now
            KDV.kvm_KundenNr = Me.kvm_KundenNr
            KDV.kvm_sb = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            KDV.kvm_sbId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
            If KDV.SAVE() Then
                LIST.Add(KDV)
                Return True
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function


    Public Shared Function DELETE(id) As Boolean
        Try
            Dim KDV As New cKundenVermerkItem(id)
            Return KDV.DELETE()
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function
    Public Function REMOVE(kvm_id) As Boolean
        Try
            For Each i In LIST
                If i.kvm_id = kvm_id Then
                    If i.DELETE() Then
                        LIST.Remove(i)
                        Return True
                    End If
                End If
            Next
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function



End Class

Public Class cKundenVermerkItem


    Property kvm_id As Integer
    Property kvm_KundenNr As Integer
    Property kvm_art As String = ""
    Property kvm_datum As Date = Now
    Property kvm_text As String = ""
    Property kvm_sbId As Integer
    Property kvm_sb As String = ""


    Dim SQL As New SQL

    Sub New()
    End Sub

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

    'Shared Sub ADD(kvm_art, kvm_text)
    '     Me.kvm_datum = Now
    '     Me.kvm_sbId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
    '    Me.kvm_sb = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
    '    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("kvm_id", kvm_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_KundenNr", kvm_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_art", kvm_art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_datum", kvm_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_text", kvm_text))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_sbId", kvm_sbId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kvm_sb", kvm_sb))
        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 tblKundenVermerke   WHERE kvm_id=@kvm_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        kvm_id = SQL.doSQLVarListID(kvm_id, sqlstr, "FMZOLL", , list)
        Return kvm_id > 0
    End Function


    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblKundenVermerke]   WHERE kvm_id=" & Me.kvm_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenVermerke WHERE kvm_id=@kvm_id  ", conn)
                    cmd.Parameters.AddWithValue("@kvm_id", kvm_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
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 [tblKundenVermerke] SET " & str & " WHERE kvm_id=@kvm_id ")

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 tblKundenVermerke  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return ""
    End Function

End Class
