forked from parzibyte/asistencia-php-rfid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
258 lines (232 loc) · 7.84 KB
/
functions.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ Si necesitas ayuda, contáctame en \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Creado por Parzibyte (https://parzibyte.me).
------------------------------------------------------------------------------------------------
Si el código es útil para ti, puedes agradecerme siguiéndome: https://parzibyte.me/blog/sigueme/
Y compartiendo mi blog con tus amigos
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1
------------------------------------------------------------------------------------------------
*/ ?>
<?php
if (!defined("RFID_STATUS_FILE")) {
define("RFID_STATUS_FILE", "rfid_status");
}
if (!defined("RFID_STATUS_READING")) {
define("RFID_STATUS_READING", "r");
}
if (!defined("RFID_STATUS_PAIRING")) {
define("RFID_STATUS_PAIRING", "p");
}
if (!defined("PAIRING_EMPLOYEE_ID_FILE")) {
define("PAIRING_EMPLOYEE_ID_FILE", "pairing_employee_id_file");
}
function getEmployeesWithRfid()
{
$query = "SELECT employee_id, rfid_serial FROM employee_rfid";
$db = getDatabase();
$statement = $db->query($query);
return $statement->fetchAll();
}
function onRfidSerialRead($rfidSerial)
{
if (getReaderStatus() === RFID_STATUS_PAIRING) {
pairEmployeeWithRfid($rfidSerial, getPairingEmployeeId());
setReaderStatus(RFID_STATUS_READING);
} else {
$employee = getEmployeeByRfidSerial($rfidSerial);
if ($employee) {
saveEmployeeAttendance($employee->id);
}
}
}
function deleteEmployeeAttendanceByIdAndDate($employeeId, $date)
{
$query = "DELETE FROM employee_attendance where employee_id = ? and date = ?";
$db = getDatabase();
$statement = $db->prepare($query);
return $statement->execute([$employeeId, $date]);
}
function saveEmployeeAttendance($employeeId)
{
$date = date("Y-m-d");
deleteEmployeeAttendanceByIdAndDate($date, $employeeId);
$status = "presence";
$query = "INSERT INTO employee_attendance(employee_id, date, status) VALUES (?, ?, ?)";
$db = getDatabase();
$statement = $db->prepare($query);
return $statement->execute([$employeeId, $date, $status]);
}
function setReaderForEmployeePairing($employeeId)
{
setReaderStatus(RFID_STATUS_PAIRING);
setPairingEmployeeId($employeeId);
}
function setPairingEmployeeId($employeeId)
{
file_put_contents(PAIRING_EMPLOYEE_ID_FILE, $employeeId);
}
function getPairingEmployeeId()
{
return file_get_contents(PAIRING_EMPLOYEE_ID_FILE);
}
function pairEmployeeWithRfid($rfidSerial, $employeeId)
{
removeRfidFromEmployee($rfidSerial);
$query = "INSERT INTO employee_rfid(employee_id, rfid_serial) VALUES (?, ?)";
$db = getDatabase();
$statement = $db->prepare($query);
return $statement->execute([$employeeId, $rfidSerial]);
}
function removeRfidFromEmployee($rfidSerial)
{
$query = "DELETE FROM employee_rfid WHERE rfid_serial = ?";
$db = getDatabase();
$statement = $db->prepare($query);
return $statement->execute([$rfidSerial]);
}
function getEmployeeByRfidSerial($rfidSerial)
{
$query = "SELECT e.id, e.name FROM employees e INNER JOIN employee_rfid
ON employee_rfid.employee_id = e.id
WHERE employee_rfid.rfid_serial = ?";
$db = getDatabase();
$statement = $db->prepare($query);
$statement->execute([$rfidSerial]);
return $statement->fetchObject();
}
function getEmployeeRfidById($employeeId)
{
$query = "SELECT rfid_serial FROM employee_rfid WHERE employee_id = ?";
$db = getDatabase();
$statement = $db->prepare($query);
$statement->execute([$employeeId]);
return $statement->fetchObject();
}
function getReaderStatus()
{
return file_get_contents(RFID_STATUS_FILE);
}
function setReaderStatus($newStatus)
{
if (!in_array($newStatus, [RFID_STATUS_PAIRING, RFID_STATUS_READING])) {
return;
}
file_put_contents(RFID_STATUS_FILE, $newStatus);
}
function getEmployeesWithAttendanceCount($start, $end)
{
$query = "select employees.name,
sum(case when status = 'presence' then 1 else 0 end) as presence_count,
sum(case when status = 'absence' then 1 else 0 end) as absence_count
from employee_attendance
inner join employees on employees.id = employee_attendance.employee_id
where date >= ? and date <= ?
group by employee_id, name;";
$db = getDatabase();
$statement = $db->prepare($query);
$statement->execute([$start, $end]);
return $statement->fetchAll();
}
function saveAttendanceData($date, $employees)
{
deleteAttendanceDataByDate($date);
$db = getDatabase();
$db->beginTransaction();
$statement = $db->prepare("INSERT INTO employee_attendance(employee_id, date, status) VALUES (?, ?, ?)");
foreach ($employees as $employee) {
$statement->execute([$employee->id, $date, $employee->status]);
}
$db->commit();
return true;
}
function deleteAttendanceDataByDate($date)
{
$db = getDatabase();
$statement = $db->prepare("DELETE FROM employee_attendance WHERE date = ?");
return $statement->execute([$date]);
}
function getAttendanceDataByDate($date)
{
$db = getDatabase();
$statement = $db->prepare("SELECT employee_id, status FROM employee_attendance WHERE date = ?");
$statement->execute([$date]);
return $statement->fetchAll();
}
function deleteEmployee($id)
{
$db = getDatabase();
$statement = $db->prepare("DELETE FROM employees WHERE id = ?");
return $statement->execute([$id]);
}
function updateEmployee($name, $id)
{
$db = getDatabase();
$statement = $db->prepare("UPDATE employees SET name = ? WHERE id = ?");
return $statement->execute([$name, $id]);
}
function getEmployeeById($id)
{
$db = getDatabase();
$statement = $db->prepare("SELECT id, name FROM employees WHERE id = ?");
$statement->execute([$id]);
return $statement->fetchObject();
}
function saveEmployee($name)
{
$db = getDatabase();
$statement = $db->prepare("INSERT INTO employees(name) VALUES (?)");
return $statement->execute([$name]);
}
function getEmployees()
{
$db = getDatabase();
$statement = $db->query("SELECT id, name FROM employees");
return $statement->fetchAll();
}
function getVarFromEnvironmentVariables($key)
{
if (defined("_ENV_CACHE")) {
$vars = _ENV_CACHE;
} else {
$file = "env.php";
if (!file_exists($file)) {
throw new Exception("The environment file ($file) does not exists. Please create it");
}
$vars = parse_ini_file($file);
define("_ENV_CACHE", $vars);
}
if (isset($vars[$key])) {
return $vars[$key];
} else {
throw new Exception("The specified key (" . $key . ") does not exist in the environment file");
}
}
function getDatabase()
{
$password = getVarFromEnvironmentVariables("MYSQL_PASSWORD");
$user = getVarFromEnvironmentVariables("MYSQL_USER");
$dbName = getVarFromEnvironmentVariables("MYSQL_DATABASE_NAME");
$database = new PDO('mysql:host=localhost;dbname=' . $dbName, $user, $password);
$database->query("set names utf8;");
$database->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
return $database;
}