From 44a5c2df2dbb285e76b2b224321620fcbaec7d78 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 16 Oct 2025 08:12:27 +0000 Subject: [PATCH] V18 --- admin/login.php | 100 +++++++++---- footer.php | 15 ++ header.php | 4 +- help.php | 135 +++++++++++++++++ help_process.php | 45 ++++++ login.php | 132 ++++++++++++++--- .../20251016_add_referral_code_to_users.sql | 1 + .../20251016_add_tier_to_user_rewards.sql | 1 + ..._add_unique_constraint_to_user_rewards.sql | 1 + .../20251016_create_reward_history_table.sql | 8 + migrations/20251016_create_rewards_table.sql | 6 + payment-success.php | 37 +++++ redeem_points.php | 80 ++++++++++ rewards.php | 130 +++++++++++++++++ signup.php | 138 +++++++++++++++--- 15 files changed, 767 insertions(+), 66 deletions(-) create mode 100644 help.php create mode 100644 help_process.php create mode 100644 migrations/20251016_add_referral_code_to_users.sql create mode 100644 migrations/20251016_add_tier_to_user_rewards.sql create mode 100644 migrations/20251016_add_unique_constraint_to_user_rewards.sql create mode 100644 migrations/20251016_create_reward_history_table.sql create mode 100644 migrations/20251016_create_rewards_table.sql create mode 100644 redeem_points.php create mode 100644 rewards.php diff --git a/admin/login.php b/admin/login.php index c86c77d8..e0275f6a 100644 --- a/admin/login.php +++ b/admin/login.php @@ -14,34 +14,84 @@ unset($_SESSION['login_error']); Admin Login + + -
-
-
-
-
- Admin Login -
-
- -
- -
-
- - -
-
- - -
- -
-
+ Back to Site +
+
+
+

Admin Login

+

Please enter your credentials to access the admin dashboard.

+ + +
+ + +
+
+ + +
+
+ + +
+ +
+
-
- + \ No newline at end of file diff --git a/footer.php b/footer.php index 7c8e7fbc..93588c42 100644 --- a/footer.php +++ b/footer.php @@ -5,5 +5,20 @@ + + + + + \ No newline at end of file diff --git a/header.php b/header.php index fbe6cef3..262e0570 100644 --- a/header.php +++ b/header.php @@ -27,9 +27,9 @@ session_start(); - + \ No newline at end of file diff --git a/migrations/20251016_add_referral_code_to_users.sql b/migrations/20251016_add_referral_code_to_users.sql new file mode 100644 index 00000000..dbd05112 --- /dev/null +++ b/migrations/20251016_add_referral_code_to_users.sql @@ -0,0 +1 @@ +ALTER TABLE users ADD COLUMN referral_code VARCHAR(255) UNIQUE; diff --git a/migrations/20251016_add_tier_to_user_rewards.sql b/migrations/20251016_add_tier_to_user_rewards.sql new file mode 100644 index 00000000..263714dc --- /dev/null +++ b/migrations/20251016_add_tier_to_user_rewards.sql @@ -0,0 +1 @@ +ALTER TABLE user_rewards ADD COLUMN tier VARCHAR(255) NOT NULL DEFAULT 'Bronze'; diff --git a/migrations/20251016_add_unique_constraint_to_user_rewards.sql b/migrations/20251016_add_unique_constraint_to_user_rewards.sql new file mode 100644 index 00000000..436bf97f --- /dev/null +++ b/migrations/20251016_add_unique_constraint_to_user_rewards.sql @@ -0,0 +1 @@ +ALTER TABLE user_rewards ADD UNIQUE (user_id); \ No newline at end of file diff --git a/migrations/20251016_create_reward_history_table.sql b/migrations/20251016_create_reward_history_table.sql new file mode 100644 index 00000000..998aaf00 --- /dev/null +++ b/migrations/20251016_create_reward_history_table.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS reward_history ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL, + points_change INT NOT NULL, + reason VARCHAR(255) NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) +); diff --git a/migrations/20251016_create_rewards_table.sql b/migrations/20251016_create_rewards_table.sql new file mode 100644 index 00000000..3c10999e --- /dev/null +++ b/migrations/20251016_create_rewards_table.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS user_rewards ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL, + points INT NOT NULL DEFAULT 0, + FOREIGN KEY (user_id) REFERENCES users(id) +); \ No newline at end of file diff --git a/payment-success.php b/payment-success.php index ab8e3ef4..33972906 100644 --- a/payment-success.php +++ b/payment-success.php @@ -72,6 +72,43 @@ try { $stmt->execute([$order_id, $item['menu_item_id'], $item['quantity'], $item['price']]); } + // Award points to the user + if (!$is_guest) { + $stmt = $pdo->prepare("SELECT points, tier FROM user_rewards WHERE user_id = ?"); + $stmt->execute([$user_id]); + $user_reward = $stmt->fetch(); + + $current_points = $user_reward ? $user_reward['points'] : 0; + $current_tier = $user_reward ? $user_reward['tier'] : 'Bronze'; + + $multiplier = 1; + if ($current_tier === 'Silver') { + $multiplier = 1.2; + } elseif ($current_tier === 'Gold') { + $multiplier = 1.5; + } + + $points_to_award = floor($total_price * $multiplier); + $new_total_points = $current_points + $points_to_award; + + $new_tier = $current_tier; + if ($new_total_points >= 5000) { + $new_tier = 'Gold'; + } elseif ($new_total_points >= 1000) { + $new_tier = 'Silver'; + } + + $reward_stmt = $pdo->prepare( + "INSERT INTO user_rewards (user_id, points, tier) VALUES (?, ?, ?) " . + "ON DUPLICATE KEY UPDATE points = VALUES(points), tier = VALUES(tier)" + ); + $reward_stmt->execute([$user_id, $new_total_points, $new_tier]); + + // Log the transaction in reward_history + $history_stmt = $pdo->prepare("INSERT INTO reward_history (user_id, points_change, reason) VALUES (?, ?, ?)"); + $history_stmt->execute([$user_id, $points_to_award, 'Order completion']); + } + // Clear cart $stmt = $pdo->prepare("DELETE FROM cart WHERE $cart_column = ?"); $stmt->execute([$cart_identifier]); diff --git a/redeem_points.php b/redeem_points.php new file mode 100644 index 00000000..d79ade20 --- /dev/null +++ b/redeem_points.php @@ -0,0 +1,80 @@ + [ + 'points' => 500, + 'discount' => 5, + 'type' => 'fixed' + ] +]; + +if (!array_key_exists($reward, $rewards)) { + $_SESSION['error_message'] = 'Invalid reward selected.'; + header('Location: rewards.php'); + exit(); +} + +$reward_details = $rewards[$reward]; +$points_required = $reward_details['points']; + +$pdo = db(); + +// Get user's current points +$stmt = $pdo->prepare("SELECT points FROM user_rewards WHERE user_id = ?"); +$stmt->execute([$user_id]); +$user_reward = $stmt->fetch(); + +$current_points = $user_reward ? $user_reward['points'] : 0; + +if ($current_points < $points_required) { + $_SESSION['error_message'] = 'You do not have enough points to redeem this reward.'; + header('Location: rewards.php'); + exit(); +} + +// Deduct points and create coupon +try { + $pdo->beginTransaction(); + + // Deduct points + $new_points = $current_points - $points_required; + $stmt = $pdo->prepare("UPDATE user_rewards SET points = ? WHERE user_id = ?"); + $stmt->execute([$new_points, $user_id]); + + // Log history + $stmt = $pdo->prepare("INSERT INTO reward_history (user_id, points_change, reason) VALUES (?, ?, ?)"); + $stmt->execute([$user_id, -$points_required, 'Redeemed ' . $reward]); + + // Create coupon + $coupon_code = 'REWARD-' . strtoupper(bin2hex(random_bytes(4))) . '-' . $user_id; + $stmt = $pdo->prepare("INSERT INTO coupons (code, type, value, expires_at, is_active) VALUES (?, ?, ?, DATE_ADD(NOW(), INTERVAL 30 DAY), 1)"); + $stmt->execute([$coupon_code, $reward_details['type'], $reward_details['discount']]); + + $pdo->commit(); + + $_SESSION['success_message'] = 'Reward redeemed successfully! Your coupon code is: ' . $coupon_code; + header('Location: rewards.php'); + exit(); + +} catch (Exception $e) { + $pdo->rollBack(); + $_SESSION['error_message'] = 'There was an error redeeming your reward. Please try again.'; + error_log($e->getMessage()); + header('Location: rewards.php'); + exit(); +} \ No newline at end of file diff --git a/rewards.php b/rewards.php new file mode 100644 index 00000000..77f7fe52 --- /dev/null +++ b/rewards.php @@ -0,0 +1,130 @@ +prepare("SELECT points, tier FROM user_rewards WHERE user_id = ?"); + $stmt->execute([$user_id]); + $result = $stmt->fetch(); + + if ($result) { + $points = $result['points']; + $tier = $result['tier']; + } else { + // If user has no entry, create one + $insert_stmt = $pdo->prepare("INSERT INTO user_rewards (user_id, points, tier) VALUES (?, ?, ?)"); + $insert_stmt->execute([$user_id, 0, 'Bronze']); + } + + $history_stmt = $pdo->prepare("SELECT points_change, reason, created_at FROM reward_history WHERE user_id = ? ORDER BY created_at DESC LIMIT 10"); + $history_stmt->execute([$user_id]); + $history = $history_stmt->fetchAll(); + +} catch (PDOException $e) { + // Handle database errors + error_log($e->getMessage()); + // You might want to show a generic error message to the user +} + + +include 'header.php'; +?> + +
+
+
+

Your Rewards

+

Welcome to the MajuroEats Rewards Program! Here you can see your points, your tier, and the rewards you can claim.

+ + + + + + + + + + + +
+

Your Current Points

+

You have points.

+
+ +
+

Your Current Tier

+

You are in the tier.

+
+ +
+

Reward Tiers

+
    +
  • Bronze: 0+ points
  • +
  • Silver: 1000+ points (1.2x point multiplier)
  • +
  • Gold: 5000+ points (1.5x point multiplier)
  • +
+
+ +
+

Available Rewards

+
    +
  • $5 off your next order (500 points)
  • +
  • Free delivery on your next order (1000 points)
  • +
+
+ +
+

Redeem Points

+
+
+ +
+ +
+
+ +
+

Reward History

+ + + + + + + + + + + + + + + + + +
DateReasonPoints
+
+
+
+
+ + diff --git a/signup.php b/signup.php index dfb71ba8..83c52ef8 100644 --- a/signup.php +++ b/signup.php @@ -1,27 +1,123 @@ - + -
-
-

Create Account

-
-
- - + + +
+
+
+

Create Your Account

+

Sign up to start ordering your favorite food.

+ + + + +
+ + +
+
+ + +
+
+ + +
+ + + -
- - +
+

Already have an account? Log in

-
- - -
- - -
-
+
+
+

Your next meal, delivered.

+

The best local restaurants, right at your fingertips.

+
+
+
- + \ No newline at end of file