
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cAbrechnung_NCTSBrg_LIST
    Public LIST As New List(Of cAbrechnung_NCTSBrg)

    Sub New(abrg_Partner)
        LOAD_LIST(abrg_Partner)

    End Sub

    Sub LOAD_LIST(abrg_Partner)
        Try
            LIST.Clear()

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select abrg_Id From [tblAbrechnung_NCTSBrg] Where [abrg_Partner] =@abrg_Partner order by abrg_GarantiewertBis", conn)
                    cmd.Parameters.AddWithValue("@abrg_Partner", abrg_Partner)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim L As New cAbrechnung_NCTSBrg(dr.Item("abrg_Id"))
                        LIST.Add(L)
                    End While
                    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
End Class
Public Class cAbrechnung_NCTSBrg
    Property abrg_Id As Integer
    Property abrg_Partner As String
    Property abrg_artAbrechnung As String = "LKW"
    Property abrg_GarantiewertBis As Decimal = 0
    Property abrg_bertrag As Decimal = 0
    Property abrg_prozent As Object = Nothing

    Public _ANZAHL As Integer = 0
    Public _BETRAG As Integer = 0

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(abrg_Id)
        Me.abrg_Id = abrg_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("abrg_Id", abrg_Id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("abrg_Partner", abrg_Partner))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("abrg_artAbrechnung", abrg_artAbrechnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("abrg_GarantiewertBis", abrg_GarantiewertBis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("abrg_bertrag", abrg_bertrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("abrg_prozent", abrg_prozent))

        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 tblAbrechnung_NCTSBrg  WHERE abrg_Id=@abrg_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 tblAbrechnung_NCTSBrg WHERE abrg_Id=@abrg_Id  ", conn)
                    cmd.Parameters.AddWithValue("@abrg_Id", abrg_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 [tblAbrechnung_NCTSBrg] SET " & str & " WHERE abrg_Id=@abrg_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 tblAbrechnung_NCTSBrg  (" & 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
