0) { $code = strtoupper('GC' . bin2hex(random_bytes(4))); db()->insert('gift_cards', [ 'gift_card_id' => generateId('gc_'), 'code' => $code, 'initial_balance' => $initialBalance, 'current_balance' => $initialBalance, 'purchaser_email' => $purchaserEmail ?: null, 'recipient_email' => $recipientEmail ?: null, 'recipient_name' => $recipientName ?: null, 'message' => $message ?: null, 'is_active' => 1 ]); setFlash('success', "Gift card created! Code: $code"); // Send email if recipient provided if ($recipientEmail) { $html = <<

You've Received a Gift!

Hi{$recipientName},

You've received a Tom's Java Jive gift card!

$initialBalance

$code

\ Shop Now

HTML; sendEmail($recipientEmail, "You've Received a Gift Card!", $html); } } else { setFlash('error', 'Invalid balance amount'); } header('Location: /admin/gift-cards.php'); exit; } if ($action === 'toggle' && !empty($_POST['gift_card_id'])) { $gc = db()->fetch("SELECT is_active FROM gift_cards WHERE gift_card_id = :id", ['id' => $_POST['gift_card_id']]); if ($gc) { db()->update('gift_cards', ['is_active' => !$gc['is_active']], 'gift_card_id = :id', ['id' => $_POST['gift_card_id']]); setFlash('success', 'Gift card status updated'); } header('Location: /admin/gift-cards.php'); exit; } if ($action === 'adjust' && !empty($_POST['gift_card_id'])) { $amount = floatval($_POST['amount'] ?? 0); if ($amount != 0) { db()->query( "UPDATE gift_cards SET current_balance = current_balance + :amt WHERE gift_card_id = :id", ['amt' => $amount, 'id' => $_POST['gift_card_id']] ); setFlash('success', 'Balance adjusted'); } header('Location: /admin/gift-cards.php'); exit; } } // Filters $search = $_GET['search'] ?? ''; $status = $_GET['status'] ?? ''; $page = max(1, intval($_GET['page'] ?? 1)); $where = ['1=1']; $params = []; if ($search) { $where[] = '(code LIKE :search OR recipient_email LIKE :search)'; $params['search'] = '%' . $search . '%'; } if ($status === 'active') { $where[] = 'is_active = 1 AND current_balance > 0'; } elseif ($status === 'depleted') { $where[] = 'current_balance <= 0'; } elseif ($status === 'disabled') { $where[] = 'is_active = 0'; } $whereClause = implode(' AND ', $where); $total = db()->count('gift_cards', $whereClause, $params); $pagination = paginate($total, $page, ADMIN_ITEMS_PER_PAGE); $giftCards = db()->fetchAll( "SELECT * FROM gift_cards WHERE {$whereClause} ORDER BY created_at DESC LIMIT :limit OFFSET :offset", array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']]) ); // Stats $totalValue = db()->fetch("SELECT SUM(current_balance) as total FROM gift_cards WHERE is_active = 1")['total'] ?? 0; $activeCount = db()->count('gift_cards', 'is_active = 1 AND current_balance > 0'); ?>
Active Cards
Outstanding Value
Clear
Code Recipient Initial Balance Status Created Actions
No gift cards found
$0.00 Disabled Depleted Active
1): ?>