0) { $stmt = db()->prepare("SELECT * FROM startups WHERE id = ? AND founder_id = ?"); $stmt->execute([$startup_id, $_SESSION['user_id']]); $existingStartup = $stmt->fetch(); if (!$existingStartup) { header("Location: startups.php"); exit; } // Check if there is already an active round $stmt = db()->prepare("SELECT id FROM funding_rounds WHERE startup_id = ? AND status = 'Active'"); $stmt->execute([$startup_id]); if ($stmt->fetch()) { header("Location: startup_details.php?id=" . $startup_id); exit; } } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $industry = trim($_POST['industry'] ?? ''); $equity_structure = trim($_POST['equity_structure'] ?? ''); $target = (float)($_POST['funding_target'] ?? 0); $status = $_POST['status'] ?? 'public'; $founder_return_rate = isset($_POST['founder_return_rate']) && $_POST['founder_return_rate'] !== '' ? (float)$_POST['founder_return_rate'] : null; // Financial Document Upload $financial_doc_path = ''; if (isset($_FILES['financial_doc']) && $_FILES['financial_doc']['error'] === UPLOAD_ERR_OK) { $upload_dir = 'assets/docs/financials/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0775, true); } $file_ext = pathinfo($_FILES['financial_doc']['name'], PATHINFO_EXTENSION); $file_name = uniqid('fin_', true) . '.' . $file_ext; $dest_path = $upload_dir . $file_name; if (move_uploaded_file($_FILES['financial_doc']['tmp_name'], $dest_path)) { $financial_doc_path = $dest_path; } else { $error = "Failed to upload financial documentation."; } } elseif (!$existingStartup) { $error = "Financial documentation is mandatory for new startups."; } if (!$error) { if ((!$existingStartup && (empty($name) || empty($description) || empty($industry) || empty($equity_structure) || empty($financial_doc_path))) || $target < 50) { $error = "Please fill in all required fields and upload financial documents. Minimum target is £50."; } else { // AI Calculation for Recommended Return Rate $recommended_return_rate = 5.0; // Default fallback $prompt = "As a financial analyst for a startup investment platform, calculate a recommended annual dividend yield (interest percentage) for the following startup. Startup Name: {$name} Industry: {$industry} Description: {$description} Funding Target: £{$target} Equity Structure: {$equity_structure} Respond ONLY with a JSON object containing a single key 'recommended_rate' with a numeric value (percentage). Example: {\"recommended_rate\": 8.5}"; $aiResponse = LocalAIApi::createResponse([ 'input' => [['role' => 'system', 'content' => 'You are a professional financial analyst. Return JSON only.'], ['role' => 'user', 'content' => $prompt], ], ]); if (!empty($aiResponse['success'])) { $decoded = LocalAIApi::decodeJsonFromResponse($aiResponse); if (isset($decoded['recommended_rate'])) { $recommended_return_rate = (float)$decoded['recommended_rate']; } } db()->beginTransaction(); try { if ($existingStartup) { // Update existing startup with new return rates if provided $stmt = db()->prepare("UPDATE startups SET recommended_return_rate = ?, founder_return_rate = ? WHERE id = ?"); $stmt->execute([$recommended_return_rate, $founder_return_rate, $existingStartup['id']]); // Create a new round $stmt = db()->prepare("INSERT INTO funding_rounds (startup_id, funding_goal, status) VALUES (?, ?, 'Active')"); $stmt->execute([$existingStartup['id'], $target]); $final_id = $existingStartup['id']; } else { // 1. Insert startup with new fields $stmt = db()->prepare("INSERT INTO startups (name, description, industry, equity_structure, financial_doc_path, founder_id, funding_target, status, recommended_return_rate, founder_return_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $description, $industry, $equity_structure, $financial_doc_path, $_SESSION['user_id'], $target, $status, $recommended_return_rate, $founder_return_rate]); $new_startup_id = db()->lastInsertId(); // 2. Insert initial funding round $stmt = db()->prepare("INSERT INTO funding_rounds (startup_id, funding_goal, status) VALUES (?, ?, 'Active')"); $stmt->execute([$new_startup_id, $target]); $final_id = $new_startup_id; } db()->commit(); $success = "Startup successfully listed with a recommended return rate of " . number_format($recommended_return_rate, 2) . "%!"; header("refresh:3;url=startup_details.php?id=" . $final_id); } catch (PDOException $e) { db()->rollBack(); $error = "Database error: " . $e->getMessage(); } } } } $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby'; ?>
= $existingStartup ? 'Update your funding goal and return rates.' : 'Complete the form below to list your startup and receive a platform-calculated return rate recommendation.' ?>