
Imports System.Data.SqlClient
    Imports System.Reflection

'Public Class cVERAG_in_eza_position_FULL
'    Inherits cVERAG_in_eza_position

'    Property shTrPos_id As Integer = ""
'    Property shTrPos_ShTrId As String = ""


'    Sub New()

'    End Sub

'    Dim SQL As New SQL
'End Class

Public Class cVERAG_in_eza_FULL
    '  Inherits cVERAG_in_eza

    Public EZA As cVERAG_in_eza = Nothing

    Property shTr_id As Integer
    Property shTr_Partnersystem As String = ""
    Property trAv_Info As String = ""



    Property shTr_Status As String = ""
    Public Property shTr_TRAvisoId As Object = Nothing


    Sub New(EZA As cVERAG_in_eza)
        setEZA(EZA)
    End Sub

    Public Sub setEZA(EZA As cVERAG_in_eza)
        Me.EZA = EZA
    End Sub
    Sub New(shTr_id As Integer)
        Me.shTr_id = shTr_id
        Me.EZA = New cVERAG_in_eza
        LOAD()
    End Sub

    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("shTr_id", shTr_id, , True, True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Partnersystem", shTr_Partnersystem,,,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Status", shTr_Status,,,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_TRAvisoId", shTr_TRAvisoId,,,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Info", trAv_Info,,,, True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Company", EZA.Company))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Department", EZA.Department))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Reference", EZA.Reference))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DeclarationCountry", EZA.DeclarationCountry))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DeclarationType", EZA.DeclarationType))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TotPackage", EZA.TotPackage))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TotGrossWeight", EZA.TotGrossWeight))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CustomsCodeDeparture", EZA.CustomsCodeDeparture))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CustomsCodeDestination", EZA.CustomsCodeDestination))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CustomsCodeEntry", EZA.CustomsCodeEntry))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TransportModeBorder", EZA.TransportModeBorder))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CountryCodeDeparture", EZA.CountryCodeDeparture))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CountryCodeDestination", EZA.CountryCodeDestination))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DEStateCodeDestination", EZA.DEStateCodeDestination))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TransportArrivalLicensePlate", EZA.TransportArrivalLicensePlate))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TransportArrivalCountryCode", EZA.TransportArrivalCountryCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TransportBorderType", EZA.TransportBorderType))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TransportBorderLicensePlate", EZA.TransportBorderLicensePlate))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Incoterm", EZA.Incoterm))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PrePaperCode", EZA.PrePaperCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PrePaperNr", EZA.PrePaperNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KindOfBusiness", EZA.KindOfBusiness))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("InvoiceAmount", EZA.InvoiceAmount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("InvoiceCurrency", EZA.InvoiceCurrency))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ContainerNr1", EZA.ContainerNr1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ContainerNr2", EZA.ContainerNr2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ContainerNr3", EZA.ContainerNr3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ContainerNr4", EZA.ContainerNr4))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FreightCostsEU", EZA.FreightCostsEU))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FreightCostsNonEU", EZA.FreightCostsNonEU))

        Return list
    End Function


    Function getParameterList_POS(POS As cVERAG_in_eza_position) As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        '  list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPos_id", POS.GoodsTariff, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PosNr", POS.PosNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariff", POS.GoodsTariff))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt1", POS.GoodsTariffExt1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt2", POS.GoodsTariffExt2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt3", POS.GoodsTariffExt3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt4", POS.GoodsTariffExt4))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt5", POS.GoodsTariffExt5))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt6", POS.GoodsTariffExt6))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt7", POS.GoodsTariffExt7))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt8", POS.GoodsTariffExt8))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffExt9", POS.GoodsTariffExt9))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode1", POS.GoodsTariffYCode1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode2", POS.GoodsTariffYCode2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode3", POS.GoodsTariffYCode3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode4", POS.GoodsTariffYCode4))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode5", POS.GoodsTariffYCode5))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode6", POS.GoodsTariffYCode6))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode7", POS.GoodsTariffYCode7))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode8", POS.GoodsTariffYCode8))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode9", POS.GoodsTariffYCode9))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsTariffYCode10", POS.GoodsTariffYCode10))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GoodsDescription", POS.GoodsDescription))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("OriginCountryCode", POS.OriginCountryCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GrossWeight", POS.GrossWeight))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("NetWeight", POS.NetWeight))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("NumberOfPackages", POS.NumberOfPackages))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KindOfPackages", POS.KindOfPackages))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ItemInvoiceAmount", POS.ItemInvoiceAmount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ItemInvoiceCurrency", POS.ItemInvoiceCurrency))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ItemAmount", POS.ItemAmount))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ItemAmountUnitCode", POS.ItemAmountUnitCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Procedure", POS.Procedure))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPos_ShTrId", Me.shTr_id,,,, True))
        Return list
    End Function


    Function getParameterList_DOC(DOC As cVERAG_in_shipment_Document) As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        ' list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPosDoc_id", POS.GoodsTariff, , True, True))

        'list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrDoc_id", Me.shTr_id,,,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrDoc_ShTrId", Me.shTr_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentType", DOC.DocumentType))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentNr", DOC.DocumentNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentDate", DOC.DocumentDate))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPosDoc_docID", DOC.shTrPosDoc_docID))

        Return list

    End Function


    Function getParameterList_ADR(ADR As cVERAG_in_eza_Adressen) As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        '  list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPos_id", POS.GoodsTariff, , True, True))
        '  list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AdressType", ADR.AdressType))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AddressCode", ADR.AddressCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CustomerID", ADR.CustomerID))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("EORI", ADR.EORI))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("NLNR", ADR.NLNR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Name1", ADR.Name1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Name2", ADR.Name2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Name3", ADR.Name3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Street1", ADR.Street1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Street2", ADR.Street2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("City", ADR.City))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CountryCode", ADR.CountryCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PostalCode", ADR.PostalCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TaxNo", ADR.TaxNo))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ContactPerson", ADR.ContactPerson))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Telefon", ADR.Telefon))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Email", ADR.Email))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrAd_ShTrId", Me.shTr_id,,,, True))


        Return list
    End Function


    Function getParameterList_DOC(DOC As cVERAG_in_eza_position_document, ByRef shTrPosDoc_ShTrPosId As Integer) As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        ' list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPosDoc_id", POS.GoodsTariff, , True, True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentType", DOC.DocumentType))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentNr", DOC.DocumentNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentDate", DOC.DocumentDate))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPosDoc_ShTrId", Me.shTr_id,,,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTrPosDoc_ShTrPosId", shTrPosDoc_ShTrPosId,,,, True))
        Return list

    End Function
    Public Function UPDATE_STATUS(status) As Boolean
        Me.shTr_Status = status
        Return Me.SAVE
    End Function

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

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblTR_Shipment WITH(updlock, serializable) WHERE shTr_id=@shTr_id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

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

    Public Function SAVE_ALL() As Boolean
        If EZA Is Nothing Then Return False
        If Not SAVE() Then Return False
        If Not SAVE_Warenposition() Then Return False
        If Not SAVE_Documents() Then Return False
        If Not SAVE_Adressen() Then Return False
        Return True
    End Function

    Public Function LOAD(Optional loadALL As Boolean = True) As Boolean
        Try
            LOAD = False
            If loadALL Then
                'shTr_Unterlagen.Clear()
                EZA.POSITIONS.Clear()

                EZA.ADDRESS.Clear()

            End If
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select * FROM tblTR_Shipment  WHERE shTr_id=@shTr_id  ", conn)
                    cmd.Parameters.AddWithValue("@shTr_id", shTr_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            If Not li.isonlyForSave Then

                                Dim propInfo As PropertyInfo = EZA.GetType.GetProperty(li.Scalarvariable)

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

                                'List.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_id", shTr_id, , True, True, True))
                                'List.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Partnersystem", shTr_Partnersystem,,,, True))
                                'List.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Status", shTr_Status,,,, True))
                            End If
                        Next
                        If loadALL Then
                            LOAD_Warenposition()
                            LOAD_Adressen()
                        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


    Shared Function LOADByReference(Reference As String, Partnersystem As String, loadALL As Boolean) As cVERAG_in_eza_FULL
        Try
            Dim EZA As New cVERAG_in_eza

            Dim EZA_FULL = New cVERAG_in_eza_FULL(EZA)
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblTR_Shipment WHERE shTr_Partnersystem=@Partnersystem and Reference=@Reference  ", conn)
                    cmd.Parameters.AddWithValue("@Partnersystem", Partnersystem)
                    cmd.Parameters.AddWithValue("@Reference", Reference)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In EZA_FULL.getParameterList()
                            Dim propInfo As PropertyInfo = EZA.GetType.GetProperty(li.Scalarvariable)

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

                        Next
                        If loadALL Then
                            EZA_FULL.LOAD_Warenposition()
                            EZA_FULL.LOAD_Adressen()
                        End If

                        dr.Close()
                        Return EZA_FULL
                    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



    Shared Function UPDATE_StatusById(shTr_id As Integer, status As Integer) As String
        If shTr_id > 0 Then
            Try
                Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_id", shTr_id, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Status", status, , True))
                'dy_Status_KEWILL_Equivalent = status
                'dy_Status = status
                Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

                Return SQL.doSQLVarList(" UPDATE [tblTR_Shipment] SET shTr_Status=@shTr_Status WHERE shTr_id=@shTr_id ", "FMZOLL", , list)

            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 If
        Return ""
    End Function



    Public Function UPDATE_Status(status As Integer) As String
        If shTr_id > 0 Then
            Try
                Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_id", shTr_id, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("shTr_Status", status, , True))
                shTr_Status = status

                Return SQL.doSQLVarList(" UPDATE [tblTR_Shipment] SET shTr_Status=@shTr_Status WHERE shTr_id=@shTr_id ", "FMZOLL", , list)

            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 If
        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 [tblTR_Shipment] SET " & str & " WHERE shTr_id=@shTr_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 tblTR_Shipment  (" & 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_Adressen()
        Try
            EZA.ADDRESS.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblTR_Shipment_Adressen WHERE shTrAd_ShTrId=@shTrAd_ShTrId  ", conn)
                    cmd.Parameters.AddWithValue("@shTrAd_ShTrId", Me.shTr_id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cVERAG_in_eza_Adressen
                        For Each i In getParameterList_ADR(l)
                            If Not i.isonlyForSave Then
                                Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                                If dr.Item(i.Text) Is DBNull.Value Or dr.Item(i.Text) Is Nothing Then
                                    propInfo.SetValue(l, Nothing)
                                Else
                                    propInfo.SetValue(l, dr.Item(i.Text))
                                End If
                            End If
                        Next
                        EZA.ADDRESS.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_Warenposition()
        Try
            EZA.POSITIONS.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblTR_Shipment_Position WHERE shTrPos_ShTrId=@shTrPos_ShTrId  ", conn)
                    cmd.Parameters.AddWithValue("@shTrPos_ShTrId", Me.shTr_id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cVERAG_in_eza_position()
                        For Each i In getParameterList_POS(l)
                            If Not i.isonlyForSave Then
                                Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                                If dr.Item(i.Text) Is DBNull.Value Or dr.Item(i.Text) Is Nothing Then
                                    propInfo.SetValue(l, Nothing)
                                Else
                                    propInfo.SetValue(l, dr.Item(i.Text))
                                End If
                            End If
                        Next

                        LOAD_WarenpositionUnterlagen(l, dr.Item("shTrPos_id"))
                        EZA.POSITIONS.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_WarenpositionUnterlagen(ByRef POS As cVERAG_in_eza_position, shTrPos_id As Integer)
        Try
            POS.DOCUMENTS.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblTR_Shipment_Position_Document WHERE shTrPosDoc_ShTrPosId=@shTrPosDoc_ShTrPosId  ", conn)
                    cmd.Parameters.AddWithValue("@shTrPosDoc_ShTrPosId", shTrPos_id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cVERAG_in_eza_position_document
                        For Each i In getParameterList_DOC(l, -1)
                            If Not i.isonlyForSave Then
                                Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                                If dr.Item(i.Text) Is DBNull.Value Or dr.Item(i.Text) Is Nothing Then
                                    propInfo.SetValue(l, Nothing)
                                Else
                                    propInfo.SetValue(l, dr.Item(i.Text))
                                End If
                            End If
                        Next
                        POS.DOCUMENTS.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
    Function DELETE_Warenpositionen() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTR_Shipment_Position] WHERE shTrPos_ShTrId=" & Me.shTr_id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
    Function DELETE_Documents() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTR_Shipment_Document] WHERE [shTrDoc_ShTrId]=" & Me.shTr_id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Function DELETE_WarenpositionUnterlagen() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTR_Shipment_Position_Document] WHERE shTrPosDoc_ShTrId =" & Me.shTr_id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Function DELETE_Adressen() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTR_Shipment_Adressen] WHERE shTrAd_ShTrId =" & Me.shTr_id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Function SAVE_Adressen() As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        If DELETE_Adressen() Then
            'jetzt die Standartofferten  einfügen
            If EZA.ADDRESS IsNot Nothing Then
                For Each p In EZA.ADDRESS
                    Dim posId As Integer
                    If INSERT_Adressen(posId, p) Then

                    Else
                        Return False
                    End If
                Next
            End If
        Else
            Return False
        End If

        Return True
    End Function


    Public Function SAVE_Warenposition() As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        If DELETE_Warenpositionen() And DELETE_WarenpositionUnterlagen() Then
            If EZA.POSITIONS IsNot Nothing Then
                'jetzt die Standartofferten  einfügen
                For Each p In EZA.POSITIONS
                    Dim posId As Integer
                    If INSERT_Position(posId, p) Then
                        SAVE_Warenposition_Documents(p, posId)
                    Else
                        Return False
                    End If
                Next
            End If
        Else
            Return False
        End If

        Return True
    End Function

    Public Function SAVE_Documents() As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        If DELETE_Documents() Then
            If EZA.POSITIONS IsNot Nothing Then
                'jetzt die Standartofferten  einfügen
                For Each p In EZA.DOCUMENTS
                    Dim posId As Integer
                    If INSERT_Documents(posId, p) Then

                    Else
                        Return False
                    End If
                Next
            End If
        Else
            Return False
        End If

        Return True
    End Function


    Public Function SAVE_Warenposition_Documents(POS As cVERAG_in_eza_position, posId As Integer) As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        'jetzt die Standartofferten  einfügen
        If POS.DOCUMENTS IsNot Nothing Then
            For Each p In POS.DOCUMENTS
                Dim posdocId As Integer

                If INSERT_Position_Document(posdocId, p, posId) Then
                Else
                    Return False
                End If
            Next
        End If
        Return True
    End Function

    Public Function INSERT_Adressen(ByRef posId As Integer, ByRef ADR As cVERAG_in_eza_Adressen) As Boolean
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList_ADR(ADR)
            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 SQL.doSQLVarListID(posId, (" INSERT INTO tblTR_Shipment_Adressen (" & str & ") VALUES(" & values & ")  "), "FMZOLL", , list)
        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 False
    End Function

    Public Function INSERT_Position(ByRef posId As Integer, ByRef POS As cVERAG_in_eza_position) As Boolean
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList_POS(POS)
            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 SQL.doSQLVarListID(posId, (" INSERT INTO tblTR_Shipment_Position (" & str & ") VALUES(" & values & ")  "), "FMZOLL", , list)
        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 False
    End Function

    Public Function INSERT_Documents(ByRef docId As Integer, ByRef DOC As cVERAG_in_shipment_Document) As Boolean
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList_DOC(DOC)
            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 SQL.doSQLVarListID(docId, (" INSERT INTO tblTR_Shipment_Document (" & str & ") VALUES(" & values & ")  "), "FMZOLL", , list)
        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 False
    End Function




    Public Function INSERT_Position_Document(ByRef posdocId As Integer, ByRef DOC As cVERAG_in_eza_position_document, posId As Integer) As Boolean
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList_DOC(DOC, posId)
            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. ','



            SQL.doSQL("insert into tbltest (test) Values('TT:: " & (" INSERT INTO tblTR_Shipment_Position_Document (" & str & ") VALUES(" & values & ")  ") & "')", "FMZOLL")

            Return SQL.doSQLVarListID(posdocId, (" INSERT INTO tblTR_Shipment_Position_Document (" & str & ") VALUES(" & values & ")  "), "FMZOLL", , list)
        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 False
    End Function







End Class

'<Serializable()>
Public Class cVERAG_in_eza

    Public Property Company As String
    Public Property Department As String
    Public Property Reference As String

    Public Property DeclarationCountry As String = ""
    Public Property DeclarationType As String = ""

    Public Property TotPackage As Integer = 0
    Public Property TotGrossWeight As Object = Nothing

    Public Property CustomsCodeDeparture As String = "" '                    GesRohmasse
    Public Property CustomsCodeDestination As String = "" '                    GesRohmasse
    Public Property CustomsCodeEntry As String = ""
    Public Property TransportModeBorder As Object = Nothing

    Public Property CountryCodeDeparture As String = ""
    Public Property CountryCodeDestination As String = ""
    Public Property DEStateCodeDestination As String = ""

    Public Property TransportArrivalLicensePlate As String = ""
    Public Property TransportArrivalCountryCode As String = ""
    Public Property TransportBorderType As Object = Nothing
    Public Property TransportBorderLicensePlate As String = ""
    Public Property Incoterm As String = ""

    Public Property PrePaperCode As String = ""
    Public Property PrePaperNr As String = ""
    Public Property KindOfBusiness As Object = Nothing
    Public Property InvoiceAmount As Object = Nothing
    Public Property InvoiceCurrency As String = ""


    Public Property ContainerNr1 As String = ""
    Public Property ContainerNr2 As String = ""
    Public Property ContainerNr3 As String = ""
    Public Property ContainerNr4 As String = ""

    Public Property FreightCostsEU As Object = Nothing
    Public Property FreightCostsNonEU As Object = Nothing


    Public Property POSITIONS As New List(Of cVERAG_in_eza_position)
    Public Property DOCUMENTS As New List(Of cVERAG_in_shipment_Document)

    Public Property ADDRESS As New List(Of cVERAG_in_eza_Adressen)



End Class




Public Class cVERAG_in_eza_Adressen
    Public Property AddressCode As String = ""  '                     VARCHAR (10)  NULL,
    Public Property CustomerID As String = ""  '                     VARCHAR (10)  NULL,
    Public Property EORI As String = ""  '                 VARCHAR (17)  NULL,
    Public Property NLNR As String = ""  '                 DECIMAL (4)   NULL,
    Public Property Name1 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Name2 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Name3 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Street1 As String = ""  '                 VARCHAR (35)  NULL,
    Public Property Street2 As String = ""  '                 VARCHAR (35)  NULL,
    Public Property City As String = ""  '                       VARCHAR (35)  NULL,
    Public Property CountryCode As String = ""  '                       VARCHAR (3)   NULL,
    Public Property PostalCode As String = ""  '                            VARCHAR (9)   NULL,
    Public Property UStID As String = ""  'NICHT MEHR --> TaxNo
    Public Property TaxNo As String = ""  '                  VARCHAR (14)  NULL,
    Public Property ContactPerson As String = ""  '                  VARCHAR (14)  NULL,
    Public Property Telefon As String = ""  '                  VARCHAR (14)  NULL,
    Public Property Email As String = ""  '                  VARCHAR (14)  NULL, 

End Class


Public Class cVERAG_in_eza_position
    Public Property PosNr As Integer = 1
    Public Property GoodsTariff As Object = Nothing
    Public Property GoodsTariffExt1 As Object = Nothing
    Public Property GoodsTariffExt2 As Object = Nothing
    Public Property GoodsTariffExt3 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt4 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt5 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt6 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt7 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt8 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt9 As Object = Nothing 'NICHT MEHR!!!
    Public Property GoodsTariffExt10 As String = "" 'NICHT MEHR!!!
    Public Property GoodsTariffYCode1 As String = ""
    Public Property GoodsTariffYCode2 As String = ""
    Public Property GoodsTariffYCode3 As String = ""
    Public Property GoodsTariffYCode4 As String = ""
    Public Property GoodsTariffYCode5 As String = ""
    Public Property GoodsTariffYCode6 As String = ""
    Public Property GoodsTariffYCode7 As String = ""
    Public Property GoodsTariffYCode8 As String = ""
    Public Property GoodsTariffYCode9 As String = ""
    Public Property GoodsTariffYCode10 As String = ""


    Public Property GoodsDescription As String = ""
    Public Property OriginCountryCode As String = ""
    Public Property GrossWeight As Object = Nothing
    Public Property NetWeight As Object = Nothing
    Public Property NumberOfPackages As Integer = 0
    Public Property KindOfPackages As String = ""
    Public Property ItemInvoiceAmount As Object = Nothing
    Public Property ItemInvoiceCurrency As String = ""
    Public Property ItemAmount As Object = Nothing
    Public Property ItemAmountUnitCode As String = ""
    Public Property Procedure As String = ""

    Public Property DOCUMENTS As New List(Of cVERAG_in_eza_position_document)

    Sub New()

    End Sub
End Class


Public Class cVERAG_in_eza_position_document
    Public Property DocumentType As String = ""
    Public Property DocumentNr As String = ""
    Public Property DocumentDate As Date = CDate("01.01.1800")
End Class


Public Class cVERAG_in_shipment_Document
    Property shTrDoc_id As Integer
    Property shTrDoc_ShTrId As Integer
    Property DocumentType As String = ""
    Property DocumentNr As String = ""
    Property DocumentDate As Date = CDate("01.01.1800")

    Property shTrPosDoc_docID As Object = Nothing


    'Public Function INSERT() As Boolean
    '    Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
    '    'Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    '    shTrDoc_id = SQL.doSQLVarListID(shTrDoc_id, getInsertCmd, "FMZOLL", , list)
    '    Return shTrDoc_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 tblVERAG_in_shippment_Positionsdaten  (" & 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
