Imports System.Data.SqlClient
Imports System.Reflection

Public Class cAbgaben

    Property [Index] As Integer = -1

    Property Status As Object = Nothing
    Property ZollamtsNr As Object = Nothing
    Property Jahr As Object = Nothing
    Property KennNr As Object = Nothing
    Property Nummer As Object = Nothing
    Property Leitzahl As Object = Nothing
    Property Belegart As Object = Nothing
    Property LeistungsNr As Object = Nothing
    Property Betrag As Object = Nothing
    Property Eingabedatum As Object = Nothing
    Property Abfertigungsdatum As Object = Nothing
    Property Übernahmedatum As Object = Nothing
    Property Bemerkung As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property Bezugsnummer As Object = Nothing
    Property Registriernummer As Object = Nothing
    Property Korrekturnummer As Object = Nothing
    Property AOFD As Object = Nothing
    Property AKTO As Object = Nothing
    Property Fälligkeitsdatum As Object = Nothing
    Property Dateiname As Object = Nothing
    Property Mandant As Object = Nothing
    Property Niederlassung As Object = Nothing
    Property LeistungsBez As Object = Nothing


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(Index)
        If Index IsNot Nothing Then
            Me.Index = Index
            LOAD()
        End If
    End Sub

    Sub New(Registriernummer, Belegart, Betrag)
        LOAD(Registriernummer, Belegart, Betrag)
    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("Index", [Index], , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Status", [Status]))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ZollamtsNr", ZollamtsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Jahr", Jahr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KennNr", KennNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Nummer", Nummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Leitzahl", Leitzahl))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Belegart", Belegart))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsNr", LeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Betrag", Betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eingabedatum", Eingabedatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Abfertigungsdatum", Abfertigungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Übernahmedatum", Übernahmedatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bemerkung", Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachbearbeiter", Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bezugsnummer", Bezugsnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Registriernummer", Registriernummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Korrekturnummer", Korrekturnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AOFD", AOFD))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AKTO", AKTO))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fälligkeitsdatum", Fälligkeitsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Dateiname", Dateiname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Mandant", Mandant))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Niederlassung", Niederlassung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsBez", LeistungsBez))

        Return list
    End Function



    Public Function SAVE(Optional errHinweis = "") As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM Zkteing WITH(updlock,serializable) WHERE [Index]=@Index) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        [Index] = SQL.doSQLVarListID([Index], sqlstr, "FMZOLL", , list,, errHinweis)
        Return [Index] > 0
    End Function

    Shared Function EXISTS(LeistungsNr, Betrag, Registriernummer, AOFD, AKTO, Fälligkeitsdatum) As Boolean
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsNr", LeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Betrag", Betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Registriernummer", Registriernummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AOFD", AOFD))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AKTO", AKTO))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fälligkeitsdatum", Fälligkeitsdatum))
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Dim count = SQL.getValueTxtBySqlVarList("SELECT count(*) FROM Zkteing WHERE LeistungsNr=@LeistungsNr AND Betrag=@Betrag AND Registriernummer=@Registriernummer AND AOFD=@AOFD AND AKTO=@AKTO AND Fälligkeitsdatum=@Fälligkeitsdatum  ", "FMZOLL", list, 0)
        Return (count > 0)
    End Function
    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Zkteing WHERE [Index]=@Index  ", conn)
                    cmd.Parameters.AddWithValue("@Index", [Index])
                    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
                            hasEntry = True
                        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 Sub LOAD(Registriernummer, Belegart, Betrag)
        If Betrag Is Nothing Then Betrag = 0
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Zkteing WHERE Registriernummer=@Registriernummer AND Belegart=@Belegart AND Betrag=@Betrag  ", conn)
                    cmd.Parameters.AddWithValue("@Registriernummer", Registriernummer)
                    cmd.Parameters.AddWithValue("@Belegart", Belegart)
                    cmd.Parameters.AddWithValue("@Betrag", CDbl(Betrag))
                    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
                            hasEntry = True
                        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 [Zkteing] SET " & str & " WHERE [Index]=@Index ")

        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 Zkteing  (" & 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 [Zkteing] WITH(updlock,serializable) WHERE [Index]=" & Me.[Index]
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Shared Function DELETE(id As Integer) As Boolean
        Dim SQL As New SQL
        Dim sqlstr = " DELETE FROM [Zkteing]  WHERE [Index]=" & id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
End Class
