Option Strict Off
Imports System.Data.SqlClient
Public Class 人事考勤打卡
    Private Property MYSQL As String
    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
    Dim 有無資料判別 As String = ""
    Private Sub 人事考勤打卡_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConString = "Data Source=mssql-147629-0.cloudclusters.net,12711;Initial Catalog=GCM-AM-SYS;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
        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")
        If Val(Strings.Left(Label7.Text, 2)) < 12 Then : Label14.Text = "上午上班" : Else : Label14.Text = "下午下班" : End If
        Label16.Text = "" : Label18.Text = "" : Label10.Text = "" : Label12.Text = "" : Button1.Text = "打卡系統運行中"

        Label1.BackColor = Color.Transparent : Label2.BackColor = Color.Transparent : Label3.BackColor = Color.Transparent : Label4.BackColor = Color.Transparent : Label5.BackColor = Color.Transparent
        Label6.BackColor = Color.Transparent : Label7.BackColor = Color.Transparent : Label8.BackColor = Color.Transparent : Label9.BackColor = Color.Transparent : Label10.BackColor = Color.Transparent
        Label11.BackColor = Color.Transparent : Label12.BackColor = Color.Transparent : Label13.BackColor = Color.Transparent : Label14.BackColor = Color.Transparent : Label15.BackColor = Color.Transparent
        Label16.BackColor = Color.Transparent : Label17.BackColor = Color.Transparent : Label18.BackColor = Color.Transparent
        'Label7.Visible = False
        Timer2.Enabled = True
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label16.Text = "" : Label18.Text = "" : Label10.Text = "" : Label12.Text = "" : Button1.Text = "打卡系統運行中"
        Timer1.Enabled = False
    End Sub
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        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")
        If Val(Strings.Left(Label7.Text, 2)) < 12 Then : Label14.Text = "上午上班" : Else : Label14.Text = "下午下班" : End If
    End Sub
    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        If TextBox2.Text <> "" Then
            Label10.Text = "" : Label12.Text = ""
        End If
        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")
        If Val(Strings.Left(Label7.Text, 2)) < 12 Then : Label14.Text = "上午上班" : Else : Label14.Text = "下午下班" : End If

        If IsNumeric(TextBox2.Text) = True Then
            If TextBox2.Text.Length = 10 Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                MYSQL = "SELECT 姓名, 員工編號 FROM 使用者權限管理表 WHERE (ID卡號 = '" & TextBox2.Text & "')"
                cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
                If dr.Read() Then
                    Label16.Text = dr("姓名").ToString
                    Label18.Text = dr("員工編號").ToString
                    conn.Close()
                Else
                    TextBox2.Text = "" : TextBox2.Focus() : conn.Close()
                End If
                Timer1.Enabled = False
                Me.Button1.PerformClick()
            End If
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    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
        Timer1.Enabled = True
        Label7.Text = Format(TimeOfDay(), "HH:mm:ss")
        Dim 判斷請假 As String = "" : Button1.Text = "打卡系統運行中"
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 請假類別 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & Label18.Text & "' AND 日期 LIKE '" & Format(Today(), "yyyy/MM/dd") & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 判斷請假 = dr("請假類別").ToString : End If : conn.Close()

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 上午上班, 下午下班 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & Label18.Text & "' AND 日期 LIKE '" & Format(Today(), "yyyy/MM/dd") & "'"
        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 Strings.Right(判斷請假, 2) = "一天" Then
            Button1.Text = "本日請假中，無法打卡"
        Else
            If Label18.Text = "" Then
                Button1.Text = "該帳號持有人無需打卡"
            Else
                Set_日期格式轉換()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 員工編號 FROM 人事考勤明細表 WHERE 員工編號 LIKE '" & Label18.Text & "' AND 日期 LIKE '" & Format(Today(), "yyyy/MM/dd") & "'"
                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'" & Label18.Text & "',N'" & DTP & "',N'','" & Format(TimeOfDay(), "HH:mm:ss") & "',''"
                    Label10.Text = Format(TimeOfDay(), "HH:mm:ss")
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Button1.Text = "打卡成功"
                ElseIf 有無資料判別 = "" And Label14.Text = "下午下班" Then
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 人事考勤明細表 (員工編號, 日期, 請假類別, 上午上班, 下午下班) SELECT N'" & Label18.Text & "',N'" & DTP & "',N'','','" & Format(TimeOfDay(), "HH:mm:ss") & "'"
                    Label12.Text = Format(TimeOfDay(), "HH:mm:ss")
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Button1.Text = "打卡成功"
                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 (員工編號 = '" & Label18.Text & "') AND (日期 LIKE '" & Format(Today(), "yyyy/MM/dd") & "')"
                    Label10.Text = Format(TimeOfDay(), "HH:mm:ss")
                    cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Button1.Text = "打卡成功"
                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 (員工編號 = '" & Label18.Text & "') AND (日期 LIKE '" & Format(Today(), "yyyy/MM/dd") & "')"
                    Label12.Text = Format(TimeOfDay(), "HH:mm:ss")
                    cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    Button1.Text = "打卡成功"
                Else
                    Button1.Text = "打卡失敗，請再試一下。"
                End If
            End If
        End If
        TextBox2.Text = "" : TextBox2.Focus()
    End Sub
End Class

