Imports System.Data.SqlClient
Imports System.Reflection

Public Class cOffenePostenDVO

    Property OP_ID As Integer
    Property Konto As Integer
    Property KundenNr As Integer
    Property Rechnung As Object = Nothing
    Property Datum As Object = Nothing
    Property Buchungsbetrag As Object = Nothing
    Property Steuercode As Object = Nothing
    Property Ländercode As Object = Nothing
    Property Steuerbetrag As Object = Nothing
    Property Buchungstext As Object = Nothing
    Property Faell_Tage1 As Object = Nothing
    Property Faell_Prozent1 As Object = Nothing
    Property Faell_Tage2 As Object = Nothing
    Property Faell_Prozent2 As Object = Nothing
    Property Faell_Tage3 As Object = Nothing
    Property Faell_Prozent3 As Object = Nothing
    Property Mahnstufe As Object = Nothing
    Property Rechnung_Gutschrift As Object = Nothing
    Property Storno As Object = Nothing
    Property Jahr As Object = Nothing


    Shared 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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Konto", Konto))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KundenNr", KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rechnung", Rechnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Datum", Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Buchungsbetrag", Buchungsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Steuercode", Steuercode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ländercode", Ländercode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Steuerbetrag", Steuerbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Buchungstext", Buchungstext))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Tage1", Faell_Tage1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Prozent1", Faell_Prozent1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Tage2", Faell_Tage2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Prozent2", Faell_Prozent2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Tage3", Faell_Tage3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Faell_Prozent3", Faell_Prozent3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Mahnstufe", Mahnstufe))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rechnung_Gutschrift", Rechnung_Gutschrift))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Storno", Storno))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Jahr", Jahr))

        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 Offene_Posten_DVO 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 Shared Function LOADByKdNr(KundenNr As Integer) As DataTable
        Return SQL.loadDgvBySql("SELECT * FROM Offene_Posten_DVO WHERE KundenNr=@KundenNr ", "FMZOLL")
    End Function

    Public Shared Function DELETE_ALL_ENTRYS() As Boolean
        Return SQL.doSQL("DELETE FROM Offene_Posten_DVO ", "FMZOLL")
    End Function

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