Option Strict Off
Imports System.Data.SqlClient
Public Class 零用金支付_對帳表
    Dim conn As New SqlConnection : Dim conn1 As New SqlConnection
    Dim cmd As New SqlCommand : Dim cmd1 As New SqlCommand
    Dim dr As SqlDataReader : Dim dr1 As SqlDataReader
    Dim da As New SqlDataAdapter : Dim da1 As New SqlDataAdapter
    Dim ds As New DataSet
    Dim KKKJJ As String : Dim KKKJJ2 As String : Dim KKKJJ3 As String : Dim KKK3 As String : Dim KKK4 As String
    Dim DTP As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowTemplate.Height = 35
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_清單1()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期, 零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, " &
               "零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付明細.項次, 零用金支付明細.用途, 零用金支付明細.金額, 零用金支付明細.會計 AS 會計作業, " &
               "零用金支付明細.出納 AS 出納作業, 零用金支付明細.備註, 零用金支付表.會計, 零用金支付表.作帳日期, 零用金支付表.出納, 零用金支付表.出納日期, 零用金支付明細.現金, 零用金支付明細.備用金, " &
               "零用金支付明細.公帳 FROM 零用金支付表 RIGHT OUTER JOIN 零用金支付明細 " &
               "ON 零用金支付表.流水號 = 零用金支付明細.流水號 WHERE (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'') AND (零用金支付表.核准人 NOT LIKE N'作廢%') " &
               " " & KKKJJ3 & KKKJJ & KKKJJ2 &
               " ORDER BY   零用金支付表.流水號 DESC, 零用金支付明細.項次"
        列印用SQL = SQL1
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 120 : DataGridView1.Columns(1).Width = 120 : DataGridView1.Columns(2).Width = 65 : DataGridView1.Columns(3).Width = 80
        DataGridView1.Columns(4).Width = 65 : DataGridView1.Columns(5).Width = 80 : DataGridView1.Columns(6).Width = 65 : DataGridView1.Columns(7).Width = 80 : DataGridView1.Columns(8).Width = 40
        DataGridView1.Columns(9).Width = 300 : DataGridView1.Columns(10).Width = 80 : DataGridView1.Columns(11).Width = 72 : DataGridView1.Columns(12).Width = 73 : DataGridView1.Columns(13).Width = 335
        DataGridView1.Columns(14).Visible = False : DataGridView1.Columns(15).Width = 80 : DataGridView1.Columns(16).Visible = False : DataGridView1.Columns(17).Width = 80
        DataGridView1.Columns(18).Width = 40 : DataGridView1.Columns(19).Width = 50 : DataGridView1.Columns(20).Width = 40
        DataGridView1.Columns(10).DefaultCellStyle.Format = "#,##0.00"
        DataGridView1.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  年份 FROM 年份清單 ORDER BY 年份 DESC"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear()
        While (dr.Read()) : ComboBox1.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
        ComboBox1下拉表單資料載入()
        ComboBox1.Text = Year(Today) : ComboBox2.SelectedIndex = Month(Today) - 1
        KKKJJ3 = "AND (零用金支付表.核准日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
        Label62.Visible = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "UPDATE 零用金支付明細 SET 會計 = N'0' WHERE (會計 IS NULL)"
        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'0' WHERE (出納 IS NULL)"
        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'0' WHERE (現金 IS NULL)"
        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'0' WHERE (備用金 IS NULL)"
        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'0' WHERE (公帳 IS NULL)"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
        conn.Close()
        Set_清單1()
    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 DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            Timer1.Enabled = True
        End If
    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView1.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView1(0, e.RowIndex).Value.ToString <> DataGridView1(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView1.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1(11, i).Value = True And DataGridView1(12, i).Value = True Then
                DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf DataGridView1(11, i).Value = True And DataGridView1(12, i).Value = False Then
                DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            End If
        Next
    End Sub
    Private Sub CheckBox6_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox6.Click
        CheckBox6.Checked = True : CheckBox8.Checked = False : CheckBox7.Checked = False : CheckBox1.Checked = False : CheckBox2.Checked = False : CheckBox3.Checked = False
        KKKJJ = "" : Set_清單1()
    End Sub
    Private Sub CheckBox8_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox8.Click
        CheckBox6.Checked = False : CheckBox8.Checked = True : CheckBox7.Checked = False : CheckBox1.Checked = False : CheckBox2.Checked = False : CheckBox3.Checked = False
        KKKJJ = " AND (零用金支付明細.會計 = 1)" : Set_清單1()
    End Sub
    Private Sub CheckBox7_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox7.Click
        CheckBox6.Checked = False : CheckBox8.Checked = False : CheckBox7.Checked = True : CheckBox1.Checked = False : CheckBox2.Checked = False : CheckBox3.Checked = False
        KKKJJ = " AND (零用金支付明細.會計 = 0 OR 零用金支付明細.會計 IS NULL)" : Set_清單1()
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        CheckBox6.Checked = False : CheckBox8.Checked = False : CheckBox7.Checked = False : CheckBox1.Checked = True : CheckBox2.Checked = False : CheckBox3.Checked = False
        KKKJJ = "" : Set_清單1()
    End Sub
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.Click
        CheckBox6.Checked = False : CheckBox8.Checked = False : CheckBox7.Checked = False : CheckBox1.Checked = False : CheckBox2.Checked = True : CheckBox3.Checked = False
        KKKJJ = " AND (零用金支付明細.出納 = 1)" : Set_清單1()
    End Sub
    Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.Click
        CheckBox6.Checked = False : CheckBox8.Checked = False : CheckBox7.Checked = False : CheckBox1.Checked = False : CheckBox2.Checked = False : CheckBox3.Checked = True
        KKKJJ = " AND (零用金支付明細.出納 = 0 OR 零用金支付明細.出納 IS NULL)" : Set_清單1()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PA = InputBox("請輸入要查詢的關鍵字")
        KKKJJ2 = " AND (零用金支付表.流水號 LIKE N'%" & PA & "%' OR 零用金支付表.部門 LIKE N'%" & PA & "%' OR 零用金支付表.申請人 LIKE N'%" & PA & "%' OR 零用金支付表.審核人 LIKE N'%" & PA &
                 "%' OR 零用金支付明細.用途 LIKE N'%" & PA & "%' OR 零用金支付明細.備註 LIKE N'%" & PA & "%')" : Set_清單1()
        KKKJJ2 = "" : PA = ""
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer1.Enabled = False : Label62.Visible = False
        Set_日期格式轉換()
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            資料數 = DataGridView1.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 會計, 出納 FROM 零用金支付明細 WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "' AND 項次  LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "'"
            cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
            If dr.Read() Then
                If dr("會計") = False And DataGridView1.Rows(i).Cells("會計作業").Value = True Then
                    If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If
                    SQL1 = "UPDATE 零用金支付明細 SET 會計 = N'1' WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "' AND 項次  LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "'"
                    cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : cmd1.ExecuteNonQuery() : conn1.Close()
                    If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If
                    SQL1 = "UPDATE 零用金支付表 SET 會計 = N'" & gUserName & "', 作帳日期 = '" & DTP & "' WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "'"
                    cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : cmd1.ExecuteNonQuery() : conn1.Close()
                End If
                If dr("出納") = False And DataGridView1.Rows(i).Cells("出納作業").Value = True Then
                    If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If
                    SQL1 = "UPDATE 零用金支付明細 SET 出納 = N'1' WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "' AND 項次  LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "'"
                    cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : cmd1.ExecuteNonQuery() : conn1.Close()
                    If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If
                    SQL1 = "UPDATE 零用金支付表 SET 出納 = N'" & gUserName & "', 出納日期 = '" & DTP & "' WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "'"
                    cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : cmd1.ExecuteNonQuery() : conn1.Close()
                End If
            End If : conn.Close()
            If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If
            SQL1 = "UPDATE 零用金支付明細 SET 現金 = '" & DataGridView1.Rows(i).Cells("現金").Value & "', 備用金 = '" & DataGridView1.Rows(i).Cells("備用金").Value &
                "', 公帳 = '" & DataGridView1.Rows(i).Cells("公帳").Value & "' WHERE 流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value &
                "' AND 項次  LIKE N'" & DataGridView1.Rows(i).Cells("項次").Value & "'"
            cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : cmd1.ExecuteNonQuery() : conn1.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        MsgBox("存檔完成") : Set_清單1()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If KKK3 = "" Or KKK4 = "" Then
        Else
            KKKJJ3 = "AND (零用金支付表.核准日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
        End If
        Set_清單1()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        列印_零用金支付明細.ShowDialog()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        KKK3 = ComboBox1.Text
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        KKK4 = ComboBox2.Text
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        ComboBox1下拉表單資料載入()
        ComboBox2.Items.Clear()
        ComboBox2.Items.Add("01") : ComboBox2.Items.Add("02") : ComboBox2.Items.Add("03") : ComboBox2.Items.Add("04") : ComboBox2.Items.Add("05") : ComboBox2.Items.Add("06")
        ComboBox2.Items.Add("07") : ComboBox2.Items.Add("08") : ComboBox2.Items.Add("09") : ComboBox2.Items.Add("10") : ComboBox2.Items.Add("11") : ComboBox2.Items.Add("12")
        KKK3 = "" : KKK4 = "" : KKKJJ3 = ""
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        PA = InputBox("請輸入要查詢的驗證碼。")
        KKKJJ2 = " AND (零用金支付表.簽名編碼 LIKE N'" & PA & "')" : Set_清單1()
        KKKJJ2 = "" : PA = ""
    End Sub
End Class