Public Class cMIC


    Shared Sub send_MIC_AvisoSendung(AVISO As cAviso, SENDUNG As cSendungen, Optional sendWOQ As Boolean = False, Optional showBezugsNr As Boolean = False)


        If sendWOQ OrElse vbYes = MsgBox("Sollen die Daten abgeschickt werden?" & vbNewLine & "Eine Änderung ist nur noch in der MIC Software möglich.", vbYesNoCancel) Then
            '  Dim cDyAnm As New cDakosy_Zollanmeldungen(cDy_ART, BezugsNr, AVISO.AvisoID, SENDUNG.tblSnd_SendungID)

            If AVISO IsNot Nothing And SENDUNG IsNot Nothing Then

                If SENDUNG.FilialenNr < 1000 Then MsgBox("Keine Filialen-Nummer!")
                If SENDUNG.AbfertigungsNr < 1000 Then MsgBox("Keine Abfertigungs-Nummer!")
                Dim BezugsNr = SENDUNG.FilialenNr & "/" & SENDUNG.AbfertigungsNr
                If showBezugsNr Then
                    BezugsNr = InputBox("MIC Bezugsnummer:", "MIC", BezugsNr)
                End If

                Dim saveFile = ""
                If generateIMPORT(AVISO, SENDUNG, saveFile, BezugsNr) Then
                    If saveFile <> "" Then
                        Process.Start(saveFile)
                        'If Not send_Data_To_Dakosy_FTP(saveFile, BezugsNr, Nothing, Nothing) Then
                        '    MsgBox("Sendefehler!")
                        'End If
                    End If
                End If
            End If
        End If
    End Sub



    Shared Function getInvPartner(KdNr As Integer, NameText As String, Type As String) As MIC_IMPORT_Invoice.TPartner

        If KdNr > 0 Then
            Dim PARTNER As New MIC_IMPORT_Invoice.TPartner
            Dim AD As New cAdressen(KdNr)
            Dim KD As New cKunde(KdNr)
            PARTNER.partnerType = Type
            PARTNER.PartnerAddress.addrName.Add(AD.Name_1)
            If If(AD.Name_2, "") <> "" Then PARTNER.PartnerAddress.addrName.Add(AD.Name_2)
            PARTNER.PartnerAddress.addrStreet = AD.Straße
            PARTNER.PartnerAddress.addrZip = AD.PLZ
            PARTNER.PartnerAddress.addrLoc = AD.Ort
            PARTNER.PartnerAddress.addrCtry = cProgramFunctions.getISO2Land(AD.LandKz)
            PARTNER.PartnerAddress.addrEori = KD.EORITIN
            PARTNER.PartnerAddress.addrTin = KD.EORITIN
            Return PARTNER
        ElseIf If(NameText, "") <> "" Then
            Dim PARTNER As New MIC_IMPORT_Invoice.TPartner
            PARTNER.partnerType = Type
            PARTNER.PartnerAddress.addrName.Add(NameText)
            Return PARTNER
        End If
        Return Nothing
    End Function


    Shared Function generateNCTS(cDY As cNCTS_TR, ByRef saveFile As String, BezugsNr As String) As Boolean 'DY_ACTION As String) As Boolean
        Try

            'Dim Interface_VERSION = "003"
            'Dim ObjectAktion = DY_ACTION

            Dim XML As New MIC_EXPORT_Shipment.ExportShipment
            'Transaktion:
            XML.ShipmentHead.company = "VERAG"
            XML.ShipmentHead.plant = "GB/01"
            XML.ShipmentHead.shipmentId = BezugsNr


            saveFile = getDefaultFolder_SEND("NCTS") & "MIC_NCTS_" & Now.ToString("yyMMdd_HHmmss") & ".xml"


            '' Dim saveDir = Application.StartupPath & "\Dakosy_SEND\"
            'Dim saveDir = "\\share01\Datenarchiv\MIC\SEND\"
            'If Not System.IO.Directory.Exists(saveDir) Then
            '    System.IO.Directory.CreateDirectory(saveDir)
            'End If
            'saveFile = saveDir & "MIC_" & Now.ToString("yyMMdd_HHmmss") & ".xml"

            'MsgBox(saveFile)
            XML.SaveToFile(saveFile)

            replaceNullDateXML(saveFile)


            Return True
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
            Return False
        End Try


    End Function

    Shared Function generateIMPORT(AVISO As cAviso, SENDUNG As cSendungen, ByRef saveFile As String, ByRef BezugsNr As String) As Boolean
        Dim Interface_VERSION = "000"

        Dim XML As New MIC_IMPORT.ImportShipment

        'XML.schemaVersion =

        XML.Shipment.company = "VERAG"
        XML.Shipment.plant = "GB/01"
        XML.Shipment.shipmentId = BezugsNr

        XML.Shipment.ShipmentHead.shipmentType = MIC_IMPORT.TShipmentHeadShipmentType.E 'E:Import
        XML.Shipment.ShipmentHead.customsDate = Now
        If SENDUNG.tblSnd_Gewicht IsNot Nothing AndAlso IsNumeric(SENDUNG.tblSnd_Gewicht) Then XML.Shipment.ShipmentHead.grossWeight = CDec(SENDUNG.tblSnd_Gewicht)
        'XML.Shipment.ShipmentHead.departurePlace = Now
        XML.Shipment.ShipmentHead.MotBorderCrossing.motVehicle = AVISO.LKW_Nr
        XML.Shipment.ShipmentHead.MotBorderCrossing.motCountry = AVISO.LKW_Nationalitaet
        XML.Shipment.ShipmentHead.MotBorder.motVehicle = AVISO.LKW_Nr
        XML.Shipment.ShipmentHead.MotBorder.motCountry = AVISO.LKW_Nationalitaet
        XML.Shipment.ShipmentHead.MotInland.motVehicle = AVISO.LKW_Nr
        XML.Shipment.ShipmentHead.MotInland.motCountry = AVISO.LKW_Nationalitaet

        Dim INV As New MIC_IMPORT_Invoice.TInvoice
        Dim ILN As New MIC_IMPORT_Invoice.TInvoiceLine

        Dim Partner_IM = getInvPartner(SENDUNG.tblSnd_EmpfaengerKdNr, SENDUNG.tblSnd_Empfaenger, "IM")
        If Partner_IM IsNot Nothing Then ILN.Partners.Add(Partner_IM)

        Dim Partner_CZ = getInvPartner(SENDUNG.tblSnd_AbsenderKdNr, SENDUNG.tblSnd_Absender, "CZ")
        If Partner_CZ IsNot Nothing Then ILN.Partners.Add(Partner_CZ)

        INV.InvoiceLines.Add(ILN)

        XML.Invoice.Add(INV)


        Dim CF_BezugsNr = New MIC_IMPORT_Invoice.TCustomerText()
        CF_BezugsNr.key = 1
        CF_BezugsNr.value = BezugsNr
        XML.Shipment.ShipmentHead.CustomerFields.Texts.Add(CF_BezugsNr)

        XML.Shipment.ShipmentHead.countryDestination = "UK"
        XML.Shipment.ShipmentHead.creationDate = Now
        XML.Shipment.ShipmentHead.creationUser = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        If SENDUNG.tblSnd_Frachtkosten IsNot Nothing Then XML.Shipment.ShipmentHead.Freight.frtAmount = SENDUNG.tblSnd_Frachtkosten


        saveFile = getDefaultFolder_SEND("IMPORT") & "MIC_IMPORT_" & Now.ToString("yyMMdd_HHmmss") & ".xml"

        'MsgBox(saveFile)
        XML.SaveToFile(saveFile)

        replaceNullDateXML(saveFile)
        Return True
    End Function
    Shared Sub replaceNullDateXML(XMLFile As String)
        Try
            If Not System.IO.File.Exists(XMLFile) Then Exit Sub
            Dim lines() As String = System.IO.File.ReadAllLines(XMLFile)
            Using writer = New System.IO.StreamWriter(XMLFile, False)
                For Each line In lines
                    If Not line.Contains("0001-01-01") Then writer.WriteLine(line)
                Next
            End Using

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub

    Shared Function getDefaultFolder_SEND(pathType As String) As String
        ' pathType: IMPORT
        ' pathType: EXPORT
        ' pathType: TRANSIT
        Try
            getDefaultFolder_SEND = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir(False) & "MIC\" & If(VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM, "TESTSYSTEM", "ECHTSYSTEM") & "\Ausgang_Zolldaten\" & pathType & "\" & Now.Year & "\" & Now.ToString("yyyyMMdd") & "\" ' Path.GetTempPath
            If Not IO.Directory.Exists(getDefaultFolder_SEND) Then
                IO.Directory.CreateDirectory(getDefaultFolder_SEND)
            End If
        Catch ex As Exception
            Try
                getDefaultFolder_SEND = "C:\TEMP\" ' Path.GetTempPath
                If Not IO.Directory.Exists(getDefaultFolder_SEND) Then
                    IO.Directory.CreateDirectory(getDefaultFolder_SEND)
                End If
            Catch ex2 As Exception
                MsgBox(ex2.Message & ex2.StackTrace)
                getDefaultFolder_SEND = ""
            End Try
        End Try

    End Function



    Shared Function send_Data_To_Dakosy_FTP(file, dySt_BezugsNr, dy_Id, FRIMA_DY)
        'Try
        '    If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ZOLL_DakosyZA_Senden", "AVISO") Then
        '        MsgBox("Sie haben keine Sendeberechtigung.")
        '        Return False
        '    End If

        '    If Not IO.File.Exists(file) Then Return False
        '    Dim F As New System.IO.FileInfo(file)

        '    Dim DY_server = ""
        '    Dim DY_name = ""
        '    Dim DY_pwd = ""
        '    Dim DY_port = ""
        '    Dim DY_PFAD_IN = ""
        '    Dim DY_PFAD_IN_KD = ""

        '    getSFTPLoginData(DY_server, DY_name, DY_pwd, DY_port, DY_PFAD_IN, "", DY_PFAD_IN_KD, FRIMA_DY)

        '    Dim ConnectionInfo As New ConnectionInfo(DY_server, DY_port, DY_name, New PasswordAuthenticationMethod(DY_name, DY_pwd))
        '    Using client = New SftpClient(ConnectionInfo)
        '        client.Connect()
        '        'client.Create(file, file)
        '        client.ChangeDirectory(DY_PFAD_IN)
        '        If IO.File.Exists(file) Then
        '            'MsgBox(file)
        '            Using uplfileStream = System.IO.File.OpenRead(file)
        '                client.UploadFile(uplfileStream, "." & F.Name, True)
        '                client.RenameFile("." & F.Name, F.Name)
        '            End Using
        '            client.Disconnect()
        '        Else
        '            MsgBox("Sendedatei nicht gefunden!")
        '        End If


        '    End Using

        '    'Dim STATUS As New DAKOSY_Worker.cDakosy_Statusmeldungen
        '    'STATUS.dySt_BezugsNr = dySt_BezugsNr
        '    'STATUS.dySt_artMitteilung = "VERAG_SND"
        '    'STATUS.dySt_dyArt = "" '"NCTS" 'T1/T2

        '    'STATUS.dySt_status = DAKOSY_Worker.cDY_Statusliste.S_03
        '    ''STATUS.dySt_ergCode = "ANR"
        '    'STATUS.dySt_ergCode = "SND"
        '    'STATUS.dySt_ergMitteilung = "An Dakosy gesendet"
        '    'STATUS.dySt_ergZeitpunkt = Now

        '    'STATUS.dySt_dyId = dy_Id
        '    'STATUS.SAVE()

        '    Return True
        'Catch ex As Exception
        '    MsgBox(ex.Message & ex.StackTrace)
        '    Return False
        'End Try
    End Function



End Class
