78 lines
2.6 KiB
PHP
78 lines
2.6 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'client') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$error = null;
|
|
$success = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['redeem_code'])) {
|
|
$gift_code = $_POST['gift_code'];
|
|
$client_id = $_SESSION['user_id'];
|
|
|
|
// Validate the gift code
|
|
$stmt = db()->prepare('SELECT * FROM gift_codes WHERE code = ?');
|
|
$stmt->execute([$gift_code]);
|
|
$gift = $stmt->fetch();
|
|
|
|
if (!$gift) {
|
|
$error = 'Invalid gift code.';
|
|
} elseif ($gift['is_redeemed']) {
|
|
$error = 'This gift code has already been redeemed.';
|
|
} else {
|
|
// Assign the package to the user
|
|
$package_id = $gift['package_id'];
|
|
|
|
$stmt = db()->prepare('SELECT SUM(quantity) as total_sessions FROM package_service_items WHERE package_id = ? AND service_type IN (\'one_on_one\',\'group_session\')');
|
|
$stmt->execute([$package_id]);
|
|
$result = $stmt->fetch();
|
|
$total_sessions = $result['total_sessions'] ?? 0;
|
|
|
|
$insert_stmt = db()->prepare(
|
|
'INSERT INTO client_packages (client_id, package_id, sessions_remaining) VALUES (?, ?, ?)'
|
|
);
|
|
$insert_stmt->execute([$client_id, $package_id, $total_sessions]);
|
|
|
|
// Mark the code as redeemed
|
|
$update_stmt = db()->prepare(
|
|
'UPDATE gift_codes SET is_redeemed = 1, redeemed_by_client_id = ?, redeemed_at = NOW() WHERE id = ?'
|
|
);
|
|
$update_stmt->execute([$client_id, $gift['id']]);
|
|
|
|
$success = 'Gift redeemed successfully! The package has been added to your account.';
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<h2>Redeem Gift</h2>
|
|
<p>Enter your gift code below to redeem your package.</p>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?php echo $success; ?></div>
|
|
<a href="dashboard.php" class="btn btn-primary">Go to Dashboard</a>
|
|
<?php else: ?>
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label for="gift_code">Gift Code</label>
|
|
<input type="text" class="form-control" id="gift_code" name="gift_code" required>
|
|
</div>
|
|
<button type="submit" name="redeem_code" class="btn btn-primary mt-3">Redeem</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|