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 ? 'deposit' : 'withdrawal', '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.customer_id, c.email, c.name, c.phone, c.wallet_balance, c.reward_points, c.is_active, c.created_at, COALESCE((SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id), 0) as order_count, COALESCE((SELECT SUM(total) FROM orders o WHERE o.customer_id = c.customer_id AND o.payment_status = 'paid'), 0) as total_spent FROM customers c WHERE {$whereClause} ORDER BY c.created_at DESC LIMIT " . (int)$pagination['per_page'] . " OFFSET " . (int)$pagination['offset'], $params ); // 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); ?>
| Customer | Phone | Orders | Total Spent | Wallet | Points | Status | Actions |
|---|---|---|---|---|---|---|---|
| No customers found | |||||||
|
= htmlspecialchars($customer['name'] ?? 'No Name') ?> = htmlspecialchars($customer['email']) ?> |
= htmlspecialchars($customer['phone'] ?? '-') ?> | = $customer['order_count'] ?? 0 ?> | = formatCurrency($customer['total_spent'] ?? 0) ?> | = formatCurrency($customer['wallet_balance'] ?? 0) ?> | = $customer['reward_points'] ?? 0 ?> | Active Inactive | |