Option Strict Off
Public Class 財務_收支單申請
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim 新項次 As String : Dim 新流水1 As String : Dim 新流水2 As String
    Dim NUM1, NUM2 As Integer : Dim 匯率1 As Double
    Private Sub Set_清單1()
        營運收支單_dgv.DataSource = Nothing : ds1.Clear()
        營運收支單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        營運收支單_dgv.ColumnHeadersHeight = 25
        營運收支單_dgv.AllowUserToAddRows = False
        營運收支單_dgv.RowTemplate.Height = 30

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If 未核准的營運成本收支單_rb.Checked = True And 作廢的營運成本收支單_rb.Checked = False And 已核准的營運成本收支單_rb.Checked = False Then
            SQL_營運成本收支單申請_審核人1()
        ElseIf 未核准的營運成本收支單_rb.Checked = False And 作廢的營運成本收支單_rb.Checked = True And 已核准的營運成本收支單_rb.Checked = False Then
            SQL_營運成本收支單申請_審核人2()
        ElseIf 未核准的營運成本收支單_rb.Checked = False And 作廢的營運成本收支單_rb.Checked = False And 已核准的營運成本收支單_rb.Checked = True Then
            SQL_營運成本收支單申請_審核人3()
        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
    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.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).ReadOnly = True : 明細表_dgv.Columns(5).ReadOnly = True

        明細表_dgv.Columns(4).DefaultCellStyle.Format = "#,##0" : 明細表_dgv.Columns(5).DefaultCellStyle.Format = "#,##0"
        明細表_dgv.Columns(6).DefaultCellStyle.Format = "#,##0"
        明細表_dgv.Columns(7).DefaultCellStyle.Format = "#,##0" : 明細表_dgv.Columns(8).DefaultCellStyle.Format = "#,##0"
        明細表_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) + Val(明細表_dgv.Rows(i).Cells("收入").Value) : 支出_tb.Text = Val(支出_tb.Text) + Val(明細表_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") : 支出_tb.Text = Format(Val(支出_tb.Text), "#,##0") : 現金餘額_tb.Text = Format(Val(現金餘額_tb.Text), "#,##0")
        零用金_tb.Text = Format(Val(零用金_tb.Text), "#,##0") : 支票_tb.Text = Format(Val(支票_tb.Text), "#,##0")
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        SQL_營運成本收支單申請_查詢公帳控制表會計科目() : ComboBox1.Items.Clear() : ComboBox6.Items.Clear()
        While (dr.Read()) : ComboBox1.Items.Add(dr("會計科目")) : ComboBox6.Items.Add(dr("編號")) : End While : conn.Close()
        If ComboBox1.Items.Count <> 0 Then : ComboBox1.SelectedIndex = 0 : End If
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        SQL_營運成本收支單申請_查詢最後一筆公帳控制表會計科目() : ComboBox5.Items.Clear() : ComboBox2.Items.Clear() : ComboBox5.Text = "" : ComboBox2.Text = ""
        While (dr.Read()) : ComboBox5.Items.Add(dr("第一階")) : ComboBox2.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        SQL_營運成本收支單申請_查詢最左邊公帳控制表會計科目() : ComboBox3.Items.Clear() : ComboBox4.Items.Clear() : ComboBox3.Text = "" : ComboBox4.Text = ""
        While (dr.Read()) : ComboBox3.Items.Add(dr("第二階")) : ComboBox4.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub 匯率()
        PA1 = Strings.Left(Format(申請日期_dtp.Value, "yyyyMMdd"), 4)
        PA2 = Strings.Mid(Format(申請日期_dtp.Value, "yyyyMMdd"), 5, 2)
        SQL_營運成本收支單申請_查詢匯率()
        If dr.Read() Then
            人民幣匯率_tb.Text = dr("中心匯率").ToString : 美金匯率_tb.Text = dr("美金匯率").ToString : conn.Close() : dr.Close()
        Else
            conn.Close() : dr.Close() : 匯率管理.Close() : 匯率管理.Show() : 匯率管理.BringToFront()
        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 = "" : 支票_tb.Text = "" : ComboBox1下拉表單資料載入() : Set_清單1()
    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 : 日期_dtp.Visible = False : 複製歷史單據重新開立_bt.Enabled = False : ComboBox6.Visible = False : 視窗1_pl.Visible = False
        營運收支單_dgv.Visible = True : Set_清單1() : ComboBox2下拉表單資料載入() : 幣別_cb.SelectedIndex = 0
        匯率()
        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 ComboBox5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox5.TextChanged
        ComboBox2.SelectedIndex = ComboBox5.SelectedIndex : 關鍵字查詢會計科目_tb.Text = "" : ComboBox3下拉表單資料載入() : ComboBox1下拉表單資料載入()
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.TextChanged
        ComboBox5.SelectedIndex = ComboBox2.SelectedIndex : ComboBox3下拉表單資料載入() : ComboBox1下拉表單資料載入()
    End Sub
    Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.TextChanged
        ComboBox4.SelectedIndex = ComboBox3.SelectedIndex : ComboBox1下拉表單資料載入()
    End Sub
    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.TextChanged
        ComboBox3.SelectedIndex = ComboBox4.SelectedIndex : ComboBox1下拉表單資料載入()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox6.SelectedIndex = ComboBox1.SelectedIndex
    End Sub
    Private Sub ComboBox6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox6.SelectedIndexChanged
        ComboBox1.SelectedIndex = ComboBox6.SelectedIndex
    End Sub
    Private Sub 關鍵字查詢會計科目_tb_TextChanged(sender As Object, e As EventArgs) Handles 關鍵字查詢會計科目_tb.TextChanged
        conn.Close()
        SQL_營運成本收支單申請_營運成本會計科目查詢() : conn.Close()
        If ComboBox1.Items.Count <> 0 Then : ComboBox1.SelectedIndex = 0 : End If
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If 資料尚未存檔_lb.Visible = True Then : 資料尚未存檔_lb.Visible = False : Else : 資料尚未存檔_lb.Visible = True : End If
    End Sub
    Private Sub 單一筆收支單_dgv_CellContentCl(sender As Object, e As DataGridViewCellEventArgs) Handles 明細表_dgv.CellEndEdit
        現金_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") : 支出_tb.Text = Format(Val(支出_tb.Text), "#,##0") : 現金餘額_tb.Text = Format(Val(現金餘額_tb.Text), "#,##0")
        零用金_tb.Text = Format(Val(零用金_tb.Text), "#,##0") : 支票_tb.Text = Format(Val(支票_tb.Text), "#,##0")
        Dim EU As String
        EU = 明細表_dgv("票期", e.RowIndex).Value.ToString
        If EU = "" Then
        Else
            日期_dtp.Text = EU
            明細表_dgv.Rows(e.RowIndex).Cells("票期").Value = Format(日期_dtp.Value, "yyyy/MM/dd")
        End If
    End Sub
    Private Sub 營運收支單_dgv_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
            Dim NUM1 As Integer = 0
            For i As Integer = 0 To 明細表_dgv.Rows.Count - 1
                PA17 = 明細表_dgv.Rows(i).Cells("項次").Value
                NUM1 += 1 : If NUM1 < 10 Then : 新項次 = "00" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 99 Then : 新項次 = NUM1 : End If
                明細表_dgv.Rows(i).Cells("項次").Value = 新項次
            Next
        End If
    End Sub
    Private Sub 明細表_dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 明細表_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            If 明細表_dgv.SelectedCells.Count = 1 Then
                If 明細表_dgv.SelectedCells(0).ColumnIndex = 4 Then
                    窗口表頭_lb.Text = "零用金-編輯指定" : 窗口說明1_lb.Text = "零用金" : NUM2 = 明細表_dgv.SelectedCells(0).RowIndex
                    If 視窗1_pl.Visible = False Then : 視窗1_pl.Visible = True : 視窗1_pl.BringToFront() : Else : 視窗1_pl.Visible = False : End If
                ElseIf 明細表_dgv.SelectedCells(0).ColumnIndex = 5 Then
                    窗口表頭_lb.Text = "支票-編輯指定" : 窗口說明1_lb.Text = "支票" : NUM2 = 明細表_dgv.SelectedCells(0).RowIndex
                    If 視窗1_pl.Visible = False Then : 視窗1_pl.Visible = True : 視窗1_pl.BringToFront() : Else : 視窗1_pl.Visible = False : End If
                End If
            End If
            選擇項次_tb.Text = 明細表_dgv(1, e.RowIndex).Value.ToString
            幣別_cb.Text = 明細表_dgv("幣別", e.RowIndex).Value.ToString
            ComboBox2.Text = Strings.Left(明細表_dgv("類別", e.RowIndex).Value.ToString, 4) : ComboBox4.Text = Strings.Left(明細表_dgv("類別", e.RowIndex).Value.ToString, 7)
            ComboBox6.Text = 明細表_dgv("類別", e.RowIndex).Value.ToString
        End If
    End Sub

    Private Sub 未核准的營運成本收支單_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 未核准的營運成本收支單_rb.Click
        未核准的營運成本收支單_rb.Checked = True : 作廢的營運成本收支單_rb.Checked = False : 已核准的營運成本收支單_rb.Checked = False
        申請_bt.Enabled = True : 複製歷史單據重新開立_bt.Enabled = False
        開立營運成本收支單_bt.Enabled = True : 收支單中新增一筆空白資料_bt.Enabled = True
        刪除選中的一筆資料_bt.Enabled = True : 刪除選中的營運成本收支單_bt.Enabled = True : Set_切換格式()
    End Sub
    Private Sub 作廢的營運成本收支單_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 作廢的營運成本收支單_rb.Click
        未核准的營運成本收支單_rb.Checked = False : 作廢的營運成本收支單_rb.Checked = True : 已核准的營運成本收支單_rb.Checked = False
        申請_bt.Enabled = False : 複製歷史單據重新開立_bt.Enabled = False
        開立營運成本收支單_bt.Enabled = False : 收支單中新增一筆空白資料_bt.Enabled = False
        刪除選中的一筆資料_bt.Enabled = False : 刪除選中的營運成本收支單_bt.Enabled = False : Set_切換格式()
    End Sub
    Private Sub 已核准的營運成本收支單_rb_CheckedChanged(sender As Object, e As EventArgs) Handles 已核准的營運成本收支單_rb.Click
        If CC(28) = False Then
            未核准的營運成本收支單_rb.Checked = False : 作廢的營運成本收支單_rb.Checked = False : 已核准的營運成本收支單_rb.Checked = True
            申請_bt.Enabled = False : 複製歷史單據重新開立_bt.Enabled = True
            開立營運成本收支單_bt.Enabled = False : 收支單中新增一筆空白資料_bt.Enabled = False
            刪除選中的一筆資料_bt.Enabled = False : 刪除選中的營運成本收支單_bt.Enabled = False : Set_切換格式()
        Else
            未核准的營運成本收支單_rb.Checked = False : 作廢的營運成本收支單_rb.Checked = False : 已核准的營運成本收支單_rb.Checked = True
            申請_bt.Enabled = True : 複製歷史單據重新開立_bt.Enabled = True
            開立營運成本收支單_bt.Enabled = True : 收支單中新增一筆空白資料_bt.Enabled = True
            刪除選中的一筆資料_bt.Enabled = True : 刪除選中的營運成本收支單_bt.Enabled = True : Set_切換格式()
        End If
    End Sub
    Private Sub 存檔()
        For i As Integer = 0 To 明細表_dgv.Rows.Count - 1
            資料數 = 明細表_dgv.Rows.Count : MyModule1.進度條()
            PA1 = 明細表_dgv.Rows(i).Cells("用途").Value.ToString : PA2 = 明細表_dgv.Rows(i).Cells("支出").Value.ToString : PA3 = 明細表_dgv.Rows(i).Cells("票期").Value.ToString
            PA4 = 明細表_dgv.Rows(i).Cells("類別").Value.ToString : PA5 = 明細表_dgv.Rows(i).Cells("收入").Value.ToString : PA6 = 明細表_dgv.Rows(i).Cells("零用金").Value.ToString
            PA7 = 明細表_dgv.Rows(i).Cells("支票").Value.ToString : PA8 = 明細表_dgv.Rows(i).Cells("匯率").Value.ToString : PA9 = 明細表_dgv.Rows(i).Cells("項次").Value.ToString
            BL1 = 明細表_dgv.Rows(i).Cells("含稅").Value.ToString : PA10 = 明細表_dgv.Rows(i).Cells("流水號").Value.ToString
            SQL_會計科目管理_修改收支單申請表() : conn.Close()
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Set_流水號()
        conn.Close() : dr.Close()
        Dim NUM1 As Integer
        For NUM1 = 0 To 99
            Dim d As String : Dim f As String : Dim g As Integer = 1
            If Month(Now) < 10 Then : d = "0" & CStr(Month(Now))
            Else : d = CStr(Month(Now))
            End If
            If Now.Day() < 10 Then : f = "0" & CStr(Now.Day())
            Else : f = CStr(Now.Day())
            End If
            If NUM1 < 10 Then : 新流水1 = "B" & Microsoft.VisualBasic.Right(CStr(Year(Now)), 2) & d & f & "0" & NUM1
            Else : 新流水1 = "B" & Microsoft.VisualBasic.Right(CStr(Year(Now)), 2) & d & f & NUM1
            End If : PA19 = 新流水1
            SQL_營運成本收支單申請_查詢收支單公帳流水號()
            If dr.Read() Then : conn.Close() : Else : conn.Close() : NUM1 = 99 : End If
        Next
    End Sub
    Private Sub 複製歷史單據重新開立_bt_Click(sender As Object, e As EventArgs) Handles 複製歷史單據重新開立_bt.Click
        開立零用金支付單()
        開立營運成本收支單_bt.Enabled = False : 未核准的營運成本收支單_rb.Enabled = False : 作廢的營運成本收支單_rb.Enabled = False : 已核准的營運成本收支單_rb.Enabled = False
        營運收支單_dgv.Enabled = False : 未核准的營運成本收支單_rb.Checked = True : 作廢的營運成本收支單_rb.Checked = False : 已核准的營運成本收支單_rb.Checked = False : 申請_bt.Enabled = True
        複製歷史單據重新開立_bt.Enabled = False : 開立營運成本收支單_bt.Enabled = True : 收支單中新增一筆空白資料_bt.Enabled = True
        刪除選中的一筆資料_bt.Enabled = True : 刪除選中的營運成本收支單_bt.Enabled = True
        Set_清單1()
        For i As Integer = 0 To 明細表_dgv.Rows.Count - 1
            Set_流水號()
            SQL_會計科目管理_開立零用金支付單()
        Next
        Set_費用清單() : MsgBox("複製單據開立完成。")
    End Sub
    Private Sub 申請日期_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 申請日期_dtp.ValueChanged
        匯率()
    End Sub
    Private Sub 存檔流程()
        Timer1.Enabled = False : 資料尚未存檔_lb.Visible = False
        開立營運成本收支單_bt.Enabled = True : 未核准的營運成本收支單_rb.Enabled = True : 作廢的營運成本收支單_rb.Enabled = True
        營運收支單_dgv.Enabled = True : 已核准的營運成本收支單_rb.Enabled = True
        存檔() : Set_清單1()
        MsgBox("申請完成,請通知主管審核。")
        明細表_dgv.DataSource = Nothing : ds.Clear()
    End Sub
    Private Sub 申請_bt_Click(sender As Object, e As EventArgs) Handles 申請_bt.Click
        If 未核准的營運成本收支單_rb.Checked = True Then
            存檔流程()
        ElseIf 已核准的營運成本收支單_rb.Checked = True Then
            If CC(56) = True Then : 存檔流程() : Else : MsgBox("已核准的資料不可以再修改。") : End If
        ElseIf 作廢的營運成本收支單_rb.Checked = True Then
            MsgBox("已作廢的資料不可以再修改。")
        End If
    End Sub
    Private Sub 收支單中新增一筆空白資料_bt_Click(sender As Object, e As EventArgs) Handles 收支單中新增一筆空白資料_bt.Click
        If ComboBox6.Text = "" Or 幣別_cb.Text = "" Then
            MsgBox("沒有選擇會計科目或幣別。")
        Else
            If 營運成本收支單號_tb.Text = "" Then
                開立零用金支付單()
            End If
            Set_日期格式轉換() : Set_流水號()
            存檔() : PA = 營運成本收支單號_tb.Text
            SQL_營運成本收支單申請_查詢最後一筆公帳支付單號() : PA = ""
            If dr.Read() Then : 新項次 = dr("項次") : Else : 新項次 = "00" : End If : conn.Close()
            NUM1 = Double.Parse(新項次) + 1
            If NUM1 < 10 Then : 新項次 = "00" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 99 Then : 新項次 = NUM1 : End If
            If 幣別_cb.Text = "台幣現金" Then
                匯率1 = 1
            ElseIf 幣別_cb.Text = "人民幣現金" Then
                匯率1 = 人民幣匯率_tb.Text
            ElseIf 幣別_cb.Text = "美金" Then
                匯率1 = 美金匯率_tb.Text
            End If
            PA16 = 匯率1
            PA15 = 新項次
            SQL_營運成本收支單申請_新增公帳流水帳資料() : conn.Close()
            Set_費用清單()
        End If
    End Sub
    Private Sub 刪除選中的一筆資料_bt_Click(sender As Object, e As EventArgs) Handles 刪除選中的一筆資料_bt.Click
        If 選擇項次_tb.Text = "" Then
            MsgBox("未選擇需要刪除的資料。")
        Else
            存檔()
            SQL_營運成本收支單申請_刪除公帳流水帳支付單號() : conn.Close()
            Set_費用清單()
            Dim NUM1 As Integer = 0
            For i As Integer = 0 To 明細表_dgv.Rows.Count - 1
                PA17 = 明細表_dgv.Rows(i).Cells("項次").Value
                NUM1 += 1 : If NUM1 < 10 Then : 新項次 = "00" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 99 Then : 新項次 = NUM1 : End If
                PA18 = 新項次
                SQL_營運成本收支單申請_修改公帳流水帳支付單號() : conn.Close()
            Next
            Set_費用清單()
        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)
    End Sub
    Private Sub Set_日期格式轉換()
        If 補單_cb.Checked = False Then
            DTP4 = Format(Today(), "yyyy/MM/dd") : DTP1 = Format(Today(), "yyyyMMdd")
        Else
            DTP1 = Format(申請日期_dtp.Value, "yyyyMMdd") : DTP4 = Format(申請日期_dtp.Value, "yyyy/MM/dd")
        End If
    End Sub
    Private Sub 開立零用金支付單()
        Dim NUM1 As Integer : Timer1.Enabled = True
        Set_日期格式轉換()
        For i As Integer = 1 To 999
            NUM1 = 0 + i
            If NUM1 < 10 Then : 營運成本收支單號_tb.Text = "OE-" & DTP1 & "-00" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : 營運成本收支單號_tb.Text = "OE-" & DTP1 & "-0" & NUM1
            ElseIf NUM1 > 99 Then : 營運成本收支單號_tb.Text = "OE-" & DTP1 & "-" & NUM1
            End If
            conn.Close()
            PA = 營運成本收支單號_tb.Text
            SQL_營運成本收支單申請_查詢第一筆公帳支付單號()
            If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
        Next
        PA = 營運成本收支單號_tb.Text
        SQL_營運成本收支單申請_新增一筆公帳控制表() : conn.Close()
        申請人_tb.Text = gUserName
        Set_清單1() : PA = ""
    End Sub
    Private Sub 開立營運成本收支單_bt_Click(sender As Object, e As EventArgs) Handles 開立營運成本收支單_bt.Click
        開立零用金支付單()
        開立營運成本收支單_bt.Enabled = False : 未核准的營運成本收支單_rb.Enabled = False
        作廢的營運成本收支單_rb.Enabled = False : 已核准的營運成本收支單_rb.Enabled = False
        營運收支單_dgv.Enabled = False
        明細表_dgv.DataSource = Nothing : ds.Clear() : conn.Close()
        現金_tb.Text = "" : 現金餘額_tb.Text = ""
        Set_清單1()
    End Sub
    Private Sub 刪除選中的營運成本收支單_bt_Click(sender As Object, e As EventArgs) Handles 刪除選中的營運成本收支單_bt.Click
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            Dim SSA As String
            SSA = InputBox("確定刪除資料請輸入 [YES] !")
            If SSA = "YES" Then
                SQL_營運成本收支單申請_刪除公帳控制表() : conn.Close()
                SQL_營運成本收支單申請_刪除公帳流水帳() : conn.Close()
                營運成本收支單號_tb.Text = "" : 申請人_tb.Text = "" : 選擇項次_tb.Text = ""
                明細表_dgv.DataSource = Nothing : ds.Clear()
                Set_清單1()
            End If
        End If
    End Sub
    '----------------------滑鼠右鍵--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新讀取ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重新讀取ToolStripMenuItem.Click
        Set_清單1()
    End Sub
    '----------------------自創視窗滑鼠拖曳功能---------------------------------------------------------------------------------------------------------
    Dim OldX, OldY As Long : Dim drag As Boolean
    Private Sub 收入_cb_CheckedChanged(sender As Object, e As EventArgs) Handles 收入_cb.Click
        If 收入_cb.Checked = True Then
            收入_cb.Checked = True : 支出_cb.Checked = False
        Else
            收入_cb.Checked = False : 支出_cb.Checked = True
        End If
    End Sub
    Private Sub 支出_cb_CheckedChanged(sender As Object, e As EventArgs) Handles 支出_cb.Click
        If 支出_cb.Checked = True Then
            收入_cb.Checked = False : 支出_cb.Checked = True
        Else
            收入_cb.Checked = True : 支出_cb.Checked = False
        End If
    End Sub
    Private Sub 確定_bt_Click(sender As Object, e As EventArgs) Handles 確定_bt.Click
        If 收入_cb.Checked = False And 支出_cb.Checked = False Then
            MsgBox("請先選擇這筆資料是收入還是支出!!!")
        Else
            If 收入_cb.Checked = True Then
                明細表_dgv.Rows(NUM2).Cells("收入").Value = 指定_ndp.Value
                If 窗口說明1_lb.Text = "支票" Then
                    明細表_dgv.Rows(NUM2).Cells("支票").Value = 指定_ndp.Value
                ElseIf 窗口說明1_lb.Text = "零用金" Then
                    明細表_dgv.Rows(NUM2).Cells("零用金").Value = 指定_ndp.Value
                End If
            Else
                明細表_dgv.Rows(NUM2).Cells("支出").Value = 指定_ndp.Value
                If 窗口說明1_lb.Text = "支票" Then
                    明細表_dgv.Rows(NUM2).Cells("支票").Value = 指定_ndp.Value
                ElseIf 窗口說明1_lb.Text = "零用金" Then
                    明細表_dgv.Rows(NUM2).Cells("零用金").Value = 指定_ndp.Value
                End If
            End If : 指定_ndp.Value = 0 : 收入_cb.Checked = False : 支出_cb.Checked = False : Cancel_bt.PerformClick()
        End If
    End Sub
    Private Sub Cancel_bt_Click(sender As Object, e As EventArgs) Handles Cancel_bt.Click
        If 視窗1_pl.Visible = False Then : 視窗1_pl.Visible = True : 視窗1_pl.BringToFront() : Else : 視窗1_pl.Visible = False : End If
    End Sub
    Private Sub 物料群組1_Panel_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗1_pl.MouseDown
        If e.Button = MouseButtons.Left Then : OldX = e.X : OldY = e.Y : drag = True : End If
    End Sub
    Private Sub 物料群組1_Panel_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗1_pl.MouseMove
        If drag Then : 視窗1_pl.Left = 視窗1_pl.Left + e.X - OldX : 視窗1_pl.Top = 視窗1_pl.Top + e.Y - OldY : End If
    End Sub
    Private Sub 物料群組1_Panel_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗1_pl.MouseUp
        drag = False
    End Sub
End Class