37762-vm/create-job.php
2026-01-23 20:42:30 +00:00

133 lines
5.8 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/auth.php';
requireLogin();
$user = getCurrentUser();
$companyId = $user['company_id'];
$error = '';
// Fetch clients for selection
$stmt = db()->prepare("SELECT id, name FROM clients WHERE company_id = ? AND is_active = 1 ORDER BY name ASC");
$stmt->execute([$companyId]);
$clients = $stmt->fetchAll();
// Fetch statuses for selection
$stmt = db()->prepare("SELECT id, name, is_default FROM job_statuses WHERE company_id = ? ORDER BY sort_order ASC");
$stmt->execute([$companyId]);
$statuses = $stmt->fetchAll();
// Fetch company settings
$stmt = db()->prepare("SELECT uprn_required FROM companies WHERE id = ?");
$stmt->execute([$companyId]);
$company = $stmt->fetch();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$clientId = $_POST['client_id'] ?? '';
$statusId = $_POST['status_id'] ?? '';
$uprn = $_POST['uprn'] ?? '';
$address = $_POST['address'] ?? '';
$description = $_POST['description'] ?? '';
if (empty($clientId) || empty($statusId) || empty($address)) {
$error = 'Please fill in all required fields.';
} elseif ($company['uprn_required'] && empty($uprn)) {
$error = 'UPRN is required for this company.';
} else {
try {
db()->beginTransaction();
// 1. Insert Job
$stmt = db()->prepare("
INSERT INTO jobs (company_id, client_id, status_id, uprn, address, description)
VALUES (?, ?, ?, ?, ?, ?)
");
$stmt->execute([$companyId, $clientId, $statusId, $uprn, $address, $description]);
$jobId = db()->lastInsertId();
// 2. Add Mandatory Folders
$stmt = db()->prepare("SELECT name FROM required_folders WHERE company_id = ?");
$stmt->execute([$companyId]);
$reqFolders = $stmt->fetchAll();
$folderStmt = db()->prepare("INSERT INTO job_folders (job_id, name, is_required) VALUES (?, ?, ?)");
foreach ($reqFolders as $rf) {
$folderStmt->execute([$jobId, $rf['name'], 1]);
}
// 3. Log activity
$logStmt = db()->prepare("INSERT INTO job_logs (job_id, user_id, action, details) VALUES (?, ?, ?, ?)");
$logStmt->execute([$jobId, $user['id'], 'created', 'Job created manually by user.']);
db()->commit();
header('Location: dashboard.php');
exit;
} catch (Exception $e) {
db()->rollBack();
$error = "Database error: " . $e->getMessage();
}
}
}
$pageTitle = "Create New Job";
require_once __DIR__ . '/includes/header.php';
?>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow-sm">
<div class="card-header bg-white py-3">
<h4 class="fw-bold m-0">Create New Job</h4>
</div>
<div class="card-body p-4">
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label fw-bold">Client <span class="text-danger">*</span></label>
<select name="client_id" class="form-select" required>
<option value="">Select Client...</option>
<?php foreach ($clients as $c): ?>
<option value="<?= $c['id'] ?>"><?= htmlspecialchars($c['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label fw-bold">Status <span class="text-danger">*</span></label>
<select name="status_id" class="form-select" required>
<?php foreach ($statuses as $s): ?>
<option value="<?= $s['id'] ?>" <?= $s['is_default'] ? 'selected' : '' ?>><?= htmlspecialchars($s['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12">
<label class="form-label fw-bold">UPRN <?= $company['uprn_required'] ? '<span class="text-danger">*</span>' : '' ?></label>
<input type="text" name="uprn" class="form-control" placeholder="Unique Property Reference Number" <?= $company['uprn_required'] ? 'required' : '' ?>>
</div>
<div class="col-12">
<label class="form-label fw-bold">Property Address <span class="text-danger">*</span></label>
<textarea name="address" class="form-control" rows="2" placeholder="Full property address" required></textarea>
</div>
<div class="col-12">
<label class="form-label fw-bold">Job Description</label>
<textarea name="description" class="form-control" rows="4" placeholder="Briefly describe the repair required..."></textarea>
</div>
</div>
<div class="d-flex justify-content-between mt-5">
<a href="dashboard.php" class="btn btn-outline-secondary">Cancel</a>
<button type="submit" class="btn btn-primary px-5">Create Job</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>