Imports System.Data.SqlClient
Imports System.Reflection

Public Class cBonitaetsauskunft

    Property ba_id As Integer = -1
    Property ba_KundenNr As Integer
    Property ba_Datum As Date
    Property ba_MaId As Integer
    Property ba_Sachbearbeiter As String = ""
    Property ba_CRArt As String = ""
    Property ba_CRAnsprechpartnerHerrFrau As String = ""
    Property ba_CRAnsprechpartner As String = ""
    Property ba_Firma As String = ""
    Property ba_Adresse1 As String = ""
    Property ba_Adresse2 As String = ""
    Property ba_LetztesBearbeitungsDatum As Object = Nothing
    Property ba_BonitaetsIndex As Integer
    Property ba_GruendundsDatum As Object = Nothing
    Property ba_Umsatz As Object = Nothing
    Property ba_Mitarbeiter As Object = Nothing
    Property ba_Bankverbindung As Object = Nothing
    Property ba_Hoechstkredit As Object = Nothing
    Property ba_Zahlungsweise As Object = Nothing
    Property ba_GFName As Object = Nothing
    Property ba_Sonstiges As Object = Nothing
    Property ba_Risikostufe As Object = Nothing

    Dim SQL As New SQL

    Sub New()
    End Sub

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

    Public 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("ba_id", ba_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_KundenNr", ba_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Datum", ba_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_MaId", ba_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Sachbearbeiter", ba_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_CRArt", ba_CRArt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_CRAnsprechpartnerHerrFrau", ba_CRAnsprechpartnerHerrFrau))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_CRAnsprechpartner", ba_CRAnsprechpartner))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Firma", ba_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Adresse1", ba_Adresse1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Adresse2", ba_Adresse2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_LetztesBearbeitungsDatum", ba_LetztesBearbeitungsDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_BonitaetsIndex", ba_BonitaetsIndex))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_GruendundsDatum", ba_GruendundsDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Umsatz", ba_Umsatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Mitarbeiter", ba_Mitarbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Bankverbindung", ba_Bankverbindung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Hoechstkredit", ba_Hoechstkredit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Zahlungsweise", ba_Zahlungsweise))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_GFName", ba_GFName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Sonstiges", ba_Sonstiges))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ba_Risikostufe", ba_Risikostufe))

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

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

    Public Shared Function LOADByKdNrDate(ba_KundenNr As Integer, ba_Datum As Date) As cBonitaetsauskunft
         Try
            Dim BONI As New cBonitaetsauskunft
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblBonitaetsauskunft WHERE ba_KundenNr=@ba_KundenNr AND  cast(ba_Datum as date)=@ba_Datum ", conn)
                    cmd.Parameters.AddWithValue("@ba_KundenNr", ba_KundenNr)
                    cmd.Parameters.AddWithValue("@ba_Datum", ba_Datum.ToShortDateString)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In BONI.getParameterList()
                            Dim propInfo As PropertyInfo = BONI.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(BONI, Nothing)
                            Else
                                propInfo.SetValue(BONI, dr.Item(li.Text))
                            End If

                        Next
                        dr.Close()
                        Return BONI
                    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
        Return Nothing
    End Function

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