
Imports System.Data.SqlClient
Imports System.Reflection
Public Class cPLOSE_Inv_Data

    Property plInv_Id As Integer
    Property plInv_SupplierRechnungsDatum As Object = Nothing
    Property plInv_SupplierRechnungsNr As String
    Property plInv_SupplierCountry As Object = Nothing
    Property plInv_Lieferant As Object = Nothing
    Property plInv_LieferantCode As Object = Nothing
    Property plInv_LieferantUID As Object = Nothing
    Property plInv_RechnungsJahr As Object = Nothing
    Property plInv_Dateiname As Object = Nothing
    Property plInv_Einlesedatum As Object = Nothing
    Property plInv_Datensatztyp As Object = Nothing
    Property plInv_PLOSEKundennummer As Object = Nothing
    Property plInv_Firmenname As Object = Nothing
    Property plInv_Nettobetrag As Object = Nothing
    Property plInv_MWSTBetrag As Object = Nothing
    Property plInv_Bruttobetrag As Object = Nothing
    Property plInv_Services As Object = Nothing
    Property plInv_PdfFileName As Object = Nothing
    Property plInv_daId As Object = Nothing
    Property plInv_tstmp As DateTime = Now





    Public hasEntry = False

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Sub New()

    End Sub

    Sub New(plInv_Id)
        Me.plInv_Id = plInv_Id
        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("plInv_Id", plInv_Id,, True))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_SupplierRechnungsDatum", plInv_SupplierRechnungsDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_SupplierRechnungsNr", plInv_SupplierRechnungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_SupplierCountry", plInv_SupplierCountry))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Lieferant", plInv_Lieferant))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_LieferantCode", plInv_LieferantCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_LieferantUID", plInv_LieferantUID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_RechnungsJahr", plInv_RechnungsJahr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Dateiname", plInv_Dateiname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Einlesedatum", plInv_Einlesedatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Datensatztyp", plInv_Datensatztyp))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_PLOSEKundennummer", plInv_PLOSEKundennummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Firmenname", plInv_Firmenname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Nettobetrag", plInv_Nettobetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_MWSTBetrag", plInv_MWSTBetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Bruttobetrag", plInv_Bruttobetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_Services", plInv_Services))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_PdfFileName", plInv_PdfFileName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_daId", plInv_daId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("plInv_tstmp", plInv_tstmp))

        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 tblPLOSE_Inv_Data  WHERE plInv_Id=@plInv_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

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

