diff --git a/admin/reports.php b/admin/reports.php index 6af0e07..7b8b11e 100644 --- a/admin/reports.php +++ b/admin/reports.php @@ -1,431 +1,254 @@ query("SELECT id, name, name_ar FROM outlets WHERE is_deleted = 0 ORDER BY name ASC"); -$allOutlets = $outletsStmt->fetchAll(); - -// Base query additions -$outletCondition = ""; -$expenseOutletCondition = ""; -$queryParams = [$startDate, $endDate]; -if (!empty($outletId)) { - $outletCondition = " AND o.outlet_id = ? "; - $expenseOutletCondition = " AND e.outlet_id = ? "; - $queryParams[] = $outletId; +$params = [':from' => $date_from . ' 00:00:00', ':to' => $date_to . ' 23:59:59']; +$outlet_query = ""; +if ($outlet_id !== 'all') { + $outlet_query = " AND o.outlet_id = :outlet_id"; + $params[':outlet_id'] = $outlet_id; } -// 0. Daily Sales -$dailyStmt = $pdo->prepare(" - SELECT - DATE(o.created_at) as sales_date, - COUNT(o.id) as order_count, - SUM(o.total_amount) as total_sales - FROM orders o - WHERE DATE(o.created_at) BETWEEN ? AND ? - AND o.status != 'cancelled' - $outletCondition - GROUP BY DATE(o.created_at) - ORDER BY DATE(o.created_at) DESC -"); -$dailyStmt->execute($queryParams); -$dailySales = $dailyStmt->fetchAll(); +try { + // Outlets for filter + $outlets = $pdo->query("SELECT id, name FROM outlets WHERE is_deleted = 0")->fetchAll(); -// 1. Sales by Staff -$staffStmt = $pdo->prepare(" - SELECT - u.full_name as staff_name, - u.full_name_ar as staff_name_ar, - u.username, - COUNT(o.id) as order_count, - SUM(o.total_amount) as total_sales - FROM orders o - JOIN users u ON o.user_id = u.id - WHERE DATE(o.created_at) BETWEEN ? AND ? - AND o.status != 'cancelled' - $outletCondition - GROUP BY o.user_id - ORDER BY total_sales DESC -"); -$staffStmt->execute($queryParams); -$staffSales = $staffStmt->fetchAll(); + // Daily Sales + $daily_sales = $pdo->prepare("SELECT DATE(o.created_at) as date, SUM(o.total_amount) as total + FROM orders o + WHERE o.created_at BETWEEN :from AND :to $outlet_query + AND o.status = 'completed' + GROUP BY DATE(o.created_at) + ORDER BY date ASC"); + $daily_sales->execute($params); + $daily_sales_data = $daily_sales->fetchAll(); -// 2. Sales by Outlet -$outletStmt = $pdo->prepare(" - SELECT - ot.name as outlet_name, - ot.name_ar as outlet_name_ar, - COUNT(o.id) as order_count, - SUM(o.total_amount) as total_sales - FROM orders o - JOIN outlets ot ON o.outlet_id = ot.id - WHERE DATE(o.created_at) BETWEEN ? AND ? - AND o.status != 'cancelled' - $outletCondition - GROUP BY o.outlet_id - ORDER BY total_sales DESC -"); -$outletStmt->execute($queryParams); -$outletSales = $outletStmt->fetchAll(); + // Staff Sales + $staff_sales = $pdo->prepare("SELECT u.username, SUM(o.total_amount) as total + FROM orders o + JOIN users u ON o.user_id = u.id + WHERE o.created_at BETWEEN :from AND :to $outlet_query + AND o.status = 'completed' + GROUP BY u.id + ORDER BY total DESC"); + $staff_sales->execute($params); + $staff_sales_data = $staff_sales->fetchAll(); -// 3. Sales by Category -$categoryStmt = $pdo->prepare(" - SELECT - c.name as category_name, - c.name_ar as category_name_ar, - SUM(oi.quantity) as items_sold, - SUM(oi.quantity * oi.unit_price) as total_sales - FROM order_items oi - JOIN orders o ON oi.order_id = o.id - JOIN products p ON oi.product_id = p.id - JOIN categories c ON p.category_id = c.id - WHERE DATE(o.created_at) BETWEEN ? AND ? - AND o.status != 'cancelled' - $outletCondition - GROUP BY c.id - ORDER BY total_sales DESC -"); -$categoryStmt->execute($queryParams); -$categorySales = $categoryStmt->fetchAll(); + // Outlet Sales + $outlet_sales = $pdo->prepare("SELECT ou.name, SUM(o.total_amount) as total + FROM orders o + JOIN outlets ou ON o.outlet_id = ou.id + WHERE o.created_at BETWEEN :from AND :to $outlet_query + AND o.status = 'completed' + GROUP BY ou.id + ORDER BY total DESC"); + $outlet_sales->execute($params); + $outlet_sales_data = $outlet_sales->fetchAll(); -// 4. Expenses by Category -$expenseCatStmt = $pdo->prepare(" - SELECT - ec.name as category_name, - ec.name_ar as category_name_ar, - SUM(e.amount) as total_amount - FROM expenses e - JOIN expense_categories ec ON e.category_id = ec.id - WHERE e.expense_date BETWEEN ? AND ? - $expenseOutletCondition - GROUP BY ec.id - ORDER BY total_amount DESC -"); -$expenseCatStmt->execute($queryParams); -$expenseSales = $expenseCatStmt->fetchAll(); + // Category Sales + $cat_sales = $pdo->prepare("SELECT c.name, SUM(oi.quantity * oi.unit_price) as total + FROM order_items oi + JOIN orders o ON oi.order_id = o.id + JOIN products p ON oi.product_id = p.id + JOIN categories c ON p.category_id = c.id + WHERE o.created_at BETWEEN :from AND :to $outlet_query + AND o.status = 'completed' + GROUP BY c.id + ORDER BY total DESC"); + $cat_sales->execute($params); + $cat_sales_data = $cat_sales->fetchAll(); -// Total Summary -$totalStmt = $pdo->prepare(" - SELECT - COUNT(o.id) as total_orders, - SUM(o.total_amount) as total_revenue - FROM orders o - WHERE DATE(o.created_at) BETWEEN ? AND ? - AND o.status != 'cancelled' - $outletCondition -"); -$totalStmt->execute($queryParams); -$summary = $totalStmt->fetch(); + // Expenses + $expenses = $pdo->prepare("SELECT ec.name, SUM(e.amount) as total + FROM expenses e + JOIN expense_categories ec ON e.category_id = ec.id + WHERE e.expense_date BETWEEN :from AND :to + AND (:outlet_id_all = 'all' OR e.outlet_id = :outlet_id_exp) + GROUP BY ec.id"); + $exp_params = [':from' => $date_from, ':to' => $date_to, ':outlet_id_all' => $outlet_id, ':outlet_id_exp' => $outlet_id]; + $expenses->execute($exp_params); + $expense_data = $expenses->fetchAll(); -$totalExpStmt = $pdo->prepare(" - SELECT SUM(e.amount) as total_expenses - FROM expenses e - WHERE e.expense_date BETWEEN ? AND ? - $expenseOutletCondition -"); -$totalExpStmt->execute($queryParams); -$totalExpenses = $totalExpStmt->fetchColumn() ?? 0; + // Summary + $total_sales = array_sum(array_column($daily_sales_data, 'total')); + $total_expenses = array_sum(array_column($expense_data, 'total')); + $net_profit = $total_sales - $total_expenses; -$netProfit = ($summary['total_revenue'] ?? 0) - $totalExpenses; +} catch (PDOException $e) { + echo '
Overview of your business performance
-| Date | -Orders | -Revenue | -
|---|---|---|
| No sales found for the selected period | ||
| = date('M d, Y (D)', strtotime($day['sales_date'])) ?> | -= $day['order_count'] ?> | -= format_currency($day['total_sales']) ?> | -
| Category | -Items Sold | -Total Revenue | ++ | |
|---|---|---|---|---|
| No data found for this period | ||||
|
- = htmlspecialchars($cat['category_name']) ?>
-
- = htmlspecialchars($cat['category_name_ar']) ?>
-
- |
- = $cat['items_sold'] ?> | -= format_currency($cat['total_sales']) ?> | -||
| + | + | |||
| Category | -Total Amount | ++ | |
|---|---|---|---|
| No data found for this period | |||
|
- = htmlspecialchars($exp['category_name']) ?>
-
- = htmlspecialchars($exp['category_name_ar']) ?>
-
- |
- = format_currency($exp['total_amount']) ?> | -||
| Staff Name | -Orders | -Total Sales | -
|---|---|---|
| No data found for this period | ||
|
- = htmlspecialchars($staff['staff_name'] ?: $staff['username']) ?>
-
- = htmlspecialchars($staff['staff_name_ar']) ?>
-
- |
- = $staff['order_count'] ?> | -= format_currency($staff['total_sales']) ?> | -
| Outlet | -Orders | -Total Sales | -
|---|---|---|
| No data found for this period | ||
|
- = htmlspecialchars($outlet['outlet_name']) ?>
-
- = htmlspecialchars($outlet['outlet_name_ar']) ?>
-
- |
- = $outlet['order_count'] ?> | -= format_currency($outlet['total_sales']) ?> | -
| + | + | |