
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cSendVorkosten
    Property sndvk_id As Integer
    Property sndvk_sendId As Object = Nothing
    Property sndvk_LeistungsNr As Object = Nothing
    Property sndvk_LeistungsBez As Object = Nothing
    Property sndvk_Preis As Object = Nothing
    Property sndvk_Firma As Object = Nothing


    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(sndvk_id)
        Me.sndvk_id = sndvk_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("sndvk_id", sndvk_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sndvk_sendId", sndvk_sendId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sndvk_LeistungsNr", sndvk_LeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sndvk_LeistungsBez", sndvk_LeistungsBez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sndvk_Preis", sndvk_Preis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sndvk_Firma", sndvk_Firma))

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

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

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM tblSendVorkosten WHERE sndvk_id=@sndvk_id  ", conn)
                    cmd.Parameters.AddWithValue("@sndvk_id", sndvk_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 [tblSendVorkosten] SET " & str & " WHERE sndvk_id=@sndvk_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 tblSendVorkosten  (" & 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 INSERT() As Boolean
        Try
            Return SQL.doSQLVarList(getInsertCmd, "AVISO", , getParameterList)
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & " 'cSENDUNGEN'", System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function
End Class
