fetch("SELECT * FROM workers WHERE entry_token = :t AND active = 1", ['t' => $token]) : null; if (!$worker) { http_response_code(404); echo 'Link not found.'; exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'set_day') { $date = $_POST['date'] ?? ''; $status = $_POST['status'] ?? ''; $reason = trim($_POST['reason'] ?? ''); $location = trim($_POST['location'] ?? ''); $currentWeek = currentWeekStart(); // Only allow editing the current week's dates via this self-entry page if (in_array($status, ['full', 'half', 'absent'], true) && in_array($date, getWeekDates($currentWeek), true)) { db()->query(" INSERT INTO day_entries (worker_id, work_date, week_start, status, absence_reason, location) VALUES (:wid, :date, :week, :status, :reason, :location) ON DUPLICATE KEY UPDATE status = :status2, absence_reason = :reason2, location = :location2 ", [ 'wid' => $worker['id'], 'date' => $date, 'week' => $currentWeek, 'status' => $status, 'reason' => $reason ?: null, 'location' => $location ?: null, 'status2' => $status, 'reason2' => $reason ?: null, 'location2' => $location ?: null, ]); } header('Location: /w.php?t=' . urlencode($token)); exit; } $markPaidError = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'mark_paid' && ($_POST['week_start'] ?? '') === currentWeekStart()) { $weekStart = currentWeekStart(); $weekDates = getWeekDates($weekStart); $ph = implode(',', array_fill(0, count($weekDates), '?')); $weekEntries = db()->query("SELECT work_date FROM day_entries WHERE worker_id = ? AND work_date IN ({$ph})", array_merge([$worker['id']], $weekDates))->fetchAll(); $filledDates = array_column($weekEntries, 'work_date'); $missing = array_diff($weekDates, $filledDates); if (!empty($missing)) { $markPaidError = 'Cannot mark paid - ' . count($missing) . ' day(s) still need an entry.'; } else { $existing = db()->fetch("SELECT * FROM week_settlements WHERE worker_id = :wid AND week_start = :w", ['wid' => $worker['id'], 'w' => $weekStart]); if ($existing) { db()->update('week_settlements', ['paid' => 1, 'paid_at' => date('Y-m-d H:i:s')], 'id = :id', ['id' => $existing['id']]); } else { db()->insert('week_settlements', ['worker_id' => $worker['id'], 'week_start' => $weekStart, 'paid' => 1, 'paid_at' => date('Y-m-d H:i:s')]); } header('Location: /w.php?t=' . urlencode($token)); exit; } } $currentWeek = currentWeekStart(); $currentDates = getWeekDates($currentWeek); $currentSettlement = db()->fetch("SELECT * FROM week_settlements WHERE worker_id = :wid AND week_start = :w", ['wid' => $worker['id'], 'w' => $currentWeek]); $currentWeekPaid = $currentSettlement ? (bool) $currentSettlement['paid'] : false; $allEntries = db()->fetchAll("SELECT * FROM day_entries WHERE worker_id = :wid ORDER BY work_date DESC", ['wid' => $worker['id']]); $entriesByDate = []; foreach ($allEntries as $en) { $entriesByDate[$en['work_date']] = $en; } // Past 2 weeks (read-only), excluding current $pastWeeks = []; foreach ($allEntries as $en) { if ($en['week_start'] !== $currentWeek && !in_array($en['week_start'], $pastWeeks, true)) { $pastWeeks[] = $en['week_start']; } } rsort($pastWeeks); $pastWeeks = array_slice($pastWeeks, 0, 2); $pageTitle = 'My Work Week - ChuckCo Time Keeper'; require_once __DIR__ . '/includes/header.php'; ?>

Hi,

This Week —

Paid

This week is locked in. Come back next week to enter new days.

Week of