diff --git a/expense_files.php b/expense_files.php index 4c61839..1b37a60 100644 --- a/expense_files.php +++ b/expense_files.php @@ -4,6 +4,34 @@ require_once __DIR__ . '/db/config.php'; $tenant_id = 1; +// Get Projects for filter +$projects_stmt = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? ORDER BY name ASC"); +$projects_stmt->execute([$tenant_id]); +$all_projects = $projects_stmt->fetchAll(); + +// Filters +$project_filter = $_GET['project_id'] ?? ''; +$start_date = $_GET['start_date'] ?? ''; +$end_date = $_GET['end_date'] ?? ''; + +$where_clauses = ["a.tenant_id = ?", "a.entity_type = 'expense'"]; +$params = [$tenant_id]; + +if ($project_filter) { + $where_clauses[] = "ex.project_id = ?"; + $params[] = $project_filter; +} +if ($start_date) { + $where_clauses[] = "ex.entry_date >= ?"; + $params[] = $start_date; +} +if ($end_date) { + $where_clauses[] = "ex.entry_date <= ?"; + $params[] = $end_date; +} + +$where_sql = implode(" AND ", $where_clauses); + // Fetch Expense Files $stmt = db()->prepare(" SELECT a.*, ex.entry_date, s.name as supplier_name, p.name as project_name @@ -11,10 +39,10 @@ $stmt = db()->prepare(" JOIN expenses ex ON a.entity_id = ex.id JOIN suppliers s ON ex.supplier_id = s.id JOIN projects p ON ex.project_id = p.id - WHERE a.tenant_id = ? AND a.entity_type = 'expense' + WHERE $where_sql ORDER BY a.created_at DESC "); -$stmt->execute([$tenant_id]); +$stmt->execute($params); $files = $stmt->fetchAll(); function formatBytes($bytes, $precision = 2) { @@ -35,6 +63,37 @@ include __DIR__ . '/includes/header.php';

Expense Files

+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ + Reset +
+
+
+
+
+
diff --git a/files.php b/files.php index 64d4c0a..44a7f2c 100644 --- a/files.php +++ b/files.php @@ -4,19 +4,53 @@ require_once __DIR__ . '/db/config.php'; $tenant_id = 1; -// Fetch All Files with their related context +// Get Projects for filter +$projects_stmt = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? ORDER BY name ASC"); +$projects_stmt->execute([$tenant_id]); +$all_projects = $projects_stmt->fetchAll(); + +// Filters +$project_filter = $_GET['project_id'] ?? ''; +$start_date = $_GET['start_date'] ?? ''; +$end_date = $_GET['end_date'] ?? ''; + +$where_clauses = ["a.tenant_id = ?"]; +$params = [$tenant_id]; + +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.*, - CASE - WHEN a.entity_type = 'labour_entry' THEN (SELECT CONCAT(le.entry_date, ' - ', e.name, ' - ', p.name) FROM labour_entries le JOIN employees e ON le.employee_id = e.id JOIN projects p ON le.project_id = p.id WHERE le.id = a.entity_id) - WHEN a.entity_type = 'expense' THEN (SELECT CONCAT(ex.entry_date, ' - ', s.name, ' - ', p.name) FROM expenses ex JOIN suppliers s ON ex.supplier_id = s.id JOIN projects p ON ex.project_id = p.id WHERE ex.id = a.entity_id) - ELSE 'Other' - END as linked_entry_info + 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 - WHERE a.tenant_id = ? + 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([$tenant_id]); +$stmt->execute($params); $files = $stmt->fetchAll(); function formatBytes($bytes, $precision = 2) { @@ -37,6 +71,37 @@ include __DIR__ . '/includes/header.php';

System Files Report

+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ + Reset +
+
+ +
+
+
@@ -74,7 +139,15 @@ include __DIR__ . '/includes/header.php';
- + diff --git a/labour_files.php b/labour_files.php index c77b63c..8a392b8 100644 --- a/labour_files.php +++ b/labour_files.php @@ -4,6 +4,34 @@ require_once __DIR__ . '/db/config.php'; $tenant_id = 1; +// Get Projects for filter +$projects_stmt = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? ORDER BY name ASC"); +$projects_stmt->execute([$tenant_id]); +$all_projects = $projects_stmt->fetchAll(); + +// Filters +$project_filter = $_GET['project_id'] ?? ''; +$start_date = $_GET['start_date'] ?? ''; +$end_date = $_GET['end_date'] ?? ''; + +$where_clauses = ["a.tenant_id = ?", "a.entity_type = 'labour_entry'"]; +$params = [$tenant_id]; + +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 @@ -11,10 +39,10 @@ $stmt = db()->prepare(" 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 a.tenant_id = ? AND a.entity_type = 'labour_entry' + WHERE $where_sql ORDER BY a.created_at DESC "); -$stmt->execute([$tenant_id]); +$stmt->execute($params); $files = $stmt->fetchAll(); function formatBytes($bytes, $precision = 2) { @@ -35,6 +63,37 @@ include __DIR__ . '/includes/header.php';

Labour Files

+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ + Reset +
+
+
+
+
+