prepare("SELECT * FROM startups WHERE id = ? AND founder_id = ?"); $stmt->execute([$startup_id, $_SESSION['user_id']]); $existingStartup = $stmt->fetch(); if (!$existingStartup) { header('Location: dashboard.php'); exit; } } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $legal_name = $_POST['legal_name'] ?? ''; $country = $_POST['country'] ?? ''; $industry = $_POST['industry'] ?? ''; $sub_industry = $_POST['sub_industry'] ?? ''; $business_model = $_POST['business_model'] ?? ''; $product_service = $_POST['product_service'] ?? ''; $operational_stage = $_POST['operational_stage'] ?? ''; // Co-founder matching fields $cofounder_equity_pct = $_POST['cofounder_equity_pct'] ?? ''; $cofounder_equity_type = $_POST['cofounder_equity_type'] ?? ''; $cofounder_responsibilities = $_POST['cofounder_responsibilities'] ?? ''; $desired_cofounder_experience = $_POST['desired_cofounder_experience'] ?? ''; $cofounder_commitment = $_POST['cofounder_commitment'] ?? ''; $other_partnership_details = $_POST['other_partnership_details'] ?? ''; // Financial stats $current_cash_balance = (float)($_POST['current_cash_balance'] ?? 0); $burn_rate = (float)($_POST['burn_rate'] ?? 0); $outstanding_debt = $_POST['outstanding_debt'] ?? ''; $accounts_receivable_payable = $_POST['accounts_receivable_payable'] ?? ''; $upload_dir = 'assets/docs/financials/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0775, true); } $doc_fields = [ 'doc_income_statements', 'doc_balance_sheets', 'doc_cash_flow_statements', 'doc_revenue_breakdown', 'doc_gross_margin' ]; $uploaded_paths = []; foreach ($doc_fields as $field) { if (isset($_FILES[$field]) && $_FILES[$field]['error'] === UPLOAD_ERR_OK) { $file_ext = pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION); $file_name = uniqid($field . '_', true) . '.' . $file_ext; $dest_path = $upload_dir . $file_name; if (move_uploaded_file($_FILES[$field]['tmp_name'], $dest_path)) { $uploaded_paths[$field] = $dest_path; } else { $error = "Failed to upload $field."; break; } } elseif ($existingStartup && !empty($existingStartup[$field])) { $uploaded_paths[$field] = $existingStartup[$field]; } else { $error = "The financial document for $field is mandatory."; break; } } if (!$error) { if (empty($name) || empty($legal_name) || empty($country) || empty($industry) || empty($business_model) || empty($product_service) || empty($operational_stage)) { $error = "Please fill in all mandatory company information fields."; } } if (!$error) { db()->beginTransaction(); try { // Compute AI Recommended Return Rate $recommended_return_rate = $existingStartup['recommended_return_rate'] ?? 0.0; // AI Prompt $prompt = "As a financial analyst, calculate a recommended annual dividend yield (interest percentage) based on this startup profile: Name: {$name} Industry: {$industry}/{$sub_industry} Business Model: {$business_model} Product: {$product_service} Stage: {$operational_stage} Cash Balance: £{$current_cash_balance} Burn Rate: £{$burn_rate} Respond ONLY with a JSON object: {\"recommended_rate\": X.X}"; $aiResponse = LocalAIApi::createResponse([ 'input' => [ ['role' => 'system', 'content' => 'You are a financial analyst. Return JSON only.'], ['role' => 'user', 'content' => $prompt], ], ]); if (!empty($aiResponse['success'])) { $decoded = LocalAIApi::decodeJsonFromResponse($aiResponse); $recommended_return_rate = (float)($decoded['recommended_rate'] ?? 5.0); } if ($existingStartup) { $stmt = db()->prepare("UPDATE startups SET name = ?, legal_name = ?, country = ?, industry = ?, sub_industry = ?, business_model = ?, product_service = ?, operational_stage = ?, cofounder_equity_pct = ?, cofounder_equity_type = ?, cofounder_responsibilities = ?, desired_cofounder_experience = ?, cofounder_commitment = ?, other_partnership_details = ?, current_cash_balance = ?, outstanding_debt = ?, accounts_receivable_payable = ?, burn_rate = ?, doc_income_statements = ?, doc_balance_sheets = ?, doc_cash_flow_statements = ?, doc_revenue_breakdown = ?, doc_gross_margin = ?, recommended_return_rate = ? WHERE id = ? AND founder_id = ?"); $stmt->execute([ $name, $legal_name, $country, $industry, $sub_industry, $business_model, $product_service, $operational_stage, $cofounder_equity_pct, $cofounder_equity_type, $cofounder_responsibilities, $desired_cofounder_experience, $cofounder_commitment, $other_partnership_details, $current_cash_balance, $outstanding_debt, $accounts_receivable_payable, $burn_rate, $uploaded_paths['doc_income_statements'], $uploaded_paths['doc_balance_sheets'], $uploaded_paths['doc_cash_flow_statements'], $uploaded_paths['doc_revenue_breakdown'], $uploaded_paths['doc_gross_margin'], $recommended_return_rate, $existingStartup['id'], $_SESSION['user_id'] ]); $final_id = $existingStartup['id']; } else { $stmt = db()->prepare("INSERT INTO startups ( name, legal_name, country, industry, sub_industry, business_model, product_service, operational_stage, cofounder_equity_pct, cofounder_equity_type, cofounder_responsibilities, desired_cofounder_experience, cofounder_commitment, other_partnership_details, current_cash_balance, outstanding_debt, accounts_receivable_payable, burn_rate, doc_income_statements, doc_balance_sheets, doc_cash_flow_statements, doc_revenue_breakdown, doc_gross_margin, founder_id, recommended_return_rate, status ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'private')"); $stmt->execute([ $name, $legal_name, $country, $industry, $sub_industry, $business_model, $product_service, $operational_stage, $cofounder_equity_pct, $cofounder_equity_type, $cofounder_responsibilities, $desired_cofounder_experience, $cofounder_commitment, $other_partnership_details, $current_cash_balance, $outstanding_debt, $accounts_receivable_payable, $burn_rate, $uploaded_paths['doc_income_statements'], $uploaded_paths['doc_balance_sheets'], $uploaded_paths['doc_cash_flow_statements'], $uploaded_paths['doc_revenue_breakdown'], $uploaded_paths['doc_gross_margin'], $_SESSION['user_id'], $recommended_return_rate ]); $final_id = db()->lastInsertId(); } db()->commit(); $success = "Startup profile saved successfully! Recommended return rate: " . number_format($recommended_return_rate, 2) . "%"; // Refresh local data $stmt = db()->prepare("SELECT * FROM startups WHERE id = ?"); $stmt->execute([$final_id]); $existingStartup = $stmt->fetch(); } catch (Exception $e) { db()->rollBack(); $error = "Database error: " . $e->getMessage(); } } } ?>