diff --git a/db/migrations/05_investment_refund_status.sql b/db/migrations/05_investment_refund_status.sql
new file mode 100644
index 0000000..3652fd1
--- /dev/null
+++ b/db/migrations/05_investment_refund_status.sql
@@ -0,0 +1,2 @@
+-- Migration: Add Refunded status to investments
+ALTER TABLE investments MODIFY COLUMN status ENUM('pending', 'approved', 'rejected', 'Refunded') DEFAULT 'pending';
diff --git a/startup_details.php b/startup_details.php
index 8002fcd..145731c 100644
--- a/startup_details.php
+++ b/startup_details.php
@@ -1,51 +1,41 @@
prepare("SELECT * FROM users WHERE id = ?");
+$stmt->execute([$user_id]);
+$user = $stmt->fetch();
-$startup_id = (int)($_GET['id'] ?? 0);
-if (!$startup_id) {
- header("Location: startups.php");
- exit;
-}
-
-// Fetch startup details (handle missing founder due to account deletion)
-$stmt = db()->prepare("SELECT s.*, u.full_name as founder_name, u.university as founder_uni, u.graduation_year FROM startups s LEFT JOIN users u ON s.founder_id = u.id WHERE s.id = ?");
+$startup_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
+$stmt = db()->prepare("SELECT * FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$startup = $stmt->fetch();
if (!$startup) {
- header("Location: startups.php");
+ header('Location: startups.php');
exit;
}
-$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
-$stmt->execute([$_SESSION['user_id']]);
-$user = $stmt->fetch();
-
-// Fetch active funding round
+// Check for active funding round
$stmt = db()->prepare("SELECT * FROM funding_rounds WHERE startup_id = ? AND status = 'Active' LIMIT 1");
$stmt->execute([$startup_id]);
$activeRound = $stmt->fetch();
-// Fetch round history
-$stmt = db()->prepare("SELECT * FROM funding_rounds WHERE startup_id = ? ORDER BY created_at DESC");
-$stmt->execute([$startup_id]);
-$rounds = $stmt->fetchAll();
-
+// Handle Founder Actions
$error = '';
$success = '';
-// Handle Round Controls (Founder Only)
-if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $startup['founder_id'] == $_SESSION['user_id']) {
+if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $user['role'] === 'founder' && $startup['founder_id'] == $user_id) {
$action = $_POST['action'];
- $round_id = (int)($_POST['round_id'] ?? 0);
+ $round_id = isset($_POST['round_id']) ? (int)$_POST['round_id'] : 0;
- if ($action === 'finish_round_early' && $activeRound && $activeRound['id'] == $round_id) {
+ if ($action === 'finish_round' && $activeRound && $activeRound['id'] == $round_id) {
$stmt = db()->prepare("UPDATE funding_rounds SET status = 'Closed' WHERE id = ?");
$stmt->execute([$round_id]);
$success = "Funding round finished early. No new investments allowed.";
@@ -125,6 +115,49 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['
$stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
$stmt->execute([$startup['founder_id'], "New investment of £" . number_format($amount) . " in " . $startup['name'] . "!"]);
+ // Check if goal reached for automated notification system
+ $stmt = db()->prepare("SELECT * FROM funding_rounds WHERE id = ?");
+ $stmt->execute([$activeRound['id']]);
+ $updatedRound = $stmt->fetch();
+
+ if ($updatedRound['funding_raised'] >= $updatedRound['funding_goal']) {
+ // Update status to 'Closed'
+ $stmt = db()->prepare("UPDATE funding_rounds SET status = 'Closed' WHERE id = ?");
+ $stmt->execute([$updatedRound['id']]);
+
+ // Notify Founder (DB)
+ $stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
+ $stmt->execute([$startup['founder_id'], "Congratulations! The funding round for " . $startup['name'] . " has reached its goal of £" . number_format($updatedRound['funding_goal']) . "!"]);
+
+ // Notify All Investors for this round (DB + Email)
+ $stmt = db()->prepare("SELECT DISTINCT u.id, u.email, u.full_name FROM investments i JOIN users u ON i.investor_id = u.id WHERE i.funding_round_id = ? AND i.status = 'approved'");
+ $stmt->execute([$updatedRound['id']]);
+ $investorsToNotify = $stmt->fetchAll();
+
+ foreach ($investorsToNotify as $invUser) {
+ // DB Notification
+ $stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
+ $stmt->execute([$invUser['id'], "Great news! The funding round for " . $startup['name'] . " that you invested in has reached its goal!"]);
+
+ // Email Notification
+ $subject = "Funding Goal Reached for " . $startup['name'];
+ $html = "
Goal Reached!
Hi " . htmlspecialchars($invUser['full_name']) . ", the funding round for " . htmlspecialchars($startup['name']) . " has successfully reached its goal. Thank you for being a part of this journey!
";
+ $text = "Goal Reached! Hi " . $invUser['full_name'] . ", the funding round for " . $startup['name'] . " has successfully reached its goal. Thank you for being a part of this journey!";
+ MailService::sendMail($invUser['email'], $subject, $html, $text);
+ }
+
+ // Email Founder
+ $stmt = db()->prepare("SELECT email, full_name FROM users WHERE id = ?");
+ $stmt->execute([$startup['founder_id']]);
+ $founderUser = $stmt->fetch();
+ if ($founderUser) {
+ $subject = "Funding Goal Reached: " . $startup['name'];
+ $html = "
Congratulations!
Your funding round for " . htmlspecialchars($startup['name']) . " has reached its goal of £" . number_format($updatedRound['funding_goal']) . ".
";
+ $text = "Congratulations! Your funding round for " . $startup['name'] . " has reached its goal of £" . number_format($updatedRound['funding_goal']) . ".";
+ MailService::sendMail($founderUser['email'], $subject, $html, $text);
+ }
+ }
+
db()->commit();
$success = "Investment successful! You've backed the current round.";
// Refresh active round data
@@ -175,145 +208,160 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
+
+
= htmlspecialchars($error) ?>
+
+
+
= htmlspecialchars($success) ?>
+
+
-
+
= substr($startup['name'], 0, 1) ?>
-
= htmlspecialchars($startup['name']) ?>
-
- Founded by = $startup['founder_name'] ? htmlspecialchars($startup['founder_name']) : 'Account Deleted' ?>
- (= htmlspecialchars($startup['founder_uni']) ?>)
+