diff --git a/db/setup.php b/db/setup.php index cdea52c..39f8189 100644 --- a/db/setup.php +++ b/db/setup.php @@ -7,7 +7,8 @@ function setup_database($pdo) { email VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, role ENUM('Admin', 'Sales Rep', 'Dispatch') NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )"); $pdo->exec("CREATE TABLE IF NOT EXISTS orders ( @@ -16,6 +17,8 @@ function setup_database($pdo) { order_date DATE NOT NULL, order_text TEXT NOT NULL, status ENUM('Pending', 'Query', 'Query Replied', 'Shipped', 'Cancelled') NOT NULL DEFAULT 'Pending', + query_text TEXT DEFAULT NULL, + reply_text TEXT DEFAULT NULL, sales_rep_id INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/index.php b/index.php index 7ab3303..b4af665 100644 --- a/index.php +++ b/index.php @@ -15,20 +15,35 @@ $shipped_orders_month_count = 0; try { // --- Fetch Dashboard Counts --- - $conditions = ''; + $base_conditions_arr = []; $params = []; + if ($user_role === 'Sales Rep') { - $conditions = " WHERE sales_rep_id = :user_id"; + $base_conditions_arr[] = "sales_rep_id = :user_id"; $params[':user_id'] = $user_id; } // Pending Orders Count - $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $conditions . " AND status = 'Pending'"); + $pending_orders_conditions_arr = $base_conditions_arr; + $pending_orders_conditions_arr[] = "status = 'Pending'"; + + $pending_orders_where_clause = ''; + if (!empty($pending_orders_conditions_arr)) { + $pending_orders_where_clause = " WHERE " . implode(" AND ", $pending_orders_conditions_arr); + } + $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $pending_orders_where_clause); $stmt->execute($params); $pending_orders_count = $stmt->fetchColumn(); // Pending Replies Count (Orders with status 'Query' or 'Query Replied') - $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $conditions . " AND (status = 'Query' OR status = 'Query Replied')"); + $pending_replies_conditions_arr = $base_conditions_arr; + $pending_replies_conditions_arr[] = "(status = 'Query' OR status = 'Query Replied')"; + + $pending_replies_where_clause = ''; + if (!empty($pending_replies_conditions_arr)) { + $pending_replies_where_clause = " WHERE " . implode(" AND ", $pending_replies_conditions_arr); + } + $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $pending_replies_where_clause); $stmt->execute($params); $pending_replies_count = $stmt->fetchColumn(); @@ -36,12 +51,21 @@ try { $current_month_start = date('Y-m-01 00:00:00'); $current_month_end = date('Y-m-t 23:59:59'); - $shipped_sql = "SELECT COUNT(*) FROM orders " . $conditions . " AND status = 'Shipped' AND created_at >= :start_date AND created_at <= :end_date"; + $shipped_conditions_arr = $base_conditions_arr; + $shipped_conditions_arr[] = "status = 'Shipped'"; + $shipped_conditions_arr[] = "created_at >= :start_date"; + $shipped_conditions_arr[] = "created_at <= :end_date"; + + $shipped_where_clause = ''; $shipped_params = $params; $shipped_params[':start_date'] = $current_month_start; $shipped_params[':end_date'] = $current_month_end; - $stmt = $pdo->prepare($shipped_sql); + if (!empty($shipped_conditions_arr)) { + $shipped_where_clause = " WHERE " . implode(" AND ", $shipped_conditions_arr); + } + + $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $shipped_where_clause); $stmt->execute($shipped_params); $shipped_orders_month_count = $stmt->fetchColumn(); @@ -133,9 +157,9 @@ try {