Imports System.Globalization

Public Class usrcntlDienstplanStunden

    Dim SQLDienst As New cDienstplan
    '   Dim ListMA As New List(Of cMAShort)
    ' Dim aktDate As Date = Now
    Dim aktWoche As Integer = 0
    Dim aktJahr As Integer = 1900
    Dim niederlassung As String = ""
    Dim cPF As New cProgramFunctions
    Public SCHICHT = "ROT"
    Public loaded As Integer = False

    Public Event NAME_CHANGED(maId As Integer)


    Sub New(aktWoche, aktJahr, niederlassung, SCHICHT)

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

        Me.aktWoche = aktWoche
        Me.aktJahr = aktJahr
        Me.niederlassung = niederlassung
        Me.SCHICHT = SCHICHT
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        'initStdMa()
    End Sub
    Public Sub initStdMa()
        loaded = False
        Try
            Dim datum_start As Date = CalendarWeek(aktWoche, aktJahr)
            Dim datum_end As Date = datum_start.AddDays(6)
            With dgvStundenMa
                .Columns.Clear()
                .DataSource = SQLDienst.loadDGV("SELECT dstma_abteilung AS ABT, dstma_kuerzel AS NAME,dstma_wochenStunden,dstma_arbvh, dstma_id,dstma_farbe,dstma_mitId,dstma_ueberstdCounter FROM tblDienstMitarb INNER JOIN tblMitarbeiter ON mit_id=dstma_mitId  WHERE  dstma_inaktiv=0 AND (mit_gekuendigt =0 OR mit_kuendigungsdatum>='" & datum_start.ToShortDateString & "')  AND dstma_niederlassung='" & niederlassung & "' ORDER BY ABT DESC, dstma_arbvh DESC, NAME")

                Try

                    Dim d2 As New DataGridViewTextBoxColumn
                    d2.Name = "stundenBerechnet"
                    .Columns.Insert(2, d2)

                    Dim d As New DataGridViewTextBoxColumn
                    d.HeaderText = "Stunden Woche"
                    d.Name = "stunden"
                    .Columns.Insert(3, d)



                    '    Dim d2 As New DataGridViewTextBoxColumn
                    ' d2.HeaderText = "Std/Mon."
                    '  .Columns.Add(d2)

                    .RowHeadersVisible = False
                    .Columns("dstma_farbe").Visible = False
                    '.Columns("dstma_kuerzel").Visible = False
                    .Columns("dstma_arbvh").Visible = False
                    .Columns("dstma_id").Visible = False
                    .Columns("dstma_wochenStunden").Visible = False
                    .Columns("dstma_mitId").Visible = False

                    .Columns("dstma_ueberstdCounter").HeaderText = "Überstd.(letzte KW)"
                    .Columns("dstma_ueberstdCounter").Width = 60


                    .Columns("stundenBerechnet").HeaderText = "Std."
                    .Columns("stundenBerechnet").Width = 40

                    .Columns("ABT").Width = 50
                    If niederlassung <> "SUB" Then .Columns("ABT").Visible = False
                    .Columns("NAME").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    '.Columns(2).Width = 50
                    .Columns("stunden").Width = 70
                    '  .Columns(9).Width = 80
                Catch ex As Exception
                    MsgBox(ex.Message)
                    MsgBox("initStdMa-Error: " & ex.StackTrace)
                End Try

                Dim STD_LIST = cDienstMitarbAbweichendeWochenstunden.GET_STD_LIST(CalendarWeek(aktWoche, aktJahr))
                For Each r As DataGridViewRow In .Rows
                    Try : r.DefaultCellStyle.ForeColor = ColorTranslator.FromHtml(r.Cells("dstma_farbe").Value) : Catch : End Try

                    Dim datum_montag As Date = CalendarWeek(aktWoche, aktJahr)
                    Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
                    Dim datum As Date = datum_montag

                    Dim ma As cDienstMA = SQLDienst.getDstMA(r.Cells("dstma_id").Value)
                    Dim Wochenstunden As Double = 0

                    If IsNumeric(ma.dstma_wochenStunden) Then Wochenstunden = ma.dstma_wochenStunden

                    'MsgBox(ma.dstma_wochenStunden)
                    'hier wird berechnet, wie viele Stunden tatsächlich zu arbeiten sind (abzgl. Feiertage)


                    Wochenstunden = cPF.getWochenstunden(STD_LIST, Wochenstunden, datum, SCHICHT, r.Cells("dstma_id").Value, ma.dstma_muster, ma.dstma_WEStdRegelAZ, ma.dstma_arbvh, niederlassung, ma.dstma_TzFeiertageStd, ma.dstma_4wo)

                    'Hier wird berechnet wie viele Std tastächlich gearbeitet wurden:
                    Dim tatStd As Double = SQLDienst.getDstStunden(r.Cells("dstma_id").Value, datum_montag, datum_montag.AddDays(6), niederlassung, ma.dstma_land)


                    r.Cells("stundenBerechnet").Value = (Wochenstunden)
                    r.Cells("stunden").Value = (tatStd) & " (" & tatStd - Wochenstunden & ")"
                    Dim arb As New cArbeitstage
                    r.Cells("dstma_ueberstdCounter").Value = SQLDienst.getDienstplanUeberSdtMaSUMMEBisKW(r.Cells("dstma_id").Value, aktWoche, aktJahr, niederlassung) '

                Next
            End With
            loaded = True
        Catch ex As Exception
            MsgBox("initStdMa-Error: " & ex.Message)
        End Try
        initUestd()
    End Sub




    Public Function CalendarWeek(ByVal nWeek As Integer, _
 ByVal nYear As Integer) As Date

        ' Wochentag des 4. Januar des Jahres ermitteln
        Dim dStart As New Date(nYear, 1, 4)
        Dim nDay As Integer = (dStart.DayOfWeek + 6) Mod 7 + 1

        ' Beginn der 1. KW des Jahres
        Dim dFirst As Date = dStart.AddDays(1 - nDay)

        ' Gesuchte KW ermitteln
        Return dFirst.AddDays((nWeek - 1) * 7)
    End Function

    Public Sub markrow(maId)
        loaded = False
        dgvStundenMa.ClearSelection()
        For Each r In dgvStundenMa.Rows
            If r.Cells("dstma_id").value = maId Then
                r.DefaultCellStyle.Font = New Font(dgvStundenMa.Font, FontStyle.Bold)
                r.Selected = True
            End If
        Next
        loaded = True
    End Sub
    Public Sub clearRowBg()
        loaded = False
        For Each r In dgvStundenMa.Rows
            r.DefaultCellStyle.Font = New Font(dgvStundenMa.Font, FontStyle.Regular)
        Next
        loaded = True
    End Sub

    Private Sub btnCounterUest_Click(sender As Object, e As EventArgs) Handles btnCounterUest.Click
        If dgvStundenMa.SelectedRows.Count = 0 Then MsgBox("Mitarbeiter auswählen!") : Exit Sub
        If Not IsNumeric(txtUeberstd.Text) Then MsgBox("Falsches Format!") : Exit Sub
        Dim maId = dgvStundenMa.SelectedRows(0).Cells("dstma_id").Value
        SQLDienst.insertupdateDienstplanUeberSdtMaAuszahlung(maId, aktWoche, aktJahr, CDbl(IIf(IsNumeric(txtUeberstd.Text), txtUeberstd.Text, 0)), niederlassung)
        'MsgBox(CDbl(IIf(IsNumeric(lblUeberStd.Text), lblUeberStd.Text, 0)))
        '  SQLDienst.updateDienstMaStd(dgvStundenMa.SelectedRows(0).Cells("dstma_id").Value, CDbl(IIf(IsNumeric(lblUeberStd.Text), lblUeberStd.Text, 0)) + CDbl(txtUeberstd.Text))


        initStdMa()
        markrow(maId)
    End Sub

   

    Private Sub dgvStundenMa_SelectionChanged(sender As Object, e As EventArgs) Handles dgvStundenMa.SelectionChanged
        If dgvStundenMa.SelectedRows.Count = 0 Then Exit Sub
        initUestd()
        If loaded Then RaiseEvent NAME_CHANGED(dgvStundenMa.SelectedRows(0).Cells("dstma_id").Value)
    End Sub

    Sub initUestd()
        If dgvStundenMa.SelectedRows.Count = 0 Then Exit Sub
        lblUeberStd.Text = dgvStundenMa.SelectedRows(0).Cells("dstma_ueberstdCounter").Value()
        txtUeberstd.Text = "0"
        txtUeberstd.Text = SQLDienst.getDienstplanAuszSdtMaSUMM_KW(dgvStundenMa.SelectedRows(0).Cells("dstma_id").Value, aktWoche, aktJahr)
        lblName.Text = dgvStundenMa.SelectedRows(0).Cells("NAME").Value()

    End Sub
End Class
