
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cVoraskasse

    Property vk_id As Integer
    Property vk_FilialeNr As Object = Nothing
    Property vk_AbfertigungsNr As Object = Nothing
    Property vk_Datum As Object = Nothing
    Property vk_LKW As Object = Nothing
    Property vk_KdNr As Object = Nothing
    Property vk_Firma As Object = Nothing
    Property vk_Betrag As Object = Nothing
    Property vk_QS_SachbearbeiterId As Object = Nothing
    Property vk_QS_Sachbearbeiter As Object = Nothing
    Property vk_QS_Bemerkung As Object = Nothing
    Property vk_GeldEingetroffen As Boolean = False
    Property vk_BH_SachbearbeiterId As Object = Nothing
    Property vk_BH_Sachbearbeiter As Object = Nothing
    Property vk_BH_Bemerkung As Object = Nothing
    Property vk_BH_DatumGeldeingang As Object = Nothing


    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(vk_id)
        Me.vk_id = vk_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("vk_id", vk_id, , True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_FilialeNr", vk_FilialeNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_AbfertigungsNr", vk_AbfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Datum", vk_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_LKW", vk_LKW))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_KdNr", vk_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Firma", vk_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Betrag", vk_Betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_SachbearbeiterId", vk_QS_SachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_Sachbearbeiter", vk_QS_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_Bemerkung", vk_QS_Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_GeldEingetroffen", vk_GeldEingetroffen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_SachbearbeiterId", vk_BH_SachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_Sachbearbeiter", vk_BH_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_Bemerkung", vk_BH_Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_DatumGeldeingang", vk_BH_DatumGeldeingang))


        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 tblVorauskasse WITH(updlock,serializable) WHERE vk_id=@vk_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Dim id = SQL.doSQLVarListID(vk_id, sqlstr, "ADMIN", , list)
        Me.vk_id = id
        Return id > 0
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblVorauskasse WHERE vk_id=@vk_id  ", conn)
                    cmd.Parameters.AddWithValue("@vk_id", vk_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.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 [tblVorauskasse] SET " & str & " WHERE vk_id=@vk_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 tblVorauskasse  (" & 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