187 lines
8.2 KiB
PHP
187 lines
8.2 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 = ?"];
|
|
$params = [$tenant_id];
|
|
|
|
if (!$include_archived) {
|
|
$where_clauses[] = "(lp.is_archived = 0 OR ep.is_archived = 0 OR (a.entity_type NOT IN ('labour_entry', 'expense')))";
|
|
}
|
|
|
|
if ($project_filter) {
|
|
$where_clauses[] = "(le.project_id = ? OR ex.project_id = ?)";
|
|
$params[] = $project_filter;
|
|
$params[] = $project_filter;
|
|
}
|
|
if ($start_date) {
|
|
$where_clauses[] = "(le.entry_date >= ? OR ex.entry_date >= ?)";
|
|
$params[] = $start_date;
|
|
$params[] = $start_date;
|
|
}
|
|
if ($end_date) {
|
|
$where_clauses[] = "(le.entry_date <= ? OR ex.entry_date <= ?)";
|
|
$params[] = $end_date;
|
|
$params[] = $end_date;
|
|
}
|
|
|
|
$where_sql = implode(" AND ", $where_clauses);
|
|
|
|
// Fetch All Files with their related context using LEFT JOINs for better filtering
|
|
$stmt = db()->prepare("
|
|
SELECT a.*,
|
|
le.entry_date as labour_date, e.name as employee_name, lp.name as labour_project,
|
|
ex.entry_date as expense_date, s.name as supplier_name, ep.name as expense_project
|
|
FROM attachments a
|
|
LEFT JOIN labour_entries le ON a.entity_id = le.id AND a.entity_type = 'labour_entry'
|
|
LEFT JOIN employees e ON le.employee_id = e.id
|
|
LEFT JOIN projects lp ON le.project_id = lp.id
|
|
LEFT JOIN expenses ex ON a.entity_id = ex.id AND a.entity_type = 'expense'
|
|
LEFT JOIN suppliers s ON ex.supplier_id = s.id
|
|
LEFT JOIN projects ep ON ex.project_id = ep.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 - All Files Report";
|
|
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">System Files Report</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="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>Type</th>
|
|
<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="7" class="text-center py-5 text-muted">No files found in the system.</td></tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($files as $f): ?>
|
|
<tr>
|
|
<td>
|
|
<?php if ($f['entity_type'] === 'labour_entry'): ?>
|
|
<span class="badge bg-soft-primary text-primary border">Labour</span>
|
|
<?php elseif ($f['entity_type'] === 'expense'): ?>
|
|
<span class="badge bg-soft-success text-success border">Expense</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-soft-secondary text-secondary border"><?= ucfirst($f['entity_type']) ?></span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<strong><?= htmlspecialchars($f['file_name']) ?></strong>
|
|
</td>
|
|
<td><small class="text-muted"><?= formatBytes((int)$f['file_size']) ?></small></td>
|
|
<td><small><?= htmlspecialchars($f['uploaded_by'] ?? 'System') ?></small></td>
|
|
<td class="small text-muted"><?= date('M j, Y', strtotime($f['created_at'])) ?></td>
|
|
<td>
|
|
<small class="text-muted text-truncate d-inline-block" style="max-width: 300px;">
|
|
<?php
|
|
if ($f['entity_type'] === 'labour_entry') {
|
|
echo htmlspecialchars(($f['labour_date'] ?? '') . ' - ' . ($f['employee_name'] ?? '') . ' - ' . ($f['labour_project'] ?? ''));
|
|
} elseif ($f['entity_type'] === 'expense') {
|
|
echo htmlspecialchars(($f['expense_date'] ?? '') . ' - ' . ($f['supplier_name'] ?? '') . ' - ' . ($f['expense_project'] ?? ''));
|
|
} else {
|
|
echo 'N/A';
|
|
}
|
|
?>
|
|
</small>
|
|
</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>
|
|
|
|
<style>
|
|
.bg-soft-primary { background-color: rgba(59, 130, 246, 0.1); }
|
|
.bg-soft-success { background-color: rgba(34, 197, 94, 0.1); }
|
|
.bg-soft-secondary { background-color: rgba(107, 114, 128, 0.1); }
|
|
</style>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|