Imports System.Windows.Forms
Imports System.Text
Imports DevExpress.XtraReports.UI

Public Class frmPrintOptions
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmPrintNVE_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F1 Then
            Prepare_Print()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Prepare_Print()
    End Sub
    Sub Prepare_Print()
        Try
            Dim SQL = "SELECT * FROM TBKOMMCOLLI_SENDUNGEN where GUID = " & CURRENT_SENDUNG
            Dim DTSENDUNG As DataTable = ClassDatabase.Return_Datatable(SQL)
            If DTSENDUNG.Rows.Count = 1 Then
                SQL = "SELECT * FROM TBKOMMCOLLI_SDG_LHM where SENDUNGNR = " & CURRENT_SENDUNG
                Dim DTKOMMCOLLI_SDG_LHM As DataTable = ClassDatabase.Return_Datatable(SQL)
                If DTKOMMCOLLI_SDG_LHM.Rows.Count > 0 Then
                    SQL = "SELECT COUNT(*) FROM TBKOMMCOLLI_SDG_LHM where SENDUNGNR = " & CURRENT_SENDUNG
                    Dim PS = ClassDatabase.Execute_Scalar(SQL, True)

                    For Each row As DataRow In DTKOMMCOLLI_SDG_LHM.Rows
                        Dim NAME1 = DTSENDUNG.Rows(0).Item("NAME1").ToString.Trim
                        Dim STRASSE = DTSENDUNG.Rows(0).Item("STRASSE").ToString.Trim
                        Dim PLZ = DTSENDUNG.Rows(0).Item("PLZ").ToString.Trim
                        Dim LKZ = DTSENDUNG.Rows(0).Item("LKZ").ToString.Trim
                        Dim ORT = DTSENDUNG.Rows(0).Item("ORT").ToString.Trim
                        Dim NVE = row.Item("NVE")
                        'Dim PS = row.Item("PACKSTUECKANZAHL")
                        If IsDBNull(NVE) Then
                            MsgBox("Es wurden noch keine NVE's erzeugt. Schliessen Sie die Sendung ab!")
                            Dim result As MsgBoxResult = MsgBox("Wollen Sie ein Beispieletikett drucken?", MsgBoxStyle.YesNo, "Bitte bestätigen")
                            ' wenn das Profil gelöscht werden soll
                            If result = MsgBoxResult.Yes Then
                                PrintViaIP(CURR_IP_PRINTER, NAME1, STRASSE, PLZ, LKZ, ORT, "123456789101112", 1)
                            End If
                        Else
                            PrintViaIP(CURR_IP_PRINTER, NAME1, STRASSE, PLZ, LKZ, ORT, NVE, Me.txtAnzahl.Text, PS)
                        End If

                    Next
                End If
            Else
                MsgBox("Achtung: mehr als eine Sendung für ID: " & CURRENT_SENDUNG, MsgBoxStyle.Critical)
            End If
        Catch ex As Exception
            MsgBox("Unbehandelte Ausnahme in Prepare Print: " & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Sub PrintViaIP(IP As String, empname As String, empStrasse As String, empPLZ As String, empLKZ As String, empOrt As String, _NVE As String,
                   Optional Anz As Integer = 1, Optional Packstueck As Integer = 1)
        Try
            Dim ipAddress As String = IP
            Dim port As Integer = 9100

            Dim pText As New StringBuilder
            pText.AppendLine("^XA") 'Every Format must start with this command
            pText.AppendLine("^CI28")
            'Barcode
            pText.AppendLine("^BY4^FO650,50^BCR,160,N,N,N,A^FD00" & _NVE & "^FS")
            pText.AppendLine("^CFA,30^FWr^FO620,60^FDNVE 00" & _NVE & "^FS")

            pText.AppendLine("^AU^FO750,700^FDS.T.a.R.^FS")
            pText.AppendLine("^CF0,22^FWr^FO720,700^FDSYSTEM-TRANSPORT auf RÄDERN^FS")
            pText.AppendLine("^AQ^FO680,700^FD35 - Giessen^FS")
            pText.AppendLine("^AQ^FO650,700^FDHellmold & Plank^FS")
            pText.AppendLine("^AQ^FO560,50^FDS.T.a.R. GmbH, Werner von Siemens-Str. 1, 34576 Homberg-Efze Telefon: 05681/988823^FS")
            pText.AppendLine("^AQ^FO480,50^FDHinweise:^FS")
            pText.AppendLine("^AQ^FO430,50^CI27^FDFixtermin! Nicht früher oder später - ^FS")

            pText.AppendLine("^FO540,50^GB1,1050,3^FS") 'Breite Linie
            pText.AppendLine("^FO50,600^GB490,1,3^FS") 'Senkrechte Linie
            pText.AppendLine("^AR^FO510,610^FDVersender:^FS")
            pText.AppendLine("^AR^FO510,780^FDReinhard Schaum^FS")
            pText.AppendLine("^AR^FO460,780^FDRheinstrasse 8^FS")
            pText.AppendLine("^AR^FO420,780^CI27^^FDDE - 35625 Hüttenberg^FS")

            pText.AppendLine("^FO405,600^GB1,500,3^FS")
            pText.AppendLine("^AT^^FO360,610^CI27^^FDAnzahl Packstücke:^FS")
            pText.AppendLine("^AT^^FO360,950^FD" & Packstueck.ToString & "^FS") 'Anzahl Packstücke Linie
            pText.AppendLine("^FO330,600^GB1,500,3^FS")
            pText.AppendLine("^AT^FO290,610^CI27^^FDEmpfänger:^FS")
            pText.AppendLine("^AU^FO230,610^CI27^^FD" & empname & "^FS")
            pText.AppendLine("^AU^FO150,610^CI27^^FD" & empStrasse & "^FS")
            pText.AppendLine("^AU^FO80,610^CI27^^FDDE-" & empPLZ & " " & empOrt & "^FS")
            pText.AppendLine("^XZ") 'Every Format must end with this command
            Dim tempZPL = pText.ToString
            Try
                'Open Connection
                Dim client As New System.Net.Sockets.TcpClient
                client.Connect(ipAddress, port)
                For i = 1 To Anz
                    'Write ZPL String to Connection
                    Dim writer As New System.IO.StreamWriter(client.GetStream())
                    writer.Write(pText)
                    writer.Flush()

                    'Close Connection
                    writer.Close()
                    client.Close()
                Next


            Catch ex As Exception
                ' MsgBox("1: " & ex.Message, MsgBoxStyle.Critical)
                'Catch Exception Here

            End Try
            RawPrinter.SendToPrinter("NVE-Etikett", pText.ToString, IP)

        Catch ex As Exception
            MsgBox("2:" & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub frmPrintOptions_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "MyDataset.VWHUP_REPORT1". Sie können sie bei Bedarf verschieben oder entfernen.
        Try
            DocumentViewerEx1.Source = Nothing
        Catch ex As Exception

        End Try


    End Sub

    Private Sub btnUebernahme_Click(sender As Object, e As EventArgs) Handles btnUebernahme.Click
        Dim report As New reportUebergabe()
        report.CreateDocument(False)
        DocumentViewerEx1.Source = report
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If txtNVE.Text <> "" Then
            PrintViaIP(CURR_IP_PRINTER, "Hans Mustermann", "Musermannstrasse 1", "12345", "DE", "Musterstadt", txtNVE.Text, 1)
        End If

    End Sub

    Private Sub txtAnzahl_TextChanged(sender As Object, e As EventArgs) Handles txtAnzahl.TextChanged

    End Sub
End Class
