Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Net
Imports System.Reflection

Imports System.Windows.Forms




Public Class cOregonNCTs_DeclarationData

    Public Property id As Integer
    Public Property auto_send As Boolean
    Public Property badge_code As String
    Public Property service_code As String
    Public Property doc_type As String
    Public Property declaration_type As String
    Public Property commercial_ref As String
    Public Property ducr As String
    Public Property mucr As String
    Public Property ducr_part As String
    Public Property declarant_eori As String
    Public Property declarant_name As String
    Public Property declarant_address As String
    Public Property declarant_city_name As String
    Public Property declarant_country_id As String
    Public Property declarant_postcode As String
    Public Property declarant_rep As String
    Public Property export_country_id As String
    Public Property consignor_eori As String
    Public Property consignor_name As String
    Public Property consignor_address As String
    Public Property consignor_city_name As String
    Public Property consignor_country_id As String
    Public Property consignor_postcode As String
    Public Property multiple_consignor As Boolean
    Public Property import_country_id As String
    Public Property consignee_eori As String
    Public Property consignee_name As String
    Public Property consignee_address As String
    Public Property consignee_city_name As String
    Public Property consignee_country_id As String
    Public Property consignee_postcode As String
    Public Property multiple_consignee As Boolean
    Public Property invoice_amount As String
    Public Property invoice_curr As String
    Public Property goods_location As String
    Public Property border_transport_type As String
    Public Property border_vehicle_code As String
    Public Property border_vehicle_coun As String
    Public Property inland_transport_type As String
    Public Property container_no As String
    Public Property gvms_interested As Boolean
    Public Property freight_amount As String
    Public Property freight_curr As String
    Public Property payment_method As String
    Public Property carrier_eori As String
    Public Property carrier_name As String
    Public Property carrier_address As String
    Public Property carrier_city_name As String
    Public Property carrier_country_id As String
    Public Property carrier_postcode As String
    Public Property total_packages As Integer
    Public Property nature_of_transaction As String
    Public Property premises_code As String
    Public Property first_dan_no As String
    Public Property second_dan_no As String
    Public Property insurance_amount As String
    Public Property insurance_curr As String
    Public Property other_amounts As String
    Public Property other_amounts_curr As String
    Public Property apportment_indicator As String
    Public Property total_gross_weight As String
    Public Property total_net_weight As String
    Public Property airfreight_cost As String
    Public Property vat_adjustment As String
    Public Property vat_adjustment_curr As String
    Public Property discount_amount As String
    Public Property discount_curr As String
    Public Property discount_rate As String
    Public Property incoterm As String
    Public Property incoterm_location As String
    Public Property trader_ref As String
    Public Property arrival_transport_type As String
    Public Property guarantee_code As Integer
    Public Property guarantee_reference As String
    Public Property status As String
    Public Property created As DateTime
    Public Property aviso_id As String
    Property parties As New List(Of cOregonNCTs_PartiesAttribute)
    Property traders As New List(Of cOregonNCTs_TraderAdditionsAttribute)
    Property goods As New List(Of cOregonNCTs_GoodsAttribute)


    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean

    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("auto_send", auto_send))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("badge_code", badge_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("service_code", service_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("doc_type", doc_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declaration_type", declaration_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("commercial_ref", commercial_ref))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ducr", ducr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("mucr", mucr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ducr_part", ducr_part))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_eori", declarant_eori))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_name", declarant_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_address", declarant_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_city_name", declarant_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_country_id", declarant_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_postcode", declarant_postcode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarant_rep", declarant_rep))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("export_country_id", export_country_id))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_eori", consignor_eori))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_name", consignor_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_address", consignor_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_city_name", consignor_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_country_id", consignor_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignor_postcode", consignor_postcode))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("multiple_consignor", multiple_consignor))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("import_country_id", import_country_id))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_eori", consignee_eori))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_name", consignee_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_address", consignee_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_city_name", consignee_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_country_id", consignee_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_postcode", consignee_postcode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("multiple_consignee", multiple_consignee))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("invoice_amount", invoice_amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("invoice_curr", invoice_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_location", goods_location))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("border_transport_type", border_transport_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("border_vehicle_code", border_vehicle_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("border_vehicle_coun", border_vehicle_coun))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("inland_transport_type", inland_transport_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("container_no", container_no))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gvms_interested", gvms_interested))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("freight_amount", freight_amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("freight_curr", freight_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("payment_method", payment_method))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_eori", carrier_eori))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_name", carrier_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_address", carrier_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_city_name", carrier_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_country_id", carrier_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("carrier_postcode", carrier_postcode))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("total_packages", total_packages))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nature_of_transaction", nature_of_transaction))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("premises_code", premises_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("first_dan_no", first_dan_no))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("second_dan_no", second_dan_no))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("insurance_amount", insurance_amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("insurance_curr", insurance_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("other_amounts", other_amounts))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("other_amounts_curr", other_amounts_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("apportment_indicator", apportment_indicator))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("total_gross_weight", total_gross_weight))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("total_net_weight", total_net_weight))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("airfreight_cost", airfreight_cost))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vat_adjustment", vat_adjustment))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vat_adjustment_curr", vat_adjustment_curr))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("discount_amount", discount_amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("discount_curr", discount_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("discount_rate", discount_rate))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("incoterm", incoterm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("incoterm_location", incoterm_location))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trader_ref", trader_ref))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("arrival_transport_type", arrival_transport_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("guarantee_code", guarantee_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("guarantee_reference", guarantee_reference))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("status", status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("created", created))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("aviso_id", aviso_id))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id)
        Me.id = Id
        LOAD()
    End Sub

    Shared Function LOADByBezugsNr(bezugsNr As String, loadALL As Boolean) As cOregonNCTs_DeclarationData
        Try
            Dim NCTS As New cOregonNCTs_DeclarationData
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                ' 
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblOregonNCTS_DeclarationData WHERE commercial_ref Like '@bezugsNr' ", conn)
                    cmd.Parameters.AddWithValue("@bezugsNr", bezugsNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In NCTS.getParameterList()
                            Dim propInfo As PropertyInfo = NCTS.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(NCTS, Nothing)
                            Else
                                propInfo.SetValue(NCTS, dr.Item(li.Text))
                            End If

                        Next
                        If loadALL Then
                            NCTS.LOAD_Goods()
                            NCTS.LOAD_Parties()
                            NCTS.LOAD_Traders()
                        End If

                        dr.Close()
                        Return NCTS
                    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
        Return Nothing
    End Function

    Public Function UPDATE_STATUS(status) As Boolean
        Me.status = status
        Return Me.SAVE
    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 tblOregonNCTS_DeclarationData WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 tblOregonNCTS_DeclarationData  (" & 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 SAVE_ALL(Optional errHinweis = "") As Boolean
        If Not SAVE(errHinweis) Then Return False
        If Not SAVE_GOODS() Or Not SAVE_Parties() Or Not SAVE_Traders() Then Return False

        Return True
    End Function

    Public Function LOAD(Optional loadALL As Boolean = True) As Boolean
        Try
            LOAD = False
            If loadALL Then
                parties.Clear()
                goods.Clear()
                traders.Clear()
            End If
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_DeclarationData WHERE Id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", 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
                        If loadALL Then
                            LOAD_Goods()
                            LOAD_Parties()
                            LOAD_Traders()
                        End If
                        LOAD = 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
        Return LOAD
    End Function

    Public Sub LOAD_Parties()
        Try
            parties.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_PartiesAttribute WHERE declarationdata_id=@id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", Me.id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOregonNCTs_PartiesAttribute
                        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
                        parties.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 Sub LOAD_Traders()
        Try
            traders.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_TraderAdditionsAttribute WHERE declarationdata_id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", Me.id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOregonNCTs_TraderAdditionsAttribute()
                        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

                        traders.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 Sub LOAD_Goods()
        Try
            traders.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_GoodsAttribute WHERE declarationdata_id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", Me.id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOregonNCTs_GoodsAttribute()
                        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

                        goods.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 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 [tblOregonNCTS_DeclarationData] 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


    Public Function SAVE_GOODS() As Boolean

        If DELETE_Goods() Then

            For Each p In goods
                p.declarationdata_id = id
                If p.INSERT() Then
                    p.INSERT_WarenpositionPackstuecke(p.id, Me.id)

                Else
                    Return False
                End If
            Next
        Else
            Return False
        End If

        Return True
    End Function



    Public Function SAVE_Parties() As Boolean

        If DELETE_parties() Then

            For Each p In parties
                p.declarationdata_id = id
                If Not p.INSERT() Then
                    Return False
                End If
            Next
        Else
            Return False
        End If
        Return True
    End Function

    Public Function SAVE_Traders() As Boolean

        If DELETE_parties() Then

            For Each p In traders
                p.declarationdata_id = id
                If Not p.INSERT() Then
                    Return False
                End If
            Next
        Else
            Return False
        End If
        Return True
    End Function

    Function DELETE_Goods() As Boolean
        Dim sqlstr = " DELETE FROM tblOregonNCTS_Declarations_GoodsAttribute WHERE declarationdata_id=" & Me.id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Function DELETE_traders() As Boolean
        Dim sqlstr = " DELETE FROM [tblOregonNCTS_Declarations_TraderAdditionsAttribute] WHERE declarationdata_id=" & Me.id & " "
        '  MsgBox(sqlstr)
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Function DELETE_parties() As Boolean
        Dim sqlstr = " DELETE FROM [tblOregonNCTS_Declarations_PartiesAttribute] WHERE declarationdata_id=" & Me.id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

End Class


Public Class cOregonNCTs_GoodsAttribute
    Public Property id As Integer
    Public Property line_no As Integer
    Public Property commodity As String
    Public Property gtip_code As String
    Public Property gtip_language As String
    Public Property brut_wg As Double
    Public Property net_wg As Double
    Public Property sender_name As String
    Public Property sender_address As String
    Public Property sender_city_name As String
    Public Property sender_country_id As String
    Public Property sender_postcode As String
    Public Property sender_eori_code As String
    Public Property consignee_name As String
    Public Property consignee_address As String
    Public Property consignee_city_name As String
    Public Property consignee_country_id As String
    Public Property consignee_postcode As String
    Public Property consignee_eori_code As String
    Public Property invoice_amount As String
    Public Property invoice_curr As String
    Public Property cpc_code As String
    Public Property additional_cpc_code As String
    Public Property container_no As String
    Public Property preferential_country_id As String
    Public Property cus_code As String
    Public Property nature_of_transaction As String
    Public Property additional_commodity_codes As String
    Public Property origin_country_id As String
    Public Property preference_request As String
    Public Property quota_desc As String
    Public Property valuation_method As String
    Public Property valuation_adjustment As String
    Public Property third_quantity_type As String
    Public Property third_quantity As String
    Public Property supplementary_unit As String
    Public Property declarationdata_id As Integer
    Property decl_PAckattributes As New List(Of cOregonNCTs_PacksAttribute)

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean

    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("line_no", line_no))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("commodity", commodity))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gtip_code", gtip_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gtip_language", gtip_language))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brut_wg", brut_wg))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("net_wg", net_wg))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_name", sender_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_address", sender_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_city_name", sender_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_country_id", sender_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_postcode", sender_postcode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sender_eori_code", sender_eori_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_name", consignee_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_address", consignee_address))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_city_name", consignee_city_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_country_id", consignee_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_postcode", consignee_postcode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("consignee_eori_code", consignee_eori_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("invoice_amount", invoice_amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("invoice_curr", invoice_curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("cpc_code", cpc_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("additional_cpc_code", additional_cpc_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("container_no", container_no))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("preferential_country_id", preferential_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("cus_code", cus_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nature_of_transaction", nature_of_transaction))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("additional_commodity_codes", additional_commodity_codes))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("origin_country_id", origin_country_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("preference_request", preference_request))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("quota_desc", quota_desc))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("valuation_method", valuation_method))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("valuation_adjustment", valuation_adjustment))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("third_quantity_type", third_quantity_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("third_quantity", third_quantity))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("supplementary_unit", supplementary_unit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarationdata_id", declarationdata_id))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(id)
        Me.id = id
        LOAD()
    End Sub

    '##########################################################################

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        declarationdata_id = SQL.doSQLVarListID(declarationdata_id, getInsertCmd, "FMZOLL", , list)
            Return declarationdata_id > 0
        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 tblOregonNCTS_Declarations_GoodsAttribute (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_GoodsAttribute WHERE Id=@Id", conn)
                    cmd.Parameters.AddWithValue("@Id", 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 Sub LOAD_PackAttributes()
        Try
            decl_PAckattributes.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_PacksAttribute WHERE goods_attribute_Posid=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", Me.id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOregonNCTs_PacksAttribute()
                        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
                        decl_PAckattributes.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_WarenpositionPackstuecke(wp_id, id) As Boolean

        For Each p In decl_PAckattributes
            p.goods_attribute_Posid = wp_id
            p.goods_attribute_id = id
            If Not p.INSERT() Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Function DELETE_PackAttributes() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblOregonNCTS_Declarations_PacksAttribute] WHERE goods_attribute_id=" & Me.declarationdata_id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function




    '#######################################################################



    Shared Sub LOAD_LIST_ByNCTS(ByRef GA_LIST As List(Of cOregonNCTs_GoodsAttribute), tco_nctsId As Integer)
        If GA_LIST Is Nothing Then GA_LIST = New List(Of cOregonNCTs_GoodsAttribute)
        GA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_GoodsAttribute WHERE AtezNCTS_Id=" & tco_nctsId, "FMZOLL").Rows
            GA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_GoodsAttribute(r("id")))
        Next
    End Sub

End Class

Public Class cOregonNCTs_PacksAttribute

    Public Property Id As Integer
    Public Property goods_attribute_id As Integer
    Public Property goods_attribute_Posid As Integer
    Public Property pack_count As Integer
    Public Property pack_type As String
    Public Property notes As String

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean


    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_attribute_id", goods_attribute_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_attribute_Posid", goods_attribute_Posid))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pack_count", pack_count))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pack_type", pack_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("notes", notes))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id)
        Me.Id = Id
        LOAD()
    End Sub

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        goods_attribute_Posid = SQL.doSQLVarListID(goods_attribute_Posid, getInsertCmd(), "FMZOLL", , list)
        Return goods_attribute_Posid > 0
    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 tblOregonNCTS_Declarations_PacksAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = Id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _Id <= 0 Then Id = IdTMP
        Return Id > 0
    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 [tblOregonNCTS_Declarations_PacksAttribute] 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


    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 tblOregonNCTS_Declarations_PacksAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_PacksAttribute WHERE Id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", 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

    Shared Sub LOAD_LIST_ByNCTS(ByRef PA_LIST As List(Of cOregonNCTs_PacksAttribute), tco_nctsId As Integer)
        If PA_LIST Is Nothing Then PA_LIST = New List(Of cOregonNCTs_PacksAttribute)
        PA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_PacksAttribute WHERE goods_attribute_id=" & tco_nctsId, "FMZOLL").Rows
            PA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_PacksAttribute(r("id")))
        Next
    End Sub


End Class

Public Class cOregonNCTs_DocumentsAttribute

    Public Property id As Integer
    Public Property goods_attribute_id As Integer
    Public Property doc_type As String
    Public Property doc_no As String
    Public Property pre_post As String

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean


    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_attribute_id", goods_attribute_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("doc_type", doc_type))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("doc_no", doc_no))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pre_post", pre_post))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id)
        Me.id = Id
        LOAD()
    End Sub

    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 tblOregonNCTS_Declarations_DocumentsAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
" commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 [tblOregonNCTS_Declarations_DocumentsAttribute] 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


    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 tblOregonNCTS_Declarations_DocumentsAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_DocumentsAttribute WHERE Id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", 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

    Shared Sub LOAD_LIST_ByNCTS(ByRef DA_LIST As List(Of cOregonNCTs_DocumentsAttribute), tco_nctsId As Integer)
        If DA_LIST Is Nothing Then DA_LIST = New List(Of cOregonNCTs_DocumentsAttribute)
        DA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_DocumentsAttribute WHERE goods_attribute_id=" & tco_nctsId, "FMZOLL").Rows
            DA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_DocumentsAttribute(r("id")))
        Next
    End Sub

End Class

Public Class cOregonNCTs_ProducedDocumentsAttribute

    Public Property id As Integer
    Public Property goods_attribute_id As Integer
    Public Property code As String
    Public Property reference As String
    Public Property status As String
    Public Property reason As String
    Public Property quantity As Integer

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean


    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_attribute_id", goods_attribute_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("code", code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("reference", reference))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("status", status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("reason", reason))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("quantity", quantity))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id)
        Me.id = Id
        LOAD()
    End Sub

    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 tblOregonNCTS_Declarations_ProducedDocumentsAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 [tblOregonNCTS_Declarations_ProducedDocumentsAttribute] 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


    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 tblOregonNCTS_Declarations_ProducedDocumentsAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_ProducedDocumentsAttribute WHERE Id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", 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

    Shared Sub LOAD_LIST_ByNCTS(ByRef PDA_LIST As List(Of cOregonNCTs_ProducedDocumentsAttribute), tco_nctsId As Integer)
        If PDA_LIST Is Nothing Then PDA_LIST = New List(Of cOregonNCTs_ProducedDocumentsAttribute)
        PDA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_ProducedDocumentsAttribute WHERE goods_attribute_id=" & tco_nctsId, "FMZOLL").Rows
            PDA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_ProducedDocumentsAttribute(r("id")))
        Next
    End Sub
End Class

Public Class cOregonNCTs_AdditionalCodesAttribute

    Public Property id As Integer
    Public Property goods_attribute_id As Integer
    Public Property code As String
    Public Property notes As String

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean

    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("goods_attribute_id", goods_attribute_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("code", code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("notes", notes))


        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id)
        Me.id = Id
        LOAD()
    End Sub

    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 tblOregonNCTS_Declarations_AdditionalCodesAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 [tblOregonNCTS_Declarations_AdditionalCodesAttribute] 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


    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 tblOregonNCTS_Declarations_AdditionalCodesAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_AdditionalCodesAttribute WHERE Id=@Id  ", conn)
                    cmd.Parameters.AddWithValue("@Id", 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

    Shared Sub LOAD_LIST_ByNCTS(ByRef ACA_LIST As List(Of cOregonNCTs_AdditionalCodesAttribute), tco_nctsId As Integer)
        If ACA_LIST Is Nothing Then ACA_LIST = New List(Of cOregonNCTs_AdditionalCodesAttribute)
        ACA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_AdditionalCodesAttribute WHERE goods_attribute_id=" & tco_nctsId, "FMZOLL").Rows
            ACA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_AdditionalCodesAttribute(r("id")))
        Next
    End Sub
End Class


Public Class cOregonNCTs_PartiesAttribute

    Public Property id As Integer
    Public Property declarationdata_id As String
    Public Property party_role As String
    Public Property party_code As String
    Public Property eori_code As String

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean

    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarationdata_id", declarationdata_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("party_role", party_role))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("party_code", party_code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eori_code", eori_code))

        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id, decId)
        Me.id = Id
        Me.declarationdata_id = decId
        LOAD()
    End Sub

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        declarationdata_id = SQL.doSQLVarListID(declarationdata_id, getInsertCmd, "FMZOLL", , list)
        Return declarationdata_id > 0
    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 tblOregonNCTS_Declarations_PartiesAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 [tblOregonNCTS_Declarations_PartiesAttribute] 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


    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 tblOregonNCTS_Declarations_PartiesAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_PartiesAttribute WHERE Id=@Id AND declarationdata_id=@decId ", conn)
                    cmd.Parameters.AddWithValue("@Id", id)
                    cmd.Parameters.AddWithValue("@decId", declarationdata_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

    Shared Sub LOAD_LIST_ByNCTS(ByRef PA_LIST As List(Of cOregonNCTs_PartiesAttribute), declarationdata_id As Integer)
        If PA_LIST Is Nothing Then PA_LIST = New List(Of cOregonNCTs_PartiesAttribute)
        PA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_PartiesAttribute WHERE declarationdata_id=" & declarationdata_id, "FMZOLL").Rows
            PA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_PartiesAttribute(r("id"), declarationdata_id))
        Next
    End Sub

End Class

Public Class cOregonNCTs_TraderAdditionsAttribute

    Public Property id As Integer
    Public Property declarationdata_id As Integer
    Public Property code As String
    Public Property amount As Integer
    Public Property curr As String
    Public Property rate_code As Integer

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim hasEntry As Boolean

    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, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("declarationdata_id", declarationdata_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("code", code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("amount", amount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("curr", curr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("rate_code", rate_code))


        Return list
    End Function

    Sub New()

    End Sub

    Sub New(Id, decId)
        Me.id = Id
        Me.declarationdata_id = decId
        LOAD()
    End Sub

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        declarationdata_id = SQL.doSQLVarListID(declarationdata_id, getInsertCmd, "FMZOLL", , list)
        Return declarationdata_id > 0
    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 tblOregonNCTS_Declarations_TraderAdditionsAttribute WITH(updlock,serializable) WHERE Id=@Id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                           " Else " &
                           " BEGIN " & getInsertCmd() & " END " &
                            " commit tran "

        Dim IdTMP = id
        IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If _id <= 0 Then id = IdTMP
        Return id > 0
    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 [tblOregonNCTS_Declarations_TraderAdditionsAttribute] 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


    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 tblOregonNCTS_Declarations_TraderAdditionsAttribute  (" & 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 Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOregonNCTS_Declarations_TraderAdditionsAttribute WHERE Id=@Id And declarationdata_id=@decId   ", conn)
                    cmd.Parameters.AddWithValue("@Id", id)
                    cmd.Parameters.AddWithValue("@decId", declarationdata_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

    Shared Sub LOAD_LIST_ByNCTS(ByRef TAA_LIST As List(Of cOregonNCTs_TraderAdditionsAttribute), declarationdata_id As Integer)
        If TAA_LIST Is Nothing Then TAA_LIST = New List(Of cOregonNCTs_TraderAdditionsAttribute)
        TAA_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT id FROM tblOregonNCTS_Declarations_TraderAdditionsAttribute WHERE declarationdata_id=" & declarationdata_id, "FMZOLL").Rows
            TAA_LIST.Add(New VERAG_PROG_ALLGEMEIN.cOregonNCTs_TraderAdditionsAttribute(r("id"), declarationdata_id))
        Next
    End Sub
End Class


Public Class cModalAPI

    '  Dim ENS As New cModalENS.NctsJsonData


    'DEV
    Shared API_STRING As String = "https://modaltrans.com"
    Shared token As String = ""
    Public dataTable As New DataTable()
    Shared jsonData As New Chilkat.JsonObject


    Shared Function SendRequestAuthentificationToken(ByVal myuri As String, ByVal contentType As String, ByVal method As String, ByRef Itoken As String) As String
        Try
            If Itoken <> "" Then Return "200"

            VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()

            Dim rest As New Chilkat.Rest
            Dim success As Boolean


            Dim bTls As Boolean = True
            Dim port As Integer = 443
            Dim bAutoReconnect As Boolean = True
            success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
            If (success <> True) Then
                Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
                Debug.WriteLine(rest.LastErrorText)
                Return rest.LastErrorText

            End If


            Dim json As New Chilkat.JsonObject
            json.UpdateString("password", "Verag2023")
            json.UpdateString("email", "mdlapi@verag.ag")


            rest.AddHeader("Content-Type", contentType)

            Dim sbRequestBody As New Chilkat.StringBuilder
            json.EmitSb(sbRequestBody)


            Dim sbResponseBody As New Chilkat.StringBuilder
            Dim ResponseStr = rest.FullRequestSb(method, myuri, sbRequestBody, sbResponseBody)


            If (rest.ResponseStatusCode <> 200) Then
                Return rest.ResponseStatusCode & " " & rest.ResponseStatusText
            End If

            Dim jsonResp = New Chilkat.JsonObject()
            jsonResp.LoadSb(sbResponseBody)

            token = jsonResp.StringOf("access_token")
            Itoken = token

            Return rest.ResponseStatusCode
        Catch ex As WebException
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

    End Function

    Shared Function authenticate() As String

        Dim myUri As String = API_STRING & "/api/v1/login"
        Dim response = SendRequestAuthentificationToken(myUri, "application/json", "POST", token)

        Return response


    End Function

    Shared Function SendRequestWithAuthHeader(acceptContentType As String, ByRef failureDesc As String) As String
        Try

            VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()

            Dim rest As New Chilkat.Rest
            Dim success As Boolean


            Dim bTls As Boolean = True
            Dim port As Integer = 443
            Dim bAutoReconnect As Boolean = True
            success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
            If (success <> True) Then
                Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
                Debug.WriteLine(rest.LastErrorText)
                failureDesc = rest.LastErrorText
                Return failureDesc
            End If


            rest.AddHeader("Content-Type", "application/json")
            rest.AddHeader("Authorization", "Bearer " & token)
            rest.AddHeader("Accept", acceptContentType)

            Dim sbRequestBody As New Chilkat.StringBuilder
            jsonData.EmitSb(sbRequestBody)
            Debug.WriteLine(sbRequestBody)

            Dim sbResponseBody As New Chilkat.StringBuilder
            success = rest.FullRequestSb("POST", "api/v1/customs_declarations ", sbRequestBody, sbResponseBody)

            'Dim bdRequest As New Chilkat.BinData
            'success = rest.GetLastDebugRequest(bdRequest)
            'Debug.WriteLine("----")
            'Debug.WriteLine(bdRequest.GetString("utf-8"))
            'Debug.WriteLine("----")



            If (rest.LastMethodSuccess <> True) Then
                Debug.WriteLine(rest.LastErrorText)
                failureDesc = rest.LastErrorText
                Return failureDesc

            Else
                If (rest.ResponseStatusCode <> 200) Then
                    failureDesc = rest.ResponseStatusText
                    Return failureDesc
                Else

                    Dim jsonResult As New Chilkat.JsonObject
                    success = jsonResult.LoadSb(sbResponseBody)
                    jsonResult.Emit()
                    Return jsonResult.StringOf("")

                End If

            End If





        Catch ex As WebException
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Function

    Shared Function setJson(declaration_data As cOregonNCTs_DeclarationData, dgv As DataGridView)

        Dim json As New Chilkat.JsonObject
        Dim success As Boolean
        json.UpdateBool("customs_declaration.declaration_data.auto_send", declaration_data.auto_send)
        json.UpdateString("customs_declaration.declaration_data.badge_code", declaration_data.badge_code)
        json.UpdateString("customs_declaration.declaration_data.service_code", declaration_data.service_code)
        json.UpdateString("customs_declaration.declaration_data.doc_type", declaration_data.doc_type)
        json.UpdateString("customs_declaration.declaration_data.declaration_type", declaration_data.declaration_type)
        json.UpdateString("customs_declaration.declaration_data.commercial_ref", declaration_data.commercial_ref)
        json.UpdateString("customs_declaration.declaration_data.ducr", declaration_data.ducr)
        json.UpdateString("customs_declaration.declaration_data.mucr", declaration_data.mucr)
        json.UpdateString("customs_declaration.declaration_data.ducr_part", declaration_data.ducr_part)
        json.UpdateString("customs_declaration.declaration_data.declarant_eori", declaration_data.carrier_eori)
        json.UpdateString("customs_declaration.declaration_data.declarant_name", declaration_data.declarant_name)
        json.UpdateString("customs_declaration.declaration_data.declarant_address", declaration_data.declarant_address)
        json.UpdateString("customs_declaration.declaration_data.declarant_city_name", declaration_data.declarant_city_name)
        json.UpdateString("customs_declaration.declaration_data.declarant_country_id", declaration_data.declarant_country_id)
        json.UpdateString("customs_declaration.declaration_data.declarant_postcode", declaration_data.declarant_postcode)
        json.UpdateString("customs_declaration.declaration_data.declarant_rep", declaration_data.declarant_rep)
        json.UpdateString("customs_declaration.declaration_data.export_country_id", declaration_data.export_country_id)
        json.UpdateString("customs_declaration.declaration_data.consignor_eori", declaration_data.consignor_eori)
        json.UpdateString("customs_declaration.declaration_data.consignor_name", declaration_data.consignor_name)
        json.UpdateString("customs_declaration.declaration_data.consignor_address", declaration_data.consignor_address)
        json.UpdateString("customs_declaration.declaration_data.consignor_city_name", declaration_data.consignor_city_name)
        json.UpdateString("customs_declaration.declaration_data.consignor_country_id", declaration_data.consignor_country_id)
        json.UpdateString("customs_declaration.declaration_data.consignor_postcode", declaration_data.consignor_postcode)
        json.UpdateBool("customs_declaration.declaration_data.multiple_consignor", declaration_data.multiple_consignor)
        json.UpdateString("customs_declaration.declaration_data.import_country_id", declaration_data.import_country_id)
        json.UpdateString("customs_declaration.declaration_data.consignee_eori", declaration_data.consignee_eori)
        json.UpdateString("customs_declaration.declaration_data.consignee_name", declaration_data.consignee_name)
        json.UpdateString("customs_declaration.declaration_data.consignee_address", declaration_data.consignee_address)
        json.UpdateString("customs_declaration.declaration_data.consignee_city_name", declaration_data.consignee_city_name)
        json.UpdateString("customs_declaration.declaration_data.consignee_country_id", declaration_data.consignee_country_id)
        json.UpdateString("customs_declaration.declaration_data.consignee_postcode", declaration_data.consignee_postcode)
        json.UpdateBool("customs_declaration.declaration_data.multiple_consignee", declaration_data.multiple_consignee)
        json.UpdateString("customs_declaration.declaration_data.invoice_amount", declaration_data.invoice_amount)
        json.UpdateString("customs_declaration.declaration_data.invoice_curr", declaration_data.invoice_curr)
        json.UpdateString("customs_declaration.declaration_data.goods_location", declaration_data.goods_location)
        json.UpdateString("customs_declaration.declaration_data.border_transport_type", declaration_data.border_transport_type)
        json.UpdateString("customs_declaration.declaration_data.border_vehicle_code", declaration_data.border_vehicle_code)
        json.UpdateString("customs_declaration.declaration_data.border_vehicle_coun", declaration_data.border_vehicle_coun)
        json.UpdateString("customs_declaration.declaration_data.inland_transport_type", declaration_data.inland_transport_type)
        json.UpdateString("customs_declaration.declaration_data.container_no", declaration_data.container_no)
        json.UpdateBool("customs_declaration.declaration_data.gvms_interested", declaration_data.gvms_interested)
        json.UpdateString("customs_declaration.declaration_data.freight_amount", declaration_data.freight_amount)
        json.UpdateString("customs_declaration.declaration_data.freight_curr", declaration_data.freight_curr)
        json.UpdateString("customs_declaration.declaration_data.payment_method", declaration_data.payment_method)
        json.UpdateString("customs_declaration.declaration_data.carrier_eori", declaration_data.carrier_eori)
        json.UpdateString("customs_declaration.declaration_data.carrier_name", declaration_data.carrier_name)
        json.UpdateString("customs_declaration.declaration_data.carrier_address", declaration_data.carrier_address)
        json.UpdateString("customs_declaration.declaration_data.carrier_city_name", declaration_data.carrier_city_name)
        json.UpdateString("customs_declaration.declaration_data.carrier_country_id", declaration_data.carrier_country_id)
        json.UpdateString("customs_declaration.declaration_data.carrier_postcode", declaration_data.carrier_postcode)
        json.UpdateInt("customs_declaration.declaration_data.total_packages", declaration_data.total_packages)
        json.UpdateString("customs_declaration.declaration_data.nature_of_transaction", declaration_data.nature_of_transaction)
        json.UpdateString("customs_declaration.declaration_data.premises_code", declaration_data.premises_code)
        json.UpdateString("customs_declaration.declaration_data.first_dan_no", declaration_data.first_dan_no)
        json.UpdateString("customs_declaration.declaration_data.second_dan_no", declaration_data.second_dan_no)
        json.UpdateString("customs_declaration.declaration_data.insurance_amount", declaration_data.insurance_amount)
        json.UpdateString("customs_declaration.declaration_data.insurance_curr", declaration_data.insurance_curr)
        json.UpdateString("customs_declaration.declaration_data.other_amounts", declaration_data.other_amounts)
        json.UpdateString("customs_declaration.declaration_data.other_amounts_curr", declaration_data.other_amounts_curr)
        json.UpdateString("customs_declaration.declaration_data.apportment_indicator", declaration_data.apportment_indicator)
        json.UpdateString("customs_declaration.declaration_data.total_gross_weight", declaration_data.total_gross_weight)
        json.UpdateString("customs_declaration.declaration_data.total_net_weight", declaration_data.total_net_weight)
        json.UpdateString("customs_declaration.declaration_data.airfreight_cost", declaration_data.airfreight_cost)
        json.UpdateString("customs_declaration.declaration_data.vat_adjustment", declaration_data.vat_adjustment)
        json.UpdateString("customs_declaration.declaration_data.vat_adjustment_curr", declaration_data.vat_adjustment_curr)
        json.UpdateString("customs_declaration.declaration_data.discount_amount", declaration_data.discount_amount)
        json.UpdateString("customs_declaration.declaration_data.discount_curr", declaration_data.discount_curr)
        json.UpdateString("customs_declaration.declaration_data.discount_rate", declaration_data.discount_rate)
        json.UpdateString("customs_declaration.declaration_data.incoterm", declaration_data.incoterm)
        json.UpdateString("customs_declaration.declaration_data.incoterm_location", declaration_data.incoterm_location)
        json.UpdateString("customs_declaration.declaration_data.trader_ref", declaration_data.trader_ref)
        json.UpdateString("customs_declaration.declaration_data.arrival_transport_type", declaration_data.arrival_transport_type)
        json.UpdateInt("customs_declaration.declaration_data.guarantee_code", declaration_data.guarantee_code)
        json.UpdateString("customs_declaration.declaration_data.guarantee_reference", declaration_data.guarantee_reference)


        Dim goodsAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_GoodsAttribute)

        VERAG_PROG_ALLGEMEIN.cOregonNCTs_GoodsAttribute.LOAD_LIST_ByNCTS(goodsAttributes_LIST, declaration_data.id)

        For Each GA In goodsAttributes_LIST
            Dim i As Integer = 0

            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].commodity", GA.commodity)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].gtip_code", GA.gtip_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].gtip_language", GA.gtip_language)
            json.UpdateNumber("customs_declaration.declaration_data.goods_attributes[" & i & "].brut_wg", GA.brut_wg)
            json.UpdateNumber("customs_declaration.declaration_data.goods_attributes[" & i & "].net_wg", GA.net_wg)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_name", GA.sender_name)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_address", GA.sender_address)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_city_name", GA.sender_city_name)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_country_id", GA.sender_country_id)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_postcode", GA.sender_postcode)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].sender_eori_code", GA.sender_eori_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_name", GA.consignee_name)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_address", GA.consignee_address)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_city_name", GA.consignee_city_name)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_country_id", GA.consignee_country_id)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_postcode", GA.consignee_postcode)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].consignee_eori_code", GA.consignee_eori_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].invoice_amount", GA.invoice_amount)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].invoice_curr", GA.invoice_curr)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].cpc_code", GA.cpc_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].additional_cpc_code", GA.additional_cpc_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].container_no", GA.container_no)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].preferential_country_id", GA.preferential_country_id)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].cus_code", GA.cus_code)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].nature_of_transaction", GA.nature_of_transaction)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].additional_commodity_codes", GA.additional_commodity_codes)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].origin_country_id", GA.origin_country_id)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].preference_request", GA.preference_request)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].quota_desc", GA.quota_desc)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].valuation_method", GA.valuation_method)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].valuation_adjustment", GA.valuation_adjustment)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].third_quantity_type", GA.third_quantity_type)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].third_quantity", GA.third_quantity)
            json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].supplementary_unit", GA.supplementary_unit)

            Dim packsAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_PacksAttribute)
            VERAG_PROG_ALLGEMEIN.cOregonNCTs_GoodsAttribute.LOAD_LIST_ByNCTS(goodsAttributes_LIST, declaration_data.id)
            For Each PA In packsAttributes_LIST
                Dim j As Integer = 0

                json.UpdateInt("customs_declaration.declaration_data.goods_attributes[" & i & "].packs_attributes[" & j & "].pack_count", PA.pack_count)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].packs_attributes[" & j & "].pack_type", PA.pack_type)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].packs_attributes[" & j & "].notes", PA.notes)
                j = j + 1
            Next


            Dim docAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_DocumentsAttribute)
            VERAG_PROG_ALLGEMEIN.cOregonNCTs_DocumentsAttribute.LOAD_LIST_ByNCTS(docAttributes_LIST, declaration_data.id)
            For Each DA In docAttributes_LIST
                Dim j As Integer = 0


                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].documents_attributes[" & j & "].doc_type", DA.doc_type)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].documents_attributes[" & j & "].doc_no", DA.doc_no)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].documents_attributes[" & j & "].pre_post", DA.pre_post)

                j = j + 1
            Next

            Dim prodocAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_ProducedDocumentsAttribute)
            VERAG_PROG_ALLGEMEIN.cOregonNCTs_ProducedDocumentsAttribute.LOAD_LIST_ByNCTS(prodocAttributes_LIST, declaration_data.id)
            For Each PDA In prodocAttributes_LIST
                Dim j As Integer = 0


                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].produced_documents_attributes[" & j & "].code", PDA.code)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].produced_documents_attributes[" & j & "].reference", PDA.reference)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].produced_documents_attributes[" & j & "].status", PDA.status)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].produced_documents_attributes[" & j & "].reason", PDA.reason)
                json.UpdateInt("customs_declaration.declaration_data.goods_attributes[" & i & "].produced_documents_attributes[" & j & "].quantity", PDA.quantity)

                j = j + 1
            Next


            Dim addcodeAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_AdditionalCodesAttribute)
            VERAG_PROG_ALLGEMEIN.cOregonNCTs_AdditionalCodesAttribute.LOAD_LIST_ByNCTS(addcodeAttributes_LIST, declaration_data.id)
            For Each ACA In addcodeAttributes_LIST
                Dim j As Integer = 0

                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].additional_codes_attributes[" & j & "].code", ACA.code)
                json.UpdateString("customs_declaration.declaration_data.goods_attributes[" & i & "].additional_codes_attributes[" & j & "].notes", ACA.notes)

                j = j + 1
            Next

            i = i + 1
        Next


        Dim partyAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_PartiesAttribute)
        VERAG_PROG_ALLGEMEIN.cOregonNCTs_PartiesAttribute.LOAD_LIST_ByNCTS(partyAttributes_LIST, declaration_data.id)
        For Each PA In partyAttributes_LIST
            Dim j As Integer = 0
            json.UpdateString("customs_declaration.declaration_data.parties_attributes[" & j & "].party_role", PA.party_role)
            json.UpdateString("customs_declaration.declaration_data.parties_attributes[" & j & "].party_code", PA.party_code)
            json.UpdateString("customs_declaration.declaration_data.parties_attributes[" & j & "].eori_code", PA.eori_code)
            j = j + 1
        Next


        Dim tradAddAttributes_LIST As New List(Of VERAG_PROG_ALLGEMEIN.cOregonNCTs_TraderAdditionsAttribute)
        VERAG_PROG_ALLGEMEIN.cOregonNCTs_TraderAdditionsAttribute.LOAD_LIST_ByNCTS(tradAddAttributes_LIST, declaration_data.id)
        For Each TAA In tradAddAttributes_LIST
            Dim j As Integer = 0
            json.UpdateString("customs_declaration.declaration_data.trader_additions_attributes[" & j & "].code", TAA.code)
            json.UpdateInt("customs_declaration.declaration_data.trader_additions_attributes[" & j & "].amount", TAA.amount)
            json.UpdateString("customs_declaration.declaration_data.trader_additions_attributes[" & j & "].curr", TAA.curr)
            json.UpdateInt("customs_declaration.declaration_data.trader_additions_attributes[" & j & "].rate_code", TAA.rate_code)
            j = j + 1
        Next



        Return True

    End Function


End Class


