Imports System.Data.SqlClient
Imports System.Reflection


Public Class cFiskalkunden
    Property FK_Id As Integer
    Property FK_Kdnr As Object = Nothing
    Property FK_Datum As Object = Nothing
    Property FK_Art As Object = Nothing
    Property FK_locked As Object = Nothing
    Property FK_Bonitaet As Object = Nothing
    Property FK_Vollmacht As Object = Nothing
    Property FK_Untervollmacht As Object = Nothing
    Property FK_UID As Object = Nothing
    Property FK_EORI As Object = Nothing
    Property FK_Website_URL As Object = Nothing
    Property FK_WebsiteSt_URL As Object = Nothing
    Property FK_VollmachtOriginal As Boolean
    Property FK_MaId As Object = Nothing
    Property FK_Abschlussdatum As Object = Nothing
    Property FK_Anmerkung As Object = Nothing
    Property FK_Lieferanten As Object = Nothing
    Property FK_Empfaenger As Object = Nothing
    Property FK_Zwischenhaendler As Object = Nothing
    Property FK_Waren As Object = Nothing
    Property FK_Gruendungsdatum As Object = Nothing
    Property FK_AnzahlMitarbeiter As Object = Nothing
    Property FK_Geschaeftszweck As Object = Nothing
    Property FK_KreditMax As Object = Nothing
    Property FK_Geschaeftsfuehrer As Object = Nothing
    Property FK_Steuerberater As Object = Nothing
    Property FK_Umsatz As Object = Nothing
    Property FK_Land As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

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

    Sub New()

    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("FK_Id", FK_Id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Kdnr", FK_Kdnr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Datum", FK_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Art", FK_Art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_locked", FK_locked))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Bonitaet", FK_Bonitaet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Vollmacht", FK_Vollmacht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Untervollmacht", FK_Untervollmacht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_UID", FK_UID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_EORI", FK_EORI))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Website_URL", FK_Website_URL))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_WebsiteSt_URL", FK_WebsiteSt_URL))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_VollmachtOriginal", FK_VollmachtOriginal))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Anmerkung", FK_Anmerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Abschlussdatum", FK_Abschlussdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_MaId", FK_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Lieferanten", FK_Lieferanten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Empfaenger", FK_Empfaenger))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Zwischenhaendler", FK_Zwischenhaendler))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Waren", FK_Waren))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Gruendungsdatum", FK_Gruendungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_AnzahlMitarbeiter", FK_AnzahlMitarbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Geschaeftszweck", FK_Geschaeftszweck))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_KreditMax", FK_KreditMax))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Geschaeftsfuehrer", FK_Geschaeftsfuehrer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Steuerberater", FK_Steuerberater))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Umsatz", FK_Umsatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FK_Land", FK_Land))

        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 [tblFiskalkunden]  WHERE FK_Id=@FK_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

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

        Dim sql = "DELETE FROM tblFiskalkunden WHERE FK_Id=@FK_Id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@FK_Id", FK_Id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

End Class


