Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Public Class 量產訂單控制表
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds1 As New DataSet : ReadOnly ds3 As New DataSet
    Dim dr As SqlDataReader
    Dim GHJK As String : Dim ESTR As String
    Dim aa As MsgBoxResult
    Private Sub Set_清單3()
        控制表_dgv.DataSource = Nothing : ds3.Clear()
        控制表_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        控制表_dgv.ColumnHeadersHeight = 25
        控制表_dgv.AllowUserToAddRows = False
        控制表_dgv.RowTemplate.Height = 35
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If PA = "" Then
            SQL1 = "SELECT  訂單流水號, 年份季度, 客戶訂單號, 型體號碼, GOODBOX AS 型體名稱, APMA確認 AS 顏色, TVW AS 楦型, 訂單數量, 工廠回復交期 AS 工廠交期, 客戶交期 AS 交期開始, 
                            客戶交期1 AS 交期結束, 單價, 總金額, 備註, 生產工廠, 訂單種類, BUYER, CFMSPEC AS 訂單屬性, 明細建檔, 材質, 東莞業務備註, LEATHER AS 手剪全套, LEA_Note AS 斬刀全套, 
                            面料, 副料, 中底, 鞋墊, 大底, 跟, 包裝確認時間 AS 包裝, 裁斷時間, 針車時間, 成型時間, 驗貨時間, 工廠進度表備註1 AS 驗貨結果, 工廠進度表備註2 AS 工廠備註, 
                            資料流水號, 實際離廠日, 出貨數量, CXL, CXLTime, 驗貨報告圖片,
                            CFMSAMPLE, TRIAL1, TRIAL2, TRIAL3, TRIAL4, 貼標時間, PO_S_Date, Requested_XF, Confirm_XF, Revised_XF, 
                            Special_Packaging, Remarks, 出貨地, 楦頭號, 長期試穿, 七號模具, 國標測試, 客戶包裝確認
                    FROM 訂單控制表  
                    WHERE ((CXL = 0) OR (CXL IS NULL)) AND (訂單數量 - 出貨數量 <> 0)
                    ORDER BY 訂單流水號 DESC, 型體號碼, APMA確認, TVW"
        Else
            SQL1 = "SELECT  訂單流水號, 年份季度, 客戶訂單號, 型體號碼, GOODBOX AS 型體名稱, APMA確認 AS 顏色, TVW AS 楦型, 訂單數量, 工廠回復交期 AS 工廠交期, 客戶交期 AS 交期開始, 
                            客戶交期1 AS 交期結束, 單價, 總金額, 備註, 生產工廠, 訂單種類, BUYER, CFMSPEC AS 訂單屬性, 明細建檔, 材質, 東莞業務備註, LEATHER AS 手剪全套, LEA_Note AS 斬刀全套, 
                            面料, 副料, 中底, 鞋墊, 大底, 跟, 包裝確認時間 AS 包裝, 裁斷時間, 針車時間, 成型時間, 驗貨時間, 工廠進度表備註1 AS 驗貨結果, 工廠進度表備註2 AS 工廠備註, 
                            資料流水號, 實際離廠日, 出貨數量, CXL, CXLTime, 驗貨報告圖片,
                            CFMSAMPLE, TRIAL1, TRIAL2, TRIAL3, TRIAL4, 貼標時間, PO_S_Date, Requested_XF, Confirm_XF, Revised_XF, 
                            Special_Packaging, Remarks, 出貨地, 楦頭號, 長期試穿, 七號模具, 國標測試, 客戶包裝確認
                    FROM 訂單控制表
                    WHERE ((CXL = 0) OR (CXL IS NULL)) AND (訂單數量 - 出貨數量 <> 0) AND 
                         (訂單流水號 LIKE N'%" & PA & "%' OR 年份季度 LIKE N'%" & PA & "%' OR 客戶訂單號 LIKE N'%" & PA & "%' OR 型體號碼 LIKE N'%" & PA & "%' OR GOODBOX LIKE N'%" & PA & "%' OR 
                          APMA確認 LIKE N'%" & PA & "%' OR TVW LIKE N'%" & PA & "%' OR 訂單數量 LIKE N'%" & PA & "%' OR 客戶交期 LIKE N'%" & PA & "%' OR 客戶交期1 LIKE N'%" & PA & "%' OR 
                          備註 LIKE N'%" & PA & "%' OR 生產工廠 LIKE N'%" & PA & "%' OR 訂單種類 LIKE N'%" & PA & "%' OR BUYER LIKE N'%" & PA & "%' OR CFMSPEC LIKE N'%" & PA & "%')
                    ORDER BY 訂單流水號 DESC, 型體號碼, APMA確認, TVW"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds3) : 控制表_dgv.DataSource = ds3.Tables(0) : conn.Close() : Set_grid4()
    End Sub
    Private Sub Set_控制表_dgv_插入勾選控件清單()
        Dim Col As New DataGridViewCheckBoxColumn With {
            .FillWeight = 40,
            .DataPropertyName = "選擇"
        }
        Col.HeaderText = "選擇" : Col.Name = "選擇"
        控制表_dgv.Columns.Insert(0, Col)
    End Sub
    Private Sub Set_grid4()
        If 控制表_dgv.Columns.Count.ToString > 60 Then
            控制表_dgv.Columns.Remove("選擇")
        End If
        Set_控制表_dgv_插入勾選控件清單()
        控制表_dgv.Columns(0).FillWeight = 40 : 控制表_dgv.Columns(1).FillWeight = 140 : 控制表_dgv.Columns(2).FillWeight = 80 : 控制表_dgv.Columns(3).FillWeight = 140
        控制表_dgv.Columns(4).FillWeight = 130 : 控制表_dgv.Columns(5).FillWeight = 140 : 控制表_dgv.Columns(6).FillWeight = 90 : 控制表_dgv.Columns(7).FillWeight = 60
        控制表_dgv.Columns(8).FillWeight = 90 : 控制表_dgv.Columns(9).FillWeight = 100 : 控制表_dgv.Columns(10).FillWeight = 100 : 控制表_dgv.Columns(11).FillWeight = 100
        控制表_dgv.Columns(12).FillWeight = 90 : 控制表_dgv.Columns(13).FillWeight = 90 : 控制表_dgv.Columns(14).FillWeight = 270 : 控制表_dgv.Columns(15).FillWeight = 90
        控制表_dgv.Columns(16).FillWeight = 120 : 控制表_dgv.Columns(17).FillWeight = 140 : 控制表_dgv.Columns(18).FillWeight = 115 : 控制表_dgv.Columns(19).FillWeight = 50
        控制表_dgv.Columns(20).FillWeight = 60 : 控制表_dgv.Columns(21).FillWeight = 495 : 控制表_dgv.Columns(22).FillWeight = 120 : 控制表_dgv.Columns(23).FillWeight = 120
        控制表_dgv.Columns(24).FillWeight = 113 : 控制表_dgv.Columns(25).FillWeight = 113 : 控制表_dgv.Columns(26).FillWeight = 113 : 控制表_dgv.Columns(27).FillWeight = 114
        控制表_dgv.Columns(28).FillWeight = 114 : 控制表_dgv.Columns(29).FillWeight = 114 : 控制表_dgv.Columns(30).FillWeight = 114 : 控制表_dgv.Columns(31).FillWeight = 120
        控制表_dgv.Columns(32).FillWeight = 120 : 控制表_dgv.Columns(33).FillWeight = 120 : 控制表_dgv.Columns(34).FillWeight = 120 : 控制表_dgv.Columns(35).FillWeight = 120
        控制表_dgv.Columns(36).FillWeight = 195 : 控制表_dgv.Columns(37).FillWeight = 123 : 控制表_dgv.Columns(38).FillWeight = 123 : 控制表_dgv.Columns(39).FillWeight = 123
        控制表_dgv.Columns(40).FillWeight = 60 : 控制表_dgv.Columns(41).FillWeight = 123 : 控制表_dgv.Columns(42).FillWeight = 123
        '795
        If Button3.Enabled = False And Button34.Enabled = True And Button35.Enabled = True And Button4.Enabled = True And Button6.Enabled = True Then
            For i As Integer = 9 To 15 : 控制表_dgv.Columns(i).Visible = True : Next
            For i As Integer = 20 To 60 : 控制表_dgv.Columns(i).Visible = False : Next
            控制表_dgv.Columns(18).Visible = True : 控制表_dgv.Columns(15).Visible = False
        ElseIf Button3.Enabled = True And Button34.Enabled = False And Button35.Enabled = True And Button4.Enabled = True And Button6.Enabled = True Then
            For i As Integer = 9 To 16 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 20 To 23 : 控制表_dgv.Columns(i).Visible = True : Next
            For i As Integer = 24 To 60 : 控制表_dgv.Columns(i).Visible = False : Next
            控制表_dgv.Columns(18).Visible = False
        ElseIf Button3.Enabled = True And Button34.Enabled = True And Button35.Enabled = False And Button4.Enabled = True And Button6.Enabled = True Then
            For i As Integer = 9 To 16 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 20 To 23 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 24 To 30 : 控制表_dgv.Columns(i).Visible = True : Next
            For i As Integer = 31 To 60 : 控制表_dgv.Columns(i).Visible = False : Next
            控制表_dgv.Columns(18).Visible = False
        ElseIf Button3.Enabled = True And Button34.Enabled = True And Button35.Enabled = True And Button4.Enabled = False And Button6.Enabled = True Then
            For i As Integer = 9 To 16 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 20 To 30 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 31 To 36 : 控制表_dgv.Columns(i).Visible = True : Next
            For i As Integer = 37 To 60 : 控制表_dgv.Columns(i).Visible = False : Next
            控制表_dgv.Columns(18).Visible = False
        ElseIf Button3.Enabled = True And Button34.Enabled = True And Button35.Enabled = True And Button4.Enabled = True And Button6.Enabled = False Then
            For i As Integer = 9 To 15 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 20 To 36 : 控制表_dgv.Columns(i).Visible = False : Next
            For i As Integer = 37 To 42 : 控制表_dgv.Columns(i).Visible = True : Next
            For i As Integer = 43 To 60 : 控制表_dgv.Columns(i).Visible = False : Next
            控制表_dgv.Columns(17).Visible = False : 控制表_dgv.Columns(15).Visible = True
        End If

        控制表_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 控制表_dgv.Columns(12).DefaultCellStyle.Format = "#,##0.00" : 控制表_dgv.Columns(13).DefaultCellStyle.Format = "#,##0.00"
        控制表_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 控制表_dgv.Columns(13).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        控制表_dgv.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        控制表_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : 控制表_dgv.Columns(13).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        控制表_dgv.Columns(12).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight

        TextBox6.Text = "0" : TextBox3.Text = "0" : TextBox8.Text = "0" : TextBox7.Text = "0" : TextBox5.Text = "0" : TextBox4.Text = "0"
        For i As Integer = 0 To 控制表_dgv.Rows.Count - 1
            If 控制表_dgv.Rows(i).Cells("訂單屬性").Value.ToString = "LANDED BRANDED" Then
                TextBox6.Text = Val(TextBox6.Text) + 控制表_dgv.Rows(i).Cells("訂單數量").Value
                TextBox5.Text = Val(TextBox5.Text) + 控制表_dgv.Rows(i).Cells("總金額").Value
            Else
                TextBox3.Text = Val(TextBox3.Text) + 控制表_dgv.Rows(i).Cells("訂單數量").Value
                TextBox4.Text = Val(TextBox4.Text) + 控制表_dgv.Rows(i).Cells("總金額").Value
            End If
            控制表_dgv("選擇", i).Value = False
        Next
        TextBox8.Text = Val(TextBox6.Text) + Val(TextBox3.Text) : TextBox7.Text = Val(TextBox5.Text) + Val(TextBox4.Text)
        TextBox7.Text = Format(Val(TextBox7.Text), "#,##0") : TextBox5.Text = Format(Val(TextBox5.Text), "#,##0") : TextBox4.Text = Format(Val(TextBox4.Text), "#,##0")
        TextBox6.Text = Format(Val(TextBox6.Text), "#,##0") : TextBox3.Text = Format(Val(TextBox3.Text), "#,##0") : TextBox8.Text = Format(Val(TextBox8.Text), "#,##0")
    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
        DataGridView2.Visible = False : TextBox13.Enabled = False : Button3.Enabled = False : 批量出貨_bt.Enabled = False
        Set_清單3()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "DELETE FROM 貨款匯入出款明細表 WHERE (出貨數量 = 0)"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader : conn.Close()
    End Sub
    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 控制表_dgv.CellClick
        If 啟用批量出貨_ch.Checked = True Then
            If 控制表_dgv("選擇", e.RowIndex).Value = True Then
                控制表_dgv("選擇", e.RowIndex).Value = False
            Else
                控制表_dgv("選擇", e.RowIndex).Value = True
            End If
            For i As Integer = 0 To 控制表_dgv.Rows.Count - 1
                If 控制表_dgv("選擇", i).Value = True Then
                    控制表_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
                Else
                    控制表_dgv.Rows(i).DefaultCellStyle.BackColor = Color.White
                End If
            Next
        Else
            If e.RowIndex = -1 Then : Else
                TextBox12.Text = 控制表_dgv.Rows(e.RowIndex).Cells("訂單流水號").Value
                TextBox2.Text = 控制表_dgv.Rows(e.RowIndex).Cells("資料流水號").Value

                TextBox13.Text = 控制表_dgv.Rows(e.RowIndex).Cells("訂單數量").Value - 控制表_dgv.Rows(e.RowIndex).Cells("出貨數量").Value
                TextBox13.Text = Format(Val(TextBox13.Text), "###0")

                If 控制表_dgv.Rows(e.RowIndex).Cells("明細建檔").Value = True Then : Button30.Enabled = True : Else : Button30.Enabled = False : End If

                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'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'  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'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'  AND 類別 LIKE N'設計圖'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    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) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                Else
                    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'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                    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'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                        While dr.Read() = True
                            Dim unused3 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                            Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                        End While
                        conn.Close()
                    Else
                        PictureBox1.Image = Nothing : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "SELECT 圖片 FROM 鞋樣素描圖控制表 WHERE 型體編號 LIKE N'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                        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'" & 控制表_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                            While dr.Read() = True
                                Dim unused1 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = 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
                End If : PictureBox1.SizeMode = 4

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 驗貨報告 FROM 驗貨報告圖庫 WHERE 驗貨報告編號 LIKE '" & 控制表_dgv.Rows(e.RowIndex).Cells("驗貨報告圖片").Value & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                PictureBox2.Image = Nothing
                While dr.Read() = True
                    Dim unused2 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("驗貨報告"), Byte())
                    Dim oStream As New MemoryStream(bytes) : PictureBox2.Image = Bitmap.FromStream(oStream)
                End While
                conn.Close() : PictureBox2.SizeMode = 4
                驗貨報告 = 控制表_dgv.Rows(e.RowIndex).Cells("驗貨報告圖片").Value
            End If
        End If
    End Sub
    Private Sub DataGridView3_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 控制表_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 控制表_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 控制表_dgv(1, e.RowIndex).Value.ToString <> 控制表_dgv(1, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(控制表_dgv.RowHeadersVisible, 控制表_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 控制表_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 控制表_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub CheckBox10_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox10.Click
        If CheckBox10.Checked = True Then : CheckBox10.Checked = True : CheckBox11.Checked = False : Else : CheckBox10.Checked = False : CheckBox11.Checked = True : End If
    End Sub
    Private Sub CheckBox11_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox11.Click
        If CheckBox11.Checked = True Then : CheckBox11.Checked = True : CheckBox10.Checked = False : Else : CheckBox11.Checked = False : CheckBox10.Checked = True : End If
    End Sub
    Private Sub CheckBox24_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox24.Click
        If CheckBox24.Checked = True Then : CheckBox24.Checked = True : CheckBox25.Checked = False : Else : CheckBox24.Checked = False : CheckBox25.Checked = True : End If
    End Sub
    Private Sub CheckBox25_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox25.Click
        If CheckBox25.Checked = True Then : CheckBox24.Checked = False : CheckBox25.Checked = True : Else : CheckBox24.Checked = True : CheckBox25.Checked = False : End If
    End Sub
    Private Sub Button30_Click(sender As Object, e As EventArgs) Handles Button30.Click
        If TextBox12.Text = "" Then
            MsgBox("請先選擇訂單資料")
        Else
            PA = TextBox12.Text
            量產訂單明細表.Show() : 量產訂單明細表.WindowState = 2
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox12.Text = "" Then
            MsgBox("未選擇需要刪除的資料")
        Else
            aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
            If aa = MsgBoxResult.Ok Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 訂單明細表抬頭 WHERE (訂單流水號 LIKE N'" & TextBox12.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 訂單控制表 WHERE (訂單流水號 LIKE N'" & TextBox12.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 訂單明細表 WHERE (訂單流水號 LIKE N'" & TextBox12.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                Set_清單3()
                MsgBox("刪除完成")
            End If
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單3() : PA = ""
    End Sub
    Private Sub Button38_Click(sender As Object, e As EventArgs) Handles Button38.Click
        aa = MsgBox("確定要CXL訂單?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            If TextBox2.Text = "" Or TextBox12.Text = "" Then
                MsgBox("沒有正確選擇要修改的資料")
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                If CheckBox24.Checked = False Then
                    SQL1 = "UPDATE 訂單控制表 SET CXL = '1', CXLTime = '" & Format(Today, "yyyy/MM/dd") & "' WHERE (資料流水號 = '" & TextBox2.Text & "')"
                ElseIf CheckBox24.Checked = True Then
                    SQL1 = "UPDATE 訂單控制表 SET CXL = '1', CXLTime = '" & Format(Today, "yyyy/MM/dd") & "' WHERE (訂單流水號 = '" & TextBox12.Text & "')"
                End If
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            End If
        End If
        Set_清單3()
    End Sub
    Private Sub DateTimePicker30_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker30.MouseDown
        DateTimePicker30.Format = DateTimePickerFormat.Short
    End Sub
    Private Sub Button27_Click(sender As Object, e As EventArgs) Handles Button27.Click
        DateTimePicker30.Format = DateTimePickerFormat.Custom : DateTimePicker30.CustomFormat = " "
    End Sub
    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        aa = MsgBox("確定要將此產品訂單移到出貨?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            If DateTimePicker30.Format = DateTimePickerFormat.Custom Then : MsgBox("ACTUAL ex-fty不能空白")
            Else
                Dim EDR As Integer
                Dim EDRW As Integer
                If CheckBox10.Checked = True Then
                    If TextBox1.Text = "" Or IsNumeric(TextBox1.Text) = False Then
                        MsgBox("出貨數量不可為空白或非數字型態")
                    Else
                        DataGridView2.DataSource = Nothing : ds1.Clear()
                        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                        DataGridView2.ColumnHeadersHeight = 25
                        DataGridView2.AllowUserToAddRows = False
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "SELECT 訂單流水號, 年份季度, 客戶訂單號, 型體號碼, GOODBOX AS 型體名稱, APMA確認 AS 顏色, TVW AS 楦型, 訂單數量, 客戶交期 AS 交期開始, 客戶交期1 AS 交期結束, 
                                       單價, 總金額, 備註, 生產工廠, 訂單種類, BUYER, CFMSPEC, CFMSAMPLE, TRIAL1, TRIAL2, TRIAL3, TRIAL4, 包裝確認時間, 貼標時間, 裁斷時間, 針車時間, 成型時間, 
                                       驗貨時間, 實際離廠日, 資料流水號, 明細建檔, LEATHER, LEA_Note, CXL, CXLTime, 出貨數量, PO_S_Date, Requested_XF, Confirm_XF, Revised_XF, Special_Packaging, 
                                       Remarks, 出貨地, 楦頭號, 材質, 長期試穿, 面料, 副料, 中底, 鞋墊, 大底, 跟, 工廠進度表備註1, 七號模具, 工廠進度表備註2, 國標測試, 客戶包裝確認, 東莞業務備註, 
                                       驗貨報告圖片 FROM 訂單控制表 
                                WHERE (資料流水號 = '" & TextBox2.Text & "') AND (CXL <> 1)"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()

                        For i As Integer = 0 To DataGridView2.Rows.Count - 1
                            GHJK = DataGridView2.Rows(i).Cells("驗貨報告圖片").Value
                            If GHJK = "" Then
                                MsgBox("驗貨報告不齊全，請重新確認。")
                                Set_清單3() : TextBox1.Text = "" : TextBox2.Text = "" : TextBox12.Text = ""
                                Exit Sub
                            End If
                        Next

                        If DataGridView2.Rows(0).Cells("出貨數量").Value >= DataGridView2.Rows(0).Cells("訂單數量").Value Then
                            MsgBox("系統有誤，請通知系統管理員處理。")
                            Exit Sub
                        End If

                        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 : EDR = Double.Parse(Strings.Right(dr("貨款流水號").ToString, 8)) : Else : EDR = 0 : End If
                        conn.Close()
                        EDR += 1
                        If EDR < 10 Then : ESTR = "PG" & "0000000" & EDR
                        ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PG" & "000000" & EDR
                        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PG" & "00000" & EDR
                        ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PG" & "0000" & EDR
                        ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PG" & "000" & EDR
                        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PG" & "00" & EDR
                        ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PG" & "0" & EDR
                        ElseIf EDR > 9999999 Then : ESTR = "PG" & EDR
                        End If

                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "INSERT INTO 貨款匯入出款明細表 (貨款流水號, 資料流水號, 訂單流水號, 年份季度, 客戶訂單號, 形體號, 顏色, 楦型, 訂單數量, 出貨數量, " &
                           "工廠價格, 實際離廠日, 匯款時間, 入帳時間, 貨款備註) " &
                           "VALUES ('" & ESTR & "','" & DataGridView2.Rows(0).Cells("資料流水號").Value & "','" & DataGridView2.Rows(0).Cells("訂單流水號").Value &
                           "','" & DataGridView2.Rows(0).Cells("年份季度").Value & "','" & DataGridView2.Rows(0).Cells("客戶訂單號").Value &
                           "','" & DataGridView2.Rows(0).Cells("型體名稱").Value & "','" & DataGridView2.Rows(0).Cells("顏色").Value &
                           "','" & DataGridView2.Rows(0).Cells("楦型").Value & "','" & DataGridView2.Rows(0).Cells("訂單數量").Value &
                           "','" & TextBox1.Text & "', '', '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', '', '', '')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                        EDRW = DataGridView2.Rows(0).Cells("出貨數量").Value + Double.Parse(TextBox1.Text)
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "UPDATE 訂單控制表 SET 實際離廠日 = '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', 出貨數量 = '" & EDRW & "' " &
                           "WHERE (資料流水號 = '" & TextBox2.Text & "')"
                        cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    End If
                Else
                    DataGridView2.DataSource = Nothing : ds1.Clear()
                    DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                    DataGridView2.ColumnHeadersHeight = 25
                    DataGridView2.AllowUserToAddRows = False
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT 訂單流水號, 年份季度, 客戶訂單號, 型體號碼, GOODBOX AS 型體名稱, APMA確認 AS 顏色, TVW AS 楦型, 訂單數量, 客戶交期 AS 交期開始, 客戶交期1 AS 交期結束, 
                                   單價, 總金額, 備註, 生產工廠, 訂單種類, BUYER, CFMSPEC, CFMSAMPLE, TRIAL1, TRIAL2, TRIAL3, TRIAL4, 包裝確認時間, 貼標時間, 裁斷時間, 針車時間, 成型時間, 
                                   驗貨時間, 實際離廠日, 資料流水號, 明細建檔, LEATHER, LEA_Note, CXL, CXLTime, 出貨數量, PO_S_Date, Requested_XF, Confirm_XF, Revised_XF, Special_Packaging, 
                                   Remarks, 出貨地, 楦頭號, 材質, 長期試穿, 面料, 副料, 中底, 鞋墊, 大底, 跟, 工廠進度表備註1, 七號模具, 工廠進度表備註2, 國標測試, 客戶包裝確認, 東莞業務備註, 
                                   驗貨報告圖片 FROM 訂單控制表 
                            WHERE (訂單流水號 = '" & TextBox12.Text & "') AND (CXL <> 1)"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()

                    For i As Integer = 0 To DataGridView2.Rows.Count - 1
                        GHJK = DataGridView2.Rows(i).Cells("驗貨報告圖片").Value
                        If GHJK = "" Then
                            MsgBox("驗貨報告不齊全，請重新確認。")
                            Set_清單3() : TextBox1.Text = "" : TextBox2.Text = "" : TextBox12.Text = ""
                            Exit Sub
                        End If
                    Next

                    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 : EDR = Double.Parse(Strings.Right(dr("貨款流水號").ToString, 8)) : Else : EDR = 0 : End If
                    conn.Close()

                    For i As Integer = 0 To DataGridView2.Rows.Count - 1
                        EDR += 1
                        If EDR < 10 Then : ESTR = "PG" & "0000000" & EDR
                        ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PG" & "000000" & EDR
                        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PG" & "00000" & EDR
                        ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PG" & "0000" & EDR
                        ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PG" & "000" & EDR
                        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PG" & "00" & EDR
                        ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PG" & "0" & EDR
                        ElseIf EDR > 9999999 Then : ESTR = "PG" & EDR
                        End If
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        If DataGridView2.Rows(i).Cells("出貨數量").Value = 0 Then
                            EDRW = DataGridView2.Rows(i).Cells("訂單數量").Value
                        Else
                            EDRW = DataGridView2.Rows(i).Cells("訂單數量").Value - DataGridView2.Rows(i).Cells("出貨數量").Value
                        End If
                        SQL1 = "INSERT INTO 貨款匯入出款明細表 (貨款流水號, 資料流水號, 訂單流水號, 年份季度, 客戶訂單號, 形體號, 顏色, 楦型, 訂單數量, 出貨數量, " &
                           "工廠價格, 實際離廠日, 匯款時間, 入帳時間, 貨款備註) " &
                           "VALUES ('" & ESTR & "','" & DataGridView2.Rows(i).Cells("資料流水號").Value & "','" & DataGridView2.Rows(i).Cells("訂單流水號").Value &
                           "','" & DataGridView2.Rows(i).Cells("年份季度").Value & "','" & DataGridView2.Rows(i).Cells("客戶訂單號").Value &
                           "','" & DataGridView2.Rows(i).Cells("型體名稱").Value & "','" & DataGridView2.Rows(i).Cells("顏色").Value &
                           "','" & DataGridView2.Rows(i).Cells("楦型").Value & "','" & DataGridView2.Rows(i).Cells("訂單數量").Value &
                           "','" & EDRW & "', '', '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', '', '', '')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Next i

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單控制表 SET 實際離廠日 = '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', 出貨數量 = 訂單數量 " &
                       "WHERE (訂單流水號 = '" & TextBox12.Text & "')"
                    cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                End If
                MsgBox("出貨資料作業完成")
            End If
        End If
        Set_清單3() : TextBox1.Text = "" : TextBox2.Text = ""
    End Sub
    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) : PictureBox2.Refresh() : PictureBox2.SizeMode = 4
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate270FlipNone) : PictureBox2.Refresh() : PictureBox2.SizeMode = 4
    End Sub
    Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate180FlipY) : PictureBox2.Refresh() : PictureBox2.SizeMode = 4
    End Sub
    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate180FlipX) : PictureBox2.Refresh() : PictureBox2.SizeMode = 4
    End Sub
    Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
        驗貨報告大圖.ShowDialog()
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If Val(TextBox1.Text) > Val(TextBox13.Text) Then
            MsgBox("出貨數量不可大於可出貨數量") : TextBox1.Text = "0"
        End If
    End Sub
    Private Sub ComboBox10_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox10.SelectedIndexChanged
        PA = ComboBox10.Text : Set_清單3() : PA = ""
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Button3.Enabled = False : Button34.Enabled = True : Button35.Enabled = True : Button4.Enabled = True : Button6.Enabled = True
        Set_grid4()
    End Sub
    Private Sub Button34_Click(sender As Object, e As EventArgs) Handles Button34.Click
        Button3.Enabled = True : Button34.Enabled = False : Button35.Enabled = True : Button4.Enabled = True : Button6.Enabled = True
        Set_grid4()
    End Sub
    Private Sub Button35_Click(sender As Object, e As EventArgs) Handles Button35.Click
        Button3.Enabled = True : Button34.Enabled = True : Button35.Enabled = False : Button4.Enabled = True : Button6.Enabled = True
        Set_grid4()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Button3.Enabled = True : Button34.Enabled = True : Button35.Enabled = True : Button4.Enabled = False : Button6.Enabled = True
        Set_grid4()
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Button3.Enabled = True : Button34.Enabled = True : Button35.Enabled = True : Button4.Enabled = True : Button6.Enabled = False
        Set_grid4()
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        列印_量產訂單控制表.ShowDialog()
    End Sub
    Private Sub 啟用批量出貨_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 啟用批量出貨_ch.Click
        If 啟用批量出貨_ch.Checked = False Then
            批量出貨_bt.Enabled = False
        Else
            批量出貨_bt.Enabled = True
        End If
    End Sub
    Private Sub 批量出貨_bt_Click(sender As Object, e As EventArgs) Handles 批量出貨_bt.Click
        aa = MsgBox("執行批量出貨之前需要在訂單控制表清單中先勾選要出貨的資料，請確定你已完成出貨資料選擇?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            Dim EDR, SD As Integer
            For I As Integer = 0 To 控制表_dgv.Rows.Count - 1
                資料數 = 控制表_dgv.Rows.Count : MyModule1.進度條()
                If 控制表_dgv("選擇", I).Value = True And 控制表_dgv("驗貨報告圖片", I).Value.ToString <> "" Then
                    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 : EDR = Double.Parse(Strings.Right(dr("貨款流水號").ToString, 8)) : Else : EDR = 0 : End If
                    conn.Close()
                    EDR += 1
                    If EDR < 10 Then : ESTR = "PG" & "0000000" & EDR
                    ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PG" & "000000" & EDR
                    ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PG" & "00000" & EDR
                    ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PG" & "0000" & EDR
                    ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PG" & "000" & EDR
                    ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PG" & "00" & EDR
                    ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PG" & "0" & EDR
                    ElseIf EDR > 9999999 Then : ESTR = "PG" & EDR
                    End If

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 貨款匯入出款明細表 (貨款流水號, 資料流水號, 訂單流水號, 年份季度, 客戶訂單號, 形體號, 顏色, 楦型, 訂單數量, 出貨數量, " &
                       "工廠價格, 實際離廠日, 匯款時間, 入帳時間, 貨款備註) " &
                       "VALUES ('" & ESTR & "','" & 控制表_dgv.Rows(I).Cells("資料流水號").Value & "','" & 控制表_dgv.Rows(I).Cells("訂單流水號").Value &
                       "','" & 控制表_dgv.Rows(I).Cells("年份季度").Value & "','" & 控制表_dgv.Rows(I).Cells("客戶訂單號").Value &
                       "','" & 控制表_dgv.Rows(I).Cells("型體名稱").Value & "','" & 控制表_dgv.Rows(I).Cells("顏色").Value &
                       "','" & 控制表_dgv.Rows(I).Cells("楦型").Value & "','" & 控制表_dgv.Rows(I).Cells("訂單數量").Value &
                       "','" & 控制表_dgv.Rows(I).Cells("訂單數量").Value & "', '', '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', '', '', '')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單控制表 SET 實際離廠日 = '" & Format(DateTimePicker30.Value, "yyyy/MM/dd") & "', 出貨數量 = '" & 控制表_dgv.Rows(I).Cells("訂單數量").Value & "' " &
                       "WHERE (資料流水號 = '" & 控制表_dgv.Rows(I).Cells("資料流水號").Value & "')"
                    cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    SD += 1
                End If
            Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
            MsgBox("一共出貨 " & SD & " 筆資料!!")
            Set_清單3() : 啟用批量出貨_ch.Checked = False : 批量出貨_bt.Enabled = False
        End If
    End Sub
End Class