WebOrbiton
v1.0.0.1

StocketBase

60 lines · 2.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. if (count(get_included_files()) === 1) {
  6. http_response_code(403);
  7. exit;
  8. }
  9. ​
  10. require_once __DIR__ . '/../includes/currency.php';
  11. ​
  12. if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_MANAGER)) {
  13. echo '<p>You don\'t have access to customers.</p>';
  14. return;
  15. }
  16. ​
  17. $usersDb = Database::users();
  18. ​
  19. $customers = $usersDb->query(
  20. "SELECT u.*, COUNT(o.id) AS order_count, COALESCE(SUM(CASE WHEN o.status IN ('paid','processing','fulfilled','shipped','completed') THEN o.total_cents ELSE 0 END), 0) AS lifetime_value
  21. FROM user_accounts u LEFT JOIN orders o ON o.user_account_id = u.id
  22. GROUP BY u.id ORDER BY u.created_at DESC"
  23. )->fetchAll();
  24. ​
  25. $storeCurrency = SiteFront::settings()['store_currency'] ?? 'USD';
  26. ​
  27. ?>
  28. <h1 class="dash-title"><?= Icons::icon("users", "icon icon-lg") ?>Customers</h1>
  29. ​
  30. <table class="dash-table">
  31. <thead>
  32. <tr>
  33. <th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th>
  34. <th><?= Icons::icon('user', 'icon icon-sm') ?>Email</th>
  35. <th><?= Icons::icon('checklist', 'icon icon-sm') ?>Orders</th>
  36. <th><?= Icons::icon('dollar', 'icon icon-sm') ?>Spent in total</th>
  37. <th><?= Icons::icon('flag', 'icon icon-sm') ?>Status</th>
  38. <th></th>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. <?php foreach ($customers as $customer): ?>
  43. <tr>
  44. <td><?= htmlspecialchars((string) ($customer['display_name'] ?? 'No name')) ?></td>
  45. <td><?= htmlspecialchars($customer['email']) ?></td>
  46. <td><?= (int) $customer['order_count'] ?></td>
  47. <td><?= htmlspecialchars(Currency::format((int) $customer['lifetime_value'], $storeCurrency)) ?></td>
  48. <td><span class="status-pill status-<?= $customer['status'] === 'active' ? 'published' : 'rejected' ?>"><?= htmlspecialchars(StatusLabel::get((string) $customer['status'])) ?></span></td>
  49. <td class="dash-table-actions"><a href="dashboard.php?view=customer-detail&id=<?= (int) $customer['id'] ?>" class="dash-btn-small"><?= Icons::icon('eye', 'icon icon-sm') ?>View</a></td>
  50. </tr>
  51. <?php endforeach; ?>
  52. <?php if (empty($customers)): ?>
  53. <tr><td colspan="6">No customers yet.</td></tr>
  54. <?php endif; ?>
  55. </tbody>
  56. </table>
  57. <?php if (Auth::hasRoleAtLeast(Auth::ROLE_SUPER_ADMIN)): ?>
  58. <p style="font-size:12.5px;color:var(--muted);margin-top:12px;">Need to block or unblock someone? Go to <a href="admin.php?tab=customers">Team &rarr; Customers</a>.</p>
  59. <?php endif; ?>
  60. ​