diff --git a/admin.php b/admin.php index d755d50..d154764 100644 --- a/admin.php +++ b/admin.php @@ -2,11 +2,37 @@ $title = 'dashboard'; require_once __DIR__ . '/includes/header.php'; +// Helper functions for status colors +if (!function_exists('getStatusColor')) { + function getStatusColor($status) { + return [ + 'received' => 'secondary', + 'processing' => 'primary', + 'ready' => 'success', + 'delivered' => 'dark', + 'cancelled' => 'danger', + ][$status] ?? 'info'; + } +} +if (!function_exists('getPaymentStatusColor')) { + function getPaymentStatusColor($status) { + return [ + 'unpaid' => 'danger', + 'partially_paid' => 'warning', + 'paid' => 'success', + ][$status] ?? 'info'; + } +} + // Stats logic $branch_id = $_SESSION['branch_id']; $is_super = ($current_role === 'super_admin'); $is_limited = ($current_role === 'limited_viewer'); +// For super_admin, "all" means no branch filtering. +// Otherwise, we filter by the selected branch. +$filter_branch = ($branch_id !== 'all'); + if (!$is_limited) { $stats = [ 'today_revenue' => 0, @@ -16,47 +42,47 @@ if (!$is_limited) { ]; // Today's revenue - $rev_where = $is_super ? "" : " AND order_id IN (SELECT id FROM orders WHERE branch_id = ?)"; - $rev_params = $is_super ? [] : [$branch_id]; + $rev_where = !$filter_branch ? "" : " AND order_id IN (SELECT id FROM orders WHERE branch_id = ?)"; + $rev_params = !$filter_branch ? [] : [$branch_id]; $stmt = db()->prepare("SELECT SUM(amount) as total FROM payments WHERE created_at >= CURDATE() $rev_where"); $stmt->execute($rev_params); $stats['today_revenue'] = $stmt->fetch()['total'] ?? 0; // Active orders (received, processing) - $ord_where = $is_super ? "WHERE status IN ('received', 'processing')" : "WHERE branch_id = ? AND status IN ('received', 'processing')"; - $ord_params = $is_super ? [] : [$branch_id]; + $ord_where = !$filter_branch ? "WHERE status IN ('received', 'processing')" : "WHERE branch_id = ? AND status IN ('received', 'processing')"; + $ord_params = !$filter_branch ? [] : [$branch_id]; $stmt = db()->prepare("SELECT COUNT(*) as count FROM orders $ord_where"); $stmt->execute($ord_params); $stats['active_orders'] = $stmt->fetch()['count'] ?? 0; // Ready orders - $ready_where = $is_super ? "WHERE status = 'ready'" : "WHERE branch_id = ? AND status = 'ready'"; - $ready_params = $is_super ? [] : [$branch_id]; + $ready_where = !$filter_branch ? "WHERE status = 'ready'" : "WHERE branch_id = ? AND status = 'ready'"; + $ready_params = !$filter_branch ? [] : [$branch_id]; $stmt = db()->prepare("SELECT COUNT(*) as count FROM orders $ready_where"); $stmt->execute($ready_params); $stats['ready_orders'] = $stmt->fetch()['count'] ?? 0; // New customers today - $cust_where = $is_super ? "WHERE created_at >= CURDATE()" : "WHERE branch_id = ? AND created_at >= CURDATE()"; - $cust_params = $is_super ? [] : [$branch_id]; + $cust_where = !$filter_branch ? "WHERE created_at >= CURDATE()" : "WHERE branch_id = ? AND created_at >= CURDATE()"; + $cust_params = !$filter_branch ? [] : [$branch_id]; $stmt = db()->prepare("SELECT COUNT(*) as count FROM customers $cust_where"); $stmt->execute($cust_params); $stats['new_customers'] = $stmt->fetch()['count'] ?? 0; - // Recent orders - $recent_where = $is_super ? "" : "WHERE o.branch_id = ?"; - $recent_params = $is_super ? [] : [$branch_id]; - $stmt = db()->prepare("SELECT o.*, c.name_en as customer_name_en, c.name_ar as customer_name_ar - FROM orders o - LEFT JOIN customers c ON o.customer_id = c.id - $recent_where - ORDER BY o.created_at DESC LIMIT 5"); - $stmt->execute($recent_params); - $recent_orders = $stmt->fetchAll(); + // Monthly Orders (Last 12 months) + $monthly_where = !$filter_branch ? "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)" : "WHERE branch_id = ? AND created_at >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)"; + $monthly_params = !$filter_branch ? [] : [$branch_id]; + $stmt = db()->prepare("SELECT DATE_FORMAT(created_at, '%Y-%m') as month_year, COUNT(*) as count + FROM orders + $monthly_where + GROUP BY month_year + ORDER BY month_year ASC"); + $stmt->execute($monthly_params); + $monthly_orders = $stmt->fetchAll(); // Charts data for Dashboard (Last 7 days) - $chart_where = $is_super ? "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)" : "WHERE branch_id = ? AND created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)"; - $chart_params = $is_super ? [] : [$branch_id]; + $chart_where = !$filter_branch ? "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)" : "WHERE branch_id = ? AND created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)"; + $chart_params = !$filter_branch ? [] : [$branch_id]; // Daily Revenue $stmt = db()->prepare("SELECT DATE(created_at) as date, SUM(total_price) as revenue @@ -68,8 +94,8 @@ if (!$is_limited) { $daily_revenue = $stmt->fetchAll(); // Orders by Status (All time or current) - $status_where = $is_super ? "" : "WHERE branch_id = ?"; - $status_params = $is_super ? [] : [$branch_id]; + $status_where = !$filter_branch ? "" : "WHERE branch_id = ?"; + $status_params = !$filter_branch ? [] : [$branch_id]; $stmt = db()->prepare("SELECT status, COUNT(*) as count FROM orders $status_where GROUP BY status"); $stmt->execute($status_params); $orders_by_status = $stmt->fetchAll(); @@ -164,37 +190,8 @@ if (!$is_limited) {
| # | -= __('customer_name') ?> | -= __('total') ?> | -= __('status') ?> | -= __('payment_status') ?> | -= __('actions') ?> | -
|---|---|---|---|---|---|
| = $order['id'] ?> | -= $lang === 'ar' ? ($order['customer_name_ar'] ?: $order['customer_name_en']) : $order['customer_name_en'] ?> | -= format_amount($order['total_price']) ?> | -= __($order['status']) ?> | -= __($order['payment_status']) ?> | -- - - - | -