127 lines
5.6 KiB
PHP
127 lines
5.6 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$msp_id = $_SESSION['msp_id'];
|
|
|
|
// Fetch available templates
|
|
$stmt = db()->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';
|
|
?>
|
|
|
|
<div class="container py-5">
|
|
<div class="header-section mb-5">
|
|
<h1>Add New Customer</h1>
|
|
<p class="text-muted">Register a new client and start the onboarding process.</p>
|
|
</div>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card shadow-sm border-0">
|
|
<div class="card-body p-4 p-md-5">
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success d-flex align-items-center mb-4">
|
|
<i class="bi bi-check-circle-fill me-2"></i>
|
|
<div><?php echo $success; ?> <a href="customers.php" class="alert-link">View customer list.</a></div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger d-flex align-items-center mb-4">
|
|
<i class="bi bi-exclamation-triangle-fill me-2"></i>
|
|
<div><?php echo $error; ?></div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="">
|
|
<div class="row g-4">
|
|
<div class="col-md-6">
|
|
<label for="name" class="form-label fw-bold">Customer Name</label>
|
|
<input type="text" name="name" id="name" class="form-control" placeholder="e.g. Acme Corporation" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label for="email" class="form-label fw-bold">Primary Contact Email</label>
|
|
<input type="email" name="email" id="email" class="form-control" placeholder="e.g. contact@acme.com" required>
|
|
</div>
|
|
<div class="col-12">
|
|
<label for="template_id" class="form-label fw-bold">Onboarding Template</label>
|
|
<select name="template_id" id="template_id" class="form-select">
|
|
<option value="">-- No template (use default tasks) --</option>
|
|
<?php foreach ($templates as $template): ?>
|
|
<option value="<?php echo $template['id']; ?>"><?php echo htmlspecialchars($template['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<div class="form-text mt-2 small text-muted">Choosing a template will automatically generate onboarding tasks based on that template.</div>
|
|
</div>
|
|
<div class="col-12 mt-5">
|
|
<div class="d-grid gap-2">
|
|
<button type="submit" class="btn btn-primary btn-lg fw-bold">
|
|
<i class="bi bi-plus-circle me-2"></i> Create & Start Onboarding
|
|
</button>
|
|
<a href="customers.php" class="btn btn-link text-muted">Cancel</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|