

Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cBesuchsbericht
    Property bb_Id As Object = Nothing
    Property bb_DatumErfassung As Date = Now
    Property bb_DatumBesuch As Object = Nothing
        Property bb_Sachbearbeiter As Object = Nothing
        Property bb_SachbearbeiterId As Integer
        Property bb_Teilnehmer As Object = Nothing
        Property bb_Kontaktart As Object = Nothing
    Property bb_Firma As Object = Nothing
    Property bb_Kunde As Object = Nothing
    Property bb_KundenNr As Object = Nothing
        Property bb_Geschaeftsfuehrer As Object = Nothing
        Property bb_Umsatz As Object = Nothing
        Property bb_Anpsprechpartner As Object = Nothing
        Property bb_Telefon As Object = Nothing
        Property bb_Mail As Object = Nothing
        Property bb_FuhrparkEigentum As Object = Nothing
        Property bb_FuhrparkLeasing As Object = Nothing
        Property bb_LKW_Plane As Boolean = False
        Property bb_LKW_Kuehltranporte As Boolean = False
        Property bb_LKW_Sonsertransporte As Boolean = False
        Property bb_LKW_Sprinter As Boolean = False
        Property bb_LKW_SiloTank As Boolean = False
        Property bb_Auftraggeber As Object = Nothing
        Property bb_Route As Object = Nothing
        Property bb_DerzeitigerLieferant As Object = Nothing
        Property bb_Zufrieden As Object = Nothing
        Property bb_ZufriedenNEIN As Object = Nothing
    Property bb_Garantie As Object = Nothing
    Property bb_Interesse_Zollabfertigung As Boolean = False
        Property bb_Interesse_Brexit As Boolean = False
        Property bb_Interesse_Parkplatz As Boolean = False
        Property bb_Interesse_LagerUmschlag As Boolean = False
        Property bb_Interesse_Rola As Boolean = False
        Property bb_Interesse_YesMoney As Boolean = False
        Property bb_Interesse_Ferry As Boolean = False
        Property bb_Vermerk As Object = Nothing

        Public hasEntry = False

        Dim SQL As New SQL

    Sub New()
        bb_Garantie = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA

    End Sub
    Sub New(bb_Id)
        Me.bb_Id = bb_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("bb_Id", bb_Id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Firma", bb_Firma)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_DatumErfassung", bb_DatumErfassung)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_DatumBesuch", bb_DatumBesuch)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Sachbearbeiter", bb_Sachbearbeiter)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_SachbearbeiterId", bb_SachbearbeiterId)) 'Integer
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Teilnehmer", bb_Teilnehmer)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Kontaktart", bb_Kontaktart)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Kunde", bb_Kunde)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_KundenNr", bb_KundenNr)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Geschaeftsfuehrer", bb_Geschaeftsfuehrer)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Umsatz", bb_Umsatz)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Anpsprechpartner", bb_Anpsprechpartner)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Telefon", bb_Telefon)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Mail", bb_Mail)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_FuhrparkEigentum", bb_FuhrparkEigentum)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_FuhrparkLeasing", bb_FuhrparkLeasing)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_LKW_Plane", bb_LKW_Plane)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_LKW_Kuehltranporte", bb_LKW_Kuehltranporte)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_LKW_Sonsertransporte", bb_LKW_Sonsertransporte)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_LKW_Sprinter", bb_LKW_Sprinter)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_LKW_SiloTank", bb_LKW_SiloTank)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Auftraggeber", bb_Auftraggeber)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Route", bb_Route)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_DerzeitigerLieferant", bb_DerzeitigerLieferant)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Zufrieden", bb_Zufrieden)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_ZufriedenNEIN", bb_ZufriedenNEIN)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Garantie", bb_Garantie)) 'Object = Nothing
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_Zollabfertigung", bb_Interesse_Zollabfertigung)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_Brexit", bb_Interesse_Brexit)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_Parkplatz", bb_Interesse_Parkplatz)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_LagerUmschlag", bb_Interesse_LagerUmschlag)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_Rola", bb_Interesse_Rola)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_YesMoney", bb_Interesse_YesMoney)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Interesse_Ferry", bb_Interesse_Ferry)) 'Boolean = False
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bb_Vermerk", bb_Vermerk)) 'Object = Nothing
        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 tblBesuchsberichte  WHERE bb_Id=@bb_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        bb_Id = SQL.doSQLVarListID(bb_Id, sqlstr, "FMZOLL", , list)
        Return (bb_Id > 0)

    End Function


    Public Shared Function DELETE(bb_Id As Integer) As Boolean
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Return SQL.doSQL("DELETE FROM tblBesuchsberichte  WHERE bb_Id=" & bb_Id)
    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblBesuchsberichte WHERE bb_Id=@bb_Id  ", conn)
                    cmd.Parameters.AddWithValue("@bb_Id", bb_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 [tblBesuchsberichte] SET " & str & " WHERE bb_Id=@bb_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 tblBesuchsberichte  (" & 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

