Option Strict Off
Imports System.Data.SqlClient
Public Class 人事請假核准
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim 編號 As String : Dim DTP As String
    Private Sub Set_考勤明細()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 萬年曆.日期, 萬年曆.星期, QQ.請假類別, AA.事由, AA.是否生效, AA.核准人, AA.核准日期, QQ.上午上班, QQ.上午上班 AS 上午下班, QQ.上午上班 AS 上午工時, QQ.下午下班 AS 下午上班, 
                       QQ.下午下班, QQ.下午下班 AS 下午工時, QQ.下午下班 AS 整日工時, BB.上午補登, BB.下午補登, BB.公出事由, QQ.員工編號
                 FROM 萬年曆 LEFT OUTER JOIN
                      (SELECT 員工編號, 日期, 請假類別, 事由, 是否生效, 核准人, 核准日期
                       FROM 人事請假單
                       WHERE (員工編號 LIKE '" & 編號 & "') AND (日期 LIKE '" & ComboBox1.Text & "/" & ComboBox2.Text & "%')) AS AA ON 萬年曆.日期 = AA.日期 
                             LEFT OUTER JOIN
                      (SELECT 員工編號, 日期, 請假類別, 上午上班, 下午下班
                       FROM 人事考勤明細表
                       WHERE (員工編號 LIKE '" & 編號 & "') AND (日期 LIKE '" & ComboBox1.Text & "/" & ComboBox2.Text & "%')) AS QQ ON 萬年曆.日期 = QQ.日期
                                                    LEFT OUTER JOIN
                      (SELECT 員工編號, 日期, 上午補登, 下午補登, 公出事由
                       FROM 人事漏打卡補登紀錄
                       WHERE (員工編號 LIKE '" & 編號 & "') AND (日期 LIKE '" & ComboBox1.Text & "/" & ComboBox2.Text & "%')) AS BB ON 萬年曆.日期 = BB.日期
                                       WHERE (萬年曆.日期 LIKE '" & ComboBox1.Text & "/" & ComboBox2.Text & "%') ORDER BY 萬年曆.日期"
        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_grid()
        DataGridView1.Columns(0).Width = 85 : DataGridView1.Columns(1).Width = 60 : DataGridView1.Columns(2).Width = 90 : DataGridView1.Columns(3).Width = 262
        DataGridView1.Columns(4).Width = 80 : DataGridView1.Columns(5).Width = 65 : DataGridView1.Columns(6).Width = 90 : DataGridView1.Columns(7).Width = 75
        DataGridView1.Columns(8).Width = 75 : DataGridView1.Columns(9).Width = 80 : DataGridView1.Columns(10).Width = 75 : DataGridView1.Columns(11).Width = 75
        DataGridView1.Columns(12).Width = 80 : DataGridView1.Columns(13).Width = 90 : DataGridView1.Columns(14).Width = 70 : DataGridView1.Columns(15).Width = 70
        DataGridView1.Columns(16).Width = 262 : DataGridView1.Columns(17).Visible = False

        Dim T， S， F， M， T1， S1 As Integer : Dim T2， S2， F2， M2 As String : Dim 上午 As Integer = 0 : Dim 下午 As Integer = 0 : Dim 漏打 As Integer = 0
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            資料數 = DataGridView1.Rows.Count : MyModule1.進度條()
            Dim QASW As Integer = 0
            If DataGridView1.Rows(i).Cells("上午上班").Value.ToString <> "" Then
                DataGridView1.Rows(i).Cells("上午下班").Value = "12:00:00"
                Dim TSFM As Integer = DateDiff("s", DataGridView1.Rows(i).Cells("上午上班").Value.ToString, DataGridView1.Rows(i).Cells("上午下班").Value)
                T = TSFM \ 86400 : T1 = TSFM Mod 86400 : S = T1 \ 3600 : S1 = T1 Mod 3600 : F = S1 \ 60 : M = S1 Mod 60
                If T = 0 Then : T2 = "" : Else : T2 = T & "天" : End If : If S = 0 Then : S2 = "" : Else : S2 = S & "時" : End If : If F = 0 Then : F2 = "" : Else : F2 = F & "分" : End If : M2 = M & "秒"
                DataGridView1.Rows(i).Cells("上午工時").Value = T2 & S2 & F2 & M2
                QASW = QASW + TSFM : 上午 = 上午 + TSFM
            End If
            If DataGridView1.Rows(i).Cells("下午下班").Value.ToString <> "" Then
                DataGridView1.Rows(i).Cells("下午上班").Value = "13:30:00"
                Dim TSFM As Integer = DateDiff("s", DataGridView1.Rows(i).Cells("下午上班").Value.ToString, DataGridView1.Rows(i).Cells("下午下班").Value)
                T = TSFM \ 86400 : T1 = TSFM Mod 86400 : S = T1 \ 3600 : S1 = T1 Mod 3600 : F = S1 \ 60 : M = S1 Mod 60
                If T = 0 Then : T2 = "" : Else : T2 = T & "天" : End If : If S = 0 Then : S2 = "" : Else : S2 = S & "時" : End If : If F = 0 Then : F2 = "" : Else : F2 = F & "分" : End If : M2 = M & "秒"
                DataGridView1.Rows(i).Cells("下午工時").Value = T2 & S2 & F2 & M2
                QASW = QASW + TSFM : 下午 = 下午 + TSFM
            End If
            If QASW = 0 Then : Else
                T = QASW \ 86400 : T1 = QASW Mod 86400 : S = T1 \ 3600 : S1 = T1 Mod 3600 : F = S1 \ 60 : M = S1 Mod 60
                If T = 0 Then : T2 = "" : Else : T2 = T & "天" : End If : If S = 0 Then : S2 = "" : Else : S2 = S & "時" : End If : If F = 0 Then : F2 = "" : Else : F2 = F & "分" : End If : M2 = M & "秒"
                DataGridView1.Rows(i).Cells("整日工時").Value = T2 & S2 & F2 & M2
            End If
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Set_人員清單()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 人事資料表.員工編號, 使用者權限管理表.姓名 AS 系統登入姓名, 人事資料表.姓名, 人事資料表.入職日期, 人事資料表.離職日期
                FROM 使用者權限管理表 LEFT OUTER JOIN
                            人事資料表 ON 使用者權限管理表.員工編號 = 人事資料表.員工編號 AND 
                            使用者權限管理表.姓名 = 人事資料表.系統登入姓名
                WHERE (使用者權限管理表.CC49 = 0) AND (人事資料表.員工編號 IS NOT NULL) AND (人事資料表.離職日期 LIKE '在職')
                ORDER BY 使用者權限管理表.職稱"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Visible = False : DataGridView2.Columns(1).Width = 150 : DataGridView2.Columns(2).Visible = False : DataGridView2.Columns(3).Visible = False
        DataGridView2.Columns(4).Visible = False
    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 ComboBox3下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  請假類別 FROM METOO請假類別 ORDER BY 請假類別 DESC"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox3.Items.Clear()
        While (dr.Read()) : ComboBox3.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
        Set_人員清單() : ComboBox1下拉表單資料載入() : ComboBox3下拉表單資料載入()
        ComboBox1.Text = Year(Today) : ComboBox2.SelectedIndex = Month(Today) - 1
        If CC(49) = True Then
            DataGridView2.Enabled = True
        Else
            DataGridView2.Enabled = False
            編號 = 員工編號 : Set_考勤明細()
            TextBox2.Text = 員工編號 : TextBox1.Text = gUserName : TextBox3.Text = 登入人部門
        End If
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox4.Text = DataGridView1(0, e.RowIndex).Value.ToString : ComboBox3.Text = DataGridView1(2, e.RowIndex).Value.ToString : TextBox5.Text = DataGridView1(3, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            編號 = DataGridView2(0, e.RowIndex).Value.ToString : TextBox2.Text = DataGridView2(0, e.RowIndex).Value.ToString
            DateTimePicker1.Value = DataGridView2(3, e.RowIndex).Value.ToString : TextBox1.Text = DataGridView2(1, e.RowIndex).Value.ToString

            Set_考勤明細()

            Dim TSFM As Integer = DateDiff("d", DateTimePicker1.Value, Today()) : TextBox18.Text = TSFM

            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 部門 FROM 使用者權限管理表 WHERE (員工編號 = '" & 編號 & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                TextBox3.Text = dr("部門").ToString
            End If
            conn.Close()
        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
        If TextBox1.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or ComboBox3.Text = "" Then
            MsgBox("請檢查資料是否有填選完畢")
        Else
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人事請假單 SET 核准人 = N'" & gUserName & "', 核准日期 = N'" & DTP & "', 是否生效 = N'已核准' WHERE (員工編號 LIKE '" & 編號 & "') AND (日期 LIKE '" & TextBox4.Text & "%')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()

            Dim 有無資料判別 As String = ""
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 員工編號 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & 編號 & "' AND 日期 LIKE '" & TextBox4.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : 有無資料判別 = dr("員工編號").ToString : End If : conn.Close()

            If 有無資料判別 = "" Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 人事考勤明細表 (員工編號, 日期, 請假類別, 上午上班, 下午下班) 
                        SELECT N'" & 編號 & "',N'" & TextBox4.Text & "',N'" & ComboBox3.Text & "','',''"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 人事考勤明細表 SET 請假類別 = N'" & ComboBox3.Text & "' 
                        WHERE (員工編號 LIKE '" & 編號 & "') AND (日期 LIKE '" & TextBox4.Text & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()
            End If
            Set_考勤明細()
            MsgBox("核准完成")
        End If
    End Sub
End Class