Imports com.sun.rowset.internal
Imports VERAG_PROG_ALLGEMEIN

Public Class cZollsysteme_Aktenbeschriftung

    Property Praeferenznachweis As String
    Property Handelsrechnung As String
    Property PräferenzAufRechnung As String
    Property Colli As String
    Property Gewicht As String
    Property num_Colli As Integer = 0
    Property num_Gewicht As Double = 0
    Property Rechnungspreis As String
    Property RechnungspreisohneWahrung As String
    Property Warenbezeichung As String
    Property AbgabenKonto As String
    Property Garantie As String
    Property Absender As String
    Property Empfaenger As String
    Property Anmelder As String
    Property Vertreter As String
    Property Sachbearbeiter As String
    Property VerBestLandZst As String
    Property Vorpapier As String
    Property ATCMRN As String
    Property ABGABEN_EUST As Double = 0
    Property ABGABEN_ZOLL As Double = 0
    Property ANZ_POS As Integer = 0
    Property Zollstelle_Eingang As String = ""
    Property Waehrungscode As String = "EUR"
    Property Incoterms As String = ""
    Property Incoterms_code As String = ""
    Property Incoterms_ort As String = ""
    Property Geschaeftsart As String = ""
    Property isUeT1 As Boolean = False
    Property dtWARENPOS As New DataTable

    Shared SQL As New VERAG_PROG_ALLGEMEIN.SQL

    'Sub New(BezugsNr, Abfertigungsart)
    '    getDataByBezugsnummer(BezugsNr, Abfertigungsart)
    'End Sub

    Function getATAMRNBezugsnummer(BezugsNr As String, Abfertigungsart As String, DY_Ref As String) As String
        If If(DY_Ref, "") <> "" Then
            BezugsNr = DY_Ref
        Else
            If BezugsNr.Length <= 12 Then Return ""
            BezugsNr = BezugsNr & "%"
        End If

        getATAMRNBezugsnummer = ""
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            ' Case "IMEX"
            ' Case "UNISPED"
            Case ""
            Case Else
                Select Case Abfertigungsart
                    Case 38, 26 'AT
                        '    getADFrom_EZOLL 
                        getATAMRNBezugsnummer = load_NCTS004_MRN_FromDAKOSY(BezugsNr, True) 'Neues NCTS Format!
                        If getATAMRNBezugsnummer = "" Then 'Falls doch nicht    
                            getATAMRNBezugsnummer = load_NCTS_MRN_FromDAKOSY(BezugsNr, True) 'Ü-T1
                        End If
                        isUeT1 = (getATAMRNBezugsnummer <> "")
                        Sachbearbeiter = "" ' Bei Ü-T1 soll der SB leer sein, sonst wird dieser evtl. beim Austragen herangezogen...
                    Case 1, 2, 3, 28, 39
                        getATAMRNBezugsnummer = load_EZA_ATA_FromDAKOSY(BezugsNr, True)
                        '... and Systeme

                    Case Else
                        getATAMRNBezugsnummer = load_NCTS004_MRN_FromDAKOSY(BezugsNr, True) 'Neues NCTS Format!
                        If getATAMRNBezugsnummer = "" Then 'Falls doch nicht    
                            getATAMRNBezugsnummer = load_NCTS_MRN_FromDAKOSY(BezugsNr, True) 'Ü-T1
                        End If
                        If getATAMRNBezugsnummer = "" Then 'Falls doch nicht    
                            getATAMRNBezugsnummer = load_EZA_ATA_FromDAKOSY(BezugsNr, True)
                        End If
                End Select
        End Select

    End Function

    Function getDataByBezugsnummer(BezugsNr As String, Abfertigungsart As String, DY_Ref As String, Grenzstelle As Object, Zollsystem_Land As String) As Boolean
        If If(DY_Ref, "") <> "" Then
            BezugsNr = DY_Ref
        Else
            If BezugsNr.Length <= 12 Then Return False
            BezugsNr = BezugsNr & "%"
        End If

        Dim PosNrAngeben = False
        If Grenzstelle IsNot Nothing Then
            Select Case Grenzstelle.ToString
                Case "DTB" : PosNrAngeben = True
                Case Else : PosNrAngeben = False
            End Select
        End If
        '   MsgBox("1")
        getDataByBezugsnummer = False
        'MsgBox("2 " & Abfertigungsart)
        Select Case Abfertigungsart
            Case 38, 26 'AT
                Select Case Zollsystem_Land
                    Case "AT"
                        'eZoll
                        getDataByBezugsnummer = load_EZA_FromEZOLL(BezugsNr, True)
                               ' MsgBox("1")
                    Case "DE"
                        getDataByBezugsnummer = load_EZA_FromDAKOSY(BezugsNr, True)

                    Case Else
                        getDataByBezugsnummer = load_EZA_FromEZOLL(BezugsNr, True)

                        '  MsgBox("3 ")
                End Select

                If Not getDataByBezugsnummer Then
                    '    getADFrom_EZOLL 
                    getDataByBezugsnummer = load_NCTS004_FromDAKOSY(BezugsNr, True, PosNrAngeben) 'Neues NCTS Format!
                    If Not getDataByBezugsnummer Then 'Falls doch nicht    
                        getDataByBezugsnummer = load_NCTS_FromDAKOSY(BezugsNr, True, PosNrAngeben)
                        isUeT1 = (getDataByBezugsnummer)
                        Sachbearbeiter = "" ' Bei Ü-T1 soll der SB leer sein, sonst wird dieser evtl. beim Austragen herangezogen...
                    End If
                End If
            Case 1, 2, 3, 28, 39
                '... and Systeme
                Select Case Zollsystem_Land
                    Case "AT"
                        'eZoll
                        getDataByBezugsnummer = load_EZA_FromEZOLL(BezugsNr, True)

                    Case Else
                        getDataByBezugsnummer = load_EZA_FromDAKOSY(BezugsNr, True)

                End Select
            Case Else
                getDataByBezugsnummer = load_EZA_FromDAKOSY(BezugsNr, True)

                getDataByBezugsnummer = load_NCTS004_FromDAKOSY(BezugsNr, True, PosNrAngeben) 'Neues NCTS Format!
                If Not getDataByBezugsnummer Then 'Falls doch nicht    

                    getDataByBezugsnummer = load_NCTS_FromDAKOSY(BezugsNr, True, PosNrAngeben)
                    If Not getDataByBezugsnummer Then 'Falls doch nicht    
                        getDataByBezugsnummer = load_EZA_FromDAKOSY(BezugsNr, True)
                    End If
                End If
        End Select


    End Function


    Function addIfNotExists(ByRef LIST As List(Of String), addstring As String) As List(Of String)
        If addstring = "" Then Return LIST
        For Each s In LIST
            If s = addstring Then Return LIST
        Next
        LIST.Add(addstring)
        Return LIST
    End Function

    Function listToString(ByRef LIST As List(Of String)) As String
        listToString = ""
        For Each s In LIST
            listToString &= ", " & s
        Next
        If listToString <> "" Then listToString = listToString.Substring(2)
    End Function


    Function load_EZA_ATA_FromDAKOSY(BezugsNr, Optional checkValidStatus = False) As String
        Dim EZA = DAKOSY_Worker.cDakosyEZA.LOADByBezugsNr(BezugsNr, True, checkValidStatus)
        If EZA Is Nothing Then Return ""
        Return If(EZA.eza_ANR, If(EZA.eza_MRN, ""))
    End Function

    Function load_NCTS_MRN_FromDAKOSY(BezugsNr, Optional checkValidStatus = False) As String
        Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut.LOADByBezugsNr(BezugsNr, True, checkValidStatus)
        If NCTS Is Nothing Then Return ""
        Return If(NCTS.ncts_MRN, If(NCTS.ncts_ANR, ""))
    End Function

    Function load_NCTS004_MRN_FromDAKOSY(BezugsNr, Optional checkValidStatus = False) As String
        Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut004.LOADByBezugsNr(BezugsNr, True, checkValidStatus)
        If NCTS Is Nothing Then Return ""
        load_NCTS004_MRN_FromDAKOSY = If(NCTS.ncts_MRN, If(NCTS.ncts_ANR, ""))
        If load_NCTS004_MRN_FromDAKOSY = "" Then load_NCTS004_MRN_FromDAKOSY = NCTS.ncts_ObjectName

    End Function



    Function load_EZA_FromEZOLL(BezugsNr As String, Optional checkValidStatus As Boolean = False) As Boolean

        Try

            Dim EZOLL_SQL As String = "EZOLL"

            '   VERAG_NCTS.Version = "1.0.2" Automaitsch, weil default
            Console.WriteLine("1")


            Dim dEZOLLLRN As DataTable = SQL.loadDgvBySql("SELECT LizenzNr, OperatorID, VorgangID, AnmID, GeaendertAm, GeaendertVon, CRN, LRN" &
                " FROM zsAnmRefs" &
                " WHERE (VorgangID = 'EF') AND (LRN like '" & BezugsNr & "')" &
                " ORDER BY GeaendertAm DESC", EZOLL_SQL)


            If dEZOLLLRN Is Nothing Then Return False
            If dEZOLLLRN.Rows.Count = 0 Then Return False

            Dim AnmID = dEZOLLLRN.Rows(0)("AnmID")




            Dim dEZOLL As DataTable = SQL.loadDgvBySql("SELECT LizenzNr, OperatorID, VorgangID, AnmID, GeaendertAm, GeaendertVon, CRN, LRN" &
                    " FROM zsAnmRefs" &
                    " WHERE (VorgangID = 'EF') AND (AnmID = '" & AnmID & "')" &
                    " ORDER BY GeaendertAm DESC", "EZOLL")

            If dEZOLL Is Nothing Then Return Nothing
            If dEZOLL.Rows.Count = 0 Then Return Nothing

            Console.WriteLine("3")


            'TextBox2.ForeColor = Color.Green
            Dim DR_zsAnmRefs As DataRow = dEZOLL.Rows(0)

            '  LRN = r.Item("LRN")
            If Not IsNull(DR_zsAnmRefs.Item("CRN")) Then ATCMRN = DR_zsAnmRefs.Item("CRN")

            Console.WriteLine("3")

            ' Praeferenznachweis
            ' Handelsrechnung
            ' PräferenzAufRechnung



            '-----------------------------------------------------------
            '-----------------------------------------------------------
            Handelsrechnung = ""

            'Absender
            ' Tabelle zsAnmConorTra lesen.
            Dim DTzsAnmConorTra As DataTable = SQL.loadDgvBySql("SELECT Na, Pst, Cty, Ctry" &
                        " FROM zsAnmConorTra" &
                        " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)
            If DTzsAnmConorTra.Rows().Count > 0 Then
                If Not IsNull(DTzsAnmConorTra.Rows(0).Item("Na")) Then Absender = Strings.Left(DTzsAnmConorTra.Rows(0).Item("Na"), 50)
                If Not IsNull(DTzsAnmConorTra.Rows(0).Item("Ctry")) Then Absender = Strings.Left(Absender & ", " & DTzsAnmConorTra.Rows(0).Item("Ctry"), 50)
                If Not IsNull(DTzsAnmConorTra.Rows(0).Item("Pst")) Then Absender = Strings.Left(Absender & ", " & DTzsAnmConorTra.Rows(0).Item("Pst"), 50)
                If Not IsNull(DTzsAnmConorTra.Rows(0).Item("Cty")) Then Absender = Strings.Left(Absender & ", " & DTzsAnmConorTra.Rows(0).Item("Cty"), 50)
                If Not IsNull(DTzsAnmConorTra.Rows(0).Item("Ctry")) Then VerBestLandZst = DTzsAnmConorTra.Rows(0).Item("Ctry")
            End If


            'Empfänger 
            ' Tabelle zsAnmConeeTra lesen.
            Dim DTzsAnmConeeTra As DataTable = SQL.loadDgvBySql("SELECT Na, Pst, Cty, Ctry" &
                            " FROM zsAnmConeeTra" &
                            " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)
            If DTzsAnmConeeTra.Rows().Count > 0 Then
                If Not IsNull(DTzsAnmConeeTra.Rows(0).Item("Na")) Then Empfaenger = Strings.Left(DTzsAnmConeeTra.Rows(0).Item("Na"), 50)
                If Not IsNull(DTzsAnmConeeTra.Rows(0).Item("Ctry")) Then Empfaenger = Strings.Left(Empfaenger & ", " & DTzsAnmConeeTra.Rows(0).Item("Ctry"), 50)
                If Not IsNull(DTzsAnmConeeTra.Rows(0).Item("Pst")) Then Empfaenger = Strings.Left(Empfaenger & ", " & DTzsAnmConeeTra.Rows(0).Item("Pst"), 50)
                If Not IsNull(DTzsAnmConeeTra.Rows(0).Item("Cty")) Then Empfaenger = Strings.Left(Empfaenger & ", " & DTzsAnmConeeTra.Rows(0).Item("Cty"), 50)
                If Not IsNull(DTzsAnmConeeTra.Rows(0).Item("Ctry")) Then VerBestLandZst &= " --> " & DTzsAnmConeeTra.Rows(0).Item("Ctry")
            End If
            Console.WriteLine("4")
            '' Tabelle zsAnmTransp lesen.
            'Dim DTzsAnmTransp As DataTable = SQL.loadDgvBySql("SELECT DepIdnt" &
            '        " FROM zsAnmTransp" &
            '        " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)
            'If DTzsAnmTransp.Rows().Count > 0 Then
            '    If Not IsNull(DTzsAnmTransp.Rows(0).Item("DepIdnt")) Then LKW_Kennzeichen = Strings.Left(DTzsAnmTransp.Rows(0).Item("DepIdnt"), 25)
            'End If

            '  Dim Währungsschlüssel As String = "900" 'Standard EUR
            ' Tabelle zsAnmValData lesen.
            Dim DTzsAnmValData As DataTable = SQL.loadDgvBySql("SELECT TransacCd,TermDelCd,InvCurr, InvVal" &
                    " FROM zsAnmValData" &
                    " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)
            If DTzsAnmValData.Rows().Count > 0 Then
                'If Not IsNull(DTzsAnmValData.Rows(0).Item("InvCurr")) Then
                '    If DTzsAnmValData.Rows(0).Item("InvCurr") = "EUR" Then
                '        Währungsschlüssel = "900"
                '    Else
                '        Währungsschlüssel = getWaehr(DTzsAnmValData.Rows(0).Item("InvCurr"))
                '    End If
                'End If
                ' fNEF.cbxWaehrung._value = DTzsAnmValData.Rows(0).Item("InvCurr") ' = DLookup("[Währungsschlüssel]", "Währungstabelle", "[Währungscode] = '" & DTzsAnmValData.Rows(0).Item("InvCurr") & "'")
                If Not IsNull(DTzsAnmValData.Rows(0).Item("InvVal")) Then Rechnungspreis = CDbl(DTzsAnmValData.Rows(0).Item("InvVal")) : RechnungspreisohneWahrung = CDbl(DTzsAnmValData.Rows(0).Item("InvVal"))
                If Not IsNull(DTzsAnmValData.Rows(0).Item("InvCurr")) Then Waehrungscode = DTzsAnmValData.Rows(0).Item("InvCurr") : Rechnungspreis &= " " & Waehrungscode
                If Not IsNull(DTzsAnmValData.Rows(0).Item("TermDelCd")) Then Incoterms = DTzsAnmValData.Rows(0).Item("TermDelCd")
                If Not IsNull(DTzsAnmValData.Rows(0).Item("TermDelCd")) Then Incoterms_code = DTzsAnmValData.Rows(0).Item("TermDelCd")
                If Not IsNull(DTzsAnmValData.Rows(0).Item("TransacCd")) Then Geschaeftsart = DTzsAnmValData.Rows(0).Item("TransacCd")



            End If


            ' Tabelle zsAnmHea lesen.
            Dim DTzsAnmHea As DataTable = SQL.loadDgvBySql("SELECT TotPack, TotGross" &
                    " FROM zsAnmHea" &
                    " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)


            If DTzsAnmHea.Rows().Count > 0 Then
                If Not IsNull(DTzsAnmHea.Rows(0).Item("TotPack")) Then Colli = DTzsAnmHea.Rows(0).Item("TotPack")
                If Not IsNull(DTzsAnmHea.Rows(0).Item("TotGross")) Then
                    Me.Gewicht = CDbl(DTzsAnmHea.Rows(0).Item("TotGross")).ToString("N1") & " kg"
                    Me.num_Gewicht = CDbl(DTzsAnmHea.Rows(0).Item("TotGross")).ToString("N1")

                End If
            End If

            'Dim pkAnz = SQL.DLookup("sum(cast(Nr as float))", "[zsAnmGdsItemPack]", "(LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL, "")



            ' Tabelle zsAnmEntryCO lesen.
            Dim DTAnmEntryCO As DataTable = SQL.loadDgvBySql("SELECT Ref" &
                    " FROM zsAnmEntryCO" &
                    " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", EZOLL_SQL)

            If DTAnmEntryCO.Rows().Count > 0 Then
                If Not IsNull(DTAnmEntryCO.Rows(0).Item("Ref")) Then Zollstelle_Eingang = DTAnmEntryCO.Rows(0).Item("Ref")
            End If


            ' Tabelle zsAnmGdsitem lesen.
            Dim DTzsAnmGdsitem As DataTable = SQL.loadDgvBySql("SELECT  GdsItemID,ComCd, GdsDes,Net, ProcCd, Orig, Pref,ItVal" &
                        " FROM zsAnmGdsitem" &
                        " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")" &
                        " ORDER BY GdsItemID", EZOLL_SQL)

            If DTzsAnmGdsitem.Rows().Count > 0 Then

                initDTWarenpos()

                For Each row In DTzsAnmGdsitem.Rows

                    Dim R As DataRow = dtWARENPOS.NewRow
                    If Not IsNull(row.Item("GdsItemID")) Then R("PosNr") = row.Item("GdsItemID")
                    If Not IsNull(row.Item("ComCd")) Then R("WarenNr") = row.Item("ComCd")
                    If Not IsNull(row.Item("GdsDes")) Then R("WarenBez") = row.Item("GdsDes")
                    If Not IsNull(row.Item("ProcCd")) Then R("Verfahren") = row.Item("ProcCd")
                    If Not IsNull(row.Item("Pref")) Then R("Preferenz") = row.Item("Pref")
                    If Not IsNull(row.Item("ItVal")) Then R("PosWert") = row.Item("ItVal")
                    If Not IsNull(row.Item("Net")) Then R("Netto") = row.Item("Net")
                    If Not IsNull(row.Item("Orig")) Then R("Herkunft") = row.Item("Orig")

                    If Not IsNull(row.Item("ComCd")) AndAlso IsNumeric(row.Item("ComCd")) Then
                        R("isCBAM") = cGreendeal_CBAM_Trn.isCBAMTnr(row.Item("ComCd"))
                    End If

                    dtWARENPOS.Rows.Add(R)

                Next

                If Not IsNull(DTzsAnmGdsitem.Rows(0).Item("GdsDes")) Then
                    If IsNull(Warenbezeichung) Then
                        Warenbezeichung = DTzsAnmGdsitem.Rows(0).Item("GdsDes")
                    Else
                        Warenbezeichung = Warenbezeichung & " " & DTzsAnmGdsitem.Rows(0).Item("GdsDes")
                    End If
                End If
                ANZ_POS = DTzsAnmGdsitem.Rows().Count
            End If


            ' Tabelle zsAnmGdsItemDocCerts lesen.
            Dim DTzsAnmGdsItemDocCerts As DataTable = SQL.loadDgvBySql("select isnull(DRef,'') as Referenznummer,isnull(IssD,'') as Datum
                            from zsAnmGdsItemDocCerts where VorgangID='EF' and DocCd IN ('N380','N325') 
                            and AnmID='" & AnmID & "' group by DRef,IssD", EZOLL_SQL)

            If DTzsAnmGdsItemDocCerts IsNot Nothing AndAlso DTzsAnmGdsItemDocCerts.Rows.Count > 0 Then
                For Each row In DTzsAnmGdsItemDocCerts.Rows
                    Handelsrechnung &= row("Datum") & " " & row("Referenznummer") & vbNewLine
                Next
            End If
            Handelsrechnung = Handelsrechnung.Trim

            '' Qualitäts-CRN -->  Tabelle zsAnmGdsItemDocCerts lesen.
            'Dim DTzsAnmGdsQ As DataTable = SQL.loadDgvBySql("Select distinct(DRef) DRef FROM [zsAnmGdsItemDocCerts] WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & " AND DocCd IN ('N002' ,'7246') )", EZOLL_SQL)
            'If DTzsAnmGdsQ IsNot Nothing AndAlso DTzsAnmGdsQ.Rows().Count > 0 Then
            '    If Not IsNull(DTzsAnmGdsQ.Rows(0).Item("DRef")) Then SPEDITIONSBUCH.QKN = DTzsAnmGdsQ.Rows(0).Item("DRef")
            'End If

            Dim ABGABEN_LIST = VERAG_PROG_ALLGEMEIN.cAbgaben.LOAD_LIST(ATCMRN)
            If ABGABEN_LIST IsNot Nothing AndAlso ABGABEN_LIST.Count > 0 Then
                For Each A In ABGABEN_LIST

                    Select Case A.Belegart
                        Case "2EU", "2IG", "5EU" ' "2PE",
                            ABGABEN_EUST += If(A.Betrag, 0)
                        Case "B00" 'EUST
                            ABGABEN_EUST += If(A.Betrag, 0)
                        Case "5EV"   '"5EV"--> EV-KONTO!!
                            ABGABEN_EUST += If(A.Betrag, 0)
                        Case Else
                            ABGABEN_ZOLL += If(A.Betrag, 0)
                    End Select
                Next
            End If

            Console.WriteLine("ABGABEN_EUST: " & ABGABEN_EUST & " /  ABGABEN_ZOLL: " & ABGABEN_ZOLL)

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return False
        End Try


        Return True
        '------------------------------------------------------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------------------------------------------------------


    End Function

    Function IsNull(o) As Boolean
        Try
            If o Is Nothing Then Return True
            If o Is DBNull.Value Then Return True
            If o.ToString.Trim = "" Then Return True
        Catch : End Try
        Return False
    End Function


    Function load_EZA_FromDAKOSY(BezugsNr As String, Optional checkValidStatus As Boolean = False) As Boolean
        Dim EZA = DAKOSY_Worker.cDakosyEZA.LOADByBezugsNr(BezugsNr, True, True)
        If EZA Is Nothing Then Return False
        Return load_EZA_FromDAKOSY(EZA)
    End Function

    'Function checkisCBAM(cbamWarenNr As String) As Boolean
    '    Return
    '    If (Left(cbamWarenNr, 4) = "2523" Or
    '                Left(cbamWarenNr, 4) = "2716" Or
    '                Left(cbamWarenNr, 4) = "2808" Or
    '                Left(cbamWarenNr, 4) = "2814" Or
    '                Left(cbamWarenNr, 4) = "3102" Or
    '                Left(cbamWarenNr, 4) = "3105" Or
    '                Left(cbamWarenNr, 4) = "7201" Or
    '                Left(cbamWarenNr, 4) = "7203" Or
    '                Left(cbamWarenNr, 4) = "7318" Or
    '                Left(cbamWarenNr, 6) = "283421" Or
    '                Left(cbamWarenNr, 6) = "260112" Or
    '                Left(cbamWarenNr, 6) = "720211" Or
    '                Left(cbamWarenNr, 6) = "720219" Or
    '                Left(cbamWarenNr, 6) = "720241" Or
    '                Left(cbamWarenNr, 6) = "720249" Or
    '                Left(cbamWarenNr, 6) = "720260" Or
    '                Left(cbamWarenNr, 6) = "280410" Or
    '                Left(cbamWarenNr, 4) = "7326" Or
    '                Left(cbamWarenNr, 4) = "7601" Or
    '                Left(cbamWarenNr, 4) = "7616" Or
    '                Left(cbamWarenNr, 8) = "25070080" Or
    '                CInt(Left(cbamWarenNr, 4)) <= 7301 And CInt(Left(cbamWarenNr, 4)) >= 7311 Or
    '                CInt(Left(cbamWarenNr, 4)) <= 7205 And CInt(Left(cbamWarenNr, 4)) >= 7229 Or
    '                CInt(Left(cbamWarenNr, 4)) <= 7603 And CInt(Left(cbamWarenNr, 4)) >= 7614) And Not Left(cbamWarenNr, 6) = "310560" Then

    '        Return True

    '    Else

    '        Return False

    '    End If

    'End Function

    Function load_EZA_FromDAKOSY(EZA As DAKOSY_Worker.cDakosyEZA) As Boolean
        Try
            If EZA Is Nothing Then Return False
            EZA.LOAD_Warenposition()

            ' 
            If IsNumeric(If(EZA.eza_GesamtRohmasse, "")) Then Me.Gewicht = CDbl(EZA.eza_GesamtRohmasse).ToString("N1") & " kg" : num_Gewicht = CDbl(EZA.eza_GesamtRohmasse).ToString("N1")
            If IsNumeric(If(EZA.eza_Rechnungspreis, "")) Then Me.Rechnungspreis = CDbl(EZA.eza_Rechnungspreis).ToString("N2")
            'Me.Rechnungspreis &= " " & If(EZA.eza_Rechnungswaehrung, "")
            ATCMRN = If(EZA.eza_ANR, "")

            Me.RechnungspreisohneWahrung = Me.Rechnungspreis
            Me.Rechnungspreis &= " " & If(EZA.eza_Rechnungswaehrung, "")

            'Property Absender As String
            'Property Empfaenger As String
            'Property Anmelder As String

            Dim RG_List As New List(Of String)
            Dim ATR_List As New List(Of String)
            Dim PAR_List As New List(Of String) ' präfAufRg
            Dim Colli As Integer = 0
            Dim cnt As Integer = 0
            If EZA.eza_WARENPOS.Count > 0 Then

                initDTWarenpos()

                Me.Warenbezeichung = EZA.eza_WARENPOS.Count & " POS: "
                Me.Warenbezeichung &= If(EZA.eza_WARENPOS.Count = 1, If(EZA.eza_WARENPOS(0).ezaWP_Warenbezeichnung, ""), "* diverse *")

                For Each POS In EZA.eza_WARENPOS

                    Dim R As DataRow = dtWARENPOS.NewRow
                    If Not IsNull(POS.ezaWP_PositionsNummer) Then R("PosNr") = POS.ezaWP_PositionsNummer
                    If Not IsNull(POS.ezaWP_WarennummerEZT) Then R("WarenNr") = POS.ezaWP_WarennummerEZT
                    If Not IsNull(POS.ezaWP_Warenbezeichnung) Then R("WarenBez") = POS.ezaWP_Warenbezeichnung
                    If Not IsNull(POS.ezaWP_AnmeldeVErfahren) Then R("Verfahren") = POS.ezaWP_AnmeldeVErfahren
                    If Not IsNull(POS.ezaWP_Artikelpreis) Then R("PosWert") = POS.ezaWP_Artikelpreis
                    If Not IsNull(POS.ezaWP_BeguenstigungBenatragtCode) Then R("Preferenz") = POS.ezaWP_BeguenstigungBenatragtCode
                    If Not IsNull(POS.ezaWP_UrsprungslandCode) Then R("Herkunft") = POS.ezaWP_UrsprungslandCode
                    If Not IsNull(POS.ezaWP_Rohmasse) Then R("Netto") = POS.ezaWP_Rohmasse
                    If Not IsNull(POS.ezaWP_WarennummerEZT) AndAlso IsNumeric(POS.ezaWP_WarennummerEZT) Then
                        R("isCBAM") = cGreendeal_CBAM_Trn.isCBAMTnr(POS.ezaWP_WarennummerEZT)

                    End If



                    dtWARENPOS.Rows.Add(R)


                    For Each UL In POS.ezaWP_UNTERLAGEN
                        If UL.ezaWpUl_Art IsNot Nothing Then
                            Select Case UL.ezaWpUl_Art
                                Case "N380", "N325" : addIfNotExists(RG_List, (If(UL.ezaWpUl_DatumAusstellung, "") & " " & If(UL.ezaWpUl_Nummer, "")).trim)
                                 'case "4EEQ" : addIfNotExists(ATR_List, "4EEQ: " & (If(UL.ezaWpUl_DatumAusstellung, "") & " " & If(UL.ezaWpUl_Nummer, "")).trim)
                                Case "N018", "N954" : addIfNotExists(ATR_List, (If(UL.ezaWpUl_DatumAusstellung, "") & " " & If(UL.ezaWpUl_Nummer, "")).trim)
                                Case "N864" : addIfNotExists(PAR_List, (If(UL.ezaWpUl_DatumAusstellung, "") & " " & If(UL.ezaWpUl_Nummer, "")).trim)
                            End Select
                        End If
                    Next
                    If IsNumeric(If(POS.ezaWP_PackstueckAnzahl, "")) Then Colli += POS.ezaWP_PackstueckAnzahl
                    cnt += 1
                Next
            End If
            ANZ_POS = EZA.eza_WARENPOS.Count
            For Each A In EZA.eza_AUFSCHUB
                If A.ezaAfb_PositionsNummer Is Nothing OrElse A.ezaAfb_PositionsNummer <= 0 Then

                    If If(A.ezaAfb_AbgabeArt, "") = "B0000" Then
                        ABGABEN_EUST += CDbl(A.ezaAfb_Abgabenbetrag)
                    Else
                        ABGABEN_ZOLL += CDbl(A.ezaAfb_Abgabenbetrag)
                    End If
                End If
            Next

            num_Colli = Colli
            Me.Colli = Colli & " Pk."

            For Each AD In EZA.eza_ADRESSEN
                If AD.ezaAd_AdressTyp IsNot Nothing Then
                    Dim Anschrift = ""
                    If If(AD.ezaAd_TeilnehmerEORI, "") <> "" Then
                        Anschrift = (SQL.getValueTxtBySql("SELECT Ordnungsbegriff  FROM [Adressen] inner join Kunden on AdressenNr=KundenNr  where EORITIN='" & AD.ezaAd_TeilnehmerEORI & "'", "FMZOLL")).Trim
                        If Anschrift = "" Then Anschrift = AD.ezaAd_TeilnehmerEORI
                    Else
                        Anschrift = If(AD.ezaAd_NameFirma1, "") & " " & If(AD.ezaAd_LandCode, "") & " " & If(AD.ezaAd_Ort, "").ToString.Trim
                    End If


                    Select Case AD.ezaAd_AdressTyp
                        Case "CZ" : Absender = Anschrift'Anschrift --> nicht benötigt
                        Case "CN" : Empfaenger = Anschrift
                        Case "DT" : Anmelder = Anschrift
                        Case "CB" : Vertreter = "" ' Anschrift - -> nicht benötigt
                    End Select
                End If
            Next
            Sachbearbeiter = If(EZA.eza_Bearbeiter, "")

            VerBestLandZst = If(EZA.eza_VersendungsLandCode, "") & " --> " & If(EZA.eza_Bestimmungsland, "")
            If VerBestLandZst.Trim.StartsWith("-->") Then VerBestLandZst = VerBestLandZst.Replace("-->", "")
            If If(EZA.eza_Bestimmungsland, "") = "DE" Then
                If If(EZA.eza_AdressierteZollstelle, "") <> "" Then VerBestLandZst &= ", DE00" & EZA.eza_AdressierteZollstelle
            Else
                If If(EZA.eza_AdressierteZollstelle, "") <> "" Then VerBestLandZst &= ", " & EZA.eza_AdressierteZollstelle
            End If


            Handelsrechnung = listToString(RG_List)
            Praeferenznachweis = listToString(ATR_List)
            PräferenzAufRechnung = listToString(PAR_List)

            Dim AUFSCHUB_List As New List(Of String)
            For Each AB In EZA.eza_AUFSCHUB
                Dim kto = (If(AB.ezaAfb_HZA, "") & If(AB.ezaAfb_KontoNr, "")).Trim
                addIfNotExists(AUFSCHUB_List, kto & "  " & load_KdFromAKto(kto))
            Next

            If EZA.eza_VorpapierArtCode IsNot Nothing AndAlso EZA.eza_VorpapierArtCode <> "" AndAlso EZA.eza_VorpapierArtCode <> "OHNE" Then
                Vorpapier = EZA.eza_VorpapierArtCode
                If If(EZA.eza_VorpapierNr, "") <> "" Then Vorpapier &= ": " & EZA.eza_VorpapierNr
            End If


            AbgabenKonto = listToString(AUFSCHUB_List)

            If EZA.eza_ArtGeschaeftCode IsNot Nothing AndAlso EZA.eza_ArtGeschaeftCode <> "" Then Geschaeftsart = EZA.eza_ArtGeschaeftCode

            If EZA.eza_LieferbedingungCode IsNot Nothing AndAlso EZA.eza_LieferbedingungCode <> "" Then Incoterms = EZA.eza_LieferbedingungCode
            If EZA.eza_LieferbedingungCode IsNot Nothing AndAlso EZA.eza_LieferbedingungCode <> "" Then Incoterms_code = EZA.eza_LieferbedingungCode

            If EZA.eza_LieferbedingungOrt IsNot Nothing AndAlso EZA.eza_LieferbedingungOrt <> "" AndAlso Incoterms <> "" Then Incoterms &= " " & EZA.eza_LieferbedingungOrt
            If EZA.eza_LieferbedingungOrt IsNot Nothing AndAlso EZA.eza_LieferbedingungOrt <> "" AndAlso Incoterms <> "" Then Incoterms_ort = " " & EZA.eza_LieferbedingungOrt

            If EZA.eza_Rechnungswaehrung IsNot Nothing AndAlso EZA.eza_Rechnungswaehrung <> "" Then Waehrungscode = EZA.eza_Rechnungswaehrung

            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return False
        End Try
        Return False
    End Function

    Function load_KdFromAKto(kto) As String
        Return SQL.getValueTxtBySql("SELECT TOP 1 Ordnungsbegriff  FROM [tblKundenAufschubkonten] inner join adressen on adressennr = [kdAkto_KundenNr] where kdAkto_hauptzollamt + kdAkto_kontonummer ='" & kto & "'", "FMZOLL")
    End Function

    Function load_NCTS_FromDAKOSY(BezugsNr As String, Optional checkValidStatus As Boolean = False, Optional PosNrAngeben As Boolean = False) As Boolean
        Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut.LOADByBezugsNr(BezugsNr, True, checkValidStatus)
        If NCTS Is Nothing Then Return False
        Return load_NCTS_FromDAKOSY(NCTS, PosNrAngeben)
    End Function
    Function load_NCTS004_FromDAKOSY(BezugsNr As String, Optional checkValidStatus As Boolean = False, Optional PosNrAngeben As Boolean = False) As Boolean
        Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut004.LOADByBezugsNr(BezugsNr, True, checkValidStatus)
        If NCTS Is Nothing Then Return False
        Return load_NCTS004_FromDAKOSY(NCTS, PosNrAngeben)
    End Function

    Function load_NCTS_FromDAKOSY(NCTS As DAKOSY_Worker.cDakosyNCTSOut, PosNrAngeben As Boolean) As Boolean
        Try
            If NCTS Is Nothing Then Return Nothing
            NCTS.LOAD_Warenposition()

            ' 
            If IsNumeric(If(NCTS.ncts_GesamtRohmasse, "")) Then Me.Gewicht = CDbl(NCTS.ncts_GesamtRohmasse).ToString("N1") & " kg" : num_Gewicht = CDbl(NCTS.ncts_GesamtRohmasse).ToString("N1")

            '        Me.Rechnungspreis &= " " & If(NCTS.NCTS_Rechnungswaehrung, "") 
            'Property Absender As String
            'Property Empfaenger As String
            'Property Anmelder As String

            Dim RG_List As New List(Of String)
            Dim ATR_List As New List(Of String)
            Dim PAR_List As New List(Of String) ' präfAufRg
            Dim Colli As Integer = 0
            Dim cnt As Integer = 0
            Dim Warenwert As Double = 0
            Dim WarenwertWaehrung As String = ""

            If NCTS.ncts_WARENPOS.Count > 0 Then
                Me.Warenbezeichung = NCTS.ncts_WARENPOS.Count & " POS: "
                Me.Warenbezeichung &= If(NCTS.ncts_WARENPOS.Count = 1, If(NCTS.ncts_WARENPOS(0).nctsWP_Warenbezeichnung, ""), "* diverse *")
                For Each POS In NCTS.ncts_WARENPOS
                    For Each UL In POS.nctsWP_UNTERLAGEN
                        If UL.nctsWpUl_Typ IsNot Nothing Then
                            Select Case UL.nctsWpUl_Typ
                                Case "380", "325"
                                    If PosNrAngeben Then
                                        addIfNotExists(RG_List, If(UL.nctsWpUl_Referenz, "") & " (" & POS.nctsWP_PositionsNummer & ")")
                                    Else
                                        addIfNotExists(RG_List, If(UL.nctsWpUl_Referenz, ""))
                                    End If

                            End Select
                        End If
                    Next
                    For Each UL In POS.nctsWP_PACKSTUECKE
                        If IsNumeric(If(UL.nctsWpPk_PackstueckeAnzahl, "")) Then Colli += UL.nctsWpPk_PackstueckeAnzahl
                    Next

                    If cnt = 0 Then WarenwertWaehrung = If(POS.nctsWP_WarenwertWaehrung, "")
                    If IsNumeric(If(POS.nctsWP_Warenwert, "")) Then Warenwert += POS.nctsWP_Warenwert

                    cnt += 1
                Next
            End If
            num_Colli = Colli
            Me.Colli = Colli & " Pk."
            'Me.Rechnungspreis = Warenwert.ToString("N2") & " " & WarenwertWaehrung

            Me.RechnungspreisohneWahrung = Warenwert.ToString("N2")
            Me.Rechnungspreis = Warenwert.ToString("N2") & " " & WarenwertWaehrung


            Absender = (If(NCTS.ncts_Versender_EORI, "") & " " & If(NCTS.ncts_Versender_Name, "") & " " & If(NCTS.ncts_Versender_Land, "") & " " & If(NCTS.ncts_Versender_Ort, "")).ToString.Trim
            Empfaenger = (If(NCTS.ncts_Empfaenger_EORI, "") & " " & If(NCTS.ncts_Empfaenger_Name, "") & " " & If(NCTS.ncts_Empfaenger_Land, "") & " " & If(NCTS.ncts_Empfaenger_Ort, "")).ToString.Trim
            Anmelder = (If(NCTS.ncts_Hauptverpflichteter_EORI, "") & " " & If(NCTS.ncts_Hauptverpflichteter_Name, "") & " " & If(NCTS.ncts_Hauptverpflichteter_Land, "") & " " & If(NCTS.ncts_Hauptverpflichteter_Ort, "")).ToString.Trim
            Sachbearbeiter = If(NCTS.ncts_Bearbeiter, "")


            ATCMRN = If(If(NCTS.ncts_MRN, "") = "", If(NCTS.ncts_ANR, ""), If(NCTS.ncts_MRN, ""))

            VerBestLandZst = If(NCTS.ncts_VersendungsLandCode, "") & " --> "
            If VerBestLandZst.Trim.StartsWith("-->") Then VerBestLandZst = VerBestLandZst.Replace("-->", "")
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand1, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_SumA_BefoerderungsrouteLand1
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand2, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand2
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand3, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand3
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand4, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand4
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand5, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand5
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand6, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand6
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand7, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand7
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand8, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand8
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand9, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand9
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand10, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand10
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand11, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand11
            If If(NCTS.ncts_SumA_BefoerderungsrouteLand12, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_SumA_BefoerderungsrouteLand12
            VerBestLandZst &= " --> " & If(NCTS.ncts_BestimmungslandCode, "") & ", "

            VerBestLandZst &= If(NCTS.ncts_AbgangsDienststellennummer, "") & " --> "
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer1, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer1
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer2, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer2
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer3, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer3
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer4, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer4
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer5, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer5
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer6, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer6
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer7, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer7
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer8, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer8
            If If(NCTS.ncts_DurchgangszollstelleDienststellnummer9, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_DurchgangszollstelleDienststellnummer9

            If If(NCTS.ncts_BestimmungsstelleDienststellennummer, "") <> "" Then VerBestLandZst &= " --> " & NCTS.ncts_BestimmungsstelleDienststellennummer

            VerBestLandZst = VerBestLandZst.Replace(" -->  --> ", " --> ")

            Handelsrechnung = listToString(RG_List)
            Praeferenznachweis = listToString(ATR_List)
            PräferenzAufRechnung = listToString(PAR_List)

            Dim AUFSCHUB_List As New List(Of String)
            Dim GRNBetrag As Double = 0
            For Each AB In NCTS.ncts_SICHERHEITSANGABEN
                addIfNotExists(AUFSCHUB_List, (If(AB.nctsSA_GRN, "")).Trim)
                If IsNumeric(If(AB.nctsSA_AbgabenBetrag, "")) Then GRNBetrag += AB.nctsSA_AbgabenBetrag
            Next

            Garantie = listToString(AUFSCHUB_List)
            Garantie &= ", " & GRNBetrag.ToString("C2")

            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return Nothing
        End Try
        Return False
    End Function

    Function load_NCTS004_FromDAKOSY(NCTS As DAKOSY_Worker.cDakosyNCTSOut004, PosNrAngeben As Boolean) As Boolean
        Try
            '  VERAG_PROG_ALLGEMEIN.cErrorHandler.sendERRORperMail("004", "004", "", "al@verag.ag", "", "")
            If NCTS Is Nothing Then Return Nothing
            NCTS.LOAD_HouseConsignment()

            ' 
            If IsNumeric(If(NCTS.ncts_GrossMass, "")) Then Me.Gewicht = CDbl(NCTS.ncts_GrossMass).ToString("N1") & " kg" : num_Gewicht = CDbl(NCTS.ncts_GrossMass).ToString("N1")



            '        Me.Rechnungspreis &= " " & If(NCTS.NCTS_Rechnungswaehrung, "") 
            'Property Absender As String
            'Property Empfaenger As String
            'Property Anmelder As String

            Dim RG_List As New List(Of String)
            Dim ATR_List As New List(Of String)
            Dim PAR_List As New List(Of String) ' präfAufRg
            Dim Colli As Integer = 0
            Dim cnt As Integer = 0
            Dim Warenwert As Double = 0
            Dim WarenwertWaehrung As String = ""



            If NCTS.ncts_HOUSE_CONSIGNMENT.Count > 0 Then

                Me.Warenbezeichung = NCTS.ncts_HOUSE_CONSIGNMENT.Count & "/" & NCTS.GET_POSCNT & " POS: "

                If NCTS.ncts_HOUSE_CONSIGNMENT(0).nctsWP_CONSIGNMENT_ITEM.Count > 0 Then
                    Me.Warenbezeichung &= If(NCTS.GET_POSCNT = 1, If(NCTS.ncts_HOUSE_CONSIGNMENT(0).nctsWP_CONSIGNMENT_ITEM(0).nctsHCCI_DescriptionOfGoods, ""), "* diverse *")
                End If


                For Each HC In NCTS.ncts_HOUSE_CONSIGNMENT
                    For Each HCCI In HC.nctsWP_CONSIGNMENT_ITEM
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_1_Type, HCCI.nctsHCCI_SupportingDocument_1_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_2_Type, HCCI.nctsHCCI_SupportingDocument_2_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_3_Type, HCCI.nctsHCCI_SupportingDocument_3_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_4_Type, HCCI.nctsHCCI_SupportingDocument_4_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_5_Type, HCCI.nctsHCCI_SupportingDocument_5_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_6_Type, HCCI.nctsHCCI_SupportingDocument_6_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_7_Type, HCCI.nctsHCCI_SupportingDocument_7_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_8_Type, HCCI.nctsHCCI_SupportingDocument_8_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)
                        addULNCTS004(RG_List, PosNrAngeben, HCCI.nctsHCCI_SupportingDocument_9_Type, HCCI.nctsHCCI_SupportingDocument_9_ReferenceNumber, HCCI.nctsHCCI_GoodsItemNumber)

                        If IsNumeric(If(HCCI.nctsHCCI_GoodsValue, "")) Then Warenwert += HCCI.nctsHCCI_GoodsValue

                    Next

                    cnt += 1
                Next
                If NCTS.GET_POSCNT = 1 Then
                    WarenwertWaehrung = If(NCTS.ncts_HOUSE_CONSIGNMENT(0).nctsWP_CONSIGNMENT_ITEM(0).nctsHCCI_CurrencyOfGoodsValue, "")
                End If

                Colli = NCTS.GET_PKCNT

            End If
            num_Colli = Colli
            Me.Colli = Colli & " Pk."
            'Me.Rechnungspreis = Warenwert.ToString("N2") & " " & WarenwertWaehrung

            Me.RechnungspreisohneWahrung = Warenwert.ToString("N2")
            Me.Rechnungspreis = Warenwert.ToString("N2") & " " & WarenwertWaehrung

            Absender = (If(NCTS.ncts_Consignor_IdentificationNumber, "") & " " & If(NCTS.ncts_Consignor_Name, "") & " " & If(NCTS.ncts_Consignor_Country, "") & " " & If(NCTS.ncts_Consignor_City, "")).ToString.Trim
            Empfaenger = (If(NCTS.ncts_Consignee_IdentificationNumber, "") & " " & If(NCTS.ncts_Consignee_Name, "") & " " & If(NCTS.ncts_Consignee_Country, "") & " " & If(NCTS.ncts_Consignee_City, "")).ToString.Trim
            Anmelder = (If(NCTS.ncts_HolderOfTheTransitProcedure_IdentificationNumber, "") & " " & If(NCTS.ncts_HolderOfTheTransitProcedure_Name, "") & " " & If(NCTS.ncts_HolderOfTheTransitProcedure_Country, "") & " " & If(NCTS.ncts_HolderOfTheTransitProcedure_City, "")).ToString.Trim
            Sachbearbeiter = If(NCTS.ncts_Bearbeiter, "")


            ATCMRN = If(If(NCTS.ncts_MRN, "") = "", If(NCTS.ncts_ANR, ""), If(NCTS.ncts_MRN, ""))
            If ATCMRN = "" Then ATCMRN = If(NCTS.ncts_ObjectName, "")

            VerBestLandZst = If(NCTS.ncts_CountryOfDispatch, "") & " --> "
            If VerBestLandZst.Trim.StartsWith("-->") Then VerBestLandZst = VerBestLandZst.Replace("-->", "")
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_1, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_1
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_2, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_2
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_3, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_3
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_4, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_4
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_5, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_5
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_6, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_6
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_7, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_7
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_8, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_8
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_9, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_9
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_10, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_10
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_11, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_11
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_12, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_12
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_13, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_13
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_14, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_14
            If If(NCTS.ncts_CountryOfRoutingOfConsignment_15, "") <> "" Then VerBestLandZst &= "" & NCTS.ncts_CountryOfRoutingOfConsignment_15
            VerBestLandZst &= " --> " & If(NCTS.ncts_CountryOfDestination, "") & ", "

            VerBestLandZst &= If(NCTS.ncts_CustomsOfficeOfDeparture, "") & " --> "
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_1_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_1_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_2_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_2_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_3_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_3_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_4_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_4_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_5_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_5_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_6_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_6_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_7_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_7_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_8_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_8_RefNumber
            If If(NCTS.ncts_CustomsOfficeOfTransitDeclared_9_RefNumber, "") <> "" Then VerBestLandZst &= ", " & NCTS.ncts_CustomsOfficeOfTransitDeclared_9_RefNumber

            If If(NCTS.ncts_CustomsOfficeOfDestinationDeclared, "") <> "" Then VerBestLandZst &= " --> " & NCTS.ncts_CustomsOfficeOfDestinationDeclared

            VerBestLandZst = VerBestLandZst.Replace(" -->  --> ", " --> ")

            Handelsrechnung = listToString(RG_List)
            Praeferenznachweis = listToString(ATR_List)
            PräferenzAufRechnung = listToString(PAR_List)

            Dim AUFSCHUB_List As New List(Of String)
            Dim GRNBetrag As Double = 0
            For Each AB In NCTS.ncts_GUARANTEE
                addIfNotExists(AUFSCHUB_List, (If(AB.nctsGRT_GRN, "")).Trim)
                If IsNumeric(If(AB.nctsGRT_AmountToBeCovered, "")) Then GRNBetrag += AB.nctsGRT_AmountToBeCovered
            Next

            Garantie = listToString(AUFSCHUB_List)
            Garantie &= ", " & GRNBetrag.ToString("C2")

            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return Nothing
        End Try
        Return False
    End Function


    Sub addULNCTS004(ByRef RG_List, PosNrAngeben, SupportingDocument_Type, SupportingDocument_ReferenceNumber, nctsHCCI_GoodsItemNumber)
        Select Case If(SupportingDocument_Type, "")
            Case "N380", "N325"
                If PosNrAngeben Then
                    addIfNotExists(RG_List, If(SupportingDocument_ReferenceNumber, "") & " (" & nctsHCCI_GoodsItemNumber & ")")
                Else
                    addIfNotExists(RG_List, If(SupportingDocument_ReferenceNumber, ""))
                End If
        End Select

    End Sub
    Function load_NCTS_FromDAKOSY(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, ByRef UniqueId As String) As Boolean
        Try
            If DY_TMP Is Nothing Then Return Nothing
            If NCTS_TMP Is Nothing Then Return Nothing
            NCTS_TMP.LOAD_Warenposition()
            num_Colli = If(NCTS_TMP.ncts_GesamtAnzahlPackstuecke, 0)
            Me.Colli = If(NCTS_TMP.ncts_GesamtAnzahlPackstuecke, "") & " Pk."
            If IsNumeric(If(NCTS_TMP.ncts_GesamtRohmasse, "")) Then Me.Gewicht = CDbl(NCTS_TMP.ncts_GesamtRohmasse).ToString("N1") & " kg" : num_Gewicht = CDbl(NCTS_TMP.ncts_GesamtRohmasse).ToString("N1")


            Dim RG_List As New List(Of String)
            Dim ATR_List As New List(Of String)
            Dim NAR_List As New List(Of String)

            For Each POS In NCTS_TMP.ncts_WARENPOS
                For Each UL In POS.nctsWP_UNTERLAGEN
                    If UL.nctsWpUl_Typ = 380 Then addIfNotExists(RG_List, UL.nctsWpUl_Referenz)
                    If UL.nctsWpUl_Typ = 380 Then addIfNotExists(ATR_List, UL.nctsWpUl_Referenz)
                    If UL.nctsWpUl_Typ = 380 Then addIfNotExists(NAR_List, UL.nctsWpUl_Referenz)
                Next
            Next

            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return Nothing
        End Try
        Return False
    End Function

    'Function load_NCTS004_FromDAKOSY(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut004, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, ByRef UniqueId As String) As Boolean
    '    Try
    '        If DY_TMP Is Nothing Then Return Nothing
    '        If NCTS_TMP Is Nothing Then Return Nothing
    '        NCTS_TMP.LOAD_HouseConsignment()
    '        num_Colli = NCTS_TMP.GET_PKCNT
    '        Me.Colli = NCTS_TMP.GET_PKCNT & " Pk."
    '        If IsNumeric(If(NCTS_TMP.ncts_GrossMass, "")) Then Me.Gewicht = CDbl(NCTS_TMP.ncts_GrossMass).ToString("N1") & " kg" : num_Gewicht = CDbl(NCTS_TMP.ncts_GrossMass).ToString("N1")


    '        Dim RG_List As New List(Of String)
    '        Dim ATR_List As New List(Of String)
    '        Dim NAR_List As New List(Of String)

    '        For Each POS In NCTS_TMP.LOAD_HouseConsignment
    '            For Each UL In POS.nctsWP_UNTERLAGEN
    '                If UL.nctsWpUl_Typ = 380 Then addIfNotExists(RG_List, UL.nctsWpUl_Referenz)
    '                If UL.nctsWpUl_Typ = 380 Then addIfNotExists(ATR_List, UL.nctsWpUl_Referenz)
    '                If UL.nctsWpUl_Typ = 380 Then addIfNotExists(NAR_List, UL.nctsWpUl_Referenz)
    '            Next
    '        Next

    '        Return True
    '    Catch ex As Exception
    '        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
    '        Return Nothing
    '    End Try
    '    Return False
    'End Function


    Shared Function genVERAG_OUT_NCTS_FromEZOLLByLRN(LRN As String, ByRef UniqueId As String) As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts

        Dim dEZOLL As DataTable = SQL.loadDgvBySql("SELECT LizenzNr, OperatorID, VorgangID, AnmID, GeaendertAm, GeaendertVon, CRN, LRN" &
                    " FROM zsAnmRefs" &
                    " WHERE (VorgangID = 'AV') AND (LRN = '" & LRN & "')" &
                    " ORDER BY GeaendertAm DESC", "EZOLL")
        If dEZOLL Is Nothing Then Return Nothing
        If dEZOLL.Rows.Count = 0 Then Return Nothing

        Dim r As DataRow = dEZOLL.Rows(0)

        Return genVERAG_OUT_NCTS_FromEZOLLByAnmId(r.Item("AnmID"), UniqueId)
    End Function

    Shared Function genVERAG_OUT_NCTS_FromEZOLLByAnmId(AnmID As String, ByRef UniqueId As String) As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts
        Dim LRN = ""
        Try

            '   VERAG_NCTS.Version = "1.0.2" Automaitsch, weil default

            Dim Bestimmungszollstelle = ""
            Dim Bürgschaft = ""

            Dim dEZOLL As DataTable = SQL.loadDgvBySql("SELECT LizenzNr, OperatorID, VorgangID, AnmID, GeaendertAm, GeaendertVon, CRN, LRN" &
                    " FROM zsAnmRefs" &
                    " WHERE (VorgangID = 'AV') AND (AnmID = '" & AnmID & "')" &
                    " ORDER BY GeaendertAm DESC", "EZOLL")

            If dEZOLL Is Nothing Then Return Nothing
            If dEZOLL.Rows.Count = 0 Then Return Nothing

            'TextBox2.ForeColor = Color.Green
            Dim r As DataRow = dEZOLL.Rows(0)

            LRN = r.Item("LRN")

            If Not SQL.IsNull(r.Item("OperatorID")) Then
                Select Case r.Item("OperatorID")
                    Case "1"
                        VERAG_NCTS.Company = "VERAG"
                        VERAG_NCTS.Department = "SUB"
                    Case "2"
                        VERAG_NCTS.Company = "VERAG"
                        VERAG_NCTS.Department = "SBG"

                    Case "3"
                        VERAG_NCTS.Company = "VERAG"
                        VERAG_NCTS.Department = "NKD"
                    Case "4", "5"
                        VERAG_NCTS.Company = "ATILLA"
                        VERAG_NCTS.Department = "SUB"
                    Case "6"
                        If LRN.StartsWith("4801") Or LRN.StartsWith("4802") Then
                            VERAG_NCTS.Company = "ATILLA"
                        Else
                            VERAG_NCTS.Company = "VERAG"
                        End If
                        VERAG_NCTS.Department = "SUB"
                    Case "8"
                        VERAG_NCTS.Company = "DURMAZ"
                        VERAG_NCTS.Department = "SBG"
                        Return Nothing
                    Case Else
                        VERAG_NCTS.Company = "ATILLA"
                        VERAG_NCTS.Department = "SUB"

                End Select
            End If


            VERAG_NCTS.ProcessData.TransactionDate = CDate(r.Item("GeaendertAm")) 'default

            VERAG_NCTS.ProcessData.MRN = r.Item("CRN")
            VERAG_NCTS.ProcessData.ReferenzNumber = LRN

            Select Case (SQL.getValueTxtBySql("SELECT ErledigungsTypID FROM zsAnmDHFRefs WHERE AnmID=" & AnmID & " ORDER BY GeaendertAm DESC", "EZOLL"))
                Case "F"
                    VERAG_NCTS.ProcessData.Status = 50
                Case "FB"
                    VERAG_NCTS.ProcessData.Status = 60
                Case Else
                    VERAG_NCTS.ProcessData.Status = 0
            End Select

            Dim DTzsAnmHea As DataTable = SQL.loadDgvBySql("SELECT * FROM zsAnmHea WHERE  (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmHea.Rows().Count > 0 Then
                Dim rDTzsAnmHea = DTzsAnmHea.Rows(0)
                VERAG_NCTS.ProcessData.ProcedureCode = rDTzsAnmHea("DecTy")
                VERAG_NCTS.NCTS_Data.SumGrossWeight = rDTzsAnmHea("TotGross") 'If(NCTS_TMP.ncts_GesamtRohmasse, "0")
                VERAG_NCTS.ProcessData.TransactionDate = CDate(rDTzsAnmHea("DecDT"))
            End If


            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = SQL.isleernothing(SQL.getValueTxtBySql("SELECT Ref FROM zsAnmDepCO WHERE AnmID=" & AnmID & " ORDER BY GeaendertAm DESC", "EZOLL"))
            VERAG_NCTS.NCTS_Data.DatDeterminationLimit = SQL.isleernothing(SQL.getValueTxtBySql("SELECT DLimit FROM zsAnmCtrlRslt WHERE AnmID=" & AnmID & " ORDER BY GeaendertAm DESC", "EZOLL"))

            'BestimmungsstelleDienststelle
            Dim DTzsAnmDestCo As DataTable = SQL.loadDgvBySql("SELECT Ref  FROM zsAnmDestCo  WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmDestCo.Rows().Count > 0 Then
                If Not SQL.IsNull(DTzsAnmDestCo.Rows(0).Item("ref")) Then VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = DTzsAnmDestCo.Rows(0).Item("ref")
            End If

            ' Kennzeichen
            Dim DTzsAnmTransp As DataTable = SQL.loadDgvBySql("SELECT DepIdnt  FROM zsAnmTransp WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmTransp.Rows().Count > 0 Then
                If Not SQL.IsNull(DTzsAnmTransp.Rows(0).Item("DepIdnt")) Then
                    VERAG_NCTS.NCTS_Data.LicensePlate = DTzsAnmTransp.Rows(0).Item("DepIdnt")
                End If
            End If

            'Versender
            Dim DTzsAnmSender As DataTable = SQL.loadDgvBySql("SELECT * FROM [zsAnmConorTra]  WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmSender.Rows().Count > 0 Then
                VERAG_NCTS.NCTS_Data.Sender.Name = DTzsAnmSender.Rows(0).Item("Na")
                If Not SQL.IsNull(DTzsAnmSender.Rows(0).Item("Pst")) Then VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = DTzsAnmSender.Rows(0).Item("Pst")
                If Not SQL.IsNull(DTzsAnmSender.Rows(0).Item("Strt")) Then VERAG_NCTS.NCTS_Data.Sender.Adress.Street = DTzsAnmSender.Rows(0).Item("Strt")
                If Not SQL.IsNull(DTzsAnmSender.Rows(0).Item("Cty")) Then VERAG_NCTS.NCTS_Data.Sender.Adress.City = DTzsAnmSender.Rows(0).Item("Cty")
                If Not SQL.IsNull(DTzsAnmSender.Rows(0).Item("Ctry")) Then
                    VERAG_NCTS.NCTS_Data.Sender.Adress.Country = DTzsAnmSender.Rows(0).Item("Ctry")
                    VERAG_NCTS.NCTS_Data.DepartureCountry = DTzsAnmSender.Rows(0).Item("Ctry")
                End If
                If Not SQL.IsNull(DTzsAnmSender.Rows(0).Item("TIN")) Then VERAG_NCTS.NCTS_Data.Sender.EORI = DTzsAnmSender.Rows(0).Item("TIN")
                'VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
                'VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            End If


            'Empfänger
            Dim DTzsAnmConeeTra As DataTable = SQL.loadDgvBySql("SELECT * FROM zsAnmConeeTra  WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmConeeTra.Rows().Count > 0 Then
                VERAG_NCTS.NCTS_Data.Consignee.Name = DTzsAnmConeeTra.Rows(0).Item("Na")
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Pst")) Then VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = DTzsAnmConeeTra.Rows(0).Item("Pst")
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Strt")) Then VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = DTzsAnmConeeTra.Rows(0).Item("Strt")
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Cty")) Then VERAG_NCTS.NCTS_Data.Consignee.Adress.City = DTzsAnmConeeTra.Rows(0).Item("Cty")
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Ctry")) Then
                    VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = DTzsAnmConeeTra.Rows(0).Item("Ctry")
                    VERAG_NCTS.NCTS_Data.DestinationCountry = DTzsAnmConeeTra.Rows(0).Item("Ctry")
                End If
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("TIN")) Then VERAG_NCTS.NCTS_Data.Consignee.EORI = DTzsAnmConeeTra.Rows(0).Item("TIN")
                'VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
                'VERAG_NCTS.NCTS_Data.Consignee.UID_Nr = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            End If


            'Frächter
            Dim DTzsAnmCarrier As DataTable = SQL.loadDgvBySql("SELECT * FROM [zsAnmCarrTra]  WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")", "EZOLL")
            If DTzsAnmCarrier.Rows().Count > 0 Then
                VERAG_NCTS.NCTS_Data.Carrier.Name = DTzsAnmCarrier.Rows(0).Item("Na")
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Pst")) Then VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = DTzsAnmCarrier.Rows(0).Item("Pst")
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Strt")) Then VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = DTzsAnmCarrier.Rows(0).Item("Strt")
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Cty")) Then VERAG_NCTS.NCTS_Data.Carrier.Adress.City = DTzsAnmCarrier.Rows(0).Item("Cty")
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Ctry")) Then VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = DTzsAnmCarrier.Rows(0).Item("Ctry")
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("TIN")) Then VERAG_NCTS.NCTS_Data.Carrier.EORI = DTzsAnmCarrier.Rows(0).Item("TIN")
                'VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
                'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            End If


            ' Tabelle zsAnmGrteeGrteeRef lesen.
            Dim DTzsAnmGrteeGrteeRef As DataTable = SQL.loadDgvBySql("SELECT GRN, GVal" &
                " FROM zsAnmGrteeGrteeRef" &
                " WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")" &
                " ORDER BY GrteeID, GrteeRefID", "EZOLL")

            If DTzsAnmGrteeGrteeRef.Rows().Count > 0 Then
                VERAG_NCTS.NCTS_Data.Guarantee.Amount = 0
                For Each DTzsAnmGrteeGrteeRef_row In DTzsAnmGrteeGrteeRef.Rows()
                    If Not SQL.IsNull(DTzsAnmGrteeGrteeRef_row.item("GRN")) Then VERAG_NCTS.NCTS_Data.Guarantee.GRN = DTzsAnmGrteeGrteeRef_row.item("GRN")
                    If Not SQL.IsNull(DTzsAnmGrteeGrteeRef_row.item("GVal")) Then VERAG_NCTS.NCTS_Data.Guarantee.Amount += DTzsAnmGrteeGrteeRef_row.item("GVal")
                Next
            End If

            'POSITIONEN
            Dim cntPos = 1
            Dim DTzsAnmGdsitem As DataTable = SQL.loadDgvBySql("SELECT * FROM zsAnmGdsitem WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")  ORDER BY GdsItemID", "EZOLL")
            If DTzsAnmGdsitem.Rows().Count > 0 Then
                For Each DTzsAnmGdsitem_row In DTzsAnmGdsitem.Rows()
                    Dim V_POS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_nctsNCTS_DataPosition
                    V_POS.PosNr = cntPos
                    Dim pkGes = 0
                    Dim pkArt = ""

                    Dim DTzsAnmGdsitemPk As DataTable = SQL.loadDgvBySql("SELECT * FROM [zsAnmGdsItemPack] WHERE (LizenzNr = '" & r.Item("LizenzNr") & "') And (OperatorID = " & r.Item("OperatorID") & ") And (VorgangID = '" & r.Item("VorgangID") & "') And (AnmID = " & AnmID & ")  AND GdsItemID='" & DTzsAnmGdsitem_row.Item("GdsItemID") & "' ORDER BY GdsItemID", "EZOLL")
                    If DTzsAnmGdsitemPk.Rows().Count > 0 Then
                        For Each DTzsAnmGdsitemPk_row In DTzsAnmGdsitemPk.Rows()
                            If Not SQL.IsNull(DTzsAnmGdsitemPk_row.item("Kind")) Then pkArt = DTzsAnmGdsitemPk_row.item("Kind")
                            If Not SQL.IsNull(DTzsAnmGdsitemPk_row.item("Nr")) Then pkGes += DTzsAnmGdsitemPk_row.item("Nr")
                        Next
                    End If

                    V_POS.Quantity = pkGes
                    V_POS.Packaging = pkArt
                    If Not SQL.IsNull(DTzsAnmGdsitem_row.item("Gross")) Then V_POS.GrossWeight = DTzsAnmGdsitem_row.item("Gross")

                    V_POS.GoodsDescription = DTzsAnmGdsitem_row.item("GdsDes")

                    VERAG_NCTS.NCTS_Data.Position.Add(V_POS)
                    cntPos += 1
                Next
            End If

            UniqueId = AnmID
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & LRN, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return Nothing
        End Try
        Return VERAG_NCTS

    End Function

    Sub initDTWarenpos()

        If dtWARENPOS IsNot Nothing And Not dtWARENPOS.Columns.Contains("PosNr") Then

            dtWARENPOS.Columns.Add("PosNr", GetType(Integer))
            dtWARENPOS.Columns.Add("WarenNr", GetType(String))
            dtWARENPOS.Columns.Add("WarenBez", GetType(String))
            dtWARENPOS.Columns.Add("Verfahren", GetType(Integer))
            dtWARENPOS.Columns.Add("Preferenz", GetType(Integer))
            dtWARENPOS.Columns.Add("PosWert", GetType(Double))
            dtWARENPOS.Columns.Add("isCBAM", GetType(Boolean))
            dtWARENPOS.Columns.Add("Herkunft", GetType(String))
            dtWARENPOS.Columns.Add("Netto", GetType(Integer))

        Else

            dtWARENPOS.Clear()

        End If


    End Sub

    Shared Function getGRTY_fromEZOLL(LRN As String) As String
        Try
            Return SQL.getValueTxtBySql("SELECT TOP 1 GRN FROM zsAnmRefs inner join zsAnmGrteeGrteeRef on zsAnmGrteeGrteeRef.AnmID=zsAnmRefs.AnmID WHERE (zsAnmRefs.VorgangID = 'AV') AND (LRN = '" & LRN & "')", "EZOLL")
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & LRN, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return ""
    End Function

    Shared Function genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP As TELOTEC_Worker.cTelotec_Anmeldung, ByRef UniqueId As String) As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts
        Try
            If NCTS_TMP Is Nothing Then Return Nothing
            If NCTS_TMP.telanm_BezugsNr Is Nothing Then Return Nothing
            If NCTS_TMP.Hea_DecTy Is Nothing Then Return Nothing
            If NCTS_TMP.Hea_DecTy <> "T1" And NCTS_TMP.Hea_DecTy <> "T2" And NCTS_TMP.Hea_DecTy <> "T2L" And NCTS_TMP.Hea_DecTy <> "T-" Then Return Nothing

            '   VERAG_NCTS.Version = "1.0.2" Automaitsch, weil default

            If NCTS_TMP.telanm_firma <> "" Then
                VERAG_NCTS.Company = NCTS_TMP.telanm_firma
            Else
                VERAG_NCTS.Company = "IMEX"
            End If
            VERAG_NCTS.Department = NCTS_TMP.telanm_niederlassung


            'VERAG_NCTS.InternalReference.OfficeCode = If(
            'VERAG_NCTS.InternalReference.PositionNumber = NCTS_TMP.ncts_niederlassung
            Select Case NCTS_TMP.telanm_Status
                Case < 50
                    VERAG_NCTS.ProcessData.Status = 0
                Case 50, 53
                    VERAG_NCTS.ProcessData.Status = 50
                Case 60
                    VERAG_NCTS.ProcessData.Status = 60
            End Select

            VERAG_NCTS.ProcessData.TransactionDate = NCTS_TMP.dec_CreateDate

            VERAG_NCTS.ProcessData.MRN = If(NCTS_TMP.telanm_CRN, "")
            VERAG_NCTS.ProcessData.ReferenzNumber = If(NCTS_TMP.telanm_BezugsNr, "")
            VERAG_NCTS.ProcessData.ProcedureCode = If(NCTS_TMP.telanm_ART, "")

            VERAG_NCTS.NCTS_Data.DatDeterminationLimit = If(NCTS_TMP.CtrlRslt_DLimit, "")
            VERAG_NCTS.NCTS_Data.LicensePlate = If(NCTS_TMP.Transp_DepIdnt, "")
            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = If(NCTS_TMP.DepCO_Ref, "")
            VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = If(NCTS_TMP.DestCO_Ref, "")
            VERAG_NCTS.NCTS_Data.DepartureCountry = NCTS_TMP.Locs_Disp
            VERAG_NCTS.NCTS_Data.DestinationCountry = If(NCTS_TMP.Locs_Dest, "")

            'Versender
            VERAG_NCTS.NCTS_Data.Sender.Name = If(NCTS_TMP.ConorTra_Na, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI = If(NCTS_TMP.ConorTra_TIN, "")
            'VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.ConorTra_UIDCtry, "") & If(NCTS_TMP.ConorTra_UID, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Country = If(NCTS_TMP.ConorTra_Ctry, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = If(NCTS_TMP.ConorTra_Pst, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Street = If(NCTS_TMP.ConorTra_Strt, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.City = If(NCTS_TMP.ConorTra_Cty, "")

            'Empfänger
            VERAG_NCTS.NCTS_Data.Consignee.Name = If(NCTS_TMP.ConeeTra_Na, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI = If(NCTS_TMP.ConeeTra_TIN, "")
            'VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            VERAG_NCTS.NCTS_Data.Consignee.UID_Nr = If(NCTS_TMP.ConeeTra_UIDCtry, "") & If(NCTS_TMP.ConeeTra_UID, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = If(NCTS_TMP.ConeeTra_Ctry, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = If(NCTS_TMP.ConeeTra_Pst, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = If(NCTS_TMP.ConeeTra_Strt, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.City = If(NCTS_TMP.ConeeTra_Cty, "")

            ''Frächter
            'VERAG_NCTS.NCTS_Data.Carrier.Name = If(NCTS_TMP.ConorTra_Na, "")
            'VERAG_NCTS.NCTS_Data.Carrier.EORI = If(NCTS_TMP.ConorTra_TIN, "")
            ''VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.ConorTra_UIDCtry, "") & If(NCTS_TMP.ConorTra_UID, "")
            'VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(NCTS_TMP.ConorTra_Ctry, "")
            'VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(NCTS_TMP.ConorTra_Pst, "")
            'VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(NCTS_TMP.ConorTra_Strt, "")
            'VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(NCTS_TMP.ConorTra_Cty, "")

            If NCTS_TMP.SICHERHEIT IsNot Nothing AndAlso NCTS_TMP.SICHERHEIT.Count > 0 Then
                VERAG_NCTS.NCTS_Data.Guarantee.GRN = If(NCTS_TMP.SICHERHEIT(0).GRN, "")
                VERAG_NCTS.NCTS_Data.Guarantee.Amount = If(NCTS_TMP.SICHERHEIT(0).GVal, "0")
            End If

                VERAG_NCTS.NCTS_Data.SumGrossWeight = If(NCTS_TMP.Hea_TotGross, "0")
                Dim cntPos = 1
                If NCTS_TMP.POSITIONSDATEN IsNot Nothing Then
                    For Each POS In NCTS_TMP.POSITIONSDATEN
                        Dim V_POS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_nctsNCTS_DataPosition
                        V_POS.PosNr = cntPos
                        Dim pkGes = 0
                        Dim pkArt = ""
                        If POS.PACKSTUECKE IsNot Nothing Then
                            For Each PK In POS.PACKSTUECKE
                                If PK.Pack_Nr IsNot Nothing Then pkGes += PK.Pack_Nr 'verpackt
                                If PK.Pack_Piec IsNot Nothing Then pkGes += PK.Pack_Piec 'unverpackt
                                If PK.Pack_Kind IsNot Nothing Then pkArt = PK.Pack_Kind
                            Next
                        End If
                        V_POS.Quantity = pkGes
                        V_POS.Packaging = pkArt
                        V_POS.GrossWeight = POS.Item_Gross
                        V_POS.GoodsDescription = POS.Item_GdsDes

                        VERAG_NCTS.NCTS_Data.Position.Add(V_POS)
                        cntPos += 1
                    Next
                End If
                UniqueId = NCTS_TMP.telanm_id

                'If XMLFilePath = "" Then
                '    Dim dir = "\\192.168.0.91\Datenarchiv\ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\TC\" & Now.Year & "\" & Now.ToShortDateString & "\"
                '    If Not IO.Directory.Exists(dir) Then IO.Directory.CreateDirectory(dir)
                '    XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_TC_" & NCTS_TMP.telanm_id & ".xml"
                'End If


                'VERAG_NCTS.SaveToFile(XMLFilePath)

            Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.telanm_BezugsNr, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                Return Nothing
            End Try
            Return VERAG_NCTS

        End Function


        Shared Function insertFTPLOG(filePath As String, user As String, NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts, vorsystem As String)
            Return SQL.doSQL("INSERT INTO tblFTP_Log (ftp_filepath,ftp_user,ftp_BezugsNr,ftp_art,ftp_status,ftp_vorsystem) VALUES ('" & filePath & "','" & user & "','" & NCTS_VERAG_OUT.ProcessData.ReferenzNumber & "','" & NCTS_VERAG_OUT.ProcessData.ProcedureCode & "','" & NCTS_VERAG_OUT.ProcessData.Status & "','" & vorsystem & "')", "FMZOLL")
        End Function


        Shared Function insertFTPLOG(filePath, user, ftp_BezugsNr, ftp_art, ftp_status, ftp_vorsystem)
            Return SQL.doSQL("INSERT INTO tblFTP_Log (ftp_filepath,ftp_user,ftp_BezugsNr,ftp_art,ftp_status,ftp_vorsystem) VALUES ('" & filePath & "','" & user & "','" & ftp_BezugsNr & "','" & ftp_art & "','" & ftp_status & "','" & ftp_vorsystem & "')", "FMZOLL")
        End Function


    End Class
