Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,128 @@
<?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'] ?? '';
$license_plate = trim($_POST['license_plate'] ?? '');
$type = trim($_POST['type'] ?? '');
$brand = trim($_POST['brand'] ?? '');
$model = trim($_POST['model'] ?? '');
$status = $_POST['status'] ?? 'available';
$tax_expiry = empty($_POST['tax_expiry']) ? null : $_POST['tax_expiry'];
// New fields
$chassis_number = trim($_POST['chassis_number'] ?? '');
$engine_number = trim($_POST['engine_number'] ?? '');
$engine_cc = empty($_POST['engine_cc']) ? null : (int)$_POST['engine_cc'];
$fuel_capacity = trim($_POST['fuel_capacity'] ?? '');
$fuel_type = trim($_POST['fuel_type'] ?? '');
$color = trim($_POST['color'] ?? '#FFFFFF');
$asset_number = trim($_POST['asset_number'] ?? '');
$vehicle_style = trim($_POST['vehicle_style'] ?? '');
$model_year = trim($_POST['model_year'] ?? '');
if (empty($license_plate) || empty($type)) {
jsonResponse(false, 'กรุณากรอกข้อมูลที่จำเป็นให้ครบถ้วน');
}
// สร้างคอลัมน์ image_path อัตโนมัติหากยังไม่มี
try {
$pdo->exec("ALTER TABLE vehicles ADD COLUMN image_path VARCHAR(255) NULL");
} catch (PDOException $e) {
// Column might already exist
}
// อัปเดต ENUM สำหรับ status ให้รองรับ disposed
try {
$pdo->exec("ALTER TABLE vehicles MODIFY COLUMN status ENUM('available', 'in_use', 'maintenance', 'disposed') DEFAULT 'available'");
} catch (PDOException $e) {
// ENUM might already be updated
}
// อัปโหลดรูปภาพใหม่
$image_path = null;
$upload_dir = '../uploads/vehicles/';
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
$file_ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
$allowed_ext = ['jpg', 'jpeg', 'png', 'webp'];
if (in_array($file_ext, $allowed_ext)) {
// ลบรูปเก่าถ้ามีการอัปโหลดใหม่ (กรณีแก้ไข)
if (!empty($id)) {
$stmt = $pdo->prepare("SELECT image_path FROM vehicles WHERE id = ?");
$stmt->execute([$id]);
$old_vehicle = $stmt->fetch();
if ($old_vehicle && !empty($old_vehicle['image_path'])) {
$old_path = '../' . $old_vehicle['image_path'];
if (file_exists($old_path)) unlink($old_path);
}
}
$new_filename = 'veh_' . time() . '_' . rand(1000, 9999) . '.' . $file_ext;
$dest = $upload_dir . $new_filename;
if (move_uploaded_file($_FILES['image']['tmp_name'], $dest)) {
$image_path = 'uploads/vehicles/' . $new_filename;
}
}
}
try {
if (empty($id)) {
// สร้างใหม่
// เช็คทะเบียนซ้ำ
$stmt = $pdo->prepare("SELECT id FROM vehicles WHERE license_plate = ?");
$stmt->execute([$license_plate]);
if ($stmt->fetch()) {
jsonResponse(false, 'ทะเบียนรถนี้มีในระบบแล้ว');
}
$stmt = $pdo->prepare("INSERT INTO vehicles (license_plate, type, brand, model, status, tax_expiry, chassis_number, engine_number, engine_cc, fuel_capacity, fuel_type, color, asset_number, vehicle_style, model_year, image_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$license_plate, $type, $brand, $model, $status, $tax_expiry, $chassis_number, $engine_number, $engine_cc, $fuel_capacity, $fuel_type, $color, $asset_number, $vehicle_style, $model_year, $image_path]);
$newId = $pdo->lastInsertId();
logActivity("Create Vehicle ({$license_plate})", 'vehicles', $newId);
notifyEvent('vehicle', "🚗 แจ้งเตือนยานพาหนะ\nมีรถใหม่เพิ่มเข้าระบบ ทะเบียน: {$license_plate} ({$brand} {$model})");
jsonResponse(true, 'เพิ่มรถใหม่สำเร็จ');
} else {
// แก้ไข
$stmt = $pdo->prepare("SELECT id FROM vehicles WHERE license_plate = ? AND id != ?");
$stmt->execute([$license_plate, $id]);
if ($stmt->fetch()) {
jsonResponse(false, 'ทะเบียนรถนี้มีในระบบแล้ว (ซ้ำกับคันอื่น)');
}
if ($image_path) {
$stmt = $pdo->prepare("UPDATE vehicles SET license_plate=?, type=?, brand=?, model=?, status=?, tax_expiry=?, chassis_number=?, engine_number=?, engine_cc=?, fuel_capacity=?, fuel_type=?, color=?, asset_number=?, vehicle_style=?, model_year=?, image_path=? WHERE id=?");
$stmt->execute([$license_plate, $type, $brand, $model, $status, $tax_expiry, $chassis_number, $engine_number, $engine_cc, $fuel_capacity, $fuel_type, $color, $asset_number, $vehicle_style, $model_year, $image_path, $id]);
} else {
$stmt = $pdo->prepare("UPDATE vehicles SET license_plate=?, type=?, brand=?, model=?, status=?, tax_expiry=?, chassis_number=?, engine_number=?, engine_cc=?, fuel_capacity=?, fuel_type=?, color=?, asset_number=?, vehicle_style=?, model_year=? WHERE id=?");
$stmt->execute([$license_plate, $type, $brand, $model, $status, $tax_expiry, $chassis_number, $engine_number, $engine_cc, $fuel_capacity, $fuel_type, $color, $asset_number, $vehicle_style, $model_year, $id]);
}
logActivity("Update Vehicle ({$license_plate})", 'vehicles', $id);
notifyEvent('vehicle', "🚗 แจ้งเตือนยานพาหนะ\nมีการอัปเดตข้อมูลรถ ทะเบียน: {$license_plate}");
jsonResponse(true, 'แก้ไขข้อมูลรถสำเร็จ');
}
} catch (PDOException $e) {
jsonResponse(false, 'Database Error: ' . $e->getMessage());
}
?>