Public Class cVERAG_out_ncts
    Shared SQL As New VERAG_PROG_ALLGEMEIN.SQL


    Shared GLO_NCTS_DY As DAKOSY_Worker.cDakosyNCTSOut = Nothing
    Shared GLO_NCTS004_DY As DAKOSY_Worker.cDakosyNCTSOut004 = Nothing
    Shared GLO_DY As DAKOSY_Worker.cDakosy_Zollanmeldungen = Nothing
    Shared GLO_NCTS_TC As TELOTEC_Worker.cTelotec_Anmeldung = Nothing

    Shared GLO_AVISO As VERAG_PROG_ALLGEMEIN.cAviso = Nothing
    Shared GLO_SND As VERAG_PROG_ALLGEMEIN.cSendungen = Nothing
    Shared cVERAG_Interface_Settings As New cVERAG_Interface_Settings 'defalut: Alles true

    'Shared Function copyFileFTP_OLD(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen) ', filePath As String)
    '    Try
    '        If NCTS_TMP Is Nothing Then Return False
    '        If NCTS_TMP.ncts_ObjectName Is Nothing Then Return False
    '        If NCTS_TMP.ncts_ObjectName.ToString.StartsWith("DU") Then Return False
    '        If NCTS_TMP.ncts_firma.ToString = "DURMAZ" Then Return False

    '        Dim filePath = SQL.getValueTxtBySql("select [dySt_pfad] from [tblDakosy_Statusmeldungen] where dySt_BezugsNr='" & NCTS_TMP.ncts_ObjectName & "' and [dySt_status]=5 and [dySt_pfad] is not null ", "FMZOLL")
    '        If filePath = "" Then Return False

    '        NCTS_TMP.LOAD_Sicherheitsangaben()
    '        If NCTS_TMP.ncts_SICHERHEITSANGABEN IsNot Nothing AndAlso NCTS_TMP.ncts_SICHERHEITSANGABEN.Count > 0 Then
    '            Dim GRN = NCTS_TMP.ncts_SICHERHEITSANGABEN(0).nctsSA_GRN
    '            Dim destFile = ""
    '            Select Case GRN
    '                                ' --> FTP Bereitstellung
    '            '\\192.168.0.91\Datenarchiv\DAKOSY\ECHTSYSTEM\FTP\TOBB
    '                Case "15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518"
    '                    frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\TOBB\OUT\", destFile)
    '                    If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "TOBB") Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
    '                Case "18DE0000000073276", "19DE0000000075520", "18DE0000000075520"
    '                    frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\OREGON\OUT\", destFile)
    '                    If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "OREGON") Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
    '                Case "17DE0000000071678", "05AT510000G000FP7" 'VERAG
    '                    frmStartOptions.copyFile(filePath, VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", destFile)
    '                    If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "VERAG") Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
    '            End Select
    '        End If
    '        Return True
    '    Catch ex As Exception
    '        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
    '        Return False
    '    End Try
    'End Function



    Shared Function copyFileFTP_VERAGncts_DY_ByBezugsNr(BezugsNr As String, ISett As cVERAG_Interface_Settings, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        cVERAG_Interface_Settings = ISett
        Dim DY_TMP = DAKOSY_Worker.cDakosy_Zollanmeldungen.LOADByBezugsNr(BezugsNr)
        Dim NCTS_TMP = DAKOSY_Worker.cDakosyNCTSOut004.LOADByBezugsNr(BezugsNr, True)
        If DY_TMP Is Nothing Then Return False
        If NCTS_TMP Is Nothing Then Return False
        Return copyFileFTP_VERAGncts004_DY(NCTS_TMP, DY_TMP, False, FilialenNrTmp, AbfertigungsNrTmp)
    End Function
    Shared Function getLRN_ByCRN(CRN As String) As String
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Return SQL.getValueTxtBySql("SELECT TOP 1 LRN FROM zsAnmRefs  WHERE CRN = '" & CRN & "' ORDER BY GeaendertAm DESC", "EZOLL")
    End Function

    Shared Function getBezugnNr_ByMRN(MRN As String) As String
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Return SQL.getValueTxtBySql("  SELECT TOP 1 ncts_ObjectName FROM tblDakosy_NCTSOut004  WHERE ncts_MRN = '" & MRN & "' ORDER BY ncts_Trans_DatumZeit DESC", "FMZOLL")
    End Function

    Shared Function copyFileFTP_VERAGncts_DY(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, Optional checkAlreadyProvided As Boolean = True, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        Try
            If NCTS_TMP Is Nothing Then Return False
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return False
            If NCTS_TMP.ncts_ObjectName.ToString.StartsWith("DU") Then Return False
            If If(NCTS_TMP.ncts_firma, "").ToString = "DURMAZ" Then Return False

            GLO_NCTS_DY = NCTS_TMP
            GLO_DY = DY_TMP

            GLO_SND = Nothing
            GLO_AVISO = Nothing

            If DY_TMP.dy_SendungsId IsNot Nothing AndAlso DY_TMP.dy_SendungsId > 0 Then
                GLO_SND = New VERAG_PROG_ALLGEMEIN.cSendungen(DY_TMP.dy_SendungsId)
            ElseIf FilialenNrTmp IsNot Nothing And AbfertigungsNrTmp IsNot Nothing Then
                GLO_SND = VERAG_PROG_ALLGEMEIN.cSendungen.LOADByFilialenNrAbfertigungsNr(FilialenNrTmp, AbfertigungsNrTmp)
            End If

            If DY_TMP.dy_AvisoId IsNot Nothing AndAlso DY_TMP.dy_AvisoId > 0 Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(DY_TMP.dy_AvisoId)
            ElseIf GLO_SND IsNot Nothing Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(GLO_SND.tblSnd_AvisoID)
            End If

            ' Dim filePath = SQL.getValueTxtBySql("select [dySt_pfad] from [tblDakosy_Statusmeldungen] where dySt_BezugsNr='" & NCTS_TMP.ncts_ObjectName & "' and [dySt_status]=5 and [dySt_pfad] is not null ", "FMZOLL")

            Dim GRN = ""
            NCTS_TMP.LOAD_Sicherheitsangaben()
            If NCTS_TMP.ncts_SICHERHEITSANGABEN IsNot Nothing AndAlso NCTS_TMP.ncts_SICHERHEITSANGABEN.Count > 0 Then
                GRN = NCTS_TMP.ncts_SICHERHEITSANGABEN(0).nctsSA_GRN
            End If

            Dim destFile = ""
            Dim filePath = ""
            Dim UniqueId = ""
            ' --> FTP Bereitstellung
            '  Dim NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts = Nothing

            Dim found As Boolean = False
            Dim vorsystem = "DY"
            Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)

            If getFTP_Paths(GRN, IF_Obj) Then
                For Each o In IF_Obj

                    Select Case o.CooName
                        'Case "EKOL", "TOBB"
                        '    'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_1(NCTS_TMP, DY_TMP, UniqueId)
                        '    'copyNCTSToFTP_V2_0_1(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        '    Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)
                        '    copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        Case "GASTON_SCHUL"
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)

                            Dim GASTON_SCHUL_EX As New VERAG_PROG_ALLGEMEIN.agsCustomsExchange.exchangeRequestType
                            GASTON_SCHUL_EX.requestDateTime = Now
                            GASTON_SCHUL_EX.creationDateTime = NCTS_VERAG_OUT.NCTS_Data.DatDeterminationLimit

                            GASTON_SCHUL_EX.movementReferenceNumber = NCTS_VERAG_OUT.ProcessData.MRN
                            GASTON_SCHUL_EX.dispatchCountry = NCTS_VERAG_OUT.NCTS_Data.DepartureCountry
                            GASTON_SCHUL_EX.destinationCountry = NCTS_VERAG_OUT.NCTS_Data.DestinationCountry

                            'filePath???
                            cGASTON_SCHUL_Interface.copyNCTSToFTP_V1_0_0(GASTON_SCHUL_EX, o, vorsystem, "", destFile, UniqueId, checkAlreadyProvided, found)
                        Case "TOBB"
                            '  VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("KONTROLLE --------- TOBB: BezugsNr: " & NCTS_TMP.ncts_ObjectName, "", System.Reflection.MethodInfo.GetCurrentMethod.Name,  , "BezugsNr: " & NCTS_TMP.ncts_ObjectName & " (DAKOSY)")
                            'Wenn TOBB, nur mit KDNR:
                            If getTOBBKdnr() > 0 Then

                                Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId, getTOBBKdnr)
                                If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            Else
                                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("TOBB: KEINE KDNR | BezugsNr: " & NCTS_TMP.ncts_ObjectName & vbNewLine & getTOBBDetails(), "", System.Reflection.MethodInfo.GetCurrentMethod.Name, VERAG_PROG_ALLGEMEIN.ERROR_OP.LOG, "BezugsNr: " & NCTS_TMP.ncts_ObjectName & " (DAKOSY)", VERAG_PROG_ALLGEMEIN.cAllgemein.EMailTOBB_Error)
                            End If
                        Case Else
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)
                            If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
                            'copyNCTSToFTP(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                    End Select
                Next
            End If

            'If getFTP_Paths(GRN, IF_Obj) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
            '    copyNCTSToFTP(NCTS_VERAG_OUT, IF_Obj, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided)
            'End If

            'Dim vorsystem = "DY"
            'Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)
            'If getFTP_Paths(GRN, IF_Obj) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
            '    For Each o In IF_Obj
            '        If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT, vorsystem, checkAlreadyProvided) Then
            '            If genXMLFile(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
            '                frmStartOptions.copyFile(filePath, o.FTP_Path, destFile)
            '                If IO.File.Exists(destFile) Then insertFTPLOG(destFile, o.CooName, NCTS_VERAG_OUT, vorsystem) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyFileFTP")
            '            End If
            '        End If
            '    Next
            'End If


            'Dim destFile = ""
            'Dim filePath = ""
            'Dim NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts = Nothing
            '' --> FTP Bereitstellung
            'Select Case GRN
            '    Case "15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\TOBB\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "TOBB", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            '    Case "18DE0000000073276", "19DE0000000075520", "18DE0000000075520"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\OREGON\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "OREGON", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            '    Case "17DE0000000071678", "05AT510000G000FP7" 'VERAG
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "VERAG", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            'End Select

            Return found
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return False
        End Try
    End Function
    Shared Function copyFileFTP_VERAGncts004_DY(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut004, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, Optional checkAlreadyProvided As Boolean = True, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        Try
            If NCTS_TMP Is Nothing Then Return False
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return False
            If NCTS_TMP.ncts_ObjectName.ToString.StartsWith("DU") Then Return False
            If If(NCTS_TMP.ncts_firma, "").ToString = "DURMAZ" Then Return False

            GLO_NCTS004_DY = NCTS_TMP
            GLO_DY = DY_TMP

            GLO_SND = Nothing
            GLO_AVISO = Nothing

            If DY_TMP.dy_SendungsId IsNot Nothing AndAlso DY_TMP.dy_SendungsId > 0 Then
                GLO_SND = New VERAG_PROG_ALLGEMEIN.cSendungen(DY_TMP.dy_SendungsId)
            ElseIf FilialenNrTmp IsNot Nothing And AbfertigungsNrTmp IsNot Nothing Then
                GLO_SND = VERAG_PROG_ALLGEMEIN.cSendungen.LOADByFilialenNrAbfertigungsNr(FilialenNrTmp, AbfertigungsNrTmp)
            End If

            If DY_TMP.dy_AvisoId IsNot Nothing AndAlso DY_TMP.dy_AvisoId > 0 Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(DY_TMP.dy_AvisoId)
            ElseIf GLO_SND IsNot Nothing Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(GLO_SND.tblSnd_AvisoID)
            End If

            ' Dim filePath = SQL.getValueTxtBySql("select [dySt_pfad] from [tblDakosy_Statusmeldungen] where dySt_BezugsNr='" & NCTS_TMP.ncts_ObjectName & "' and [dySt_status]=5 and [dySt_pfad] is not null ", "FMZOLL")

            Dim GRN = ""
            NCTS_TMP.LOAD_Guarantee()
            If NCTS_TMP.ncts_GUARANTEE IsNot Nothing AndAlso NCTS_TMP.ncts_GUARANTEE.Count > 0 Then
                GRN = NCTS_TMP.ncts_GUARANTEE(0).nctsGRT_GRN
            End If

            Dim destFile = ""
            Dim filePath = ""
            Dim UniqueId = ""
            ' --> FTP Bereitstellung
            '  Dim NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts = Nothing

            Dim found As Boolean = False
            Dim vorsystem = "DY"
            Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)

            If getFTP_Paths(GRN, IF_Obj) Then
                For Each o In IF_Obj

                    Select Case o.CooName
                        'Case "EKOL", "TOBB"
                        '    'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_1(NCTS_TMP, DY_TMP, UniqueId)
                        '    'copyNCTSToFTP_V2_0_1(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        '    Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)
                        '    copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        Case "GASTON_SCHUL"
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS004_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)

                            Dim GASTON_SCHUL_EX As New VERAG_PROG_ALLGEMEIN.agsCustomsExchange.exchangeRequestType
                            GASTON_SCHUL_EX.requestDateTime = Now
                            GASTON_SCHUL_EX.creationDateTime = NCTS_VERAG_OUT.NCTS_Data.DatDeterminationLimit

                            GASTON_SCHUL_EX.movementReferenceNumber = NCTS_VERAG_OUT.ProcessData.MRN
                            GASTON_SCHUL_EX.dispatchCountry = NCTS_VERAG_OUT.NCTS_Data.DepartureCountry
                            GASTON_SCHUL_EX.destinationCountry = NCTS_VERAG_OUT.NCTS_Data.DestinationCountry

                            'filePath???
                            cGASTON_SCHUL_Interface.copyNCTSToFTP_V1_0_0(GASTON_SCHUL_EX, o, vorsystem, "", destFile, UniqueId, checkAlreadyProvided, found)
                        Case "TOBB"
                            '  VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("KONTROLLE --------- TOBB: BezugsNr: " & NCTS_TMP.ncts_ObjectName, "", System.Reflection.MethodInfo.GetCurrentMethod.Name,  , "BezugsNr: " & NCTS_TMP.ncts_ObjectName & " (DAKOSY)")
                            'Wenn TOBB, nur mit KDNR:
                            If getTOBBKdnr() > 0 Then

                                Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS004_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId, getTOBBKdnr)
                                If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            Else
                                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("TOBB: KEINE KDNR | BezugsNr: " & NCTS_TMP.ncts_ObjectName & vbNewLine & getTOBBDetails(), "", System.Reflection.MethodInfo.GetCurrentMethod.Name, VERAG_PROG_ALLGEMEIN.ERROR_OP.LOG, "BezugsNr: " & NCTS_TMP.ncts_ObjectName & " (DAKOSY)", VERAG_PROG_ALLGEMEIN.cAllgemein.EMailTOBB_Error)
                            End If
                        Case Else
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS004_FromDAKOSY_V2_0_2(NCTS_TMP, DY_TMP, UniqueId)
                            If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
                            'copyNCTSToFTP(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                    End Select
                Next
            End If

            'If getFTP_Paths(GRN, IF_Obj) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
            '    copyNCTSToFTP(NCTS_VERAG_OUT, IF_Obj, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided)
            'End If

            'Dim vorsystem = "DY"
            'Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)
            'If getFTP_Paths(GRN, IF_Obj) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, UniqueId)
            '    For Each o In IF_Obj
            '        If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT, vorsystem, checkAlreadyProvided) Then
            '            If genXMLFile(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
            '                frmStartOptions.copyFile(filePath, o.FTP_Path, destFile)
            '                If IO.File.Exists(destFile) Then insertFTPLOG(destFile, o.CooName, NCTS_VERAG_OUT, vorsystem) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyFileFTP")
            '            End If
            '        End If
            '    Next
            'End If


            'Dim destFile = ""
            'Dim filePath = ""
            'Dim NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts = Nothing
            '' --> FTP Bereitstellung
            'Select Case GRN
            '    Case "15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\TOBB\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "TOBB", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            '    Case "18DE0000000073276", "19DE0000000075520", "18DE0000000075520"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\OREGON\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "OREGON", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            '    Case "17DE0000000071678", "05AT510000G000FP7" 'VERAG
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP, DY_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "VERAG", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.ncts_ObjectName, "", "copyFileFTP")
            'End Select

            Return found
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return False
        End Try
    End Function

    Shared Function copyFileFTP_VERAGncts_TC_ByBezugsNr(BezugsNr As String, ISett As cVERAG_Interface_Settings, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        cVERAG_Interface_Settings = ISett
        Dim NCTS_TMP = TELOTEC_Worker.cTelotec_Anmeldung.LOAD_ByBezugsNr(BezugsNr)
        If NCTS_TMP Is Nothing Then Return False
        Return copyFileFTP_VERAGncts_TC(NCTS_TMP, False, FilialenNrTmp, AbfertigungsNrTmp)
    End Function
    Shared Function copyFileFTP_VERAGncts_TC(NCTS_TMP As TELOTEC_Worker.cTelotec_Anmeldung, Optional checkAlreadyProvided As Boolean = True, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        Try
            If NCTS_TMP Is Nothing Then Return False
            If NCTS_TMP.telanm_BezugsNr Is Nothing Then Return False
            'If NCTS_TMP.telanm_firma.ToString = "DURMAZ" Then Return False
            Select Case NCTS_TMP.telanm_ART
                Case "T1", "T2", "T2L", "T-"
                Case Else
                    Return False
            End Select

            GLO_SND = Nothing
            GLO_AVISO = Nothing
            GLO_NCTS_TC = NCTS_TMP

            If NCTS_TMP.telanm_SendungsId IsNot Nothing AndAlso NCTS_TMP.telanm_SendungsId > 0 Then
                GLO_SND = New VERAG_PROG_ALLGEMEIN.cSendungen(NCTS_TMP.telanm_SendungsId)
            ElseIf FilialenNrTmp IsNot Nothing And AbfertigungsNrTmp IsNot Nothing Then
                GLO_SND = VERAG_PROG_ALLGEMEIN.cSendungen.LOADByFilialenNrAbfertigungsNr(FilialenNrTmp, AbfertigungsNrTmp)
            End If

            If NCTS_TMP.telanm_AvisoId IsNot Nothing AndAlso NCTS_TMP.telanm_AvisoId > 0 Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(NCTS_TMP.telanm_AvisoId)
            ElseIf GLO_SND IsNot Nothing Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(GLO_SND.tblSnd_AvisoID)
            End If

            ' Dim filePath = SQL.getValueTxtBySql("select [dySt_pfad] from [tblDakosy_Statusmeldungen] where dySt_BezugsNr='" & NCTS_TMP.ncts_ObjectName & "' and [dySt_status]=5 and [dySt_pfad] is not null ", "FMZOLL")

            '  NCTS_TMP.LOAD_Sicherheitsangaben()
            Dim GRN = ""
            If NCTS_TMP.SICHERHEIT IsNot Nothing AndAlso NCTS_TMP.SICHERHEIT.Count > 0 Then
                GRN = NCTS_TMP.SICHERHEIT(0).GRN
            End If
            Dim destFile = ""
            Dim filePath = ""
            Dim UniqueId = ""

            ' --> FTP Bereitstellung
            'Dim NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts = Nothing
            Dim found As Boolean = False
            Dim vorsystem = "TC"

            Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)
            If getFTP_Paths(GRN, IF_Obj) Then
                For Each o In IF_Obj
                    Select Case o.CooName
                        'Case "EKOL", "TOBB"
                        '    'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC_V2_0_1(NCTS_TMP, UniqueId)
                        '    'copyNCTSToFTP_V2_0_1(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        '    Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC_V2_0_2(NCTS_TMP, UniqueId)
                        '    copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        Case "TOBB"
                            ' VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("KONTROLLE --------- TOBB: BezugsNr: " & NCTS_TMP.telanm_BezugsNr, "", System.Reflection.MethodInfo.GetCurrentMethod.Name,  , "BezugsNr: " & NCTS_TMP.telanm_BezugsNr & " (TELOTEC)")
                            'Wenn TOBB, nur mit KDNR:
                            If getTOBBKdnr() > 0 Then
                                Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC_V2_0_2(NCTS_TMP, UniqueId, getTOBBKdnr)
                                copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            Else
                                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("TOBB: KEINE KDNR | BezugsNr: " & NCTS_TMP.telanm_BezugsNr & vbNewLine & getTOBBDetails(), "", System.Reflection.MethodInfo.GetCurrentMethod.Name, VERAG_PROG_ALLGEMEIN.ERROR_OP.LOG, "BezugsNr: " & NCTS_TMP.telanm_BezugsNr & " (TELOTEC)", VERAG_PROG_ALLGEMEIN.cAllgemein.EMailTOBB_Error)
                            End If

                        Case Else
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC_V2_0_2(NCTS_TMP, UniqueId)
                            copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP, UniqueId)
                            'copyNCTSToFTP(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                    End Select
                Next
            End If

            'If getFTP_Paths(GRN, IF_Obj) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP, UniqueId)
            '    copyNCTSToFTP(NCTS_VERAG_OUT, IF_Obj, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided)
            'End If


            'Select Case GRN
            '    Case "15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\TOBB\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "TOBB", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.telanm_BezugsNr, "", "copyFileFTP")
            '    Case "18DE0000000073276", "19DE0000000075520", "18DE0000000075520"
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, "\\ftps.verag.ag\FTP\OREGON\OUT\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "OREGON", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.telanm_BezugsNr, "", "copyFileFTP")
            '    Case "17DE0000000071678", "05AT510000G000FP7" 'VERAG
            '        NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromTELOTEC(NCTS_TMP, filePath)
            '        If filePath = "" Then Return False
            '        frmStartOptions.copyFile(filePath, VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", destFile)
            '        If IO.File.Exists(destFile) Then insertFTPLOG(destFile, "VERAG", NCTS_VERAG_OUT) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & NCTS_TMP.telanm_BezugsNr, "", "copyFileFTP")
            'End Select

            Return found
        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 False
        End Try
    End Function

    Shared Sub copyNCTSToFTP(ByRef NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts, IF_Obj As cVERAG_InterfaceSharedObjects, ByRef vorsystem As String, ByRef filePath As String, ByRef destFile As String, ByRef UniqueId As String, Optional checkAlreadyProvided As Boolean = True, ByRef Optional found As Boolean = False)
        If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, checkAlreadyProvided) Then
            If genXMLFile(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
                frmStartOptions.copyFile(filePath, IF_Obj.FTP_Path, destFile)
                If System.IO.File.Exists(destFile) Then
                    insertFTPLOG(destFile, NCTS_VERAG_OUT.Company, NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, NCTS_VERAG_OUT.ProcessData.MRN)
                    found = True
                Else
                    Try
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP",,,,, NCTS_VERAG_OUT.ProcessData.ReferenzNumber)
                    Catch
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP")
                    End Try
                End If
            End If
        End If

    End Sub

    Shared Sub copyNCTSToFTP_V2_0_1(ByRef NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_ncts, IF_Obj As cVERAG_InterfaceSharedObjects, ByRef vorsystem As String, ByRef filePath As String, ByRef destFile As String, ByRef UniqueId As String, Optional checkAlreadyProvided As Boolean = True, ByRef Optional found As Boolean = False)
        If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, checkAlreadyProvided) Then
            If genXMLFile_V2_0_1(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
                frmStartOptions.copyFile(filePath, IF_Obj.FTP_Path, destFile)
                If System.IO.File.Exists(destFile) Then
                    insertFTPLOG(destFile, NCTS_VERAG_OUT.Company, NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, NCTS_VERAG_OUT.ProcessData.MRN)
                    found = True
                Else
                    Try
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP",,,,, NCTS_VERAG_OUT.ProcessData.ReferenzNumber)
                    Catch
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP")
                    End Try
                End If
            End If
        End If

    End Sub


    Shared Sub copyNCTSToFTP_V2_0_2(ByRef NCTS_VERAG_OUT As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts, IF_Obj As cVERAG_InterfaceSharedObjects, ByRef vorsystem As String, ByRef filePath As String, ByRef destFile As String, ByRef UniqueId As String, Optional checkAlreadyProvided As Boolean = True, ByRef Optional found As Boolean = False)
        If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, checkAlreadyProvided) Then
            If genXMLFile_V2_0_2(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
                frmStartOptions.copyFile(filePath, IF_Obj.FTP_Path, destFile)
                If System.IO.File.Exists(destFile) Then
                    insertFTPLOG(destFile, NCTS_VERAG_OUT.Company, NCTS_VERAG_OUT.ProcessData.ReferenzNumber, NCTS_VERAG_OUT.ProcessData.ProcedureCode, NCTS_VERAG_OUT.ProcessData.Status, vorsystem, IF_Obj.toSystem, NCTS_VERAG_OUT.ProcessData.MRN)
                    found = True
                Else
                    Try
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP",,,,, NCTS_VERAG_OUT.ProcessData.ReferenzNumber)
                    Catch
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP")
                    End Try
                End If
            End If
        End If

    End Sub


    Shared Function copyFileFTP_VERAGncts_EZOLL_ByBezugsNr(LRN As String, ISett As cVERAG_Interface_Settings, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        cVERAG_Interface_Settings = ISett
        Dim AnmId = SQL.getValueTxtBySql("SELECT TOP 1 AnmId  FROM zsAnmRefs WHERE (VorgangID = 'AV') AND (LRN = '" & LRN & "')", "EZOLL")
        If AnmId = "" Then Return False
        Return copyFileFTP_VERAGncts_EZOLL(AnmId, False, FilialenNrTmp, AbfertigungsNrTmp)
    End Function

    Shared Function copyFileFTP_VERAGncts_EZOLL(AnmId As String, Optional checkAlreadyProvided As Boolean = True, Optional FilialenNrTmp As Object = Nothing, Optional AbfertigungsNrTmp As Object = Nothing) As Boolean
        Dim LRN = ""
        Try

            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 False
            If dEZOLL.Rows.Count <= 0 Then Return False
            'If NCTS_TMP.telanm_firma.ToString = "DURMAZ" Then Return False

            LRN = dEZOLL.Rows(0)("LRN")

            GLO_SND = Nothing
            GLO_AVISO = Nothing


            'Versuche 
            Dim f_tmp As Integer = -1
            Dim a_tmp As Integer = -1
            If LRN.Length >= 13 Then
                If IsNumeric(LRN.Substring(0, 4)) Then
                    f_tmp = LRN.Substring(0, 4)
                End If
                If IsNumeric(LRN.Substring(5, 8)) Then
                    a_tmp = LRN.Substring(5, 8)
                End If
            End If

            If f_tmp > 0 And a_tmp > 0 Then
                GLO_SND = VERAG_PROG_ALLGEMEIN.cSendungen.LOADByFilialenNrAbfertigungsNr(f_tmp, a_tmp)
            ElseIf FilialenNrTmp IsNot Nothing And AbfertigungsNrTmp IsNot Nothing Then
                GLO_SND = VERAG_PROG_ALLGEMEIN.cSendungen.LOADByFilialenNrAbfertigungsNr(FilialenNrTmp, AbfertigungsNrTmp)
            End If

            If GLO_SND IsNot Nothing Then
                GLO_AVISO = VERAG_PROG_ALLGEMEIN.cAviso.getAvisoById(GLO_SND.tblSnd_AvisoID)
            End If



            Select Case SQL.getValueTxtBySql("SELECT TOP 1 DecTy  FROM zsAnmHea WHERE (VorgangID = 'AV') AND (AnmId = '" & AnmId & "')", "EZOLL")
                Case "T1", "T2", "T2L", "T-"
                Case Else
                    Return False
            End Select

            ' Tabelle zsAnmGrteeGrteeRef lesen.
            Dim GRN As String = SQL.getValueTxtBySql("SELECT TOP 1 GRN FROM zsAnmGrteeGrteeRef WHERE AnmID = " & AnmId & " ORDER BY GrteeID, GrteeRefID", "EZOLL")
            Dim destFile = ""
            Dim filePath = ""
            Dim UniqueId = ""

            ' --> FTP Bereitstellung
            Dim found As Boolean = False
            Dim vorsystem = "DHF"
            Dim IF_Obj As New List(Of cVERAG_InterfaceSharedObjects)
            If getFTP_Paths(GRN, IF_Obj) Then
                For Each o In IF_Obj
                    Select Case o.CooName
                        'Case "EKOL", "TOBB"
                        '    'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId_V2_0_1(AnmId, UniqueId)
                        '    'copyNCTSToFTP_V2_0_1(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        '    Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId_V2_0_2(AnmId, UniqueId)
                        '    copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                        Case "TOBB"
                            'VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("KONTROLLE --------- TOBB: BezugsNr: " & LRN, "", System.Reflection.MethodInfo.GetCurrentMethod.Name,  , "BezugsNr: " & LRN & " (DHF/EZOLL)")
                            'Wenn TOBB, nur mit KDNR:
                            If getTOBBKdnr() > 0 Then
                                Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId_V2_0_2(AnmId, UniqueId, getTOBBKdnr)
                                If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            Else
                                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("TOBB: KEINE KDNR | BezugsNr: " & LRN & vbNewLine & getTOBBDetails(), "", System.Reflection.MethodInfo.GetCurrentMethod.Name, VERAG_PROG_ALLGEMEIN.ERROR_OP.LOG, "BezugsNr: " & LRN & " (DHF/EZOLL)", VERAG_PROG_ALLGEMEIN.cAllgemein.EMailTOBB_Error)
                            End If

                        Case Else
                            Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId_V2_0_2(AnmId, UniqueId)
                            If NCTS_VERAG_OUT IsNot Nothing Then copyNCTSToFTP_V2_0_2(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                            'Dim NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId(AnmId, UniqueId)
                            'copyNCTSToFTP(NCTS_VERAG_OUT, o, vorsystem, filePath, destFile, UniqueId, checkAlreadyProvided, found)
                    End Select
                Next
            End If


            'If getFTP_Paths(GRN, FTP_Path, CooName) Then
            '    NCTS_VERAG_OUT = genVERAG_OUT_NCTS_FromEZOLLByAnmId(AnmId, UniqueId)
            '    If NCTS_VERAG_OUT IsNot Nothing AndAlso Not alreadyProvidedInInterface(NCTS_VERAG_OUT, vorsystem, checkAlreadyProvided) Then
            '        If genXMLFile(NCTS_VERAG_OUT, filePath, vorsystem, UniqueId) <> "" Then
            '            frmStartOptions.copyFile(filePath, FTP_Path, destFile)
            '            If IO.File.Exists(destFile) Then insertFTPLOG(destFile, CooName, NCTS_VERAG_OUT, vorsystem) Else VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile & vbNewLine & LRN, "", "copyFileFTP")
            '        End If
            '    End If
            'End If

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


    Shared Function getFTP_Paths(GRN As String, ByRef IF_Obj As List(Of cVERAG_InterfaceSharedObjects)) As Boolean

        IF_Obj = New List(Of cVERAG_InterfaceSharedObjects)

        Try
            If VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.TOBB.Contains(GRN) Then 'TOBB
                If cVERAG_Interface_Settings.acticeInterface_TOBB Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\TOBB\OUT\", "TOBB")) : IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\datenarchiv\Datenarchiv\DAKOSY\ECHTSYSTEM\FTP\TOBB\" & Now.ToString("yyyyMM") & "\", "TOBB", "VERAG"))
            End If
            If VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.OREGON.Contains(GRN) Then 'OREGON
                If cVERAG_Interface_Settings.acticeInterface_OREGON Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\OREGON\OUT\", "OREGON")) : IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\datenarchiv\Datenarchiv\DAKOSY\ECHTSYSTEM\FTP\OREGON\" & Now.ToString("yyyyMM") & "\", "OREGON", "VERAG"))
            End If
            If VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.VERAG.Contains(GRN) Then 'VERAG
                If cVERAG_Interface_Settings.acticeInterface_VERAG Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", "VERAG"))
            End If
            If VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.OREGON.Contains(GRN) Then 'IMEX
                If cVERAG_Interface_Settings.acticeInterface_IMEX Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\IMEX\", "IMEX", "VERAG"))
            End If


            'Select Case GRN
            '    Case "15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518", "21TR0004000079552", "21TR0004000078157", "21TR0004000092350", "20TR0004000069756", "21TR0004000090364"
            '        If cVERAG_Interface_Settings.acticeInterface_TOBB Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\TOBB\OUT\", "TOBB")) : IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\datenarchiv\Datenarchiv\DAKOSY\ECHTSYSTEM\FTP\TOBB\", "TOBB"))
            '    Case "18DE0000000073276", "19DE0000000075520", "18DE0000000075520", "15TR0004000020500", "ITTR0000000000153", "21DE0000000079716"
            '        If cVERAG_Interface_Settings.acticeInterface_OREGON Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\OREGON\OUT\", "OREGON")) : IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\datenarchiv\Datenarchiv\DAKOSY\ECHTSYSTEM\FTP\OREGON\", "OREGON"))
            '    Case "17DE0000000071678", "05AT510000G000FP7" 'VERAG
            '        If cVERAG_Interface_Settings.acticeInterface_VERAG Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\VERAG\", "VERAG"))
            '    Case "16AT520000G000VA6" 'IMEX
            '        If cVERAG_Interface_Settings.acticeInterface_IMEX Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "DAKOSY\ECHTSYSTEM\FTP\IMEX\", "IMEX"))
            '    Case Else
            'End Select

            If cVERAG_Interface_Settings.acticeInterface_EKOL AndAlso isFTP_EKOL_NCTS() Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\EKOL\OUT\", "EKOL"))
            If cVERAG_Interface_Settings.acticeInterface_GASTONSCHUL_OUT AndAlso ifGASTON_SCHUL() Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\GASTON_SCHUL\OUT\", "GASTON_SCHUL"))
            'If cVERAG_Interface_Settings.acticeInterface_TEST AndAlso isFTP_TEST_NCTS() Then IF_Obj.Add(New cVERAG_InterfaceSharedObjects("\\ftps.verag.ag\FTP\TEST\OUT\", "TEST"))

            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
    End Function


    'Shared Function isFTP_EKOL_NCTS_OLD() As Boolean
    '    Try
    '        If GLO_AVISO IsNot Nothing Then ' FRÄCHTER,AVISIERER
    '            If GLO_AVISO.Frächter_KdNr > 0 Then
    '                Select Case GLO_AVISO.Frächter_KdNr
    '                    Case 724284, 717845, 709869, 629246, 100041, 609759, 703729, 711217 'Zukunft: Kundenstamm--> NCTS_OUT_Partnersystem = "EKOL"
    '                        Return True
    '                End Select
    '                Select Case GLO_AVISO.Auftraggeber_KdNr
    '                    Case 724284, 717845, 709869, 629246, 100041, 609759, 703729, 711217
    '                        Return True
    '                End Select
    '            End If

    '            If GLO_AVISO.Frächter.ToUpper.StartsWith("EKOL") Then Return True
    '            If GLO_AVISO.Auftraggeber.ToUpper.StartsWith("EKOL") Then Return True
    '        End If

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


    Shared Function isFTP_EKOL_NCTS() As Boolean
        Try
            If GLO_SND IsNot Nothing Then ' FRÄCHTER,AVISIERER
                If GLO_SND.tblSnd_FrachtfuehrerKdNr > 0 Then
                    Select Case GLO_SND.tblSnd_FrachtfuehrerKdNr
                        Case 724284, 717845, 709869, 629246, 100041, 609759, 703729, 711217 'Zukunft: Kundenstamm--> NCTS_OUT_Partnersystem = "EKOL"
                            Return True
                    End Select
                    Select Case GLO_SND.tblSnd_AvisiererKdNr
                        Case 724284, 717845, 709869, 629246, 100041, 609759, 703729, 711217
                            Return True
                    End Select
                End If

                If If(GLO_SND.tblSnd_Frachtfuehrer, "").ToUpper.StartsWith("EKOL") Then Return True
                If If(GLO_SND.tblSnd_Avisierer, "").ToUpper.StartsWith("EKOL") Then Return True
            End If

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




    Shared Function ifGASTON_SCHUL() As Boolean
        Try
            If GLO_SND IsNot Nothing Then ' FRÄCHTER,AVISIERER
                If GLO_SND.tblSnd_NCTSVerzollungsadresse.Contains("GASTON SCHUL") Then
                    Return True
                End If
            End If

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

    Shared Function isFTP_TEST_NCTS() As Boolean
        Try
            If GLO_SND IsNot Nothing Then ' AndAlso GLO_snd.Frächter_KdNr > 0 Then ' FRÄCHTER,AVISIERER
                If GLO_SND.tblSnd_Frachtfuehrer.ToUpper.StartsWith("IBRAH") Then Return True
                If GLO_SND.tblSnd_Avisierer.ToUpper.StartsWith("IBRAH") Then Return True
            End If

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


    'Shared Function isFTP_TEST_NCTS() As Boolean
    '    Try
    '        If GLO_AVISO IsNot Nothing Then ' AndAlso GLO_AVISO.Frächter_KdNr > 0 Then ' FRÄCHTER,AVISIERER
    '            If GLO_AVISO.Frächter.ToUpper.StartsWith("IBRAH") Then Return True
    '            If GLO_AVISO.Auftraggeber.ToUpper.StartsWith("IBRAH") Then Return True
    '        End If

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


    Shared Function alreadyProvidedInInterface(ReferenzNumber As String, ProcedureCode As String, Status As String, vorsystem As String, ftp_CooName As String, Optional checkAlreadyProvided As Boolean = True) As Boolean
        If Not checkAlreadyProvided Then Return False ' wenn es nicht geprüft werden soll, dann wird "Nicht bereitgestellt" zurückgegeben

        Try
            '     If SQL.getValueTxtBySql("SELECT count(*)  FROM  [tblFTP_Log]  where [ftp_BezugsNr]='" & ReferenzNumber & "' AND [ftp_art]='" & ProcedureCode & "' AND [ftp_status]='" & Status & "' AND [ftp_vorsystem]='" & vorsystem & "' AND [ftp_CooName]='" & ftp_CooName & "'   ", "FMZOLL",,, 0) = 0 Then
            If SQL.getValueTxtBySql("SELECT count(*)  FROM  tblAPI  where [api_bezugsnr]='" & ReferenzNumber & "' AND [api_apiUArt]='" & ProcedureCode & "' AND api_apiZollStatus='" & Status & "' AND api_Vorsystem='" & vorsystem & "' AND api_Partnersystem='" & ftp_CooName & "'   ", "ADMIN",,, 0) = 0 Then
                Return False
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return True
    End Function

    Shared Function genXMLFile(VERAG_NCTS As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_ncts, ByRef XMLFilePath As String, Code As String, UniqueId As String) As String
        Try
            If XMLFilePath = "" Then
                Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\" & Code & "\" & Now.Year & "\" & Now.ToShortDateString & "\"
                If Not System.IO.Directory.Exists(dir) Then System.IO.Directory.CreateDirectory(dir)
                XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_" & Code & "_" & UniqueId & ".xml"
                While System.IO.File.Exists(XMLFilePath)
                    XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmssffff") & "_" & Code & "_" & UniqueId & ".xml"
                End While
            End If
            If VERAG_NCTS IsNot Nothing Then
                VERAG_NCTS.SaveToFile(XMLFilePath)
                removeTypeFromXML(XMLFilePath) ' Type-Attribute entfernen
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath
    End Function

    Shared Function genXMLFile_V2_0_1(VERAG_NCTS As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_ncts, ByRef XMLFilePath As String, Code As String, UniqueId As String) As String
        Try
            If XMLFilePath = "" Then
                Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\" & Code & "\" & Now.Year & "\" & Now.ToShortDateString & "\"
                If Not System.IO.Directory.Exists(dir) Then System.IO.Directory.CreateDirectory(dir)
                XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_" & Code & "_" & UniqueId & ".xml"
            End If
            If VERAG_NCTS IsNot Nothing Then
                VERAG_NCTS.SaveToFile(XMLFilePath)
                removeTypeFromXML(XMLFilePath) ' Type-Attribute entfernen
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath
    End Function

    Shared Function genXMLFile_V2_0_2(VERAG_NCTS As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts, ByRef XMLFilePath As String, Code As String, UniqueId As String) As String
        Try
            If XMLFilePath = "" Then
                Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\" & Code & "\" & Now.Year & "\" & Now.ToShortDateString & "\"
                If Not System.IO.Directory.Exists(dir) Then System.IO.Directory.CreateDirectory(dir)
                XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_" & Code & "_" & UniqueId & ".xml"
            End If
            If VERAG_NCTS IsNot Nothing Then
                VERAG_NCTS.SaveToFile(XMLFilePath)
                removeTypeFromXML(XMLFilePath) ' Type-Attribute entfernen
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath

    End Function
    Shared Function removeTypeFromXML(XMLFilePath) As String
        Try
            Dim fileContents = System.IO.File.ReadAllText(XMLFilePath)
            fileContents = fileContents.Replace(" xsi:type=""xsd:string""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:dateTime""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:decimal""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:int""", "")
            System.IO.File.WriteAllText(XMLFilePath, fileContents)
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath
    End Function


    Shared Function genVERAG_OUT_NCTS_FromDAKOSY(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, 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 DY_TMP Is Nothing Then Return Nothing
            If NCTS_TMP Is Nothing Then Return Nothing
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return Nothing
            NCTS_TMP.LOAD_Warenposition()

            VERAG_NCTS.Version = "2.0.0" ' Automaitsch, weil default ' doch nicht?

            If NCTS_TMP.ncts_firma <> "" Then
                VERAG_NCTS.Company = NCTS_TMP.ncts_firma
            Else
                VERAG_NCTS.Company = "VERAG"
            End If
            VERAG_NCTS.Department = NCTS_TMP.ncts_niederlassung

            'VERAG_NCTS.InternalReference.OfficeCode = If(
            'VERAG_NCTS.InternalReference.PositionNumber = NCTS_TMP.ncts_niederlassung

            Select Case DY_TMP.dy_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.ncts_Trans_DatumZeit

            VERAG_NCTS.ProcessData.MRN = If(NCTS_TMP.ncts_MRN, "")
            VERAG_NCTS.ProcessData.ReferenzNumber = If(NCTS_TMP.ncts_ObjectName, "")
            VERAG_NCTS.ProcessData.ProcedureCode = If(NCTS_TMP.ncts_ArtAnmeldung, "")

            If IsDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, "")) Then VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, ""))
            VERAG_NCTS.NCTS_Data.LicensePlate = If(NCTS_TMP.ncts_Befoerderungsmittel_KennzeichenAbgang, "")
            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = If(NCTS_TMP.ncts_AbgangsDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = If(NCTS_TMP.ncts_BestimmungsstelleDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DepartureCountry = If(NCTS_TMP.ncts_VersendungsLandCode, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.DestinationCountry = If(NCTS_TMP.ncts_BestimmungslandCode, "").replace("SRB", "RS")

            'Versender
            VERAG_NCTS.NCTS_Data.Sender.Name = If(NCTS_TMP.ncts_Versender_Name, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI = If(NCTS_TMP.ncts_Versender_EORI, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Country = If(NCTS_TMP.ncts_Versender_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = If(NCTS_TMP.ncts_Versender_PLZ, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Street = If(NCTS_TMP.ncts_Versender_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.City = If(NCTS_TMP.ncts_Versender_Ort, "")

            'Empfänger
            VERAG_NCTS.NCTS_Data.Consignee.Name = If(NCTS_TMP.ncts_Empfaenger_Name, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI = If(NCTS_TMP.ncts_Empfaenger_EORI, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Consignee.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = If(NCTS_TMP.ncts_Empfaenger_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = If(NCTS_TMP.ncts_Empfaenger_PLZ, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = If(NCTS_TMP.ncts_Empfaenger_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.City = If(NCTS_TMP.ncts_Empfaenger_Ort, "")

            'Frächter
            VERAG_NCTS.NCTS_Data.Carrier.Name = If(NCTS_TMP.ncts_SumA_Befoerderer_Name, "")
            VERAG_NCTS.NCTS_Data.Carrier.EORI = If(NCTS_TMP.ncts_SumA_Befoerderer_EORI, "")
            VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_SumA_Befoerderer_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(NCTS_TMP.ncts_SumA_Befoerderer_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(NCTS_TMP.ncts_SumA_Befoerderer_PLZ, "")
            VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(NCTS_TMP.ncts_SumA_Befoerderer_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(NCTS_TMP.ncts_SumA_Befoerderer_Ort, "")

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

            VERAG_NCTS.NCTS_Data.SumGrossWeight = If(NCTS_TMP.ncts_GesamtRohmasse, "0")
            Dim cntPos = 1
            If NCTS_TMP.ncts_WARENPOS IsNot Nothing Then
                For Each POS In NCTS_TMP.ncts_WARENPOS
                    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.nctsWP_PACKSTUECKE IsNot Nothing Then
                        For Each PK In POS.nctsWP_PACKSTUECKE
                            If PK.nctsWpPk_PackstueckeAnzahl IsNot Nothing Then pkGes += PK.nctsWpPk_PackstueckeAnzahl
                            If PK.nctsWpPk_Verpackungsart IsNot Nothing Then pkArt = PK.nctsWpPk_Verpackungsart
                        Next
                    End If
                    V_POS.Quantity = pkGes
                    V_POS.Packaging = pkArt
                    V_POS.GrossWeight = POS.nctsWP_Rohmasse
                    V_POS.GoodsDescription = POS.nctsWP_Warenbezeichnung

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

            If False Then
                Try
                    If DY_TMP.dy_VBD_dsId IsNot Nothing AndAlso IsNumeric(DY_TMP.dy_VBD_dsId) Then
                        Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(DY_TMP.dy_VBD_dsId)
                        Dim Path = DS.GET_TOP1_PATH()
                        If Path <> "" And System.IO.File.Exists(Path) Then
                            Dim ATT As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_nctsAttachment
                            ATT.DocumentType = VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS.VERAG_out_nctsAttachmentDocumentType.NCTS
                            ATT.FileType = "PDF"
                            ATT.Name = "NCTS_VBD_" & If(VERAG_NCTS.ProcessData.ProcedureCode, "") & ".pdf"
                            Convert.ToBase64String(System.IO.File.ReadAllBytes(Path))
                            'ATT.base64BinaryStream = Convert.ToBase64CharArray(System.IO.File.ReadAllBytes(Path))
                            ATT.base64BinaryStream = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(System.IO.File.ReadAllBytes(Path)))
                            VERAG_NCTS.Attachments.Add(ATT)
                        End If
                    End If
                Catch ex As Exception
                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                End Try
            End If

            'If XMLFilePath = "" Then
            '    Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\DY\" & Now.Year & "\" & Now.ToShortDateString & "\"
            '    If Not IO.Directory.Exists(dir) Then IO.Directory.CreateDirectory(dir)
            '    XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_DY_" & DY_TMP.dy_Id & ".xml"
            'End If

            UniqueId = DY_TMP.dy_Id
            'VERAG_NCTS.SaveToFile(XMLFilePath)

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

    End Function



    Shared Function genVERAG_OUT_NCTS_FromDAKOSY_V2_0_1(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, ByRef UniqueId As String) As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_ncts
        Try
            If DY_TMP Is Nothing Then Return Nothing
            If NCTS_TMP Is Nothing Then Return Nothing
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return Nothing
            NCTS_TMP.LOAD_Warenposition()

            VERAG_NCTS.Version = "2.0.1" ' Automaitsch, weil default ' doch nicht?

            If NCTS_TMP.ncts_firma <> "" Then
                VERAG_NCTS.Company = NCTS_TMP.ncts_firma
            Else
                VERAG_NCTS.Company = "VERAG"
            End If
            VERAG_NCTS.Department = NCTS_TMP.ncts_niederlassung

            'VERAG_NCTS.InternalReference.OfficeCode = If(
            'VERAG_NCTS.InternalReference.PositionNumber = NCTS_TMP.ncts_niederlassung

            Select Case DY_TMP.dy_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.ncts_Trans_DatumZeit

            VERAG_NCTS.ProcessData.MRN = If(NCTS_TMP.ncts_MRN, "")
            VERAG_NCTS.ProcessData.ReferenzNumber = If(NCTS_TMP.ncts_ObjectName, "")
            VERAG_NCTS.ProcessData.ProcedureCode = If(NCTS_TMP.ncts_ArtAnmeldung, "")

            If IsDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, "")) Then VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, ""))
            VERAG_NCTS.NCTS_Data.LicensePlate = If(NCTS_TMP.ncts_Befoerderungsmittel_KennzeichenAbgang, "")
            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = If(NCTS_TMP.ncts_AbgangsDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = If(NCTS_TMP.ncts_BestimmungsstelleDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DepartureCountry = If(NCTS_TMP.ncts_VersendungsLandCode, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.DestinationCountry = If(NCTS_TMP.ncts_BestimmungslandCode, "").replace("SRB", "RS")

            'Versender
            VERAG_NCTS.NCTS_Data.Sender.Name = If(NCTS_TMP.ncts_Versender_Name, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI = If(NCTS_TMP.ncts_Versender_EORI, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Country = If(NCTS_TMP.ncts_Versender_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = If(NCTS_TMP.ncts_Versender_PLZ, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Street = If(NCTS_TMP.ncts_Versender_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.City = If(NCTS_TMP.ncts_Versender_Ort, "")

            'Empfänger
            VERAG_NCTS.NCTS_Data.Consignee.Name = If(NCTS_TMP.ncts_Empfaenger_Name, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI = If(NCTS_TMP.ncts_Empfaenger_EORI, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Consignee.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = If(NCTS_TMP.ncts_Empfaenger_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = If(NCTS_TMP.ncts_Empfaenger_PLZ, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = If(NCTS_TMP.ncts_Empfaenger_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.City = If(NCTS_TMP.ncts_Empfaenger_Ort, "")

            'Frächter
            Dim FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "").replace("SRB", "RS")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR
            Else
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(NCTS_TMP.ncts_SumA_Befoerderer_Name, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(NCTS_TMP.ncts_SumA_Befoerderer_EORI, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_SumA_Befoerderer_NLNR, "")
                'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.vers, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(NCTS_TMP.ncts_SumA_Befoerderer_Land, "").replace("SRB", "RS")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(NCTS_TMP.ncts_SumA_Befoerderer_PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(NCTS_TMP.ncts_SumA_Befoerderer_StrasseHausnummer, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(NCTS_TMP.ncts_SumA_Befoerderer_Ort, "")
            End If



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

            VERAG_NCTS.NCTS_Data.SumGrossWeight = If(NCTS_TMP.ncts_GesamtRohmasse, "0")
            Dim cntPos = 1
            If NCTS_TMP.ncts_WARENPOS IsNot Nothing Then
                For Each POS In NCTS_TMP.ncts_WARENPOS
                    Dim V_POS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_nctsNCTS_DataPosition
                    V_POS.PosNr = cntPos
                    Dim pkGes = 0
                    Dim pkArt = ""
                    If POS.nctsWP_PACKSTUECKE IsNot Nothing Then
                        For Each PK In POS.nctsWP_PACKSTUECKE
                            If PK.nctsWpPk_PackstueckeAnzahl IsNot Nothing Then pkGes += PK.nctsWpPk_PackstueckeAnzahl
                            If PK.nctsWpPk_Verpackungsart IsNot Nothing Then pkArt = PK.nctsWpPk_Verpackungsart
                        Next
                    End If
                    V_POS.Quantity = pkGes
                    V_POS.Packaging = pkArt
                    V_POS.GrossWeight = POS.nctsWP_Rohmasse
                    V_POS.GoodsDescription = POS.nctsWP_Warenbezeichnung

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

            If False Then
                Try
                    If DY_TMP.dy_VBD_dsId IsNot Nothing AndAlso IsNumeric(DY_TMP.dy_VBD_dsId) Then
                        Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(DY_TMP.dy_VBD_dsId)
                        Dim Path = DS.GET_TOP1_PATH()
                        If Path <> "" And System.IO.File.Exists(Path) Then
                            Dim ATT As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_nctsAttachment
                            ATT.DocumentType = VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_nctsAttachmentDocumentType.NCTS
                            ATT.FileType = "PDF"
                            ATT.Name = "NCTS_VBD_" & If(VERAG_NCTS.ProcessData.ProcedureCode, "") & ".pdf"
                            Convert.ToBase64String(System.IO.File.ReadAllBytes(Path))
                            'ATT.base64BinaryStream = Convert.ToBase64CharArray(System.IO.File.ReadAllBytes(Path))
                            ATT.base64BinaryStream = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(System.IO.File.ReadAllBytes(Path)))
                            VERAG_NCTS.Attachments.Add(ATT)
                        End If
                    End If
                Catch ex As Exception
                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                End Try

            End If
            'If XMLFilePath = "" Then
            '    Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\DY\" & Now.Year & "\" & Now.ToShortDateString & "\"
            '    If Not IO.Directory.Exists(dir) Then IO.Directory.CreateDirectory(dir)
            '    XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_DY_" & DY_TMP.dy_Id & ".xml"
            'End If

            UniqueId = DY_TMP.dy_Id
            'VERAG_NCTS.SaveToFile(XMLFilePath)

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

    End Function

    Shared Function getAuftraggeberKdnr() As Integer
        If GLO_SND IsNot Nothing Then
            If GLO_SND.tblSnd_AuftraggeberKdNr > 0 Then
                Return GLO_SND.tblSnd_AuftraggeberKdNr
            End If
        End If

        Return -1
    End Function
    Shared Function getFraechterKdnr() As Integer
        If GLO_SND IsNot Nothing Then
            If GLO_SND.tblSnd_FrachtfuehrerKdNr > 0 Then
                Return GLO_SND.tblSnd_FrachtfuehrerKdNr
            End If
        End If
        If GLO_AVISO IsNot Nothing Then
            If GLO_AVISO.Frächter_KdNr > 0 Then
                Return GLO_AVISO.Frächter_KdNr
            End If
        End If
        Return -1
    End Function
    Shared Function getFraechterKdNrName() As String
        If GLO_SND IsNot Nothing Then
            If GLO_SND.tblSnd_FrachtfuehrerKdNr > 0 Then
                Return "KDNR: " & GLO_SND.tblSnd_FrachtfuehrerKdNr & " NAME: " & GLO_SND.tblSnd_Frachtfuehrer
            ElseIf GLO_SND.tblSnd_Frachtfuehrer <> "" Then
                Return " NAME: " & GLO_SND.tblSnd_Frachtfuehrer
            End If
        End If
        If GLO_AVISO IsNot Nothing Then
            If GLO_AVISO.Frächter_KdNr > 0 Then
                Return "KDNR: " & GLO_AVISO.Frächter_KdNr & " NAME: " & GLO_AVISO.Frächter
            Else
                Return " NAME: " & GLO_AVISO.Frächter
            End If
        End If
        Return ""
    End Function

    Shared Function getAvisiererKdNrName() As String
        If GLO_SND IsNot Nothing Then
            If GLO_SND.tblSnd_AvisiererKdNr > 0 Then
                Return "KDNR: " & GLO_SND.tblSnd_AvisiererKdNr & " NAME: " & GLO_SND.tblSnd_Avisierer
            ElseIf GLO_SND.tblSnd_Avisierer <> "" Then
                Return " NAME: " & GLO_SND.tblSnd_Avisierer
            End If
        End If
        If GLO_AVISO IsNot Nothing Then
            If GLO_AVISO.Auftraggeber_KdNr > 0 Then
                Return "KDNR: " & GLO_AVISO.Auftraggeber_KdNr & " NAME: " & GLO_AVISO.Auftraggeber
            ElseIf GLO_AVISO.Auftraggeber <> "" Then
                Return " NAME: " & GLO_AVISO.Auftraggeber_KdNr
            End If
        End If
        Return ""
    End Function
    Shared Function getAuftraggeberKdNrName() As String
        If GLO_SND IsNot Nothing Then

            If GLO_SND.tblSnd_AuftraggeberKdNr > 0 Then
                Return "KDNR: " & GLO_SND.tblSnd_AuftraggeberKdNr & " NAME: " & GLO_SND.tblSnd_Auftraggeber
            Else
                Return " NAME: " & GLO_SND.tblSnd_Auftraggeber
            End If
        End If
        Return ""
    End Function

    Shared Function getTOBBKdnr() As Integer
        Dim KdNr = -1
        If KdNr < 0 Then KdNr = getTOBBKdnrFromKdNr(getAuftraggeberKdnr())
        If KdNr < 0 Then KdNr = getTOBBKdnrFromKdNr(getFraechterKdnr())
        If KdNr < 0 Then KdNr = getTOBBKdnrFromKdNr(getAvisiererKdnr())
        Return KdNr
    End Function

    Shared Function getTOBBKdnrFromKdNr(KdNr) As Integer
        If KdNr > 0 Then
            Dim KD_ERW As New VERAG_PROG_ALLGEMEIN.cKundenErweitert(KdNr)
            Return If(KD_ERW.kde_TOBB_KundenNr, -1)
        End If
        Return -1
    End Function
    Shared Function getTOBBDetails() As String

        getTOBBDetails = vbNewLine & "Auftraggeber: " & getAuftraggeberKdNrName() & vbNewLine & "Frächter: " & getFraechterKdNrName() & vbNewLine & "Avisierer: " & getAvisiererKdNrName()
        getTOBBDetails &= vbNewLine & "AVISO: " & If(GLO_AVISO Is Nothing, "keine Zuweisung ", GLO_AVISO.AvisoID)
        getTOBBDetails &= vbNewLine & "SENDUNG: " & If(GLO_SND Is Nothing, "keine Zuweisung ", GLO_SND.tblSnd_SendungID)

    End Function
    Shared Function getAvisiererKdnr() As Integer
        If GLO_SND IsNot Nothing Then
            If GLO_SND.tblSnd_AvisiererKdNr > 0 Then
                Return GLO_SND.tblSnd_AvisiererKdNr
            End If
        End If
        If GLO_AVISO IsNot Nothing Then
            If GLO_AVISO.Auftraggeber_KdNr > 0 Then
                Return GLO_AVISO.Auftraggeber_KdNr
            End If
        End If
        Return -1
    End Function

    Shared Function genVERAG_OUT_NCTS_FromDAKOSY_V2_0_2(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, ByRef UniqueId As String, Optional ClientReference As String = "") As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Try
            If DY_TMP Is Nothing Then Return Nothing
            If NCTS_TMP Is Nothing Then Return Nothing
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return Nothing
            NCTS_TMP.LOAD_Warenposition()

            VERAG_NCTS.Version = "2.0.2" ' Automaitsch, weil default ' doch nicht?

            If NCTS_TMP.ncts_firma <> "" Then
                VERAG_NCTS.Company = NCTS_TMP.ncts_firma
            Else
                VERAG_NCTS.Company = "VERAG"
            End If

            '    If VERAG_NCTS.Company = "UNISPED" Then Return Nothing ' ---> KEIN UNISPED, erst klären!!!!!!!

            VERAG_NCTS.Department = NCTS_TMP.ncts_niederlassung

            'VERAG_NCTS.InternalReference.OfficeCode = If(
            'VERAG_NCTS.InternalReference.PositionNumber = NCTS_TMP.ncts_niederlassung

            Select Case DY_TMP.dy_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.ncts_Trans_DatumZeit

            VERAG_NCTS.ProcessData.MRN = If(NCTS_TMP.ncts_MRN, "")
            VERAG_NCTS.ProcessData.ReferenzNumber = If(NCTS_TMP.ncts_ObjectName, "")
            VERAG_NCTS.ProcessData.ProcedureCode = If(NCTS_TMP.ncts_ArtAnmeldung, "")

            If IsDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, "")) Then VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(If(NCTS_TMP.ncts_Wiedergestellungsdatum, ""))
            VERAG_NCTS.NCTS_Data.LicensePlate = If(NCTS_TMP.ncts_Befoerderungsmittel_KennzeichenAbgang, "")
            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = If(NCTS_TMP.ncts_AbgangsDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = If(NCTS_TMP.ncts_BestimmungsstelleDienststellennummer, "")
            VERAG_NCTS.NCTS_Data.DepartureCountry = If(NCTS_TMP.ncts_VersendungsLandCode, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.DestinationCountry = If(NCTS_TMP.ncts_BestimmungslandCode, "").replace("SRB", "RS")

            'Versender
            VERAG_NCTS.NCTS_Data.Sender.Name = If(NCTS_TMP.ncts_Versender_Name, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI = If(NCTS_TMP.ncts_Versender_EORI, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Versender_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Country = If(NCTS_TMP.ncts_Versender_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = If(NCTS_TMP.ncts_Versender_PLZ, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Street = If(NCTS_TMP.ncts_Versender_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.City = If(NCTS_TMP.ncts_Versender_Ort, "")

            'Empfänger
            VERAG_NCTS.NCTS_Data.Consignee.Name = If(NCTS_TMP.ncts_Empfaenger_Name, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI = If(NCTS_TMP.ncts_Empfaenger_EORI, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Empfaenger_NLNR, "")
            'VERAG_NCTS.NCTS_Data.Consignee.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = If(NCTS_TMP.ncts_Empfaenger_Land, "").replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = If(NCTS_TMP.ncts_Empfaenger_PLZ, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = If(NCTS_TMP.ncts_Empfaenger_StrasseHausnummer, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.City = If(NCTS_TMP.ncts_Empfaenger_Ort, "")

            'Frächter
            Dim FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "").replace("SRB", "RS")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR

            Else
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(NCTS_TMP.ncts_SumA_Befoerderer_Name, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(NCTS_TMP.ncts_SumA_Befoerderer_EORI, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_SumA_Befoerderer_NLNR, "")
                'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.vers, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(NCTS_TMP.ncts_SumA_Befoerderer_Land, "").replace("SRB", "RS")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(NCTS_TMP.ncts_SumA_Befoerderer_PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(NCTS_TMP.ncts_SumA_Befoerderer_StrasseHausnummer, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(NCTS_TMP.ncts_SumA_Befoerderer_Ort, "")
            End If

            If ClientReference <> "" Then
                VERAG_NCTS.ClientReference = ClientReference
            Else
                Dim AVISIERER_KDNR = getAvisiererKdnr()
                If AVISIERER_KDNR > 0 Then
                    VERAG_NCTS.ClientReference = AVISIERER_KDNR
                Else
                    VERAG_NCTS.ClientReference = ""
                End If
            End If

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

            VERAG_NCTS.NCTS_Data.SumGrossWeight = If(NCTS_TMP.ncts_GesamtRohmasse, "0")
            Dim cntPos = 1
            If NCTS_TMP.ncts_WARENPOS IsNot Nothing Then
                For Each POS In NCTS_TMP.ncts_WARENPOS
                    Dim V_POS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_nctsNCTS_DataPosition
                    V_POS.PosNr = cntPos
                    Dim pkGes = 0
                    Dim pkArt = ""
                    If POS.nctsWP_PACKSTUECKE IsNot Nothing Then
                        For Each PK In POS.nctsWP_PACKSTUECKE
                            If PK.nctsWpPk_PackstueckeAnzahl IsNot Nothing Then pkGes += PK.nctsWpPk_PackstueckeAnzahl
                            If PK.nctsWpPk_Verpackungsart IsNot Nothing Then pkArt = PK.nctsWpPk_Verpackungsart
                        Next
                    End If
                    V_POS.Quantity = pkGes
                    V_POS.Packaging = pkArt
                    V_POS.GrossWeight = POS.nctsWP_Rohmasse
                    V_POS.GoodsDescription = POS.nctsWP_Warenbezeichnung

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

            If False Then
                Try
                    If DY_TMP.dy_VBD_dsId IsNot Nothing AndAlso IsNumeric(DY_TMP.dy_VBD_dsId) Then
                        Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(DY_TMP.dy_VBD_dsId)
                        Dim Path = DS.GET_TOP1_PATH()
                        If Path <> "" And System.IO.File.Exists(Path) Then
                            Dim ATT As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_nctsAttachment
                            ATT.DocumentType = VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_nctsAttachmentDocumentType.NCTS
                            ATT.FileType = "PDF"
                            ATT.Name = "NCTS_VBD_" & If(VERAG_NCTS.ProcessData.ProcedureCode, "") & ".pdf"
                            Convert.ToBase64String(System.IO.File.ReadAllBytes(Path))
                            'ATT.base64BinaryStream = Convert.ToBase64CharArray(System.IO.File.ReadAllBytes(Path))
                            ATT.base64BinaryStream = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(System.IO.File.ReadAllBytes(Path)))
                            VERAG_NCTS.Attachments.Add(ATT)
                        End If
                    End If
                Catch ex As Exception
                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine & "BezugsNr: " & NCTS_TMP.ncts_ObjectName, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                End Try

            End If
            'If XMLFilePath = "" Then
            '    Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\VERAG\NCTS_OUT\DY\" & Now.Year & "\" & Now.ToShortDateString & "\"
            '    If Not IO.Directory.Exists(dir) Then IO.Directory.CreateDirectory(dir)
            '    XMLFilePath = dir & "VERAG_NCTS_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_DY_" & DY_TMP.dy_Id & ".xml"
            'End If

            UniqueId = DY_TMP.dy_Id
            'VERAG_NCTS.SaveToFile(XMLFilePath)

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

    End Function


    Shared Function genVERAG_OUT_NCTS004_FromDAKOSY_V2_0_2(NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut004, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, ByRef UniqueId As String, Optional ClientReference As String = "") As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Try
            If DY_TMP Is Nothing Then Return Nothing
            If NCTS_TMP Is Nothing Then Return Nothing
            If NCTS_TMP.ncts_ObjectName Is Nothing Then Return Nothing
            NCTS_TMP.LOAD_HouseConsignment()

            VERAG_NCTS.Version = "2.0.2" ' Automaitsch, weil default ' doch nicht?

            If NCTS_TMP.ncts_firma <> "" Then
                VERAG_NCTS.Company = NCTS_TMP.ncts_firma
            Else
                VERAG_NCTS.Company = "VERAG"
            End If

            '    If VERAG_NCTS.Company = "UNISPED" Then Return Nothing ' ---> KEIN UNISPED, erst klären!!!!!!!

            VERAG_NCTS.Department = NCTS_TMP.ncts_niederlassung

            'VERAG_NCTS.InternalReference.OfficeCode = If(
            'VERAG_NCTS.InternalReference.PositionNumber = NCTS_TMP.ncts_niederlassung

            Select Case DY_TMP.dy_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.ncts_Trans_DatumZeit

            VERAG_NCTS.ProcessData.MRN = If(NCTS_TMP.ncts_MRN, "")
            VERAG_NCTS.ProcessData.ReferenzNumber = If(NCTS_TMP.ncts_ObjectName, "")
            VERAG_NCTS.ProcessData.ProcedureCode = If(NCTS_TMP.ncts_Declarationtype, "")

            If IsDate(If(NCTS_TMP.ncts_LimitDate, "")) Then VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(If(NCTS_TMP.ncts_LimitDate, ""))
            VERAG_NCTS.NCTS_Data.LicensePlate = If(NCTS_TMP.ncts_ActiveBorderTransportMeans_1_IdentificationNumber, "")
            VERAG_NCTS.NCTS_Data.DepartureCustomsOffice = If(NCTS_TMP.ncts_CustomsOfficeOfDeparture, "")
            VERAG_NCTS.NCTS_Data.DestinationCustomsOffice = If(NCTS_TMP.ncts_CustomsOfficeOfDestinationDeclared, "")
            VERAG_NCTS.NCTS_Data.DepartureCountry = If(NCTS_TMP.ncts_CountryOfDispatch, "").Replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.DestinationCountry = If(NCTS_TMP.ncts_CountryOfDestination, "").Replace("SRB", "RS")

            'Versender
            VERAG_NCTS.NCTS_Data.Sender.Name = If(NCTS_TMP.ncts_Consignor_Name, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI = If(NCTS_TMP.ncts_Consignor_IdentificationNumber, "")
            VERAG_NCTS.NCTS_Data.Sender.EORI_NL = If(NCTS_TMP.ncts_Consignor_SubsidiaryNumber, "")
            'VERAG_NCTS.NCTS_Data.Sender.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Country = If(NCTS_TMP.ncts_Consignor_Country, "").Replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Postcode = If(NCTS_TMP.ncts_Consignor_Postcode, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.Street = If(NCTS_TMP.ncts_Consignor_StreetNumber, "")
            VERAG_NCTS.NCTS_Data.Sender.Adress.City = If(NCTS_TMP.ncts_Consignor_City, "")

            'Empfänger
            VERAG_NCTS.NCTS_Data.Consignee.Name = If(NCTS_TMP.ncts_Consignee_Name, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI = If(NCTS_TMP.ncts_Consignee_IdentificationNumber, "")
            VERAG_NCTS.NCTS_Data.Consignee.EORI_NL = If(NCTS_TMP.ncts_Consignee_SubsidiaryNumber, "")
            'VERAG_NCTS.NCTS_Data.consignee.UID_Nr = If(NCTS_TMP.vers, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Country = If(NCTS_TMP.ncts_Consignee_Country, "").Replace("SRB", "RS")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Postcode = If(NCTS_TMP.ncts_Consignee_Postcode, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.Street = If(NCTS_TMP.ncts_Consignee_StreetNumber, "")
            VERAG_NCTS.NCTS_Data.Consignee.Adress.City = If(NCTS_TMP.ncts_Consignee_City, "")


            'Frächter
            Dim FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "").replace("SRB", "RS")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR

            Else
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(NCTS_TMP.ncts_Carrier_ContactPerson_Name, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(NCTS_TMP.ncts_Carrier_IdentificationNumber, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(NCTS_TMP.ncts_Carrier_SubsidiaryNumber, "")
                'VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(NCTS_TMP.vers, "")
                ' VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(NCTS_TMP.ncts_Carrier_, "").Replace("SRB", "RS")
                'VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(NCTS_TMP.ncts_SumA_Befoerderer_PLZ, "")
                'VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(NCTS_TMP.ncts_SumA_Befoerderer_StrasseHausnummer, "")
                'VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(NCTS_TMP.ncts_SumA_Befoerderer_Ort, "")
            End If

            If ClientReference <> "" Then
                VERAG_NCTS.ClientReference = ClientReference
            Else
                Dim AVISIERER_KDNR = getAvisiererKdnr()
                If AVISIERER_KDNR > 0 Then
                    VERAG_NCTS.ClientReference = AVISIERER_KDNR
                Else
                    VERAG_NCTS.ClientReference = ""
                End If
            End If

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

            VERAG_NCTS.NCTS_Data.SumGrossWeight = If(NCTS_TMP.ncts_GrossMass, "0")
            Dim cntPos = 1
            If NCTS_TMP.ncts_HOUSE_CONSIGNMENT IsNot Nothing Then
                For Each HC In NCTS_TMP.ncts_HOUSE_CONSIGNMENT
                    For Each POS In HC.nctsWP_CONSIGNMENT_ITEM
                        Dim V_POS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_nctsNCTS_DataPosition
                        V_POS.PosNr = cntPos
                        Dim pkGes = 0
                        Dim pkArt = ""
                        If POS.nctsHCCI_PACKAGING IsNot Nothing Then
                            For Each PK In POS.nctsHCCI_PACKAGING
                                If PK.nctsHCCIPK_NumberOfPackages IsNot Nothing Then pkGes += PK.nctsHCCIPK_NumberOfPackages
                                If PK.nctsHCCIPK_TypeOfPackages IsNot Nothing Then pkArt = PK.nctsHCCIPK_TypeOfPackages
                            Next
                        End If
                        V_POS.Quantity = pkGes
                        V_POS.Packaging = pkArt
                        V_POS.GrossWeight = POS.nctsHCCI_GrossMass
                        V_POS.GoodsDescription = POS.nctsHCCI_DescriptionOfGoods

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

            UniqueId = DY_TMP.dy_Id
            'VERAG_NCTS.SaveToFile(XMLFilePath)

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

    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 = "2.0.0" ' Automaitsch, weil default ' doch nicht?

            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") 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


            If r.Item("GeaendertAm") IsNot DBNull.Value AndAlso IsDate(r.Item("GeaendertAm")) Then
                VERAG_NCTS.ProcessData.TransactionDate = CDate(r.Item("GeaendertAm")) 'default
            Else
                VERAG_NCTS.ProcessData.TransactionDate = Now.ToShortDateString
            End If

            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("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Na")) 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
                If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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

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

            VERAG_NCTS.Version = "2.0.1" ' Automaitsch, weil default ' doch nicht?

            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") 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


            If r.Item("GeaendertAm") IsNot DBNull.Value AndAlso IsDate(r.Item("GeaendertAm")) Then
                VERAG_NCTS.ProcessData.TransactionDate = CDate(r.Item("GeaendertAm")) 'default
            Else
                VERAG_NCTS.ProcessData.TransactionDate = Now.ToShortDateString
            End If

            VERAG_NCTS.ProcessData.MRN = If(r.Item("CRN") IsNot DBNull.Value, r.Item("CRN"), "")
            VERAG_NCTS.ProcessData.ReferenzNumber = LRN

            Select Case (SQL.getValueTxtBySql("SELECT isnull(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("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Na")) 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 FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR
            Else
                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
                    If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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
            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_V2_0_1.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


    Shared Function genVERAG_OUT_NCTS_FromEZOLLByAnmId_V2_0_2(AnmID As String, ByRef UniqueId As String, Optional ClientReference As String = "") As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Dim LRN = ""
        Try

            VERAG_NCTS.Version = "2.0.2" ' Automaitsch, weil default ' doch nicht?

            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") 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


            If r.Item("GeaendertAm") IsNot DBNull.Value AndAlso IsDate(r.Item("GeaendertAm")) Then
                VERAG_NCTS.ProcessData.TransactionDate = CDate(r.Item("GeaendertAm")) 'default
            Else
                VERAG_NCTS.ProcessData.TransactionDate = Now.ToShortDateString
            End If

            VERAG_NCTS.ProcessData.MRN = If(r.Item("CRN") IsNot DBNull.Value, r.Item("CRN"), "")
            VERAG_NCTS.ProcessData.ReferenzNumber = LRN

            Select Case (SQL.getValueTxtBySql("SELECT isnull(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("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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
                If Not SQL.IsNull(DTzsAnmConeeTra.Rows(0).Item("Na")) 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 FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)

                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR
            Else
                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
                    If Not SQL.IsNull(DTzsAnmCarrier.Rows(0).Item("Na")) Then VERAG_NCTS.NCTS_Data.Consignee.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
            End If

            If ClientReference <> "" Then
                VERAG_NCTS.ClientReference = ClientReference
            Else
                Dim AVISIERER_KDNR = getAvisiererKdnr()
                If AVISIERER_KDNR > 0 Then
                    VERAG_NCTS.ClientReference = AVISIERER_KDNR
                Else
                    VERAG_NCTS.ClientReference = ""
                End If
            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_V2_0_2.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

    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_V2_0_1(NCTS_TMP As TELOTEC_Worker.cTelotec_Anmeldung, ByRef UniqueId As String) As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_1.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 = "2.0.1" ' Automaitsch, weil default ' doch nicht?

            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, "")

            If NCTS_TMP.CtrlRslt_DLimit IsNot Nothing AndAlso NCTS_TMP.CtrlRslt_DLimit.ToString <> "" Then
                VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(NCTS_TMP.CtrlRslt_DLimit)
            End If
            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
            Dim FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR
            Else

                '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, "")

            End If


            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_V2_0_1.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 = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "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 genVERAG_OUT_NCTS_FromTELOTEC_V2_0_2(NCTS_TMP As TELOTEC_Worker.cTelotec_Anmeldung, ByRef UniqueId As String, Optional ClientReference As String = "") As VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.VERAG_out_ncts
        Dim VERAG_NCTS As New VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS_V2_0_2.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 = "2.0.2" ' Automaitsch, weil default ' doch nicht?

            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, "")

            If NCTS_TMP.CtrlRslt_DLimit IsNot Nothing AndAlso NCTS_TMP.CtrlRslt_DLimit.ToString <> "" Then
                VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(NCTS_TMP.CtrlRslt_DLimit)
            End If
            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
            Dim FRAECHTER_KDNR = getFraechterKdnr()
            If FRAECHTER_KDNR > 0 Then
                Dim AD = New VERAG_PROG_ALLGEMEIN.cAdressen(FRAECHTER_KDNR)
                Dim KD = New VERAG_PROG_ALLGEMEIN.cKunde(FRAECHTER_KDNR)
                VERAG_NCTS.NCTS_Data.Carrier.Name = If(AD.Name_1, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI = If(KD.EORITIN, "")
                VERAG_NCTS.NCTS_Data.Carrier.EORI_NL = If(KD.EORITIN_NL, "")
                VERAG_NCTS.NCTS_Data.Carrier.UID_Nr = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Country = If(AD.LandKz, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Postcode = If(AD.PLZ, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.Street = If(AD.Straße, "")
                VERAG_NCTS.NCTS_Data.Carrier.Adress.City = If(AD.Ort, "")
                VERAG_NCTS.NCTS_Data.Carrier.CustomerCode = FRAECHTER_KDNR
            Else

                '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, "")
            End If

            If ClientReference <> "" Then
                VERAG_NCTS.ClientReference = ClientReference
            Else
                Dim AVISIERER_KDNR = getAvisiererKdnr()
                If AVISIERER_KDNR > 0 Then
                    VERAG_NCTS.ClientReference = AVISIERER_KDNR
                Else
                    VERAG_NCTS.ClientReference = ""
                End If
            End If





            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_V2_0_2.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 = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "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 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 = "2.0.0" ' Automaitsch, weil default ' doch nicht?

            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, "")

            If NCTS_TMP.CtrlRslt_DLimit IsNot Nothing AndAlso NCTS_TMP.CtrlRslt_DLimit.ToString <> "" Then
                VERAG_NCTS.NCTS_Data.DatDeterminationLimit = CDate(NCTS_TMP.CtrlRslt_DLimit)
            End If
            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 = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "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, Firma As String, ReferenzNumber As String, ProcedureCode As String, Status As String, vorsystem As String, partnersystem As String, MRN As String)

        Dim API As New VERAG_PROG_ALLGEMEIN.cAPI

        API.api_InOut = VERAG_PROG_ALLGEMEIN.cAPI_INOUT.OUT
        API.api_Type = VERAG_PROG_ALLGEMEIN.cAPI_Type.FTP
        API.api_apiArt = VERAG_PROG_ALLGEMEIN.cAPI_ART.VERAG_OUT_NCTS
        API.api_Vorsystem = vorsystem
        API.api_Partnersystem = partnersystem
        API.api_Zollbelegnummer = MRN
        API.api_Firma = Firma
        API.api_apiUArt = ProcedureCode
        API.api_BezugsNr = ReferenzNumber
        API.api_filepathDestination = filePath
        API.api_apiZollStatus = Status

        If GLO_AVISO IsNot Nothing Then API.api_AvisoId = GLO_AVISO.AvisoID
        If GLO_SND IsNot Nothing Then API.api_SendungsId = GLO_SND.tblSnd_SendungID
        API.SAVE()
        API.UPDTAE_OK()
        '  Return SQL.doSQL("INSERT INTO tblFTP_Log (ftp_filepath,ftp_user,ftp_BezugsNr,ftp_art,ftp_status,ftp_vorsystem) VALUES ('" & filePath & "','" & user & "','" & ReferenzNumber & "','" & ProcedureCode & "','" & Status & "','" & vorsystem & "')", "FMZOLL")
    End Function


    'Shared Function insertFTPLOG(filePath, user, ftp_BezugsNr, ftp_art, ftp_status, ftp_vorsystem)
    '    Dim API As New VERAG_PROG_ALLGEMEIN.cAPI

    '    API.api_InOut = VERAG_PROG_ALLGEMEIN.cAPI_INOUT.OUT
    '    API.api_Type = VERAG_PROG_ALLGEMEIN.cAPI_Type.FTP
    '    API.api_apiArt = VERAG_PROG_ALLGEMEIN.cAPI_ART.VERAG_OUT_NCTS
    '    API.api_Vorsystem = vorsystem
    '    API.api_apiUArt = ProcedureCode
    '    API.api_BezugsNr = ReferenzNumber
    '    API.api_filepathDestination = filePath
    '    API.api_apiZollStatus = Status
    '    If GLO_AVISO IsNot Nothing Then API.api_AvisoId = GLO_AVISO.AvisoID
    '    If GLO_SND IsNot Nothing Then API.api_SendungsId = GLO_SND.tblSnd_SendungID
    '    API.SAVE()

    '    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

Class cGASTON_SCHUL_Interface


    Shared Sub copyNCTSToFTP_V1_0_0(ByRef GS_OUT As VERAG_PROG_ALLGEMEIN.agsCustomsExchange.exchangeRequestType, IF_Obj As cVERAG_InterfaceSharedObjects, ByRef vorsystem As String, ByRef filePath As String, ByRef destFile As String, ByRef UniqueId As String, Optional checkAlreadyProvided As Boolean = True, ByRef Optional found As Boolean = False)
        If GS_OUT IsNot Nothing AndAlso Not cVERAG_out_ncts.alreadyProvidedInInterface(GS_OUT.movementReferenceNumber, "OUT", "0", vorsystem, IF_Obj.toSystem, checkAlreadyProvided) Then
            If genXMLFile_V1_0_0(GS_OUT, filePath, vorsystem, UniqueId) <> "" Then
                frmStartOptions.copyFile(filePath, IF_Obj.FTP_Path, destFile)
                If System.IO.File.Exists(destFile) Then
                    cVERAG_out_ncts.insertFTPLOG(destFile, "VERAG", GS_OUT.movementReferenceNumber, "OUT", "0", vorsystem, IF_Obj.toSystem, GS_OUT.movementReferenceNumber)
                    found = True
                Else
                    Try
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP",,,,, GS_OUT.movementReferenceNumber)
                    Catch
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Datei existiert nicht: " & destFile, "", "copyNCTSToFTP")
                    End Try
                End If
            End If
        End If

    End Sub

    Shared Function genXMLFile_V1_0_0(GS_OUT As VERAG_PROG_ALLGEMEIN.agsCustomsExchange.exchangeRequestType, ByRef XMLFilePath As String, Code As String, UniqueId As String) As String
        Try
            If XMLFilePath = "" Then
                Dim dir = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir() & "ZOLL_SCHNITTSTELLEN\GASTON_SCHUL\OUT\" & Now.Year & "\" & Now.ToShortDateString & "\"
                If Not System.IO.Directory.Exists(dir) Then System.IO.Directory.CreateDirectory(dir)
                XMLFilePath = dir & "GASTON_SCHUL_OUT_" & Now.ToString("ddMMyyyHHmmss") & "_" & Code & "_" & UniqueId & ".xml"
            End If
            If GS_OUT IsNot Nothing Then
                GS_OUT.SaveToFile(XMLFilePath)
                removeTypeFromXML(XMLFilePath) ' Type-Attribute entfernen
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath

    End Function
    Shared Function removeTypeFromXML(XMLFilePath) As String
        Try
            Dim fileContents = System.IO.File.ReadAllText(XMLFilePath)
            fileContents = fileContents.Replace(" xsi:type=""xsd:string""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:dateTime""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:decimal""", "")
            fileContents = fileContents.Replace(" xsi:type=""xsd:int""", "")
            System.IO.File.WriteAllText(XMLFilePath, fileContents)
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return XMLFilePath
    End Function



End Class