Imports System.IO
Imports System.Windows.Forms.DataVisualization.Charting
Public Class 採購單初級審核
    Dim NUM1, II, 選擇項 As Integer
    Dim 新項次, 物料規格, QQW(11) As String
    Dim 單據總價 As Long
    Private Sub Set_單價()
        Dim ds6 As New DataSet : 價錢_dgv.DataSource = Nothing : ds6.Clear()
        價錢_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        價錢_dgv.ColumnHeadersHeight = 25
        價錢_dgv.AllowUserToAddRows = False

        If 日期_rb.Checked = False And 單價_rb.Checked = True Then : SQL_加權平均()
        ElseIf 日期_rb.Checked = True And 單價_rb.Checked = False Then : SQL_最近日期() : End If
        da.Fill(ds6) : 價錢_dgv.DataSource = ds6.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_採購單清單()
        Dim ds As New DataSet : 明細_dgv.DataSource = Nothing : ds.Clear()
        明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        明細_dgv.ColumnHeadersHeight = 45
        明細_dgv.AllowUserToAddRows = False
        If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then : 明細_dgv.RowTemplate.Height = 65
        ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then : 明細_dgv.RowTemplate.Height = 45
        ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then : 明細_dgv.RowTemplate.Height = 25 : End If

        PA10 = 採購單號_tb.Text : SQL_採購單明細表()
        da.Fill(ds) : 明細_dgv.DataSource = ds.Tables(0) : conn.Close()

        明細_dgv.Columns(0).Visible = False : 明細_dgv.Columns(1).FillWeight = 50 : 明細_dgv.Columns(3).FillWeight = 250 : 明細_dgv.Columns(4).FillWeight = 250
        明細_dgv.Columns(5).FillWeight = 150 : 明細_dgv.Columns(6).FillWeight = 80 : 明細_dgv.Columns(7).FillWeight = 80 : 明細_dgv.Columns(8).FillWeight = 80
        明細_dgv.Columns(9).FillWeight = 80 : 明細_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).Visible = False : 明細_dgv.Columns(18).Visible = False : 明細_dgv.Columns(19).Visible = False : 明細_dgv.Columns(20).Visible = False
        明細_dgv.Columns(21).Visible = False : 明細_dgv.Columns(22).Visible = False : 明細_dgv.Columns(23).Visible = False : 明細_dgv.Columns(24).FillWeight = 90
        明細_dgv.Columns(25).FillWeight = 80 : 明細_dgv.Columns("KEY").Visible = False : 明細_dgv.Columns("OUP").FillWeight = 80 : 明細_dgv.Columns(2).FillWeight = 80
        明細_dgv.Columns(26).FillWeight = 80 : 明細_dgv.Columns("SP%").FillWeight = 80
        明細_dgv.Columns(6).DefaultCellStyle.Format = "#,##0" : 明細_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 明細_dgv.Columns(9).DefaultCellStyle.Format = "#,##0"
        明細_dgv.Columns("OUP").DefaultCellStyle.Format = "#,##0" : 明細_dgv.Columns(15).DefaultCellStyle.Format = "#,##0"
        明細_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns("OUP").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns("OUP").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        明細_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        明細_dgv.Columns(1).ReadOnly = True : 明細_dgv.Columns(2).ReadOnly = True : 明細_dgv.Columns(3).ReadOnly = True : 明細_dgv.Columns(4).ReadOnly = True
        明細_dgv.Columns("單位").ReadOnly = True : 明細_dgv.Columns("金額").ReadOnly = True : 明細_dgv.Columns("OUP").ReadOnly = True : 明細_dgv.Columns("SP%").ReadOnly = True

        If 低網速模式 = False Then
            明細_dgv.Columns(28).FillWeight = 80 : 明細_dgv.Columns("Image").Visible = True
            Dim mycol As DataGridViewImageColumn = 明細_dgv.Columns("Image")
            mycol.ImageLayout = DataGridViewImageCellLayout.Zoom
        Else
            If 明細_dgv.Columns.Count = 29 Then
                明細_dgv.Columns("Image").Visible = False
            End If
        End If
        新項次 = "00"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            明細_dgv.Columns("OUP").DefaultCellStyle.Format = "#,0"
            NUM1 = Double.Parse(新項次) + 1
            If NUM1 < 10 Then : 新項次 = "0" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1
            End If
            明細_dgv.Rows(i).Cells("No").Value = 新項次

            '-------------------------規格合併呈現----------------------------------------------------------------------------------------------------------------------------------------
            If i = 0 Then
                AAA1 = "" : AAA2 = "" : AAA3 = "" : AAA4 = "" : AAA5 = "" : AAA6 = "" : AAA7 = "" : AAA8 = ""
                PA3 = 明細_dgv.Rows(i).Cells("規格庫抬頭指定").Value
                SQL_規格庫抬頭讀取()
                If dr.Read() Then
                    AAA1 = dr("A1") : AAA2 = dr("A2") : AAA3 = dr("A3") : AAA4 = dr("A4") : AAA5 = dr("A5") : AAA6 = dr("A6") : AAA7 = dr("A7") : AAA8 = dr("A8")
                End If
                conn.Close()
            Else
                If 明細_dgv.Rows(i).Cells("規格庫抬頭指定").Value.ToString <> 明細_dgv.Rows(i - 1).Cells("規格庫抬頭指定").Value.ToString Then
                    AAA1 = "" : AAA2 = "" : AAA3 = "" : AAA4 = "" : AAA5 = "" : AAA6 = "" : AAA7 = "" : AAA8 = ""
                    PA3 = 明細_dgv.Rows(i).Cells("規格庫抬頭指定").Value
                    SQL_規格庫抬頭讀取()
                    If dr.Read() Then
                        AAA1 = dr("A1") : AAA2 = dr("A2") : AAA3 = dr("A3") : AAA4 = dr("A4") : AAA5 = dr("A5") : AAA6 = dr("A6") : AAA7 = dr("A7") : AAA8 = dr("A8")
                    End If
                    conn.Close()
                End If
            End If
            Dim ZA1, ZA2, ZA3, ZA4, ZA5, ZA6, ZA7, ZA8 As String : 物料規格 = ""
            If 詳細資料2_ch.Checked = True Then
                Dim columns() As String = {"規格", "A2", "A3", "A4", "A5", "A6", "A7", "A8"}
                Dim AAAs() As String = {"AAA1", "AAA2", "AAA3", "AAA4", "AAA5", "AAA6", "AAA7", "AAA8"}
                物料規格 = ""
                For j As Integer = 0 To columns.Length - 1
                    Dim cellValue As String = 明細_dgv.Rows(i).Cells(columns(j)).Value.ToString()
                    If cellValue <> "" Then
                        Dim ZA As String = AAAs(j) & "=" & cellValue
                        If 物料規格 = "" Then : 物料規格 = ZA : Else : 物料規格 &= "、" & ZA : End If
                    End If
                Next
            Else
                If 明細_dgv.Rows(i).Cells("規格").Value.ToString <> "" Then : ZA1 = 明細_dgv.Rows(i).Cells("規格").Value.ToString : 物料規格 = ZA1 : End If
                For j As Integer = 2 To 8
                    Dim cellName As String = "A" & j
                    If 明細_dgv.Rows(i).Cells(cellName).Value.ToString <> "" Then
                        Select Case j
                            Case 2 : ZA2 = 明細_dgv.Rows(i).Cells(cellName).Value : Case 3 : ZA3 = 明細_dgv.Rows(i).Cells(cellName).Value
                            Case 4 : ZA4 = 明細_dgv.Rows(i).Cells(cellName).Value : Case 5 : ZA5 = 明細_dgv.Rows(i).Cells(cellName).Value
                            Case 6 : ZA6 = 明細_dgv.Rows(i).Cells(cellName).Value : Case 7 : ZA7 = 明細_dgv.Rows(i).Cells(cellName).Value
                            Case 8 : ZA8 = 明細_dgv.Rows(i).Cells(cellName).Value
                        End Select
                        If 物料規格 = "" Then : 物料規格 = 明細_dgv.Rows(i).Cells(cellName).Value
                        Else : 物料規格 = 物料規格 + "、" + 明細_dgv.Rows(i).Cells(cellName).Value : End If
                    End If
                Next
            End If
            明細_dgv.Rows(i).Cells("規格").Value = 物料規格
            '--------------取得歷史單價------------------------------------------------------------------------------------------
            PA = 明細_dgv.Rows(i).Cells("料號").Value
            Set_單價()
            If 價錢_dgv.Rows.Count = 0 Then : 明細_dgv.Rows(i).Cells("OUP").Value = 0 : Else
                明細_dgv.Rows(i).Cells("OUP").Value = 價錢_dgv.Rows(0).Cells("單價").Value
            End If
        Next i
        價格() : 總金額計算()
    End Sub
    Private Sub Set_供應商資料()
        Dim ds1 As New DataSet : 供應商資料_dgv.DataSource = Nothing : ds1.Clear()
        供應商資料_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        供應商資料_dgv.ColumnHeadersHeight = 40 : 供應商資料_dgv.RowTemplate.Height = 30 : 供應商資料_dgv.AllowUserToAddRows = False
        SQL2 = "" : SQL_採購一級核准_供應商清單() : da.Fill(ds1) : 供應商資料_dgv.DataSource = ds1.Tables(0) : conn.Close()
        供應商1_cb.Items.Clear() : 供應商1_cb.Items.Add("全部的供應商")
        For i As Integer = 0 To 供應商資料_dgv.Rows.Count - 1 : 供應商1_cb.Items.Add(供應商資料_dgv.Rows(i).Cells(1).Value.ToString) : Next
    End Sub
    Private Sub 公司類別_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 公司類別_cb.SelectedIndexChanged
        Set_供應商資料() : CB選擇清單(供應商1_cb, 供應商1_cb.Text) : Set_清單2()
    End Sub
    Private Sub 價格()
        Dim 差異, 比對 As Double
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            If 明細_dgv.Rows(i).Cells("OUP").Value = 0 And 明細_dgv.Rows(i).Cells("單價").Value = 0 Then
                明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Lavender
                明細_dgv.Rows(i).Cells("SP%").Value = "0 % →"
            ElseIf 明細_dgv.Rows(i).Cells("單價").Value = 0 And 明細_dgv.Rows(i).Cells("OUP").Value <> 0 Then
                明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.Lavender
                明細_dgv.Rows(i).Cells("SP%").Value = "0 % →"
            ElseIf 明細_dgv.Rows(i).Cells("OUP").Value <> 0 And 明細_dgv.Rows(i).Cells("單價").Value <> 0 Then
                差異 = 明細_dgv.Rows(i).Cells("單價").Value - 明細_dgv.Rows(i).Cells("OUP").Value : 比對 = 差異 / 明細_dgv.Rows(i).Cells("單價").Value * 100
                If 比對 > 0 Then : 明細_dgv.Rows(i).Cells("SP%").Value = CInt(比對) & " % ↗" : 比對 *= 1
                ElseIf 比對 < 0 Then : 明細_dgv.Rows(i).Cells("SP%").Value = CInt(比對) & " % ↘" : 比對 *= -1
                ElseIf 比對 = 0 Then : 明細_dgv.Rows(i).Cells("SP%").Value = CInt(比對) & " % →" : End If
                If 比對 < 比例1 Then : 明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.White
                ElseIf 比對 > 比例1 And 比對 < 比例2 Then : 明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
                ElseIf 比對 > 比例2 And 比對 < 比例3 Then : 明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
                ElseIf 比對 >= 比例3 Then : 明細_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink : End If
            End If
        Next
    End Sub
    Private Sub 總金額計算()
        Dim FFGG As Long = 0
        總金額_tb.Text = "0"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            明細_dgv.Rows(i).Cells("金額").Value = 明細_dgv.Rows(i).Cells("單價").Value * 明細_dgv.Rows(i).Cells("數量").Value
            FFGG = CLng(FFGG) + 明細_dgv.Rows(i).Cells("金額").Value
        Next i
        折扣金額_tb.Text = CLng(折扣比例_tb.Text) * FFGG / 100
        If 稅金_ch.Checked = True Then
            If 折扣比例_tb.Text = "0" Then
                稅金金額_tb.Text = FFGG * CLng(稅金比例_tb.Text) / 100
            Else
                稅金金額_tb.Text = (FFGG - 折扣金額_tb.Text) * CLng(稅金比例_tb.Text) / 100
            End If
        Else
            稅金金額_tb.Text = "0"
        End If
        總金額_tb.Text = FFGG - CLng(折扣金額_tb.Text) + CLng(稅金金額_tb.Text)
        折扣金額_tb.Text = Format(CLng(折扣金額_tb.Text), "#,##0") : 稅金金額_tb.Text = Format(CLng(稅金金額_tb.Text), "#,##0")
        總金額_tb.Text = Format(CLng(總金額_tb.Text), "#,##0")
    End Sub
    Private Sub Set_清單2()
        Dim ds2 As New DataSet : 單據_dgv.DataSource = Nothing : ds2.Clear()
        單據_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        單據_dgv.ColumnHeadersHeight = 25 : 單據_dgv.AllowUserToAddRows = False
        Dim 供應商編號 As String
        If 供應商1_cb.Text = "全部的供應商" Then : 供應商編號 = "%%" : Else
            For i As Integer = 0 To 供應商資料_dgv.Rows.Count - 1
                If 供應商1_cb.Text = 供應商資料_dgv.Rows(i).Cells(1).Value.ToString Then
                    供應商編號 = 供應商資料_dgv.Rows(i).Cells(0).Value.ToString : Exit For
                End If
            Next
        End If
        SQL_建築物料採購單初級核准_清單(供應商編號)
        da.Fill(ds2) : 單據_dgv.DataSource = ds2.Tables(0) : conn.Close()
        單據_dgv.Columns(21).FillWeight = 350 : 單據_dgv.Columns(24).Visible = False : 單據_dgv.Columns("已會計").Visible = False : 單據_dgv.Columns("已出納").Visible = False
        For i As Integer = 0 To 20 : 單據_dgv.Columns(i).Visible = False : Next
    End Sub
    Private Sub Set_清單3()
        Dim ds1 As New DataSet : 最新單價_dgv.DataSource = Nothing : ds1.Clear()
        最新單價_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        最新單價_dgv.ColumnHeadersHeight = 25 : 最新單價_dgv.AllowUserToAddRows = False
        PA14 = 供應商編碼_tb.Text : SQL_物料最新單價查詢()
        da.Fill(ds1) : 最新單價_dgv.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單()
        Dim ds1 As New DataSet : 簽名檔_dgv.DataSource = Nothing : ds1.Clear()
        簽名檔_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        簽名檔_dgv.ColumnHeadersHeight = 25 : 簽名檔_dgv.AllowUserToAddRows = False

        SQL_主管簽名檔管理()
        da.Fill(ds1) : 簽名檔_dgv.DataSource = ds1.Tables(0) : conn.Close()

        簽名檔_dgv.Columns(0).FillWeight = 173 : 簽名檔_dgv.Columns(1).Visible = False
        If 簽名檔_dgv.Rows.Count = 0 Then : Else
            圖片編碼_tb.Text = 簽名檔_dgv(0, 0).Value.ToString
            PA1 = 圖片編碼_tb.Text : 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

            For QAZ = 0 To 999
                QQW(0) = Strings.Right(簽名檔_dgv(0, 0).Value.ToString, 2) : QQW(1) = Strings.Right(Year(Today), 2) : QQW(2) = Strings.Left(Year(Today), 2) : QQW(3) = Month(Today) + 9
                QQW(4) = Strings.Right(QQW(0), 1) : QQW(5) = Strings.Left(QQW(0), 1) : QQW(6) = Strings.Right(QQW(1), 1) : QQW(7) = Strings.Left(QQW(1), 1) : QQW(8) = Strings.Right(QQW(2), 1)
                QQW(9) = Strings.Left(QQW(2), 1) : QQW(10) = Strings.Right(QQW(3), 1) : QQW(11) = Strings.Left(QQW(3), 1)
                For RT = 4 To 11
                    Dim ran = New Random(DateTime.Now.Millisecond) : Dim NUM1 As Integer = ran.Next(1, 4096)
                    For I = 0 To 4096 : If NUM1 > 9 Then : NUM1 -= 9 : ElseIf NUM1 < 9 Then : I = 4096 : End If : Next
                    If NUM1 = 1 Then : II = RT : 轉換() : ElseIf NUM1 = 2 Then : II = RT : 轉換() : ElseIf NUM1 = 3 Then : ElseIf NUM1 = 4 Then : II = RT : 轉換() : ElseIf NUM1 = 5 Then
                    ElseIf NUM1 = 6 Then : II = RT : 轉換() : ElseIf NUM1 = 7 Then : ElseIf NUM1 = 8 Then : II = RT : 轉換() : ElseIf NUM1 = 9 Then : II = RT : 轉換() : End If
                Next

                Dim ran1 = New Random(DateTime.Now.Millisecond) : Dim NUM2 As Integer = ran1.Next(1, 4096)
                For I = 0 To 4096 : If NUM2 > 99 Then : NUM2 -= 49 : ElseIf NUM2 < 99 Then : I = 4096 : End If : Next

                驗證碼_tb.Text = QQW(11) & "2" & QQW(9) & "7" & QQW(7) & "0" & QQW(5) & QQW(4) & "1" & QQW(6) & "3" & QQW(8) & "9" & QQW(10) & NUM2

                PA20 = 驗證碼_tb.Text : SQL_建築驗證碼()
                If dr.Read() = True Then : Else : QAZ = 999 : End If : conn.Close()
            Next
            PA20 = 驗證碼_tb.Text : SQL_建築驗證碼()
            If dr.Read() = True Then : MsgBox("沒有生成可用亂數，請找系統管理員!!") : Exit Sub : End If
        End If
    End Sub
    Private Sub 轉換()
        If QQW(II) = "1" Then : QQW(II) = "A" : ElseIf QQW(II) = "2" Then : QQW(II) = "B" : ElseIf QQW(II) = "3" Then : QQW(II) = "C" : ElseIf QQW(II) = "4" Then : QQW(II) = "D"
        ElseIf QQW(II) = "5" Then : QQW(II) = "E" : ElseIf QQW(II) = "6" Then : QQW(II) = "F" : ElseIf QQW(II) = "7" Then : QQW(II) = "G" : ElseIf QQW(II) = "8" Then : QQW(II) = "H"
        ElseIf QQW(II) = "9" Then : QQW(II) = "I" : ElseIf QQW(II) = "0" Then : QQW(II) = "J"
        End If
    End Sub
    Private Sub 供應商_cb_下拉表單資料載入()
        SQL_供應商資料表_公司名_下拉用() : 供應商_cb.Items.Clear() : While (dr.Read()) : 供應商_cb.Items.Add(dr("公司名")) : End While : conn.Close()
    End Sub
    Private Sub Set_料號比價()
        Dim ds1 As New DataSet
        料號_dgv.DataSource = Nothing : ds1.Clear()
        料號_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        料號_dgv.ColumnHeadersHeight = 25 : 料號_dgv.AllowUserToAddRows = False
        PA = 料號1_tb.Text : SQL_料號比對價格()
        da.Fill(ds1) : 料號_dgv.DataSource = ds1.Tables(0) : conn.Close()
        料號_dgv.Columns("排序").Visible = False : 料號_dgv.Columns(3).DefaultCellStyle.Format = "#,##0.00" : 料號_dgv.Columns(3).FillWeight = 50
    End Sub
    Private Sub 核准_bt_MouseEnter(sender As Object, e As EventArgs) Handles 核准_bt.MouseEnter
        ToolTip1.SetToolTip(Me.核准_bt, "核准")
    End Sub
    Private Sub 作廢_bt_MouseEnter(sender As Object, e As EventArgs) Handles 作廢_bt.MouseEnter
        ToolTip1.SetToolTip(Me.作廢_bt, "作廢")
    End Sub
    Private Sub 存檔_bt_MouseEnter(sender As Object, e As EventArgs) Handles 存檔_bt.MouseEnter
        ToolTip1.SetToolTip(Me.存檔_bt, "存檔")
    End Sub
    Private Sub 刪除一筆選中的細項資料_bt_MouseEnter(sender As Object, e As EventArgs) Handles 刪除一筆選中的細項資料_bt.MouseEnter
        ToolTip1.SetToolTip(Me.刪除一筆選中的細項資料_bt, "刪除一筆選中的細項資料")
    End Sub
    Private Sub 料號_bt_MouseEnter(sender As Object, e As EventArgs) Handles 料號_bt.MouseEnter
        ToolTip1.SetToolTip(Me.料號_bt, "價格分析")
    End Sub
    Private Sub 採購單初級審核_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = FH_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        單價_rb.Checked = True : Panel3.SendToBack()
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False : ToolTip1.ToolTipTitle = "系統說明"
        Label4.Text = "森林綠代表漲幅大於" & 比例1 & "小於" & 比例2 & "。淡藍色代表漲幅大於" & 比例2 & "小於" & 比例3 & "。紅色代表漲幅大於" & 比例3
        採購單一級核准_Size()
    End Sub
    Private Sub 採購單一級核准_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        計算分流01.Enabled = True
    End Sub
    Private Sub 計算分流01_Tick(sender As Object, e As EventArgs) Handles 計算分流01.Tick
        計算分流01.Enabled = False : 公司類別_cb.SelectedIndex = 0
        Set_供應商資料() : 供應商1_cb.Text = "全部的供應商"
        供應商_cb_下拉表單資料載入() : Set_清單2() : Set_清單()
        If 驗證碼_tb.Text = "" Or 圖片編碼_tb.Text = "" Then : End If
    End Sub
    Private Sub 採購單一級核准_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        採購單一級核准_Size()
    End Sub
    Private Sub 採購單一級核准_Size()
        簽名檔_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6) : 明細_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        單據_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6) : 料號_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 單價_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 單價_rb.Click
        Set_單價()
    End Sub
    Private Sub 日期_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 日期_rb.Click
        Set_單價()
    End Sub
    Private Sub 明細_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 明細_dgv.CellEnter
        價格() : 總金額計算()
    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 : 通知訊息 = 明細_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 = 8 Then
                If 已超出 = False Then
                    Dim x As Double = 0
                    For i As Integer = 0 To 明細_dgv.SelectedCells.Count - 1
                        Dim 變數 As String = "" : Dim 取變數 As String
                        取變數 = 明細_dgv.Rows(明細_dgv.SelectedCells.Item(i).RowIndex).Cells(明細_dgv.SelectedCells.Item(i).ColumnIndex).Value
                        For ii As Integer = 1 To Len(取變數)
                            If ii = 1 Then : 變數 = Strings.Left(取變數, ii) : Else : If Strings.Mid(取變數, ii, 1) <> "," Then : 變數 &= Strings.Mid(取變數, ii, 1) : End If : End If
                        Next : x += Val(變數)
                    Next i : FH_ERP_SYS.合計_TSSL.Text = "Total : " & Strings.Format(x, "#,##0")
                Else : FH_ERP_SYS.合計_TSSL.Text = "Total : " & Strings.Format(0, "#,##0")
                End If
            Else : 已超出 = True : FH_ERP_SYS.合計_TSSL.Text = "Total : " & Strings.Format(0, "#,##0")
            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("項次01").Value : PA1 = 明細_dgv.Rows(e.RowIndex).Cells("群組碼").Value : PA2 = 明細_dgv.Rows(e.RowIndex).Cells("料號原則").Value
            編號_tb.Text = 明細_dgv.Rows(e.RowIndex).Cells("No").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)
                物料圖檔_pb.Image = Bitmap.FromStream(oStream)
            End While : conn.Close() : 物料圖檔_pb.SizeMode = 4
            料號1_tb.Text = 明細_dgv("料號", e.RowIndex).Value.ToString
            品名_tb.Text = 明細_dgv("品名", e.RowIndex).Value.ToString
            If 歷史單價_P.Visible = True Then : Set_料號比價() : 繪圖() : 料號_dgv.Sort(料號_dgv.Columns("排序"), System.ComponentModel.ListSortDirection.Descending) : 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(0, e.RowIndex).Value.ToString
            PA1 = 圖片編碼_tb.Text : 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

            For QAZ = 0 To 999
                QQW(0) = Strings.Right(簽名檔_dgv(0, e.RowIndex).Value.ToString, 2) : QQW(1) = Strings.Right(Year(Today), 2) : QQW(2) = Strings.Left(Year(Today), 2) : QQW(3) = Month(Today) + 9
                QQW(4) = Strings.Right(QQW(0), 1) : QQW(5) = Strings.Left(QQW(0), 1) : QQW(6) = Strings.Right(QQW(1), 1) : QQW(7) = Strings.Left(QQW(1), 1) : QQW(8) = Strings.Right(QQW(2), 1)
                QQW(9) = Strings.Left(QQW(2), 1) : QQW(10) = Strings.Right(QQW(3), 1) : QQW(11) = Strings.Left(QQW(3), 1)
                For RT = 4 To 11
                    Dim ran = New Random(DateTime.Now.Millisecond) : Dim NUM1 As Integer = ran.Next(1, 4096)
                    For I = 0 To 4096 : If NUM1 > 9 Then : NUM1 -= 9 : ElseIf NUM1 < 9 Then : I = 4096 : End If : Next
                    If NUM1 = 1 Then : II = RT : 轉換() : ElseIf NUM1 = 2 Then : II = RT : 轉換() : ElseIf NUM1 = 3 Then : ElseIf NUM1 = 4 Then : II = RT : 轉換() : ElseIf NUM1 = 5 Then
                    ElseIf NUM1 = 6 Then : II = RT : 轉換() : ElseIf NUM1 = 7 Then : ElseIf NUM1 = 8 Then : II = RT : 轉換() : ElseIf NUM1 = 9 Then : II = RT : 轉換() : End If
                Next
                Dim ran1 = New Random(DateTime.Now.Millisecond) : Dim NUM2 As Integer = ran1.Next(1, 4096)
                For I = 0 To 4096 : If NUM2 > 99 Then : NUM2 -= 49 : ElseIf NUM2 < 99 Then : I = 4096 : End If : Next
                驗證碼_tb.Text = QQW(11) & "2" & QQW(9) & "7" & QQW(7) & "0" & QQW(5) & QQW(4) & "1" & QQW(6) & "3" & QQW(8) & "9" & QQW(10) & NUM2
                PA20 = 驗證碼_tb.Text : SQL_建築驗證碼() : If dr.Read() = True Then : Else : QAZ = 999 : End If : conn.Close()
            Next
            PA20 = 驗證碼_tb.Text : SQL_建築驗證碼()
            If dr.Read() = True Then : MsgBox("沒有生成可用亂數，請找系統管理員!!") : Exit Sub : End If
        End If
    End Sub
    Private Sub 單據_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 單據_dgv.CellClick
        If e.RowIndex = -1 Then : Else : 選擇項 = e.RowIndex : 單據資料選擇() : End If
    End Sub
    Private Sub 單據資料選擇()
        If 單據_dgv("總價", 選擇項).Value <= 建築採購簽核級別基數 Then : 簽核提示_lb.Text = "簽  核  級  別 : 已  達  一  級  簽  核  級  別"
        ElseIf 單據_dgv("總價", 選擇項).Value > 建築採購簽核級別基數 Then : 簽核提示_lb.Text = "簽  核  級  別 : 已  達  二  級  簽  核  級  別" : End If
        採購單號_tb.Text = 單據_dgv(0, 選擇項).Value.ToString : 折扣比例_tb.Text = 單據_dgv(13, 選擇項).Value.ToString : 稅金_ch.Checked = 單據_dgv(14, 選擇項).Value
        稅金比例_tb.Text = 單據_dgv(15, 選擇項).Value.ToString : 單據總價 = 單據_dgv("總價", 選擇項).Value
        供應商編碼_tb.Text = 單據_dgv(1, 選擇項).Value.ToString : conn.Close()
        PA1 = 供應商編碼_tb.Text : SQL_建築物料採購單生成_供應商資料一()
        If dr.Read() Then : 供應商_cb.Text = dr("公司名") : conn.Close() : End If : conn.Close()
        使用地點_tb.Text = 單據_dgv(3, 選擇項).Value.ToString : 需求時間_dtp.Value = 單據_dgv(2, 選擇項).Value
        Set_採購單清單() : 歷史單價_P.Visible = False
    End Sub
    Private Sub 供應商_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商_cb.SelectedIndexChanged
        PA1 = 供應商_cb.Text : SQL_建築物料採購單生成_供應商資料二()
        If dr.Read() Then : 供應商編碼_tb.Text = dr("供應商編碼") : 地址_tb.Text = dr("公司地址") : 電話_tb.Text = dr("公司電話1")
            郵件_tb.Text = dr("聯繫人1郵箱") : 傳真_tb.Text = dr("公司傳真1") : End If
        conn.Close()
    End Sub
    Private Sub 供應商編碼_tb_TextChanged(sender As Object, e As EventArgs) Handles 供應商編碼_tb.TextChanged
        Set_清單3()
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") : DTP2 = Format(需求時間_dtp.Value, "yyyy/MM/dd")
    End Sub
    Private Sub 核准_bt_Click(sender As Object, e As EventArgs) Handles 核准_bt.Click
        If 單據總價 = 0 Then : MsgBox("單據總價為零，無法通過審核") : Else
            Set_日期格式轉換() : SQL_建築物料採購單初級核准() : conn.Close()
            '---LINE_AUTO 部門主管------------------------------------------------------------------------------------------------------------------------------------------
            LIN訊息通知("CC216", "FH-系統通知(主管)", 採購單號_tb.Text)

            PA1 = 供應商編碼_tb.Text : PA5 = Strings.Mid(採購單號_tb.Text, 7, 8)
            For i As Integer = 0 To 明細_dgv.Rows.Count - 1
                If CLng(明細_dgv.Rows(i).Cells("單價").Value.ToString) > 0 Then
                    PA2 = 明細_dgv.Rows(i).Cells("料號").Value.ToString : PA3 = 明細_dgv.Rows(i).Cells("單價").Value.ToString
                    SQL_供應商商品_採購清單_查詢() : If dr.Read Then : SQL_供應商商品_採購清單_修改() : Else : SQL_供應商商品_採購清單_新增() : End If
                End If
            Next
            明細_dgv.DataSource = Nothing : 手動號碼_tb.Text = "" : Set_清單2() : MsgBox("採購單核准完畢。")
        End If
    End Sub
    Private Sub 作廢_bt_Click(sender As Object, e As EventArgs) Handles 作廢_bt.Click
        PA11 = 採購單號_tb.Text
        SQL_採購單控制表_作廢1() : conn.Close() : Set_清單2() : MsgBox("單據已作廢!!")
        明細_dgv.DataSource = Nothing
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        Set_日期格式轉換()
        PA1 = 供應商編碼_tb.Text : PA2 = 使用地點_tb.Text : PA3 = 折扣比例_tb.Text : BL1 = 稅金_ch.Checked : PA4 = 稅金比例_tb.Text : PA5 = 採購單號_tb.Text
        SQL_採購單控制表_修改() : conn.Close()
        存檔() : Set_清單2() : MsgBox("存檔完成!!")
    End Sub
    Private Sub 存檔()
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            資料數 = 明細_dgv.Rows.Count : MyMod.進度條()
            PA11 = 明細_dgv.Rows(i).Cells("品名").Value : PA12 = 明細_dgv.Rows(i).Cells("規格").Value : PA6 = 明細_dgv.Rows(i).Cells("備註").Value
            PA7 = 明細_dgv.Rows(i).Cells("單價").Value : PA8 = 明細_dgv.Rows(i).Cells("數量").Value : PA9 = 明細_dgv.Rows(i).Cells("單位").Value
            PA5 = 採購單號_tb.Text : PA10 = 明細_dgv.Rows(i).Cells("項次01").Value : PA4 = 明細_dgv.Rows(i).Cells("KEY").Value
            SQL_採購單明細表_修改二() : conn.Close()
        Next : FH_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub 刪除一筆選中的細項資料_bt_Click(sender As Object, e As EventArgs) Handles 刪除一筆選中的細項資料_bt.Click
        If 項次_tb.Text = "" Then : MsgBox("未選擇需要刪除的資料!!") : Else
            Dim result As DialogResult = MessageBox.Show("確定要作刪除筆資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = DialogResult.Yes Then
                存檔()
                PA1 = 採購單號_tb.Text : PA2 = 項次_tb.Text
                SQL_採購單明細表_刪除() : conn.Close()
                Set_採購單清單()
                Dim NUM1 As Integer = 0
                For i As Integer = 0 To 明細_dgv.Rows.Count - 1
                    NUM1 += 1 : If NUM1 < 10 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1 : End If
                    PA18 = 新項次 : PA19 = 採購單號_tb.Text : PA20 = 明細_dgv.Rows(i).Cells("項次01").Value
                    SQL_建築物料採購單一級核准料_新項次() : conn.Close()
                Next
                Set_採購單清單() : 項次_tb.Text = ""
            End If
        End If
    End Sub
    Private Sub 料號_bt_Click(sender As Object, e As EventArgs) Handles 料號_bt.Click
        If 歷史單價_P.Visible = True Then : 歷史單價_P.Visible = False : Else
            If 料號1_tb.Text = "" Then : MsgBox("沒有選擇料號!!") : Else
                Set_料號比價() : 歷史單價_P.Visible = True : 繪圖()
                料號_dgv.Sort(料號_dgv.Columns("排序"), System.ComponentModel.ListSortDirection.Descending)
            End If
        End If : 料號1_tb.Text = ""
    End Sub
    Private Sub 繪圖()
        Chart1.Series.Clear() : Chart1.ChartAreas(0).AxisX.Minimum = 0
        Chart1.ChartAreas(0).AxisY.LabelStyle.Font = New Font("Arial", 8) : Chart1.ChartAreas(0).AxisY.LabelStyle.Format = "Rp " & "#,##0"

        If 品名_tb.Text <> "" Then
            If 折線圖_ch.Checked = True Then
                Chart1.Series.Add(品名_tb.Text & "_1") : Chart1.Series(品名_tb.Text & "_1").ChartType = SeriesChartType.Line : Chart1.Series(品名_tb.Text & "_1").IsVisibleInLegend = False
            End If
            If 長條圖_ch.Checked = True Then
                Chart1.Series.Add(品名_tb.Text) : Chart1.Series(品名_tb.Text).ChartType = SeriesChartType.Column
            End If
            Chart1.Series(品名_tb.Text).IsValueShownAsLabel = False : Chart1.Series(品名_tb.Text).IsVisibleInLegend = False : Chart1.Series(品名_tb.Text).IsXValueIndexed = False
            For i As Integer = 0 To 料號_dgv.Rows.Count - 1
                Chart1.Series(品名_tb.Text).Points.AddXY(料號_dgv.Rows(i).Cells("合約號").Value.ToString, 料號_dgv.Rows(i).Cells("單價").Value.ToString)
            Next
        End If
        Chart1.ChartAreas(0).BackColor = Color.Transparent : Chart1.Legends(0).BackColor = Color.Transparent
        If 折線圖_ch.Checked = True Then
            Chart1.Series(品名_tb.Text & "_1").LabelForeColor = Color.Blue
        End If
        If 長條圖_ch.Checked = True Then
            Chart1.Series(品名_tb.Text).LabelForeColor = Color.Blue
        End If
        Chart1.ChartAreas(0).AxisX.MajorGrid.LineColor = Color.Black : Chart1.ChartAreas(0).AxisY.MajorGrid.LineColor = Color.Black
        Chart1.ChartAreas(0).AxisX.LineColor = Color.Black : Chart1.ChartAreas(0).AxisY.LineColor = Color.Black
        Chart1.ChartAreas(0).AxisX.LabelStyle.ForeColor = Color.Black : Chart1.ChartAreas(0).AxisY.LabelStyle.ForeColor = Color.Black
    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_採購單清單()
    End Sub
    Private Sub 供應商1_cb_Click(sender As Object, e As EventArgs) Handles 供應商1_cb.Click
        CB選擇清單(供應商1_cb, 供應商1_cb.Text) : 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_採購單清單()
    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_採購單清單()
    End Sub
    Private Sub 物料圖檔_pb_Click(sender As Object, e As EventArgs) Handles 物料圖檔_pb.Click
        If IsNothing(物料圖檔_pb.Image) = False Then : 圖片傳遞 = 物料圖檔_pb.Image : 圖片放大視窗.ShowDialog() : End If
    End Sub
    Private Sub 詳細資料2_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 詳細資料2_ch.Click
        If 詳細資料2_ch.Checked = True Then : 詳細資料2_ch.Checked = True : Else : 詳細資料2_ch.Checked = False : End If : Set_採購單清單()
    End Sub
End Class