Option Strict Off
Imports System.Data.SqlClient
Public Class 一般物料請購單核准
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet : Dim ds4 As New DataSet
    Dim dr As SqlDataReader
    Dim DTP As String : Dim DTP1 As String : Dim 新項次 As String
    Dim NUM1 As Integer
    Private Sub Set_使用者清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowTemplate.Height = 35
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 請購單號, 項次, 品名, 規格, 備註, 數量, 單價, 數量 * 單價 AS 金額, 用途 FROM 一般物料請購單明細表 WHERE 請購單號 LIKE N'" & TextBox1.Text & "' ORDER BY 項次"
        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_grid()
        DataGridView1.Columns(0).Visible = False : DataGridView1.Columns(1).Width = 80 : DataGridView1.Columns(2).Width = 200 : DataGridView1.Columns(3).Width = 200 : DataGridView1.Columns(4).Width = 200
        DataGridView1.Columns(5).Width = 80 : DataGridView1.Columns(6).Width = 80 : DataGridView1.Columns(7).Width = 80 : DataGridView1.Columns(8).Width = 305
        DataGridView1.Columns(5).DefaultCellStyle.Format = "#,##0.00" : DataGridView1.Columns(6).DefaultCellStyle.Format = "#,##0.00" : DataGridView1.Columns(7).DefaultCellStyle.Format = "#,##0.00"
        DataGridView1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : DataGridView1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight : DataGridView1.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter

        TextBox5.Text = "0.00"
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            TextBox5.Text = Val(TextBox5.Text) + DataGridView1.Rows(i).Cells("金額").Value
        Next i
        TextBox5.Text = Format(Val(TextBox5.Text), "#,##0.00")
    End Sub
    Private Sub Set_清單1()
        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
        If 登入人部門 = "NA" Then
            SQL1 = "SELECT 請購單號, 申請人, LEFT(申請日期, 10) AS 申請日期, 核准人, LEFT(核准日期, 10) AS 核准日期 FROM 一般物料請購單控制表 " &
                   "WHERE (核准人 LIKE N'') ORDER BY 請購單號 DESC"
        Else
            SQL1 = "SELECT 請購單號, 申請人, LEFT(申請日期, 10) AS 申請日期, 核准人, LEFT(核准日期, 10) AS 核准日期 FROM 一般物料請購單控制表 " &
                   "WHERE (申請人 LIKE N'" & gUserName & "') AND (核准人 LIKE N'') ORDER BY 請購單號 DESC"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 150 : DataGridView2.Columns(1).Width = 115 : DataGridView2.Columns(2).Width = 115 : DataGridView2.Columns(3).Width = 115 : DataGridView2.Columns(4).Width = 115
    End Sub
    Private Sub Set_清單3()
        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
        If 登入人部門 = "NA" Then
            SQL1 = "SELECT 請購單號, 申請人, LEFT(申請日期, 10) AS 申請日期, 核准人, LEFT(核准日期, 10) AS 核准日期 FROM 一般物料請購單控制表 " &
                   "WHERE (核准人 LIKE N'作廢%') ORDER BY 請購單號 DESC"
        Else
            SQL1 = "SELECT 請購單號, 申請人, LEFT(申請日期, 10) AS 申請日期, 核准人, LEFT(核准日期, 10) AS 核准日期 FROM 一般物料請購單控制表 " &
                   "WHERE (申請人 LIKE N'" & gUserName & "') AND (核准人 LIKE N'作廢%') ORDER BY 請購單號 DESC"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds3) : DataGridView4.DataSource = ds3.Tables(0) : conn.Close() : Set_grid3()
    End Sub
    Private Sub Set_grid3()
        DataGridView4.Columns(0).Width = 150 : DataGridView4.Columns(1).Width = 115 : DataGridView4.Columns(2).Width = 115 : DataGridView4.Columns(3).Width = 115 : DataGridView4.Columns(4).Width = 115
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss") : DTP1 = Format(Today(), "yyyyMMdd")
    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
        Set_清單1() : Set_清單3()
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView2(0, e.RowIndex).Value.ToString : TextBox3.Text = DataGridView2(1, e.RowIndex).Value.ToString : DateTimePicker1.Value = DataGridView2(2, e.RowIndex).Value.ToString
            Button3.Enabled = True : Button5.Enabled = True : Set_使用者清單()
        End If
    End Sub
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView4.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView4(0, e.RowIndex).Value.ToString : TextBox3.Text = DataGridView4(1, e.RowIndex).Value.ToString : DateTimePicker1.Value = DataGridView4(2, e.RowIndex).Value.ToString
            Button3.Enabled = False : Button5.Enabled = False : Set_使用者清單()
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Set_日期格式轉換()
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇請購單據")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 一般物料請購單控制表 SET 核准人 = N'" & gUserName & "', 核准日期 = N'" & DTP & "' WHERE (請購單號 = N'" & TextBox1.Text & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            MsgBox("單據  " & TextBox1.Text & "  核准完成")
            Set_清單1() : Set_清單3()
            TextBox1.Text = "" : TextBox5.Text = "" : TextBox3.Text = ""
            DataGridView1.DataSource = Nothing : ds.Clear()
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Set_日期格式轉換()
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇請購單據")
        Else
            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 = "UPDATE 一般物料請購單控制表 SET 核准人 = N'作廢-" & gUserName & "', 核准日期 = N'" & DTP & "' WHERE (請購單號 = N'" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                MsgBox("單據  " & TextBox1.Text & "  已作廢")
                Set_清單1() : Set_清單3()
                TextBox1.Text = "" : TextBox5.Text = "" : TextBox3.Text = ""
                DataGridView1.DataSource = Nothing : ds.Clear()
            End If
        End If
    End Sub
End Class