Option Strict Off
Public Class 營運成本收支單審核
    ReadOnly ds, ds1, ds2 As New DataSet
    'Dim dr As SqlDataReader
    Private ReadOnly QQW(11) As String
    Dim WWEE As String : Private II As Integer
    Private Sub Set_清單1()
        彙總_dgv.DataSource = Nothing : ds1.Clear()
        彙總_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        彙總_dgv.ColumnHeadersHeight = 25
        彙總_dgv.AllowUserToAddRows = False
        彙總_dgv.RowTemplate.Height = 30

        If 未核准的營運成本收支單_rb.Checked = True And 作廢的營運成本收支單_rb.Checked = False Then
            SQL_營運成本收支單審核_審核人1()
        ElseIf 未核准的營運成本收支單_rb.Checked = False And 作廢的營運成本收支單_rb.Checked = True Then
            SQL_營運成本收支單審核_審核人2()
        End If
        SQL_營運成本收支單審核_查詢收支單審核()
        da.Fill(ds1) : 彙總_dgv.DataSource = ds1.Tables(0) : conn.Close()
        彙總_dgv.Columns(0).FillWeight = 160 : 彙總_dgv.Columns(1).FillWeight = 100 : 彙總_dgv.Columns(2).FillWeight = 100 : 彙總_dgv.Columns(3).Visible = False
        彙總_dgv.Columns(4).Visible = False
    End Sub
    Private Sub Set_費用清單()
        明細_dgv.DataSource = Nothing : ds.Clear()
        明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        明細_dgv.ColumnHeadersHeight = 25
        明細_dgv.AllowUserToAddRows = False
        明細_dgv.RowTemplate.Height = 35
        SQL_營運成本收支單審核_查詢費用清單() : da.SelectCommand = cmd : da.Fill(ds) : 明細_dgv.DataSource = ds.Tables(0) : conn.Close()

        明細_dgv.Columns(0).Visible = False : 明細_dgv.Columns(1).FillWeight = 80 : 明細_dgv.Columns(2).FillWeight = 300 : 明細_dgv.Columns(3).FillWeight = 500
        明細_dgv.Columns(4).FillWeight = 140 : 明細_dgv.Columns(5).FillWeight = 140 : 明細_dgv.Columns(6).FillWeight = 140 : 明細_dgv.Columns(7).FillWeight = 140
        明細_dgv.Columns(8).FillWeight = 140 : 明細_dgv.Columns(9).FillWeight = 140 : 明細_dgv.Columns(10).FillWeight = 140 : 明細_dgv.Columns(11).Visible = False
        明細_dgv.Columns(12).Visible = False

        明細_dgv.Columns(1).ReadOnly = True : 明細_dgv.Columns(2).ReadOnly = True : 明細_dgv.Columns(9).ReadOnly = True : 明細_dgv.Columns(10).ReadOnly = True

        明細_dgv.Columns(4).DefaultCellStyle.Format = "#,##0.00" : 明細_dgv.Columns(5).DefaultCellStyle.Format = "#,##0.00" : 明細_dgv.Columns(6).DefaultCellStyle.Format = "#,##0.00"
        明細_dgv.Columns(7).DefaultCellStyle.Format = "#,##0.00" : 明細_dgv.Columns(8).DefaultCellStyle.Format = "#,##0.00"
        明細_dgv.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : 明細_dgv.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        明細_dgv.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(9).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 明細_dgv.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        明細_dgv.EditMode = DataGridViewEditMode.EditOnEnter

        現金_tb.Text = "0" : 支出_tb.Text = "0" : 現金餘額_tb.Text = "0" : 零用金_tb.Text = "0" : 支票_tb.Text = "0"
        For i As Integer = 0 To 明細_dgv.Rows.Count - 1
            現金_tb.Text = Val(現金_tb.Text) + 明細_dgv.Rows(i).Cells("現金").Value : 支出_tb.Text = Val(支出_tb.Text) + 明細_dgv.Rows(i).Cells("支出").Value
            零用金_tb.Text = Val(零用金_tb.Text) + 明細_dgv.Rows(i).Cells("零用金").Value : 支票_tb.Text = Val(支票_tb.Text) + 明細_dgv.Rows(i).Cells("支票").Value
        Next i
        現金餘額_tb.Text = Val(零用金_tb.Text) + Val(現金_tb.Text) - Val(支出_tb.Text)
        If Val(現金餘額_tb.Text) > 0 Then : 現金餘額_tb.ForeColor = Color.Blue : Else : 現金餘額_tb.ForeColor = Color.Red : End If
        現金_tb.Text = Format(Val(現金_tb.Text), "#,##0.00") : 支出_tb.Text = Format(Val(支出_tb.Text), "#,##0.00") : 現金餘額_tb.Text = Format(Val(現金餘額_tb.Text), "#,##0.00")
        零用金_tb.Text = Format(Val(零用金_tb.Text), "#,##0.00") : 支票_tb.Text = Format(Val(支票_tb.Text), "#,##0.00")
    End Sub
    Private Sub Set_清單()
        簽名清單_dgv.DataSource = Nothing : ds2.Clear()
        簽名清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        簽名清單_dgv.ColumnHeadersHeight = 25
        簽名清單_dgv.AllowUserToAddRows = False
        簽名清單_dgv.RowTemplate.Height = 23
        SQL_營運成本收支單審核_查詢圖片流水號() : da.Fill(ds2) : 簽名清單_dgv.DataSource = ds2.Tables(0) : conn.Close()

        簽名清單_dgv.Columns(0).FillWeight = 173 : 簽名清單_dgv.Columns(1).Visible = False
        If 簽名清單_dgv.Rows.Count = 0 Then : Else
            圖片編碼_tb.Text = 簽名清單_dgv(0, 0).Value.ToString
            SQL_營運成本收支單審核_查詢簽名檔圖片()
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("簽名檔圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                簽名圖_pb.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            簽名圖_pb.SizeMode = 4
            For QAZ = 0 To 999
                QQW(0) = Strings.Right(簽名清單_dgv(0, 0).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 ran = New Random(DateTime.Now.Millisecond) : Dim NUM1 As Integer = ran.Next(1, 4096)
                    For I = 0 To 4096 : If NUM1 > 9 Then : 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 ran1 = New Random(DateTime.Now.Millisecond) : Dim NUM2 As Integer = ran1.Next(1, 4096)
                For I = 0 To 4096 : If NUM2 > 99 Then : NUM2 -= 49 : ElseIf NUM2 < 99 Then : I = 4096 : End If : Next

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

                SQL_營運成本收支單審核_查詢簽名編碼()
                If dr.Read() = True Then : Else : QAZ = 999 : End If : conn.Close()
            Next
        End If
    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 營運成本收支單審核_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        未核准的營運成本收支單_rb.Checked = True
        Set_清單1() : Set_清單()

        If CC(49) = False Then : 批量審核_bt.Enabled = False : End If
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 7.6
        End If
    End Sub
    Private Sub 營運成本收支單審核_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 8
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 彙總_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            營運成本收支單號_tb.Text = 彙總_dgv(0, e.RowIndex).Value.ToString : 審核日期_dtp.Value = 彙總_dgv(1, e.RowIndex).Value.ToString : Set_費用清單()
            申請人_tb.Text = 彙總_dgv(3, e.RowIndex).Value.ToString : 備註_tb.Text = 彙總_dgv(4, e.RowIndex).Value.ToString
        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
            圖片編碼_tb.Text = 簽名清單_dgv(0, e.RowIndex).Value.ToString
            SQL_營運成本收支單審核_查詢簽名檔圖片()
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("簽名檔圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                簽名圖_pb.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            簽名圖_pb.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 ran = New Random(DateTime.Now.Millisecond) : Dim NUM1 As Integer = ran.Next(1, 4096)
                    For I = 0 To 4096 : If NUM1 > 9 Then : 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 ran1 = New Random(DateTime.Now.Millisecond) : Dim NUM2 As Integer = ran1.Next(1, 4096)
                For I = 0 To 4096 : If NUM2 > 99 Then : NUM2 -= 49 : ElseIf NUM2 < 99 Then : I = 4096 : End If : Next

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

                SQL_營運成本收支單審核_查詢簽名編碼()
                If dr.Read() Then : Else : QAZ = 999 : End If : conn.Close()
            Next
        End If
    End Sub
    Private Sub Set_切換格式()
        明細_dgv.DataSource = Nothing : ds.Clear() : 營運成本收支單號_tb.Text = "" : 申請人_tb.Text = "" : 現金_tb.Text = "" : 支出_tb.Text = "" : 現金餘額_tb.Text = ""
        零用金_tb.Text = "" : 支票_tb.Text = "" : Set_清單1()
    End Sub
    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles 未核准的營運成本收支單_rb.Click
        未核准的營運成本收支單_rb.Checked = True : 作廢的營運成本收支單_rb.Checked = False
        明細_dgv.DataSource = Nothing : ds.Clear() : 營運成本收支單號_tb.Text = "" : 申請人_tb.Text = "" : 現金_tb.Text = "" : 支出_tb.Text = "" : 現金餘額_tb.Text = ""
        審核通過_bt.Enabled = True : 單據作廢_bt.Enabled = True : 審核通過ToolStripMenuItem.Enabled = True : 單據作廢ToolStripMenuItem.Enabled = True : Set_切換格式()
    End Sub
    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles 作廢的營運成本收支單_rb.Click
        未核准的營運成本收支單_rb.Checked = False : 作廢的營運成本收支單_rb.Checked = True
        明細_dgv.DataSource = Nothing : ds.Clear() : 營運成本收支單號_tb.Text = "" : 申請人_tb.Text = "" : 現金_tb.Text = "" : 支出_tb.Text = "" : 現金餘額_tb.Text = ""
        審核通過_bt.Enabled = False : 單據作廢_bt.Enabled = False : 審核通過ToolStripMenuItem.Enabled = False : 單據作廢ToolStripMenuItem.Enabled = False : Set_切換格式()
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub 審核通過_bt_Click(sender As Object, e As EventArgs) Handles 審核通過_bt.Click
        If 營運成本收支單號_tb.Text = "" Then
            MsgBox("請選擇需要審核的單據。")
        Else
            If 非當天日期_ch.Checked = False Then
                Set_日期格式轉換()
            Else
                DTP = Format(審核日期_dtp.Value, "yyyy/MM/dd")
            End If
            conn.Close()

            If gUserName = "呂苡瑄" Then : PA = gUserName
            Else : PA = "呂宜嫺" : End If

            SQL_營運成本收支單審核_修改公帳控制表()
            SQL_營運成本收支單審核_修改公帳流水帳() : conn.Close()
            For i As Integer = 0 To 明細_dgv.Rows.Count - 1
                PA1 = 明細_dgv.Rows(i).Cells("項次").Value
                If 明細_dgv.Rows(i).Cells("零用金").Value <> 0 Then
                    SQL_營運成本收支單審核_修改公帳流水帳備用金() : conn.Close()
                ElseIf 明細_dgv.Rows(i).Cells("現金").Value <> 0 Then
                    SQL_營運成本收支單審核_修改公帳流水帳現金() : conn.Close()
                ElseIf 明細_dgv.Rows(i).Cells("支出").Value <> 0 Then
                    SQL_營運成本收支單審核_修改公帳流水帳支出() : conn.Close()
                End If
            Next
            MsgBox("審核完畢。")
            營運成本收支單號_tb.Text = "" : 申請人_tb.Text = ""
            明細_dgv.DataSource = Nothing : ds.Clear()
            Set_清單1() : Set_清單()
        End If
    End Sub
    Private Sub 單據作廢_bt_Click(sender As Object, e As EventArgs) Handles 單據作廢_bt.Click
        If 營運成本收支單號_tb.Text = "" Then
            MsgBox("請選擇需要審核的單據。")
        Else
            PA = InputBox("請輸入作廢原因。")
            If PA = "" Then
                MsgBox("請輸入作廢原因,不可空白。")
            Else
                If 非當天日期_ch.Checked = False Then
                    Set_日期格式轉換()
                Else
                    DTP = Format(審核日期_dtp.Value, "yyyy/MM/dd")
                End If
                conn.Close()
                SQL_營運成本收支單審核_審核人3() : conn.Close()
                MsgBox("審核完畢。")
                PA = ""
                營運成本收支單號_tb.Text = "" : 申請人_tb.Text = ""
                明細_dgv.DataSource = Nothing : ds.Clear()
                Set_清單1()
            End If
        End If
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        彙總_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        明細_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        簽名清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
    Private Sub 批量審核_bt_Click_1(sender As Object, e As EventArgs) Handles 批量審核_bt.Click
        Dim NN1 As Integer = 0 : 資料數 = 彙總_dgv.Rows.Count
        For I As Integer = 0 To 彙總_dgv.Rows.Count - 1
            MyModule1.進度條()
            NN1 += 1
            營運成本收支單號_tb.Text = 彙總_dgv(0, I).Value.ToString : 審核日期_dtp.Value = 彙總_dgv(1, I).Value.ToString : Set_費用清單()
            申請人_tb.Text = 彙總_dgv(3, I).Value.ToString : 備註_tb.Text = 彙總_dgv(4, I).Value.ToString

            If 營運成本收支單號_tb.Text = "" Then
                MsgBox("請選擇需要審核的單據。")
            Else
                If 非當天日期_ch.Checked = False Then
                    Set_日期格式轉換()
                Else
                    DTP = Format(審核日期_dtp.Value, "yyyy/MM/dd")
                End If
                conn.Close()

                If gUserName = "呂苡瑄" Then : PA = gUserName
                Else : PA = "呂宜嫺" : End If

                SQL_營運成本收支單審核_修改公帳控制表()
                SQL_營運成本收支單審核_修改公帳流水帳() : conn.Close()
                For ii As Integer = 0 To 明細_dgv.Rows.Count - 1
                    PA1 = 明細_dgv.Rows(ii).Cells("項次").Value
                    If 明細_dgv.Rows(ii).Cells("零用金").Value <> 0 Then
                        SQL_營運成本收支單審核_修改公帳流水帳備用金() : conn.Close()
                    ElseIf 明細_dgv.Rows(ii).Cells("現金").Value <> 0 Then
                        SQL_營運成本收支單審核_修改公帳流水帳現金() : conn.Close()
                    ElseIf 明細_dgv.Rows(ii).Cells("支出").Value <> 0 Then
                        SQL_營運成本收支單審核_修改公帳流水帳支出() : conn.Close()
                    End If
                Next
                營運成本收支單號_tb.Text = "" : 申請人_tb.Text = ""
                明細_dgv.DataSource = Nothing : ds.Clear()
                Set_清單()
            End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Set_清單1() : MsgBox("審核完畢，共自動審核了 [ " & NN1 & " ] 筆財務收支單。")
    End Sub
    '----------------------滑鼠右鍵--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新讀取ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重新讀取ToolStripMenuItem.Click
        Set_清單1()
    End Sub
    Private Sub 審核通過ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 審核通過ToolStripMenuItem.Click
        Me.審核通過_bt.PerformClick()
    End Sub
    Private Sub 單據作廢ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 單據作廢ToolStripMenuItem.Click
        Me.單據作廢_bt.PerformClick()
    End Sub
End Class