Option Strict Off
Public Class 單據狀態查詢
    Dim 首次開啟 As Boolean = False : ReadOnly ds1, ds2, ds3, ds4 As New DataSet
    Dim EDR As Integer = 0 : Dim ESTR As String
    Private Sub Set_清單1()
        銷售單據_dgv.DataSource = Nothing : ds1.Clear()
        銷售單據_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        銷售單據_dgv.ColumnHeadersHeight = 40
        銷售單據_dgv.AllowUserToAddRows = False
        銷售單據_dgv.RowTemplate.Height = 25

        PA19 = 業務_cb.Text
        SQL_單據狀態查詢_銷售控制表()
        da.Fill(ds1) : 銷售單據_dgv.DataSource = ds1.Tables(0) : conn.Close()

        銷售單據_dgv.Columns(0).Visible = False : 銷售單據_dgv.Columns(1).FillWeight = 70 : 銷售單據_dgv.Columns(2).FillWeight = 120 : 銷售單據_dgv.Columns(3).Visible = False
        銷售單據_dgv.Columns(4).FillWeight = 80 : 銷售單據_dgv.Columns(5).FillWeight = 50 : 銷售單據_dgv.Columns(6).FillWeight = 50 : 銷售單據_dgv.Columns(7).FillWeight = 50
        銷售單據_dgv.Columns(8).FillWeight = 50 : 銷售單據_dgv.Columns(9).FillWeight = 80 : 銷售單據_dgv.Columns(10).FillWeight = 50
        For i As Integer = 0 To 銷售單據_dgv.Rows.Count - 1
            If Strings.Left(銷售單據_dgv("銷售審核", i).Value.ToString, 2) = "作廢" Then
                銷售單據_dgv("銷售審核", i).Value = "作廢"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString <> "" Then
                銷售單據_dgv("銷售審核", i).Value = "已審"
            End If

            If IsDBNull(銷售單據_dgv("轉採購", i).Value) = True Then : 銷售單據_dgv("轉採購", i).Value = False : End If
            If IsDBNull(銷售單據_dgv("已入庫", i).Value) = True Then : 銷售單據_dgv("已入庫", i).Value = False : End If
            If IsDBNull(銷售單據_dgv("已出貨", i).Value) = True Then : 銷售單據_dgv("已出貨", i).Value = False : End If
            If IsDBNull(銷售單據_dgv("已驗證", i).Value) = True Then : 銷售單據_dgv("已驗證", i).Value = False : End If

            If 銷售單據_dgv("銷售審核", i).Value.ToString = "作廢" Then
                銷售單據_dgv("單據狀態", i).Value = "銷售單作廢"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "" Then
                銷售單據_dgv("單據狀態", i).Value = "銷售單已開"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = False And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv("單據狀態", i).Value = "銷售單已審"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv("單據狀態", i).Value = "銷售轉採購"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv("單據狀態", i).Value = "無訂製品單"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv("單據狀態", i).Value = "物料已入庫"
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv("單據狀態", i).Value = "物料已出貨"
            Else
                銷售單據_dgv("單據狀態", i).Value = "資料異常"
            End If

            If 銷售單據_dgv("銷售審核", i).Value.ToString = "作廢" Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Red
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = False And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightCyan
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightSalmon
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And 銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGray
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "" Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.White
            Else
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Red
            End If
        Next
        資料筆數_tb.Text = 銷售單據_dgv.Rows.Count
    End Sub
    Private Sub Set_清單4()
        銷售明細_dgv.DataSource = Nothing : ds4.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

        PA2 = "" : PA2 = 單號_tb.Text
        SQL_單據狀態查詢_銷售明細表()
        da.Fill(ds4) : 銷售明細_dgv.DataSource = ds4.Tables(0) : conn.Close()

        銷售明細_dgv.Columns(0).Visible = False : 銷售明細_dgv.Columns(1).FillWeight = 40 : 銷售明細_dgv.Columns(2).FillWeight = 110 : 銷售明細_dgv.Columns(3).FillWeight = 200
        銷售明細_dgv.Columns(4).FillWeight = 90 : 銷售明細_dgv.Columns(5).FillWeight = 120 : 銷售明細_dgv.Columns(6).FillWeight = 90 : 銷售明細_dgv.Columns(7).FillWeight = 190
        銷售明細_dgv.Columns(8).FillWeight = 50 : 銷售明細_dgv.Columns(9).Visible = False : 銷售明細_dgv.Columns(10).Visible = False : 銷售明細_dgv.Columns(11).Visible = False
        銷售明細_dgv.Columns(12).Visible = False : 銷售明細_dgv.Columns(13).Visible = False : 銷售明細_dgv.Columns(14).Visible = False : 銷售明細_dgv.Columns(15).Visible = False
        銷售明細_dgv.Columns(16).Visible = False : 銷售明細_dgv.Columns(17).FillWeight = 50 : 銷售明細_dgv.Columns(18).Visible = False : 銷售明細_dgv.Columns(19).Visible = False

        銷售明細_dgv.Columns(1).ReadOnly = True : 銷售明細_dgv.Columns(2).ReadOnly = True : 銷售明細_dgv.Columns(3).ReadOnly = True : 銷售明細_dgv.Columns(4).ReadOnly = True
        銷售明細_dgv.Columns(5).ReadOnly = True : 銷售明細_dgv.Columns(6).ReadOnly = True : 銷售明細_dgv.Columns(8).ReadOnly = True : 銷售明細_dgv.Columns(12).ReadOnly = True
        銷售明細_dgv.Columns(13).ReadOnly = True

        銷售明細_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0" : 銷售明細_dgv.Columns("成本").DefaultCellStyle.Format = "#,##0"
        銷售明細_dgv.Columns("單價").DefaultCellStyle.Format = "#,##0.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("售價").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("料號").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        銷售明細_dgv.Columns("料號").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        Dim 記數, 記數1 As Integer
        For i As Integer = 0 To 銷售明細_dgv.Rows.Count - 1
            If 銷售明細_dgv("含稅", i).Value = False Then
                銷售明細_dgv("售價", i).Value = 銷售明細_dgv("數量", i).Value * 銷售明細_dgv("單價", i).Value
            ElseIf 銷售明細_dgv("含稅", i).Value = True Then
                銷售明細_dgv("售價", i).Value = 銷售明細_dgv("數量", i).Value * 銷售明細_dgv("單價", i).Value * 1.05
            End If
            銷售明細_dgv("項次", i).Value = i + 1
            If 銷售明細_dgv("倉名", i).Value.ToString = "工廠品" Then
                記數 = 記數 + 1
            End If
            If Strings.Left(銷售明細_dgv("料號", i).Value.ToString, 4) = "1001" Then : Else
                記數1 = 記數1 + 1
            End If
        Next
        資料筆數4_tb.Text = 記數
        資料筆數2_tb.Text = 記數1
    End Sub
    Private Sub Set_清單2()
        採購清單_dgv.DataSource = Nothing : ds2.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(ds2) : 採購清單_dgv.DataSource = ds2.Tables(0) : conn.Close()

        採購清單_dgv.Columns(0).Visible = False : 採購清單_dgv.Columns(1).Visible = False : 採購清單_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).Visible = False
        採購清單_dgv.Columns(16).Visible = False : 採購清單_dgv.Columns(17).Visible = False : 採購清單_dgv.Columns(18).Visible = False : 採購清單_dgv.Columns(19).Visible = False
        採購清單_dgv.Columns(20).Visible = False : 採購清單_dgv.Columns(21).FillWeight = 50 : 採購清單_dgv.Columns(22).FillWeight = 50 : 採購清單_dgv.Columns(23).FillWeight = 100

        採購清單_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

        資料筆數1_tb.Text = 採購清單_dgv.Rows.Count
    End Sub
    Private Sub Set_清單3()
        明細表_dgv.DataSource = Nothing : ds3.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(ds3) : 明細表_dgv.DataSource = ds3.Tables(0) : conn.Close()

        明細表_dgv.Columns(0).Visible = False : 明細表_dgv.Columns(1).Visible = False : 明細表_dgv.Columns(2).FillWeight = 80 : 明細表_dgv.Columns(3).FillWeight = 220
        明細表_dgv.Columns(4).FillWeight = 70 : 明細表_dgv.Columns(5).FillWeight = 130 : 明細表_dgv.Columns(6).FillWeight = 70 : 明細表_dgv.Columns(7).FillWeight = 220
        明細表_dgv.Columns(8).FillWeight = 70 : 明細表_dgv.Columns(9).FillWeight = 50 : 明細表_dgv.Columns(10).FillWeight = 50 : 明細表_dgv.Columns(11).FillWeight = 90
        明細表_dgv.Columns(12).Visible = False

        明細表_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(0).ReadOnly = True : 明細表_dgv.Columns(1).ReadOnly = True : 明細表_dgv.Columns(2).ReadOnly = True : 明細表_dgv.Columns(3).ReadOnly = True
        明細表_dgv.Columns(4).ReadOnly = True : 明細表_dgv.Columns(5).ReadOnly = True : 明細表_dgv.Columns(6).ReadOnly = True : 明細表_dgv.Columns(7).ReadOnly = True
        明細表_dgv.Columns(8).ReadOnly = True

        資料筆數3_tb.Text = 明細表_dgv.Rows.Count
    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
        Set_清單1()
        If CC(51) = False Then
            退採購_bt.Enabled = False : 銷售轉採購_bt.Enabled = False : 銷售單退回_bt.Enabled = False : 銷售單退出庫狀態_bt.Enabled = False : 刪除_bt.Enabled = False
            銷售單進採購狀態_bt.Enabled = False : 銷售單進出庫狀態_bt.Enabled = False : 單據驗證跳過_bt.Enabled = False : 驗證計算_bt.Enabled = False
            銷售明細退庫_bt.Enabled = False : 銷售明細退出庫_bt.Enabled = False : 採購單退回_批量_bt.Enabled = False : 出庫單退回_批量_bt.Enabled = False
        End If
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
            Set_清單2() : Set_清單3() : Set_清單4()
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 7.6
            Set_清單2() : Set_清單3() : Set_清單4()
        End If
        首次開啟 = True
    End Sub
    Private Sub 單據狀態查詢_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If 首次開啟 = True Then
            If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
                字體_NUD.Value = 9
                Set_清單2() : Set_清單3() : Set_清單4()
            ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
                字體_NUD.Value = 7.6
                Set_清單2() : Set_清單3() : Set_清單4()
            End If
        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.Rows(e.RowIndex).Cells("資料流水號").Value
        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.Rows(e.RowIndex).Cells("資料流水號").Value
        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.Rows(e.RowIndex).Cells("料號").Value : 物料狀態_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("物料狀態").Value
            訂製狀態_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("訂製狀態").Value : 倉儲狀態_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("倉儲狀態").Value
            備註_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("備註").Value : 數量_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("數量").Value
            成本_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("成本").Value : 資料流水號_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("資料流水號").Value
            採購日期_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("審核日期").Value : 交貨日期_tb.Text = 銷售明細_dgv.Rows(e.RowIndex).Cells("交貨日期").Value
        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.Rows(e.RowIndex).Cells("單號").Value : 轉採購_ch.Checked = 銷售單據_dgv.Rows(e.RowIndex).Cells("轉採購").Value
            已入庫_ch.Checked = 銷售單據_dgv.Rows(e.RowIndex).Cells("已入庫").Value : 已出貨_ch.Checked = 銷售單據_dgv.Rows(e.RowIndex).Cells("已出貨").Value
            Set_清單4() : Set_清單2() : Set_清單3()
        End If
    End Sub
    Private Sub 銷售單據_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 銷售單據_dgv.RowPostPaint
        For i As Integer = 0 To 銷售單據_dgv.Rows.Count - 1
            If 銷售單據_dgv("銷售審核", i).Value.ToString = "作廢" Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Red
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = False And
                       銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And
                       銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightCyan
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And
                       銷售單據_dgv("已入庫", i).Value = False And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And
                       銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = False Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightSalmon
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "已審" And 銷售單據_dgv("轉採購", i).Value = True And
                       銷售單據_dgv("已入庫", i).Value = True And 銷售單據_dgv("已出貨", i).Value = True Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGray
            ElseIf 銷售單據_dgv("銷售審核", i).Value.ToString = "" Then
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.White
            Else
                銷售單據_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Red
            End If
        Next
        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 業務_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 業務_cb.SelectedIndexChanged
        Set_清單1()
    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_清單4() : Set_清單2() : Set_清單3()
    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_清單4() : Set_清單2() : Set_清單3()
    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_清單4() : Set_清單2() : Set_清單3()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        銷售明細_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 銷售單退回_bt_Click(sender As Object, e As EventArgs) Handles 銷售單退回_bt.Click
        PA = 採購流水_tb.Text : SQL_採購控制表_刪除() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
    End Sub
    Private Sub 採購單退回_批量_bt_Click(sender As Object, e As EventArgs) Handles 採購單退回_批量_bt.Click
        Dim aa1 As MsgBoxResult = MsgBox("是否要執行批量退採?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            For I As Integer = 0 To 採購清單_dgv.Rows.Count - 1
                採購流水_tb.Text = 採購清單_dgv.Rows(I).Cells("資料流水號").Value
                PA = 採購流水_tb.Text : SQL_採購控制表_刪除()
            Next
            Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        PA20 = 倉儲流水_tb.Text : SQL_倉儲控制表_刪除明細() : PA20 = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        If 已出貨_ch.Checked = True Then
            Dim aa1 As MsgBoxResult = MsgBox("銷售單是否要跟著退庫?", MsgBoxStyle.OkCancel)
            If aa1 = MsgBoxResult.Ok Then
                銷售單退出庫狀態_bt.PerformClick()
            End If
        End If
    End Sub
    Private Sub 出庫單退回_批量_bt_Click(sender As Object, e As EventArgs) Handles 出庫單退回_批量_bt.Click
        Dim aa1 As MsgBoxResult = MsgBox("是否要執行批量退庫?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            For I As Integer = 0 To 明細表_dgv.Rows.Count - 1
                倉儲流水_tb.Text = 明細表_dgv.Rows(I).Cells("資料流水號").Value
                PA20 = 倉儲流水_tb.Text : SQL_倉儲控制表_刪除明細()
            Next
            PA = 單號_tb.Text : SQL_銷售控制表_出貨返回() : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        End If
    End Sub
    Private Sub 銷售單進出庫狀態_bt_Click(sender As Object, e As EventArgs) Handles 銷售單進出庫狀態_bt.Click
        If 單號_tb.Text = "" Then : MsgBox("沒已選則單號!!") : Else
            PA = 單號_tb.Text : SQL_銷售控制表_出貨修改() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("修改完成!!!")
        End If
    End Sub
    Private Sub 銷售單退出庫狀態_bt_Click(sender As Object, e As EventArgs) Handles 銷售單退出庫狀態_bt.Click
        If 單號_tb.Text = "" Then : MsgBox("沒已選則單號!!") : Else
            PA = 單號_tb.Text : SQL_銷售控制表_出貨返回() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        End If
    End Sub
    Private Sub 銷售明細退出庫_bt_Click(sender As Object, e As EventArgs) Handles 銷售明細退出庫_bt.Click
        Dim aa1 As MsgBoxResult = MsgBox("確認退單前要確定出庫單中無資料?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            PA = 單號_tb.Text : SQL_銷售明細_出貨返回() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        End If
    End Sub
    Private Sub 銷售單進採購狀態_bt_Click(sender As Object, e As EventArgs) Handles 銷售單進採購狀態_bt.Click
        If 單號_tb.Text = "" Then : MsgBox("沒已選則單號!!") : Else
            PA = 單號_tb.Text : SQL_銷售控制表_轉採購修改() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("修改完成!!!")
        End If
    End Sub
    Private Sub 單據驗證跳過_bt_Click(sender As Object, e As EventArgs) Handles 單據驗證跳過_bt.Click
        If 單號_tb.Text = "" Then : MsgBox("沒已選則單號!!") : Else
            If 已出貨_ch.Checked = True And 轉採購_ch.Checked = True Then
                PA = 單號_tb.Text : SQL_銷售控制表_驗證跳過() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("修改完成!!!")
            Else : MsgBox("單據還未走完流程!!!") : End If
        End If
    End Sub
    Private Sub 銷售明細退庫_bt_Click(sender As Object, e As EventArgs) Handles 銷售明細退庫_bt.Click
        PA = 單號_tb.Text : PA1 = 料號_tb.Text : SQL_銷售明細_出貨返回_單筆() : PA = "" : PA1 = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
    End Sub
    Private Sub 退採購_bt_Click(sender As Object, e As EventArgs) Handles 退採購_bt.Click
        If 單號_tb.Text = "" Then : MsgBox("沒已選則單號!!") : Else
            PA = 單號_tb.Text : SQL_銷售控制表_轉採購返回() : PA = "" : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("退單完成!!!")
        End If
    End Sub
    Private Sub 驗證計算_bt_Click(sender As Object, e As EventArgs) Handles 驗證計算_bt.Click
        Dim 錯誤資訊, 錯誤資訊1 As String : Dim 採購單 As Integer
        資料數 = 銷售單據_dgv.Rows.Count : 錯誤資訊 = "錯誤資訊如下 : "
        For i As Integer = 0 To 銷售單據_dgv.Rows.Count - 1
            MyModule1.進度條()
            If 銷售單據_dgv.Rows(i).Cells("已驗證").Value = False Then
                單號_tb.Text = 銷售單據_dgv.Rows(i).Cells("單號").Value
                Set_清單4() : Set_清單2() : Set_清單3()
                Dim 驗證 As Boolean = False
                If 銷售單據_dgv.Rows(i).Cells("轉採購").Value = True And 銷售明細_dgv.Rows.Count = 0 Then
                    錯誤資訊1 = 單號_tb.Text & " - [銷售單查無資料]!!" : 驗證 = True
                    錯誤資訊 = 錯誤資訊 & vbCrLf &
                                   錯誤資訊1
                End If
                If 銷售單據_dgv.Rows(i).Cells("已出貨").Value = True And Val(資料筆數2_tb.Text) * 2 <> Val(資料筆數3_tb.Text) Then
                    錯誤資訊1 = 單號_tb.Text & " - [出庫單資料異常]!!" : 驗證 = True
                    錯誤資訊 = 錯誤資訊 & vbCrLf &
                                   錯誤資訊1
                End If
                採購單 = 0
                For II As Integer = 0 To 銷售明細_dgv.Rows.Count - 1
                    If 銷售明細_dgv.Rows(II).Cells("倉名").Value = "工廠品" Then
                        採購單 += 1
                    End If
                Next
                If 採購單 <> Val(資料筆數1_tb.Text) Then
                    錯誤資訊1 = 單號_tb.Text & " - [採購單資料異常]!!" : 驗證 = True
                    錯誤資訊 = 錯誤資訊 & vbCrLf &
                                   錯誤資訊1
                End If
                If 驗證 = False And 銷售單據_dgv.Rows(i).Cells("轉採購").Value = True And
                        銷售單據_dgv.Rows(i).Cells("已出貨").Value = True Then
                    PA = 單號_tb.Text : SQL_銷售控制表_驗證跳過() : PA = ""
                End If
            End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : Set_清單1() : Set_清單4() : Set_清單2() : Set_清單3()
        If 錯誤資訊 = "錯誤資訊如下 : " Then : 錯誤資訊 = "沒有錯誤資料" : End If : MsgBox(錯誤資訊)
    End Sub
    Private Sub 銷售轉採購_bt_Click(sender As Object, e As EventArgs) Handles 銷售轉採購_bt.Click
        Dim aa As MsgBoxResult = MsgBox("確定要補銷售單?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            SQL_採購控制表_最後一筆資料()
            If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("資料流水號").ToString, 9)) : Else : EDR = 0 : End If
            conn.Close() : EDR += 1
            If EDR < 10 Then : ESTR = "PU" & "00000000" & EDR
            ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PU" & "0000000" & EDR
            ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PU" & "000000" & EDR
            ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PU" & "00000" & EDR
            ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PU" & "0000" & EDR
            ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PU" & "000" & EDR
            ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PU" & "00" & EDR
            ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "PU" & "0" & EDR
            ElseIf EDR > 99999999 Then : ESTR = "PU" & EDR : End If

            PA1 = ESTR : PA2 = 單號_tb.Text : PA3 = Strings.Left(料號_tb.Text, 4) : PA4 = 料號_tb.Text
            PA5 = 物料狀態_tb.Text : PA6 = 訂製狀態_tb.Text : PA7 = 倉儲狀態_tb.Text
            PA8 = 備註_tb.Text : NU1 = 數量_tb.Text : NU2 = 成本_tb.Text
            NU3 = NU1 * NU2 : PA9 = gUserName : PA10 = Format(Today(), "yyyy/MM/dd") : PA11 = 採購日期_tb.Text : PA12 = 交貨日期_tb.Text : BL1 = True
            PA13 = 資料流水號_tb.Text
            SQL_採購控制表_新增() : Set_清單4() : Set_清單2() : Set_清單3() : MsgBox("補銷售單完成!!!")
        End If : conn.Close()
    End Sub
End Class