Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Public Class 歷史單價紀錄表
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Dim 新流水號 As String : Dim DTP1 As String : Dim DTP2 As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 流水號, 客戶簡稱, 客戶備註 AS 客戶, XF1, XF2, 工廠, 形體號, 形體名, 鞋面, 顏色, 工廠價, 客戶價, 備註, 鞋圖編號 FROM 歷史單價紀錄表 ORDER BY 客戶簡稱, 形體號, 形體名,顏色"
        Else
            SQL1 = "SELECT 流水號, 客戶簡稱, 客戶備註 AS 客戶, XF1, XF2, 工廠, 形體號, 形體名, 鞋面, 顏色, 工廠價, 客戶價, 備註, 鞋圖編號 FROM 歷史單價紀錄表 " &
                   "WHERE (客戶簡稱 LIKE N'%" & PA & "%') OR (工廠 LIKE N'%" & PA & "%') OR (形體號 LIKE N'%" & PA & "%') OR (形體名 LIKE N'%" & PA & "%') OR (鞋面 LIKE N'%" & PA & "%') OR (顏色 LIKE N'%" & PA & "%') " &
                   "ORDER BY 客戶簡稱, 形體號, 形體名,顏色"
        End If
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Visible = False : DataGridView1.Columns(1).Width = 110 : DataGridView1.Columns(2).Visible = False : DataGridView1.Columns(3).Width = 90 : DataGridView1.Columns(4).Width = 90
        DataGridView1.Columns(5).Width = 100 : DataGridView1.Columns(6).Width = 180 : DataGridView1.Columns(7).Width = 210 : DataGridView1.Columns(8).Width = 290 : DataGridView1.Columns(9).Width = 160
        DataGridView1.Columns(10).Width = 60 : DataGridView1.Columns(11).Width = 60 : DataGridView1.Columns(12).Width = 515 : DataGridView1.Columns(13).Visible = False
        DataGridView1.Columns(10).DefaultCellStyle.Format = "#,##0.00" : DataGridView1.Columns(11).DefaultCellStyle.Format = "#,##0.00"
        DataGridView1.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : DataGridView1.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : DataGridView1.Columns(11).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 客戶簡稱 FROM 客戶控制表 ORDER BY 客戶簡稱"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear() : While (dr.Read()) : ComboBox1.Items.Add(dr("客戶簡稱")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 工廠 FROM 工廠控制表 ORDER BY 工廠"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("工廠")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 顏色 FROM 顏色清單 ORDER BY 顏色"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox3.Items.Clear() : While (dr.Read()) : ComboBox3.Items.Add(dr("顏色")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox4下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 型體號碼 FROM 型體控制表 WHERE 客戶簡稱 LIKE N'" & ComboBox1.Text & "' "
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox4.Items.Clear() : While (dr.Read()) : ComboBox4.Items.Add(dr("型體號碼")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox5下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 型體名稱 FROM 型體控制表 WHERE 型體號碼 LIKE N'" & ComboBox4.Text & "' "
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox5.Items.Clear() : While (dr.Read()) : ComboBox5.Items.Add(dr("型體名稱")) : End While : conn.Close()
    End Sub
    Private Sub Set_日期格式轉換()
        DTP1 = Format(DateTimePicker1.Value, "yyyy/MM/dd") : DTP2 = Format(DateTimePicker2.Value, "yyyy/MM/dd")
    End Sub
    Private Sub 歷史單價紀錄表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        TextBox1.Enabled = False : TextBox9.Enabled = False : Button11.Enabled = False : Label62.Visible = False
        ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入()
        Set_清單()
        If CC(6) = False Then
            Button29.Enabled = False : Button1.Enabled = False : Button12.Enabled = False
        End If
        If CC(7) = False Then
            Button10.Enabled = False
        End If
        If CC(8) = False Then
            Button3.Enabled = False : Button4.Enabled = False
        End If
    End Sub
    Private Sub 歷史單價紀錄表_Unload(sender As Object, e As EventArgs) Handles MyBase.Closing
        If Button11.Enabled = True Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 歷史單價紀錄表 WHERE (流水號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
        End If
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Label62.Visible = True Then : Label62.Visible = False : Else : Label62.Visible = True : End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            If Button11.Enabled = False Then
                TextBox1.Text = DataGridView1(0, e.RowIndex).Value.ToString : ComboBox1.Text = DataGridView1(1, e.RowIndex).Value.ToString : TextBox2.Text = DataGridView1(2, e.RowIndex).Value.ToString
                DateTimePicker1.Text = DataGridView1(3, e.RowIndex).Value.ToString : DateTimePicker2.Text = DataGridView1(4, e.RowIndex).Value.ToString : ComboBox2.Text = DataGridView1(5, e.RowIndex).Value.ToString
                ComboBox4.Text = DataGridView1(6, e.RowIndex).Value.ToString : ComboBox5.Text = DataGridView1(7, e.RowIndex).Value.ToString : TextBox5.Text = DataGridView1(8, e.RowIndex).Value.ToString
                ComboBox3.Text = DataGridView1(9, e.RowIndex).Value.ToString : TextBox6.Text = DataGridView1(10, e.RowIndex).Value.ToString : TextBox7.Text = DataGridView1(11, e.RowIndex).Value.ToString
                TextBox8.Text = DataGridView1(12, e.RowIndex).Value.ToString : TextBox9.Text = DataGridView1(13, e.RowIndex).Value.ToString
                TextBox6.Text = Format(Val(TextBox6.Text), "#,##0.00") : TextBox7.Text = Format(Val(TextBox7.Text), "#,##0.00")
                PictureBox1.Image = Nothing
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 圖片 FROM 鞋型圖片資料庫 WHERE 客戶 LIKE N'" & DataGridView1(1, e.RowIndex).Value.ToString & "' AND 形體號 LIKE N'" & DataGridView1(6, e.RowIndex).Value.ToString &
                       "' AND 形體名 LIKE N'" & DataGridView1(7, e.RowIndex).Value.ToString & "' AND 顏色 LIKE N'" & DataGridView1(9, e.RowIndex).Value.ToString & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    PictureBox1.Image = Nothing : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT 圖片 FROM 鞋型圖片資料庫 WHERE 客戶 LIKE N'" & DataGridView1(1, e.RowIndex).Value.ToString & "' AND 形體號 LIKE N'" & DataGridView1(6, e.RowIndex).Value.ToString &
                       "' AND 形體名 LIKE N'" & DataGridView1(7, e.RowIndex).Value.ToString & "' AND 顏色 LIKE N'" & DataGridView1(9, e.RowIndex).Value.ToString & "'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                Else
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 WHERE 客戶 LIKE N'" & DataGridView1(1, e.RowIndex).Value.ToString & "' AND 形體號 LIKE N'" & DataGridView1(6, e.RowIndex).Value.ToString &
                           "' AND 形體名 LIKE N'" & DataGridView1(7, e.RowIndex).Value.ToString & "' AND 類別 LIKE N'設計圖'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    If dr.Read() Then
                        PictureBox1.Image = Nothing : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 WHERE 客戶 LIKE N'" & DataGridView1(1, e.RowIndex).Value.ToString & "' AND 形體號 LIKE N'" & DataGridView1(6, e.RowIndex).Value.ToString &
                           "' AND 形體名 LIKE N'" & DataGridView1(7, e.RowIndex).Value.ToString & "' AND 類別 LIKE N'設計圖'"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                        While dr.Read() = True
                            Dim bytes As Byte() = New Byte(-1) {} : bytes = DirectCast(dr.Item("圖片"), Byte()) : Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                        End While
                        conn.Close()
                    Else
                        conn.Close()
                    End If
                End If : PictureBox1.SizeMode = 4
            End If
        End If
    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView1.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView1(1, e.RowIndex).Value.ToString <> DataGridView1(1, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView1.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單() : PA = ""
    End Sub
    Private Sub Button32_Click(sender As Object, e As EventArgs) Handles Button32.Click
        PA = "" : Set_清單()
    End Sub
    Private Sub 新增準備1()
        Dim NUM1 As Integer
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP(1) 流水號 FROM 歷史單價紀錄表 ORDER BY 流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 新流水號 = dr("流水號") : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Right(新流水號, 6)) + 1
        If NUM1 < 10 Then : 新流水號 = "PR" & "00000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號 = "PR" & "0000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號 = "PR" & "000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號 = "PR" & "00" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 新流水號 = "PR" & "0" & NUM1
        ElseIf NUM1 > 99999 Then : 新流水號 = "PR" & NUM1
        End If
        TextBox1.Text = 新流水號
    End Sub
    Private Sub 新增準備2()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 歷史單價紀錄表 ( 流水號, 客戶簡稱, 客戶備註, XF1, XF2, 工廠, 形體號, 形體名, 鞋面, 顏色, 工廠價, 客戶價, 備註, 鞋圖編號) " &
               "VALUES (N'" & TextBox1.Text & "',N'" & ComboBox1.Text & "',N'" & TextBox2.Text & "','" & DTP1 & "','" & DTP2 & "', N'" & ComboBox2.Text & "', N'" & ComboBox4.Text & "', N'" & ComboBox5.Text &
                    "', N'" & TextBox5.Text & "', N'" & ComboBox3.Text & "', N'" & TextBox6.Text & "', N'" & TextBox7.Text & "', N'" & TextBox8.Text & "', N'" & TextBox9.Text & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Button11.Enabled = True : Button29.Enabled = False : Button1.Enabled = False : Button12.Enabled = False
        MsgBox("資料新增準備完成")
    End Sub
    Private Sub Button29_Click(sender As Object, e As EventArgs) Handles Button29.Click
        Timer1.Enabled = True
        新增準備1()
        ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入()
        DateTimePicker1.Value = Today() : DateTimePicker2.Value = Today()
        TextBox2.Text = "" : ComboBox4.Text = "" : ComboBox5.Text = "" : TextBox5.Text = "" : TextBox6.Text = "0.00" : TextBox7.Text = "0.00" : TextBox8.Text = "" : TextBox9.Text = ""
        Set_日期格式轉換()
        新增準備2()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Enabled = True
        If ComboBox4.Text = "" Or ComboBox5.Text = "" Or TextBox5.Text = "" Or ComboBox1.Text = "" Or ComboBox2.Text = "" Or ComboBox3.Text = "" Then
            MsgBox("複製新增方式需要先選擇一筆被複製的舊資料")
        Else
            新增準備1() : Set_日期格式轉換() : 新增準備2()
        End If
    End Sub
    Private Sub 修改1()
        If ComboBox4.Text = "" Or ComboBox5.Text = "" Or TextBox5.Text = "" Or ComboBox1.Text = "" Or ComboBox2.Text = "" Or ComboBox3.Text = "" Then
            MsgBox("紅色字體的資料欄位為必填項，不可空白")
        Else
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 歷史單價紀錄表 SET 客戶簡稱 = N'" & ComboBox1.Text & "', 客戶備註 = N'" & TextBox2.Text & "', XF1 = '" & DTP1 & "', XF2 = '" & DTP2 & "', 工廠 = N'" & ComboBox2.Text &
                   "', 形體號 = N'" & ComboBox4.Text & "', 形體名 = N'" & ComboBox5.Text & "', 鞋面 = N'" & TextBox5.Text & "', 顏色 = N'" & ComboBox3.Text & "', 工廠價 = N'" & TextBox6.Text &
                   "', 客戶價 = N'" & TextBox7.Text & "', 備註 = N'" & TextBox8.Text & "', 鞋圖編號 = N'" & TextBox9.Text & "' WHERE (流水號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()
            Button11.Enabled = False : Button29.Enabled = True : Button1.Enabled = True : Button12.Enabled = True
            MsgBox("存檔完成") : Set_清單()
        End If
        Timer1.Enabled = False : Label62.Visible = False
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        修改1()
    End Sub
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        修改1()
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        SQL1 = "DELETE FROM 歷史單價紀錄表 WHERE (流水號 = '" & TextBox1.Text & "')"
        cmd.CommandText = SQL1 : cmd.Connection = conn
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            cmd.ExecuteNonQuery()
            cmd.ExecuteNonQuery()
            MsgBox("刪除完成") : Set_清單()
        End If
        conn.Close()
    End Sub
    Private Sub Button28_Click(sender As Object, e As EventArgs) Handles Button28.Click
        MsgBox("等待開發")
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        清單修改.Close() : 工廠清單修改 = True : 清單修改.ShowDialog() : ComboBox2下拉表單資料載入() : 工廠清單修改 = False
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        清單修改.Close() : 顏色清單修改 = True : 清單修改.ShowDialog() : ComboBox3下拉表單資料載入() : 顏色清單修改 = False
    End Sub
    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
        ComboBox5下拉表單資料載入()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox4下拉表單資料載入()
    End Sub
End Class