prepare("SELECT * FROM employees WHERE id = ?"); $employee->execute([$id]); $employee = $employee->fetch(); if (!$employee) { die("Employee not found."); } $pageTitle = "Employee Detail: " . htmlspecialchars($employee['name']); include __DIR__ . '/includes/header.php'; // Fetch recent labour entries $stmt = $db->prepare(" SELECT l.*, p.name as project_name, lt.name as labour_type FROM labour_entries l JOIN projects p ON l.project_id = p.id JOIN labour_types lt ON l.labour_type_id = lt.id WHERE l.employee_id = ? ORDER BY l.entry_date DESC LIMIT 10 "); $stmt->execute([$id]); $labourEntries = $stmt->fetchAll(); // Fetch summary stats $stats = $db->prepare(" SELECT (SELECT SUM(hours) FROM labour_entries WHERE employee_id = ?) as total_hours, (SELECT COUNT(DISTINCT project_id) FROM labour_entries WHERE employee_id = ?) as project_count, (SELECT COUNT(*) FROM attachments a JOIN labour_entries le ON a.entity_id = le.id WHERE a.entity_type = 'labour_entry' AND le.employee_id = ?) as file_count "); $stats->execute([$id, $id, $id]); $stats = $stats->fetch(); // Fetch recent expenses (linked via attachments uploaded by this employee name) $expenseStmt = $db->prepare(" SELECT ex.*, et.name as expense_type, s.name as supplier_name, p.name as project_name FROM expenses ex JOIN expense_types et ON ex.expense_type_id = et.id LEFT JOIN suppliers s ON ex.supplier_id = s.id JOIN projects p ON ex.project_id = p.id JOIN attachments a ON a.entity_id = ex.id AND a.entity_type = 'expense' WHERE a.uploaded_by = ? ORDER BY ex.entry_date DESC LIMIT 10 "); $expenseStmt->execute([$employee['name']]); $expenseEntries = $expenseStmt->fetchAll(); // Fetch recent files (uploaded by this employee or linked to their labour) $fileStmt = $db->prepare(" SELECT a.*, COALESCE(le.entry_date, ex.entry_date) as entry_date, COALESCE(p1.name, p2.name) as project_name FROM attachments a LEFT JOIN labour_entries le ON a.entity_id = le.id AND a.entity_type = 'labour_entry' LEFT JOIN projects p1 ON le.project_id = p1.id LEFT JOIN expenses ex ON a.entity_id = ex.id AND a.entity_type = 'expense' LEFT JOIN projects p2 ON ex.project_id = p2.id WHERE a.uploaded_by = ? OR (a.entity_type = 'labour_entry' AND le.employee_id = ?) ORDER BY a.created_at DESC LIMIT 10 "); $fileStmt->execute([$employee['name'], $id]); $recentFiles = $fileStmt->fetchAll(); function formatBytes($bytes, $precision = 2) { $units = ['B', 'KB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow); return round($bytes, $precision) . ' ' . $units[$pow]; } ?>
= htmlspecialchars($employee['position'] ?? 'Staff') ?> • Joined = $employee['start_date'] ? date('M j, Y', strtotime($employee['start_date'])) : 'N/A' ?>
| Date | Project | Type | Hours |
|---|---|---|---|
| No entries found. | |||
| = date('M j, Y', strtotime($le['entry_date'])) ?> | = htmlspecialchars($le['project_name']) ?> | = htmlspecialchars($le['labour_type']) ?> | = number_format($le['hours'], 1) ?> |
| Date | Project | Type | Amount |
|---|---|---|---|
| No expenses captured by this employee. | |||
| = date('M j, Y', strtotime($ee['entry_date'])) ?> | = htmlspecialchars($ee['project_name']) ?> | = htmlspecialchars($ee['expense_type']) ?> | $= number_format($ee['amount'], 2) ?> |
| Filename | Linked To | Project | Size | Date | Action |
|---|---|---|---|---|---|
| No files found. | |||||
|
= htmlspecialchars($f['file_name']) ?>
|
= ucfirst($f['entity_type']) ?> = $f['entry_date'] ?> | = htmlspecialchars($f['project_name'] ?? 'N/A') ?> | = formatBytes((int)$f['file_size']) ?> | = date('M j, Y', strtotime($f['created_at'])) ?> | View |