1 && !$companyId) { header('Location: index.php'); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($step == 1) { $companyName = $_POST['company_name'] ?? ''; $uprnRequired = isset($_POST['uprn_required']) ? 1 : 0; $statuses = $_POST['statuses'] ?? []; $folders = $_POST['folders'] ?? []; $defaultStatusIndex = (int)($_POST['default_status'] ?? 0); if (empty($companyName)) { $error = "Company name is required."; } elseif (empty(array_filter($statuses, 'trim'))) { $error = "At least one job status is required."; } elseif (empty(array_filter($folders, 'trim'))) { $error = "At least one required folder is required."; } else { try { db()->beginTransaction(); // 1. Create Company $stmt = db()->prepare("INSERT INTO companies (name, uprn_required) VALUES (?, ?)"); $stmt->execute([$companyName, $uprnRequired]); $companyId = db()->lastInsertId(); $_SESSION['company_id'] = $companyId; // Store company ID in session for next steps // 2. Insert Statuses $stmt = db()->prepare("INSERT INTO job_statuses (company_id, name, is_default, sort_order) VALUES (?, ?, ?, ?)"); foreach ($statuses as $index => $statusName) { $statusName = trim($statusName); if ($statusName === '') continue; $isDefault = ($index === $defaultStatusIndex) ? 1 : 0; $stmt->execute([$companyId, $statusName, $isDefault, $index]); } // 3. Insert Folders $stmt = db()->prepare("INSERT INTO required_folders (company_id, name) VALUES (?, ?)"); foreach ($folders as $folderName) { $folderName = trim($folderName); if ($folderName === '') continue; $stmt->execute([$companyId, $folderName]); } // 4. Create first Admin user (simplified for demo) $adminEmail = 'admin@' . strtolower(str_replace(' ', '', $companyName)) . '.com'; $stmt = db()->prepare("INSERT INTO users (company_id, name, email, password, role) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$companyId, 'Admin User', $adminEmail, password_hash('password123', PASSWORD_DEFAULT), 'admin']); db()->commit(); header('Location: setup.php?step=2'); // Redirect to next step exit; } catch (Exception $e) { db()->rollBack(); $error = "Database error: " . $e->getMessage(); } } } elseif ($step == 2) { // Step 2: Client Setup $clientNames = $_POST['client_names'] ?? []; $contactPeople = $_POST['contact_people'] ?? []; $clientEmails = $_POST['client_emails'] ?? []; $clientPhones = $_POST['client_phones'] ?? []; if (empty(array_filter($clientNames, 'trim'))) { $error = "At least one client is required."; } else { try { db()->beginTransaction(); $stmt = db()->prepare("INSERT INTO clients (company_id, name, contact_person, email, phone) VALUES (?, ?, ?, ?, ?)"); foreach ($clientNames as $index => $clientName) { $clientName = trim($clientName); if ($clientName === '') continue; $contactPerson = trim($contactPeople[$index] ?? ''); $clientEmail = trim($clientEmails[$index] ?? ''); $clientPhone = trim($clientPhones[$index] ?? ''); $stmt->execute([$companyId, $clientName, $contactPerson, $clientEmail, $clientPhone]); } db()->commit(); session_destroy(); // Clear session after successful setup $success = "Company setup successfully! You can now log in."; header('Refresh: 2; URL=index.php'); // Redirect to index exit; } catch (Exception $e) { db()->rollBack(); $error = "Database error: " . $e->getMessage(); } } } } $pageTitle = "Company Onboarding"; ?> <?= $pageTitle ?> - RepairsPro

Company Onboarding - Step

If enabled, every job must have a unique UPRN.

Define the workflow stages for your jobs.


These folders will appear on every job automatically.

Add your initial clients. You can add more later.