refactor: sjednoceni zdroje casu na MySQL NOW() + audit log cleanup a UI

- attendance handlery pouzivaji getDbNow() misto PHP date()
- nova helper funkce getDbNow() v AttendanceHelpers.php
- audit log: cleanup endpoint (POST) s volbou stari zaznamu
- audit log: filtry na jednom radku
- dashboard: aktivita prejmenovana na Audit log s odkazem

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 20:48:05 +01:00
parent 36a864c852
commit 5529219234
5 changed files with 157 additions and 17 deletions

View File

@@ -26,12 +26,44 @@ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
if (!in_array($_SERVER['REQUEST_METHOD'], ['GET', 'POST'], true)) {
errorResponse('Method not allowed', 405);
}
requirePermission($authData, 'settings.audit');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = getJsonInput();
$action = $input['action'] ?? '';
if ($action !== 'cleanup') {
errorResponse('Neplatná akce');
}
$days = (int) ($input['days'] ?? 90);
$pdo = db();
if ($days === 0) {
$stmt = $pdo->query('DELETE FROM audit_logs');
$deleted = $stmt->rowCount();
$msg = $deleted > 0
? "Smazáno všech $deleted záznamů"
: 'Audit log je prázdný';
} else {
$days = max(1, $days);
$stmt = $pdo->prepare(
'DELETE FROM audit_logs WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)'
);
$stmt->execute([$days]);
$deleted = $stmt->rowCount();
$msg = $deleted > 0
? "Smazáno $deleted záznamů starších $days dní"
: "Žádné záznamy starší než $days dní nebyly nalezeny";
}
successResponse(['deleted' => $deleted], $msg);
}
$page = max(1, (int) ($_GET['page'] ?? 1));
$perPage = max(1, min(100, (int) ($_GET['per_page'] ?? 50)));

View File

@@ -4,7 +4,8 @@ declare(strict_types=1);
function handleGetCurrent(PDO $pdo, int $userId): void
{
$today = date('Y-m-d');
$dbTime = getDbNow($pdo);
$today = $dbTime['today'];
$stmt = $pdo->prepare("
SELECT id, user_id, shift_date, arrival_time, arrival_lat, arrival_lng,
@@ -68,13 +69,13 @@ function handleGetCurrent(PDO $pdo, int $userId): void
$leaveBalance = getLeaveBalance($pdo, $userId);
$currentYear = (int)date('Y');
$currentMonth = (int)date('m');
$currentYear = $dbTime['year'];
$currentMonth = $dbTime['month'];
$fund = CzechHolidays::getMonthlyWorkFund($currentYear, $currentMonth);
$businessDays = CzechHolidays::getBusinessDaysInMonth($currentYear, $currentMonth);
$startDate = date('Y-m-01');
$endDate = date('Y-m-t');
$startDate = substr($dbTime['today'], 0, 7) . '-01';
$endDate = date('Y-m-t', strtotime($startDate));
$stmt = $pdo->prepare('
SELECT id, user_id, shift_date, arrival_time, break_start, break_end,
@@ -253,8 +254,9 @@ function handlePunch(PDO $pdo, int $userId): void
{
$input = getJsonInput();
$action = $input['punch_action'] ?? '';
$today = date('Y-m-d');
$rawNow = date('Y-m-d H:i:s');
$dbTime = getDbNow($pdo);
$today = $dbTime['today'];
$rawNow = $dbTime['now'];
$lat = isset($input['latitude']) && $input['latitude'] !== '' ? (float)$input['latitude'] : null;
$lng = isset($input['longitude']) && $input['longitude'] !== '' ? (float)$input['longitude'] : null;
@@ -508,7 +510,7 @@ function handleSwitchProject(PDO $pdo, int $userId): void
}
$attendanceId = $currentShift['id'];
$now = date('Y-m-d H:i:s');
$now = getDbNow($pdo)['now'];
$stmt = $pdo->prepare(
'UPDATE attendance_project_logs SET ended_at = ?

View File

@@ -6,6 +6,21 @@
declare(strict_types=1);
/**
* Vraci aktualni cas a datum z MySQL (jednotny zdroj casu)
* @return array{now: string, today: string, year: int, month: int}
*/
function getDbNow(PDO $pdo): array
{
$row = $pdo->query("SELECT NOW() AS now, CURDATE() AS today, YEAR(NOW()) AS y, MONTH(NOW()) AS m")->fetch();
return [
'now' => $row['now'],
'today' => $row['today'],
'year' => (int)$row['y'],
'month' => (int)$row['m'],
];
}
function roundUpTo15Minutes(string $datetime): string
{
$timestamp = strtotime($datetime);
@@ -327,15 +342,14 @@ function addFundDataToUserTotals(PDO $pdo, array &$userTotals, int $year, int $m
}
unset($ut);
$today = date('Y-m-d');
$stmt = $pdo->prepare("
SELECT DISTINCT user_id FROM attendance
WHERE shift_date = ?
WHERE shift_date = CURDATE()
AND arrival_time IS NOT NULL
AND departure_time IS NULL
AND (leave_type IS NULL OR leave_type = 'work')
");
$stmt->execute([$today]);
$stmt->execute();
$workingNow = $stmt->fetchAll(PDO::FETCH_COLUMN);
foreach ($workingNow as $uid) {
if (isset($userTotals[$uid])) {