Imports System.Drawing.Drawing2D

Public Class frmZollUserStat
    Dim dbSQL As New SQL
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click, Button9.Click
        Me.Cursor = Cursors.WaitCursor
        DataGridView1.Columns.Clear()
        Try
            'Dim sql As String = " SELECT [Personal].[Name], [Personal].Niederlassung,/*COUNT(distinct ([AbfertigungsNr]))*/ sum(Abfertigungsanzahl) as anzahl "
            ' sql &= " FROM [Speditionsbuch] INNER JOIN [Personal] ON [ID]=[Personalnummer] "
            ' sql &= " WHERE 1=1 AND "
            Dim sql As String = " SELECT mit_nname + ' ' + mit_vname as [Name], mit_niederlassung , "
            sql &= " (SELECT TOP 1 isnull([dstma_wochenStunden],0) FROM [SQLGUIDE01.verag.ost.dmn].ADMIN.dbo.tblDienstMitarb where [dstma_mitId]=mit_id ) as dstma_wochenStunden, "
            sql &= " isnull((SELECT sum(Abfertigungsanzahl) as anzahl FROM [Speditionsbuch] WHERE Abfertigungsdatum BETWEEN '" & MyTextBox1.Text & "' AND '" & MyTextBox3.Text & "' "

            If MyListBox1.SelectedItems.Count > 0 Then sql &= " AND Abfertigungsart IN (" & getValues() & ") "

            If sender Is Button7 Then
                sql &= " AND isnull([Personalnummer],0)>0 AND  [Personalnummer] = (case when mit_FMZollPersIDKasseIsnotSpedBuch = 1 then mit_FMZollPersIDSpedBuch else mit_fmzollpersid end )),0) as anzahl "
            Else
                sql &= " AND isnull([PersonalnummerQS],0)>0 AND  [PersonalnummerQS] = mit_id),0) as anzahl "
            End If

            sql &= getAbfArtSplitter()

            sql &= " FROM " & VERAG_PROG_ALLGEMEIN.cAllgemein.AVISO_SERVER & ".ADMIN.dbo.tblMitarbeiter " 'left join " & VERAG_PROG_ALLGEMEIN.cAllgemein.AVISO_SERVER & ".ADMIN.dbo.tblDienstMitarb ON [dstma_mitId]=mit_id"
            sql &= " WHERE 1=1  "
            sql &= " and mit_abteilung IN ('ZOLL','QS') and mit_pseudoUser=0"

            Select Case cbxgekuendigt.CheckState
                Case CheckState.Unchecked : sql &= " and mit_gekuendigt = 0 "
                Case CheckState.Checked : sql &= " and mit_gekuendigt = 1 "
            End Select
            ' sql &= " and mit_gekuendigt = 0 "
            Dim filter As String = ""
            '    If MyTextBox1.Text <> "" And MyTextBox3.Text <> "" Then
            'filter += " Abfertigungsdatum BETWEEN '" & MyTextBox1.Text & "' AND '" & MyTextBox3.Text & "' "
            '   End If
            '    filter += " AND Abfertigungsart IN (" & getValues() & ") "

            'If MyComboBox1.Text <> "" Then filter += " AND FilialenNr IN (" & DirectCast(MyComboBox1.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value & ") "
            If MyComboBox2.Text <> "" Then filter += " AND mit_niederlassung IN ('" & DirectCast(MyComboBox2.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value & "') "
            If MyComboBox1.Text <> "" Then
                filter += " AND (mit_firma IN ('" & DirectCast(MyComboBox1.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value & "')  OR (mit_firma='ALL' AND mit_firmaHaupt  IN ('" & DirectCast(MyComboBox1.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value & "') )) "
            End If


            ' filter += " AND [Personalnummer] is not null "

            Dim grouporder As String = " ORDER BY  anzahl desc "
            sql += filter + grouporder
            MsgBox(sql)
            Dim data As DataTable = dbSQL.loadDgvBySql(sql, "FMZOLL", 300)
            DataGridView1.DataSource = data
            initDGV()

        Catch ex As Exception
            MsgBox("Fehler beim Laden der Daten. " & vbNewLine & vbNewLine & ex.Message & ex.StackTrace)
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Sub initDGV()
        If DataGridView1.Rows.Count = 0 Then Exit Sub
        With DataGridView1
            .Columns("Name").HeaderText = "Mitarbeiter"
            .Columns("Name").Width = 200
            ' .Columns("Niederlassung").Visible = False
            .Columns("anzahl").Visible = False
            .Columns("mit_niederlassung").HeaderText = "Niederlassung"
            .Columns("dstma_wochenStunden").HeaderText = "Wochenstunden"

            Dim c As New DataGridViewImageColumn()
            c.HeaderText = "Anzahl"
            c.Width = 200
            c.Name = "anzahlShowBar"
            .Columns.Insert(2, c)

            '.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader
            For Each col In .Columns
                Try : col.MinimumWidth = 50
                Catch ex As Exception : End Try
            Next
        End With
    End Sub

    Function getAbfArtSplitter() As String
        getAbfArtSplitter = ""
        If cbxNachAbfertigungsartSplitten.Checked Then
            For Each i In MyListBox1.SelectedItems
                Dim s = ", isnull((SELECT sum(Abfertigungsanzahl) as anzahl FROM [Speditionsbuch] WHERE Abfertigungsdatum BETWEEN '" & MyTextBox1.Text & "' AND '" & MyTextBox3.Text & "' "
                s &= " AND Abfertigungsart IN (" & DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value & ") "
                s &= " AND [Personalnummer] = (case when mit_FMZollPersIDKasseIsnotSpedBuch = 1 then mit_FMZollPersIDSpedBuch else mit_fmzollpersid end )"
                s &= "),0) as [davon " & DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Text & "] "
                getAbfArtSplitter += s
            Next
        End If
    End Function

    Function getLabel(t As String) As Label
        Dim l As New Label
        l.Text = t
        Return l
    End Function

  
    Private Sub frmZollUserStat_Load(sender As Object, e As EventArgs) Handles Me.Load

        MyListBox1.fillWithSQL(" SELECT [Abfertigungsart],[Abfertigungsbezeichnung] FROM [Abfertigungsarten] ", , "FMZOLL")
        '     MyComboBox1.fillWithSQL("SELECT Filialen.FilialenNr, Filialen.Grenzstelle FROM Filialen WHERE (((Filialen.Fil_Status)='A')) ORDER BY Filialen.FilialenNr ", , "FMZOLL")
        MyComboBox1.Items.Clear()
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG", "VERAG"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED", "UNISPED"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("FRONTOFFICE", "FRONTOFFICE"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AMBAR", "AMBAR"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATILLA", "ATILLA"))
        MyComboBox1.changeItem("VERAG")
        MyComboBox2.fillWithSQL("SELECT [nl_kuerzel] ,nl_kuerzel FROM [ADMIN].[dbo].[tblNiederlassungen]", False, "ADMIN")
        'MyComboBox2.fillWithSQL("SELECT Niederlassung FROM [Personal] WHERE Niederlassung is not null  GROUP BY Niederlassung", False, "FMZOLL")
        Button6.PerformClick()
    End Sub
    Function getValues() As String
        Dim s As String = ""

        For Each i In MyListBox1.SelectedItems
            s += DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value & ", "
        Next
        If s.EndsWith(", ") Then
            s = s.Substring(0, s.Length - 2)
        End If
        Return s
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i = 0 To MyListBox1.Items.Count - 1
            MyListBox1.SetSelected(i, True)
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For i = 0 To MyListBox1.Items.Count - 1
            MyListBox1.SetSelected(i, False)
        Next
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        MyTextBox1.Text = "01.01." & Now.Year
        MyTextBox3.Text = "12.12." & Now.Year
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        MyTextBox1.Text = "01.01." & (Now.Year - 1)
        MyTextBox3.Text = "12.12." & (Now.Year - 1)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim d As Date = New Date(Now.Year, Now.Month, 1)
        MyTextBox1.Text = d.ToShortDateString

        d = d.AddMonths(1) : d = d.AddDays(-1)
        MyTextBox3.Text = d.ToShortDateString
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim d As Date = New Date(Now.Year, Now.Month, 1)
        d = d.AddMonths(-1)
        MyTextBox1.Text = d.ToShortDateString

        d = d.AddMonths(1) : d = d.AddDays(-1)
        MyTextBox3.Text = d.ToShortDateString
    End Sub

    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If DataGridView1.Rows.Count = 0 Then Exit Sub

        Dim topvalue As Integer = DataGridView1.Rows(0).Cells("Anzahl").Value
        If topvalue = 0 Then Exit Sub
        If DataGridView1.Columns("anzahlShowBar") Is Nothing Then Exit Sub
        If e.ColumnIndex = DataGridView1.Columns("anzahlShowBar").Index Then ' Or DataGridView1.Columns(e.ColumnIndex).Name.StartsWith("davon") Then

            If IsNumeric(DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value) Then
                Dim veragBlueBrush As New SolidBrush(Color.FromArgb(0, 54, 128))
                Dim p As DataGridViewImageCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
                '  e.Graphics.FillRectangle(Brushes.WhiteSmoke, New Rectangle(p.ContentBounds.Location, New Size(New Point(5, 5))))
                'Dim img As Bitmap = New Bitmap("C:\tmp.jpg" )
                e.FormattingApplied = True

                Dim Width_bar As Integer = p.Size.Width / topvalue * DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value

                Dim image As New Bitmap(p.Size.Width, p.Size.Height)
                Dim gr As Graphics = Graphics.FromImage(image)
                gr.FillRectangle(veragBlueBrush, New Rectangle(0, 0, Width_bar, p.Size.Height))
                '   MsgBox(Width_bar)


                ' Dim font As New Font("Arial", 50, False, False)
                ' Dim brush As New SolidBrush(Color.Yellow)
                ' Dim pen As New Pen(Color.Red, 1)

                '  gr.DrawString(DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value, font, pen, brush, 20, 20)

                gr.SmoothingMode = SmoothingMode.AntiAlias
                Using gp As New GraphicsPath, f As New Font("Microsoft Sans Serif", 12, FontStyle.Regular), pen As New Pen(veragBlueBrush, 3)
                    gp.AddString(CStr(DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value), f.FontFamily, f.Style, f.Size + 2, New Point(0, 2), StringFormat.GenericDefault)
                    gr.DrawPath(pen, gp)
                    gr.FillPath(Brushes.White, gp)
                End Using

                gr.Dispose()



                p.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
                e.Value = New Bitmap(image)
                image.Dispose()


                'Exit Sub
            End If
        End If

        If False Then

            If DataGridView1.Columns(e.ColumnIndex).Name.StartsWith("davon") Then

                If IsNumeric(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then
                    Dim veragBlueBrush As New SolidBrush(Color.FromArgb(0, 54, 128))
                    Dim p As DataGridViewTextBoxCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
                    '  e.Graphics.FillRectangle(Brushes.WhiteSmoke, New Rectangle(p.ContentBounds.Location, New Size(New Point(5, 5))))
                    'Dim img As Bitmap = New Bitmap("C:\tmp.jpg" )
                    e.FormattingApplied = True


                    Dim Width_bar As Integer = p.Size.Width / topvalue * DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

                    Dim image As New Bitmap(p.Size.Width, p.Size.Height)
                    Dim gr As Graphics = Graphics.FromImage(image)
                    gr.FillRectangle(veragBlueBrush, New Rectangle(0, 0, Width_bar, p.Size.Height))
                    '   MsgBox(Width_bar)


                    ' Dim font As New Font("Arial", 50, False, False)
                    ' Dim brush As New SolidBrush(Color.Yellow)
                    ' Dim pen As New Pen(Color.Red, 1)

                    '  gr.DrawString(DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value, font, pen, brush, 20, 20)

                    gr.SmoothingMode = SmoothingMode.AntiAlias
                    Using gp As New GraphicsPath, f As New Font("Microsoft Sans Serif", 12, FontStyle.Regular), pen As New Pen(veragBlueBrush, 3)
                        gp.AddString(CStr(DataGridView1.Rows(e.RowIndex).Cells("Anzahl").Value), f.FontFamily, f.Style, f.Size + 2, New Point(0, 2), StringFormat.GenericDefault)
                        gr.DrawPath(pen, gp)
                        gr.FillPath(Brushes.White, gp)
                    End Using

                    gr.Dispose()



                    p.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
                    p.Value = New Bitmap(image)
                    image.Dispose()


                    'Exit Sub
                End If
            End If
        End If

    End Sub

    Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
        ' MsgBox("p")

    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs)

        'DataGridView1_Paint(DataGridView1, New PaintEventArgs(DataGridView1.CreateGraphics, DataGridView1))
    End Sub

    Private Sub Button8_Click_1(sender As Object, e As EventArgs) Handles Button8.Click

        DataGridView1.Columns("anzahlShowBar").Visible = False
        cProgramFunctions.genExcelFromDGV_NEW(DataGridView1, True)
        DataGridView1.Columns("anzahlShowBar").Visible = True
    End Sub
End Class