35583-vm/edit_work_order.php
Flatlogic Bot 58e3bb7010 1.2
2025-11-09 03:37:51 +00:00

170 lines
8.5 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'includes/functions.php';
require_once 'mail/MailService.php';
start_secure_session();
require_login();
$work_order = null;
$error_message = '';
$success_message = '';
$technicians = get_users_by_role('technician');
if (isset($_GET['id'])) {
$work_order = get_work_order_by_id($_GET['id']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$customer_name = $_POST['customer_name'];
$customer_email = $_POST['customer_email'];
$job_type = $_POST['job_type'];
$status = $_POST['status'];
$address = $_POST['address'];
$technician_id = $_POST['technician_id'] ?? null;
$original_work_order = get_work_order_by_id($id);
if (update_work_order($id, $customer_name, $customer_email, $job_type, $status, $address, $technician_id)) {
if ($status === 'Completed' && !empty($customer_email)) {
$subject = "Work Order Completed";
$body = "<p>Dear {$customer_name},</p><p>Your work order for <strong>{$job_type}</strong> has been completed.</p><p>Thank you for your business!</p>";
MailService::sendMail($customer_email, $subject, $body);
}
if ($technician_id != $original_work_order['assigned_technician_id']) {
$technician_user = get_user_by_id($technician_id);
if ($technician_user && !empty($technician_user['email'])) {
$subject = "You have been assigned a new work order";
$body = "<p>Hello {$technician_user['username']},</p><p>You have been assigned to a new work order #{$id}.</p><p><a href=\"http://{\$_SERVER['HTTP_HOST']}/work_order_details.php?id={$id}\">Click here to view the details.</a></p>";
MailService::sendMail($technician_user['email'], $subject, $body);
}
}
header("Location: work_order_details.php?id=" . $id . "&success=1");
exit;
} else {
$error_message = "Failed to update work order.";
}
}
if (!$work_order) {
header("Location: index.php?error=notfound");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Work Order #<?php echo htmlspecialchars($work_order['id']); ?> - Flatlogic</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
</head>
<body class="d-flex">
<div class="sidebar d-flex flex-column p-3">
<a href="index.php" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<i class="bi bi-buildings me-2"></i>
<span class="fs-4">Flatlogic</span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="index.php" class="nav-link text-white" aria-current="page">
<i class="bi bi-speedometer2 me-2"></i>
Dashboard
</a>
</li>
<li class="nav-item">
<a href="reports.php" class="nav-link text-white">
<i class="bi bi-bar-chart-line me-2"></i>
Reports
</a>
</li>
<li class="nav-item">
<a href="inventory.php" class="nav-link text-white">
<i class="bi bi-box-seam me-2"></i>
Inventory
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle me-2"></i>
<strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong>
</a>
<ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
<li><a class="dropdown-item" href="logout.php">Sign out</a></li>
</ul>
</div>
</div>
<main class="main-content flex-grow-1 p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0">Edit Work Order #<?php echo htmlspecialchars($work_order['id']); ?></h1>
<a href="work_order_details.php?id=<?php echo htmlspecialchars($work_order['id']); ?>" class="btn btn-secondary">
<i class="bi bi-arrow-left me-1"></i>
Back to Details
</a>
</div>
<div class="card">
<div class="card-body">
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form action="edit_work_order.php?id=<?php echo htmlspecialchars($work_order['id']); ?>" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($work_order['id']); ?>">
<div class="mb-3">
<label for="customer_name" class="form-label">Customer Name</label>
<input type="text" class="form-control" id="customer_name" name="customer_name" value="<?php echo htmlspecialchars($work_order['customer_name']); ?>" required>
</div>
<div class="mb-3">
<label for="customer_email" class="form-label">Customer Email</label>
<input type="email" class="form-control" id="customer_email" name="customer_email" value="<?php echo htmlspecialchars($work_order['customer_email']); ?>">
</div>
<div class="mb-3">
<label for="job_type" class="form-label">Job Type</label>
<input type="text" class="form-control" id="job_type" name="job_type" value="<?php echo htmlspecialchars($work_order['job_type']); ?>" required>
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status" required>
<option value="Pending Dispatch" <?php echo ($work_order['status'] == 'Pending Dispatch') ? 'selected' : ''; ?>>Pending Dispatch</option>
<option value="In Progress" <?php echo ($work_order['status'] == 'In Progress') ? 'selected' : ''; ?>>In Progress</option>
<option value="Completed" <?php echo ($work_order['status'] == 'Completed') ? 'selected' : ''; ?>>Completed</option>
<option value="On Hold" <?php echo ($work_order['status'] == 'On Hold') ? 'selected' : ''; ?>>On Hold</option>
<option value="Cancelled" <?php echo ($work_order['status'] == 'Cancelled') ? 'selected' : ''; ?>>Cancelled</option>
</select>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" id="address" name="address" rows="3" required><?php echo htmlspecialchars($work_order['address']); ?></textarea>
</div>
<div class="mb-3">
<label for="technician_id" class="form-label">Assigned Technician</label>
<select class="form-select" id="technician_id" name="technician_id">
<option value="">Unassigned</option>
<?php foreach ($technicians as $technician):
$selected = ($work_order['assigned_technician_id'] == $technician['id']) ? 'selected' : '';
echo "<option value=\"{$technician['id']}\" {$selected}>" . htmlspecialchars($technician['username']) . "</option>";
endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>