Option Strict Off
Public Class 零用金支付申請
    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 DTP As String : Dim DTP1 As String : Dim 新項次 As String
    Dim NUM1 As Integer
    Private Sub Set_財務單清單()
        明細_dgv.DataSource = Nothing : ds.Clear()
        明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        明細_dgv.ColumnHeadersHeight = 25
        明細_dgv.AllowUserToAddRows = False
        明細_dgv.RowTemplate.Height = 35

        PA1 = TextBox1.Text : SQL_財務帳清單()
        da.Fill(ds) : 明細_dgv.DataSource = ds.Tables(0) : conn.Close()

        明細_dgv.Columns(0).Visible = False : 明細_dgv.Columns(1).Width = 80 : 明細_dgv.Columns(2).Width = 342 : 明細_dgv.Columns(3).Width = 140 : 明細_dgv.Columns(4).Width = 350
        明細_dgv.Columns(3).DefaultCellStyle.Format = "#,##0.00"
        明細_dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        TextBox5.Text = "0.00"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            TextBox5.Text = Val(TextBox5.Text) + 明細_dgv.Rows(i).Cells("金額").Value
        Next i
        TextBox5.Text = Format(Val(TextBox5.Text), "#,##0.00")
    End Sub
    Private Sub Set_清單1()
        一級未審核_dgv.DataSource = Nothing : ds1.Clear()
        一級未審核_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        一級未審核_dgv.ColumnHeadersHeight = 25
        一級未審核_dgv.AllowUserToAddRows = False

        PA = "一級未審核" : SQL_財務單據()
        da.Fill(ds1) : 一級未審核_dgv.DataSource = ds1.Tables(0) : conn.Close()

        一級未審核_dgv.Columns(0).Width = 120 : 一級未審核_dgv.Columns(1).Width = 114 : 一級未審核_dgv.Columns(2).Width = 114 : 一級未審核_dgv.Columns(3).Width = 115
        一級未審核_dgv.Columns(4).Width = 115 : 一級未審核_dgv.Columns(5).Width = 115 : 一級未審核_dgv.Columns(6).Width = 115 : 一級未審核_dgv.Columns(7).Width = 115
        一級未審核_dgv.Columns(8).Visible = False : 一級未審核_dgv.Columns(9).Visible = False
    End Sub
    Private Sub Set_清單2()
        一級作廢_dgv.DataSource = Nothing : ds2.Clear()
        一級作廢_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        一級作廢_dgv.ColumnHeadersHeight = 25
        一級作廢_dgv.AllowUserToAddRows = False

        PA = "一級作廢" : SQL_財務單據()
        da.Fill(ds2) : 一級作廢_dgv.DataSource = ds2.Tables(0) : conn.Close()

        一級作廢_dgv.Columns(0).Width = 120 : 一級作廢_dgv.Columns(1).Width = 114 : 一級作廢_dgv.Columns(2).Width = 114 : 一級作廢_dgv.Columns(3).Width = 115
        一級作廢_dgv.Columns(4).Width = 115 : 一級作廢_dgv.Columns(5).Width = 115 : 一級作廢_dgv.Columns(6).Width = 115 : 一級作廢_dgv.Columns(7).Width = 115
        一級作廢_dgv.Columns(8).Visible = False : 一級作廢_dgv.Columns(9).Visible = False
    End Sub
    Private Sub Set_清單3()
        二級未核准_dgv.DataSource = Nothing : ds3.Clear()
        二級未核准_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        二級未核准_dgv.ColumnHeadersHeight = 25
        二級未核准_dgv.AllowUserToAddRows = False

        PA = "二級未核准" : SQL_財務單據()
        da.Fill(ds3) : 二級未核准_dgv.DataSource = ds3.Tables(0) : conn.Close()

        二級未核准_dgv.Columns(0).Width = 120 : 二級未核准_dgv.Columns(1).Width = 114 : 二級未核准_dgv.Columns(2).Width = 114 : 二級未核准_dgv.Columns(3).Width = 115
        二級未核准_dgv.Columns(4).Width = 115 : 二級未核准_dgv.Columns(5).Width = 115 : 二級未核准_dgv.Columns(6).Width = 115 : 二級未核准_dgv.Columns(7).Width = 115
        二級未核准_dgv.Columns(8).Visible = False : 二級未核准_dgv.Columns(9).Visible = False
    End Sub
    Private Sub Set_清單4()
        二級作廢_dgv.DataSource = Nothing : ds4.Clear()
        二級作廢_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        二級作廢_dgv.ColumnHeadersHeight = 25
        二級作廢_dgv.AllowUserToAddRows = False

        PA = "二級作廢" : SQL_財務單據()
        da.Fill(ds4) : 二級作廢_dgv.DataSource = ds4.Tables(0) : conn.Close()

        二級作廢_dgv.Columns(0).Width = 120 : 二級作廢_dgv.Columns(1).Width = 114 : 二級作廢_dgv.Columns(2).Width = 114 : 二級作廢_dgv.Columns(3).Width = 115
        二級作廢_dgv.Columns(4).Width = 115 : 二級作廢_dgv.Columns(5).Width = 115 : 二級作廢_dgv.Columns(6).Width = 115 : 二級作廢_dgv.Columns(7).Width = 115
        二級作廢_dgv.Columns(8).Visible = False : 二級作廢_dgv.Columns(9).Visible = False
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss") : DTP1 = Format(Today(), "yyyyMMdd")
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        SQL_帳本下拉清單()
        帳本名稱_cb.Items.Clear() : 帳本編號_cb.Items.Clear() : While (dr.Read()) : 帳本名稱_cb.Items.Add(dr("帳本名稱")) : 帳本編號_cb.Items.Add(dr("編號")) : End While : conn.Close()
    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 : TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : 帳本編號_cb.Visible = False
        ComboBox1下拉表單資料載入() : 帳本名稱_cb.SelectedIndex = 0 : PA19 = 帳本編號_cb.Text
        Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
    End Sub
    Private Sub 零用金支付申請_Unload(sender As Object, e As EventArgs) Handles MyBase.Closing
        If Button2.Enabled = True Then
            PA = TextBox1.Text : SQL_財務單據刪除() : 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 明細_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox4.Text = 明細_dgv(1, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 一級未審核_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 一級未審核_dgv(0, e.RowIndex).Value.ToString : TextBox2.Text = 一級未審核_dgv(1, e.RowIndex).Value.ToString : TextBox3.Text = 一級未審核_dgv(2, e.RowIndex).Value.ToString
            Button3.Enabled = True : Button5.Enabled = True : Set_財務單清單()
        End If
    End Sub
    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 一級作廢_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 一級作廢_dgv(0, e.RowIndex).Value.ToString : TextBox2.Text = 一級作廢_dgv(1, e.RowIndex).Value.ToString : TextBox3.Text = 一級作廢_dgv(2, e.RowIndex).Value.ToString
            Button3.Enabled = False : Button5.Enabled = False : Set_財務單清單()
        End If
    End Sub
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 二級未核准_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 二級未核准_dgv(0, e.RowIndex).Value.ToString : TextBox2.Text = 二級未核准_dgv(1, e.RowIndex).Value.ToString : TextBox3.Text = 二級未核准_dgv(2, e.RowIndex).Value.ToString
            Button3.Enabled = False : Button5.Enabled = False : Set_財務單清單()
        End If
    End Sub
    Private Sub DataGridView5_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 二級作廢_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 二級作廢_dgv(0, e.RowIndex).Value.ToString : TextBox2.Text = 二級作廢_dgv(1, e.RowIndex).Value.ToString : TextBox3.Text = 二級作廢_dgv(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 = "PC-" & DTP1 & "-00" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox1.Text = "PC-" & DTP1 & "-0" & NUM1
            ElseIf NUM1 > 99 Then : TextBox1.Text = "PC-" & DTP1 & "-" & NUM1 : End If
            PA = TextBox1.Text : SQL_零用金支付表最後一筆資料()
            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'" & PA & "',N'" & 登入人部門 & "',N'" & gUserName & "',N'" & DTP & "',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'" & 帳本編號_cb.Text & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
        TextBox2.Text = 登入人部門 : TextBox3.Text = gUserName
        明細_dgv.DataSource = Nothing : ds.Clear()
        conn.Close()
    End Sub
    Private Sub 存檔()
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            資料數 = 明細_dgv.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 零用金支付明細 SET  用途 = N'" & 明細_dgv.Rows(i).Cells("用途").Value & "', 金額 = N'" & 明細_dgv.Rows(i).Cells("金額").Value &
                   "', 備註 = N'" & 明細_dgv.Rows(i).Cells("備註").Value & "' WHERE (流水號 LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & 明細_dgv.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'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 明細_dgv.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'" & 明細_dgv.Rows(i).Cells("項次").Value & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
                conn.Close()
            Next
            Set_財務單清單()
        End If
    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 = "" : TextBox2.Text = "" : TextBox3.Text = "" : TextBox4.Text = ""
        明細_dgv.DataSource = Nothing : ds.Clear()
        MsgBox("申請完成，請通知主管審核。")
        Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
    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
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            資料數 = 明細_dgv.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 零用金支付明細 SET  用途 = N'" & 明細_dgv.Rows(i).Cells("用途").Value & "', 金額 = N'" & 明細_dgv.Rows(i).Cells("金額").Value &
                   "', 備註 = N'" & 明細_dgv.Rows(i).Cells("備註").Value & "' WHERE (流水號 LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & 明細_dgv.Rows(i).Cells("項次").Value & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    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 = "" : TextBox2.Text = "" : TextBox3.Text = "" : TextBox4.Text = ""
            明細_dgv.DataSource = Nothing : ds.Clear()
            Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
        End If
    End Sub
    Private Sub 帳本名稱_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 帳本名稱_cb.SelectedIndexChanged
        帳本編號_cb.SelectedIndex = 帳本名稱_cb.SelectedIndex : PA19 = 帳本編號_cb.Text : Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : Set_財務單清單()
    End Sub
    Private Sub 帳本編號_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 帳本編號_cb.SelectedIndexChanged
        帳本名稱_cb.SelectedIndex = 帳本編號_cb.SelectedIndex : PA19 = 帳本編號_cb.Text : Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : Set_財務單清單()
    End Sub
End Class