Option Strict Off
Imports System.Data.SqlClient
Public Class 成本資料庫管理
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds3 As New DataSet : ReadOnly ds4 As New DataSet : ReadOnly ds5 As New DataSet
    Private Sub Set_訂單清單()
        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
        SQL1 = "SELECT     訂單控制表.BUYER, 訂單控制表.年份季度, 訂單控制表.訂單流水號, 訂單控制表.客戶訂單號, 訂單控制表.型體號碼, 訂單控制表.GOODBOX AS 型體名稱, 
                           訂單控制表.APMA確認 AS 顏色, 訂單控制表.TVW AS 楦型, 訂單控制表.訂單數量, 訂單控制表.客戶交期 AS 交期開始, 訂單控制表.客戶交期1 AS 交期結束, 訂單控制表.單價, 
                           訂單控制表.總金額 AS 銷售金額, 訂單控制表.備註, 訂單控制表.生產工廠, 訂單控制表.資料流水號, 訂單價格利潤明細表.Quantity, 訂單價格利潤明細表.Currency, 
                           訂單價格利潤明細表.Exchange, 訂單價格利潤明細表.Price, 訂單價格利潤明細表.Discount_price AS [Discount price], 
                           訂單價格利潤明細表.Commercial_invoice AS [Commercial invoice], 訂單價格利潤明細表.Overage_invoice AS [Overage invoice], 
                           訂單價格利潤明細表.Selling_price AS [Selling price], 訂單價格利潤明細表.HTS_code AS [HTS code], 訂單價格利潤明細表.Remark1, 
                           訂單價格利潤明細表.Fty_price AS [Fty price], 訂單價格利潤明細表.Mold_cost AS [Mold cost], 訂單價格利潤明細表.Collected_mold_cost AS [Collected mold cost], 
                           訂單價格利潤明細表.Amortization_qty AS [Amortization qty], 訂單價格利潤明細表.Profit, 訂單價格利潤明細表.Commission, 訂單價格利潤明細表.Remark2, 
                           訂單價格利潤明細表.Price AS 客戶付款, 訂單價格利潤明細表.Price AS 未收款項, 訂單價格利潤明細表.Price AS 預收款項, 訂單價格利潤明細表.Price AS 成本金額, 
                           訂單價格利潤明細表.Price AS 利潤, 訂單價格利潤明細表.Price AS 實際收款, 訂單價格利潤明細表.年份, 訂單價格利潤表暫存檔.利潤表流水號, 訂單價格利潤明細表.審核
                FROM       訂單控制表 INNER JOIN 訂單價格利潤表暫存檔 ON 訂單控制表.資料流水號 = 訂單價格利潤表暫存檔.資料流水號 LEFT OUTER JOIN
                           訂單價格利潤明細表 ON 訂單控制表.資料流水號 = 訂單價格利潤明細表.資料流水號
                WHERE (訂單價格利潤明細表.年份 LIKE '" & ComboBox1.Text & "') AND (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.訂單流水號 IS NULL) 
                ORDER BY   訂單控制表.訂單流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_DGV2載入前設定()
        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
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_DGV3載入前設定()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        DataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView3.ColumnHeadersHeight = 25
        DataGridView3.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV3載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_DGV4載入前設定()
        DataGridView4.DataSource = Nothing : ds3.Clear()
        DataGridView4.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView4.ColumnHeadersHeight = 25
        DataGridView4.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV4載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds3) : DataGridView4.DataSource = ds3.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_DGV5載入前設定()
        DataGridView5.DataSource = Nothing : ds4.Clear()
        DataGridView5.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView5.ColumnHeadersHeight = 25
        DataGridView5.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV5載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds4) : DataGridView5.DataSource = ds4.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_DGV6載入前設定()
        DataGridView6.DataSource = Nothing : ds5.Clear()
        DataGridView6.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView6.ColumnHeadersHeight = 25
        DataGridView6.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV6載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds5) : DataGridView6.DataSource = ds5.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Visible = False : DataGridView1.Columns(1).Visible = False : DataGridView1.Columns(2).Visible = False : DataGridView1.Columns(3).Visible = False
        DataGridView1.Columns(4).Visible = False : DataGridView1.Columns(5).Visible = False : DataGridView1.Columns(6).Visible = False : DataGridView1.Columns(7).Visible = False
        DataGridView1.Columns(8).Visible = False : DataGridView1.Columns(9).Visible = False : DataGridView1.Columns(10).Visible = False : DataGridView1.Columns(11).Visible = False
        DataGridView1.Columns(12).Visible = False : DataGridView1.Columns(13).Visible = False : DataGridView1.Columns(18).Visible = False : DataGridView1.Columns(19).Visible = False
        DataGridView1.Columns(20).Visible = False : DataGridView1.Columns(21).Visible = False : DataGridView1.Columns(24).Visible = False : DataGridView1.Columns(25).Visible = False
        DataGridView1.Columns(26).Visible = False : DataGridView1.Columns(27).Visible = False : DataGridView1.Columns(28).Visible = False : DataGridView1.Columns(29).Visible = False
        DataGridView1.Columns(30).Visible = False : DataGridView1.Columns(31).Visible = False : DataGridView1.Columns(34).Visible = False
        DataGridView1.Columns(14).Width = 85 : DataGridView1.Columns(15).Width = 85 : DataGridView1.Columns(16).Width = 85 : DataGridView1.Columns(17).Width = 85
        DataGridView1.Columns(22).Width = 85 : DataGridView1.Columns(23).Width = 85 : DataGridView1.Columns(32).Width = 85 : DataGridView1.Columns(33).Width = 110
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 90 : DataGridView2.Columns(1).Width = 80 : DataGridView2.Columns(2).Width = 120 : DataGridView2.Columns(3).Width = 70 : DataGridView2.Columns(4).Width = 80
        DataGridView3.Columns(0).Width = 90 : DataGridView3.Columns(1).Width = 90 : DataGridView3.Columns(2).Width = 120 : DataGridView3.Columns(3).Width = 70 : DataGridView3.Columns(4).Width = 90
        DataGridView3.Columns(5).Width = 90 : DataGridView3.Columns(6).Width = 80 : DataGridView3.Columns(7).Width = 70
        DataGridView4.Columns(0).Width = 90 : DataGridView4.Columns(1).Width = 70 : DataGridView4.Columns(2).Width = 120 : DataGridView4.Columns(3).Width = 90 : DataGridView4.Columns(4).Width = 80
        DataGridView4.Columns(5).Width = 70
        DataGridView5.Columns(0).Width = 80 : DataGridView5.Columns(1).Width = 105 : DataGridView5.Columns(2).Width = 80 : DataGridView5.Columns(3).Width = 90 : DataGridView5.Columns(4).Width = 90
        DataGridView5.Columns(5).Width = 90 : DataGridView5.Columns(6).Width = 70
        DataGridView6.Columns(0).Width = 90 : DataGridView6.Columns(1).Width = 70 : DataGridView6.Columns(2).Width = 120 : DataGridView6.Columns(3).Width = 90 : DataGridView6.Columns(4).Width = 80
        DataGridView6.Columns(5).Width = 70
    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
        ComboBox1.Text = Year(Today)
        Set_訂單清單() : Set_grid()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Set_訂單清單() : Set_grid()
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1(33, e.RowIndex).Value.ToString
            Set_DGV2載入前設定()
            SQL1 = "SELECT 資料流水號, 訂單流水號, 客戶流水號, 年份, 利潤表流水號 FROM 訂單價格利潤明細表 WHERE (利潤表流水號 LIKE '" & TextBox1.Text & "')"
            Set_DGV2載入後設定()

            TextBox2.Text = DataGridView2(1, 0).Value.ToString : TextBox3.Text = DataGridView2(0, 0).Value.ToString

            Set_DGV3載入前設定()
            SQL1 = "SELECT 訂單價格利潤明細表.資料流水號, 訂單價格利潤明細表.天盾訂單號, 訂單價格利潤明細表.客戶流水號, 訂單價格利潤明細表.年份, 訂單價格利潤明細表.利潤表流水號, 訂單控制表.形體號, " &
                   "訂單控制表.顏色, 訂單控制表.楦型 FROM 訂單價格利潤明細表 LEFT OUTER JOIN 訂單控制表 ON 訂單價格利潤明細表.資料流水號 = 訂單控制表.資料流水號 " &
                   "WHERE (訂單價格利潤明細表.天盾訂單號 LIKE '" & TextBox2.Text & "') ORDER BY 訂單控制表.形體號, 訂單控制表.顏色, 訂單控制表.楦型"
            Set_DGV3載入後設定()

            Set_DGV4載入前設定()
            SQL1 = "SELECT 訂單流水號, 年份季度, 客戶訂單號, 形體號, 顏色, 楦型 FROM 訂單控制表 WHERE (訂單流水號 LIKE '" & TextBox2.Text & "') ORDER BY   形體號, 顏色, 楦型"
            Set_DGV4載入後設定()

            Set_DGV5載入前設定()
            SQL1 = "SELECT 天盾流水號, 客戶訂單號, ItemNumber, ProductName, ProductDescription, Color, DM FROM 訂單明細表 WHERE (天盾流水號 LIKE '" & TextBox2.Text & "') ORDER BY   ProductName, Color, DM"
            Set_DGV5載入後設定()

            Set_DGV6載入前設定()
            SQL1 = "SELECT 訂單流水號, 年份季度, 客戶訂單號, 形體號, 顏色, 楦型 FROM 訂單控制表 WHERE (資料流水號 LIKE '" & TextBox3.Text & "') ORDER BY   形體號, 顏色, 楦型"
            Set_DGV6載入後設定()
            Set_grid1()
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim aa As MsgBoxResult
        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 (利潤表流水號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            Set_訂單清單() : Set_grid()
            TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = ""
            DataGridView2.DataSource = Nothing : DataGridView3.DataSource = Nothing : DataGridView4.DataSource = Nothing : DataGridView5.DataSource = Nothing : DataGridView6.DataSource = Nothing
        End If
        MsgBox("刪除完成")
    End Sub
End Class