
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cMDMZinsberechnung
    Property id As Integer
    Property kundenNr As Object = Nothing
    Property belegNr As Object = Nothing
    Property buchungstext As Object = Nothing
    Property kundeZahlungsziel As Object = Nothing
    Property zinssatz As Object = Nothing
    Property belegZahlungsziel As Object = Nothing
    Property ueberschreitungInTagen As Object = Nothing
    Property zinsbetrag As Object = Nothing
    Property timestamp As Object = Nothing
    Property verrechnet As Object = Nothing
    Property belegDatum As Object = Nothing
    Property zahlungDatum As Object = Nothing
    Property zahlungsBetrag As Object = Nothing
    Property zahlungsperiode_start As Object = Nothing
    Property zahlungsperiode_ende As Object = Nothing

    Property faelligkeitsDatum As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(id)
        Me.id = id
        LOAD()
    End Sub

    Sub New(kundennr, BelegNr)
        Me.kundenNr = kundennr
        Me.belegNr = BelegNr
        LOAD(kundennr, BelegNr)

    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("id", id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kundenNr", kundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("belegNr", belegNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("buchungstext", buchungstext))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kundeZahlungsziel", kundeZahlungsziel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zinssatz", zinssatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("belegZahlungsziel", belegZahlungsziel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ueberschreitungInTagen", ueberschreitungInTagen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zinsbetrag", zinsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("timestamp", timestamp))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("verrechnet", verrechnet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("belegDatum", belegDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zahlungDatum", zahlungDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zahlungsBetrag", zahlungsBetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zahlungsperiode_start", zahlungsperiode_start))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zahlungsperiode_ende", zahlungsperiode_ende))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("faelligkeitsDatum", faelligkeitsDatum))


        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 tblMDM_Zinsberechnung  WHERE id=@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 tblMDM_Zinsberechnung WHERE id=@id  ", conn)
                    cmd.Parameters.AddWithValue("@id", 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
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub

    Public Sub LOAD(kundennr, BelegNr)
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblMDM_Zinsberechnung WHERE kundennr=@kundennr and BelegNr=@BelegNr  ", conn)
                    cmd.Parameters.AddWithValue("@kundennr", kundennr)
                    cmd.Parameters.AddWithValue("@BelegNr", BelegNr)
                    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
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 [tblMDM_Zinsberechnung] SET " & str & " WHERE id=@id ")

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 tblMDM_Zinsberechnung  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return ""
    End Function


    Public Shared Function GET_NICHTABGERECHNETE_ZB(ByRef dt As DataTable, KundenNr As Integer) As Boolean
        Try
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()

                Using cmd As New SqlCommand("SELECT id,belegNr,belegDatum,buchungstext,zahlungsBetrag,zahlungDatum, ueberschreitungInTagen, zinssatz,zinsbetrag, zahlungsperiode_start,zahlungsperiode_ende
                                            
                                        FROM 
                                            tblMDM_Zinsberechnung
                                        WHERE 
                                            kundenNr = @KundenNr 
                                            AND zinsbetrag <> 0
                                            AND verrechnet is null
                                            AND left(buchungstext,6) <> 'Zinsen'
                                        ORDER BY 
                                            belegDatum", conn)

                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)

                    Dim dr = cmd.ExecuteReader()
                    dt.Load(dr)
                    'While dr.Read
                    '    dt.Load(dr)
                    'End While
                    dr.Close()

                End Using
            End Using
            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False

    End Function

End Class
