Imports Microsoft.Office.Interop
Imports System.Drawing.Printing

Public Class frmSDLAbholungEintrag

    Public SDL_ABHOLUNG As cSDLAbholung = Nothing

    Dim TMP_KdNr As Integer
    Dim TMP_SDLNr As Integer
    Dim saveInProgress = False
    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Sub New(id As Integer)
        InitializeComponent()
        If id > 0 Then SDL_ABHOLUNG = New cSDLAbholung(id) : setValue()
    End Sub


    Sub New(KdNr As Integer, SDLNr As Integer)
        InitializeComponent()
        TMP_KdNr = KdNr
        TMP_SDLNr = SDLNr
    End Sub

    Sub New(SDL_ABHOLUNG As cSDLAbholung)
        InitializeComponent()
        Me.SDL_ABHOLUNG = SDL_ABHOLUNG
        setValue()
    End Sub


    Private Sub frmSDLAbholungEintrag_Load(sender As Object, e As EventArgs) Handles Me.Load
        KdFirma.initKdBox(Me) 
        cboSDLNr.fillWithSQL("SELECT [SDLNr],cast([SDLNr] as varchar(3)) + '  ' + [SDLBez] FROM [Speditionsdienstleistungen] WHERE SDLTypNr<=2 ORDER BY SDLBez", False, "SDL", True)

        initDrucker()

        lblSBErfassung.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        lblErfassung.Text = Now.ToShortDateString
        lblSBAholung.Text = ""
        lblAholung.Text = ""


        If TMP_KdNr > 0 Then KdFirma.KdNr = TMP_KdNr
        If TMP_SDLNr > 0 Then cboSDLNr.changeItem(TMP_SDLNr) : btnSDLLaden.PerformClick()

        cboSprache.Items.Clear()
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DEUTSCH", "DE"))
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SERBISCH", "YU"))
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("TÜRKISCH", "TR"))
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("RUMÄNISCH", "RO"))

        If KdFirma.KdNr <> "" Then
            Select Case KdFirma.KdNr.ToString.Substring(0, 1)
                Case "1" : cboSprache.changeItem("TR")
                Case "2" : cboSprache.changeItem("RO")
                Case "3" : cboSprache.changeItem("YU")
                Case "4" : cboSprache.changeItem("YU")
                Case Else : cboSprache.changeItem("DE")
            End Select
        End If

        If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "MDM" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "EDV" Then
            lblErr_SaveMDM.Visible = True
        End If

        If VERAG_PROG_ALLGEMEIN.cAllgemein.ABTEILUNG <> "MDM" Then
            dgvPositionen.Columns("PIN").Visible = False
        End If
    End Sub



    Sub initDrucker()
        ' alle installierten Drucker ermitteln und 
        ' in in ein ArrayList-Objekt speichern
        Dim sPrinters As New ArrayList
        For Each sPrinter As String In PrinterSettings.InstalledPrinters
            sPrinters.Add(sPrinter)
        Next

        ' Array sortieren
        sPrinters.Sort()

        ' jetzt alle Drucker in sortierter Reihenfolge in einer
        ' ComboBox zur Auswahl anbieten
        Dim oPS As New System.Drawing.Printing.PrinterSettings
        For i As Integer = 0 To sPrinters.Count - 1
            cboPrinter.Items.Add(sPrinters(i))
        Next

        If sPrinters.Count > 0 Then
            cboPrinter.SelectedIndex = 0
            For Each i In cboPrinter.Items
                If i.ToString = oPS.PrinterName Then
                    cboPrinter.SelectedItem = i
                    Exit For
                End If
            Next
        End If
    End Sub

    Sub setValue()
        If SDL_ABHOLUNG Is Nothing Then Exit Sub
        Me.txtLKW.Text = If(SDL_ABHOLUNG.sdlAb_AbholungLKWKz, "")
        Me.txtFahrer.Text = If(SDL_ABHOLUNG.sdlAb_AbholungFahrername, "")
        If If(SDL_ABHOLUNG.sdlAb_AbholungKdNr, 0) > 0 Then
            Me.KdFirma.KdNr = SDL_ABHOLUNG.sdlAb_AbholungKdNr
        Else
            Me.KdFirma.Text = If(SDL_ABHOLUNG.sdlAb_AbholungFirma, "")
            MsgBox(SDL_ABHOLUNG.sdlAb_AbholungFirma)
        End If

        If SDL_ABHOLUNG.sdlAb_FahrerAbholAuftrag Is Nothing Then
            Me.cbxFahrerAbholung.CheckState = CheckState.Indeterminate
        ElseIf SDL_ABHOLUNG.sdlAb_FahrerAbholAuftrag Then
            Me.cbxFahrerAbholung.CheckState = CheckState.Checked
        Else
            Me.cbxFahrerAbholung.CheckState = CheckState.Unchecked
        End If

        Me.lblErfassung.Text = If(SDL_ABHOLUNG.sdlAb_Erfasst IsNot Nothing, CDate(SDL_ABHOLUNG.sdlAb_Erfasst).ToString("dd.MM.yyyy HH:mm"), "")
        Me.lblAholung.Text = If(SDL_ABHOLUNG.sdlAb_Abholung IsNot Nothing, CDate(SDL_ABHOLUNG.sdlAb_Abholung).ToString("dd.MM.yyyy HH:mm"), "")
        Me.lblSBErfassung.Text = If(SDL_ABHOLUNG.sdlAb_ErfassungSachbearbeiter, "")
        Me.lblSBAholung.Text = If(SDL_ABHOLUNG.sdlAb_AbholungSachbearbeiter, "")

        Me.txtHandy.Text = If(SDL_ABHOLUNG.sdlAb_AbholungFahrerSMS, "")
        Me.txtVermerkAbholung.Text = If(SDL_ABHOLUNG.sdlAb_AbholungVermerk, "")
        Me.txtVermerkErfassung.Text = If(SDL_ABHOLUNG.sdlAb_ErfassungVermerk, "")
        Me.cbxAbgeholt.CheckedValue = SDL_ABHOLUNG.sdlAb_Abgeholt
        Me.cbxAbholungBeiFremddienstleister.Checked = SDL_ABHOLUNG.sdlAb_AbholungFremdDL

        dgvPositionen.Rows.Clear()
        For Each r In SDL_ABHOLUNG.POS
            dgvPositionen.Rows.Add(r.sdlAbp_KfzKennzeichen, r.sdlAbp_SDLNr, r.sdlAbp_KarteBoxBezeichnung, r.sdlAbp_KartenNr, r.sdlAbp_PIN)
        Next

        If SDL_ABHOLUNG.sdlAb_Id > 0 Then lblAuftragsnr.Text = SDL_ABHOLUNG.sdlAb_Id.ToString("000000.##")
    End Sub


    Sub getValue()
        If SDL_ABHOLUNG Is Nothing Then SDL_ABHOLUNG = New cSDLAbholung(-1)

        SDL_ABHOLUNG.sdlAb_AbholungLKWKz = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(txtLKW.Text)
        SDL_ABHOLUNG.sdlAb_AbholungFahrername = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(txtFahrer.Text)
        SDL_ABHOLUNG.sdlAb_AbholungKdNr = Me.KdFirma.KdNrNullInt
        SDL_ABHOLUNG.sdlAb_AbholungFirma = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(KdFirma.KdName)
        SDL_ABHOLUNG.sdlAb_AbholungFahrerSMS = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(txtHandy.Text)
        SDL_ABHOLUNG.sdlAb_AbholungVermerk = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(txtVermerkAbholung.Text)
        SDL_ABHOLUNG.sdlAb_ErfassungVermerk = VERAG_PROG_ALLGEMEIN.SQL.isNullNothing(txtVermerkErfassung.Text)
        SDL_ABHOLUNG.sdlAb_AbholungFremdDL = cbxAbholungBeiFremddienstleister.Checked

        If cbxFahrerAbholung.CheckState = CheckState.Indeterminate Then
             SDL_ABHOLUNG.sdlAb_FahrerAbholAuftrag = Nothing
        Else
            SDL_ABHOLUNG.sdlAb_FahrerAbholAuftrag = (cbxFahrerAbholung.CheckState = CheckState.Checked)
        End If

        If SDL_ABHOLUNG.sdlAb_ErfassungSachbearbeiter Is Nothing Then SDL_ABHOLUNG.sdlAb_ErfassungSachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        If SDL_ABHOLUNG.sdlAb_ErfassungSachbearbeiterId Is Nothing Then SDL_ABHOLUNG.sdlAb_ErfassungSachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID

        SDL_ABHOLUNG.POS.Clear()
        '   For i = 0 To DataGridView.RowCount - 1
        For Each r As DataGridViewRow In dgvPositionen.Rows
            'Dim r As DataGridViewRow = DataGridView.Rows(i)
            Dim P As New cSDLAbholungPositionen
            P.sdlAbp_KundenNr = Me.KdFirma.KdNrNullInt
            P.sdlAbp_KartenNr = r.Cells("KartenNr").Value
            P.sdlAbp_KfzKennzeichen = r.Cells("KfzKennzeichen").Value
            P.sdlAbp_SDLNr = r.Cells("SDLNr").Value
            P.sdlAbp_KarteBoxBezeichnung = r.Cells("KarteBoxBezeichnung").Value
            P.sdlAbp_PIN = r.Cells("PIN").Value
            SDL_ABHOLUNG.POS.Add(P)
        Next

    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If cbxFahrerAbholung.CheckState = CheckState.Indeterminate Then
            cbxFahrerAbholung.CheckState = CheckState.Unchecked
        End If


        If saveME() Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub btnAbbrechen_Click(sender As Object, e As EventArgs) Handles btnAbbrechen.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        saveME()
        Me.DialogResult = Windows.Forms.DialogResult.None
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If saveME() Then
            Dim f As New frmSendSMS(txtHandy.Text, "Sie können die Karten/Boxen mit folgedner Auftragnummer bei VERAG Suben abholen: " & SDL_ABHOLUNG.sdlAb_Id.ToString("000000.##"))
            f.ShowDialog(Me)
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnSDLLaden.Click
        initdgv()
    End Sub



    Sub initdgv()
        Dim kundenSQL As New kundenSQL
        Dim sqlStr As String = ""

        Select Case cboSDLNr._value
            Case "209", "203" : cbxAbholungBeiFremddienstleister.Checked = True
        End Select

        Dim sdlnr As Integer = If(IsNumeric(cboSDLNr._value), cboSDLNr._value, -1)

        sqlStr = " SELECT  [KfzKennzeichen] ,SDLNr, " &
                             "  cast(CASE [SDLNr] " &
                                 "       WHEN '207'      THEN 'FREJUS'   " &
                                 "       WHEN '100'      THEN 'IDS'" &
                                 "       WHEN '101'      THEN 'WÖLFL'" &
                                 "       WHEN '202'      THEN 'IT'  " &
                                 "       WHEN '208'      THEN 'MSE' " &
                                 "       WHEN '211'      THEN 'MULTI-BOX' " &
                                 "       WHEN '210'      THEN 'SI' " &
                                 "       WHEN '212'      THEN 'UTA' " &
                                 "       WHEN '213'      THEN 'HUGO' " &
                                 "       WHEN '214'      THEN 'ROADMASTER' " &
                                 "       WHEN '215'      THEN 'IT' " &
                                 "       WHEN '216'      THEN 'CZ' " &
                                                                  "       WHEN '203'      THEN 'CZ' " &
                                 "       WHEN '209'      THEN 'PL' " &
                                 "       WHEN '500'      THEN 'ABGASPLAKETTE' " &
                                 "       WHEN '501'      THEN 'VERAG-CARD' " &
                                 "       Else '-' " &
                             "   END as VARCHAR(30)) AS KARTEBOX, " &
                             "  cast(CASE [SDLNr] " &
                                 "       WHEN '207'      THEN cast([FREJUS-CARD-Nr] as VARCHAR(200))  " &
                                 "       WHEN '100'      THEN cast(KartenNr as VARCHAR(200))" &
                                 "       WHEN '101'      THEN cast([KartenSchlüsselNr] as VARCHAR(200))" &
                                 "       WHEN '202'      THEN cast([TELEPASS-Nr] as VARCHAR(200)) " &
                                 "       WHEN '208'      THEN cast(KartenNr as VARCHAR(200)) " &
                                 "       WHEN '211'      THEN cast([PAN-Nr] as VARCHAR(200)) " &
                                 "       WHEN '210'      THEN cast([SERIE-Nr] as VARCHAR(200)) " &
                                 "       WHEN '212'      THEN cast(KartenNr as VARCHAR(200)) " &
                                 "       WHEN '213'      THEN cast(OBUID as VARCHAR(200)) " &
                                 "       WHEN '214'     THEN cast(OBUID as VARCHAR(200)) " &
                                 "       WHEN '215'      THEN cast([TELEPASS-Nr] as VARCHAR(200)) " &
                                 "       WHEN '216'      THEN cast(KartenNr as VARCHAR(200)) " &
                                 "       WHEN '203'      THEN cast(KartenNr as VARCHAR(200)) " &
                                 "       WHEN '209'      THEN cast(KartenNr/*[MSE-Kd-Nr]*/ as VARCHAR(200)) " &
                                 "       WHEN '500'      THEN '-' " &
                                 "       WHEN '501'      THEN cast(KartenNr as VARCHAR(200)) " &
                                 "       Else '-' " &
                             "   END as VARCHAR(200)) AS KARTEBOX_NR, " &
                                 "       [PIN] " &
                             " FROM [SDL] " &
                             " WHERE KundenNr = " & KdFirma.KdNr_value
        If sdlnr > 0 Then : sqlStr &= " AND SDLNr=" & sdlnr
        Else : sqlStr &= " AND SDLNr IN (100,101,202,207,208,210,211,212,213,214,215,216,500,501)"
        End If
        sqlStr &= " AND Lieferdatum between '" & datVon.Value.ToShortDateString & "' and '" & datBis.Value.ToShortDateString & "' "

        Select Case sdlnr
            Case 209, 203
                btnFahrer.Enabled = False
                btnFahrerDrucken.Enabled = False
        End Select

        ' Select Case sdlnr
        '    Case 100, 212, 208 : btnFahrer.Enabled = True : btnFahrerDrucken.Enabled = True
        'End Select


        Dim order As String = " ORDER BY KfzKennzeichen,SDLNr, Bestelldatum, BestellNr,Ausstellungsdatum,KartenNr,OBUID"

        Dim dt = kundenSQL.loadSQLDataAdapterBySqlWithoutReadonlyProblem(sqlStr & order, "SDL")
        If Not cboAnTabelleAnfuegen.Checked Then dgvPositionen.Rows.Clear()

        If dt IsNot Nothing Then
            For Each r In dt.Rows
                dgvPositionen.Rows.Add(r("KfzKennzeichen"), r("SDLNr"), r("KARTEBOX"), r("KARTEBOX_NR"), r("PIN"), r("KfzKennzeichen") & r("SDLNr"))
            Next
        End If
        FilterKZ()

        

        'CreateTableForGridView()
    End Sub


    Sub FilterKZ()
        'FahrerAbholAuftrag
        ' MsgBox("1")
        For Each r As DataGridViewRow In dgvPositionen.Rows
            r.Cells("SortClmn").Value = r.Cells("KfzKennzeichen").Value & r.Cells("SDLNr").Value
        Next
        ' MsgBox("2")
        'dgvPositionen.Sort(dgvPositionen.Columns("KfzKennzeichen"), System.ComponentModel.ListSortDirection.Ascending)
        dgvPositionen.Sort(dgvPositionen.Columns("SortClmn"), System.ComponentModel.ListSortDirection.Ascending)
    End Sub

    Function genFormular(sender As Button, Optional ByRef WordPath As String = "") As Microsoft.Office.Interop.Word.Document
        Me.Cursor = Cursors.WaitCursor
        FilterKZ()
        Dim SDLNr As Integer = -1 ' If(IsNumeric(cboSDLNr._value), cboSDLNr._value, -1)

        If dgvPositionen.Rows.Count > 0 Then
            SDLNr = -1 'PL+CZ
            Dim plfound = False
            Dim czfound = False
            For Each r As DataGridViewRow In dgvPositionen.Rows
                If r.Cells("SDLNr").Value IsNot DBNull.Value Then
                    Select Case r.Cells("SDLNr").Value
                        Case 209 : plfound = True
                        Case 203 : czfound = True
                        Case Else : SDLNr = -1
                    End Select
                End If
            Next
            If SDLNr > 0 Then
                If plfound And czfound Then
                    SDLNr = 999
                ElseIf plfound Then
                    SDLNr = 209
                ElseIf czfound Then
                    SDLNr = 203
                End If
            End If
        End If

        If SDLNr < 0 Then
            For Each r As DataGridViewRow In dgvPositionen.Rows
                If r.Cells("SDLNr").Value IsNot DBNull.Value Then
                    If SDLNr <= 0 Then
                        SDLNr = r.Cells("SDLNr").Value
                    Else
                        If SDLNr <> r.Cells("SDLNr").Value Then
                            SDLNr = -1
                            Exit For
                            'Verschiedene Leistungen --> SDLNR=-1 --> richtige .docx
                        End If
                    End If
                End If
            Next
        End If

        Dim KdNr As Integer = KdFirma.KdNr_value

        Dim fahrer = False
        If sender Is btnFahrer Or sender Is btnFahrerDrucken Then
            fahrer = True
        End If
        Dim TMPvisible = True
        If sender Is Button3 Then TMPvisible = False

        Dim print = False
        Dim printername = cboPrinter.Text
        If sender Is btnFaxDrucken Or sender Is btnFahrerDrucken Then
            print = True
        End If
        lblErrGb.Visible = False
        lblErrSprache.Visible = False

        Dim cGenWord As New cGenWord
        Dim gb = txtGueltigBis.Text
        If Not cbxVerlaengerung.Checked Then gb = " - " ' damit gb nicht leer ist
        Dim sprache = cboSprache._value ' DirectCast(cboSprache.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value


        If Not saveME(fahrer) Then Return Nothing

        If cbxVerlaengerung.Checked Then
            If fahrer Then

                'Dim f As New frmSDLAbholungEintrag(getSDLAbholung())
                ' If f.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                genFormular = cGenWord.genUebernahmebestaetigung(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                genFormular = cGenWord.genUebernahmebestaetigungKuvert(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                'End If

            Else

                ' If SDLNr = 209 Then

                'If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                ' If sprache <> "TR" And sprache <> "YU" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                'genFormular = cGenWord.genAbholbereitPL(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                '  ElseIf SDLNr = 203 Then

                '  If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Me.Cursor = Cursors.Default : Return Nothing
                '  genFormular = cGenWord.genAbholbereitCZ(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)

                If SDLNr = 100 Then 'IDS
                    ' If fahrer Then Exit Sub
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    If sprache <> "DE" And sprache <> "RO" And sprache <> "YU" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitIDS(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, cbxVerlaengerung.Checked, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                ElseIf SDLNr = 212 Then 'UTA
                    ' If fahrer Then Exit Sub
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    If sprache <> "DE" And sprache <> "RO" And sprache <> "YU" And sprache <> "TR" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitUTA(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, cbxVerlaengerung.Checked, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                ElseIf SDLNr = 208 Then 'MSE
                    '  If fahrer Then Exit Sub
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    If sprache <> "DE" And sprache <> "RO" And sprache <> "YU" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitMSE(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, cbxVerlaengerung.Checked, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                ElseIf SDLNr = 213 Then
                    Select Case cboSprache._value
                        Case "RO", "DE", "YU"
                            If fahrer Then

                                ' Dim f As New frmSDLAbholungEintrag(getSDLAbholung())
                                ' If f.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                                genFormular = cGenWord.genUebernahmebestaetigung(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                                'End If
                            Else
                                genFormular = cGenWord.genAbholbereitHUGO(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                            End If
                        Case Else : MsgBox("Diese Sprache ist bei der Leistung '213 HUGO' nicht verfügbar") : Me.Cursor = Cursors.Default : Return Nothing
                    End Select
                Else
                    MsgBox("Für diese Leistung ist die Funktion 'Verlängerung' nicht verfügbar") : Me.Cursor = Cursors.Default : Return Nothing
                End If
            End If
        Else
            If fahrer Then

                ' Dim f As New frmSDLAbholungEintrag(getSDLAbholung())
                'If f.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

                genFormular = cGenWord.genUebernahmebestaetigung(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                genFormular = cGenWord.genUebernahmebestaetigungKuvert(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                'End If
            Else
                If SDLNr = 999 Then 'Spezialfall PL+CZ
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    If sprache <> "TR" And sprache <> "YU" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitCZPL(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)

                ElseIf SDLNr = 209 Then
                    '    If fahrer Then Exit Sub
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    If sprache <> "DE" And sprache <> "TR" And sprache <> "YU" Then lblErrSprache.Visible = True : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitPL(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                ElseIf SDLNr = 203 Then
                    '  If fahrer Then Exit Sub
                    If gb = "" Then lblErrGb.Visible = True : Me.Cursor = Cursors.Default : Me.Cursor = Cursors.Default : Return Nothing
                    genFormular = cGenWord.genAbholbereitCZ(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, gb, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                Else

                    genFormular = cGenWord.genAbholbereit(cProgramFunctions.dgridViewTods(dgvPositionen), KdNr, VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, cboSprache._value, print, printername, SDL_ABHOLUNG.sdlAb_Id, WordPath, TMPvisible)
                End If
            End If
        End If
        Me.Cursor = Cursors.Default
    End Function

    Function saveME(Optional FahrerAbholAuftrag As Boolean = False) As Boolean
        '  lblErr_SaveMDM.Visible = False
        saveInProgress = True
        If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "MDM" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "EDV" Then
            lblErr_SaveMDM.Visible = True
            Return True
        End If
        lbl_ErrKunde.Visible = False
        If KdFirma.KdNr_value <= 0 Then lbl_ErrKunde.Visible = True : Return False

        getValue()
        If FahrerAbholAuftrag Then SDL_ABHOLUNG.sdlAb_FahrerAbholAuftrag = (cbxFahrerAbholung.CheckState = CheckState.Checked)
        saveME = SDL_ABHOLUNG.SAVE()
        setValue()
        saveInProgress = False
    End Function



    Private Sub btnFax_Click(sender As Object, e As EventArgs) Handles btnFax.Click
         genFormular(sender)
      End Sub

    Private Sub btnFaxDrucken_Click(sender As Object, e As EventArgs) Handles btnFaxDrucken.Click
        genFormular(sender)
    End Sub

    Private Sub btnFahrer_Click(sender As Object, e As EventArgs) Handles btnFahrer.Click
        If cbxFahrerAbholung.CheckState = CheckState.Indeterminate Then
            cbxFahrerAbholung.CheckState = CheckState.Checked
        End If
        genFormular(sender)
    End Sub

    Private Sub btnFahrerDrucken_Click(sender As Object, e As EventArgs) Handles btnFahrerDrucken.Click
        If cbxFahrerAbholung.CheckState = CheckState.Indeterminate Then
            cbxFahrerAbholung.CheckState = CheckState.Checked
        End If
        genFormular(sender)
    End Sub

    Private Sub cbxVerlaengerung_CheckedChanged(sender As Object, e As EventArgs) Handles cbxVerlaengerung.CheckedChanged
        txtGueltigBis.Enabled = sender.checked
    End Sub

    Private Sub MyComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSprache.SelectedIndexChanged, txtGueltigBis.TextChanged
        lblErrGb.Visible = False
        lblErrSprache.Visible = False
    End Sub

    Private Sub KdFirma_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Handles KdFirma.PropertyChanged
        If KdFirma.KdNr_value > 0 Then
            lbl_ErrKunde.Visible = False
        End If
        If Not saveInProgress Then

            If KdFirma.KdNr <> "" Then
                Select Case KdFirma.KdNr.ToString.Substring(0, 1)
                    Case "1" : cboSprache.changeItem("TR")
                    Case "2" : cboSprache.changeItem("RO")
                    Case "3" : cboSprache.changeItem("YU")
                    Case "4" : cboSprache.changeItem("YU")
                    Case Else : cboSprache.changeItem("DE")
                End Select
            End If
        End If
    End Sub

    Private Sub KdFirma_TextChanged(sender As Object, e As EventArgs) Handles KdFirma.TextChanged

    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
        Dim WordPath = ""
        Dim oWORD As Microsoft.Office.Interop.Word.Document = genFormular(sender, WordPath)
        If oWORD IsNot Nothing AndAlso WordPath <> "" Then
 
            oWORD.SaveAs(WordPath.Replace(".docx", "").Replace(".dotx", "") & ".pdf", Word.WdSaveFormat.wdFormatPDF)
            openMail(WordPath.Replace(".docx", "").Replace(".dotx", "") & ".pdf")
            oWORD.Close()
        End If
    End Sub

    Sub openMail(dest_path)
        Try



            Dim outl As New Outlook.Application
            Dim Mail As Object 'Microsoft.Office.Interop.Outlook.MailItem
            Mail = outl.CreateItem(0)
            ' Mail.To = If(KdFirma.KdData_ADRESSEN IsNot Nothing, KdFirma.KdData_ADRESSEN.E_Mail, "")
            Select Case KdFirma.KdNr.ToString.Substring(0, 1)
                Case "1" : Mail.To = "maut.tr@verag.ag"
                Case "2" : Mail.To = "ma@verag.ag" : Mail.CC = "maut.ro@verag.ag;ft@verag.ag"
                Case "3" : Mail.To = "z.batinic@verag.ag"
                Case "4" : Mail.To = "z.batinic@verag.ag"
                Case Else
            End Select
            Mail.Attachments.Add(dest_path, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, , "VERAG_Formular.pdf")
            Mail.Display()

        Catch ex As Exception
            MsgBox("Fehler beim erstellen der E-Mail:" & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub cbxAbholungBeiFremddienstleister_CheckedChanged(sender As Object, e As EventArgs) Handles cbxAbholungBeiFremddienstleister.CheckedChanged
        pnl.Enabled = Not cbxAbholungBeiFremddienstleister.Checked
        If cbxAbholungBeiFremddienstleister.Checked Then cbxFahrerAbholung.Checked = False
    End Sub
End Class