prepare(" SELECT s.*, fr.id as round_id, fr.funding_goal, fr.funding_raised, fr.status as round_status, s.founder_id as founder_id FROM startups s LEFT JOIN funding_rounds fr ON s.id = fr.startup_id AND fr.status = 'Active' WHERE s.id = ? "); $stmt->execute([$startupId]); $startup = $stmt->fetch(); if (!$startup) { die("Startup not found."); } if ($startup['round_status'] !== 'Active') { die("This startup does not have an active funding round."); } $error = ''; $success = ''; // Get investor's current balance $stmt = db()->prepare("SELECT balance FROM users WHERE id = ?"); $stmt->execute([$_SESSION['user_id']]); $investor_balance = (float)$stmt->fetchColumn(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $amount = (float)($_POST['amount'] ?? 0); $investor_id = $_SESSION['user_id']; $remaining = $startup['funding_goal'] - $startup['funding_raised']; if ($amount < 50) { $error = "Minimum investment is £50."; } elseif ($amount > $investor_balance) { $error = "Insufficient funds in your money pot. Please add funds first."; } elseif ($amount > $remaining) { $error = "You cannot invest more than the remaining goal (£" . number_format($remaining, 2) . ")."; } else { db()->beginTransaction(); try { // Repayment & Dividend Logic $interest_rate = (float)($startup['founder_return_rate'] ?? 0); $repayment_term = (int)($startup['repayment_term'] ?? 12); if ($repayment_term <= 0) $repayment_term = 12; $total_return = $amount + ($amount * ($interest_rate / 100)); $monthly_dividend = $total_return / $repayment_term; // First payment is one month after investment date $next_payment_date = date('Y-m-d', strtotime('+1 month')); // 1. Create investment record $equity_pct = 0.00; // Mock logic for equity $stmt = db()->prepare("INSERT INTO investments ( startup_id, investor_id, funding_round_id, amount, interest_rate, repayment_term, total_return, monthly_dividend, next_payment_date, equity_pct, status ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'approved')"); $stmt->execute([ $startupId, $investor_id, $startup['round_id'], $amount, $interest_rate, $repayment_term, $total_return, $monthly_dividend, $next_payment_date, $equity_pct ]); // 2. Update funding_rounds raised amount $stmt = db()->prepare("UPDATE funding_rounds SET funding_raised = funding_raised + ? WHERE id = ?"); $stmt->execute([$amount, $startup['round_id']]); // 3. Update startup total raised $stmt = db()->prepare("UPDATE startups SET funding_raised = funding_raised + ? WHERE id = ?"); $stmt->execute([$amount, $startupId]); // 4. TRANSFER MONEY: Investor pot -> Founder pot // Deduct from investor $stmt = db()->prepare("UPDATE users SET balance = balance - ? WHERE id = ?"); $stmt->execute([$amount, $investor_id]); // Add to founder $stmt = db()->prepare("UPDATE users SET balance = balance + ? WHERE id = ?"); $stmt->execute([$amount, $startup['founder_id']]); db()->commit(); $success = "Investment of £" . number_format($amount, 2) . " confirmed successfully! Repayment schedule: £" . number_format($monthly_dividend, 2) . "/month for $repayment_term months."; header("refresh:3;url=portfolio.php"); } catch (Exception $e) { db()->rollBack(); $error = "Error: " . $e->getMessage(); } } } $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby'; ?>
You are participating in the active funding round for this startup.