
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cZollArtikel

    Property zollArt_id As Integer
    Property KundenNr As Integer
    Property zollArt_Teilsatzschluessel As Object = Nothing
    Property zollArt_Verarbeitungskennzeichen As Object = Nothing
    Property zollArt_Artikelnummer As Object = Nothing
    Property zollArt_Dynamische_Ergaenzung_1 As Object = Nothing
    Property zollArt_Dynamische_Ergaenzung_2 As Object = Nothing
    Property zollArt_Kundennummer As Object = Nothing
    Property zollArt_Warencodenummer As Object = Nothing
    Property zollArt_Zusatzcode_1 As Object = Nothing
    Property zollArt_Zusatzcode_2 As Object = Nothing
    Property zollArt_Kennzeichen_Gueltig As Object = Nothing
    Property zollArt_Kurzbezeichnung As Object = Nothing
    Property zollArt_Warenbeschreibung As Object = Nothing
    Property zollArt_Statistische_Maßeinheit As Object = Nothing
    Property zollArt_Statistische_Maßeinheit_Qualifikator As Object = Nothing
    Property zollArt_Besondere_Maßeinheit As Object = Nothing
    Property zollArt_Besondere_Maßeinheit_Qualifikator As Object = Nothing
    Property zollArt_Gewicht As Object = Nothing
    Property zollArt_Menge As Object = Nothing
    Property Erfassungsdatum As Object = Nothing
    Property Aenderungsdatum As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property zollArt_Status As Object = Nothing


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(zollArt_id)
        If zollArt_id IsNot Nothing Then
            Me.zollArt_id = zollArt_id
            LOAD()
        End If
    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("zollArt_id", zollArt_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KundenNr", KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Teilsatzschluessel", zollArt_Teilsatzschluessel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Verarbeitungskennzeichen", zollArt_Verarbeitungskennzeichen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Artikelnummer", zollArt_Artikelnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Dynamische_Ergaenzung_1", zollArt_Dynamische_Ergaenzung_1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Dynamische_Ergaenzung_2", zollArt_Dynamische_Ergaenzung_2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Kundennummer", zollArt_Kundennummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Warencodenummer", zollArt_Warencodenummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Zusatzcode_1", zollArt_Zusatzcode_1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Zusatzcode_2", zollArt_Zusatzcode_2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Kennzeichen_Gueltig", zollArt_Kennzeichen_Gueltig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Kurzbezeichnung", zollArt_Kurzbezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Warenbeschreibung", zollArt_Warenbeschreibung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Statistische_Maßeinheit", zollArt_Statistische_Maßeinheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Statistische_Maßeinheit_Qualifikator", zollArt_Statistische_Maßeinheit_Qualifikator))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Besondere_Maßeinheit", zollArt_Besondere_Maßeinheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Besondere_Maßeinheit_Qualifikator", zollArt_Besondere_Maßeinheit_Qualifikator))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Gewicht", zollArt_Gewicht))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Menge", zollArt_Menge))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Erfassungsdatum", Erfassungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Aenderungsdatum", Aenderungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachbearbeiter", Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("zollArt_Status", zollArt_Status))


        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 tblZollArtikel  WHERE zollArt_id=@zollArt_id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select * FROM tblZollArtikel WHERE zollArt_id=@zollArt_id  ", conn)
                    cmd.Parameters.AddWithValue("@zollArt_id", zollArt_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
                            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 tblZollArtikel SET " & str & " WHERE zollArt_id=@zollArt_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 tblZollArtikel  (" & 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 tblZollArtikel WITH(updlock,serializable) WHERE zollArt_id=" & Me.zollArt_id
        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 tblZollArtikel  WHERE zollArt_id=" & id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
End Class
