Imports System.Data.SqlClient
Imports System.Reflection

Public Class cOffene_Posten



    Property OP_ID As Object = Nothing
    Property Kontonummer As Object = Nothing
    Property Kontoname As Object = Nothing
    Property Belegnummer As Object = Nothing
    Property Belegdatum As Object = Nothing
    Property Fälligkeitsdatum As Object = Nothing
    Property Skontodatum As Object = Nothing
    Property Skontoprozent As Object = Nothing
    Property Bruttobetrag As Object = Nothing
    Property Waehrung As Object = Nothing
    Property Steuersatz As Object = Nothing
    Property Gesamtausstand As Object = Nothing
    Property Steuersatz2 As Object = Nothing
    Property Ausstand2 As Object = Nothing
    Property Buchungstext As Object = Nothing
    Property Bemerkung As Object = Nothing
    Property Mahnstufe As Object = Nothing
    Property Mahndatum As Object = Nothing
    Property Gegenkonto As Object = Nothing
    Property Status As Object = Nothing
    Property KumSaldoEUR As Object = Nothing

    Property Firma_ID As Object = Nothing

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(OP_ID)
        Me.OP_ID = OP_ID
        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("OP_ID", OP_ID, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("OP_ID", OP_ID))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kontonummer", Kontonummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kontoname", Kontoname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Belegnummer", Belegnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Belegdatum", Belegdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fälligkeitsdatum", Fälligkeitsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Skontodatum", Skontodatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Skontoprozent", Skontoprozent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bruttobetrag", Bruttobetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Waehrung", Waehrung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Steuersatz", (Steuersatz))) '.ToString("N2")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gesamtausstand", Gesamtausstand))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Steuersatz2", Steuersatz2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ausstand2", Ausstand2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Buchungstext", Buchungstext))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bemerkung", Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Mahnstufe", Mahnstufe))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Mahndatum", Mahndatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gegenkonto", Gegenkonto))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Status", Status))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KumSaldoEUR", KumSaldoEUR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma_ID", Firma_ID))
        Return list
    End Function

    Public Shared Function DELETE_ALL() As Boolean
        Dim SQL As New SQL
        Return SQL.doSQL("DELETE FROM [Offene Posten]", "FMZOLL")
    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 [Offene Posten] WITH(updlock,serializable) WHERE OP_ID=@OP_ID) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        OP_ID = SQL.doSQLVarListID(OP_ID, sqlstr, "FMZOLL", , list)
        Return OP_ID > 0
    End Function


    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()


        OP_ID = SQL.doSQLVarListID(OP_ID, getInsertCmd(), "FMZOLL", , list)
        Return OP_ID > 0
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [Offene Posten] WHERE OP_ID=@OP_ID  ", conn)
                    cmd.Parameters.AddWithValue("@OP_ID", OP_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

                    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 [Offene Posten] SET " & str & " WHERE OP_ID=@OP_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 [Offene Posten]  (" & 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 [Offene Posten] WITH(updlock,serializable) WHERE OP_ID=" & Me.OP_ID
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


End Class
