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 DTP As String : Dim DTP1 As String
    Private Sub 考勤打卡_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        Label1.Text = Year(Today) : Label3.Text = Month(Today) : Label5.Text = Strings.Right(Format(Today(), "yyyy/MM/dd"), 2) : Label7.Text = Format(TimeOfDay(), "HH:mm:ss")

        Dim 判斷請假 As String = ""
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 請假類別 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & 員工編號 & "' AND 日期 LIKE '" & Today() & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 判斷請假 = dr("請假類別").ToString : End If : conn.Close()
        If Strings.Right(判斷請假, 2) = "一天" Then : Button1.Enabled = False : Button1.Text = "本日請假中，無法打卡" : End If

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 上午上班, 下午下班 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & 員工編號 & "' AND 日期 LIKE '" & Today() & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : Label10.Text = dr("上午上班").ToString : Label12.Text = dr("下午下班").ToString : End If : conn.Close()

        If Label10.Text = "XXX" Or Label10.Text = "" Then : Label10.Text = "未打卡" : End If : If Label12.Text = "XXX" Or Label12.Text = "" Then : Label12.Text = "未打卡" : End If

        If Val(Strings.Left(Label7.Text, 2)) < 12 Then : Label14.Text = "上午上班" : Else : Label14.Text = "下午下班" : End If

        If 員工編號 = "" Then : Button1.Text = "該帳號持有人無需打卡" : Button1.Enabled = False : End If
    End Sub
    Dim 有無資料判別 As String = ""
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Set_日期格式轉換()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 員工編號 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & 員工編號 & "' AND 日期 LIKE '" & Today() & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 有無資料判別 = dr("員工編號").ToString : End If : conn.Close()

        If 有無資料判別 = "" And Label14.Text = "上午上班" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 人事考勤明細表 (員工編號, 日期, 請假類別, 上午上班, 下午下班) SELECT N'" & 員工編號 & "',N'" & DTP & "',N'','" & Format(TimeOfDay(), "HH:mm:ss") & "',''"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        ElseIf 有無資料判別 = "" And Label14.Text = "下午下班" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 人事考勤明細表 (員工編號, 日期, 請假類別, 上午上班, 下午下班) SELECT N'" & 員工編號 & "',N'" & DTP & "',N'','','" & Format(TimeOfDay(), "HH:mm:ss") & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        ElseIf 有無資料判別 <> "" And Label14.Text = "上午上班" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人事考勤明細表 SET  上午上班 = '" & Format(TimeOfDay(), "HH:mm:ss") & "' WHERE (員工編號 = '" & 員工編號 & "') AND (日期 LIKE '" & Today() & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        ElseIf 有無資料判別 <> "" And Label14.Text = "下午下班" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人事考勤明細表 SET  下午下班 = '" & Format(TimeOfDay(), "HH:mm:ss") & "' WHERE (員工編號 = '" & 員工編號 & "') AND (日期 LIKE '" & Today() & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        End If
        MsgBox("打卡完成")
        Me.Close()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class