Option Strict Off
Imports System.IO
Public Class 零用金支付單一覽表
    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
    Dim DTP As String
    Private Sub Set_財務單清單()
        明細_dgv.DataSource = Nothing : ds.Clear()
        明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        明細_dgv.ColumnHeadersHeight = 25
        明細_dgv.AllowUserToAddRows = False

        PA1 = TextBox1.Text : SQL_財務帳清單() : 列印用SQL2 = SQL1
        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

        TextBox7.Text = "0.00"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            TextBox7.Text = Val(TextBox7.Text) + 明細_dgv.Rows(i).Cells("金額").Value
        Next i
        TextBox7.Text = Format(Val(TextBox7.Text), "#,##0.00")
    End Sub
    Private Sub Set_清單3()
        已審核_dgv.DataSource = Nothing : ds3.Clear()
        已審核_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        已審核_dgv.ColumnHeadersHeight = 25
        已審核_dgv.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 審核日期, 零用金支付表.核准人, 
                           LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 
                           零用金支付明細.出納 AS 已出納
                    FROM 零用金支付表 INNER JOIN
                           零用金支付明細 ON 零用金支付表.流水號 = 零用金支付明細.流水號
                    WHERE (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.審核人 NOT LIKE N'') AND 
                            (零用金支付表.核准人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'') AND (帳本編號 LIKE '" & 帳本編號_cb.Text & "')
                    GROUP BY 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10), 
                            零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10), 零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10), 
                            零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 零用金支付明細.出納
                    ORDER BY 零用金支付表.流水號 DESC"
        Else
            SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期, 
                           零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, 零用金支付表.核准人, 
                           LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 
                           零用金支付明細.出納 AS 已出納
                    FROM 零用金支付表 INNER JOIN
                           零用金支付明細 ON 零用金支付表.流水號 = 零用金支付明細.流水號
                    WHERE (零用金支付表.申請人 LIKE N'" & gUserName & "') AND (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.審核人 NOT LIKE N'') AND 
                          (零用金支付表.核准人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'') AND (帳本編號 LIKE '" & 帳本編號_cb.Text & "')
                    GROUP BY 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10), 
                            零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10), 零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10), 
                            零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 零用金支付明細.出納
                    ORDER BY 零用金支付表.流水號 DESC"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds3) : 已審核_dgv.DataSource = ds3.Tables(0) : conn.Close() : Set_grid3()
    End Sub
    Private Sub Set_grid3()
        已審核_dgv.Columns(0).Width = 120 : 已審核_dgv.Columns(1).Width = 114 : 已審核_dgv.Columns(2).Visible = False : 已審核_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 : 已審核_dgv.Columns(10).Width = 113
        For i As Integer = 0 To 已審核_dgv.Rows.Count - 1
            If 已審核_dgv.Rows(i).Cells("已出納").Value Is DBNull.Value Then
                已審核_dgv.Rows(i).Cells("已出納").Value = False
            End If
        Next
    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
        TextBox6.Visible = False : 帳本編號_cb.Visible = False
        ComboBox1下拉表單資料載入() : 帳本名稱_cb.SelectedIndex = 0 : PA19 = 帳本編號_cb.Text : Set_清單3()
    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
            TextBox4.Text = 已審核_dgv(4, e.RowIndex).Value.ToString : TextBox5.Text = 已審核_dgv(6, e.RowIndex).Value.ToString : TextBox13.Text = 已審核_dgv(9, e.RowIndex).Value.ToString
            TextBox6.Text = 已審核_dgv(8, e.RowIndex).Value.ToString : CheckBox1.Checked = 已審核_dgv(10, e.RowIndex).Value
            DateTimePicker1.Value = 已審核_dgv(3, e.RowIndex).Value.ToString : DateTimePicker2.Value = 已審核_dgv(5, e.RowIndex).Value.ToString
            DateTimePicker3.Value = 已審核_dgv(7, e.RowIndex).Value.ToString
            列印用SQL = 已審核_dgv(0, e.RowIndex).Value.ToString
            簽核圖片編號 = TextBox6.Text

            Set_財務單清單()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & 已審核_dgv(8, e.RowIndex).Value.ToString & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            While dr.Read() = True
                Dim bytes As Byte() = New Byte(-1) {}
                bytes = DirectCast(dr.Item("簽名檔圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                PictureBox1.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            PictureBox1.SizeMode = 4
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If 列印用SQL2 = "" Then
            MsgBox("沒有選擇零用金支付單")
        Else
            列印_零用金支付單.ShowDialog() : 列印用SQL = "" : 列印用SQL2 = ""
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Set_日期格式轉換()
        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 & "' WHERE 流水號 LIKE N'" & 已審核_dgv.Rows(i).Cells("流水號").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 零用金支付表 SET 出納 = N'" & gUserName & "', 出納日期 = '" & DTP & "' WHERE 流水號 LIKE N'" & 已審核_dgv.Rows(i).Cells("流水號").Value & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        MsgBox("存檔完成")
    End Sub
    Private Sub 帳本名稱_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 帳本名稱_cb.SelectedIndexChanged
        帳本編號_cb.SelectedIndex = 帳本名稱_cb.SelectedIndex : PA19 = 帳本編號_cb.Text : Set_清單3()
        TextBox4.Text = "" : TextBox5.Text = "" : TextBox13.Text = "" : TextBox6.Text = ""
        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_清單3()
        TextBox4.Text = "" : TextBox5.Text = "" : TextBox13.Text = "" : TextBox6.Text = ""
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : Set_財務單清單()
    End Sub
End Class