49 lines
1.9 KiB
PHP
49 lines
1.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../includes/db.php';
|
|
require_once '../includes/api_helper.php';
|
|
require_once '../includes/logger.php';
|
|
require_once '../includes/notifier.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
jsonResponse(false, 'Unauthorized', [], 401);
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
jsonResponse(false, 'Invalid request method');
|
|
}
|
|
|
|
$id = $_POST['id'] ?? '';
|
|
|
|
if (empty($id)) {
|
|
jsonResponse(false, 'ไม่พบข้อมูลที่ต้องการลบ');
|
|
}
|
|
|
|
try {
|
|
// ตรวจสอบว่ามีการจองผูกอยู่หรือไม่
|
|
$stmt = $pdo->prepare("SELECT id FROM schedules WHERE driver_id = ? LIMIT 1");
|
|
$stmt->execute([$id]);
|
|
if ($stmt->fetch()) {
|
|
jsonResponse(false, 'ไม่สามารถลบได้ เนื่องจากพนักงานขับรถคนนี้มีคิวการจองอยู่ในระบบ');
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT first_name, last_name FROM drivers WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$driverInfo = $stmt->fetch();
|
|
$driverName = $driverInfo ? trim($driverInfo['first_name'] . ' ' . $driverInfo['last_name']) : $id;
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM drivers WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
logActivity("Delete Driver ({$driverName})", 'drivers', $id);
|
|
notifyEvent('driver', "👤 แจ้งเตือนพนักงานขับรถ\nลบข้อมูลพนักงานออกจากระบบ: {$driverName}");
|
|
jsonResponse(true, 'ลบข้อมูลสำเร็จ');
|
|
} else {
|
|
jsonResponse(false, 'ไม่พบข้อมูลในฐานข้อมูล');
|
|
}
|
|
} catch (PDOException $e) {
|
|
jsonResponse(false, 'Database Error: ' . $e->getMessage());
|
|
}
|
|
?>
|