prepare("SELECT * FROM startups WHERE founder_id = ?"); $stmt->execute([$_SESSION['user_id']]); $existingStartup = $stmt->fetch(); 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'] ?? ''; $cofounder_equity_pct = $_POST['cofounder_equity_pct'] ?? 0.0; $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'] ?? ''; $current_cash_balance = $_POST['current_cash_balance'] ?? 0.0; $outstanding_debt = $_POST['outstanding_debt'] ?? ''; $accounts_receivable_payable = $_POST['accounts_receivable_payable'] ?? ''; $burn_rate = $_POST['burn_rate'] ?? 0.0; $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) { $uploaded_paths[$field] = $existingStartup[$field] ?? null; if (isset($_FILES[$field]) && $_FILES[$field]['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION); $new_name = $field . '_' . time() . '.' . $ext; $upload_dir = 'assets/docs/financials/'; if (!is_dir($upload_dir)) mkdir($upload_dir, 0775, true); $upload_path = $upload_dir . $new_name; if (move_uploaded_file($_FILES[$field]['tmp_name'], $upload_path)) { $uploaded_paths[$field] = $upload_path; } } } 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; $recommended_return_reasoning = $existingStartup['recommended_return_reasoning'] ?? ''; $doc_list = []; foreach ($uploaded_paths as $key => $path) { if ($path) { $label = str_replace(['doc_', '_'], ['', ' '], $key); $doc_list[] = ucwords($label); } } $docs_str = !empty($doc_list) ? implode(", ", $doc_list) : "None"; // 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} Documents provided: {$docs_str} Based on the financial documents provided and the company's stage/industry, please provide: 1. A recommended return rate (percentage). 2. A brief reasoning (2-3 sentences) on how you arrived at this figure, explicitly mentioning how the provided documents influenced your decision. Respond ONLY with a JSON object: {\"recommended_rate\": X.X, \"reasoning\": \"...\"}"; $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); $recommended_return_reasoning = (string)($decoded['reasoning'] ?? 'Based on industry standards and provided financials.'); } 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 = ?, recommended_return_reasoning = ? 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, $recommended_return_reasoning, $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, recommended_return_reasoning, 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, $recommended_return_reasoning ]); $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(); } } } ?>