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
        Button2.Enabled = False : Button6.Enabled = False : Button15.Enabled = False : Button3.Enabled = False : Button4.Enabled = False : Button5.Enabled = False
        TextBox1.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False
        Set_清單1() : Set_清單3()
    End Sub
    Private Sub 一般物料請購單申請_Unload(sender As Object, e As EventArgs) Handles MyBase.Closing
        If Button2.Enabled = True Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 一般物料請購單控制表 WHERE (請購單號 LIKE N'" & TextBox1.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'" & 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
            TextBox4.Text = DataGridView1(1, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells("金額").Value = Val(DataGridView1.Rows(i).Cells("數量").Value) * Val(DataGridView1.Rows(i).Cells("單價").Value)
        Next i
    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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Enabled = True
        Button1.Enabled = False : Button2.Enabled = True : Button6.Enabled = True : Button15.Enabled = True
        Set_日期格式轉換() : Dim NUM1 As Integer
        For i As Integer = 1 To 999
            NUM1 = 0 + i
            If NUM1 < 10 Then : TextBox1.Text = "RE-" & DTP1 & "-00" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox1.Text = "RE-" & DTP1 & "-0" & NUM1
            ElseIf NUM1 > 99 Then : TextBox1.Text = "RE-" & DTP1 & "-" & NUM1
            End If
            conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP (1)  請購單號 FROM 一般物料請購單控制表 WHERE ( 請購單號 LIKE N'" & TextBox1.Text & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
        Next
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 一般物料請購單控制表 (請購單號, 申請人, 申請日期, 核准人, 核准日期, 已採購, 轉請款, 零用金單號) " &
               "VALUES (N'" & TextBox1.Text & "',N'" & gUserName & "',N'" & DTP & "',N'',N'',N'0',N'0',N'')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
        TextBox3.Text = gUserName
        DataGridView1.DataSource = Nothing : ds.Clear()
        conn.Close()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer1.Enabled = False : Label62.Visible = False
        Button1.Enabled = True : Button2.Enabled = False : Button6.Enabled = False : Button15.Enabled = False
        存檔()
        TextBox1.Text = "" : TextBox3.Text = "" : TextBox4.Text = ""
        DataGridView1.DataSource = Nothing : ds.Clear()
        MsgBox("申請完成，請通知總經理審核。")
        Set_清單1() : Set_清單3()
    End Sub
    Private Sub 存檔()
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            資料數 = DataGridView1.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 一般物料請購單明細表 SET  品名 = N'" & DataGridView1.Rows(i).Cells("品名").Value & "', 規格 = N'" & DataGridView1.Rows(i).Cells("規格").Value &
                   "', 備註 = N'" & DataGridView1.Rows(i).Cells("備註").Value & "', 數量 = N'" & DataGridView1.Rows(i).Cells("數量").Value & "', 單價 = N'" & DataGridView1.Rows(i).Cells("單價").Value &
                   "', 用途 = N'" & DataGridView1.Rows(i).Cells("用途").Value & "' WHERE (請購單號 LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        存檔()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP(1) 項次 FROM 一般物料請購單明細表 WHERE 請購單號 LIKE N'" & TextBox1.Text & "' ORDER BY 項次 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 新項次 = dr("項次") : Else : 新項次 = "00" : End If : conn.Close()
        NUM1 = Double.Parse(新項次) + 1
        If NUM1 < 10 Then : 新項次 = "0" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1
        End If
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 一般物料請購單明細表 (請購單號, 品名, 規格, 備註, 數量, 單價, 用途, 項次) " &
               "VALUES (N'" & TextBox1.Text & "',N'',N'',N'',N'0',N'0',N'',N'" & 新項次 & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
        Set_使用者清單()
    End Sub
    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        If TextBox4.Text = "" Then
            MsgBox("未選擇需要刪除的資料")
        Else
            存檔()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 一般物料請購單明細表 WHERE (請購單號 LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & TextBox4.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            Set_使用者清單()
            Dim NUM1 As Integer = 0
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                NUM1 = NUM1 + 1 : If NUM1 < 10 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1 : End If
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 一般物料請購單明細表 SET 項次 = N'" & 新項次 & "' WHERE (請購單號 LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
                conn.Close()
            Next
            Set_使用者清單()
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Button3.Enabled = False : Button4.Enabled = True : Button6.Enabled = True : Button15.Enabled = True
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Button3.Enabled = True : Button4.Enabled = False : Button6.Enabled = False : Button15.Enabled = False
        存檔()
    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 (請購單號 LIKE N'" & TextBox1.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'" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            TextBox1.Text = "" : TextBox3.Text = "" : TextBox4.Text = ""
            DataGridView1.DataSource = Nothing : ds.Clear()
            Set_清單1() : Set_清單3()
        End If
    End Sub
End Class