
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cFiskal_Daten
    Property fisk_KundenNr As Integer
    Property fisk_schreibenFA As Boolean = False
    Property fisk_schreibenSB As Boolean = False
    Property fisk_schreibenCMR As Boolean = False
    Property fisk_schreibenFA_Adr As Object = Nothing
    Property fisk_schreibenSB_Adr As Object = Nothing
    Property fisk_schreibenSB_Mail As Object = Nothing
    Property fisk_schreibenFA_Mail As Object = Nothing



    Dim SQL As New SQL

    Sub New(KundenNr)
        Me.fisk_KundenNr = KundenNr
        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("fisk_KundenNr", fisk_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenFA", fisk_schreibenFA))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenSB", fisk_schreibenSB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenCMR", fisk_schreibenCMR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenFA_Adr", fisk_schreibenFA_Adr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenSB_Adr", fisk_schreibenSB_Adr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenSB_Mail", fisk_schreibenSB_Mail))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fisk_schreibenFA_Mail", fisk_schreibenFA_Mail))
        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 tblFiskal_Daten WITH(updlock,serializable) WHERE fisk_KundenNr=@fisk_KundenNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblFiskal_Daten WHERE fisk_KundenNr=@fisk_KundenNr  ", conn)
                    cmd.Parameters.AddWithValue("@fisk_KundenNr", fisk_KundenNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.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 [tblFiskal_Daten] SET " & str & " WHERE fisk_KundenNr=@fisk_KundenNr ")

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