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 = 40 : DataGridView1.Columns(2).Width = 180 : DataGridView1.Columns(3).Width = 180 : DataGridView1.Columns(4).Width = 180
        DataGridView1.Columns(5).Width = 80 : DataGridView1.Columns(6).Width = 80 : DataGridView1.Columns(7).Width = 80 : DataGridView1.Columns(8).Width = 285
        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 (核准人 NOT LIKE N'') AND (核准人 NOT LIKE N'作廢%') ORDER BY 請購單號 DESC"
        Else
            SQL1 = "SELECT 請購單號, 申請人, LEFT(申請日期, 10) AS 申請日期, 核准人, LEFT(核准日期, 10) AS 核准日期, 已採購, 轉請款, 零用金單號 FROM 一般物料請購單控制表 " &
                   "WHERE (申請人 LIKE N'" & gUserName & "') AND (核准人 NOT LIKE N'') AND (核准人 NOT 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 = 135 : DataGridView2.Columns(1).Width = 80 : DataGridView2.Columns(2).Width = 90 : DataGridView2.Columns(3).Width = 80 : DataGridView2.Columns(4).Width = 90
        DataGridView2.Columns(5).Width = 60 : DataGridView2.Columns(6).Width = 60 : DataGridView2.Columns(7).Width = 135
    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()
    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 Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇請購單據")
        Else
            Dim GGG As Boolean
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 已採購 FROM 一般物料請購單控制表 WHERE ( 請購單號 LIKE N'" & TextBox1.Text & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : GGG = dr("已採購") : End If : conn.Close()

            If GGG = True Then
                MsgBox("該單據已經確認過")
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 一般物料請購單控制表 SET 已採購 = N'1' WHERE (請購單號 = N'" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                MsgBox("單據  " & TextBox1.Text & "  確認採購完成")
                Set_清單1()
            End If
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss") : DTP1 = Format(Today(), "yyyyMMdd")
    End Sub
    Dim WWW As String
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇請購單據")
        Else
            Dim GGG As Boolean
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 已採購 FROM 一般物料請購單控制表 WHERE ( 請購單號 LIKE N'" & TextBox1.Text & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : GGG = dr("已採購") : End If : conn.Close()

            If GGG = False Then
                MsgBox("物料未確認已經採購之前，無法轉零用金支付單請款")
            Else
                Dim AAA As Boolean
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 轉請款 FROM 一般物料請購單控制表 WHERE ( 請購單號 LIKE N'" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then : AAA = dr("轉請款") : End If : conn.Close()

                If AAA = True Then
                    MsgBox("該單據已經轉過零用金支付單，無法重複開立")
                Else
                    Set_日期格式轉換() : Dim NUM1 As Integer
                    For i As Integer = 1 To 999
                        NUM1 = 0 + i
                        If NUM1 < 10 Then : WWW = "PC-" & DTP1 & "-00" & NUM1
                        ElseIf NUM1 > 9 And NUM1 < 100 Then : WWW = "PC-" & DTP1 & "-0" & NUM1
                        ElseIf NUM1 > 99 Then : WWW = "PC-" & 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'" & WWW & "')"
                        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'" & WWW & "',N'" & 登入人部門 & "',N'" & gUserName & "',N'" & DTP & "',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                    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 = "INSERT INTO 零用金支付明細 (流水號, 用途, 金額, 備註, 項次) " &
                               "VALUES (N'" & WWW & "',N'" & DataGridView1.Rows(i).Cells("品名").Value & "-" & DataGridView1.Rows(i).Cells("規格").Value & "-" & DataGridView1.Rows(i).Cells("備註").Value &
                               "',N'" & DataGridView1.Rows(i).Cells("金額").Value & "',N'" & DataGridView1.Rows(i).Cells("用途").Value & "',N'" & DataGridView1.Rows(i).Cells("項次").Value & "')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 一般物料請購單控制表 SET 轉請款 = N'1', 零用金單號 = '" & WWW & "' WHERE (請購單號 = N'" & TextBox1.Text & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    MsgBox("單據  " & TextBox1.Text & "  確認採購完成")
                    Set_清單1()
                End If
            End If
        End If
    End Sub
End Class