prepare("SELECT * FROM onboarding_templates WHERE msp_id = ? ORDER BY name ASC"); $stmt->execute([$msp_id]); $templates = $stmt->fetchAll(); $success = ''; $error = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; $template_id = $_POST['template_id'] ?: null; if ($name && $email) { try { db()->beginTransaction(); $stmt = db()->prepare("INSERT INTO customers (msp_id, template_id, name, contact_email, status) VALUES (?, ?, ?, ?, 'onboarding')"); $stmt->execute([$msp_id, $template_id, $name, $email]); $customer_id = db()->lastInsertId(); if ($template_id) { // Seed tasks from template steps $stmt = db()->prepare("SELECT title FROM onboarding_template_steps WHERE template_id = ? ORDER BY order_index ASC"); $stmt->execute([$template_id]); $steps = $stmt->fetchAll(); foreach ($steps as $step) { $stmt = db()->prepare("INSERT INTO onboarding_tasks (customer_id, task_name) VALUES (?, ?)"); $stmt->execute([$customer_id, "Complete Step: " . $step['title']]); } } else { // Default tasks if no template $default_tasks = ['Initial Call', 'Service Agreement signed', 'Access credentials received', 'Network audit completed']; foreach ($default_tasks as $task) { $stmt = db()->prepare("INSERT INTO onboarding_tasks (customer_id, task_name) VALUES (?, ?)"); $stmt->execute([$customer_id, $task]); } } db()->commit(); $success = "Customer added successfully!"; } catch (PDOException $e) { db()->rollBack(); $error = "Error adding customer: " . $e->getMessage(); } } else { $error = "Please fill in all required fields."; } } include 'header.php'; ?>
Register a new client and start the onboarding process.