38438-vm/labour_files.php
2026-02-15 15:19:05 +00:00

164 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/db/config.php';
$tenant_id = 1;
// Filters
$project_filter = $_GET['project_id'] ?? '';
$start_date = $_GET['start_date'] ?? '';
$end_date = $_GET['end_date'] ?? '';
$include_archived = isset($_GET['include_archived']) && $_GET['include_archived'] === '1';
// Get Projects for filter
$projects_sql = "SELECT id, name FROM projects WHERE tenant_id = ?";
if (!$include_archived) {
$projects_sql .= " AND is_archived = 0";
}
$projects_sql .= " ORDER BY name ASC";
$projects_stmt = db()->prepare($projects_sql);
$projects_stmt->execute([$tenant_id]);
$all_projects = $projects_stmt->fetchAll();
$where_clauses = ["a.tenant_id = ?", "a.entity_type = 'labour_entry'"];
$params = [$tenant_id];
if (!$include_archived) {
$where_clauses[] = "p.is_archived = 0";
}
if ($project_filter) {
$where_clauses[] = "le.project_id = ?";
$params[] = $project_filter;
}
if ($start_date) {
$where_clauses[] = "le.entry_date >= ?";
$params[] = $start_date;
}
if ($end_date) {
$where_clauses[] = "le.entry_date <= ?";
$params[] = $end_date;
}
$where_sql = implode(" AND ", $where_clauses);
// Fetch Labour Files
$stmt = db()->prepare("
SELECT a.*, le.entry_date, e.name as employee_name, p.name as project_name
FROM attachments a
JOIN labour_entries le ON a.entity_id = le.id
JOIN employees e ON le.employee_id = e.id
JOIN projects p ON le.project_id = p.id
WHERE $where_sql
ORDER BY a.created_at DESC
");
$stmt->execute($params);
$files = $stmt->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];
}
$pageTitle = "SR&ED Manager - Labour Files";
include __DIR__ . '/includes/header.php';
?>
<div class="container-fluid py-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fw-bold mb-0">Labour Files</h2>
</div>
<!-- Filters -->
<div class="card border-0 shadow-sm mb-4">
<div class="card-body">
<form method="GET" class="row g-3">
<div class="col-md-4">
<label class="form-label small fw-bold">Project</label>
<select name="project_id" class="form-select">
<option value="">All Projects</option>
<?php foreach ($all_projects as $p): ?>
<option value="<?= $p['id'] ?>" <?= $project_filter == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<label class="form-label small fw-bold">Start Date</label>
<input type="date" name="start_date" class="form-control" value="<?= htmlspecialchars($start_date) ?>">
</div>
<div class="col-md-3">
<label class="form-label small fw-bold">End Date</label>
<input type="date" name="end_date" class="form-control" value="<?= htmlspecialchars($end_date) ?>">
</div>
<div class="col-md-2 d-flex align-items-center mt-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="include_archived" id="includeArchived" value="1" <?= $include_archived ? 'checked' : '' ?> onchange="this.form.submit()">
<label class="form-check-label small" for="includeArchived">Include Archived</label>
</div>
</div>
<div class="col-md-2 d-flex align-items-end">
<div class="d-grid w-100 gap-2 d-md-flex">
<button type="submit" class="btn btn-primary">Filter</button>
<a href="labour_files.php" class="btn btn-outline-secondary">Reset</a>
</div>
</div>
</form>
</div>
</div>
<div class="card border-0 shadow-sm">
<div class="table-responsive">
<table class="table align-middle mb-0">
<thead class="bg-light">
<tr>
<th>Filename</th>
<th>Size</th>
<th>Uploaded By</th>
<th>Created At</th>
<th>Linked Entry</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($files)): ?>
<tr><td colspan="6" class="text-center py-5 text-muted">No labour files found.</td></tr>
<?php endif; ?>
<?php foreach ($files as $f): ?>
<tr>
<td>
<i class="bi bi-file-earmark-text me-2 text-primary"></i>
<strong><?= htmlspecialchars($f['file_name']) ?></strong>
</td>
<td><small class="text-muted"><?= formatBytes((int)$f['file_size']) ?></small></td>
<td>
<div class="d-flex align-items-center">
<div class="bg-light rounded-circle p-1 me-2" style="width: 24px; height: 24px; display: flex; align-items: center; justify-content: center;">
<i class="bi bi-person small"></i>
</div>
<small><?= htmlspecialchars($f['uploaded_by'] ?? 'System') ?></small>
</div>
</td>
<td class="small text-muted"><?= date('M j, Y H:i', strtotime($f['created_at'])) ?></td>
<td>
<div class="small">
<span class="fw-bold text-dark"><?= $f['entry_date'] ?></span><br>
<span class="text-muted"><?= htmlspecialchars($f['employee_name']) ?> - <?= htmlspecialchars($f['project_name']) ?></span>
</div>
</td>
<td class="text-end">
<a href="<?= htmlspecialchars($f['file_path']) ?>" target="_blank" class="btn btn-sm btn-outline-primary">View</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>