-
Notifications
You must be signed in to change notification settings - Fork 7
/
functions.php
138 lines (127 loc) · 4.56 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
<?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
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;";
$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;
}