
Imports Microsoft.Office.Interop
Public Class frmKundenUIDEORI_List
    Dim ART = "" 'UID/EORI
    Dim kdNr As Integer = 0
    Dim kundenSQL As New kundenSQL
    Private Sub frmKundenUIDEORI_List_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' genReport()

        Select Case ART
            Case "UID"
            Case "EORI"
        End Select
    End Sub


    Public Function genReport(dt As DataTable) As Microsoft.Office.Interop.Word.Document
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            Dim pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\Zoll\EORI_UID.dotx"
            '  Select Case language
            'Case "DE" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_DE.dotx"
            'Case "YU" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_YU.dotx"
            'Case "RO" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_RO.dotx"
            'Case "TR" : pfadWord = AppDomain.CurrentDomain.BaseDirectory & "Resources\MDM_Schreiben\Abholbereit\Abholbereit_TR.dotx"
            'Case Else : MsgBox("Keine Sprache ausgewählt!") : Return odoc
            ' End Select

            Dim cGenWord As New cGenWord
            Dim oWord As New Microsoft.Office.Interop.Word.Application
            odoc = cGenWord.openWord(pfadWord, True, oWord)


            '    Dim dt As DataTable = SQL.loadDgvBySql("SELECT * FROM SDL WHERE KundenNr=" & kdNr & " AND SDLNr=210 AND Retourniert=1 AND RetourniertAm between '" & datum.ToShortDateString & " 00:00:00' and '" & datum.ToShortDateString & " 23:59:59'")
            Dim KUNDE = kundenSQL.getKundeFMZOLLByKdNr(kdNr)
            If KUNDE Is Nothing Then MsgBox("Kunde nicht gefunden!") : Return odoc
            '  Dim PartnerKdNrFromFMZOLL As cPartnerKdNRFMZoll = kundenSQL.getPartnerKdNrFromFMZOLL(kdNr)

            Dim oTable As Word.Table

            odoc.FormFields("Adresse1").Range.Text = KUNDE.Name_1 & " " & If(KUNDE.Name_2, "")
            odoc.FormFields("Adresse2").Range.Text = KUNDE.Straße
            odoc.FormFields("Adresse3").Range.Text = KUNDE.LandKz & " " & KUNDE.PLZ & " " & KUNDE.Ort
            If KUNDE.Telefax <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "FAX: " & KUNDE.Telefax
            ElseIf KUNDE.E_Mail <> "" Then
                odoc.FormFields("Adresse5").Range.Text = "E-Mail: " & KUNDE.E_Mail
            End If


            '  odoc.FormFields("KdNrVERAG").Range.Text = KUNDE.KundenNr
            ' odoc.FormFields("KdNrMST").Range.Text = PartnerKdNrFromFMZOLL.MSEKundenNr
            odoc.FormFields("Datum").Range.Text = Now.ToShortDateString
            odoc.FormFields("KundenNr").Range.Text = KUNDE.AdressenNr
            odoc.FormFields("AuftragsNr").Range.Text = ART

            Dim pinVisible = False
            If odoc.Range.Bookmarks.Exists("TabelleKarten") Then
                If odoc.Bookmarks("TabelleKarten").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten").Range.Tables(1)
                    Dim i = 1
                    Dim kfzTmp = ""
                    '   MsgBox(dt.Rows.Count)
                    For Each r As DataRow In dt.Rows
                        If r("KfzKennzeichen").ToString <> "" Or r("KartenNr").ToString <> "" Then
                            If cSqlDb.checkNullStr(r("KfzKennzeichen")) <> kfzTmp Then
                                kfzTmp = cSqlDb.checkNullStr(r("KfzKennzeichen"))
                                oTable.Rows(i + 1).Cells(1).Range.Text = kfzTmp
                            Else
                                oTable.Rows(i + 1).Cells(1).Range.Text = ""
                            End If
                            oTable.Rows(i + 1).Cells(2).Range.Text = cSqlDb.checkNullStr(r("KarteBoxBezeichnung"))
                            oTable.Rows(i + 1).Cells(3).Range.Text = cSqlDb.checkNullStr(r("KartenNr"))
                            oTable.Rows(i + 1).Cells(4).Range.Text = cSqlDb.checkNullStr(r("PIN"))
                            If i < dt.Rows.Count Then oTable.Rows.Add()
                            '    MsgBox(cSqlDb.checkNullStr(r("KarteBoxBezeichnung")))
                            i += 1


                        End If
                    Next
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If


            odoc.Save() : odoc.Activate()

            'If autodruck Then
            '    Dim t As New Threading.Thread(Sub()
            '                                      Print(odoc, PrinterName)
            '                                  End Sub)
            '    t.IsBackground = True
            '    t.Start()
            'End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return odoc
    End Function

End Class