Option Strict Off
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 System.Data.SqlClient
Public Class 採購應負帳款
    ReadOnly ds, ds1, da2 As New DataSet
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim N2 As Integer : Dim 新流水1 As String : Dim 新項次 As String : Dim 新支付單號 As String
    Dim N1 As Integer : Dim NUM1 As Integer : Dim 匯率1 As Double : Dim 狀態 As String
    Dim 首次 As Boolean
    Private Sub Set_清單1()
        供應商_dgv.DataSource = Nothing : ds1.Clear()
        供應商_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        供應商_dgv.ColumnHeadersHeight = 40
        供應商_dgv.AllowUserToAddRows = False
        供應商_dgv.RowTemplate.Height = 30

        合計_dgv.RowTemplate.Height = 30

        SQL_採購對帳單_總表()
        da.Fill(ds1) : 供應商_dgv.DataSource = ds1.Tables(0) : conn.Close()

        供應商_dgv.Columns(0).FillWeight = 50 : 供應商_dgv.Columns(1).FillWeight = 120 : 供應商_dgv.Columns(2).FillWeight = 70 : 供應商_dgv.Columns(3).Visible = False
        供應商_dgv.Columns(4).FillWeight = 50 : 供應商_dgv.Columns(5).FillWeight = 80 : 供應商_dgv.Columns(6).Visible = False : 供應商_dgv.Columns(7).FillWeight = 50
        供應商_dgv.Columns(8).FillWeight = 80 : 供應商_dgv.Columns(9).FillWeight = 80 : 供應商_dgv.Columns(10).FillWeight = 80 : 供應商_dgv.Columns(11).Visible = False
        供應商_dgv.Columns(12).FillWeight = 80 : 供應商_dgv.Columns(13).FillWeight = 50 : 供應商_dgv.Columns(14).FillWeight = 50 : 供應商_dgv.Columns(15).FillWeight = 100
        供應商_dgv.Columns(16).FillWeight = 100 : 供應商_dgv.Columns(17).FillWeight = 150 : 供應商_dgv.Columns(18).FillWeight = 70

        供應商_dgv.Columns(0).ReadOnly = True : 供應商_dgv.Columns(1).ReadOnly = True : 供應商_dgv.Columns(2).ReadOnly = True : 供應商_dgv.Columns(5).ReadOnly = True
        供應商_dgv.Columns(8).ReadOnly = True : 供應商_dgv.Columns(10).ReadOnly = True : 供應商_dgv.Columns(14).ReadOnly = True : 供應商_dgv.Columns(15).ReadOnly = True
        供應商_dgv.Columns(16).ReadOnly = True

        資料筆數1_tb.Text = 供應商_dgv.Rows.Count : 供應商_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        供應商_dgv.Columns(4).DefaultCellStyle.BackColor = Color.MistyRose
        供應商_dgv.Columns(7).DefaultCellStyle.BackColor = Color.MistyRose
        供應商_dgv.Columns(9).DefaultCellStyle.BackColor = Color.MistyRose
        供應商_dgv.Columns(12).DefaultCellStyle.BackColor = Color.MistyRose
        供應商_dgv.Columns(13).DefaultCellStyle.BackColor = Color.MistyRose

        稅金_tb.Text = "0" : 廣告費_tb.Text = "0" : 其他扣款_tb.Text = "0" : 實付金額_tb.Text = "0" : 公司實付_tb.Text = "0" : 個人實付_tb.Text = "0"

        For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
            If 供應商_dgv("稅金%", i).Value.ToString = "" Then : 供應商_dgv("稅金%", i).Value = 供應商_dgv("稅金1", i).Value.ToString : End If
            供應商_dgv("稅金", i).Value = 供應商_dgv("當月貨款", i).Value / 100 * 供應商_dgv("稅金%", i).Value
            If 供應商_dgv("廣告%", i).Value.ToString = "" Then : 供應商_dgv("廣告%", i).Value = 供應商_dgv("廣告1", i).Value.ToString : End If
            供應商_dgv("廣告", i).Value = 供應商_dgv("當月貨款", i).Value / 100 * 供應商_dgv("廣告%", i).Value
            If 供應商_dgv("其他扣款", i).Value.ToString = "" Then : 供應商_dgv("其他扣款", i).Value = "0" : End If
            供應商_dgv("實付金額", i).Value = 供應商_dgv("當月貨款", i).Value - 供應商_dgv("稅金", i).Value - 供應商_dgv("廣告", i).Value - 供應商_dgv("其他扣款", i).Value
            If 供應商_dgv("公司%", i).Value.ToString = "" Then : 供應商_dgv("公司%", i).Value = "0" : End If
            If 供應商_dgv("個人%", i).Value.ToString = "" Then : 供應商_dgv("個人%", i).Value = "0" : End If
            供應商_dgv("個人%", i).Value = 100 - 供應商_dgv("公司%", i).Value
            供應商_dgv("公司", i).Value = 供應商_dgv("實付金額", i).Value / 100 * 供應商_dgv("公司%", i).Value
            供應商_dgv("個人", i).Value = 供應商_dgv("實付金額", i).Value / 100 * 供應商_dgv("個人%", i).Value

            稅金_tb.Text = Val(稅金_tb.Text) + 供應商_dgv("稅金", i).Value
            廣告費_tb.Text = Val(廣告費_tb.Text) + 供應商_dgv("廣告", i).Value
            其他扣款_tb.Text = Val(其他扣款_tb.Text) + 供應商_dgv("其他扣款", i).Value
            實付金額_tb.Text = Val(實付金額_tb.Text) + 供應商_dgv("實付金額", i).Value
            公司實付_tb.Text = Val(公司實付_tb.Text) + 供應商_dgv("公司", i).Value
            個人實付_tb.Text = Val(個人實付_tb.Text) + 供應商_dgv("個人", i).Value

            If 供應商_dgv("票期", i).Value.ToString = "" Then
                If Strings.Mid(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 6, 2) = "12" Then
                    If 供應商_dgv("票期1", i).Value.ToString = "30" Then
                        供應商_dgv("票期1", i).Value = "28"
                    End If
                End If
                DTP1.Value = 結束日期_dtp.Value : DTP1.Value = DTP1.Value.AddMonths(2)
                If 供應商_dgv("票期1", i).Value < 10 Then
                    PA16 = "0" & 供應商_dgv("票期1", i).Value.ToString
                Else
                    PA16 = 供應商_dgv("票期1", i).Value.ToString
                End If
                供應商_dgv("票期", i).Value = Strings.Left(Strings.Format(DTP1.Value, "yyyy/MM/dd"), 7) & "/" & PA16
            End If
        Next

        供應商_dgv.Columns("公司").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("公司").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("公司").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("個人").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("個人").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("個人").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("公司%").DefaultCellStyle.Format = "#,##0.00"
        供應商_dgv.Columns("公司%").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("公司%").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("個人%").DefaultCellStyle.Format = "#,##0.00"
        供應商_dgv.Columns("個人%").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("個人%").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("實付金額").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("實付金額").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("實付金額").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("其他扣款").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("其他扣款").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("其他扣款").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("廣告").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("廣告").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("廣告").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("廣告%").DefaultCellStyle.Format = "#,##0.00"
        供應商_dgv.Columns("廣告%").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("廣告%").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("稅金").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("稅金").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("稅金").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("稅金%").DefaultCellStyle.Format = "#,##0.00"
        供應商_dgv.Columns("稅金%").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("稅金%").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("當月貨款").DefaultCellStyle.Format = "#,##0"
        供應商_dgv.Columns("當月貨款").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        供應商_dgv.Columns("當月貨款").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("供應商").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("供應商").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("編號").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("編號").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("票期").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("票期").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        If 首次 = True Then : Else : Set_清單3() : End If
    End Sub
    Private Sub Set_清單2()
        採購清單_dgv.DataSource = Nothing : ds.Clear()
        採購清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        採購清單_dgv.ColumnHeadersHeight = 40
        採購清單_dgv.AllowUserToAddRows = False

        If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then
            採購清單_dgv.RowTemplate.Height = 95
        ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then
            採購清單_dgv.RowTemplate.Height = 60
        ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then
            採購清單_dgv.RowTemplate.Height = 25
        End If

        SQL_採購對帳單_明細()
        da.Fill(ds) : 採購清單_dgv.DataSource = ds.Tables(0) : conn.Close()

        採購清單_dgv.Columns(0).Visible = False : 採購清單_dgv.Columns(1).FillWeight = 80 : 採購清單_dgv.Columns(2).Visible = False : 採購清單_dgv.Columns(3).FillWeight = 80
        採購清單_dgv.Columns(4).FillWeight = 80 : 採購清單_dgv.Columns(5).FillWeight = 200 : 採購清單_dgv.Columns(6).Visible = False : 採購清單_dgv.Columns(7).FillWeight = 100
        採購清單_dgv.Columns(8).Visible = False : 採購清單_dgv.Columns(9).FillWeight = 160 : 採購清單_dgv.Columns(10).Visible = False : 採購清單_dgv.Columns(11).FillWeight = 95
        採購清單_dgv.Columns(12).FillWeight = 145 : 採購清單_dgv.Columns(13).Visible = False : 採購清單_dgv.Columns(14).FillWeight = 45 : 採購清單_dgv.Columns(15).FillWeight = 95
        採購清單_dgv.Columns(16).FillWeight = 80 : 採購清單_dgv.Columns(17).Visible = False : 採購清單_dgv.Columns(18).FillWeight = 70 : 採購清單_dgv.Columns(19).FillWeight = 70
        採購清單_dgv.Columns(20).FillWeight = 70 : 採購清單_dgv.Columns(21).FillWeight = 70 : 採購清單_dgv.Columns(22).FillWeight = 70 : 採購清單_dgv.Columns(23).FillWeight = 45
        採購清單_dgv.Columns(24).FillWeight = 45

        採購清單_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0" : 採購清單_dgv.Columns("成本").DefaultCellStyle.Format = "#,##0"
        採購清單_dgv.Columns("金額").DefaultCellStyle.Format = "#,##0"
        採購清單_dgv.Columns("數量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("數量").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        採購清單_dgv.Columns(1).ReadOnly = True : 採購清單_dgv.Columns(3).ReadOnly = True : 採購清單_dgv.Columns(4).ReadOnly = True : 採購清單_dgv.Columns(5).ReadOnly = True
        採購清單_dgv.Columns(7).ReadOnly = True : 採購清單_dgv.Columns(9).ReadOnly = True : 採購清單_dgv.Columns(11).ReadOnly = True : 採購清單_dgv.Columns(12).ReadOnly = True
        採購清單_dgv.Columns(13).ReadOnly = True : 採購清單_dgv.Columns(14).ReadOnly = True : 採購清單_dgv.Columns(15).ReadOnly = True : 採購清單_dgv.Columns(16).ReadOnly = True
        採購清單_dgv.Columns(17).ReadOnly = True : 採購清單_dgv.Columns(18).ReadOnly = True : 採購清單_dgv.Columns(19).ReadOnly = True : 採購清單_dgv.Columns(20).ReadOnly = True
        採購清單_dgv.Columns(21).ReadOnly = True : 採購清單_dgv.Columns(22).ReadOnly = True : 採購清單_dgv.Columns(23).ReadOnly = True

        採購清單_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        資料筆數_tb.Text = 採購清單_dgv.Rows.Count
        Set_合記計算()
    End Sub
    Private Sub Set_清單3()
        合計_dgv.DataSource = Nothing : da2.Clear()
        合計_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        合計_dgv.ColumnHeadersHeight = 25
        合計_dgv.AllowUserToAddRows = False

        SQL_採購對帳單_合計()
        da.Fill(da2) : 合計_dgv.DataSource = da2.Tables(0) : conn.Close()

        For i As Integer = 0 To 合計_dgv.Rows.Count - 1
            合計_dgv("個人", i).Value = "0" : 合計_dgv("公司", i).Value = "0"
        Next
        For i As Integer = 0 To 合計_dgv.Rows.Count - 1
            For ii As Integer = 0 To 供應商_dgv.Rows.Count - 1
                If 供應商_dgv("票期", ii).Value.ToString = 合計_dgv("票期", i).Value.ToString Then
                    合計_dgv("公司", i).Value = Val(合計_dgv("公司", i).Value.ToString) + Val(供應商_dgv("公司", ii).Value)
                    合計_dgv("個人", i).Value = Val(合計_dgv("個人", i).Value.ToString) + Val(供應商_dgv("個人", ii).Value)
                End If
            Next
        Next
        合計_dgv.Columns("公司").DefaultCellStyle.Format = "#,##0"
        合計_dgv.Columns("公司").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        合計_dgv.Columns("公司").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        合計_dgv.Columns("個人").DefaultCellStyle.Format = "#,##0"
        合計_dgv.Columns("個人").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        合計_dgv.Columns("個人").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    End Sub
    Private Sub Set_合記計算()
        當月貨款_tb.Text = "0" : 採購金額1_tb.Text = "0"
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            採購清單_dgv("金額", i).Value = 採購清單_dgv("數量", i).Value * 採購清單_dgv("成本", i).Value
            當月貨款_tb.Text = Val(當月貨款_tb.Text) + 採購清單_dgv("金額", i).Value
            If 供應商_tb.Text = 採購清單_dgv("供應商", i).Value.ToString Then
                採購金額1_tb.Text = Val(採購金額1_tb.Text) + 採購清單_dgv("金額", i).Value
            End If
        Next
        當月貨款_tb.Text = Strings.Format(Val(當月貨款_tb.Text), "#,##0")
        採購金額1_tb.Text = Strings.Format(Val(採購金額1_tb.Text), "#,##0") : 稅金_tb.Text = Strings.Format(Val(稅金_tb.Text), "#,##0")
        廣告費_tb.Text = Strings.Format(Val(廣告費_tb.Text), "#,##0") : 其他扣款_tb.Text = Strings.Format(Val(其他扣款_tb.Text), "#,##0") : 實付金額_tb.Text = Strings.Format(Val(實付金額_tb.Text), "#,##0")
        公司實付_tb.Text = Strings.Format(Val(公司實付_tb.Text), "#,##0") : 個人實付_tb.Text = Strings.Format(Val(個人實付_tb.Text), "#,##0")
    End Sub
    Private Sub 日期格式()
        If 會計月_ch.Checked = True Then
            '-------------日期一----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "01" : 開始日期_dtp.Value = DTP
            開始日期_dtp.Value = 開始日期_dtp.Value.AddMonths(-1)
            DTP = Strings.Left(Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd"), 8) & "26" : 開始日期_dtp.Value = DTP
            '-------------日期二----------------------------------------------------------------------------------------
            DTP = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 8) & "25" : 結束日期_dtp.Value = DTP
        Else
            '-------------日期一----------------------------------------------------------------------------------------
            開始日期_dtp.Value = 開始日期_dtp.Value.AddMonths(1)
            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 If
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        SQL_採購應付帳款_查詢公帳控制表會計科目() : ComboBox1.Items.Clear() : ComboBox6.Items.Clear()
        While (dr.Read()) : ComboBox1.Items.Add(dr("會計科目")) : ComboBox6.Items.Add(dr("編號")) : End While : conn.Close()
        If ComboBox1.Items.Count <> 0 Then : ComboBox1.SelectedIndex = 0 : End If
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        SQL_採購應付帳款_查詢最後一筆公帳控制表會計科目() : ComboBox5.Items.Clear() : ComboBox2.Items.Clear() : ComboBox5.Text = "" : ComboBox2.Text = ""
        While (dr.Read()) : ComboBox5.Items.Add(dr("第一階")) : ComboBox2.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        SQL_採購應付帳款_查詢最左邊公帳控制表會計科目() : ComboBox3.Items.Clear() : ComboBox4.Items.Clear() : ComboBox3.Text = "" : ComboBox4.Text = ""
        While (dr.Read()) : ComboBox3.Items.Add(dr("第二階")) : ComboBox4.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
        小間距_ch.Checked = True : 供應商_dgv.Visible = False : 供應商_dgv.Visible = True : 應負帳款退回_bt.Visible = False
        提示_lb.Text = "目前呈現表單為應付帳款，若要退回採購明細資料請點選下方採購明細"
        首次 = True : 日期格式() : 首次 = False : Set_清單3()
        ComboBox3.Items.Clear() : ComboBox4.Items.Clear() : ComboBox3.Text = "" : ComboBox4.Text = ""
        ComboBox5.Items.Clear() : ComboBox2.Items.Clear() : ComboBox5.Text = "" : ComboBox2.Text = ""
        ComboBox6.Visible = False
        ComboBox2下拉表單資料載入() : 幣別_cb.SelectedIndex = 0
        ComboBox5.SelectedIndex = 0 : ComboBox3.SelectedIndex = 1 : ComboBox1.SelectedIndex = 5 : ComboBox2.SelectedIndex = 0 : ComboBox4.SelectedIndex = 1
    End Sub
    Private Sub 供應商_dgv_CellEndEdit(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 供應商_dgv.CellEndEdit
        For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
            If 供應商_dgv("稅金%", i).Value.ToString = "" Then : 供應商_dgv("稅金%", i).Value = 供應商_dgv("稅金1", i).Value.ToString : End If
            供應商_dgv("稅金", i).Value = 供應商_dgv("當月貨款", i).Value / 100 * 供應商_dgv("稅金%", i).Value
            If 供應商_dgv("廣告%", i).Value.ToString = "" Then : 供應商_dgv("廣告%", i).Value = 供應商_dgv("廣告1", i).Value.ToString : End If
            供應商_dgv("廣告", i).Value = 供應商_dgv("當月貨款", i).Value / 100 * 供應商_dgv("廣告%", i).Value
            If 供應商_dgv("其他扣款", i).Value.ToString = "" Then : 供應商_dgv("其他扣款", i).Value = "0" : End If
            供應商_dgv("實付金額", i).Value = 供應商_dgv("當月貨款", i).Value - 供應商_dgv("稅金", i).Value - 供應商_dgv("廣告", i).Value - 供應商_dgv("其他扣款", i).Value
            If 供應商_dgv("公司%", i).Value.ToString = "" Then : 供應商_dgv("公司%", i).Value = "0" : End If
            If 供應商_dgv("個人%", i).Value.ToString = "" Then : 供應商_dgv("個人%", i).Value = "0" : End If
            供應商_dgv("個人%", i).Value = 100 - 供應商_dgv("公司%", i).Value
            供應商_dgv("公司", i).Value = 供應商_dgv("實付金額", i).Value / 100 * 供應商_dgv("公司%", i).Value
            供應商_dgv("個人", i).Value = 供應商_dgv("實付金額", i).Value / 100 * 供應商_dgv("個人%", i).Value

            If 供應商_dgv("票期", i).Value.ToString = "" Then
                If Strings.Mid(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 6, 2) = "12" Then
                    If 供應商_dgv("票期1", i).Value.ToString = "30" Then
                        供應商_dgv("票期1", i).Value = "28"
                    End If
                End If
                DTP1.Value = 結束日期_dtp.Value : DTP1.Value = DTP1.Value.AddMonths(2)
                供應商_dgv("票期", i).Value = Strings.Left(Strings.Format(DTP1.Value, "yyyy/MM/dd"), 7) & "/" & 供應商_dgv("票期1", i).Value.ToString
            End If
        Next

        Dim EU As String
        EU = 供應商_dgv("票期", e.RowIndex).Value.ToString
        If EU = "" Then : Else
            If IsDate(EU) Then
                DTP2.Text = EU : 供應商_dgv.Rows(e.RowIndex).Cells("票期").Value = Strings.Format(DTP2.Value, "yyyy/MM/dd")
            Else
                供應商_dgv.Rows(e.RowIndex).Cells("票期").Value = ""
            End If
        End If
        For i As Integer = 0 To 合計_dgv.Rows.Count - 1
            合計_dgv("個人", i).Value = "0" : 合計_dgv("公司", i).Value = "0"
        Next
        For i As Integer = 0 To 合計_dgv.Rows.Count - 1
            For ii As Integer = 0 To 供應商_dgv.Rows.Count - 1
                If 供應商_dgv("票期", ii).Value.ToString = 合計_dgv("票期", i).Value.ToString Then
                    合計_dgv("公司", i).Value = Val(合計_dgv("公司", i).Value.ToString) + Val(供應商_dgv("公司", ii).Value)
                    合計_dgv("個人", i).Value = Val(合計_dgv("個人", i).Value.ToString) + Val(供應商_dgv("個人", ii).Value)
                End If
            Next
        Next
    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("供應商", e.RowIndex).Value.ToString <> 採購清單_dgv("供應商", 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 採購清單_dgv_CellEndEdit(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellEndEdit
        PA20 = 採購清單_dgv(8, e.RowIndex).Value : SQL_銷售明細_改倉名()
        If dr.Read() Then : 採購清單_dgv("倉儲狀態", e.RowIndex).Value = dr("倉名編號") : End If
        PA19 = 採購清單_dgv(11, e.RowIndex).Value : SQL_銷售明細_改物料訂製狀態()
        If dr.Read() Then : 採購清單_dgv("訂製狀態", e.RowIndex).Value = dr("編碼") : End If
        PA18 = 採購清單_dgv(14, e.RowIndex).Value : SQL_銷售明細_倉儲狀態()
        If dr.Read() Then : 採購清單_dgv("物料狀態", e.RowIndex).Value = dr("編碼") : End If : conn.Close()

        Dim EU(1) As String
        EU(0) = 採購清單_dgv("採購日期", e.RowIndex).Value.ToString
        If EU(0) = "" Then : Else
            日期1_dtp.Text = EU(0) : 採購清單_dgv.Rows(e.RowIndex).Cells("採購日期").Value = Strings.Format(日期1_dtp.Value, "yyyy/MM/dd")
        End If
        EU(1) = 採購清單_dgv("預計進廠日", e.RowIndex).Value.ToString
        If EU(1) = "" Then : Else
            日期2_dtp.Text = EU(1) : 採購清單_dgv.Rows(e.RowIndex).Cells("預計進廠日").Value = Strings.Format(日期2_dtp.Value, "yyyy/MM/dd")
        End If
    End Sub
    Private Sub 採購清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            供應商_tb.Text = 採購清單_dgv("供應商", e.RowIndex).Value.ToString
            資料流水號_tb.Text = 採購清單_dgv.Rows(e.RowIndex).Cells("資料流水號").Value.ToString
            PA15 = 採購清單_dgv(0, e.RowIndex).Value.ToString
            採購金額1_tb.Text = 採購清單_dgv.Rows(e.RowIndex).Cells("金額").Value
            採購金額1_tb.Text = Strings.Format(Val(採購金額1_tb.Text), "#,##0")
            物料圖1_pb.Image = Nothing : PA1 = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料檔案_是否有圖()
            If dr.Read() Then
                If dr("是否有圖") = True Then
                    PA = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料圖庫_圖片顯示() '--------------------圖片顯示
                    While dr.Read() = True
                        Dim unused As Byte() = New Byte(-1) {}
                        Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes)
                        物料圖1_pb.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                    物料圖1_pb.SizeMode = 4
                Else
                    物料圖1_pb.Image = Nothing
                End If
            End If : conn.Close()
        End If

    End Sub
    Private Sub 上個月_bt_Click(sender As Object, e As EventArgs) Handles 上個月_bt.Click
        If 會計月_ch.Checked = True Then
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        Else
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            開始日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            結束日期_dtp.Value = 中間日期_dtp.Value
        End If
        日期格式()
    End Sub
    Private Sub 下個月_bt_Click(sender As Object, e As EventArgs) Handles 下個月_bt.Click
        If 會計月_ch.Checked = True Then
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(1)
            開始日期_dtp.Value = 中間日期_dtp.Value
            結束日期_dtp.Value = 中間日期_dtp.Value
        Else
            中間日期_dtp.Value = 中間日期_dtp.Value.AddMonths(1)
            開始日期_dtp.Value = 中間日期_dtp.Value.AddMonths(-1)
            結束日期_dtp.Value = 中間日期_dtp.Value
        End If
        日期格式()
    End Sub
    Private Sub 開始日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 開始日期_dtp.ValueChanged
        Set_清單1() : Set_清單2()
    End Sub
    Private Sub 結束日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 結束日期_dtp.ValueChanged
        Set_清單1() : Set_清單2()
    End Sub
    Private Sub 會計月_CheckedChanged(sender As Object, e As EventArgs) Handles 會計月_ch.Click
        會計月_ch.Checked = True : 一般月_ch.Checked = False : 日期格式()
    End Sub
    Private Sub 一般月_CheckedChanged(sender As Object, e As EventArgs) Handles 一般月_ch.Click
        會計月_ch.Checked = False : 一般月_ch.Checked = True : 日期格式()
    End Sub
    Private Sub 窄間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距_ch.Click
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False
        Set_清單2()
    End Sub
    Private Sub 中間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False
        Set_清單2()
    End Sub
    Private Sub 大間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = False : 大間距_ch.Checked = True
        Set_清單2()
    End Sub
    Private Sub 採購明細_bt_Click(sender As Object, e As EventArgs) Handles 採購明細_bt.Click
        採購明細_bt.Enabled = False : 應負帳款_bt.Enabled = True : 採購清單_dgv.Visible = True : 供應商_dgv.Visible = False
        應負帳款退回_bt.Visible = True : 提示_lb.Text = "要將採購明細資料退回時，請點選採購明細中要退回的資料後點選左邊的應負帳款退回"
        刪除_bt.Enabled = False : ComboBox5.Enabled = False : ComboBox2.Enabled = False : ComboBox3.Enabled = False : ComboBox4.Enabled = False
        ComboBox1.Enabled = False : 幣別_cb.Enabled = False
    End Sub
    Private Sub 應負帳款_bt_Click(sender As Object, e As EventArgs) Handles 應負帳款_bt.Click
        採購明細_bt.Enabled = True : 應負帳款_bt.Enabled = False : 採購清單_dgv.Visible = False : 供應商_dgv.Visible = True
        應負帳款退回_bt.Visible = False : 提示_lb.Text = "目前呈現表單為應付帳款，若要退回採購明細資料請點選下方採購明細"
        刪除_bt.Enabled = True : ComboBox5.Enabled = True : ComboBox2.Enabled = True : ComboBox3.Enabled = True : ComboBox4.Enabled = True
        ComboBox1.Enabled = True : 幣別_cb.Enabled = True
    End Sub
    Private Sub 重新讀取_bt_Click(sender As Object, e As EventArgs) Handles 重新讀取_bt.Click
        Set_清單1() : Set_清單2()
    End Sub
    Private Shared Function NewMethod(xlBook As Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet
        Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
    End Function
    Private Sub 匯出Excel_bt_Click(sender As Object, e As EventArgs) Handles 匯出Excel_bt.Click
        xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
        xlApp.Visible = True
        xlApp.DisplayAlerts = True
        xlApp.Application.WindowState = xlMaximized
        xlBook = xlApp.Workbooks.Add
        xlSheet = NewMethod(xlBook)
        xlBook.Activate()
        xlSheet.Activate()

        xlSheet.Cells(1, 1) = "雅邸家具有限公司-採購應付帳款 - " & Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 7)
        xlSheet.Cells(2, 1) = "編號 "
        xlSheet.Cells(2, 2) = "供應商"
        xlSheet.Cells(2, 3) = "當月貨款"
        xlSheet.Cells(2, 4) = "稅金"
        xlSheet.Cells(2, 5) = "廣告"
        xlSheet.Cells(2, 6) = "其他扣款"
        xlSheet.Cells(2, 7) = "實付金額"
        xlSheet.Cells(2, 8) = "票期"
        xlSheet.Cells(2, 9) = "公司"
        xlSheet.Cells(2, 10) = "個人"
        xlSheet.Cells(2, 11) = "支票_公司"
        xlSheet.Cells(2, 12) = "支票_私人"

        For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
            xlSheet.Cells(i + 3, 1) = 供應商_dgv.Rows(i).Cells("編號").Value
            xlSheet.Cells(i + 3, 2) = 供應商_dgv.Rows(i).Cells("供應商").Value
            xlSheet.Cells(i + 3, 3) = 供應商_dgv.Rows(i).Cells("當月貨款").Value
            xlSheet.Cells(i + 3, 4) = 供應商_dgv.Rows(i).Cells("稅金").Value
            xlSheet.Cells(i + 3, 5) = 供應商_dgv.Rows(i).Cells("廣告").Value
            xlSheet.Cells(i + 3, 6) = 供應商_dgv.Rows(i).Cells("其他扣款").Value
            xlSheet.Cells(i + 3, 7) = 供應商_dgv.Rows(i).Cells("實付金額").Value
            xlSheet.Cells(i + 3, 8) = 供應商_dgv.Rows(i).Cells("票期").Value
            xlSheet.Cells(i + 3, 9) = 供應商_dgv.Rows(i).Cells("公司").Value
            xlSheet.Cells(i + 3, 10) = 供應商_dgv.Rows(i).Cells("個人").Value
            xlSheet.Cells(i + 3, 11) = 供應商_dgv.Rows(i).Cells("支票_公司").Value
            xlSheet.Cells(i + 3, 12) = 供應商_dgv.Rows(i).Cells("支票_私人").Value
            N2 = i + 3
        Next i

        xlSheet.Cells(N2 + 1, 1) = "合計"
        xlSheet.Cells(N2 + 1, 3) = "=SUM(C3:C" & N2 & ")"
        xlSheet.Cells(N2 + 1, 4) = "=SUM(D3:D" & N2 & ")"
        xlSheet.Cells(N2 + 1, 5) = "=SUM(E3:E" & N2 & ")"
        xlSheet.Cells(N2 + 1, 6) = "=SUM(F3:F" & N2 & ")"
        xlSheet.Cells(N2 + 1, 7) = "=SUM(G3:G" & N2 & ")"
        xlSheet.Cells(N2 + 1, 9) = "=SUM(I3:I" & N2 & ")"
        xlSheet.Cells(N2 + 1, 10) = "=SUM(J3:J" & N2 & ")"
        BB(xlApp, xlSheet)

        xlSheet.Cells(N2 + 4, 8) = "票期"
        xlSheet.Cells(N2 + 4, 9) = "公司"
        xlSheet.Cells(N2 + 4, 10) = "個人"

        For i As Integer = 0 To 合計_dgv.Rows.Count - 1
            xlSheet.Cells(i + N2 + 5, 8) = 合計_dgv.Rows(i).Cells("票期").Value
            xlSheet.Cells(i + N2 + 5, 9) = 合計_dgv.Rows(i).Cells("公司").Value
            xlSheet.Cells(i + N2 + 5, 10) = 合計_dgv.Rows(i).Cells("個人").Value
            N1 = i + N2 + 5
        Next i
        CC(xlApp, xlSheet)

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlApp.Application.WindowState = xlMinimized
        MsgBox("列印完成!!!")
    End Sub
    Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        xlSheet.Cells.Select()

        With myExcel.Selection.Font
            .Name = "微軟正黑體" : .Size = 11 : .Strikethrough = False : .Superscript = False : .Subscript = False
            .OutlineFont = False : .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With

        xlSheet.Rows("1:1").Select
        myExcel.Selection.RowHeight = 35
        xlSheet.Rows("2:2").Select
        myExcel.Selection.RowHeight = 30
        xlSheet.Rows("1:1").Select

        With myExcel.Selection.Font
            .Name = "微軟正黑體" : .Size = 22 : .Strikethrough = False : .Superscript = False : .Subscript = False
            .OutlineFont = False : .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With

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

        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Columns("A:A").Select
        xlSheet.Range("A2").Activate()
        myExcel.Selection.ColumnWidth = 7
        xlSheet.Range("A3:A8").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        With myExcel.Selection
            .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        With myExcel.Selection
            .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Range("P47").Select()
        xlSheet.Columns("B:B").Select : xlSheet.Range("B2").Activate() : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("C:C").Select : xlSheet.Range("C2").Activate() : myExcel.Selection.ColumnWidth = 10
        xlSheet.Columns("D:D").Select : xlSheet.Range("D2").Activate() : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("E:E").Select : xlSheet.Range("E2").Activate() : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("F:F").Select : xlSheet.Range("F2").Activate() : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("G:G").Select : xlSheet.Range("G2").Activate() : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("H:H").Select : xlSheet.Range("H2").Activate() : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("I:I").Select : xlSheet.Range("I2").Activate() : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("J:J").Select : xlSheet.Range("J2").Activate() : myExcel.Selection.ColumnWidth = 10
        xlSheet.Columns("K:K").Select : xlSheet.Range("K2").Activate() : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("L:L").Select : xlSheet.Range("L2").Activate() : myExcel.Selection.ColumnWidth = 14

        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext
        End With

        With myExcel.Selection : .HorizontalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext
        End With

        xlSheet.Rows("3:3").Select
        myExcel.ActiveWindow.FreezePanes = False
        myExcel.ActiveWindow.FreezePanes = True
        xlSheet.Range("A1:L3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        xlSheet.Range("A3:L" & N2).Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With

        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With
        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        myExcel.ActiveWindow.DisplayGridlines = False : xlSheet.Range("C3:G9").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Columns("K:K").Select() : xlSheet.Range("K2").Activate() : myExcel.Selection.ColumnWidth = 35 : xlSheet.Rows("2:2").Select
        xlSheet.Range("A1:L1").Select() : myExcel.Selection.Font.Bold = True : xlSheet.Rows("2:2").Select

        With myExcel.Selection.Font
            .Name = "微軟正黑體" : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With

        xlSheet.Range("I3:J9").Select()

        With myExcel.Selection
            .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Columns("K:K").Select : xlSheet.Range("K2").Activate()
        myExcel.Selection.ColumnWidth = 30 : xlSheet.Rows("2:2").Select

        With myExcel.Selection
            .HorizontalAlignment = xlGeneral : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext
        End With
        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext
        End With

        xlSheet.Rows("2:2").Select
        xlSheet.Columns("A:A").Select
        xlSheet.Range("A2").Activate()

        With myExcel.Selection : .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext
        End With

        xlSheet.Range("P6,C:G,I:J").Select() : xlSheet.Range("I2").Activate()
        myExcel.Selection.Style = "Comma" : myExcel.Selection.Style = "Comma"
        myExcel.Selection.NumberFormatLocal = "_-* #,##0.0_-;-* #,##0.0_-;_-* ""-""??_-;_-@_-"
        myExcel.Selection.NumberFormatLocal = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
        xlSheet.Range("A1:L1").Select()

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

        With myExcel.Selection
            .HorizontalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext
        End With
    End Sub
    Private Sub CC(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet)
        xlSheet.Range("H" & N2 + 4 & ":J" & N1).Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With

        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

        With myExcel.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With
        With myExcel.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With
        With myExcel.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With
        With myExcel.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlMedium
        End With
        With myExcel.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
        With myExcel.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin
        End With
    End Sub

    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        供應商_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
        合計_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
        採購清單_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
    End Sub

    Private Sub 應負帳款退回_bt_Click(sender As Object, e As EventArgs) Handles 應負帳款退回_bt.Click
        If 資料流水號_tb.Text <> "" Then
            PA = 資料流水號_tb.Text : PA2 = "0" : PA1 = ""
            SQL_採購控制表_過帳()
        Else
            MsgBox("沒有選擇資料!!!")
        End If
        Set_清單2() : 資料流水號_tb.Text = ""
    End Sub

    Private Sub ComboBox5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox5.TextChanged
        ComboBox2.SelectedIndex = ComboBox5.SelectedIndex : ComboBox3下拉表單資料載入() : ComboBox1下拉表單資料載入()
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.TextChanged
        ComboBox5.SelectedIndex = ComboBox2.SelectedIndex : ComboBox3下拉表單資料載入() : ComboBox1下拉表單資料載入()
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.TextChanged
        ComboBox4.SelectedIndex = ComboBox3.SelectedIndex : ComboBox1下拉表單資料載入()
    End Sub

    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.TextChanged
        ComboBox3.SelectedIndex = ComboBox4.SelectedIndex : ComboBox1下拉表單資料載入()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox6.SelectedIndex = ComboBox1.SelectedIndex
    End Sub

    Private Sub ComboBox6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox6.SelectedIndexChanged
        ComboBox1.SelectedIndex = ComboBox6.SelectedIndex
    End Sub

    Private Sub 開啟供應商資料表_bt_Click(sender As Object, e As EventArgs) Handles 開啟供應商資料表_bt.Click
        PA = "" : 供應商資料表.Show() : 供應商資料表.BringToFront()
    End Sub

    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        PA = Strings.Left(Strings.Format(結束日期_dtp.Value, "yyyy/MM/dd"), 7)
        SQL_採購對帳單_判斷是否有資料()
        If dr.Read() Then : SQL_採購對帳單_刪除當天() : conn.Close() : End If : conn.Close()
        For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
            PA1 = 供應商_dgv.Rows(i).Cells("編號").Value : PA2 = 供應商_dgv.Rows(i).Cells("稅金%").Value
            PA3 = 供應商_dgv.Rows(i).Cells("廣告%").Value : PA4 = 供應商_dgv.Rows(i).Cells("其他扣款").Value
            PA5 = 供應商_dgv.Rows(i).Cells("票期").Value : PA6 = 供應商_dgv.Rows(i).Cells("公司%").Value
            PA7 = 供應商_dgv.Rows(i).Cells("個人%").Value
            SQL_採購對帳單_存檔()
        Next : MsgBox("存檔完成!!") : conn.Close()
    End Sub
    Private Sub 開單_bt_Click(sender As Object, e As EventArgs) Handles 開單_bt.Click
        Dim aa As Integer = 0 : Dim bb As Integer = 0 : Dim cc As Integer = 0
        PA10 = 0 : PA11 = 0
        For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
            PA10 = Val(PA10) + Val(供應商_dgv.Rows(i).Cells("公司").Value.ToString)
            PA11 = Val(PA11) + Val(供應商_dgv.Rows(i).Cells("個人").Value.ToString)
        Next

        If ComboBox6.Text = "" Or 幣別_cb.Text = "" Then
            MsgBox("沒有選擇會計科目或幣別。")
        Else
            If 供應商_dgv.Rows.Count = 0 Then
                MsgBox("沒有任何一筆資料，無法開單")
            Else
                If Val(PA10) + Val(PA11) = 0 Then
                    MsgBox("總金額為0，無法開單")
                Else
                    '-----寫入公帳控制表-------------------------------------
                    開立零用金支付單()
                    '-----寫入公帳流水帳-------------------------------------
                    For i As Integer = 0 To 供應商_dgv.Rows.Count - 1
                        資料數 = 供應商_dgv.Rows.Count : MyModule1.進度條()
                        If Val(供應商_dgv.Rows(i).Cells("公司").Value.ToString) = 0 Then
                        Else
                            PA1 = 供應商_dgv.Rows(i).Cells("供應商").Value : PA2 = 供應商_dgv.Rows(i).Cells("公司").Value : PA3 = 供應商_dgv.Rows(i).Cells("票期").Value
                            Set_日期格式轉換()
                            Set_流水號() : PA = 營運成本收支單號_tb.Text
                            SQL_營運成本收支單申請_查詢最後一筆公帳支付單號()
                            If dr.Read() Then : 新項次 = dr("項次") : Else : 新項次 = "00" : End If : conn.Close()
                            NUM1 = Double.Parse(新項次) + 1
                            If NUM1 < 10 Then : 新項次 = "0" & NUM1
                            ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1
                            End If
                            If 幣別_cb.Text = "台幣現金" Then
                                匯率1 = 1
                            End If
                            PA16 = 匯率1
                            PA15 = 新項次
                            SQL_採購應負帳款_新增公帳流水帳資料()
                            aa += 1
                            conn.Close()
                        End If

                        If Val(供應商_dgv.Rows(i).Cells("個人").Value.ToString) = 0 Then
                        Else
                            PA1 = 供應商_dgv.Rows(i).Cells("供應商").Value : PA2 = 供應商_dgv.Rows(i).Cells("個人").Value : PA3 = 供應商_dgv.Rows(i).Cells("票期").Value
                            Set_日期格式轉換()
                            Set_流水號() : PA = 營運成本收支單號_tb.Text
                            SQL_營運成本收支單申請_查詢最後一筆公帳支付單號()
                            If dr.Read() Then : 新項次 = dr("項次") : Else : 新項次 = "00" : End If : conn.Close()
                            NUM1 = Double.Parse(新項次) + 1
                            If NUM1 < 10 Then : 新項次 = "0" & NUM1
                            ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1
                            End If
                            If 幣別_cb.Text = "台幣現金" Then
                                匯率1 = 1
                            End If
                            PA16 = 匯率1
                            PA15 = 新項次
                            SQL_採購應負帳款_新增公帳流水帳資料1()
                            bb += 1
                            conn.Close()
                        End If
                    Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
                    cc = aa + bb
                    MsgBox("單據轉完成，申請" & cc & "筆。公司" & aa & "筆、個人" & bb & "筆。")
                End If
            End If
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP3 = Strings.Format(開始日期_dtp.Value, "yyyyMMdd") : DTP4 = Strings.Format(開始日期_dtp.Value, "yyyy/MM/dd") : DTP = Strings.Format(日期_dtp.Value, "yyyy/MM/dd")
    End Sub

    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        SQL採購應負帳款_查詢明細()
        If dr.Read() Then : 新支付單號 = dr("支付單號") : Else : 新支付單號 = "" : End If : conn.Close()
        營運成本收支單號_tb.Text = 新支付單號
        SQL採購應負帳款_查詢審核狀態()
        If dr.Read() Then : 狀態 = Strings.Left(dr("審核人"), 2) : Else : 狀態 = "" : End If : conn.Close()
        If (狀態 = "" Or 狀態 = "作廢") Then
            SQL採購轉財務_刪除控制表() : SQL採購轉財務_刪除流水帳() : conn.Close()
            開單_bt.PerformClick()
        Else
            MsgBox("這張單據已經被審核過")
        End If
    End Sub

    Private Sub 開立零用金支付單()
        Dim NUM1 As Integer
        Set_日期格式轉換()
        For i As Integer = 1 To 999
            NUM1 = 0 + i
            If NUM1 < 10 Then : 營運成本收支單號_tb.Text = "OE-" & DTP3 & "-00" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : 營運成本收支單號_tb.Text = "OE-" & DTP3 & "-0" & NUM1
            ElseIf NUM1 > 99 Then : 營運成本收支單號_tb.Text = "OE-" & DTP3 & "-" & NUM1
            End If
            conn.Close()
            PA = 營運成本收支單號_tb.Text
            SQL_營運成本收支單申請_查詢第一筆公帳支付單號()
            If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
        Next
        申請人_tb.Text = gUserName
        SQL_營運成本收支單申請_新增一筆公帳控制表() : conn.Close()
    End Sub
    Private Sub Set_流水號()
        conn.Close() : dr.Close()
        Dim NUM1 As Integer
        For NUM1 = 0 To 99
            Dim d As String : Dim f As String : Dim g As Integer = 1
            If Month(Now) < 10 Then : d = "0" & CStr(Month(Now))
            Else : d = CStr(Month(Now))
            End If
            If Now.Day() < 10 Then : f = "0" & CStr(Now.Day())
            Else : f = CStr(Now.Day())
            End If
            If NUM1 < 10 Then : 新流水1 = "B" & Microsoft.VisualBasic.Right(CStr(Year(Now)), 2) & d & f & "0" & NUM1
            Else : 新流水1 = "B" & Microsoft.VisualBasic.Right(CStr(Year(Now)), 2) & d & f & NUM1
            End If : PA19 = 新流水1
            SQL_營運成本收支單申請_查詢收支單公帳流水號()
            If dr.Read() Then : conn.Close() : Else : conn.Close() : NUM1 = 99 : End If
        Next
    End Sub
End Class