300 lines
12 KiB
PHP
300 lines
12 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$tenant_id = 1;
|
|
|
|
// Fetch Highlights Data
|
|
$projectHighlights = db()->prepare("
|
|
SELECT p.*,
|
|
CONCAT(e.first_name, ' ', e.last_name) as owner_name,
|
|
COALESCE((SELECT SUM(hours) FROM labour_entries WHERE project_id = p.id), 0) as total_hours
|
|
FROM projects p
|
|
LEFT JOIN employees e ON p.owner_id = e.id
|
|
WHERE p.tenant_id = ?
|
|
ORDER BY p.created_at DESC
|
|
LIMIT 5
|
|
");
|
|
$projectHighlights->execute([$tenant_id]);
|
|
$projectList = $projectHighlights->fetchAll();
|
|
|
|
$labourHighlights = db()->prepare("
|
|
SELECT le.*, p.name as project_name, e.name as employee_name
|
|
FROM labour_entries le
|
|
JOIN projects p ON le.project_id = p.id
|
|
JOIN employees e ON le.employee_id = e.id
|
|
WHERE le.tenant_id = ?
|
|
ORDER BY le.entry_date DESC, le.created_at DESC
|
|
LIMIT 5
|
|
");
|
|
$labourHighlights->execute([$tenant_id]);
|
|
$labourList = $labourHighlights->fetchAll();
|
|
|
|
$expenseHighlights = db()->prepare("
|
|
SELECT e.*, p.name as project_name, s.name as supplier_name
|
|
FROM expenses e
|
|
JOIN projects p ON e.project_id = p.id
|
|
JOIN suppliers s ON e.supplier_id = s.id
|
|
WHERE e.tenant_id = ?
|
|
ORDER BY e.entry_date DESC, e.created_at DESC
|
|
LIMIT 5
|
|
");
|
|
$expenseHighlights->execute([$tenant_id]);
|
|
$expenseList = $expenseHighlights->fetchAll();
|
|
|
|
$employeeHighlights = db()->prepare("
|
|
SELECT e.*
|
|
FROM employees e
|
|
WHERE e.tenant_id = ?
|
|
ORDER BY e.created_at DESC
|
|
LIMIT 5
|
|
");
|
|
$employeeHighlights->execute([$tenant_id]);
|
|
$employeeList = $employeeHighlights->fetchAll();
|
|
|
|
// Chart Data
|
|
$chart_days = isset($_GET['chart_days']) ? (int)$_GET['chart_days'] : 7;
|
|
if (!in_array($chart_days, [7, 14, 30])) $chart_days = 7;
|
|
|
|
$chartDataQuery = db()->prepare("
|
|
SELECT entry_date, SUM(hours) as total_hours
|
|
FROM labour_entries
|
|
WHERE tenant_id = ? AND entry_date > DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
|
|
GROUP BY entry_date
|
|
ORDER BY entry_date ASC
|
|
");
|
|
$chartDataQuery->execute([$tenant_id, $chart_days]);
|
|
$rawChartData = $chartDataQuery->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
$chartLabels = [];
|
|
$chartValues = [];
|
|
for ($i = $chart_days - 1; $i >= 0; $i--) {
|
|
$date = date('Y-m-d', strtotime("-$i days"));
|
|
$chartLabels[] = date('M d', strtotime($date));
|
|
$chartValues[] = (float)($rawChartData[$date] ?? 0);
|
|
}
|
|
|
|
$activities = db()->prepare("SELECT * FROM activity_log WHERE tenant_id = ? ORDER BY created_at DESC LIMIT 10");
|
|
$activities->execute([$tenant_id]);
|
|
$activityList = $activities->fetchAll();
|
|
|
|
$pageTitle = "SR&ED Manager - Dashboard";
|
|
include __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid py-4">
|
|
<!-- Dashboard Chart Section -->
|
|
<div class="row mb-4">
|
|
<div class="col-12">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-header bg-white d-flex justify-content-between align-items-center py-3">
|
|
<h5 class="mb-0 fw-bold">Labour Hours Overview</h5>
|
|
<div class="btn-group btn-group-sm">
|
|
<a href="?chart_days=7" class="btn btn-outline-primary <?= $chart_days == 7 ? 'active' : '' ?>">7 Days</a>
|
|
<a href="?chart_days=14" class="btn btn-outline-primary <?= $chart_days == 14 ? 'active' : '' ?>">14 Days</a>
|
|
<a href="?chart_days=30" class="btn btn-outline-primary <?= $chart_days == 30 ? 'active' : '' ?>">30 Days</a>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div style="height: 300px;">
|
|
<canvas id="hoursChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row g-4">
|
|
<!-- Projects Highlight -->
|
|
<div class="col-md-6">
|
|
<div class="card border-0 shadow-sm h-100">
|
|
<div class="card-header bg-white d-flex justify-content-between align-items-center">
|
|
<h6 class="mb-0 fw-bold">Recent Projects</h6>
|
|
<a href="projects.php" class="btn btn-sm btn-link text-decoration-none">View All</a>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm align-middle mb-0">
|
|
<thead>
|
|
<tr class="extra-small text-muted">
|
|
<th>PROJECT</th>
|
|
<th>STATUS</th>
|
|
<th class="text-end">HOURS</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($projectList as $p): ?>
|
|
<tr>
|
|
<td>
|
|
<div class="fw-bold small"><?= htmlspecialchars($p['name']) ?></div>
|
|
<div class="extra-small text-muted"><?= htmlspecialchars($p['code']) ?></div>
|
|
</td>
|
|
<td><span class="status-badge status-<?= str_replace('_', '-', $p['status']) ?>"><?= ucfirst($p['status']) ?></span></td>
|
|
<td class="text-end fw-bold"><?= number_format((float)$p['total_hours'], 1) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Labour Highlights -->
|
|
<div class="col-md-6">
|
|
<div class="card border-0 shadow-sm h-100">
|
|
<div class="card-header bg-white d-flex justify-content-between align-items-center">
|
|
<h6 class="mb-0 fw-bold">Recent Labour</h6>
|
|
<a href="labour.php" class="btn btn-sm btn-link text-decoration-none">View All</a>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm align-middle mb-0">
|
|
<thead>
|
|
<tr class="extra-small text-muted">
|
|
<th>DATE</th>
|
|
<th>EMPLOYEE</th>
|
|
<th class="text-end">HOURS</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($labourList as $l): ?>
|
|
<tr>
|
|
<td class="extra-small text-muted"><?= $l['entry_date'] ?></td>
|
|
<td class="small fw-bold"><?= htmlspecialchars($l['employee_name']) ?></td>
|
|
<td class="text-end"><span class="badge bg-light text-primary border"><?= number_format((float)$l['hours'], 1) ?>h</span></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Expense Highlights -->
|
|
<div class="col-md-6">
|
|
<div class="card border-0 shadow-sm h-100">
|
|
<div class="card-header bg-white d-flex justify-content-between align-items-center">
|
|
<h6 class="mb-0 fw-bold">Recent Expenses</h6>
|
|
<a href="expenses.php" class="btn btn-sm btn-link text-decoration-none">View All</a>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm align-middle mb-0">
|
|
<thead>
|
|
<tr class="extra-small text-muted">
|
|
<th>DATE</th>
|
|
<th>SUPPLIER</th>
|
|
<th class="text-end">AMOUNT</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($expenseList as $ex): ?>
|
|
<tr>
|
|
<td class="extra-small text-muted"><?= $ex['entry_date'] ?></td>
|
|
<td class="small fw-bold"><?= htmlspecialchars($ex['supplier_name']) ?></td>
|
|
<td class="text-end fw-bold">$<?= number_format((float)$ex['amount'], 2) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Employee Highlights -->
|
|
<div class="col-md-6">
|
|
<div class="card border-0 shadow-sm h-100">
|
|
<div class="card-header bg-white d-flex justify-content-between align-items-center">
|
|
<h6 class="mb-0 fw-bold">New Employees</h6>
|
|
<a href="employees.php" class="btn btn-sm btn-link text-decoration-none">View All</a>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm align-middle mb-0">
|
|
<thead>
|
|
<tr class="extra-small text-muted">
|
|
<th>NAME</th>
|
|
<th>POSITION</th>
|
|
<th class="text-end">JOINED</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($employeeList as $e): ?>
|
|
<tr>
|
|
<td class="small fw-bold"><?= htmlspecialchars($e['first_name'] . ' ' . $e['last_name']) ?></td>
|
|
<td class="extra-small text-muted"><?= htmlspecialchars($e['position']) ?></td>
|
|
<td class="text-end extra-small text-muted"><?= $e['start_date'] ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Recent Activity Section -->
|
|
<div class="row mt-4" id="activity-section">
|
|
<div class="col-12">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-header bg-white py-3">
|
|
<h5 class="mb-0 fw-bold">Recent Activity</h5>
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead>
|
|
<tr class="extra-small text-muted">
|
|
<th style="width: 200px;">TIME</th>
|
|
<th>ACTION</th>
|
|
<th>DETAILS</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($activityList as $a): ?>
|
|
<tr>
|
|
<td class="text-muted extra-small"><?= date('M d, Y H:i', strtotime($a['created_at'])) ?></td>
|
|
<td><span class="badge bg-light text-primary border extra-small"><?= htmlspecialchars($a['action']) ?></span></td>
|
|
<td class="small"><?= htmlspecialchars($a['details']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const ctx = document.getElementById('hoursChart').getContext('2d');
|
|
new Chart(ctx, {
|
|
type: 'line',
|
|
data: {
|
|
labels: <?= json_encode($chartLabels) ?>,
|
|
datasets: [{
|
|
label: 'Labour Hours',
|
|
data: <?= json_encode($chartValues) ?>,
|
|
borderColor: '#3b82f6',
|
|
backgroundColor: 'rgba(59, 130, 246, 0.1)',
|
|
fill: true,
|
|
tension: 0.4,
|
|
pointRadius: 4,
|
|
pointBackgroundColor: '#3b82f6'
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: { display: false }
|
|
},
|
|
scales: {
|
|
y: { beginAtZero: true, grid: { borderDash: [5, 5] } },
|
|
x: { grid: { display: false } }
|
|
}
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|