Public Class 業績結單控制表
    ReadOnly ds, ds1, ds2, ds3, ds4, ds5, ds6, ds7, ds8 As New DataSet
    Private Sub Set_清單1()
        彙總1_dgv.DataSource = Nothing : ds1.Clear()
        彙總1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        彙總1_dgv.ColumnHeadersHeight = 25
        彙總1_dgv.AllowUserToAddRows = False
        彙總1_dgv.RowTemplate.Height = 25
        If 已完單_bt.Enabled = False Then : SQL_業績完單2()
        ElseIf 未完單_bt.Enabled = False Then : SQL_業績完單1()
        ElseIf 不可完單_bt.Enabled = False Then : SQL_業績完單3() : End If

        da.Fill(ds1) : 彙總1_dgv.DataSource = ds1.Tables(0) : conn.Close()
        彙總1_dgv.Columns(0).FillWeight = 80 : 彙總1_dgv.Columns(1).FillWeight = 80 : 彙總1_dgv.Columns(2).FillWeight = 80
        彙總1_dgv.Columns(3).FillWeight = 80 : 彙總1_dgv.Columns(4).FillWeight = 80 : 彙總1_dgv.Columns(5).FillWeight = 80
        彙總1_dgv.Columns(6).FillWeight = 80 : 彙總1_dgv.Columns(7).FillWeight = 80 : 彙總1_dgv.Columns(9).FillWeight = 200
        彙總1_dgv.Columns(10).FillWeight = 80 : 彙總1_dgv.Columns(11).FillWeight = 80
        For I As Integer = 1 To 5
            彙總1_dgv.Columns(I).DefaultCellStyle.Format = "#,##0" : 彙總1_dgv.Columns(I).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Next
        For I As Integer = 0 To 彙總1_dgv.Rows.Count - 1
            If 彙總1_dgv.Rows(I).Cells(1).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(1).Value = 0 : End If
            If 彙總1_dgv.Rows(I).Cells(2).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(2).Value = 0 : End If
            If 彙總1_dgv.Rows(I).Cells(3).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(3).Value = 0 : End If
            If 彙總1_dgv.Rows(I).Cells(4).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(4).Value = 0 : End If
            If 彙總1_dgv.Rows(I).Cells(5).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(5).Value = 0 : End If
        Next
    End Sub
    Private Sub Set_獎金基礎下拉()
        SQL_獎金基礎下拉()
        While (dr.Read()) : 獎金基礎_ch.Items.Add(dr("獎金計算")) : End While : conn.Close()
    End Sub
    Private Sub 業績單據完單設定_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        未完單_bt.Enabled = False
        Set_獎金基礎下拉() : Set_清單1()
    End Sub
    Dim 已超出 As Boolean
    Private Sub 彙總1_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 彙總1_dgv.MouseUp
        'If 彙總1_dgv.SelectedCells.Count = 1 Then : MsgBox(彙總1_dgv.SelectedCells(0).ColumnIndex & "  " & 彙總1_dgv.SelectedCells(0).RowIndex) : End If
        If 彙總1_dgv.SelectedCells.Count = 1 Then : 已超出 = False : End If
        If 彙總1_dgv.SelectedCells.Count > 0 Then
            If 彙總1_dgv.SelectedCells(0).ColumnIndex >= 1 And 彙總1_dgv.SelectedCells(0).ColumnIndex <= 5 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 彙總1_dgv.SelectedCells.Count - 1
                        x += Val(彙總1_dgv.Rows(彙總1_dgv.SelectedCells.Item(i).RowIndex).Cells(彙總1_dgv.SelectedCells.Item(i).ColumnIndex).Value)
                    Next i : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(x, "#,##0")
                Else : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0") : End If
            Else : 已超出 = True : YD_ERP_SYS.合計_TSSL.Text = "合計 : " & Strings.Format(0, "#,##0")
            End If
        End If
    End Sub
    Private Sub 彙總1_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 彙總1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            業務單號_tb.Text = 彙總1_dgv("單號", e.RowIndex).Value.ToString
            If 帶入交貨日_ch.Checked = True Then
                開始日期_dtp.Value = 彙總1_dgv("交貨日期", e.RowIndex).Value.ToString
            Else
                If 彙總1_dgv("轉業績日期", e.RowIndex).Value.ToString <> "" Then
                    開始日期_dtp.Value = 彙總1_dgv("轉業績日期", e.RowIndex).Value.ToString
                End If
            End If
            If Strings.Left(業務單號_tb.Text, 1) = 預設1_tb.Text Then : 獎金基礎_ch.Text = 基礎1_tb.Text
            ElseIf Strings.Left(業務單號_tb.Text, 1) = 預設2_tb.Text Then : 獎金基礎_ch.Text = 基礎2_tb.Text
            ElseIf Strings.Left(業務單號_tb.Text, 1) = 預設3_tb.Text Then : 獎金基礎_ch.Text = 基礎3_tb.Text
            Else : 獎金基礎_ch.Text = 基礎4_tb.Text : End If
        End If
    End Sub
    Private Sub 彙總1_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 彙總1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 彙總1_dgv.Rows.Count - 1 Then : Exit Sub : Else
            If 已完單_bt.Enabled = False Then
                If Strings.Left(彙總1_dgv("轉業績日期", e.RowIndex).Value.ToString, 7) <> Strings.Left(彙總1_dgv("轉業績日期", e.RowIndex + 1).Value.ToString, 7) Then
                    Dim startX As Integer = IIf(彙總1_dgv.RowHeadersVisible, 彙總1_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                    Dim endX As Integer = startX + 彙總1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 彙總1_dgv.HorizontalScrollingOffset
                    e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
                End If
            Else
                If Strings.Left(彙總1_dgv("交貨日期", e.RowIndex).Value.ToString, 7) <> Strings.Left(彙總1_dgv("交貨日期", e.RowIndex + 1).Value.ToString, 7) Then
                    Dim startX As Integer = IIf(彙總1_dgv.RowHeadersVisible, 彙總1_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                    Dim endX As Integer = startX + 彙總1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 彙總1_dgv.HorizontalScrollingOffset
                    e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
                End If
            End If
        End If
    End Sub
    Private Sub 查詢單號_tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 查詢單號_tb.KeyPress
        If e.KeyChar = Chr(13) Then : 查詢_bt.PerformClick() : End If
    End Sub
    Private Sub 不可完單_bt_Click(sender As Object, e As EventArgs) Handles 不可完單_bt.Click
        已完單_bt.Enabled = True : 未完單_bt.Enabled = True : 不可完單_bt.Enabled = False : 存檔_bt.Enabled = False : 開始日期_dtp.Value = Today() : Set_清單1()
    End Sub
    Private Sub 已完單_bt_Click(sender As Object, e As EventArgs) Handles 已完單_bt.Click
        已完單_bt.Enabled = False : 未完單_bt.Enabled = True : 不可完單_bt.Enabled = True : 存檔_bt.Enabled = True : 開始日期_dtp.Value = Today() : Set_清單1()
    End Sub
    Private Sub 未完單_bt_Click(sender As Object, e As EventArgs) Handles 未完單_bt.Click
        已完單_bt.Enabled = True : 未完單_bt.Enabled = False : 不可完單_bt.Enabled = True : 存檔_bt.Enabled = True : 開始日期_dtp.Value = Today() : Set_清單1()
    End Sub
    Private Sub 查詢_bt_Click(sender As Object, e As EventArgs) Handles 查詢_bt.Click
        Set_清單1()
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        SQL_業績完單_修正() : Set_清單1()
    End Sub
    Private Sub 完單取消_bt_Click(sender As Object, e As EventArgs) Handles 完單取消_bt.Click
        SQL_業績完單_修正退回() : Set_清單1()
    End Sub
End Class