Imports System.Data.SqlClient

Public Class usrCntlDienstplan

    Private bindingMA As New BindingSource
    Private dataAdapterMA As New SqlDataAdapter

    Dim SQLDienst As New cDienstplan
    Dim niederlassung = ""

    Private Sub btnDienstplan_Click(sender As Object, e As EventArgs)
        frmDienstplan.Show()
    End Sub

    'Sub getNiederlassung()
    '   If tbcntr.TabPages.Count = 0 Then Exit Sub
    '   niederlassung = tbcntr.SelectedTab.Name.Replace("tb", "")
    ' End Sub

    Private Sub usrCntlDienstplan_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' getNiederlassung()

        ' If frmLogin.USRID <> 4 Then
        '  tbcntr.SelectedIndex = 0
        'tbcntr.Enabled = False
        If VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME = "ADMIN" Then
            btnAuswertung.Visible = True
            Button5.Visible = True
            cbxAuswertungPDF.Visible = True
        End If

        initNiederlassungen(True)
        '  tbcntr.SelectedIndex = 0
       

        '  getNiederlassung()
        Me.dgvMA.DataSource = Me.bindingMA
        initMABinding()


        cboMonat.Items.Clear()
        For i = 1 To 12
            cboMonat.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(CDate("01." & i & "." & Now.Year).ToString("MMMM"), i))
        Next
        cboMonat.changeItem(Now.Month)
        numJahr.Value = Now.Year

        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("DP_Bearbeiten", Me.FindForm) Then
            Panel3.Enabled = False
        End If

    End Sub


    Private Sub tbFirmen_TabIndexChanged(sender As Object, e As EventArgs) Handles tbFirmen.SelectedIndexChanged
        initNiederlassungen()
    End Sub

    Sub initNiederlassungen(Optional start = False)
        MyFlowLayoutPanel1.Controls.Clear()
        If tbFirmen.TabPages.Count = 0 Then Exit Sub
        If tbFirmen.SelectedIndex < 0 Then Exit Sub
        Dim s As New VERAG_PROG_ALLGEMEIN.SQL
        Dim dt As DataTable = s.loadDgvBySql("SELECT [dpset_id],[dpset_niederlassung]  FROM [tblDienstSettings] WHERE dpset_firma='" & tbFirmen.SelectedTab.Text & "' order by dpset_reihenfolge ", "ADMIN")
        For Each r As DataRow In dt.Rows
            ' Dim d As New TabPage()
            '  d.Text = r("dpset_niederlassung")
            ' d.Name = "tb" & r("dpset_niederlassung")


            Dim b As New Button
            b.Size = New Size(70, 36)
            b.Text = r("dpset_niederlassung")
            b.Name = "tb" & r("dpset_niederlassung")
            b.FlatStyle = FlatStyle.Flat

            If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardDP IsNot Nothing Then
                If b.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardDP Then

                End If
            End If


            AddHandler b.Click, Sub()
                                    initButtons(b.Text)
                                End Sub
            MyFlowLayoutPanel1.Controls.Add(b)
        Next
        Dim initDone = False

        If start Then

            If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", Me.FindForm) And Not VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma = "ALLE" Then
                'tbFirmen.Enabled = False


                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma <> "ATILLA" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma2 <> "ATILLA" Then tbFirmen.TabPages.Remove(tbAtilla2)
                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma <> "IMEX" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma2 <> "IMEX" Then tbFirmen.TabPages.Remove(tbImex)
                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma <> "VERAG" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma2 <> "VERAG" Then tbFirmen.TabPages.Remove(tbVerag)
                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma <> "FRONTOFFICE" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma2 <> "FRONTOFFICE" Then tbFirmen.TabPages.Remove(tbFrontOffice)
                If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma <> "AMBAR" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma2 <> "AMBAR" Then tbFirmen.TabPages.Remove(tbAMBAR)

                Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma
                    Case "ATILLA" : tbFirmen.SelectedTab = tbAtilla2
                    Case "IMEX" : tbFirmen.SelectedTab = tbImex
                    Case "VERAG" : tbFirmen.SelectedTab = tbVerag
                    Case "FRONTOFFICE" : tbFirmen.SelectedTab = tbFrontOffice
                    Case "AMBAR" : tbFirmen.SelectedTab = tbAMBAR
                        '   Case "ALLE" : tbFirmen.SelectedTab = tbVerag : tbFirmen.Enabled = True
                    Case Else : tbFirmen.SelectedTab = tbVerag
                End Select
            End If
            ''Einschränkung DP Firma:
            'If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma IsNot Nothing Then
            '    Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma
            '        Case "ATILLA" : tbFirmen.SelectedTab = tbAtilla2
            '        Case "IMEX" : tbFirmen.SelectedTab = tbImex
            '        Case "VERAG" : tbFirmen.SelectedTab = tbVerag
            '        Case "FRONTOFFICE" : tbFirmen.SelectedTab = tbFrontOffice
            '            '   Case "ALLE" : tbFirmen.SelectedTab = tbVerag : tbFirmen.Enabled = True
            '        Case Else : tbFirmen.SelectedTab = tbVerag
            '    End Select
            '    If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", Me.FindForm) And Not VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma = "ALLE" Then tbFirmen.Enabled = False
            'End If

            'Wenn zusätzlich eingeschränkt auf NL:
            If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardDP IsNot Nothing AndAlso VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardDP <> "" Then
                initButtons(VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardDP)
                initDone = True
                If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", Me.FindForm) And Not VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_DpUserStandardFirma = "ALLE" Then
                    For Each b As Button In MyFlowLayoutPanel1.Controls
                        b.Enabled = False
                        tbFirmen.Enabled = False
                    Next
                End If
            End If
        End If

        If Not initDone Then
            If MyFlowLayoutPanel1.Controls.Count > 0 Then
                initButtons(DirectCast(MyFlowLayoutPanel1.Controls(0), Button).Text)
            End If
        End If
    End Sub

    Sub initButtons(s)
        For Each b As Button In MyFlowLayoutPanel1.Controls
            If b.Text = s Then
                b.BackColor = Color.SteelBlue
                b.ForeColor = Color.White
            Else
                b.BackColor = Color.White
                b.ForeColor = Color.Black
            End If
        Next
        niederlassung = s
        initMABinding()
    End Sub


    Public Sub initMABinding()


        Dim sqlstr As String = " SELECT dstma_id,dstma_mitId,dstma_kuerzel,mit_nname + ' ' + mit_vname, dstma_abteilung,dstma_funktion,dstma_arbvh,dstma_wochenStunden,dstma_stammSchicht,dstma_farbe,dstma_reihenfolge FROM tblDienstMitarb,tblMitarbeiter WHERE mit_id=dstma_mitId and mit_gekuendigt =" & If(cbxGekAnzeigen.Checked, 1, 0) & "  and dstma_inaktiv =" & If(cbxInaktiv.Checked, 1, 0) & " AND dstma_niederlassung='" & niederlassung & "' ORDER BY dstma_kuerzel"
        ' dgvLKW.DataSource = bindingSDL


        Dim d As New SqlDataAdapter()
        Try
            Dim connectionString As String = My.Resources.connStringAdmin

            'Dim sqlstr As String = "SELECT top 200 * FROM SDL " 'WHERE KundenNr=" & kdNr & " "
            'If TextBox11.Text <> "" Then sqlstr &= " AND KfzKennzeichen LIKE '" & TextBox11.Text & "%'" 'Suchparam
            'Dim filter As String = getFilter()
            'If filter <> "" Then sqlstr &= " AND " & filter
            ' MsgBox(sqlstr)
            Me.dataAdapterMA = New SqlDataAdapter(sqlstr, connectionString)
        Catch ex As SqlException
            MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.")
        End Try
        Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapterMA)
        Dim table As New DataTable()
        Me.dataAdapterMA.Fill(table)
        '   table.Columns("KundenNr").DefaultValue = kdNr
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        'bindingSDL.Clear()
        dgvMA.DataBindings.Clear()
        bindingMA.DataSource = table
        initAllg()
    End Sub

    Sub initAllg()
        With dgvMA
            If .ColumnCount <= 0 Then Exit Sub
            .RowTemplate.Height = 20
            .AllowUserToOrderColumns = False
            .AllowUserToResizeRows = False
            .AllowUserToAddRows = False
            .Columns(0).Visible = False
            .Columns(1).Visible = False

            .Columns(2).HeaderText = "Kürzel"
            .Columns(3).HeaderText = "Name"
            .Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns(4).HeaderText = "Abteilung"
            .Columns(5).HeaderText = "Funktion"
            .Columns(6).HeaderText = "Arbeitsverhältnis"
            .Columns(7).HeaderText = "Wochenstunden"
            .Columns(8).HeaderText = "Stamm-Schicht"
            .Columns(10).HeaderText = "Reihenf."
            .Columns(9).Visible = False
            '.AutoGenerateColumns = False '??HM:::
        End With

    End Sub

    Private Sub Button64_Click(sender As Object, e As EventArgs) Handles Button64.Click
        'MsgBox(niederlassung)
        Dim frmDiestAddMa As New frmDiestAddMa
        frmDiestAddMa.niederlassung = niederlassung
        frmDiestAddMa.Show()

        AddHandler frmDiestAddMa.FormClosing, AddressOf initMABinding
    End Sub


    Private Sub btnDienstplan_Click_1(sender As Object, e As EventArgs) Handles btnDienstplan.Click
        Dim frmDienstplanVariabel As New frmDienstplanVariabel(niederlassung)
        frmDienstplanVariabel.Show()
        Exit Sub
        If niederlassung = "ATILLA" Then : frmDienstplanATILLA.niederlassung = niederlassung : frmDienstplanATILLA.Show()
        ElseIf niederlassung = "WAI" Then : frmDienstplanVariabel.niederlassung = niederlassung : frmDienstplanVariabel.Show()
        ElseIf niederlassung = "SBG" Then : frmDienstplanVariabel.niederlassung = niederlassung : frmDienstplanVariabel.Show()
        ElseIf niederlassung = "NKD" Then : frmDienstplanVariabel.niederlassung = niederlassung : frmDienstplanVariabel.Show()
        ElseIf niederlassung = "SUB" Then : frmDienstplan.niederlassung = niederlassung : frmDienstplan.Show()
        ElseIf niederlassung = "VERR" Then : frmDienstplanVariabel.niederlassung = niederlassung : frmDienstplanVariabel.Show()
        End If
    End Sub

    Private Sub dgvMA_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMA.CellDoubleClick
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("DP_Bearbeiten", Me.FindForm) Then

            frmDiestAddMa.MA_ID = dgvMA.CurrentRow.Cells(0).Value
            frmDiestAddMa.niederlassung = niederlassung
            frmDiestAddMa.Show()
            AddHandler frmDiestAddMa.FormClosing, AddressOf initMABinding
        End If
    End Sub

    Private Sub Button65_Click(sender As Object, e As EventArgs) Handles Button65.Click
        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", Me.FindForm) Then
            MsgBox("Nur Administratoren können einen Eintrag löschen!")
        Else
            Dim sql As New cDienstplan
            If vbYes = MsgBox("Möchten Sie den Mitarbeiter " & dgvMA.CurrentRow.Cells(2).Value & " wirklich aus dem Dienstplan löschen?", vbYesNoCancel) Then
                '  sql.delDienstNichtAnwesend2(dgvMA.CurrentRow.Cells(0).Value)
                sql.delDienstNichtAnwesend_ByMaID(dgvMA.CurrentRow.Cells(0).Value)
                sql.delDienstNichtAnwesendKW_ByMaID(dgvMA.CurrentRow.Cells(0).Value)
                sql.delDstEintraegeAllMaId(dgvMA.CurrentRow.Cells(0).Value)
                sql.delDienstTeilzeit(dgvMA.CurrentRow.Cells(0).Value)
                sql.delDienstMitarbeiter(dgvMA.CurrentRow.Cells(0).Value)
                initMABinding()
            End If
        End If
    End Sub

    Private Sub dgvMA_Paint(sender As Object, e As PaintEventArgs) Handles dgvMA.Paint
        For Each r As DataGridViewRow In dgvMA.Rows
            Try : r.DefaultCellStyle.ForeColor = ColorTranslator.FromHtml(r.Cells(9).Value) : Catch : End Try
        Next
    End Sub

    Private Sub tbcntr_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxGekAnzeigen.CheckedChanged
        '   getNiederlassung()
        initMABinding()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New frmSchichtenZeiten
        f.niederlassung = niederlassung
        f.ShowDialog(Me)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim frmUrlaubsplaner As New frmUrlaubsplaner
        frmUrlaubsplaner.niederlassung = niederlassung
        frmUrlaubsplaner.Show()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim f As New frmDienstEinstellungen
        f.niederlassung = niederlassung
        f.ShowDialog(Me)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs)
        frmDienstplanVariabel.niederlassung = niederlassung : frmDienstplanVariabel.Show()
    End Sub

    Private Sub btnAuswertung_Click(sender As Object, e As EventArgs) Handles btnAuswertung.Click
        If dgvMA.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim pdfPath = ""
        If cbxAuswertungPDF.Checked Then
            Dim fd As New FolderBrowserDialog
            If fd.ShowDialog = DialogResult.OK Then
                pdfPath = fd.SelectedPath
            End If
        End If
        For Each r In dgvMA.SelectedRows
            genMonatsauswertungMA(r.Cells("dstma_id").Value, True, cbxAuswertungPDF.Checked, pdfPath)
        Next
    End Sub

    Sub genMonatsauswertungMA(dstMaID, groupKW, Optional pdfExport = False, Optional pdfPfad = "")
        Dim displayFilter = False

        Dim vonDat = CDate("01." & cboMonat._value & "." & numJahr.Value)
        Dim bisDat = vonDat.AddMonths(1).AddDays(-1)
        Dim sqlstr As String = ""
        sqlstr &= " SELECT * ,datepart(ISO_WEEK,dstetr_datum) as KW FROM [tblDienstplanEintraege]  "
        sqlstr &= " where dstetr_datum between '" & vonDat.ToShortDateString & "' and '" & bisDat.ToShortDateString & "' "
        sqlstr &= " and dstetr_dstmaId='" & dstMaID & "' "
        sqlstr &= " ORDER BY dstetr_datum ASC "

        Dim MA_DST As cDienstMA = SQLDienst.getDstMAByDstMaId(dstMaID)
        Dim MA As New VERAG_PROG_ALLGEMEIN.cMitarbeiter(MA_DST.dstma_mitId)


        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Dim dt As DataTable = SQL.loadDgvBySql_Param(sqlstr, "ADMIN")
        'DataGridView.DataSource = dt


        Dim print As New frmPrintLayout
        print.Text = "Monatsauswertung"

        Dim rpt As New rptDienstplanMaMonatsbericht

        rpt.DataSource = dt
        If groupKW Then rpt.GroupHeader1.DataField = "KW"

        rpt.txtMonat.Text = cboMonat.Text & " " & numJahr.Value
        rpt.txtMAName.Text = MA.mit_vname & " " & MA.mit_nname
        rpt.txtZeitraum.Text = vonDat.ToShortDateString & " - " & bisDat.ToShortDateString

        Dim sum = 0

        'rpt.lblSachbearbeiter.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
        ' rpt.lblDat.Text = Now.ToLongDateString


        Dim sumBetragLand As Double = 0


        Dim sumA As Integer = 0
        Dim sumU As Integer = 0
        Dim sumK As Integer = 0
        Dim sumS As Integer = 0
        Dim sumUeStd As Double = 0
        Dim datumKW = Now

        Dim tatStdSum As Double = 0

        Dim cPF As New cProgramFunctions
        Dim FT As New VERAG_PROG_ALLGEMEIN.cFeiertage(datumKW.Year)

        AddHandler rpt.Detail.Format, Sub()

                                          If Not IsDBNull(rpt.Fields.Item("dstetr_datum").Value) AndAlso rpt.Fields.Item("dstetr_datum").Value <> "" AndAlso IsDate(rpt.Fields.Item("dstetr_datum").Value) Then
                                              'MsgBox(rpt.Fields.Item("dstetr_datum").Value)
                                              datumKW = CDate(rpt.Fields.Item("dstetr_datum").Value).ToShortDateString
                                          End If


                                          Select Case datumKW.DayOfWeek
                                              Case 6 : rpt.Detail.BackColor = Color.FromArgb(240, 240, 240)
                                              Case 0 : rpt.Detail.BackColor = Color.FromArgb(220, 220, 220)
                                              Case Else : rpt.Detail.BackColor = Color.White
                                          End Select

                                          rpt.txtTag.ForeColor = Color.Black
                                          If FT.isFeiertag(datumKW, IIf(MA_DST.dstma_land <> "", MA_DST.dstma_land, cDienstSettings.getLand(niederlassung))) Or datumKW.ToString("dd.MM.") = "31.12." Or datumKW.ToString("dd.MM.") = "24.12." Or datumKW.DayOfWeek = 0 Then 'Feiertag oder 1/2 Tag
                                              rpt.txtTag.ForeColor = Color.Red
                                              rpt.Detail.BackColor = Color.FromArgb(220, 220, 220)
                                          End If

                                          Dim datum = Now
                                          If Not IsDBNull(rpt.Fields.Item("dstetr_datum").Value) Then
                                              datum = CDate(rpt.Fields.Item("dstetr_datum").Value).ToShortDateString
                                              rpt.txtTag.Text = datum.ToString("ddd")
                                              rpt.txtDatum.Text = datum.ToShortDateString
                                          Else
                                              rpt.txtTag.Text = ""
                                              rpt.txtDatum.Text = ""
                                          End If
                                          Dim von = isDbnullEmpty(rpt.Fields.Item("dstetr_von").Value, "")
                                          Dim bis = isDbnullEmpty(rpt.Fields.Item("dstetr_bis").Value, "")


                                          rpt.txtArbeitstag.Text = ""
                                          rpt.txtUrl.Text = ""
                                          rpt.txtSonst.Text = ""
                                          rpt.txtKS.Text = ""

                                          Dim benutzerschicht = True
                                          Dim arbeit = False
                                          Dim art = isDbnullEmpty(rpt.Fields.Item("dstetr_art").Value, "")


                                          Select Case art
                                              Case "FREI", "" 'NIX
                                              Case "KS" : rpt.txtKS.Text = "K" : sumK += 1
                                              Case "URL" : rpt.txtUrl.Text = "U" : sumU += 1
                                              Case "DR", "SONST", "BS" : rpt.txtSonst.Text = "S" : sumS += 1
                                              Case "ZA"
                                              Case Else
                                                  If benutzerschicht Then rpt.txtArbeitstag.Text = "A" : arbeit = True : sumA += 1
                                          End Select


                                          rpt.txtVonBis.Text = ""
                                          If arbeit Then
                                              If von <> "" Or bis <> "" Then
                                                  rpt.txtVonBis.Text = von
                                                  rpt.txtVonBis.Text &= " - "
                                                  rpt.txtVonBis.Text &= bis
                                              End If
                                              rpt.txtPause.Text = isDbnullEmpty(rpt.Fields.Item("dstetr_pause").Value, "")
                                          Else
                                              rpt.txtPause.Text = ""
                                              'rpt.txtUeberstd.Text = ""
                                          End If


                                          Dim tatStd As Double = 0
                                          If arbeit Then
                                              tatStd = SQLDienst.getDstStunden(MA_DST.dstma_id, datum, datum, niederlassung, MA_DST.dstma_land)
                                              tatStdSum += tatStd
                                          End If

                                          If tatStd = 0 Then
                                              rpt.txtStd.Text = ""
                                          Else
                                              rpt.txtStd.Text = CDbl(tatStd).ToString("N2")
                                          End If



                                          rpt.txtBemerkung.Text = ""

                                          '  sumUeStd += ueStd
                                          ' sumBar += CDbl(rpt.Fields.Item("Barzahlungsbetrag").Value)
                                          ' sumAnz += CDbl(rpt.Fields.Item("Anzahl").Value)
                                      End Sub

        AddHandler rpt.GroupHeader1.Format, Sub()
                                                sumA = 0
                                                sumU = 0
                                                sumK = 0
                                                sumS = 0
                                                sumUeStd = 0

                                                Dim datum = Now
                                                If Not IsDBNull(rpt.Fields.Item("dstetr_datum").Value) Then
                                                    datum = CDate(rpt.Fields.Item("dstetr_datum").Value).ToShortDateString
                                                End If

                                                If groupKW Then
                                                    rpt.lblKW.Text = "KW " & cPF.getKW(datum)
                                                Else
                                                    rpt.lblKW.Text = vonDat.ToString("MMM ") & vonDat.Year
                                                End If

                                            End Sub

        AddHandler rpt.GroupFooter1.Format, Sub()


                                                '   datumKW = datumKW.AddDays(-1)
                                                rpt.txtSumA.Text = sumA
                                                rpt.txtSumU.Text = sumU
                                                rpt.txtSumK.Text = sumK
                                                rpt.txtSumS.Text = sumS
                                                '     rpt.txtSumWoStunden.Text = CDbl(sumUeStd).ToString("N2")

                                                'Zuerst den montag der aktueellen Woche lt. Datum ermitteln:
                                                MsgBox(cPF.DateToWeek(datumKW))
                                                Dim datumMO As Date = cPF.GetWeekStartDate(cPF.DateToWeek(datumKW).Substring(4, 2), cPF.DateToWeek(datumKW).Substring(0, 4))

                                                'Aktuelle Woche und Jahr anhand des Montages ermitteln:
                                                Dim aktWoche = cPF.DateToWeek(datumMO).Substring(4, 2)
                                                Dim aktJahr = cPF.DateToWeek(datumMO).Substring(0, 4)
                                                Dim SCHICHT = cPF.getSchicht(aktWoche, aktJahr)

                                                Dim STD_LIST = cDienstMitarbAbweichendeWochenstunden.GET_STD_LIST(datumMO)

                                                '    MsgBox(sollStd)

                                                If groupKW Then
                                                    Dim sollStd As Double = cPF.getWochenstunden(STD_LIST, MA_DST.dstma_wochenStunden, datumMO, SCHICHT, MA_DST.dstma_mitId, MA_DST.dstma_muster, MA_DST.dstma_WEStdRegelAZ, MA_DST.dstma_arbvh, niederlassung, MA_DST.dstma_TzFeiertageStd, MA_DST.dstma_4wo)
                                                    Dim tatStd As Double = SQLDienst.getDstStunden(MA_DST.dstma_id, datumMO, datumMO.AddDays(6), niederlassung, MA_DST.dstma_land)

                                                    rpt.txtSumWoStunden.Text = CDbl(tatStd).ToString("N2")
                                                    rpt.txtWoStd.Text = CDbl(sollStd * -1).ToString("N2")
                                                    rpt.txtUeStd.Text = CDbl(tatStd - sollStd).ToString("N2")
                                                Else
                                                    ' Dim sollStd As Double = cPF.getWochenstunden(STD_LIST, MA_DST.dstma_wochenStunden, datumMO, SCHICHT, MA_DST.dstma_mitId, MA_DST.dstma_muster, MA_DST.dstma_WEStdRegelAZ, MA_DST.dstma_arbvh, niederlassung, MA_DST.dstma_TzFeiertageStd)

                                                    rpt.txtSumWoStunden.Text = CDbl(tatStdSum).ToString("N2")
                                                    ' rpt.txtWoStd.Text = CDbl(sollStd * -1).ToString("N2")
                                                    '  rpt.txtUeStd.Text = CDbl(tatStd - sollStd).ToString("N2")
                                                    rpt.txtWoStd.Text = ""
                                                    rpt.txtUeStd.Text = ""

                                                End If

                                            End Sub

        If pdfExport Then
            rpt.Run()
            Dim tmpPath = pdfPfad
            If tmpPath = "" Then
                tmpPath = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath(MA.mit_vname & " " & MA.mit_nname, ".pdf", , False)
            Else
                tmpPath &= "\" & MA.mit_vname & " " & MA.mit_nname & ".pdf"
            End If

            Dim p As New GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport
            p.NeverEmbedFonts = ""
            p.Export(rpt.Document, tmpPath)

        Else
            print.Viewer.LoadDocument(rpt)
            print.Viewer.ViewType = GrapeCity.Viewer.Common.Model.ViewType.Continuous
            print.Show()
        End If

    End Sub



    Private Function CalendarWeek(aktWoche As Object, aktJahr As Object) As Date
        Throw New NotImplementedException()
    End Function

    Function isDbnullEmpty(o As Object, return_value As Object) As String
        If IsDBNull(o) Then : isDbnullEmpty = return_value
        ElseIf IsNothing(o) Then : isDbnullEmpty = return_value
        Else : isDbnullEmpty = o
        End If
    End Function

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If dgvMA.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim pdfPath = ""
        If cbxAuswertungPDF.Checked Then
            Dim fd As New FolderBrowserDialog
            If fd.ShowDialog = DialogResult.OK Then
                pdfPath = fd.SelectedPath
            End If
        End If
        For Each r In dgvMA.SelectedRows
            genMonatsauswertungMA(r.Cells("dstma_id").Value, False, cbxAuswertungPDF.Checked, pdfPath)
        Next

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim table As New DataTable()
        Me.dataAdapterMA.Fill(table)
        VERAG_PROG_ALLGEMEIN.cProgramFunctions.genCSVFromDT(table, True) '
    End Sub
End Class
