Imports System.Data.SqlClient
Imports System.Reflection

Public Class cFiskalkundenAnhaenge

    Property fka_id As Integer
    Property fka_KdNr As Integer
    Property fka_fkId As Integer
    Property fka_Name As String
    Property fka_docId As Integer
    Property fka_Art As Object = Nothing
    Property fka_Datum As Object = Nothing

    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(fka_id As Integer)
        Me.fka_id = fka_id
        LOAD()

    End Sub

    Sub New(fka_Name, fka_docId, fka_Art)

        Me.fka_Name = fka_Name
        Me.fka_docId = fka_docId
        Me.fka_Art = fka_Art
        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("fka_id", fka_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_KdNr", fka_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_Name", fka_Name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_docId", fka_docId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_Art", fka_Art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_Datum", fka_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fka_fkId", fka_fkId))

        Return list
    End Function


    Shared Function LOAD_LIST_ByKdNr(fka_KdNr As Integer, fka_fkId As Integer, fka_Art As String) As List(Of cFiskalkundenAnhaenge)
        Dim ANH_LIST = New List(Of cFiskalkundenAnhaenge)
        ANH_LIST.Clear()
        Dim SQL As New SQL
        Dim dgv = SQL.loadDgvBySql("SELECT fka_id FROM [tblFiskalkundenAnhaenge] WHERE fka_KdNr=" & fka_KdNr & " And fka_fkId = " & fka_fkId & " And fka_Art = '" & fka_Art & "'" & " ORDER BY fka_Datum desc", "AVISO")
        If dgv IsNot Nothing Then
            For Each r In dgv.Rows

                ANH_LIST.Add(New VERAG_PROG_ALLGEMEIN.cFiskalkundenAnhaenge(r("fka_id")))
            Next
        End If

        Return ANH_LIST

    End Function

    Public Function SAVE(Optional errHinweis = "") As Boolean
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN If EXISTS(Select * FROM [tblFiskalkundenAnhaenge] With(updlock, serializable) WHERE fka_id=@fka_id) " &
                           " BEGIN " & getUpdateCmd() & " End " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " End " &
                           " commit tran "

        fka_id = SQL.doSQLVarListID(fka_id, sqlstr, "AVISO", , list,, errHinweis)
        Return fka_id > 0
    End Function

        Public Sub LOAD()
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("Select * FROM [tblFiskalkundenAnhaenge] WHERE fka_id=@fka_id  ", conn)
                    cmd.Parameters.AddWithValue("@fka_id", fka_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
                            hasEntry = True
                        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 [tblFiskalkundenAnhaenge] SET " & str & " WHERE fka_id=@fka_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 [tblFiskalkundenAnhaenge]  (" & 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 DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblFiskalkundenAnhaenge]  WHERE fka_id=" & fka_id
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function

    Public Function DELETEALL(fka_KdNr As Integer, fka_fkId As Integer) As Boolean
        Dim sqlstr = " DELETE FROM [tblFiskalkundenAnhaenge]  WHERE fka_KdNr=" & fka_KdNr & " and fka_fkId=" & fka_fkId
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function

End Class
