Imports GrapeCity.ActiveReports.ReportsCore.Tools

Public Class usrCntlSDLAbholung

    Dim FahrerAbholAuftrag = False
    Dim MONAT As Date = CDate("01." & Now.Month & "." & Now.Year) '.AddMonths(-1)
    Dim srcTAG As Date = CDate(Now.ToShortDateString)
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim JAHR As Date = CDate("01.01." & Now.Year)


    Sub New()

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

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

    End Sub


    Dim fullyPainted = False



    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = 15 AndAlso Not fullyPainted Then
            fullyPainted = True

            setDGVOptions()

            '  initAllg()
            '  initFilter()
        End If
    End Sub

    Sub New(FahrerAbholAuftrag As Boolean)

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

        If FahrerAbholAuftrag Then
            Me.FahrerAbholAuftrag = FahrerAbholAuftrag
            cbxFahrerAbholungsAuftrag.Checked = True
            cbxFahrerAbholungsAuftrag.Enabled = False
        End If
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub usrCntlSDLAbholung_Load(sender As Object, e As EventArgs) Handles Me.Load
        MyComboBox1.Items.Clear()
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Auftragsdatum", "AUFTRAG"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Abholungsdatum", "ABHOLUNG"))
        MyComboBox1.changeItem("AUFTRAG")

        txtAbfertDat.Text = Now.AddMonths(-6).ToShortDateString 'CDate("01." & Now.ToString("MM.yyyy")).AddMonths(-1).ToShortDateString
        txtAbfertDatBis.Text = Now.ToShortDateString

        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year

        cboSachbearbeiterErf.fillWithSQL("SELECT [mit_id],[mit_vname] + ' '+ [mit_nname] as NAME " &
                                  " FROM [ADMIN].[dbo].[tblMitarbeiter] WHERE mit_niederlassung IN ('SUB','Suben')  AND mit_abteilung='MDM' AND [mit_gekuendigt]=0   order by name", False, "ADMIN", True)
        cboSachbearbeiterAbh.fillWithSQL("SELECT [mit_id],[mit_vname] + ' '+ [mit_nname] as NAME " &
                                        " FROM [ADMIN].[dbo].[tblMitarbeiter] WHERE mit_niederlassung IN ('SUB','Suben')  AND mit_abteilung='ZOLL' AND [mit_gekuendigt]=0   order by name", False, "ADMIN", True)
        '    btnMonat.PerformClick()
        '   If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "MDM" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "EDV" Then
        'cbxFahrerAbholungsAuftrag. 
        '   End If

        btnSuche.PerformClick()

        ' If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("BH_Vorauskasse_buchen", "SDL") Then
        'pnlBH.Visible = True
        ' End If
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", "SDL") Then
            btnDel.Visible = True
        End If

        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung
            Case "ZOLL" : Panel2.Visible = False
                '  Case "MDM" : pnlBH.Visible = False
            Case "MDM" : Button1.Text = "Karten/Boxen" & vbNewLine & "abgeholt/verschickt"

        End Select


        txtAuftragsnr.Focus()

    End Sub


    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtAbfertDat._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtAbfertDatBis._value = CDate(sender.value).ToShortDateString
    End Sub

    Sub initBtns(MonatJahr)
        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year
        Select Case MonatJahr
            Case "Monat"
                DateTimePicker1.Value = MONAT
                DateTimePicker2.Value = MONAT.AddMonths(1).AddDays(-1)
            Case "Jahr"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
                'End If
            Case "Tag"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = srcTAG
                DateTimePicker2.Value = srcTAG
                If srcTAG = Now.ToShortDateString Then
                    Button5.Text = "Heute"
                ElseIf srcTAG = Now.AddDays(-1).ToShortDateString Then
                    Button5.Text = "Gestern"
                ElseIf srcTAG = Now.AddDays(-2).ToShortDateString Then
                    Button5.Text = "Vorgestern"
                Else
                    Button5.Text = srcTAG.ToString("dd.MM")
                End If

                'End If
            Case ""
                DateTimePicker1.Value = ("01." & Now.Month & "." & Now.Year)
                DateTimePicker2.Value = Now
        End Select
        btnSuche.PerformClick()
    End Sub


    Private Sub btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        Me.Cursor = Cursors.WaitCursor

        Dim TopMax = If(IsNumeric(txtMaxSrch.Text), " TOP " & txtMaxSrch.Text, "")
        Dim sqlstr As String = ""


        sqlstr &= " SELECT " & TopMax & " [sdlAb_Id],[sdlAb_Erfasst],[sdlAb_AbholungFirma],[sdlAb_AbholungKdNr],[sdlAb_AbholungVermerk],[sdlAb_AbholungLKWKz],[sdlAb_AbholungFahrername],[sdlAb_AbholungFahrerSMS],[sdlAb_Abholung],[sdlAb_AbholungSachbearbeiter],[sdlAb_AbholungSachbearbeiterId],[sdlAb_Storniert],[sdlAb_ErfassungSachbearbeiter],[sdlAb_FahrerAbholAuftrag],[sdlAb_ErfassungSachbearbeiterId],[sdlAb_Abgeholt]  FROM [tblSDLAbholung]  WHERE 1=1 "

        With MyDatagridview1
            '  .DataSource = myTable
            .SET_SQL(sqlstr & getWhere() & " ORDER BY sdlAb_AbholungFirma asc ", "SDL", getList)
            .LOAD()
            setDGVOptions()
        End With
        showDGV()

        Me.Cursor = Cursors.Default

    End Sub


    Function getWhere() As String
        Dim sqlstr = ""

        If MyComboBox1._value = "AUFTRAG" Then
            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([sdlAb_Erfasst] BETWEEN @DatVon AND @DatBis ) "
                Else
                    sqlstr &= " AND ([sdlAb_Erfasst] BETWEEN @DatVon AND @DatVon ) "
                End If
            End If
        Else
            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([sdlAb_Abholung] BETWEEN @DatVon AND @DatBis ) "
                Else
                    sqlstr &= " AND ([sdlAb_Abholung] BETWEEN @DatVon AND @DatVon ) "
                End If
            End If
        End If
        If Not IsNumeric(txtAuftragsnr.Text) Then txtAuftragsnr.Text = ""
        If txtAuftragsnr.Text <> "" Then sqlstr &= " AND sdlAb_Id LIKE @AuftrNr "
        If cboSachbearbeiterErf._value <> "" Then sqlstr &= " AND sdlAb_ErfassungSachbearbeiterId LIKE @PersonalnummerErf "
        If cboSachbearbeiterAbh._value <> "" Then sqlstr &= " AND sdlAb_AbholungSachbearbeiterId LIKE @PersonalnummerAbh "
        If txtLKW.Text <> "" Then sqlstr &= " AND sdlAb_AbholungLKWKz LIKE @LKW "
        If txtFirma.Text <> "" Then sqlstr &= " AND sdlAb_AbholungFirma LIKE @Firma "
        If txtFahrer.Text <> "" Then sqlstr &= " AND sdlAb_AbholungFahrername LIKE @Fahrer "

        If cbxAbhegolt.CheckState <> CheckState.Indeterminate Then
            sqlstr &= " AND sdlAb_Abgeholt =" & If(cbxAbhegolt.Checked, 1, 0) & " "
        End If
        If cbxStorno.CheckState <> CheckState.Indeterminate Then
            sqlstr &= " AND sdlAb_Storniert =" & If(cbxStorno.Checked, 1, 0) & " "
        End If

        If cbxFahrerAbholungsAuftrag.CheckState <> CheckState.Indeterminate Then
            sqlstr &= " AND sdlAb_FahrerAbholAuftrag =" & If(cbxFahrerAbholungsAuftrag.Checked, 1, 0) & " "
        End If

        If cbxAbholungFremdDL.CheckState <> CheckState.Indeterminate Then
            sqlstr &= " AND sdlAb_AbholungFremdDL =" & If(cbxAbholungFremdDL.Checked, 1, 0) & " "
        End If



        Return sqlstr
    End Function

    Function getList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatVon", txtAbfertDat.Text & " 00:00:00"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatBis", txtAbfertDatBis.Text & " 23:59:59"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AuftrNr", txtAuftragsnr.Text.Replace("*", "%")))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PersonalnummerErf", cboSachbearbeiterErf._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PersonalnummerAbh", cboSachbearbeiterAbh._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKW", txtLKW.Text & "%".Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma", "%" & txtFirma.Text & "%"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fahrer", txtFahrer.Text & "%".Replace("*", "%")))


        Return list

    End Function

    Sub showDGV()

        With MyDatagridview1

            If .ColumnCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False

            ' .Columns("sdlAb_Id").Visible = False
            .Columns("sdlAb_Id").Width = 80
            .Columns("sdlAb_Id").HeaderText = "Auftrags-Nr"
            .Columns("sdlAb_Id").DefaultCellStyle.Format = "000000.##"
            .Columns("sdlAb_Id").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("sdlAb_Erfasst").Width = 100
            .Columns("sdlAb_Erfasst").HeaderText = "Datum"
            .Columns("sdlAb_Erfasst").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter


            .Columns("sdlAb_ErfassungSachbearbeiter").Width = 100
            .Columns("sdlAb_ErfassungSachbearbeiter").HeaderText = "Erfassung SB"
            .Columns("sdlAb_AbholungSachbearbeiter").Width = 100
            .Columns("sdlAb_AbholungSachbearbeiter").HeaderText = "Abhol. SB"

            .Columns("sdlAb_ErfassungSachbearbeiterId").Visible = False
            .Columns("sdlAb_AbholungSachbearbeiterId").Visible = False

            '.Columns("sdlAb_ErfassungVermerk").MinimumWidth = 100
            ' .Columns("sdlAb_ErfassungVermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            '.Columns("sdlAb_ErfassungVermerk").HeaderText = "Vermerk"

            .Columns("sdlAb_AbholungLKWKz").Width = 110
            .Columns("sdlAb_AbholungLKWKz").HeaderText = "LKW Abholung"

            .Columns("sdlAb_AbholungFahrername").Width = 150
            .Columns("sdlAb_AbholungFahrername").HeaderText = "Fahrer Abholung"

            .Columns("sdlAb_AbholungFahrerSMS").Width = 100
            .Columns("sdlAb_AbholungFahrerSMS").HeaderText = "Handy"

            .Columns("sdlAb_AbholungKdNr").Width = 60
            .Columns("sdlAb_AbholungKdNr").HeaderText = "KdNr"
            .Columns("sdlAb_AbholungKdNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

            .Columns("sdlAb_AbholungFirma").Width = 200
            .Columns("sdlAb_AbholungFirma").HeaderText = "Firma Abholung"

            .Columns("sdlAb_Abholung").Width = 120
            .Columns("sdlAb_Abholung").HeaderText = "Datum"

            .Columns("sdlAb_Abgeholt").Width = 50
            .Columns("sdlAb_Abgeholt").HeaderText = "Abgeholt"

            .Columns("sdlAb_AbholungVermerk").MinimumWidth = 100
            .Columns("sdlAb_AbholungVermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("sdlAb_AbholungVermerk").HeaderText = "Vermerk"
            .Columns("sdlAb_AbholungVermerk").DefaultCellStyle.ForeColor = Color.Red
            .Columns("sdlAb_AbholungVermerk").DefaultCellStyle.Font = New Font(.Font.FontFamily, Font.Size, FontStyle.Bold)


            .Columns("sdlAb_Storniert").HeaderText = "Storno"
            .Columns("sdlAb_Storniert").Width = 45



            .Columns("sdlAb_FahrerAbholAuftrag").HeaderText = "ZB Fahrer"
            .Columns("sdlAb_FahrerAbholAuftrag").Width = 60
            '  lblErgebnis.Text = "Ergebnise: " & .RowCount

            colorizeCells()
        End With
    End Sub


    Private Sub FlatButton7_Click(sender As Object, e As EventArgs) Handles FlatButton7.Click
        MyDatagridview1.RELOAD()
        setDGVOptions()
    End Sub

    Sub setDGVOptions()
        Try
            With MyDatagridview1
                For Each r As DataGridViewRow In .Rows
                    If r.Cells("sdlAb_Storniert").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Storniert").Value Then
                        r.DefaultCellStyle.ForeColor = Color.Red
                        r.DefaultCellStyle.SelectionForeColor = Color.Red
                        '    r.DefaultCellStyle.SelectionBackColor = Color.White
                    ElseIf r.Cells("sdlAb_Abgeholt").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Abgeholt").Value Then
                        If r.Cells("sdlAb_FahrerAbholAuftrag").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_FahrerAbholAuftrag").Value Then
                            r.DefaultCellStyle.ForeColor = Color.DarkGreen
                            r.DefaultCellStyle.SelectionForeColor = Color.DarkGreen
                        Else
                            r.DefaultCellStyle.ForeColor = Color.RoyalBlue
                            r.DefaultCellStyle.SelectionForeColor = Color.RoyalBlue

                        End If

                       '  r.DefaultCellStyle.SelectionBackColor = Color.White
                    End If
                Next
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV_NEW(MyDatagridview1, False)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_Abgeholt").Value IsNot DBNull.Value AndAlso MyDatagridview1.SelectedRows(0).Cells("sdlAb_Abgeholt").Value Then
                'ist schon abgeholt
                Exit Sub
            End If

            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)
            Dim FahrerTmp = Nothing
            If (VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung = "MDM") Then
                FahrerTmp = FahrerTmp
            End If
            VK.SET_ABHOLUNG(True, txtBemerkung.Text, FahrerTmp)


            txtBemerkung.Text = ""
            MyDatagridview1.RELOAD()
            setDGVOptions()
            ' cVorauskasseOptions.insertVermerk(MyDatagridview1.SelectedRows(0).Cells("vk_FilialeNr").Value, MyDatagridview1.SelectedRows(0).Cells("vk_AbfertigungsNr").Value, 14)
        End If
    End Sub

    Private Sub MyDatagridview1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MyDatagridview1.CellMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            MyDatagridview1.ClearSelection()
            MyDatagridview1.Rows(e.RowIndex).Selected = True
            MyDatagridview1.CurrentCell = MyDatagridview1.Rows(e.RowIndex).Cells(0)
            cntxtOptionen.Show(System.Windows.Forms.Cursor.Position)
        End If
    End Sub

    Private Sub MyDatagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles MyDatagridview1.SelectionChanged
        txtBemerkung.Text = ""
        '  MyTextBox1.Text = ""
        Label7.Visible = False
        pnlBH.Enabled = True
        Panel2.Enabled = True
        btnBuchungAufheben.Visible = False
        If MyDatagridview1.SelectedRows.Count > 0 Then
            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_FahrerAbholAuftrag").Value Is DBNull.Value OrElse Not MyDatagridview1.SelectedRows(0).Cells("sdlAb_FahrerAbholAuftrag").Value Then
                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "MDM" Then ' MDM dürfen immer auf abgeholt setzen, kann sein, dass p.Post geschickt, auch wenn urspr. zur Abholung gedacht.
                    pnlBH.Enabled = False
                End If
                Panel2.Enabled = False
            End If

            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_Abgeholt").Value Then
                btnBuchungAufheben.Visible = True
            End If
            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_AbholungVermerk").Value IsNot DBNull.Value Then
                txtBemerkung.Text = MyDatagridview1.SelectedRows(0).Cells("sdlAb_AbholungVermerk").Value
                ' MyTextBox1.Text = txtBemerkung.Text
            End If

            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_AbholungKdNr").Value IsNot DBNull.Value AndAlso IsNumeric(MyDatagridview1.SelectedRows(0).Cells("sdlAb_AbholungKdNr").Value) Then
                Dim cntFirma = SQL.getValueTxtBySql("SELECT COUNT(*) FROM [SDL].[dbo].[tblSDLAbholung]  where sdlAb_AbholungKdNr=" & MyDatagridview1.SelectedRows(0).Cells("sdlAb_AbholungKdNr").Value & " AND sdlAb_Abgeholt=0 AND sdlAb_FahrerAbholAuftrag=1 AND sdlAb_Storniert=0 ", "SDL")
                If cntFirma > 1 Then Label7.Text = "Es sind " & cntFirma & " Aufträge vorhanden!" : Label7.Visible = True
            End If
        End If
    End Sub

    Private Sub btnBuchungAufheben_Click(sender As Object, e As EventArgs) Handles btnBuchungAufheben.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)
            VK.SET_ABHOLUNG(False, txtBemerkung.Text)
            txtBemerkung.Text = ""

            MyDatagridview1.RELOAD()
            setDGVOptions()

        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)

            If VK.sdlAb_Storniert Then
                If vbYes <> MsgBox("Möchten Sie die Stornierung auflösen?", vbYesNoCancel) Then Exit Sub
            Else
                If vbYes <> MsgBox("Möchten Sie den Auftrag wirklich stornieren?", vbYesNoCancel) Then Exit Sub
            End If

            VK.SET_STORNO()
            MyDatagridview1.RELOAD()
            setDGVOptions()
        End If
    End Sub


    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        srcTAG = srcTAG.AddDays(-1)
        initBtns("Tag")
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        srcTAG = srcTAG.AddDays(1)
        initBtns("Tag")
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        initBtns("Tag")
    End Sub

    Private Sub btnMonat_Click(sender As Object, e As EventArgs) Handles btnMonat.Click
        initBtns("Monat")
    End Sub



    Private Sub btnJahr_Click(sender As Object, e As EventArgs) Handles btnJahr.Click
        initBtns("Jahr")
    End Sub

    Private Sub btnBisMonat_Click(sender As Object, e As EventArgs) Handles btnBisMonat.Click
        MONAT = MONAT.AddMonths(1)
        initBtns("Monat")
    End Sub

    Private Sub btnVonMonat_Click(sender As Object, e As EventArgs) Handles btnVonMonat.Click
        MONAT = MONAT.AddMonths(-1)
        initBtns("Monat")
    End Sub

    Private Sub btnBisJahr_Click(sender As Object, e As EventArgs) Handles btnBisJahr.Click
        JAHR = JAHR.AddYears(1)
        initBtns("Jahr")
    End Sub

    Private Sub btnVonJahr_Click(sender As Object, e As EventArgs) Handles btnVonJahr.Click
        JAHR = JAHR.AddYears(-1)
        initBtns("Jahr")
    End Sub '

    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click

        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)
            If vbYes <> MsgBox("Möchten Sie den Auftrag wirklich löschen?", vbYesNoCancel) Then Exit Sub
            VK.delete()
            MyDatagridview1.RELOAD()
            setDGVOptions()
        End If

    End Sub


    Private Sub DataGridView_CellContentClick(sender As Object, e As EventArgs) Handles MyDatagridview1.DoubleClick, MyDatagridview1.CellContentDoubleClick
        If MyDatagridview1.SelectedRows.Count = 0 Then Exit Sub
        'If tbcntr.SelectedIndex = 0 Then
        Try
            Dim f As New frmSDLAbholungEintrag(CInt(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value))

            f.ShowDialog(Me)
            MyDatagridview1.RELOAD()
            setDGVOptions()
        Catch ex As Exception
            MsgBox(ex.StackTrace)
        End Try
        ' End If

        '   frmNacherfassung.showAll()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New frmSDLAbholungEintrag
        If f.ShowDialog(Me) = DialogResult.OK Then
            MyDatagridview1.RELOAD()
            setDGVOptions()
        End If
    End Sub
     
    Private Sub AbgeholtToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AbgeholtToolStripMenuItem.Click
        Button1.PerformClick()
    End Sub

    Private Sub StornoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StornoToolStripMenuItem.Click
        Button3.PerformClick()
    End Sub

    Private Sub txtAuftragsnr_KeyDown(sender As Object, e As KeyEventArgs) Handles txtAuftragsnr.KeyDown, txtFirma.KeyDown, txtLKW.KeyDown, txtFahrer.KeyDown
        If e.KeyCode = Keys.Return Then
            btnSuche.PerformClick()
        End If
    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            
            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)
            VK.SET_BEMERKUNG(txtBemerkung.Text)
            txtBemerkung.Text = ""
            MyDatagridview1.RELOAD()
            setDGVOptions()
            ' cVorauskasseOptions.insertVermerk(MyDatagridview1.SelectedRows(0).Cells("vk_FilialeNr").Value, MyDatagridview1.SelectedRows(0).Cells("vk_AbfertigungsNr").Value, 14)
        End If
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            If MyDatagridview1.SelectedRows(0).Cells("sdlAb_FahrerAbholAuftrag").Value Is DBNull.Value OrElse Not MyDatagridview1.SelectedRows(0).Cells("sdlAb_FahrerAbholAuftrag").Value Then
                'ist eh nicht zum Abholen
                Exit Sub
            End If

            Dim VK As New cSDLAbholung(MyDatagridview1.SelectedRows(0).Cells("sdlAb_Id").Value)
            VK.SET_FAHRERABHOLUNG(False, txtBemerkung.Text)
            txtBemerkung.Text = ""
            MyDatagridview1.RELOAD()
            setDGVOptions()
            ' cVorauskasseOptions.insertVermerk(MyDatagridview1.SelectedRows(0).Cells("vk_FilialeNr").Value, MyDatagridview1.SelectedRows(0).Cells("vk_AbfertigungsNr").Value, 14)
        End If
    End Sub


    Private Sub MyDatagridview1_Layout(sender As Object, e As LayoutEventArgs) Handles MyDatagridview1.Layout
        colorizeCells()
    End Sub

    Sub colorizeCells()
        Dim kdNrTmp = "-1"
        Dim bgColor = Color.White

        For Each r As DataGridViewRow In MyDatagridview1.Rows
            ' MsgBox(r.Cells("sdlAb_AbholungKdNr").Value & " - " & kdNrTmp)
            If r.Cells("sdlAb_AbholungKdNr").Value <> kdNrTmp Then
                kdNrTmp = r.Cells("sdlAb_AbholungKdNr").Value
                bgColor = If(bgColor = Color.White, Color.LightGoldenrodYellow, Color.White)
            End If
            r.DefaultCellStyle.BackColor = bgColor

        Next
    End Sub

    Private Sub DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

    End Sub
End Class

