181 lines
9.1 KiB
PHP
181 lines
9.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'investor') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$startupId = $_GET['id'] ?? null;
|
|
if (!$startupId) {
|
|
header('Location: startups.php');
|
|
exit;
|
|
}
|
|
|
|
$stmt = db()->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';
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Invest in <?= htmlspecialchars($startup['name']) ?> | <?= htmlspecialchars($platformName) ?></title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
|
</head>
|
|
<body style="background: #000; color: #fff; padding: 60px 20px;">
|
|
|
|
<div class="container" style="max-width: 600px; margin: 0 auto;">
|
|
<div class="card" style="padding: 40px; background: #1a1a1a; border-radius: 24px; border: 1px solid rgba(255,255,255,0.1);">
|
|
<h1 style="font-size: 32px; font-weight: 900; margin-bottom: 10px;">Back <?= htmlspecialchars($startup['name']) ?></h1>
|
|
<p style="color: #999; margin-bottom: 30px;">
|
|
You are participating in the active funding round for this startup.
|
|
</p>
|
|
|
|
<!-- Wallet Info -->
|
|
<div style="background: rgba(0, 242, 255, 0.05); padding: 15px; border-radius: 12px; border: 1px solid rgba(0, 242, 255, 0.2); margin-bottom: 25px; display: flex; align-items: center; justify-content: space-between;">
|
|
<div>
|
|
<span style="font-size: 12px; color: #999; text-transform: uppercase;">Your Wallet Balance</span>
|
|
<div style="font-size: 18px; font-weight: 900; color: var(--accent-blue);">£<?= number_format($investor_balance, 2) ?></div>
|
|
</div>
|
|
<a href="dashboard.php" style="font-size: 12px; color: var(--accent-blue); text-decoration: none; font-weight: 600;">Add Funds <i class="fas fa-plus"></i></a>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div style="background: rgba(255, 0, 0, 0.1); border: 1px solid rgba(255, 0, 0, 0.3); color: #ff5555; padding: 15px; border-radius: 12px; margin-bottom: 25px;">
|
|
<?= htmlspecialchars($error) ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div style="background: rgba(0, 242, 255, 0.1); border: 1px solid var(--accent-blue); color: var(--accent-blue); padding: 15px; border-radius: 12px; margin-bottom: 25px;">
|
|
<?= htmlspecialchars($success) ?>
|
|
</div>
|
|
<?php else: ?>
|
|
<div style="background: rgba(255,255,255,0.03); padding: 25px; border-radius: 16px; border: 1px solid rgba(255,255,255,0.05); margin-bottom: 30px;">
|
|
<div style="display: flex; justify-content: space-between; margin-bottom: 15px;">
|
|
<span style="color: #999;">Interest Rate</span>
|
|
<span style="font-weight: 700; color: #fff;"><?= number_format($startup['founder_return_rate'] ?? 0, 1) ?>%</span>
|
|
</div>
|
|
<div style="display: flex; justify-content: space-between; margin-bottom: 15px;">
|
|
<span style="color: #999;">Repayment Term</span>
|
|
<span style="font-weight: 700; color: #fff;"><?= htmlspecialchars($startup['repayment_term'] ?? 12) ?> Months</span>
|
|
</div>
|
|
<div style="display: flex; justify-content: space-between; padding-top: 15px; border-top: 1px solid rgba(255,255,255,0.05);">
|
|
<span style="color: #999;">Remaining Allocation</span>
|
|
<?php $remaining = $startup['funding_goal'] - $startup['funding_raised']; ?>
|
|
<span style="font-weight: 900; color: var(--accent-blue);">£<?= number_format(max(0, $remaining), 2) ?></span>
|
|
</div>
|
|
</div>
|
|
|
|
<form method="POST">
|
|
<div style="margin-bottom: 30px;">
|
|
<label style="display: block; margin-bottom: 10px; font-weight: 600; color: #999; text-transform: uppercase; font-size: 12px; letter-spacing: 1px;">Investment Amount (£)</label>
|
|
<input type="number" name="amount" min="50" step="0.01" required placeholder="Min £50..."
|
|
style="width: 100%; padding: 20px; background: #000; border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; color: #fff; font-size: 24px; font-weight: 900;">
|
|
<p style="margin-top: 10px; font-size: 13px; color: #777;">The minimum investment is £50. Terms are locked upon confirmation.</p>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary" style="width: 100%; padding: 20px; font-size: 18px; font-weight: 800; border-radius: 16px; background: var(--accent-blue); color: #000; border: none; cursor: pointer;">
|
|
Confirm Investment <i class="fas fa-rocket" style="margin-left: 10px;"></i>
|
|
</button>
|
|
<a href="startup_details.php?id=<?= $startupId ?>" style="display: block; text-align: center; margin-top: 20px; color: #999; text-decoration: none; font-size: 14px;">Back to Startup Profile</a>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|