Option Strict Off
Imports System.IO
Public Class 零用金支付核准
    Dim ds As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet : Dim ds4 As New DataSet
    Dim DTP As String : Dim DTP2 As String
    Dim QQW(11) As String : Dim II 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_清單3()
        二級未核准_dgv.DataSource = Nothing : ds3.Clear()
        二級未核准_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        二級未核准_dgv.ColumnHeadersHeight = 25
        二級未核准_dgv.AllowUserToAddRows = False

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

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

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

        二級作廢_dgv.Columns(0).FillWeight = 120 : 二級作廢_dgv.Columns(1).FillWeight = 114 : 二級作廢_dgv.Columns(2).FillWeight = 114 : 二級作廢_dgv.Columns(3).FillWeight = 115
        二級作廢_dgv.Columns(4).FillWeight = 115 : 二級作廢_dgv.Columns(5).FillWeight = 115 : 二級作廢_dgv.Columns(6).FillWeight = 115 : 二級作廢_dgv.Columns(7).FillWeight = 115
        二級作廢_dgv.Columns(8).Visible = False : 二級作廢_dgv.Columns(9).FillWeight = 90
    End Sub
    Private Sub Set_清單()
        簽名檔_dgv.DataSource = Nothing : ds2.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 gUserName = "李協叡" Then
            SQL1 = "SELECT 圖片流水號 AS 圖檔編碼, 簽名檔人名 FROM 簽名檔管理 ORDER BY 圖片流水號"
        Else
            SQL1 = "SELECT 圖片流水號 AS 圖檔編碼, 簽名檔人名 FROM 簽名檔管理 WHERE 簽名檔人名 LIKE N'" & gUserName & "' ORDER BY 圖片流水號"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : 簽名檔_dgv.DataSource = ds2.Tables(0) : conn.Close()
        簽名檔_dgv.Columns(0).Width = 110 : 簽名檔_dgv.Columns(1).Width = 63
    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_清單() : Set_清單3() : Set_清單4()
    End Sub
    Private Sub 轉換()
        If QQW(II) = "1" Then : QQW(II) = "A" : ElseIf QQW(II) = "2" Then : QQW(II) = "B" : ElseIf QQW(II) = "3" Then : QQW(II) = "C" : ElseIf QQW(II) = "4" Then : QQW(II) = "D"
        ElseIf QQW(II) = "5" Then : QQW(II) = "E" : ElseIf QQW(II) = "6" Then : QQW(II) = "F" : ElseIf QQW(II) = "7" Then : QQW(II) = "G" : ElseIf QQW(II) = "8" Then : QQW(II) = "H"
        ElseIf QQW(II) = "9" Then : QQW(II) = "I" : ElseIf QQW(II) = "0" Then : QQW(II) = "J"
        End If
    End Sub
    Public Sub Delay(ByRef Interval As Double)  'Interval单位为毫秒
        Dim time As DateTime = DateTime.Now : Dim Span As Double = Interval * 1234 : While ((DateTime.Now.Ticks - time.Ticks) < Span) : Application.DoEvents() : End While
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 簽名檔_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox14.Text = 簽名檔_dgv(0, e.RowIndex).Value.ToString
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & 簽名檔_dgv(0, 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
            For QAZ = 0 To 999
                QQW(0) = Strings.Right(簽名檔_dgv(0, e.RowIndex).Value.ToString, 2) : QQW(1) = Strings.Right(Year(Today), 2) : QQW(2) = Strings.Left(Year(Today), 2) : QQW(3) = Month(Today) + 9
                QQW(4) = Strings.Right(QQW(0), 1) : QQW(5) = Strings.Left(QQW(0), 1) : QQW(6) = Strings.Right(QQW(1), 1) : QQW(7) = Strings.Left(QQW(1), 1) : QQW(8) = Strings.Right(QQW(2), 1)
                QQW(9) = Strings.Left(QQW(2), 1) : QQW(10) = Strings.Right(QQW(3), 1) : QQW(11) = Strings.Left(QQW(3), 1)
                For RT = 4 To 11
                    Delay(3)
                    Dim NUM1 As Integer = 0 : Dim ran = New Random(DateTime.Now.Millisecond) : NUM1 = ran.Next(1, 4096)
                    For I = 0 To 4096 : If NUM1 > 9 Then : NUM1 = NUM1 - 9 : ElseIf NUM1 < 9 Then : I = 4096 : End If : Next
                    If NUM1 = 1 Then : II = RT : 轉換() : ElseIf NUM1 = 2 Then : II = RT : 轉換() : ElseIf NUM1 = 3 Then : ElseIf NUM1 = 4 Then : II = RT : 轉換() : ElseIf NUM1 = 5 Then
                    ElseIf NUM1 = 6 Then : II = RT : 轉換() : ElseIf NUM1 = 7 Then : ElseIf NUM1 = 8 Then : II = RT : 轉換() : ElseIf NUM1 = 9 Then : II = RT : 轉換() : End If
                Next
                Dim NUM2 As Integer = 0 : Dim ran1 = New Random(DateTime.Now.Millisecond) : NUM2 = ran1.Next(1, 4096)
                For I = 0 To 4096 : If NUM2 > 99 Then : NUM2 = NUM2 - 49 : ElseIf NUM2 < 99 Then : I = 4096 : End If : Next

                TextBox13.Text = QQW(11) & "2" & QQW(9) & "7" & QQW(7) & "0" & QQW(5) & QQW(4) & "1" & QQW(6) & "3" & QQW(8) & "9" & QQW(10) & NUM2

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 簽名編碼 FROM 零用金支付表 WHERE 簽名編碼 LIKE N'" & TextBox13.Text & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() = True Then : Else : QAZ = 999 : End If : conn.Close()
            Next
        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
            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
            Set_財務單清單()
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss") : DTP2 = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Set_日期格式轉換()
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇零用金支付單")
        Else
            If TextBox13.Text = "" Then
                MsgBox("沒有選擇簽名檔")
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 零用金支付表 SET 核准人 = N'" & gUserName & "', 核准日期 = N'" & DTP & "', 核准圖檔 = N'" & TextBox14.Text & "', 簽名編碼 = N'" & TextBox13.Text & "' " &
                       "WHERE (流水號 = N'" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                MsgBox("單據  " & TextBox1.Text & "  核准完成")
                Set_清單3() : Set_清單4()
                TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : TextBox13.Text = "" : TextBox14.Text = ""
                明細_dgv.DataSource = Nothing : ds.Clear()
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.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_清單3() : Set_清單4()
                TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : TextBox13.Text = "" : TextBox14.Text = ""
                明細_dgv.DataSource = Nothing : ds.Clear()
            End If
        End If
    End Sub
End Class