query("SELECT industry_name, industry_code FROM industry_codes ORDER BY industry_name"); $industries = $industriesStmt->fetchAll(); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_project'])) { $project_name = trim($_POST['project_name'] ?? ''); $client_reference = trim($_POST['client_reference'] ?? ''); $industry = $_POST['industry'] ?? ''; $deadline_date = $_POST['deadline_date'] ?? ''; $deadline_time = $_POST['deadline_time'] ?? ''; $eloi = intval($_POST['eloi'] ?? 0); $sample_size = intval($_POST['sample_size'] ?? 0); $description = trim($_POST['description'] ?? ''); // Validation if (empty($project_name) || empty($industry) || empty($deadline_date) || empty($deadline_time)) { $error = 'Please fill in all required fields.'; } elseif ($eloi <= 0 || $sample_size <= 0) { $error = 'eLOI and Sample Size must be greater than 0.'; } elseif (strlen($client_reference) > 16) { $error = 'Client reference must be 16 characters or less.'; } else { try { // Get industry code $indStmt = $pdo->prepare("SELECT industry_code FROM industry_codes WHERE industry_name = ?"); $indStmt->execute([$industry]); $industry_code = $indStmt->fetchColumn(); if (!$industry_code) { $industry_code = '99'; // Default to "Other" } // Generate Project ID: RR + Industry(2) + DDMMYY + Sequence(4) $date_part = date('dmy'); // DDMMYY format // Get today's sequence number $today_start = date('Y-m-d 00:00:00'); $today_end = date('Y-m-d 23:59:59'); $seqStmt = $pdo->prepare(" SELECT COUNT(*) as count FROM projects WHERE created_at BETWEEN ? AND ? "); $seqStmt->execute([$today_start, $today_end]); $sequence = $seqStmt->fetchColumn() + 1; $sequence_part = str_pad($sequence, 4, '0', STR_PAD_LEFT); $project_id = 'RR' . $industry_code . $date_part . $sequence_part; // Combine deadline date and time $deadline = $deadline_date . ' ' . $deadline_time . ':00'; // Insert project $stmt = $pdo->prepare(" INSERT INTO projects (project_id, project_name, client_reference, client_id, industry, deadline, eloi, sample_size, description, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'Created') "); $stmt->execute([ $project_id, $project_name, $client_reference, $_SESSION['client_id'], $industry, $deadline, $eloi, $sample_size, $description ]); $new_project_id = $pdo->lastInsertId(); // Log activity $logStmt = $pdo->prepare(" INSERT INTO project_activity_log (project_id, client_id, action, details, ip_address) VALUES (?, ?, 'created', ?, ?) "); $logStmt->execute([ $new_project_id, $_SESSION['client_id'], "Created project: $project_name", $_SERVER['REMOTE_ADDR'] ?? 'unknown' ]); $success = "Project created successfully! Project ID: $project_id"; // Clear form $_POST = []; } catch (Exception $e) { error_log("Create project error: " . $e->getMessage()); $error = 'An error occurred while creating the project. Please try again.'; } } } include 'client-portal-header.php'; ?>
Fill in the details to create a new survey project