-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
529 lines (445 loc) · 17.6 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
session_start();
// Set default language if not set
$lang = $_SESSION['lang'] ?? 'de';
$langFile = __DIR__ . "/languages/$lang.php";
// Load language file first
if (file_exists($langFile)) {
require_once $langFile;
} else {
require_once __DIR__ . '/languages/de.php'; // Fallback to German
}
// Now include other dependencies
require_once 'config.php';
// Ensure the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
// Establish a PDO connection
try {
$conn = new PDO("sqlite:$database");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// Handle connection error
die("Verbindungsfehler: " . $e->getMessage());
}
// Fetch user's regular working hours and previous overtime
try {
$stmt = $conn->prepare('SELECT regelarbeitszeit, ueberstunden FROM users WHERE id = :user_id');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$userData = $stmt->fetch(PDO::FETCH_ASSOC);
$userRegularWorkingHours = $userData['regelarbeitszeit'] ?? 8; // Default to 8 hours if not set
$previousOvertime = $userData['ueberstunden'] ?? 0.0; // Default to 0 if not set
} catch (PDOException $e) {
// Handle query error
die("Datenbankfehler: " . $e->getMessage());
}
// Fetch all entries from 'zeiterfassung' for the current user, adding week number
try {
$stmt = $conn->prepare('
SELECT *, strftime("%W", startzeit) as weekNumber
FROM zeiterfassung
WHERE user_id = :user_id
ORDER BY startzeit DESC
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Handle query error
die("Datenbankfehler: " . $e->getMessage());
}
$months = [
"01" => MONTH_JANUARY,
"02" => MONTH_FEBRUARY,
"03" => MONTH_MARCH,
"04" => MONTH_APRIL,
"05" => MONTH_MAY,
"06" => MONTH_JUNE,
"07" => MONTH_JULY,
"08" => MONTH_AUGUST,
"09" => MONTH_SEPTEMBER,
"10" => MONTH_OCTOBER,
"11" => MONTH_NOVEMBER,
"12" => MONTH_DECEMBER
];
// Getting current date details
$currentWeekNumber = date("W");
$currentYear = date("Y");
$currentMonth = date("m");
$currentMonthName = $months[$currentMonth];
// Calculate total worked minutes this week excluding breaks
try {
$stmt = $conn->prepare('
SELECT SUM(
((strftime("%s", endzeit) - strftime("%s", startzeit)) / 60) - COALESCE(pause, 0)
) as totalMinutes
FROM zeiterfassung
WHERE user_id = :user_id
AND strftime("%Y", startzeit) = :currentYear
AND strftime("%W", startzeit) = :weekNumber
AND beschreibung != "Feiertag"
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':currentYear', $currentYear, PDO::PARAM_STR);
$stmt->bindParam(':weekNumber', $currentWeekNumber, PDO::PARAM_STR);
$stmt->execute();
$totalMinutesThisWeek = $stmt->fetchColumn();
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
// Ensuring the result is a number
if ($totalMinutesThisWeek === false) {
$totalMinutesThisWeek = 0;
}
$totalMinutesThisWeek = $totalMinutesThisWeek ?? 0;
$totalHours = intdiv($totalMinutesThisWeek, 60);
$remainingMinutes = $totalMinutesThisWeek % 60;
// Converting total minutes into hours with one decimal place
$totalHoursThisWeek = $totalHours + ($remainingMinutes / 60);
// First day of the current month
$currentMonthStart = "{$currentYear}-{$currentMonth}-01";
$currentMonthEnd = date("Y-m-t"); // Last day of the current month
$workingHoursThisMonth = getWorkingHours($currentMonthStart, $currentMonthEnd, $userRegularWorkingHours);
// Getting today's date
$currentDate = date("Y-m-d");
// Adjusting SQL query to fetch data for the current calendar week
$stmt = $conn->prepare('
SELECT
strftime("%Y-%m-%d", startzeit) AS tag,
SUM((strftime("%s", endzeit) - strftime("%s", startzeit)) / 60.0) AS arbeitsstunden
FROM
zeiterfassung
WHERE
user_id = :user_id
AND strftime("%W", startzeit) = :weekNumber
AND strftime("%Y", startzeit) = :currentYear
GROUP BY
strftime("%Y-%m-%d", startzeit)
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':weekNumber', $currentWeekNumber, PDO::PARAM_STR);
$stmt->bindParam(':currentYear', $currentYear, PDO::PARAM_STR);
$stmt->execute();
$workHoursPerDay = $stmt->fetchAll(PDO::FETCH_ASSOC);
$days = [];
$hours = [];
// Looping through the work hours per day
foreach ($workHoursPerDay as $record) {
$date = date("d.m.Y", strtotime($record['tag']));
$roundedHours = round($record['arbeitsstunden'] / 60, 2);
$days[] = $date;
$hours[] = $roundedHours;
}
// Function to check if a date is a holiday
function istFeiertag($datum)
{
global $conn;
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM Feiertage WHERE Datum = ?");
$stmt->execute([$datum]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['count'] > 0;
}
// Function to get working days between two dates
function getWorkingDays($startDate, $endDate)
{
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$end->modify('+1 day'); // Adding one day to the end date
$interval = DateInterval::createFromDateString('1 day');
$dateRange = new DatePeriod($begin, $interval, $end);
$workingDays = 0;
foreach ($dateRange as $date) {
if ($date->format('N') < 6 && !istFeiertag($date->format('Y-m-d'))) {
$workingDays++;
}
}
return $workingDays;
}
// Function to get working hours between two dates
function getWorkingHours($startDate, $endDate, $hoursPerDay)
{
$workingDays = getWorkingDays($startDate, $endDate);
return $workingDays * $hoursPerDay;
}
// Function to fetch holidays from the database for a given year
function fetchFeiertageDB($jahr)
{
global $conn;
$stmt = $conn->prepare("SELECT EXISTS(SELECT 1 FROM Feiertage WHERE strftime('%Y', Datum) = ?)");
$stmt->execute([$jahr]);
$exists = $stmt->fetchColumn();
if ($exists) {
return;
}
$url = "https://feiertage-api.de/api/?jahr=" . urlencode($jahr) . "&nur_land=BW";
$json = file_get_contents($url);
if ($json === false) {
throw new Exception("Error fetching holiday data.");
}
$data = json_decode($json, true);
if (!is_array($data)) {
error_log("Unexpected format of holiday data for year $jahr");
return;
}
$feiertage = array_filter($data, function ($feiertag) {
return empty($feiertag['hinweis']);
});
$feiertage = array_map(function ($feiertag, $name) {
return [
'datum' => $feiertag['datum'] ?? null,
'name' => $name
];
}, $feiertage, array_keys($feiertage));
$feiertage = array_filter($feiertage, function ($feiertag) {
return $feiertag['datum'] !== null;
});
if (count($feiertage) > 0) {
$placeholders = implode(',', array_fill(0, count($feiertage), '(?, ?)'));
$stmt = $conn->prepare("INSERT INTO Feiertage (Datum, Name) VALUES $placeholders");
$flatParams = [];
foreach ($feiertage as $feiertag) {
$flatParams[] = $feiertag['datum'];
$flatParams[] = $feiertag['name'];
}
$stmt->execute($flatParams);
}
}
// Calling the function at the beginning of the year
fetchFeiertageDB($currentYear);
// Function to fetch holidays for this week
function fetchFeiertageDieseWoche($currentYear, $currentWeekNumber)
{
global $conn;
$stmt = $conn->prepare("
SELECT DATE(datum) AS datum, name
FROM Feiertage
WHERE strftime('%Y', datum) = :jahr
AND strftime('%W', datum) = :weekNumber
");
$stmt->bindParam(':jahr', $currentYear);
$stmt->bindParam(':weekNumber', $currentWeekNumber);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Fetching holidays of this week
$feiertageDieseWoche = fetchFeiertageDieseWoche($currentYear, $currentWeekNumber);
// First day of the current month
$firstDayOfTheMonth = "{$currentYear}-{$currentMonth}-01";
// Last day of the current month
$lastDayOfTheMonth = date("Y-m-t", strtotime($currentMonthStart));
$workingDaysThisMonth = getWorkingDays($firstDayOfTheMonth, $lastDayOfTheMonth);
$totalOverHours = 0; // Gesamtüberstunden
$workHoursByDate = []; // Speichert Arbeitsstunden pro Datum
foreach ($records as $record) {
$datum = (new DateTime($record['startzeit']))->format('Y-m-d');
$startzeit = new DateTime($record['startzeit']);
$endzeit = new DateTime($record['endzeit']);
$gesamtdauer = $endzeit->diff($startzeit);
$gesamtstunden = $gesamtdauer->h + ($gesamtdauer->i / 60);
$pause = is_numeric($record['pause']) ? ($record['pause'] / 60) : 0;
$arbeitstunden = $gesamtstunden - $pause;
// Speichern der Arbeitsstunden pro Datum
if (!isset($workHoursByDate[$datum])) {
$workHoursByDate[$datum] = 0;
}
$workHoursByDate[$datum] += $arbeitstunden;
}
// Initialisierung der Gesamtüberstunden
$totalOverHours = 0;
// Durchlaufen der gesammelten Arbeitsstunden pro Tag
foreach ($workHoursByDate as $datum => $hours) {
$wochentag = date('N', strtotime($datum));
if ($wochentag >= 1 && $wochentag <= 5) { // Montag bis Freitag
$totalOverHours += $hours - $userRegularWorkingHours;
} else {
$totalOverHours += $hours; // Wochenendarbeit als Überstunden
}
}
// Hinzufügen der vorherigen Überstunden
$totalOverHours += $previousOvertime;
// Berechnung der Gesamtüberstunden in Stunden und Minuten
if ($totalOverHours < 0) {
// Negative Gesamtüberstunden, verwenden Absolutwerte für Stunden und Minuten
$totalOverHoursHours = floor(abs($totalOverHours)); // Absolutwert für Stunden, abgerundet auf die nächste niedrigere ganze Zahl
$totalOverHoursMinutes = round((abs($totalOverHours) - $totalOverHoursHours) * 60); // Differenz zu ganzen Stunden, umgewandelt in Minuten
$totalOverHoursFormatted = sprintf("-%02d:%02d", $totalOverHoursHours, $totalOverHoursMinutes);
} else {
// Positive Gesamtüberstunden
$totalOverHoursHours = floor($totalOverHours); // Ganze Stunden
$totalOverHoursMinutes = round(($totalOverHours - $totalOverHoursHours) * 60); // Umwandlung der restlichen Dezimalstunden in Minuten
$totalOverHoursFormatted = sprintf("%02d:%02d", $totalOverHoursHours, $totalOverHoursMinutes);
}
// SQL query for total working hours this month
$stmt = $conn->prepare('
SELECT
SUM((strftime("%s", endzeit) - strftime("%s", startzeit)) / 60) as totalMinutes
FROM
zeiterfassung
WHERE
user_id = :user_id
AND strftime("%Y", startzeit) = :currentYear
AND strftime("%m", startzeit) = :currentMonth
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':currentYear', $currentYear, PDO::PARAM_STR);
$stmt->bindParam(':currentMonth', $currentMonth, PDO::PARAM_STR);
$stmt->execute();
$totalMinutesThisMonthFromRecords = $stmt->fetchColumn();
$totalHoursThisMonthFromRecords = floor($totalMinutesThisMonthFromRecords / 60);
// Over hours:
$overHoursThisMonth = $totalHoursThisMonthFromRecords - $workingHoursThisMonth;
// Total working hours of the year
$stmt = $conn->prepare('
SELECT
SUM((strftime("%s", endzeit) - strftime("%s", startzeit)) / 60) as totalMinutes
FROM
zeiterfassung
WHERE
user_id = :user_id
AND strftime("%Y", startzeit) = :currentYear
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':currentYear', $currentYear, PDO::PARAM_STR);
$stmt->execute();
$totalMinutesThisYearFromRecords = $stmt->fetchColumn();
$totalHoursThisYearFromRecords = floor($totalMinutesThisYearFromRecords / 60);
// 2. Expected working hours of the year
$firstDayOfTheYear = "{$currentYear}-01-01";
$lastDayOfTheYear = "{$currentYear}-12-31";
$workingHoursThisYear = getWorkingHours($firstDayOfTheYear, $lastDayOfTheYear, $userRegularWorkingHours);
// Calculating over hours for the year
$overHoursThisYear = $totalHoursThisYearFromRecords - $workingHoursThisYear;
// Determining the first and last working day of the current week (Monday to Friday)
$firstDayOfWeek = date('Y-m-d', strtotime('Monday this week'));
$lastWorkingDayOfWeek = date('Y-m-d', strtotime('Friday this week'));
$expectedHoursThisWeek = 5 * $userRegularWorkingHours; // 5 Arbeitstage pro Woche
// Checking each day of the working week if it's a holiday
$currentDate = $firstDayOfWeek;
while (strtotime($currentDate) <= strtotime($lastWorkingDayOfWeek)) {
if (istFeiertag($currentDate)) {
$expectedHoursThisWeek -= $userRegularWorkingHours;
}
$currentDate = date('Y-m-d', strtotime("+1 day", strtotime($currentDate)));
}
// Calculating over hours for this week
$overHoursThisWeek = round($totalHoursThisWeek - $expectedHoursThisWeek, 1);
// Function to get holidays for a year
function getFeiertageForYear($jahr)
{
global $conn;
$stmt = $conn->prepare("SELECT DATE(datum) AS datum, name FROM Feiertage WHERE strftime('%Y', datum) = ?");
$stmt->execute([$jahr]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$currentYear = date("Y");
$feiertageThisYear = getFeiertageForYear($currentYear);
// Function to get German day name
function getGermanDayName($date)
{
$days = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
return $days[date("w", strtotime($date))];
}
function getEntryById($conn, $id)
{
$stmt = $conn->prepare("SELECT * FROM zeiterfassung WHERE id = :id AND user_id = :user_id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
echo json_encode(["success" => true, "data" => $result]);
} else {
echo json_encode(["success" => false, "message" => "Kein Eintrag gefunden mit ID: $id"]);
}
}
// Gamification: Counting the different weeks worked
$stmt = $conn->prepare("SELECT COUNT(DISTINCT strftime('%W', startzeit)) as weeksCount FROM zeiterfassung WHERE user_id = :user_id");
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$isFirstWeek = $result['weeksCount'] == 1;
$events = [];
// Looping through the records to create events
foreach ($records as $record) {
$startDateTime = new DateTime($record['startzeit']);
$endDateTime = new DateTime($record['endzeit']);
// Überprüfen, ob das 'beschreibung'-Feld leer ist und entsprechend den Titel setzen
$title = empty($record['beschreibung']) ? 'Arbeit' : $record['beschreibung'];
$events[] = [
'id' => $record['id'],
'title' => $title,
'start' => $startDateTime->format("Y-m-d\TH:i:s"),
'end' => $endDateTime->format("Y-m-d\TH:i:s"),
'category' => 'time',
'dueDateClass' => '',
'isAllDay' => false,
'standort' => $record['standort']
];
}
// Looping through the holidays to create events
foreach ($feiertageThisYear as $feiertag) {
$feiertagDateTime = new DateTime($feiertag['datum']); // Corrected key to 'datum'
$events[] = [
'id' => 'feiertag_' . $feiertag['datum'], // Corrected key to 'datum'
'title' => 'Feiertag - ' . $feiertag['name'], // Including the name of the holiday
'start' => $feiertagDateTime->format("Y-m-d"),
'end' => $feiertagDateTime->format("Y-m-d"),
'category' => 'allday',
'isAllDay' => true
];
}
function calculateDuration($startzeit, $endzeit, $pause) {
if (empty($startzeit) || empty($endzeit)) {
return '-';
}
$start = new DateTime($startzeit);
$end = new DateTime($endzeit);
// Calculate difference in minutes
$diff = ($end->getTimestamp() - $start->getTimestamp()) / 60;
// Subtract pause
$totalMinutes = $diff - intval($pause);
// Calculate hours and minutes
$hours = floor($totalMinutes / 60);
$minutes = $totalMinutes % 60;
return sprintf("%02d:%02d", $hours, $minutes);
}
function getPauseDuration($totalHours)
{
global $conn;
$stmt = $conn->prepare("SELECT minimum_pause FROM pause_settings WHERE hours_threshold <= ? ORDER BY hours_threshold DESC LIMIT 1");
$stmt->execute([$totalHours]);
return (int)$stmt->fetchColumn();
}
// Example function to fetch time records with Standort and Beschreibung
function getTimeRecords($conn, $user_id) {
try {
$stmt = $conn->prepare('SELECT id, startzeit, endzeit, pause, standort, beschreibung FROM zeiterfassung WHERE user_id = :user_id ORDER BY startzeit DESC');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Datenbankfehler: " . $e->getMessage());
}
}
// Ensure functions handling updates include Standort and Beschreibung if necessary
function updateTimeRecord($conn, $id, $column, $data) {
// Validate column
$allowedColumns = ['startzeit', 'endzeit', 'pause', 'standort', 'beschreibung'];
if (!in_array($column, $allowedColumns)) {
throw new Exception("Ungültige Spalte.");
}
try {
$stmt = $conn->prepare("UPDATE zeiterfassung SET $column = :data WHERE id = :id AND user_id = :user_id");
$stmt->bindParam(':data', $data);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
} catch (PDOException $e) {
throw new Exception("Datenbankfehler: " . $e->getMessage());
}
}
?>