From 037f8c27be4c3b9325b5ffa8448baebea23783d7 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sun, 15 Feb 2026 15:54:39 +0000 Subject: [PATCH] update to filtering --- api/export_expenses.php | 57 +++++++++++++++++++++ api/export_labour.php | 68 ++++++++++++++++++++++++ expenses.php | 78 ++++++++++++++++++++++++++-- labour.php | 102 +++++++++++++++++++++++++++++++++++- projects.php | 111 ++++++++++++++++++---------------------- 5 files changed, 350 insertions(+), 66 deletions(-) create mode 100644 api/export_expenses.php create mode 100644 api/export_labour.php diff --git a/api/export_expenses.php b/api/export_expenses.php new file mode 100644 index 0000000..e593d8b --- /dev/null +++ b/api/export_expenses.php @@ -0,0 +1,57 @@ += ?"; + $params[] = $filter_start; +} +if ($filter_end) { + $where[] = "e.entry_date <= ?"; + $params[] = $filter_end; +} + +$where_clause = implode(" AND ", $where); + +$stmt = db()->prepare(" + SELECT e.entry_date, s.name as supplier_name, p.name as project_name, e.amount, e.allocation_percent, et.name as expense_type, e.notes + FROM expenses e + JOIN projects p ON e.project_id = p.id + JOIN suppliers s ON e.supplier_id = s.id + LEFT JOIN expense_types et ON e.expense_type_id = et.id + WHERE $where_clause + ORDER BY e.entry_date DESC +"); +$stmt->execute($params); +$data = $stmt->fetchAll(PDO::FETCH_ASSOC); + +$filename_suffix = date('Y-m-d_H-i-s'); +header('Content-Type: text/csv; charset=utf-8'); +header('Content-Disposition: attachment; filename=expenses_export_' . $filename_suffix . '.csv'); + +$output = fopen('php://output', 'w'); +fputcsv($output, ['Date', 'Supplier', 'Project', 'Amount', 'Allocation %', 'Expense Type', 'Notes']); + +foreach ($data as $row) { + fputcsv($output, $row); +} +fclose($output); +exit; diff --git a/api/export_labour.php b/api/export_labour.php new file mode 100644 index 0000000..d967697 --- /dev/null +++ b/api/export_labour.php @@ -0,0 +1,68 @@ += ?"; + $params[] = $filter_start; +} +if ($filter_end) { + $where[] = "le.entry_date <= ?"; + $params[] = $filter_end; +} + +$where_clause = implode(" AND ", $where); + +$stmt = db()->prepare(" + SELECT le.entry_date, e.name as employee_name, p.name as project_name, le.hours, lt.name as labour_type, et.name as evidence_type, le.notes + FROM labour_entries le + JOIN projects p ON le.project_id = p.id + JOIN employees e ON le.employee_id = e.id + LEFT JOIN labour_types lt ON le.labour_type_id = lt.id + LEFT JOIN evidence_types et ON le.evidence_type_id = et.id + WHERE $where_clause + ORDER BY le.entry_date DESC +"); +$stmt->execute($params); +$data = $stmt->fetchAll(PDO::FETCH_ASSOC); + +$filename_suffix = date('Y-m-d_H-i-s'); +header('Content-Type: text/csv; charset=utf-8'); +header('Content-Disposition: attachment; filename=labour_export_' . $filename_suffix . '.csv'); + +$output = fopen('php://output', 'w'); +fputcsv($output, ['Date', 'Employee', 'Project', 'Hours', 'Labour Type', 'Evidence Type', 'Notes']); + +foreach ($data as $row) { + fputcsv($output, $row); +} +fclose($output); +exit; diff --git a/expenses.php b/expenses.php index 6afb8a0..f340f8d 100644 --- a/expenses.php +++ b/expenses.php @@ -48,16 +48,43 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_expense'])) { } // Fetch Data +$filter_project = (int)($_GET['project_id'] ?? 0); +$filter_supplier = (int)($_GET['supplier_id'] ?? 0); +$filter_start = $_GET['start_date'] ?? ''; +$filter_end = $_GET['end_date'] ?? ''; + +$where = ["e.tenant_id = ?"]; +$params = [$tenant_id]; + +if ($filter_project) { + $where[] = "e.project_id = ?"; + $params[] = $filter_project; +} +if ($filter_supplier) { + $where[] = "e.supplier_id = ?"; + $params[] = $filter_supplier; +} +if ($filter_start) { + $where[] = "e.entry_date >= ?"; + $params[] = $filter_start; +} +if ($filter_end) { + $where[] = "e.entry_date <= ?"; + $params[] = $filter_end; +} + +$where_clause = implode(" AND ", $where); + $expenseEntries = db()->prepare(" SELECT e.*, p.name as project_name, s.name as supplier_name, et.name as expense_type FROM expenses e JOIN projects p ON e.project_id = p.id JOIN suppliers s ON e.supplier_id = s.id LEFT JOIN expense_types et ON e.expense_type_id = et.id - WHERE e.tenant_id = ? + WHERE $where_clause ORDER BY e.entry_date DESC, e.created_at DESC "); -$expenseEntries->execute([$tenant_id]); +$expenseEntries->execute($params); $expenseList = $expenseEntries->fetchAll(); $projects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? AND is_archived = 0 ORDER BY name"); @@ -79,7 +106,52 @@ include __DIR__ . '/includes/header.php';

Expenses

- +
+ Export to Excel + +
+
+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + Reset +
+
+
+
diff --git a/labour.php b/labour.php index fa6eca4..ee314cc 100644 --- a/labour.php +++ b/labour.php @@ -82,6 +82,43 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_labour'])) { } // Fetch Data +$filter_project = (int)($_GET['project_id'] ?? 0); +$filter_employee = (int)($_GET['employee_id'] ?? 0); +$filter_type = (int)($_GET['labour_type_id'] ?? 0); +$filter_evidence = (int)($_GET['evidence_type_id'] ?? 0); +$filter_start = $_GET['start_date'] ?? ''; +$filter_end = $_GET['end_date'] ?? ''; + +$where = ["le.tenant_id = ?"]; +$params = [$tenant_id]; + +if ($filter_project) { + $where[] = "le.project_id = ?"; + $params[] = $filter_project; +} +if ($filter_employee) { + $where[] = "le.employee_id = ?"; + $params[] = $filter_employee; +} +if ($filter_type) { + $where[] = "le.labour_type_id = ?"; + $params[] = $filter_type; +} +if ($filter_evidence) { + $where[] = "le.evidence_type_id = ?"; + $params[] = $filter_evidence; +} +if ($filter_start) { + $where[] = "le.entry_date >= ?"; + $params[] = $filter_start; +} +if ($filter_end) { + $where[] = "le.entry_date <= ?"; + $params[] = $filter_end; +} + +$where_clause = implode(" AND ", $where); + $labourEntries = db()->prepare(" SELECT le.*, p.name as project_name, e.name as employee_name, lt.name as labour_type, et.name as evidence_type FROM labour_entries le @@ -89,10 +126,10 @@ $labourEntries = db()->prepare(" JOIN employees e ON le.employee_id = e.id LEFT JOIN labour_types lt ON le.labour_type_id = lt.id LEFT JOIN evidence_types et ON le.evidence_type_id = et.id - WHERE le.tenant_id = ? + WHERE $where_clause ORDER BY le.entry_date DESC, le.created_at DESC "); -$labourEntries->execute([$tenant_id]); +$labourEntries->execute($params); $labourList = $labourEntries->fetchAll(); $projects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? AND is_archived = 0 ORDER BY name"); @@ -141,11 +178,72 @@ include __DIR__ . '/includes/header.php';

Labour Tracking

+ Export to Excel
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + Reset +
+
+
+
+
+ +
+
+
+
+ +
+ + +
+
+
+ + +
+
+ +
+ +
+ + +
+
+
+
+
+ > + +
+
+
+
+ + Reset +
+
+
+
+
+
@@ -198,62 +247,6 @@ include __DIR__ . '/includes/header.php';
- - -
@@ -311,10 +304,6 @@ include __DIR__ . '/includes/header.php';