Imports System.Data.SqlClient
Imports System.Reflection

Public Class cEPBes
    Property ID As Integer
    Property ANMELDUNG1 As Object = Nothing
    Property ANMELDUNG2 As Object = Nothing
    Property VersenderAusführerNr As Object = Nothing
    Property VersenderAusführer As Object = Nothing
    Property Vordrucke1 As Object = Nothing
    Property Vordrucke2 As Object = Nothing
    Property Positionen As Object = Nothing
    Property EmpfängerNr As Object = Nothing
    Property Empfänger As Object = Nothing
    Property Handelsland As Object = Nothing
    Property AnmelderVertreterNr As Object = Nothing
    Property Anmelder As Object = Nothing
    Property Vertreter As Object = Nothing
    Property VersendungsAusfuhrland As Object = Nothing
    Property VersendungsAusfuhrlandCode As Object = Nothing
    Property Ursprungsland As Object = Nothing
    Property BestimmungslandCodeA As Object = Nothing
    Property BestimmungslandCodeB As Object = Nothing
    Property KennzeichenAnkunft As Object = Nothing
    Property StaatszugehörigkeitAnkunft As Object = Nothing
    Property Container As Object = Nothing
    Property LieferbedingungCode As Object = Nothing
    Property Lieferbedingung As Object = Nothing
    Property KennzeichenGrenze As Object = Nothing
    Property StaatszugehörigkeitGrenze As Object = Nothing
    Property Währung As Object = Nothing
    Property Rechnungsbetrag As Object = Nothing
    Property Umrechnungskurs As Object = Nothing
    Property Geschäftsart As Object = Nothing
    Property VerkehrszweigGrenze As Object = Nothing
    Property InländischerVerkehrszweig As Object = Nothing
    Property Entladeort As Object = Nothing
    Property Eingangszollstelle As Object = Nothing
    Property Warenort As Object = Nothing
    Property Packstücke As Object = Nothing
    Property Warenbezeichnung As Object = Nothing
    Property PositionsNr As Object = Nothing
    Property Warennummer1 As Object = Nothing
    Property Warennummer2 As Object = Nothing
    Property Warennummer3 As Object = Nothing
    Property Warennummer4 As Object = Nothing
    Property Warennummer5 As Object = Nothing
    Property UrsprungslandCode As Object = Nothing
    Property Rohmasse As Object = Nothing
    Property Präferenz As Object = Nothing
    Property Verfahren As Object = Nothing
    Property Eigenmasse As Object = Nothing
    Property Kontingent As Object = Nothing
    Property SumAnmeldungVorpapier As Object = Nothing
    Property BesondereMasseinheit As Object = Nothing
    Property Vorsteuerabzug As Object = Nothing
    Property BesondereVermerke As Object = Nothing
    Property BesondereVermerkeZeile1 As Object = Nothing
    Property BesondereVermerkeBetrag1 As Object = Nothing
    Property BesondereVermerkeHinweis1 As Object = Nothing
    Property BesondereVermerkeZeile2 As Object = Nothing
    Property BesondereVermerkeBetrag2 As Object = Nothing
    Property BesondereVermerkeHinweis2 As Object = Nothing
    Property BesondereVermerkeZeile3 As Object = Nothing
    Property BesondereVermerkeBetrag3 As Object = Nothing
    Property BesondereVermerkeHinweis3 As Object = Nothing
    Property StatistischerWert As Object = Nothing
    Property Summe As Object = Nothing
    Property Zahlungsaufschub As Object = Nothing
    Property Lagerbezeichnung As Object = Nothing
    Property AngabenFürVerbuchungszwecke As Object = Nothing
    Property Ort As Object = Nothing
    Property Datum As Object = Nothing
    Property Unterschrift As Object = Nothing
    Property ErstelltDatum As Object = Nothing
    Property ErstelltSachbearbeiter As Object = Nothing
    Property GeändertDatum As Object = Nothing
    Property GeändertSachbearbeiter As Object = Nothing
    Property GedrucktDatum As Object = Nothing
    Property GedrucktSachbearbeiter As Object = Nothing
    Property FilialenNr As Object = Nothing
    Property AbfertigungsNr As Object = Nothing
    Property UnterNr As Object = Nothing
    Property Formular As Object = Nothing
    Property Zulassungsinhaber As Object = Nothing
    Property Bewilligungsnummer As Object = Nothing
    Property Ordnungsnummer As Object = Nothing
    Property Rechnungspositionsbetrag As Object = Nothing
    Property ANMELDUNG3 As Object = Nothing
    Property Ladelisten As Object = Nothing
    Property Bestimmungsland As Object = Nothing
    Property Hauptverpflichteter As Object = Nothing
    Property HauptverpflichteterVertretenDurch As Object = Nothing
    Property HauptverpflichteterOrtDatum As Object = Nothing
    Property Durchgangszollstelle1 As Object = Nothing
    Property Durchgangszollstelle2 As Object = Nothing
    Property Durchgangszollstelle3 As Object = Nothing
    Property Durchgangszollstelle4 As Object = Nothing
    Property Durchgangszollstelle5 As Object = Nothing
    Property Durchgangszollstelle6 As Object = Nothing
    Property Sicherheit As Object = Nothing
    Property SicherheitCode As Object = Nothing
    Property Bestimmungsstelle As Object = Nothing
    Property HauptverpflichteterNr As Object = Nothing
    Property Skontobetrag As Object = Nothing
    Property Artikelpreis As Object = Nothing
    Property Bemessungsgrundlage As Object = Nothing


    Property POS As New List(Of cEPBesPos)

    Dim SQL As New SQL


    Sub New()
    End Sub

    Sub New(ID As Integer)
        Me.ID = ID
        LOAD()
    End Sub


    Function getMaxId() As Integer
        ID = SQL.getValueTxtBySql("SELECT ISNULL(MAX(ID),0)+1 FROM EPBes", "FMZOLL")
        Return ID
    End Function

    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)) ' nicht Primary, weil ID nicht Auto vergeben wird
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ANMELDUNG1", ANMELDUNG1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ANMELDUNG2", ANMELDUNG2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VersenderAusführerNr", VersenderAusführerNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VersenderAusführer", VersenderAusführer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vordrucke1", Vordrucke1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vordrucke2", Vordrucke2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Positionen", Positionen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("EmpfängerNr", EmpfängerNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Empfänger", Empfänger))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Handelsland", Handelsland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AnmelderVertreterNr", AnmelderVertreterNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Anmelder", Anmelder))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vertreter", Vertreter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VersendungsAusfuhrland", VersendungsAusfuhrland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VersendungsAusfuhrlandCode", VersendungsAusfuhrlandCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ursprungsland", Ursprungsland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BestimmungslandCodeA", BestimmungslandCodeA))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BestimmungslandCodeB", BestimmungslandCodeB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KennzeichenAnkunft", KennzeichenAnkunft))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("StaatszugehörigkeitAnkunft", StaatszugehörigkeitAnkunft))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Container", Container))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LieferbedingungCode", LieferbedingungCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Lieferbedingung", Lieferbedingung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KennzeichenGrenze", KennzeichenGrenze))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("StaatszugehörigkeitGrenze", StaatszugehörigkeitGrenze))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Währung", Währung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rechnungsbetrag", Rechnungsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Umrechnungskurs", Umrechnungskurs))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Geschäftsart", Geschäftsart))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VerkehrszweigGrenze", VerkehrszweigGrenze))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("InländischerVerkehrszweig", InländischerVerkehrszweig))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Entladeort", Entladeort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eingangszollstelle", Eingangszollstelle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warenort", Warenort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Packstücke", Packstücke))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warenbezeichnung", Warenbezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PositionsNr", PositionsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer1", Warennummer1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer2", Warennummer2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer3", Warennummer3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer4", Warennummer4))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer5", Warennummer5))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UrsprungslandCode", UrsprungslandCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rohmasse", Rohmasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Präferenz", Präferenz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Verfahren", Verfahren))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eigenmasse", Eigenmasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kontingent", Kontingent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SumAnmeldungVorpapier", SumAnmeldungVorpapier))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereMasseinheit", BesondereMasseinheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vorsteuerabzug", Vorsteuerabzug))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerke", BesondereVermerke))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeZeile1", BesondereVermerkeZeile1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeBetrag1", BesondereVermerkeBetrag1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeHinweis1", BesondereVermerkeHinweis1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeZeile2", BesondereVermerkeZeile2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeBetrag2", BesondereVermerkeBetrag2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeHinweis2", BesondereVermerkeHinweis2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeZeile3", BesondereVermerkeZeile3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeBetrag3", BesondereVermerkeBetrag3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerkeHinweis3", BesondereVermerkeHinweis3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("StatistischerWert", StatistischerWert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Summe", Summe))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Zahlungsaufschub", Zahlungsaufschub))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Lagerbezeichnung", Lagerbezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AngabenFürVerbuchungszwecke", AngabenFürVerbuchungszwecke))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ort", Ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Datum", Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Unterschrift", Unterschrift))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErstelltDatum", ErstelltDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErstelltSachbearbeiter", ErstelltSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GeändertDatum", GeändertDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GeändertSachbearbeiter", GeändertSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GedrucktDatum", GedrucktDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GedrucktSachbearbeiter", GedrucktSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FilialenNr", FilialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbfertigungsNr", AbfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UnterNr", UnterNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Formular", Formular))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Zulassungsinhaber", Zulassungsinhaber))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bewilligungsnummer", Bewilligungsnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ordnungsnummer", Ordnungsnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rechnungspositionsbetrag", Rechnungspositionsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ANMELDUNG3", ANMELDUNG3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ladelisten", Ladelisten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bestimmungsland", Bestimmungsland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Hauptverpflichteter", Hauptverpflichteter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("HauptverpflichteterVertretenDurch", HauptverpflichteterVertretenDurch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("HauptverpflichteterOrtDatum", HauptverpflichteterOrtDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle1", Durchgangszollstelle1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle2", Durchgangszollstelle2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle3", Durchgangszollstelle3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle4", Durchgangszollstelle4))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle5", Durchgangszollstelle5))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Durchgangszollstelle6", Durchgangszollstelle6))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sicherheit", Sicherheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SicherheitCode", SicherheitCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bestimmungsstelle", Bestimmungsstelle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("HauptverpflichteterNr", HauptverpflichteterNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Skontobetrag", Skontobetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Artikelpreis", Artikelpreis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bemessungsgrundlage", Bemessungsgrundlage))


        Return list
    End Function

    Public Sub LOAD()
        Try
            POS.Clear()
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM EPBes WHERE ID=@ID ", conn)
                    cmd.Parameters.AddWithValue("@ID", Me.ID)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each i In Me.getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(i.Text))
                            End If
                        Next
                    End If
                    dr.Close()
                    LOAD_LIST()
                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_LIST()
        Try
            POS.Clear()
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM EPBesPOS WHERE EPBes_ID=@ID order by PositionsNr ", conn)
                    cmd.Parameters.AddWithValue("@ID", ID)
                    '  cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cEPBesPos
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        POS.Add(l)

                    End While
                    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 INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    End Function

    Public Function SAVE() As Boolean

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM EPBes WITH(updlock,serializable) WHERE ID=@ID) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        If SQL.doSQLVarList(sqlstr, "FMZOLL", , list) Then

            DELETE_POS()
            For Each p In POS
                p.SAVE(ID)
            Next
            Return True
        End If


        Return False

    End Function


    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [EPBes] WITH(updlock,serializable) WHERE ID=" & Me.ID
        Return SQL.doSQL(sqlstr, "FMZOLL")

    End Function


    Public Function DELETE_POS() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [EPBesPOS] WITH(updlock,serializable) WHERE EPBes_ID=" & Me.ID
        Return SQL.doSQL(sqlstr, "FMZOLL")

    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 EPBes  (" & 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 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 [EPBes] SET " & str & " WHERE ID=@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



End Class

Public Class cEPBesPos


    Property EPBes_ID As Integer

    Property Packstücke As Object = Nothing
    Property Warenbezeichnung As Object = Nothing
    Property PositionsNr As Object = Nothing
    Property Warennummer1 As Object = Nothing
    Property Warennummer2 As Object = Nothing
    Property Warennummer3 As Object = Nothing
    Property Warennummer4 As Object = Nothing
    Property Warennummer5 As Object = Nothing
    Property UrsprungslandCode As Object = Nothing
    Property Rohmasse As Object = Nothing
    Property Präferenz As Object = Nothing
    Property Verfahren As Object = Nothing
    Property Eigenmasse As Object = Nothing
    Property Kontingent As Object = Nothing
    Property SumAnmeldungVorpapier As Object = Nothing
    Property BesondereMasseinheit As Object = Nothing
    Property Vorsteuerabzug As Object = Nothing
    Property Bemessungsgrundlage As Object = Nothing
    Property BesondereVermerke As Object = Nothing



    Dim SQL As New SQL


    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("EPBes_ID", EPBes_ID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Packstücke", Packstücke))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warenbezeichnung", Warenbezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PositionsNr", PositionsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer1", Warennummer1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer2", Warennummer2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer3", Warennummer3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer4", Warennummer4))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Warennummer5", Warennummer5))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UrsprungslandCode", UrsprungslandCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Rohmasse", Rohmasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Präferenz", Präferenz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Verfahren", Verfahren))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eigenmasse", Eigenmasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kontingent", Kontingent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SumAnmeldungVorpapier", SumAnmeldungVorpapier))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereMasseinheit", BesondereMasseinheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vorsteuerabzug", Vorsteuerabzug))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BesondereVermerke", BesondereVermerke))
        Return list
    End Function



    Public Function SAVE(id) As Boolean
        Me.EPBes_ID = id
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Return SQL.doSQLVarList(getInsertCmd(), "FMZOLL", , list)
    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 EPBesPOS  (" & 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

Class cBerechnungsbogen

    Property ID As Object = Nothing
    Property ErstelltDatum As Object = Nothing
    Property ErstelltSachbearbeiter As Object = Nothing
    Property GeändertDatum As Object = Nothing
    Property GeändertSachbearbeiter As Object = Nothing
    Property GedrucktDatum As Object = Nothing
    Property GedrucktSachbearbeiter As Object = Nothing
    Property Index As Object = Nothing
    Property AbsenderKundenNr As Object = Nothing
    Property AbsenderAdresse As Object = Nothing
    Property AbsenderOffertenNr As Object = Nothing
    Property EmpfängerKundenNr As Object = Nothing
    Property EmpfängerAdresse As Object = Nothing
    Property EmpfängerOffertenNr As Object = Nothing
    Property VermittlerKundenNr As Object = Nothing
    Property VermittlerAdresse As Object = Nothing
    Property VermittlerOffertenNr As Object = Nothing
    Property Bemerkungen As Object = Nothing
    Property Vermerk As Object = Nothing
    Property Sperre As Object = Nothing

    Dim SQL As New SQL


    Sub New()
    End Sub

    Sub New(ID As Integer)
        Me.ID = 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("ID", ID)) ', , True)) ' nicht Primary, weil ID nicht Auto vergeben wird
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErstelltDatum", ErstelltDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErstelltSachbearbeiter", ErstelltSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GeändertDatum", GeändertDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GeändertSachbearbeiter", GeändertSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GedrucktDatum", GedrucktDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GedrucktSachbearbeiter", GedrucktSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Index", Index))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbsenderKundenNr", AbsenderKundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbsenderAdresse", AbsenderAdresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbsenderOffertenNr", AbsenderOffertenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("EmpfängerKundenNr", EmpfängerKundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("EmpfängerAdresse", EmpfängerAdresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("EmpfängerOffertenNr", EmpfängerOffertenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VermittlerKundenNr", VermittlerKundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VermittlerAdresse", VermittlerAdresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("VermittlerOffertenNr", VermittlerOffertenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bemerkungen", Bemerkungen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Vermerk", Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sperre", Sperre))

        Return list
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Berechnungsbogen WHERE ID=@ID ", conn)
                    cmd.Parameters.AddWithValue("@ID", Me.ID)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each i In Me.getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(i.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 INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    End Function

    Public Function SAVE() As Boolean

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM Berechnungsbogen WITH(updlock,serializable) WHERE ID=@ID) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

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


    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [Berechnungsbogen] WITH(updlock,serializable) WHERE ID=" & Me.ID
        Return SQL.doSQL(sqlstr, "FMZOLL")

    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 Berechnungsbogen  (" & 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 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 [Berechnungsbogen] SET " & str & " WHERE ID=@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



End Class