diff --git a/admin.php b/admin.php index ffea986..d755d50 100644 --- a/admin.php +++ b/admin.php @@ -4,167 +4,274 @@ require_once __DIR__ . '/includes/header.php'; // Stats logic $branch_id = $_SESSION['branch_id']; -$stats = [ - 'today_revenue' => 0, - 'active_orders' => 0, - 'new_customers' => 0, - 'ready_orders' => 0, -]; +$is_super = ($current_role === 'super_admin'); +$is_limited = ($current_role === 'limited_viewer'); -// Today's revenue -$stmt = db()->prepare("SELECT SUM(amount) as total FROM payments WHERE created_at >= CURDATE() AND order_id IN (SELECT id FROM orders WHERE branch_id = ?)"); -$stmt->execute([$branch_id]); -$stats['today_revenue'] = $stmt->fetch()['total'] ?? 0; +if (!$is_limited) { + $stats = [ + 'today_revenue' => 0, + 'active_orders' => 0, + 'new_customers' => 0, + 'ready_orders' => 0, + ]; -// Active orders (received, processing) -$stmt = db()->prepare("SELECT COUNT(*) as count FROM orders WHERE branch_id = ? AND status IN ('received', 'processing')"); -$stmt->execute([$branch_id]); -$stats['active_orders'] = $stmt->fetch()['count'] ?? 0; + // Today's revenue + $rev_where = $is_super ? "" : " AND order_id IN (SELECT id FROM orders WHERE branch_id = ?)"; + $rev_params = $is_super ? [] : [$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; -// Ready orders -$stmt = db()->prepare("SELECT COUNT(*) as count FROM orders WHERE branch_id = ? AND status = 'ready'"); -$stmt->execute([$branch_id]); -$stats['ready_orders'] = $stmt->fetch()['count'] ?? 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]; + $stmt = db()->prepare("SELECT COUNT(*) as count FROM orders $ord_where"); + $stmt->execute($ord_params); + $stats['active_orders'] = $stmt->fetch()['count'] ?? 0; -// New customers today -$stmt = db()->prepare("SELECT COUNT(*) as count FROM customers WHERE branch_id = ? AND created_at >= CURDATE()"); -$stmt->execute([$branch_id]); -$stats['new_customers'] = $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]; + $stmt = db()->prepare("SELECT COUNT(*) as count FROM orders $ready_where"); + $stmt->execute($ready_params); + $stats['ready_orders'] = $stmt->fetch()['count'] ?? 0; -// Recent orders -$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 - WHERE o.branch_id = ? - ORDER BY o.created_at DESC LIMIT 5"); -$stmt->execute([$branch_id]); -$recent_orders = $stmt->fetchAll(); + // New customers today + $cust_where = $is_super ? "WHERE created_at >= CURDATE()" : "WHERE branch_id = ? AND created_at >= CURDATE()"; + $cust_params = $is_super ? [] : [$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(); + + // 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]; + + // Daily Revenue + $stmt = db()->prepare("SELECT DATE(created_at) as date, SUM(total_price) as revenue + FROM orders + $chart_where + GROUP BY DATE(created_at) + ORDER BY DATE(created_at) ASC"); + $stmt->execute($chart_params); + $daily_revenue = $stmt->fetchAll(); + + // Orders by Status (All time or current) + $status_where = $is_super ? "" : "WHERE branch_id = ?"; + $status_params = $is_super ? [] : [$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(); +} ?> -
+ = is_arabic() ? 'مرحباً بك في لوحة التحكم' : 'Welcome to your dashboard' ?> +
+| # | -= __('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']) ?> | -- - - - | -
| # | += __('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']) ?> | ++ + + + | +
You don't have permission to add branches.
+