Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Public Class 業績獎金彙總表
    ReadOnly ds, ds1, ds2, ds3, ds4, ds5, ds6, ds7, ds8 As New DataSet
    Dim xlApp As Application : Dim xlBook As Workbook : Dim xlSheet As Worksheet
    Dim N1 As Integer
    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

        SQL_業績彙總1()
        da.Fill(ds1) : 彙總1_dgv.DataSource = ds1.Tables(0) : conn.Close()
        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
            If 彙總1_dgv.Rows(I).Cells(6).Value.ToString = "" Then : 彙總1_dgv.Rows(I).Cells(6).Value = 0 : End If
        Next
    End Sub
    Private Sub Set_清單2()
        衍生運費_dgv.DataSource = Nothing : ds2.Clear()
        衍生運費_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        衍生運費_dgv.ColumnHeadersHeight = 25
        衍生運費_dgv.AllowUserToAddRows = False
        衍生運費_dgv.RowTemplate.Height = 25

        SQL_業績彙總2()
        da.Fill(ds2) : 衍生運費_dgv.DataSource = ds2.Tables(0) : conn.Close()
        衍生運費_dgv.Columns(1).DefaultCellStyle.Format = "#,##0"
        衍生運費_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub Set_清單3()
        其他_dgv.DataSource = Nothing : ds.Clear()
        其他_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        其他_dgv.ColumnHeadersHeight = 25
        其他_dgv.AllowUserToAddRows = False
        其他_dgv.RowTemplate.Height = 25

        SQL_業績彙總3()
        da.Fill(ds) : 其他_dgv.DataSource = ds.Tables(0) : conn.Close()
        其他_dgv.Columns(1).DefaultCellStyle.Format = "#,##0"
        其他_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub Set_清單4()
        外車車資_dgv.DataSource = Nothing : ds3.Clear()
        外車車資_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        外車車資_dgv.ColumnHeadersHeight = 25
        外車車資_dgv.AllowUserToAddRows = False
        外車車資_dgv.RowTemplate.Height = 25

        SQL_業績彙總4()
        da.Fill(ds3) : 外車車資_dgv.DataSource = ds3.Tables(0) : conn.Close()
        外車車資_dgv.Columns(1).DefaultCellStyle.Format = "#,##0" : 外車車資_dgv.Columns(2).DefaultCellStyle.Format = "#,##0"
        外車車資_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        外車車資_dgv.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub Set_清單5()
        料號一_dgv.DataSource = Nothing : ds4.Clear()
        料號一_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        料號一_dgv.ColumnHeadersHeight = 25
        料號一_dgv.AllowUserToAddRows = False
        料號一_dgv.RowTemplate.Height = 25

        SQL_業績彙總5()
        da.Fill(ds4) : 料號一_dgv.DataSource = ds4.Tables(0) : conn.Close()
        料號一_dgv.Columns(1).DefaultCellStyle.Format = "#,##0"
        料號一_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub Set_清單6()
        備註運費_dgv.DataSource = Nothing : ds5.Clear()
        備註運費_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        備註運費_dgv.ColumnHeadersHeight = 25
        備註運費_dgv.AllowUserToAddRows = False
        備註運費_dgv.RowTemplate.Height = 25

        SQL_業績彙總6()
        da.Fill(ds5) : 備註運費_dgv.DataSource = ds5.Tables(0) : conn.Close()
        備註運費_dgv.Columns(1).DefaultCellStyle.Format = "#,##0"
        備註運費_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub Set_彙總明細()
        獎金明細表_dgv.DataSource = Nothing : ds7.Clear()
        獎金明細表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        獎金明細表_dgv.ColumnHeadersHeight = 25
        獎金明細表_dgv.AllowUserToAddRows = False
        獎金明細表_dgv.RowTemplate.Height = 25

        SQL_業績主表2()
        da.Fill(ds7) : 獎金明細表_dgv.DataSource = ds7.Tables(0) : conn.Close()
        獎金明細表_dgv.Columns(11).Visible = False : 獎金明細表_dgv.Columns(14).FillWeight = 300

        獎金明細表_dgv.Columns(2).DefaultCellStyle.Format = "#,##0" : 獎金明細表_dgv.Columns(3).DefaultCellStyle.Format = "#,##0"
        獎金明細表_dgv.Columns(4).DefaultCellStyle.Format = "#,##0" : 獎金明細表_dgv.Columns(5).DefaultCellStyle.Format = "#,##0"
        獎金明細表_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 獎金明細表_dgv.Columns(7).DefaultCellStyle.Format = "#,##0"
        獎金明細表_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 獎金明細表_dgv.Columns(9).DefaultCellStyle.Format = "#,##0"
        獎金明細表_dgv.Columns(10).DefaultCellStyle.Format = "#,##0"
        獎金明細表_dgv.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金明細表_dgv.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        For I As Integer = 0 To 獎金明細表_dgv.Rows.Count - 1
            獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value = "0" : 獎金明細表_dgv.Rows(I).Cells("成本").Value = "0" : 獎金明細表_dgv.Rows(I).Cells("運費").Value = "0"
            獎金明細表_dgv.Rows(I).Cells("稅額(發票)").Value = "0" : 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value = "0" : 獎金明細表_dgv.Rows(I).Cells("手續費(刷卡)").Value = "0"
            獎金明細表_dgv.Rows(I).Cells("稅額(2.5%)").Value = "0"
            '--------------銷貨總額---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 彙總1_dgv.Rows.Count - 1
                If 彙總1_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value = 獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value + 彙總1_dgv.Rows(II).Cells("銷貨總額").Value
                End If
            Next
            For II As Integer = 0 To 外車車資_dgv.Rows.Count - 1
                If 外車車資_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    '獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value = 獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value + 外車車資_dgv.Rows(II).Cells("外車_收").Value
                    獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value = 獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value
                End If
            Next
            '--------------成本---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 彙總1_dgv.Rows.Count - 1
                If 彙總1_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("成本").Value = 獎金明細表_dgv.Rows(I).Cells("成本").Value + 彙總1_dgv.Rows(II).Cells("成本").Value
                End If
            Next
            '--------------運費---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 彙總1_dgv.Rows.Count - 1
                If 彙總1_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("運費").Value = 獎金明細表_dgv.Rows(I).Cells("運費").Value + 彙總1_dgv.Rows(II).Cells("運費").Value
                End If
            Next
            For II As Integer = 0 To 衍生運費_dgv.Rows.Count - 1
                If 衍生運費_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("運費").Value = 獎金明細表_dgv.Rows(I).Cells("運費").Value + 衍生運費_dgv.Rows(II).Cells("衍生運費").Value
                End If
            Next
            'For II As Integer = 0 To 外車車資_dgv.Rows.Count - 1
            '    If 外車車資_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
            '        獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value = 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value + 外車車資_dgv.Rows(II).Cells("外車_支").Value
            '    End If
            'Next
            For II As Integer = 0 To 備註運費_dgv.Rows.Count - 1
                If 備註運費_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value = 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value + 備註運費_dgv.Rows(II).Cells("備註車資").Value
                End If
            Next
            '--------------稅額(發票)---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 彙總1_dgv.Rows.Count - 1
                If 彙總1_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("稅額(發票)").Value = 獎金明細表_dgv.Rows(I).Cells("稅額(發票)").Value + 彙總1_dgv.Rows(II).Cells("稅金").Value
                    獎金明細表_dgv.Rows(I).Cells("稅額(2.5%)").Value = 獎金明細表_dgv.Rows(I).Cells("稅額(2.5%)").Value + 彙總1_dgv.Rows(II).Cells("稅金2.5%").Value
                End If
            Next
            '--------------其他(傭金)---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 料號一_dgv.Rows.Count - 1
                If 料號一_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value = 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value + 料號一_dgv.Rows(II).Cells("料號1_收支").Value
                End If
            Next
            For II As Integer = 0 To 其他_dgv.Rows.Count - 1
                If 其他_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value = 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value + 其他_dgv.Rows(II).Cells("其他支出").Value
                End If
            Next
            '--------------手續費(刷卡)---------------------------------------------------------------------------------------------------------------------------------------------
            For II As Integer = 0 To 彙總1_dgv.Rows.Count - 1
                If 彙總1_dgv.Rows(II).Cells("單號").Value.ToString = 獎金明細表_dgv.Rows(I).Cells("編號").Value.ToString Then
                    獎金明細表_dgv.Rows(I).Cells("手續費(刷卡)").Value = 獎金明細表_dgv.Rows(I).Cells("手續費(刷卡)").Value + 彙總1_dgv.Rows(II).Cells("刷卡費用").Value
                End If
            Next
        Next
        For I As Integer = 0 To 獎金明細表_dgv.Rows.Count - 1
            獎金明細表_dgv.Rows(I).Cells("毛利").Value = "0" : 獎金明細表_dgv.Rows(I).Cells("獎金").Value = "0"
            獎金明細表_dgv.Rows(I).Cells("毛利").Value = 獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value - 獎金明細表_dgv.Rows(I).Cells("成本").Value -
                                                             獎金明細表_dgv.Rows(I).Cells("運費").Value - 獎金明細表_dgv.Rows(I).Cells("稅額(發票)").Value -
                                                             獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value - 獎金明細表_dgv.Rows(I).Cells("手續費(刷卡)").Value -
                                                             獎金明細表_dgv.Rows(I).Cells("管銷費用").Value + 獎金明細表_dgv.Rows(I).Cells("稅額(2.5%)").Value
        Next
    End Sub
    Private Sub Set_彙總過度表()
        過度表_dgv.DataSource = Nothing : ds8.Clear()
        過度表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        過度表_dgv.ColumnHeadersHeight = 25 : 過度表_dgv.AllowUserToAddRows = False : 過度表_dgv.RowTemplate.Height = 25

        SQL_業績主表3()
        da.Fill(ds8) : 過度表_dgv.DataSource = ds8.Tables(0) : conn.Close()

        For I As Integer = 11 To 30 : 過度表_dgv.Columns(I).Visible = False : Next

        過度表_dgv.Columns(2).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(3).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(4).DefaultCellStyle.Format = "#,##0"
        過度表_dgv.Columns(5).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(7).DefaultCellStyle.Format = "#,##0"
        過度表_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(9).DefaultCellStyle.Format = "#,##0" : 過度表_dgv.Columns(10).DefaultCellStyle.Format = "#,##0"
        過度表_dgv.Columns(11).DefaultCellStyle.Format = "#,##0"
        過度表_dgv.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        過度表_dgv.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 過度表_dgv.Columns(11).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        For I As Integer = 0 To 過度表_dgv.Rows.Count - 1
            過度表_dgv.Rows(I).Cells("銷貨總額").Value = "0" : 過度表_dgv.Rows(I).Cells("成本").Value = "0" : 過度表_dgv.Rows(I).Cells("運費").Value = "0"
            過度表_dgv.Rows(I).Cells("稅額(發票)").Value = "0" : 過度表_dgv.Rows(I).Cells("其他(傭金)").Value = "0" : 過度表_dgv.Rows(I).Cells("手續費(刷卡)").Value = "0"
            過度表_dgv.Rows(I).Cells("稅額(2.5%)").Value = "0"
            For II As Integer = 0 To 獎金明細表_dgv.Rows.Count - 1
                If Strings.Left(獎金明細表_dgv.Rows(II).Cells("編號").Value.ToString, 2) = 過度表_dgv.Rows(I).Cells("編號").Value.ToString And
                       獎金明細表_dgv.Rows(II).Cells("獎金基礎").Value.ToString = 過度表_dgv.Rows(I).Cells("獎金基礎").Value.ToString Then
                    過度表_dgv.Rows(I).Cells("銷貨總額").Value = 過度表_dgv.Rows(I).Cells("銷貨總額").Value + 獎金明細表_dgv.Rows(II).Cells("銷貨總額").Value
                    過度表_dgv.Rows(I).Cells("成本").Value = 過度表_dgv.Rows(I).Cells("成本").Value + 獎金明細表_dgv.Rows(II).Cells("成本").Value
                    過度表_dgv.Rows(I).Cells("運費").Value = 過度表_dgv.Rows(I).Cells("運費").Value + 獎金明細表_dgv.Rows(II).Cells("運費").Value
                    過度表_dgv.Rows(I).Cells("稅額(發票)").Value = 過度表_dgv.Rows(I).Cells("稅額(發票)").Value + 獎金明細表_dgv.Rows(II).Cells("稅額(發票)").Value
                    過度表_dgv.Rows(I).Cells("稅額(2.5%)").Value = 過度表_dgv.Rows(I).Cells("稅額(2.5%)").Value + 獎金明細表_dgv.Rows(II).Cells("稅額(2.5%)").Value
                    過度表_dgv.Rows(I).Cells("其他(傭金)").Value = 過度表_dgv.Rows(I).Cells("其他(傭金)").Value + 獎金明細表_dgv.Rows(II).Cells("其他(傭金)").Value
                    過度表_dgv.Rows(I).Cells("手續費(刷卡)").Value = 過度表_dgv.Rows(I).Cells("手續費(刷卡)").Value + 獎金明細表_dgv.Rows(II).Cells("手續費(刷卡)").Value
                End If
            Next

        Next
        For I As Integer = 0 To 過度表_dgv.Rows.Count - 1
            過度表_dgv.Rows(I).Cells("毛利").Value = "0" : 過度表_dgv.Rows(I).Cells("獎金").Value = "0"
            過度表_dgv.Rows(I).Cells("毛利").Value = 過度表_dgv.Rows(I).Cells("銷貨總額").Value - 過度表_dgv.Rows(I).Cells("成本").Value - 過度表_dgv.Rows(I).Cells("運費").Value -
                                                         過度表_dgv.Rows(I).Cells("稅額(發票)").Value - 過度表_dgv.Rows(I).Cells("其他(傭金)").Value - 過度表_dgv.Rows(I).Cells("手續費(刷卡)").Value -
                                                         過度表_dgv.Rows(I).Cells("管銷").Value + 過度表_dgv.Rows(I).Cells("稅額(2.5%)").Value

            Dim 毛利遞減 As Integer = 過度表_dgv.Rows(I).Cells("毛利").Value
            If 過度表_dgv.Rows(I).Cells("階級1").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級1").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("毛利").Value / 100 * 過度表_dgv.Rows(I).Cells("比例1").Value
            Else
                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                (過度表_dgv.Rows(I).Cells("階級1").Value / 100 * 過度表_dgv.Rows(I).Cells("比例1").Value)
                毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級1").Value
                If 過度表_dgv.Rows(I).Cells("階級2").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級2").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                    過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例2").Value)
                Else
                    過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                               ((過度表_dgv.Rows(I).Cells("階級2").Value - 過度表_dgv.Rows(I).Cells("階級1").Value) / 100 * 過度表_dgv.Rows(I).Cells("比例2").Value)
                    毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級2").Value
                    If 過度表_dgv.Rows(I).Cells("階級3").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級3").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                        過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例3").Value)
                    Else
                        過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                   ((過度表_dgv.Rows(I).Cells("階級3").Value - 過度表_dgv.Rows(I).Cells("階級2").Value) /
                                                                    100 * 過度表_dgv.Rows(I).Cells("比例3").Value)
                        毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級3").Value
                        If 過度表_dgv.Rows(I).Cells("階級4").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級4").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                            過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例4").Value)
                        Else
                            過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                       ((過度表_dgv.Rows(I).Cells("階級4").Value - 過度表_dgv.Rows(I).Cells("階級3").Value) /
                                                                         100 * 過度表_dgv.Rows(I).Cells("比例4").Value)
                            毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級4").Value
                            If 過度表_dgv.Rows(I).Cells("階級5").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級5").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例5").Value)
                            Else
                                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                           ((過度表_dgv.Rows(I).Cells("階級5").Value - 過度表_dgv.Rows(I).Cells("階級4").Value) /
                                                                             100 * 過度表_dgv.Rows(I).Cells("比例5").Value)
                                毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級5").Value
                                If 過度表_dgv.Rows(I).Cells("階級6").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級6").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                                    過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例6").Value)
                                Else
                                    過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                               ((過度表_dgv.Rows(I).Cells("階級6").Value - 過度表_dgv.Rows(I).Cells("階級5").Value) /
                                                                                 100 * 過度表_dgv.Rows(I).Cells("比例6").Value)
                                    毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級6").Value
                                    If 過度表_dgv.Rows(I).Cells("階級7").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級7").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                                        過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例7").Value)
                                    Else
                                        過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                                   ((過度表_dgv.Rows(I).Cells("階級7").Value - 過度表_dgv.Rows(I).Cells("階級6").Value) /
                                                                                     100 * 過度表_dgv.Rows(I).Cells("比例7").Value)
                                        毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級7").Value
                                        If 過度表_dgv.Rows(I).Cells("階級8").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級8").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                                            過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例8").Value)
                                        Else
                                            過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                                       ((過度表_dgv.Rows(I).Cells("階級8").Value - 過度表_dgv.Rows(I).Cells("階級7").Value) /
                                                                                         100 * 過度表_dgv.Rows(I).Cells("比例8").Value)
                                            毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級8").Value
                                            If 過度表_dgv.Rows(I).Cells("階級9").Value = 0 Or 過度表_dgv.Rows(I).Cells("階級9").Value >= 過度表_dgv.Rows(I).Cells("毛利").Value Then
                                                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例9").Value)
                                            Else
                                                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value +
                                                                                           ((過度表_dgv.Rows(I).Cells("階級9").Value - 過度表_dgv.Rows(I).Cells("階級8").Value) /
                                                                                             100 * 過度表_dgv.Rows(I).Cells("比例9").Value)
                                                毛利遞減 = 過度表_dgv.Rows(I).Cells("毛利").Value - 過度表_dgv.Rows(I).Cells("階級9").Value
                                                過度表_dgv.Rows(I).Cells("獎金").Value = 過度表_dgv.Rows(I).Cells("獎金").Value + (毛利遞減 / 100 * 過度表_dgv.Rows(I).Cells("比例10").Value)
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        Next
    End Sub
    Private Sub Set_彙總主表()
        獎金彙總_dgv.DataSource = Nothing : ds6.Clear()
        獎金彙總_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        獎金彙總_dgv.ColumnHeadersHeight = 25
        獎金彙總_dgv.AllowUserToAddRows = False
        獎金彙總_dgv.RowTemplate.Height = 25

        SQL_業績主表1()
        da.Fill(ds6) : 獎金彙總_dgv.DataSource = ds6.Tables(0) : conn.Close()
        獎金彙總_dgv.Columns(2).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(3).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(4).DefaultCellStyle.Format = "#,##0"
        獎金彙總_dgv.Columns(5).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(7).DefaultCellStyle.Format = "#,##0"
        獎金彙總_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(9).DefaultCellStyle.Format = "#,##0" : 獎金彙總_dgv.Columns(10).DefaultCellStyle.Format = "#,##0"
        獎金彙總_dgv.Columns(11).DefaultCellStyle.Format = "#,##0"
        獎金彙總_dgv.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        獎金彙總_dgv.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 獎金彙總_dgv.Columns(11).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        NUD1.Value = 0 : NUD2.Value = 0 : NUD3.Value = 0 : NUD4.Value = 0 : NUD5.Value = 0
        For I As Integer = 0 To 獎金彙總_dgv.Rows.Count - 1
            獎金彙總_dgv.Rows(I).Cells("銷貨總額").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("成本").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("運費").Value = "0"
            獎金彙總_dgv.Rows(I).Cells("稅額(發票)").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("其他(傭金)").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("手續費(刷卡)").Value = "0"
            獎金彙總_dgv.Rows(I).Cells("毛利").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("獎金").Value = "0" : 獎金彙總_dgv.Rows(I).Cells("管銷").Value = "0"
            獎金彙總_dgv.Rows(I).Cells("稅額(2.5%)").Value = "0"
            For II As Integer = 0 To 過度表_dgv.Rows.Count - 1
                If 過度表_dgv.Rows(II).Cells("姓名").Value.ToString = 獎金彙總_dgv.Rows(I).Cells("姓名").Value.ToString Then
                    獎金彙總_dgv.Rows(I).Cells("銷貨總額").Value = 獎金彙總_dgv.Rows(I).Cells("銷貨總額").Value + 過度表_dgv.Rows(II).Cells("銷貨總額").Value
                    獎金彙總_dgv.Rows(I).Cells("成本").Value = 獎金彙總_dgv.Rows(I).Cells("成本").Value + 過度表_dgv.Rows(II).Cells("成本").Value
                    獎金彙總_dgv.Rows(I).Cells("運費").Value = 獎金彙總_dgv.Rows(I).Cells("運費").Value + 過度表_dgv.Rows(II).Cells("運費").Value
                    獎金彙總_dgv.Rows(I).Cells("稅額(發票)").Value = 獎金彙總_dgv.Rows(I).Cells("稅額(發票)").Value + 過度表_dgv.Rows(II).Cells("稅額(發票)").Value
                    獎金彙總_dgv.Rows(I).Cells("其他(傭金)").Value = 獎金彙總_dgv.Rows(I).Cells("其他(傭金)").Value + 過度表_dgv.Rows(II).Cells("其他(傭金)").Value
                    獎金彙總_dgv.Rows(I).Cells("手續費(刷卡)").Value = 獎金彙總_dgv.Rows(I).Cells("手續費(刷卡)").Value + 過度表_dgv.Rows(II).Cells("手續費(刷卡)").Value
                    獎金彙總_dgv.Rows(I).Cells("毛利").Value = 獎金彙總_dgv.Rows(I).Cells("毛利").Value + 過度表_dgv.Rows(II).Cells("毛利").Value
                    獎金彙總_dgv.Rows(I).Cells("獎金").Value = 獎金彙總_dgv.Rows(I).Cells("獎金").Value + 過度表_dgv.Rows(II).Cells("獎金").Value
                    獎金彙總_dgv.Rows(I).Cells("管銷").Value = 獎金彙總_dgv.Rows(I).Cells("管銷").Value + 過度表_dgv.Rows(II).Cells("管銷").Value
                    獎金彙總_dgv.Rows(I).Cells("稅額(2.5%)").Value = 獎金彙總_dgv.Rows(I).Cells("稅額(2.5%)").Value + 過度表_dgv.Rows(II).Cells("稅額(2.5%)").Value
                End If
            Next
            NUD1.Value = NUD1.Value + 獎金彙總_dgv.Rows(I).Cells("運費").Value
        Next
        SQL_業績運費計算_查詢()
        If dr.Read() Then : NUD2.Value = dr("扣運費").ToString : NUD3.Value = dr("已出未收").ToString : NUD4.Value = dr("代運費").ToString
        Else : NUD2.Value = 0 : NUD3.Value = 0 : NUD4.Value = 0 : End If
        NUD5.Value = NUD1.Value - NUD2.Value - NUD3.Value - NUD4.Value
    End Sub
    Private Sub 日期格式()
        '-------------日期一----------------------------------------------------------------------------------------
        DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 開始日期_dtp.Value = DTP
        '-------------日期二----------------------------------------------------------------------------------------
        DTP = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 結束日期_dtp.Value = DTP
        結束日期_dtp.Value = 結束日期_dtp.Value.AddMonths(1) : 結束日期_dtp.Value = 結束日期_dtp.Value.AddDays(-1)
    End Sub
    Private Sub 計算()
        Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4() : Set_清單5() : Set_清單6() : Set_彙總明細() : Set_彙總過度表() : Set_彙總主表()
    End Sub
    Private Sub 業務_cb_下拉表單資料載入()
        SQL_業務編號_下拉清單()
        業務_cb.Items.Clear() : 業務_cb.Items.Clear()
        While (dr.Read()) : 業務_cb.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
        業務_cb_下拉表單資料載入() : SQL_銷售控制表_空值修正1() : SQL_銷售控制表_空值修正2()
        彙總表_bt.Enabled = False : 明細表_bt.Enabled = True : 獎金明細表_dgv.Visible = False : 獎金彙總_dgv.Visible = True
        日期格式() : 計算()

        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 8
        End If
    End Sub
    Private Sub 業績獎金彙總表_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 8
        End If
    End Sub
    Dim 已超出 As Boolean
    Private Sub 獎金彙總_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 獎金彙總_dgv.MouseUp
        'If 獎金彙總_dgv.SelectedCells.Count = 1 Then : MsgBox(獎金彙總_dgv.SelectedCells(0).ColumnIndex & "  " & 獎金彙總_dgv.SelectedCells(0).RowIndex) : End If
        If 獎金彙總_dgv.SelectedCells.Count = 1 Then : 已超出 = False : End If
        If 獎金彙總_dgv.SelectedCells.Count > 0 Then
            If 獎金彙總_dgv.SelectedCells(0).ColumnIndex >= 2 And 獎金彙總_dgv.SelectedCells(0).ColumnIndex <= 10 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 獎金彙總_dgv.SelectedCells.Count - 1
                        x += Val(獎金彙總_dgv.Rows(獎金彙總_dgv.SelectedCells.Item(i).RowIndex).Cells(獎金彙總_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 獎金明細表_dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 獎金明細表_dgv.MouseUp
        If 獎金明細表_dgv.SelectedCells.Count = 1 Then : 已超出 = False : End If
        If 獎金明細表_dgv.SelectedCells.Count > 0 Then
            If 獎金明細表_dgv.SelectedCells(0).ColumnIndex >= 2 And 獎金明細表_dgv.SelectedCells(0).ColumnIndex <= 8 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 獎金明細表_dgv.SelectedCells.Count - 1
                        x += Val(獎金明細表_dgv.Rows(獎金明細表_dgv.SelectedCells.Item(i).RowIndex).Cells(獎金明細表_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
        'If 獎金明細表_dgv.SelectedCells.Count = 1 Then : MsgBox(獎金明細表_dgv.SelectedCells(0).ColumnIndex & "  " & 獎金明細表_dgv.SelectedCells(0).RowIndex) : End If
    End Sub
    Private Sub NUD2_ValueChanged(sender As Object, e As EventArgs) Handles NUD2.ValueChanged
        NUD5.Value = NUD1.Value - NUD2.Value - NUD3.Value - NUD4.Value
    End Sub
    Private Sub NUD3_ValueChanged(sender As Object, e As EventArgs) Handles NUD3.ValueChanged
        NUD5.Value = NUD1.Value - NUD2.Value - NUD3.Value - NUD4.Value
    End Sub
    Private Sub NUD4_ValueChanged(sender As Object, e As EventArgs) Handles NUD4.ValueChanged
        NUD5.Value = NUD1.Value - NUD2.Value - NUD3.Value - NUD4.Value
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        SQL_業績運費計算_查詢()
        If dr.Read() Then
            SQL_業績運費計算_修正() : MsgBox("修改完成!!!")
        Else
            SQL_業績運費計算_新增() : MsgBox("新增完成!!!")
        End If : conn.Close()
    End Sub
    Private Sub 獎金明細表_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 獎金明細表_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 獎金明細表_dgv.Rows.Count - 1 Then : Exit Sub : Else
            If 獎金明細表_dgv(1, e.RowIndex).Value.ToString <> 獎金明細表_dgv(1, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(獎金明細表_dgv.RowHeadersVisible, 獎金明細表_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 獎金明細表_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 獎金明細表_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
            End If
        End If
    End Sub
    Private Sub 開始日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 開始日期_dtp.Click
        計算()
    End Sub
    Private Sub 結束日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 結束日期_dtp.Click
        計算()
    End Sub
    Private Sub 業務_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 業務_cb.SelectedIndexChanged
        計算()
    End Sub
    Private Sub 重新計算_bt_Click(sender As Object, e As EventArgs) Handles 重新計算_bt.Click
        日期格式() : 計算()
    End Sub
    Private Sub 上個月_bt_Click(sender As Object, e As EventArgs) Handles 上個月_bt.Click
        中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
        開始日期_dtp.Value = 中間日期_dtp.Value
        結束日期_dtp.Value = 中間日期_dtp.Value
        日期格式() : 計算()
    End Sub
    Private Sub 下個月_bt_Click(sender As Object, e As EventArgs) Handles 下個月_bt.Click
        中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(1)
        開始日期_dtp.Value = 中間日期_dtp.Value
        結束日期_dtp.Value = 中間日期_dtp.Value
        日期格式() : 計算()
    End Sub
    Private Sub 彙總表_bt_Click(sender As Object, e As EventArgs) Handles 彙總表_bt.Click
        彙總表_bt.Enabled = False : 明細表_bt.Enabled = True : 獎金明細表_dgv.Visible = False : 獎金彙總_dgv.Visible = True
    End Sub
    Private Sub 明細表_bt_Click(sender As Object, e As EventArgs) Handles 明細表_bt.Click
        彙總表_bt.Enabled = True : 明細表_bt.Enabled = False : 獎金明細表_dgv.Visible = True : 獎金彙總_dgv.Visible = False
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        彙總1_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        衍生運費_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        其他_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        外車車資_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        料號一_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        備註運費_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        獎金彙總_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        獎金明細表_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
    Private Sub 列印成EXCEL_bt_Click(sender As Object, e As EventArgs) Handles 列印成EXCEL_bt.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        If 獎金彙總_dgv.Visible = True Then
            xlSheet.Cells(1, 1) = "雅邸家居" : xlSheet.Cells(2, 1) = "業績獎金彙總表" : xlSheet.Cells(3, 1) = "日期區間:" & 開始日期_dtp.Value & " ~ " & 結束日期_dtp.Value
            xlSheet.Cells(4, 1) = "編號" : xlSheet.Cells(4, 2) = "業務姓名" : xlSheet.Cells(4, 3) = "銷貨總額" : xlSheet.Cells(4, 4) = "成本" : xlSheet.Cells(4, 6) = "運費"
            xlSheet.Cells(4, 7) = "稅額(2.5%)"
            xlSheet.Cells(4, 8) = "稅額(發票)" : xlSheet.Cells(4, 9) = "其他(傭金)" : xlSheet.Cells(4, 10) = "手續費(刷卡)" : xlSheet.Cells(4, 11) = "業績毛利"
            xlSheet.Cells(4, 12) = "獎金" : xlSheet.Cells(4, 5) = "管銷"
            For I As Integer = 0 To 獎金彙總_dgv.Rows.Count - 1
                xlSheet.Cells(I + 5, 1) = 獎金彙總_dgv.Rows(I).Cells("編號").Value : xlSheet.Cells(I + 5, 2) = 獎金彙總_dgv.Rows(I).Cells("姓名").Value
                xlSheet.Cells(I + 5, 3) = 獎金彙總_dgv.Rows(I).Cells("銷貨總額").Value : xlSheet.Cells(I + 5, 4) = 獎金彙總_dgv.Rows(I).Cells("成本").Value
                xlSheet.Cells(I + 5, 6) = 獎金彙總_dgv.Rows(I).Cells("運費").Value : xlSheet.Cells(I + 5, 7) = 獎金彙總_dgv.Rows(I).Cells("稅額(2.5%)").Value
                xlSheet.Cells(I + 5, 9) = 獎金彙總_dgv.Rows(I).Cells("其他(傭金)").Value : xlSheet.Cells(I + 5, 10) = 獎金彙總_dgv.Rows(I).Cells("手續費(刷卡)").Value
                xlSheet.Cells(I + 5, 11) = 獎金彙總_dgv.Rows(I).Cells("毛利").Value : xlSheet.Cells(I + 5, 12) = 獎金彙總_dgv.Rows(I).Cells("獎金").Value
                xlSheet.Cells(I + 5, 5) = 獎金彙總_dgv.Rows(I).Cells("管銷").Value : xlSheet.Cells(I + 5, 8) = 獎金彙總_dgv.Rows(I).Cells("稅額(發票)").Value
                N1 = I
            Next
        Else
            xlSheet.Cells(1, 1) = "雅邸家居" : xlSheet.Cells(2, 1) = "業績明細表" : xlSheet.Cells(3, 1) = "日期區間:" & 開始日期_dtp.Value & " ~ " & 結束日期_dtp.Value
            xlSheet.Cells(4, 1) = "編號" : xlSheet.Cells(4, 2) = "客戶姓名" : xlSheet.Cells(4, 3) = "銷貨總額" : xlSheet.Cells(4, 4) = "成本" : xlSheet.Cells(4, 6) = "運費"
            xlSheet.Cells(4, 7) = "稅額(2.5%)"
            xlSheet.Cells(4, 8) = "稅額(發票)" : xlSheet.Cells(4, 9) = "其他(傭金)" : xlSheet.Cells(4, 10) = "手續費(刷卡)" : xlSheet.Cells(4, 11) = "業績毛利"
            xlSheet.Cells(4, 12) = "獎金" : xlSheet.Cells(4, 5) = "管銷"
            For I As Integer = 0 To 獎金明細表_dgv.Rows.Count - 1
                xlSheet.Cells(I + 5, 1) = 獎金明細表_dgv.Rows(I).Cells("編號").Value : xlSheet.Cells(I + 5, 2) = 獎金明細表_dgv.Rows(I).Cells("名稱").Value
                xlSheet.Cells(I + 5, 3) = 獎金明細表_dgv.Rows(I).Cells("銷貨總額").Value : xlSheet.Cells(I + 5, 4) = 獎金明細表_dgv.Rows(I).Cells("成本").Value
                xlSheet.Cells(I + 5, 6) = 獎金明細表_dgv.Rows(I).Cells("運費").Value : xlSheet.Cells(I + 5, 7) = 獎金明細表_dgv.Rows(I).Cells("稅額(2.5%)").Value
                xlSheet.Cells(I + 5, 9) = 獎金明細表_dgv.Rows(I).Cells("其他(傭金)").Value : xlSheet.Cells(I + 5, 10) = 獎金明細表_dgv.Rows(I).Cells("手續費(刷卡)").Value
                xlSheet.Cells(I + 5, 11) = 獎金明細表_dgv.Rows(I).Cells("毛利").Value : xlSheet.Cells(I + 5, 12) = 獎金明細表_dgv.Rows(I).Cells("獎金基礎").Value
                xlSheet.Cells(I + 5, 5) = 獎金明細表_dgv.Rows(I).Cells("管銷費用").Value : xlSheet.Cells(I + 5, 8) = 獎金明細表_dgv.Rows(I).Cells("稅額(發票)").Value
                N1 = I
            Next
        End If
        xlSheet.Cells(N1 + 6, 2) = "合計"
        xlSheet.Cells(N1 + 6, 3) = "=SUM(C5:C" & N1 + 5 & ")" : xlSheet.Cells(N1 + 6, 4) = "=SUM(D5:D" & N1 + 5 & ")"
        xlSheet.Cells(N1 + 6, 6) = "=SUM(F5:F" & N1 + 5 & ")" : xlSheet.Cells(N1 + 6, 7) = "=SUM(G5:G" & N1 + 5 & ")"
        xlSheet.Cells(N1 + 6, 8) = "=SUM(H5:H" & N1 + 5 & ")" : xlSheet.Cells(N1 + 6, 9) = "=SUM(I5:I" & N1 + 5 & ")"
        xlSheet.Cells(N1 + 6, 10) = "=SUM(J5:J" & N1 + 5 & ")" : xlSheet.Cells(N1 + 6, 11) = "=SUM(K5:K" & N1 + 5 & ")"
        xlSheet.Cells(N1 + 6, 5) = "=SUM(E5:E" & N1 + 5 & ")"
        xlSheet.Cells(N1 + 7, 3) = "扣運費(上月已結)" : xlSheet.Cells(N1 + 7, 6) = NUD2.Value
        xlSheet.Cells(N1 + 8, 3) = "已出貨未扣款" : xlSheet.Cells(N1 + 8, 6) = NUD3.Value
        xlSheet.Cells(N1 + 9, 3) = "代運費" : xlSheet.Cells(N1 + 9, 6) = NUD4.Value
        xlSheet.Cells(N1 + 10, 3) = "共計" : xlSheet.Cells(N1 + 10, 6) = NUD5.Value
        BB(xlApp, xlSheet)

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        xlSheet.Cells.Select()
        MsgBox("列印完成。")
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Columns("A:A").Select
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Columns("B:B").Select
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("C4:K4").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 5
        xlSheet.Range("C5:L" & N1 + 6 & "").Select() : myExcel.Selection.Style = "Comma" : myExcel.Selection.NumberFormatLocal = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
        xlSheet.Range("F" & N1 + 7 & ":F" & N1 + 10 & "").Select() : myExcel.Selection.Style = "Comma" : myExcel.Selection.NumberFormatLocal = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
        xlSheet.Columns("C:K").EntireColumn.AutoFit

        xlSheet.Range("B" & N1 + 7 & ":E" & N1 + 7 & "").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("B" & N1 + 8 & ":E" & N1 + 8 & "").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("B" & N1 + 9 & ":E" & N1 + 9 & "").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("B" & N1 + 10 & ":E" & N1 + 10 & "").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("B" & N1 + 7 & ":E" & N1 + 10 & "").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : End With
        xlSheet.Range("A1:L1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("A2:K2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("A3:L3").Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        xlSheet.Range("A1:L1").Select()
        With myExcel.Selection.Font : .Name = "新細明體" : .Size = 24 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontMinor : End With

        xlSheet.Range("A4:L4").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlDouble : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThick : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A3:L3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A" & N1 + 5 & ":L" & N1 + 5 & "").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

        xlSheet.Range("A" & N1 + 9 & ":F" & N1 + 9 & "").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        myExcel.Selection.Borders(xlEdgeLeft).LineStyle = xlNone : myExcel.Selection.Borders(xlEdgeTop).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium : End With
        myExcel.Selection.Borders(xlEdgeRight).LineStyle = xlNone : myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        myExcel.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        If 獎金彙總_dgv.Visible = True Then : Else
            xlSheet.Columns("A:A").Select : myExcel.Columns("A:A").EntireColumn.AutoFit
            xlSheet.Columns("B:B").Select : myExcel.Columns("B:B").EntireColumn.AutoFit
            xlSheet.Columns("C:C").Select : myExcel.Columns("C:C").EntireColumn.AutoFit
        End If
        myExcel.ActiveWindow.DisplayGridlines = False
    End Sub
End Class