-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendance.php
116 lines (98 loc) · 3.4 KB
/
attendance.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
if(isset($_POST['employee'])){
$output = array('error'=>false);
include 'conn.php';
include 'timezone.php';
$employee = $_POST['employee'];
$status = $_POST['status'];
$sql = "SELECT * FROM employees WHERE employee_id = '$employee'";
$query = $conn->query($sql);
if($query->num_rows > 0){
$row = $query->fetch_assoc();
$id = $row['id'];
$date_now = date('Y-m-d');
if($status == 'in'){
$sql = "SELECT * FROM attendance WHERE employee_id = '$id' AND date = '$date_now' AND time_in IS NOT NULL";
$query = $conn->query($sql);
if($query->num_rows > 0){
$output['error'] = true;
$output['message'] = 'You have timed in for today';
}
else{
//updates
$sched = $row['schedule_id'];
$lognow = date('H:i:s');
$sql = "SELECT * FROM schedules WHERE id = '$sched'";
$squery = $conn->query($sql);
$srow = $squery->fetch_assoc();
$logstatus = ($lognow > $srow['time_in']) ? 0 : 1;
//
$sql = "INSERT INTO attendance (employee_id, date, time_in, status) VALUES ('$id', '$date_now', NOW(), '$logstatus')";
if($conn->query($sql)){
$output['message'] = 'Time in: '.$row['firstname'].' '.$row['lastname'];
}
else{
$output['error'] = true;
$output['message'] = $conn->error;
}
}
}
else{
$sql = "SELECT *, attendance.id AS uid FROM attendance LEFT JOIN employees ON employees.id=attendance.employee_id WHERE attendance.employee_id = '$id' AND date = '$date_now'";
$query = $conn->query($sql);
if($query->num_rows < 1){
$output['error'] = true;
$output['message'] = 'Cannot Timeout. No time in.';
}
else{
$row = $query->fetch_assoc();
if($row['time_out'] != '00:00:00'){
$output['error'] = true;
$output['message'] = 'You have timed out for today';
}
else{
$sql = "UPDATE attendance SET time_out = NOW() WHERE id = '".$row['uid']."'";
if($conn->query($sql)){
$output['message'] = 'Time out: '.$row['firstname'].' '.$row['lastname'];
$sql = "SELECT * FROM attendance WHERE id = '".$row['uid']."'";
$query = $conn->query($sql);
$urow = $query->fetch_assoc();
$time_in = $urow['time_in'];
$time_out = $urow['time_out'];
$sql = "SELECT * FROM employees LEFT JOIN schedules ON schedules.id=employees.schedule_id WHERE employees.id = '$id'";
$query = $conn->query($sql);
$srow = $query->fetch_assoc();
if($srow['time_in'] > $urow['time_in']){
$time_in = $srow['time_in'];
}
if($srow['time_out'] < $urow['time_in']){
$time_out = $srow['time_out'];
}
$time_in = new DateTime($time_in);
$time_out = new DateTime($time_out);
$interval = $time_in->diff($time_out);
$hrs = $interval->format('%h');
$mins = $interval->format('%i');
$mins = $mins/60;
$int = $hrs + $mins;
if($int > 4){
$int = $int - 1;
}
$sql = "UPDATE attendance SET num_hr = '$int' WHERE id = '".$row['uid']."'";
$conn->query($sql);
}
else{
$output['error'] = true;
$output['message'] = $conn->error;
}
}
}
}
}
else{
$output['error'] = true;
$output['message'] = 'Employee ID not found';
}
}
echo json_encode($output);
?>