
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cEssensbestellung
    'Property _BASE_id As Integer
    Property eb_kw As Object = Nothing
    Property eb_mitid As Object = Nothing
    Property eb_montag As Object = Nothing
    Property eb_dienstag As Object = Nothing
    Property eb_mittwoch As Object = Nothing
    Property eb_donnerstag As Object = Nothing
    Property eb_freitag As Object = Nothing
    Property eb_preis As Object = Nothing
    Property eb_gebaeude As Object = Nothing
    Property eb_abteilung As Object = Nothing
    Property eb_datum As Date = Nothing
    Property eb_mitname As Object = Nothing
    Property eb_abgerechnet_am As Object = Nothing
    Property eb_storniert_am As Object = Nothing
    Property eb_storniert As Boolean
    Property eb_abgerechnet As Boolean
    Property eb_anmerkung As Object = Nothing
    Property eb_gesendet_am As Object = Nothing
    Property eb_gesendet As Boolean


    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(eb_kw, eb_mitid, eb_gebaeude, eb_abteilung, eb_mitname, eb_anmerkung)
        Me.eb_kw = eb_kw
        Me.eb_mitid = eb_mitid
        Me.eb_gebaeude = eb_gebaeude
        Me.eb_abteilung = eb_abteilung
        Me.eb_mitname = eb_mitname
        Me.eb_anmerkung = eb_anmerkung
        LOAD()
    End Sub

    Sub New()


    End Sub

    Sub New(eb_kw, eb_mitid)
        Me.eb_kw = eb_kw
        Me.eb_mitid = eb_mitid
        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("_BASE_id", _BASE_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_kw", eb_kw))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_mitid", eb_mitid))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_montag", eb_montag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_dienstag", eb_dienstag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_mittwoch", eb_mittwoch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_donnerstag", eb_donnerstag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_freitag", eb_freitag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_gebaeude", eb_gebaeude))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_abteilung", eb_abteilung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_preis", eb_preis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_datum", eb_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_mitname", eb_mitname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_storniert", eb_storniert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_storniert_am", eb_storniert_am))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_abgerechnet", eb_abgerechnet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_abgerechnet_am", eb_abgerechnet_am))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_anmerkung", eb_anmerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_gesendet", eb_gesendet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_gesendet_am", eb_gesendet_am))


        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 tblEssensbestellungen  WHERE eb_kw=@eb_kw AND eb_mitid=@eb_mitid) " &
                       " 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 tblEssensbestellungen WHERE eb_kw=@eb_kw AND eb_mitid=@eb_mitid  ", conn)
                    cmd.Parameters.AddWithValue("@eb_kw", eb_kw)
                    cmd.Parameters.AddWithValue("@eb_mitid", eb_mitid)
                    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 [tblEssensbestellungen] SET " & str & " WHERE eb_kw=@eb_kw AND eb_mitid=@eb_mitid ")

        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 tblEssensbestellungen  (" & 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

