Imports System.Globalization
Imports System.IO
Imports System.Xml
Imports VERAG_PROG_ALLGEMEIN

Public Class cMAVIWorker
    Public Shared Dateiname = ""

    Shared SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Shared Partnersystem = "" ' FUnzt noch nciht --> leer für zuordnung guid mrn, sonst problem

    Public Shared Function readNCTS_VersandanmeldungXML_CC015B(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer, d As String) As String
        Try
            readNCTS_VersandanmeldungXML_CC015B = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC015B") Then
                '   Dim xmlCC015B = XmlString.Substring(XmlString.IndexOf("<CC015B>"), XmlString.IndexOf("</CC015B>") + 9 - XmlString.IndexOf("<CC015B>"))
                '  MsgBox(xmlCC015B)
                '  Dim p = Path.GetTempFileName

                Dim LRN = ""
                If doc.GetElementsByTagName("RefNumHEA4").Count > 0 Then
                    LRN = doc.GetElementsByTagName("RefNumHEA4")(0).InnerText
                End If

                readNCTS_VersandanmeldungXML_CC015B = LRN


                'Using outputFile As New StreamWriter(p, False, System.Text.Encoding.Default)
                '    outputFile.Write(xmlCC015B)
                '    outputFile.Close()


                Dim AnsprechpTMP_Email = ""
                Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr(LRN, True,, Partnersystem)
                If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                NCTS.ncts_ObjectName = LRN

                NCTS.ncts_Partnersystem = ncts_Partnersystem


                If doc.GetElementsByTagName("GuaRefNumGRNREF1").Count > 0 Then ' AndAlso doc.GetElementsByTagName("AmoConREF7").Count > 0 Then
                    Dim BRG As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Sicherheitsangaben
                    BRG.nctsSA_GRN = doc.GetElementsByTagName("GuaRefNumGRNREF1")(0).InnerText
                    Dim Betrag = 0.0
                    If doc.GetElementsByTagName("AmoConREF7").Count > 0 Then Betrag = doc.GetElementsByTagName("AmoConREF7")(0).InnerText

                    If IsNumeric(Betrag) Then
                        BRG.nctsSA_AbgabenBetrag = CDbl(Betrag)
                    End If

                    NCTS.ncts_SICHERHEITSANGABEN = New List(Of VERAG_PROG_ALLGEMEIN.cNCTS_TR_Sicherheitsangaben)
                    NCTS.ncts_SICHERHEITSANGABEN.Add(BRG)
                End If


                If If(NCTS.ncts_Status, 0) <= 5 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_05
                ' If If(NCTS.ncts_Status_KEWILL_Equivalent, 0) <= 5 Then NCTS.ncts_Status_KEWILL_Equivalent = DAKOSY_Worker.cDY_Statusliste.S_05
                NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status


                '-------------------------------------------------------------------------------------------------
                '-------DATEN AUS TR NCTS LADEN-------------------------------------------------------------------
                '-------------------------------------------------------------------------------------------------
                cNCTSPartnerWorker.cTR_NCTS.GET_FROM_NCTSTR_CC015B(d, NCTS)
                '-------------------------------------------------------------------------------------------------
                '------------------------------------------------------------------------------------------------- 


                If NCTS.SAVE_ALL() Then
                    '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                    Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                    NR.nctsNr_nctsId = NCTS.ncts_Id
                    NR.nctsNr_typ = "CC015B"
                    NR.nctsNr_path = ""
                    NR.nctsNr_LRN = LRN
                    NR.nctsNr_MRN = Nothing
                    NR.nctsNr_GUID = Nothing
                    If NR.SAVE() Then NS_id = NR.nctsNr_id
                Else
                    readNCTS_VersandanmeldungXML_CC015B = ""
                End If


                'Try
                '    System.IO.File.Delete(p)
                'Catch ex As Exception
                '    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                'End Try

                FORM.addDGVEinarbeitung("NCTS: Versandanmeldung", readNCTS_VersandanmeldungXML_CC015B)

                '    End Using

            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_VersandanmeldungXML_CC015B = ""
        End Try
    End Function




    Public Shared Function readNCTS_VersandanmeldungXML_CC015B_RES(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_VersandanmeldungXML_CC015B_RES = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC015B_RES") Then
                '   Dim xmlCC015B = XmlString.Substring(XmlString.IndexOf("<CC015B>"), XmlString.IndexOf("</CC015B>") + 9 - XmlString.IndexOf("<CC015B>"))
                '  MsgBox(xmlCC015B)
                '  Dim p = Path.GetTempFileName

                Dim LRN = ""
                Dim GUID = ""
                If doc.GetElementsByTagName("LRN").Count > 0 Then
                    LRN = doc.GetElementsByTagName("LRN")(0).InnerText
                End If

                readNCTS_VersandanmeldungXML_CC015B_RES = LRN

                If doc.GetElementsByTagName("GUID").Count > 0 Then
                    GUID = doc.GetElementsByTagName("GUID")(0).InnerText
                End If


                Dim AnsprechpTMP_Email = ""
                Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr_or_GUID_or_MRN(LRN, GUID, "", True,, Partnersystem)
                If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                NCTS.ncts_ObjectName = LRN
                NCTS.ncts_GUID = GUID

                NCTS.ncts_Partnersystem = ncts_Partnersystem









                If If(NCTS.ncts_Status, 0) <= 5 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_05
                ' If If(NCTS.ncts_Status_KEWILL_Equivalent, 0) <= 5 Then NCTS.ncts_Status_KEWILL_Equivalent = DAKOSY_Worker.cDY_Statusliste.S_05
                NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                If NCTS.SAVE_ALL() Then
                    '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                    Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                    NR.nctsNr_nctsId = NCTS.ncts_Id
                    NR.nctsNr_typ = "CC15B_RES"
                    NR.nctsNr_path = ""
                    NR.nctsNr_LRN = LRN
                    NR.nctsNr_MRN = Nothing
                    NR.nctsNr_GUID = GUID
                    If NR.SAVE() Then NS_id = NR.nctsNr_id
                Else
                    readNCTS_VersandanmeldungXML_CC015B_RES = ""
                End If



                'Try
                '    System.IO.File.Delete(p)
                'Catch ex As Exception
                '    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                'End Try

                FORM.addDGVEinarbeitung("NCTS: Versandanmeldung - Ergänzung GUID", readNCTS_VersandanmeldungXML_CC015B_RES)

                '    End Using

            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_VersandanmeldungXML_CC015B_RES = ""
        End Try
    End Function


    Public Shared Function readNCTS_BestaetigungZollXML_CC029B(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_BestaetigungZollXML_CC029B = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC029B>") Then
                Dim xmlCC029B = XmlString.Substring(XmlString.IndexOf("<CC029B>"), XmlString.IndexOf("</CC029B>") + 9 - XmlString.IndexOf("<CC029B>"))
                '  MsgBox(xmlCC029B)
                Dim p = Path.GetTempFileName

                Dim LRN = ""
                If doc.GetElementsByTagName("RefNumHEA4").Count > 0 Then
                    LRN = doc.GetElementsByTagName("RefNumHEA4")(0).InnerText
                End If

                Dim MRN = ""
                If doc.GetElementsByTagName("DocNumHEA5").Count > 0 Then
                    MRN = doc.GetElementsByTagName("DocNumHEA5")(0).InnerText
                End If




                Dim Eroeffnung = Nothing
                If doc.GetElementsByTagName("AccDatHEA158").Count > 0 Then
                    Try
                        Eroeffnung = Date.ParseExact(doc.GetElementsByTagName("AccDatHEA158")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try
                End If



                Dim ncts_Befoerderungsmittel_KennzeichenAbgang = Nothing
                If doc.GetElementsByTagName("IdeOfMeaOfTraAtDHEA78").Count > 0 Then
                    ncts_Befoerderungsmittel_KennzeichenAbgang = doc.GetElementsByTagName("IdeOfMeaOfTraAtDHEA78")(0).InnerText
                End If
                Dim ncts_Befoerderungsmittel_KennzeichenGrenzueberschreitend = Nothing
                If doc.GetElementsByTagName("IdeOfMeaOfTraCroHEA85").Count > 0 Then
                    ncts_Befoerderungsmittel_KennzeichenGrenzueberschreitend = doc.GetElementsByTagName("IdeOfMeaOfTraCroHEA85")(0).InnerText
                End If


                Dim Wiedergestellungsdatum = Nothing
                If doc.GetElementsByTagName("DatLimERS69").Count > 0 Then
                    Try
                        Wiedergestellungsdatum = Date.ParseExact(doc.GetElementsByTagName("DatLimERS69")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try
                End If



                readNCTS_BestaetigungZollXML_CC029B = LRN


                Using outputFile As New StreamWriter(p, False, System.Text.Encoding.Default)
                    outputFile.Write(xmlCC029B)
                    outputFile.Close()


                    Dim AnsprechpTMP_Email = ""
                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr_or_GUID_or_MRN(LRN, "", MRN, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                    NCTS.ncts_ObjectName = LRN

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    NCTS.ncts_MRN = MRN
                    NCTS.ncts_Eroeffnung = If(Eroeffnung Is Nothing, Now.ToShortDateString, Eroeffnung)
                    NCTS.ncts_Wiedergestellungsdatum = Wiedergestellungsdatum
                    NCTS.ncts_Befoerderungsmittel_KennzeichenAbgang = ncts_Befoerderungsmittel_KennzeichenAbgang
                    NCTS.ncts_Befoerderungsmittel_KennzeichenGrenzueberschreitend = ncts_Befoerderungsmittel_KennzeichenGrenzueberschreitend
                    'If doc.GetElementsByTagName("GuaRefNumGRNREF1").Count > 0 AndAlso doc.GetElementsByTagName("AmoConREF7").Count > 0 Then
                    '    Dim BRG As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Sicherheitsangaben
                    '    BRG.nctsSA_GRN = doc.GetElementsByTagName("GuaRefNumGRNREF1")(0).InnerText
                    '    Dim Betrag = doc.GetElementsByTagName("AmoConREF7")(0).InnerText
                    '    If IsNumeric(Betrag) Then
                    '        BRG.nctsSA_AbgabenBetrag = CDbl(Betrag)
                    '    End If

                    '    NCTS.ncts_SICHERHEITSANGABEN = New List(Of VERAG_PROG_ALLGEMEIN.cNCTS_TR_Sicherheitsangaben)
                    '    NCTS.ncts_SICHERHEITSANGABEN.Add(BRG)
                    'End If


                    If If(NCTS.ncts_Status, 0) <= 5 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_50
                    ' If If(NCTS.ncts_Status_KEWILL_Equivalent, 0) <= 5 Then NCTS.ncts_Status_KEWILL_Equivalent = DAKOSY_Worker.cDY_Statusliste.S_05
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "CC029B"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = LRN
                        NR.nctsNr_MRN = MRN
                        NR.nctsNr_GUID = Nothing
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_BestaetigungZollXML_CC029B = ""
                    End If



                    Try
                        System.IO.File.Delete(p)
                    Catch ex As Exception
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    End Try

                    FORM.addDGVEinarbeitung("NCTS: BestätigungZoll", readNCTS_BestaetigungZollXML_CC029B)

                End Using

            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_BestaetigungZollXML_CC029B = ""
        End Try
    End Function




    Public Shared Function readNCTS_ErledigungsmitteilungXML_CC045A(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_ErledigungsmitteilungXML_CC045A = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC045A") Then
                '   Dim xmlCC015B = XmlString.Substring(XmlString.IndexOf("<CC015B>"), XmlString.IndexOf("</CC015B>") + 9 - XmlString.IndexOf("<CC015B>"))
                '  MsgBox(xmlCC015B)
                '  Dim p = Path.GetTempFileName


                Dim MRN = ""
                If doc.GetElementsByTagName("DocNumHEA5").Count > 0 Then
                    MRN = doc.GetElementsByTagName("DocNumHEA5")(0).InnerText
                End If

                Dim TMSTMP As Date
                If doc.GetElementsByTagName("WriOffDatHEA619").Count > 0 Then
                    'If IsDate(doc.GetElementsByTagName("WriOffDatHEA619")(0).InnerText) Then
                    '    TMSTMP = CDate(doc.GetElementsByTagName("WriOffDatHEA619")(0).InnerText)
                    'End If
                    Try
                        TMSTMP = Date.ParseExact(doc.GetElementsByTagName("WriOffDatHEA619")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByMRN(MRN, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                    'NCTS.ncts_ObjectName = LRN 
                    '  NCTS.ncts_GUID = Guid
                    NCTS.ncts_MRN = MRN
                    NCTS.ncts_Erledigung = TMSTMP.ToShortDateString
                    readNCTS_ErledigungsmitteilungXML_CC045A = MRN

                    NCTS.ncts_Partnersystem = ncts_Partnersystem


                    If If(NCTS.ncts_Status, 0) <= 60 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_60
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "CC045A"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = Nothing
                        NR.nctsNr_MRN = MRN
                        NR.nctsNr_GUID = Nothing
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_ErledigungsmitteilungXML_CC045A = ""
                    End If



                    'Try
                    '    System.IO.File.Delete(p)
                    'Catch ex As Exception
                    '    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    'End Try

                    FORM.addDGVEinarbeitung("NCTS: Erledigungsmitteilung", readNCTS_ErledigungsmitteilungXML_CC045A)

                    '    End Using

                End If
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_ErledigungsmitteilungXML_CC045A = ""
        End Try
    End Function

    Public Shared Function readNCTS_BestaetigungZollXML_ARR_INF(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_BestaetigungZollXML_ARR_INF = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<ARR_INF") Or XmlString.Contains("<ARRINF") Then
                '   Dim xmlCC015B = XmlString.Substring(XmlString.IndexOf("<CC015B>"), XmlString.IndexOf("</CC015B>") + 9 - XmlString.IndexOf("<CC015B>"))
                '  MsgBox(xmlCC015B)
                '  Dim p = Path.GetTempFileName


                Dim GUID = ""
                If doc.GetElementsByTagName("GUID").Count > 0 Then
                    GUID = doc.GetElementsByTagName("GUID")(0).InnerText
                End If

                Dim TMSTMP As Date
                If doc.GetElementsByTagName("TMSTMP").Count > 0 Then
                    If IsDate(doc.GetElementsByTagName("TMSTMP")(0).InnerText) Then
                        TMSTMP = CDate(doc.GetElementsByTagName("TMSTMP")(0).InnerText)
                    End If

                    Dim AnsprechpTMP_Email = ""

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByGUID(GUID, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                    'NCTS.ncts_ObjectName = LRN 
                    NCTS.ncts_GUID = GUID

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    NCTS.ncts_Ankunft = TMSTMP.ToShortDateString
                    readNCTS_BestaetigungZollXML_ARR_INF = GUID

                    If If(NCTS.ncts_Status, 0) <= 53 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_53
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "ARR_INF"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = Nothing
                        NR.nctsNr_MRN = Nothing
                        NR.nctsNr_GUID = GUID
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_BestaetigungZollXML_ARR_INF = ""
                    End If



                    'Try
                    '    System.IO.File.Delete(p)
                    'Catch ex As Exception
                    '    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    'End Try

                    FORM.addDGVEinarbeitung("NCTS: Ankunftsanzeige", readNCTS_BestaetigungZollXML_ARR_INF)

                    '    End Using

                End If
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_BestaetigungZollXML_ARR_INF = ""
        End Try
    End Function


    Public Shared Function readNCTS_AbbruchXML_CC009A(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_AbbruchXML_CC009A = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC009A>") Then
                Dim xmlCC009A = XmlString.Substring(XmlString.IndexOf("<CC009A>"), XmlString.IndexOf("</CC009A>") + 9 - XmlString.IndexOf("<CC009A>"))
                '  MsgBox(xmlCC029B)
                Dim p = Path.GetTempFileName

                Dim MRN = ""
                If doc.GetElementsByTagName("DocNumHEA5").Count > 0 Then
                    MRN = doc.GetElementsByTagName("DocNumHEA5")(0).InnerText
                End If


                Dim Abbruchdatum = Nothing
                If doc.GetElementsByTagName("DatOfCanDecHEA146").Count > 0 Then
                    Try
                        Abbruchdatum = Date.ParseExact(doc.GetElementsByTagName("DatOfCanDecHEA146")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try
                End If


                Using outputFile As New StreamWriter(p, False, System.Text.Encoding.Default)
                    outputFile.Write(xmlCC009A)
                    outputFile.Close()

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr_or_GUID_or_MRN("", "", MRN, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    NCTS.ncts_MRN = MRN


                    NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_80
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    readNCTS_AbbruchXML_CC009A = NCTS.ncts_ObjectName

                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "CC009A"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = Nothing
                        NR.nctsNr_MRN = MRN
                        NR.nctsNr_GUID = Nothing
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_AbbruchXML_CC009A = ""
                    End If


                    Try
                        System.IO.File.Delete(p)
                    Catch ex As Exception
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    End Try

                    FORM.addDGVEinarbeitung("NCTS: Abbruch", readNCTS_AbbruchXML_CC009A)

                End Using
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_AbbruchXML_CC009A = ""
        End Try
    End Function



    Public Shared Function readNCTS_RueckweisungXML_CC016A(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_RueckweisungXML_CC016A = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC016A>") Then
                Dim xmlCC016A = XmlString.Substring(XmlString.IndexOf("<CC016A>"), XmlString.IndexOf("</CC016A>") + 9 - XmlString.IndexOf("<CC016A>"))
                '  MsgBox(xmlCC029B)
                Dim p = Path.GetTempFileName

                Dim LRN = ""
                If doc.GetElementsByTagName("RefNumHEA4").Count > 0 Then
                    LRN = doc.GetElementsByTagName("RefNumHEA4")(0).InnerText
                End If

                Dim Abbruchdatum = Nothing
                If doc.GetElementsByTagName("DecRejDatHEA159").Count > 0 Then
                    Try
                        Abbruchdatum = Date.ParseExact(doc.GetElementsByTagName("DecRejDatHEA159")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try
                End If


                Using outputFile As New StreamWriter(p, False, System.Text.Encoding.Default)
                    outputFile.Write(xmlCC016A)
                    outputFile.Close()

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr_or_GUID_or_MRN(LRN, "", "", True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    NCTS.ncts_ObjectName = LRN


                    NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_65
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    readNCTS_RueckweisungXML_CC016A = LRN
                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "CC016A"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = LRN
                        NR.nctsNr_MRN = Nothing
                        NR.nctsNr_GUID = Nothing
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_RueckweisungXML_CC016A = ""
                    End If


                    Try
                        System.IO.File.Delete(p)
                    Catch ex As Exception
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    End Try

                    FORM.addDGVEinarbeitung("NCTS: Rückweisung", readNCTS_RueckweisungXML_CC016A)

                End Using
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_RueckweisungXML_CC016A = ""
        End Try
    End Function


    Public Shared Function readNCTS_KeineFreigabeXML_CC051B(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_KeineFreigabeXML_CC051B = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<CC051B>") Then
                Dim xmlCC051B = XmlString.Substring(XmlString.IndexOf("<CC051B>"), XmlString.IndexOf("</CC051B>") + 9 - XmlString.IndexOf("<CC051B>"))
                '  MsgBox(xmlCC029B)
                Dim p = Path.GetTempFileName

                Dim MRN = ""
                If doc.GetElementsByTagName("DocNumHEA5").Count > 0 Then
                    MRN = doc.GetElementsByTagName("DocNumHEA5")(0).InnerText
                End If

                Dim Abbruchdatum = Nothing
                If doc.GetElementsByTagName("DecRejDatHEA159").Count > 0 Then
                    Try
                        Abbruchdatum = Date.ParseExact(doc.GetElementsByTagName("DecRejDatHEA159")(0).InnerText, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
                    Catch ex As Exception
                    End Try
                End If


                Using outputFile As New StreamWriter(p, False, System.Text.Encoding.Default)
                    outputFile.Write(xmlCC051B)
                    outputFile.Close()

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByBezugsNr_or_GUID_or_MRN("", "", MRN, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    NCTS.ncts_MRN = MRN


                    NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_65
                    NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status

                    readNCTS_KeineFreigabeXML_CC051B = NCTS.ncts_ObjectName

                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "CC051B"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = Nothing
                        NR.nctsNr_MRN = MRN
                        NR.nctsNr_GUID = Nothing
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_KeineFreigabeXML_CC051B = ""
                    End If


                    Try
                        System.IO.File.Delete(p)
                    Catch ex As Exception
                        VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    End Try

                    FORM.addDGVEinarbeitung("NCTS: KeineFreigabe", readNCTS_KeineFreigabeXML_CC051B)

                End Using
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_KeineFreigabeXML_CC051B = ""
        End Try
    End Function

    Public Shared Function readNCTS_BestaetigungZollXML_GUA_INF(ncts_Partnersystem As String, FORM As frmTRNCTS_NachrichtenVerarbeitung, doc As XmlDocument, ByRef NS_id As Integer) As String
        Try
            readNCTS_BestaetigungZollXML_GUA_INF = ""
            Dim sw = New StringWriter()
            Dim xw = New XmlTextWriter(sw)
            doc.WriteTo(xw)
            Dim XmlString = sw.ToString()
            If XmlString.Contains("<GUA_INF") Or XmlString.Contains("<GUAINF") Then
                '   Dim xmlCC015B = XmlString.Substring(XmlString.IndexOf("<CC015B>"), XmlString.IndexOf("</CC015B>") + 9 - XmlString.IndexOf("<CC015B>"))
                '  MsgBox(xmlCC015B)
                '  Dim p = Path.GetTempFileName


                Dim GUID = ""
                If doc.GetElementsByTagName("GUID").Count > 0 Then
                    GUID = doc.GetElementsByTagName("GUID")(0).InnerText
                End If

                Dim TMSTMP As Date
                If doc.GetElementsByTagName("TMSTMP").Count > 0 Then
                    If IsDate(doc.GetElementsByTagName("TMSTMP")(0).InnerText) Then
                        TMSTMP = CDate(doc.GetElementsByTagName("TMSTMP")(0).InnerText)
                    End If

                    Dim AnsprechpTMP_Email = ""

                    Dim NCTS As VERAG_PROG_ALLGEMEIN.cNCTS_TR = VERAG_PROG_ALLGEMEIN.cNCTS_TR.LOADByGUID(GUID, True,, Partnersystem)
                    If NCTS Is Nothing Then NCTS = New VERAG_PROG_ALLGEMEIN.cNCTS_TR
                    'NCTS.ncts_ObjectName = LRN 
                    NCTS.ncts_GUID = GUID

                    NCTS.ncts_Partnersystem = ncts_Partnersystem
                    readNCTS_BestaetigungZollXML_GUA_INF = GUID

                    'If If(NCTS.ncts_Status, 0) <= 53 Then NCTS.ncts_Status = DAKOSY_Worker.cDY_Statusliste.S_53
                    'NCTS.ncts_Status_KEWILL_Equivalent = NCTS.ncts_Status


                    Dim GRN = ""
                    '  Dim CurREF8 = ""
                    Dim AmoConREF7 = ""

                    If doc.GetElementsByTagName("GRN").Count > 0 Then
                        GRN = doc.GetElementsByTagName("GRN")(0).InnerText
                    End If
                    If doc.GetElementsByTagName("AmoConREF7").Count > 0 Then
                        AmoConREF7 = doc.GetElementsByTagName("AmoConREF7")(0).InnerText
                    End If

                    For Each GUA In NCTS.ncts_SICHERHEITSANGABEN
                        If GUA.nctsSA_GRN = GRN Then
                            GUA.nctsSA_AbgabenBetrag = AmoConREF7
                        End If
                    Next




                    If NCTS.SAVE_ALL() Then
                        '    If If(NCTS.ncts_Status, 0) <= 5 Then DAKOSY_Worker.cDakosy_Statusmeldungen.InsertStatusMeldung(DY_TMP, DAKOSY_Worker.cDY_Statusliste.S_05,,,, statusTMP)
                        Dim NR As New VERAG_PROG_ALLGEMEIN.cNCTS_TR_Nachrichten()
                        NR.nctsNr_nctsId = NCTS.ncts_Id
                        NR.nctsNr_typ = "GUA_INF"
                        NR.nctsNr_path = ""
                        NR.nctsNr_LRN = Nothing
                        NR.nctsNr_MRN = Nothing
                        NR.nctsNr_GUID = GUID
                        If NR.SAVE() Then NS_id = NR.nctsNr_id
                    Else
                        readNCTS_BestaetigungZollXML_GUA_INF = ""
                    End If



                    'Try
                    '    System.IO.File.Delete(p)
                    'Catch ex As Exception
                    '    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                    'End Try

                    FORM.addDGVEinarbeitung("NCTS: Garantie_Info", readNCTS_BestaetigungZollXML_GUA_INF)

                    '    End Using

                End If
            End If

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            readNCTS_BestaetigungZollXML_GUA_INF = ""
        End Try
    End Function




    Shared Function getDatumXMLNothing(dateTmp) As Object
        If dateTmp IsNot Nothing AndAlso dateTmp.ToString.Trim <> "" AndAlso IsDate(dateTmp) Then
            Dim datum As Date = CDate(dateTmp).ToShortDateString()
            If datum > CDate("01.01.2000") Then
                Return datum
            End If
        End If
        Return Nothing
    End Function


    Shared Function cutLength(s, length)
        If s.length > 35 Then s = s.Substring(0, length)
        Return s
    End Function



    'Shared Function saveDY_NCTS(STATUS As DAKOSY_Worker.cDakosy_Statusmeldungen, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, transID As String, transDate As Date, Optional ANR As String = "", Optional MRN As String = "") As DAKOSY_Worker.cDakosyNCTSOut
    '    'EINTRAG IN tblDakosyNCTSOut
    '    Dim NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut = Nothing
    '    NCTS_TMP = DAKOSY_Worker.cDakosyNCTSOut.LOADByBezugsNr(STATUS.dySt_BezugsNr.ToString, False)
    '    If NCTS_TMP Is Nothing Then
    '        NCTS_TMP = New DAKOSY_Worker.cDakosyNCTSOut
    '        NCTS_TMP.ncts_ObjectName = STATUS.dySt_BezugsNr
    '        If NCTS_TMP IsNot Nothing Then NCTS_TMP.ncts_dyaAnmID = DY_TMP.dy_Id

    '        If DAKOSY_Worker.cDakosy_Statusmeldungen.isNewestTransaktion(STATUS.dySt_BezugsNr, transID, transDate) Then
    '            NCTS_TMP.ncts_Status = DY_TMP.dy_Status 'weil der Status bereit überprüft wurde --> neuste TransaktionsID
    '            NCTS_TMP.ncts_Status_KEWILL_Equivalent = DY_TMP.dy_Status 'weil der Status bereit überprüft wurde --> neuste TransaktionsID
    '        End If
    '        'NCTS_TMP.ncts_Status_KEWILL_Equivalent = STATUS.dySt_status
    '        If ANR <> "" Or MRN <> "" Then
    '            If ANR <> "" Then NCTS_TMP.ncts_ANR = ANR
    '            If MRN <> "" Then NCTS_TMP.ncts_MRN = MRN
    '        End If
    '        NCTS_TMP.SAVE()
    '        'Eintragen
    '    Else
    '        'Update     
    '        NCTS_TMP.UPDATE_Status(DY_TMP.dy_Status) 'weil der Status bereit überprüft wurde --> neuste TransaktionsID
    '        If ANR <> "" Or MRN <> "" Then
    '            If ANR <> "" Then NCTS_TMP.ncts_ANR = ANR : NCTS_TMP.SAVE()
    '            If MRN <> "" Then NCTS_TMP.ncts_MRN = MRN : NCTS_TMP.SAVE()
    '        End If
    '    End If
    '    saveDY_NCTS = NCTS_TMP
    'End Function

    'Shared Sub SaveNCTS_FirmaNL(ByRef NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, DY_TMP As DAKOSY_Worker.cDakosy_Zollanmeldungen, division As String)

    '    If division <> "" Then
    '        If (NCTS_TMP.ncts_firma Is Nothing OrElse NCTS_TMP.ncts_firma = "") Or (NCTS_TMP.ncts_niederlassung Is Nothing OrElse NCTS_TMP.ncts_niederlassung = "") Then
    '            Select Case division
    '                Case "SUW"
    '                    NCTS_TMP.ncts_firma = "ATILLA"
    '                    NCTS_TMP.ncts_niederlassung = "SUB"
    '                Case "SUB"
    '                    NCTS_TMP.ncts_firma = "VERAG"
    '                    NCTS_TMP.ncts_niederlassung = "SUB"
    '                Case "DUR"
    '                    NCTS_TMP.ncts_firma = "ATILLA"
    '                    NCTS_TMP.ncts_niederlassung = "SBG"
    '                Case "NKD"
    '                    NCTS_TMP.ncts_firma = "VERAG"
    '                    NCTS_TMP.ncts_niederlassung = "NKD"
    '                Case "WAI"
    '                    NCTS_TMP.ncts_firma = "VERAG"
    '                    NCTS_TMP.ncts_niederlassung = "WAI"
    '                Case "SBG"
    '                    NCTS_TMP.ncts_firma = "VERAG"
    '                    NCTS_TMP.ncts_niederlassung = "SBG"
    '                Case "IME"
    '                    NCTS_TMP.ncts_firma = "IMEX"
    '                    NCTS_TMP.ncts_niederlassung = "SUB"
    '                Case "GLO"
    '                    NCTS_TMP.ncts_firma = "IMEX"
    '                    NCTS_TMP.ncts_niederlassung = "GLO"
    '                Case "AMB"
    '                    NCTS_TMP.ncts_firma = "AMBAR"
    '                    NCTS_TMP.ncts_niederlassung = "AMBAR"
    '                Case "NEU"
    '                    NCTS_TMP.ncts_firma = "VERAG"
    '                    NCTS_TMP.ncts_niederlassung = "NEU"
    '                Case "ATSP", "ATSPED"
    '                    NCTS_TMP.ncts_firma = "UNISPED"
    '                    NCTS_TMP.ncts_niederlassung = "ATSP"
    '            End Select
    '            NCTS_TMP.SAVE()
    '        End If

    '    End If
    '    If DY_TMP.dy_AvisoId Is Nothing Then
    '        DY_TMP.tryFindAviso()
    '        DY_TMP.SAVE()
    '    End If
    'End Sub


    Shared Sub SaveNCTS_WGDAT(ByRef NCTS_TMP As DAKOSY_Worker.cDakosyNCTSOut, WGDAT As Object)

        If WGDAT IsNot Nothing AndAlso IsDate(WGDAT) Then
            NCTS_TMP.ncts_Wiedergestellungsdatum = CDate(WGDAT)
            NCTS_TMP.SAVE()
        End If
    End Sub



End Class
