Imports System.Data.SqlClient
Imports System.Reflection



Public Class cAvisoAnhangsarten_LIST
    Public LIST As New List(Of cAvisoAnhangsarten)
    Dim SQL As New SQL


    Sub New(aktiv As Boolean, firma As String, filiale As String)
        LOAD_LIST(aktiv, firma, filiale)
    End Sub

    Public Sub LOAD_LIST(aktiv As Boolean, Firma As String, Filiale As String)
        Try

            LIST.Clear()

            For Each r In SQL.loadDgvBySql("Select aa_id FROM [tblAvisoAnhangsArten] WHERE aa_aktiv=" & IIf(aktiv, "1", "0") & " And ((aa_firma Is null And aa_filiale Is null) " & IIf(Firma <> "", " Or aa_firma = '" & Firma & "'", "") & IIf(Filiale <> "", " OR aa_filiale = '" & Filiale & "'", "") & ")", "AVISO").Rows
                LIST.Add(New VERAG_PROG_ALLGEMEIN.cAvisoAnhangsarten(r("aa_id")))
            Next

        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


End Class



Public Class cAvisoAnhangsarten
    Property aa_id As Integer
    Property aa_name As Object = Nothing
    Property aa_sort As Object = Nothing
    Property aa_isBrexti As Object = Nothing
    Property aa_aktiv As Object = Nothing
    Property aa_bezeichnung As Object = Nothing
    Property aa_firma As Object = Nothing
    Property aa_filiale As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL


    Sub New()

    End Sub

    Sub New(aa_id)
        Me.aa_id = aa_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("aa_id", aa_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_name", aa_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_sort", aa_sort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_isBrexti", aa_isBrexti))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_aktiv", aa_aktiv))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_bezeichnung", aa_bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_firma", aa_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aa_filiale", aa_filiale))

        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 [tblAvisoAnhangsArten]  WHERE aa_id=@aa_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.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM [tblAvisoAnhangsArten] WHERE aa_id=@aa_id  ", conn)
                    cmd.Parameters.AddWithValue("@aa_id", aa_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 [tblAvisoAnhangsArten] SET " & str & " WHERE aa_id=@aa_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 [tblAvisoAnhangsArten]  (" & 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

