Imports System.Data.SqlClient
Imports System.Reflection

Public Class cLeistungen

    Property LeistungsNr As Object = Nothing
    Property LeistungsBez As Object = Nothing
    Property BezeichnungAusListe As Object = Nothing
    Property BerechnungsartNr As Object = Nothing
    Property Steuerpflichtig As Object = Nothing
    Property SKR As Object = Nothing
    Property Vorlageprovision As Object = Nothing
    Property Kreditaufwendungen As Object = Nothing
    Property FixeTaxe As Object = Nothing
    Property Abfertigungskosten As Object = Nothing
    Property Preissperre As Object = Nothing
    Property Kostenstelle As Object = Nothing
    Property AufschubkontoNr As Object = Nothing
    Property Gl_account As Object = Nothing
    Property Analysis_type As Object = Nothing
    Property Analysis_code As Object = Nothing
    Property SachkontoInlandSteuerpflichtig As Object = Nothing
    Property SachkontoInlandSteuerfrei As Object = Nothing
    Property SachkontoEUSteuerpflichtig As Object = Nothing
    Property SachkontoEUSteuerfrei As Object = Nothing
    Property Sachkonto3Land As Object = Nothing
    Property BS415 As Object = Nothing
    Property BGebUStV As Object = Nothing

    Property IMEX_Gl_account As Object = Nothing
    Property IMEX_SachkontoInlandSteuerpflichtig As Object = Nothing
    Property IMEX_SachkontoInlandSteuerfrei As Object = Nothing
    Property IMEX_SachkontoEUSteuerpflichtig As Object = Nothing
    Property IMEX_SachkontoEUSteuerfrei As Object = Nothing
    Property IMEX_Sachkonto3Land As Object = Nothing

    Dim SQL As New SQL

    Sub New(LeistungsNr)
        Me.LeistungsNr = LeistungsNr
        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("LeistungsNr", LeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsBez", LeistungsBez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BezeichnungAusListe", BezeichnungAusListe))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BerechnungsartNr", BerechnungsartNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Steuerpflichtig", Steuerpflichtig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SKR", SKR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vorlageprovision", Vorlageprovision))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kreditaufwendungen", Kreditaufwendungen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FixeTaxe", FixeTaxe))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Abfertigungskosten", Abfertigungskosten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Preissperre", Preissperre))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kostenstelle", Kostenstelle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AufschubkontoNr", AufschubkontoNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gl_account", Gl_account))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Analysis_type", Analysis_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Analysis_code", Analysis_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SachkontoInlandSteuerpflichtig", SachkontoInlandSteuerpflichtig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SachkontoInlandSteuerfrei", SachkontoInlandSteuerfrei))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SachkontoEUSteuerpflichtig", SachkontoEUSteuerpflichtig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SachkontoEUSteuerfrei", SachkontoEUSteuerfrei))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachkonto3Land", Sachkonto3Land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BS415", BS415))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BGebUStV", BGebUStV))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_Gl_account", IMEX_Gl_account))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_SachkontoInlandSteuerpflichtig", IMEX_SachkontoInlandSteuerpflichtig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_SachkontoInlandSteuerfrei", IMEX_SachkontoInlandSteuerfrei))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_SachkontoEUSteuerpflichtig", IMEX_SachkontoEUSteuerpflichtig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_SachkontoEUSteuerfrei", IMEX_SachkontoEUSteuerfrei))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("IMEX_Sachkonto3Land", IMEX_Sachkonto3Land))
        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 Leistungen WITH(updlock,serializable) WHERE LeistungsNr=@LeistungsNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "FMZOLL", , list)
    End Function

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

        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 Leistungen  (" & 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
