fetch("SELECT id FROM customers WHERE email = :email", ['email' => $email]); if ($existing) { setFlash('error', 'Customer with this email already exists'); } else { db()->insert('customers', [ 'customer_id' => generateId('cust_'), 'email' => $email, 'name' => $name ?: null, 'phone' => $phone ?: null, 'wallet_balance' => $walletBalance, 'reward_points' => $rewardPoints, 'is_active' => 1 ]); setFlash('success', 'Customer created successfully'); } } header('Location: /admin/customers.php'); exit; } if ($action === 'update' && !empty($_POST['customer_id'])) { $customerId = $_POST['customer_id']; $name = trim($_POST['name'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $walletBalance = floatval($_POST['wallet_balance'] ?? 0); $rewardPoints = intval($_POST['reward_points'] ?? 0); $isActive = isset($_POST['is_active']) ? 1 : 0; db()->update('customers', [ 'name' => $name ?: null, 'phone' => $phone ?: null, 'wallet_balance' => $walletBalance, 'reward_points' => $rewardPoints, 'is_active' => $isActive ], 'customer_id = :id', ['id' => $customerId]); setFlash('success', 'Customer updated successfully'); header('Location: /admin/customers.php'); exit; } if ($action === 'delete' && !empty($_POST['customer_id'])) { db()->delete('customers', 'customer_id = :id', ['id' => $_POST['customer_id']]); setFlash('success', 'Customer deleted'); header('Location: /admin/customers.php'); exit; } if ($action === 'adjust_wallet' && !empty($_POST['customer_id'])) { $amount = floatval($_POST['amount'] ?? 0); $reason = trim($_POST['reason'] ?? ''); if ($amount != 0) { db()->query( "UPDATE customers SET wallet_balance = wallet_balance + :amt WHERE customer_id = :id", ['amt' => $amount, 'id' => $_POST['customer_id']] ); // Log transaction db()->insert('wallet_transactions', [ 'transaction_id' => generateId('wt_'), 'customer_id' => $_POST['customer_id'], 'amount' => $amount, 'type' => $amount > 0 ? 'credit' : 'debit', 'description' => $reason ?: 'Admin adjustment', 'balance_after' => db()->fetch("SELECT wallet_balance FROM customers WHERE customer_id = :id", ['id' => $_POST['customer_id']])['wallet_balance'] ?? 0 ]); setFlash('success', 'Wallet adjusted by $' . number_format($amount, 2)); } header('Location: /admin/customers.php'); exit; } } // Filters $search = $_GET['search'] ?? ''; $status = $_GET['status'] ?? ''; $page = max(1, intval($_GET['page'] ?? 1)); $where = ['1=1']; $params = []; if ($search) { $where[] = '(email LIKE :search OR name LIKE :search OR phone LIKE :search)'; $params['search'] = '%' . $search . '%'; } if ($status === 'active') { $where[] = 'is_active = 1'; } elseif ($status === 'inactive') { $where[] = 'is_active = 0'; } $whereClause = implode(' AND ', $where); $total = db()->count('customers', $whereClause, $params); $pagination = paginate($total, $page, ADMIN_ITEMS_PER_PAGE); $customers = db()->fetchAll( "SELECT c.*, (SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id) as order_count, (SELECT SUM(total) FROM orders o WHERE o.customer_id = c.customer_id AND o.payment_status = 'paid') as total_spent FROM customers c WHERE {$whereClause} ORDER BY c.created_at DESC LIMIT :limit OFFSET :offset", array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']]) ); // Stats $totalCustomers = db()->count('customers'); $activeCustomers = db()->count('customers', 'is_active = 1'); $totalWalletBalance = (float)(db()->fetch("SELECT COALESCE(SUM(wallet_balance),0) as total FROM customers")['total'] ?? 0); ?>
Total Customers
Active
Total Wallet Balance
Clear
customers found
Customer Phone Orders Total Spent Wallet Points Status Actions
No customers found

Active Inactive
1): ?>