| Server IP : 103.233.193.20 / Your IP : 216.73.216.169 Web Server : Apache/2 System : Linux host1.itclever.com 4.18.0-553.16.1.el8_10.x86_64 #1 SMP Thu Aug 8 17:47:08 UTC 2024 x86_64 User : oriscomadm ( 1120) PHP Version : 5.6.40 Disable Function : exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/oriscomadm/domains/oriscom.com/private_html/taxi_estimate/ |
Upload File : |
<?php
header('Content-Type: application/json; charset=utf-8');
include("db_connect.php");
/* ===============================
รับข้อมูล JSON
================================ */
$json = file_get_contents('php://input');
$data = json_decode($json, true);
error_log("📥 Received data: " . print_r($data, true));
if (!$data) {
echo json_encode([
'success' => false,
'message' => 'Invalid JSON data'
]);
exit;
}
/* ===============================
ตรวจสอบฟิลด์ที่จำเป็น
================================ */
$required_fields = [
'origin_name',
'origin_lat',
'origin_lon',
'destination_name',
'destination_lat',
'destination_lon'
];
$missing_fields = [];
foreach ($required_fields as $field) {
if (!isset($data[$field])) {
$missing_fields[] = $field;
}
}
if (!empty($missing_fields)) {
echo json_encode([
'success' => false,
'message' => 'Missing required fields: ' . implode(', ', $missing_fields)
]);
exit;
}
/* ===============================
fallback ค่า meter_id
================================ */
$data['meter_id'] = $data['meter_id'] ?? null;
/* ===============================
Generate trip_no อัตโนมัติ (ไม่ใช้ค่าที่ส่งมา)
================================ */
$trip_no = null;
if (!empty($data['meter_id'])) {
$sqlTrip = "
SELECT MAX(trip_no) AS last_trip
FROM oris_tb_taxi_estimate
WHERE meter_id = ?
";
$stmtTrip = $conn->prepare($sqlTrip);
if (!$stmtTrip) {
echo json_encode([
'success' => false,
'message' => "Prepare trip query failed: " . $conn->error
]);
exit;
}
$stmtTrip->bind_param("s", $data['meter_id']);
$stmtTrip->execute();
$resultTrip = $stmtTrip->get_result();
$rowTrip = $resultTrip->fetch_assoc();
$stmtTrip->close();
// คำนวณ trip_no ใหม่ทุกครั้ง
if (!empty($rowTrip['last_trip'])) {
$nextTrip = (int)$rowTrip['last_trip'] + 1;
} else {
$nextTrip = 1;
}
$trip_no = str_pad($nextTrip, 5, '0', STR_PAD_LEFT);
error_log("🎫 Generated trip_no: {$trip_no} for meter_id: {$data['meter_id']}");
}
try {
/* ===============================
SQL INSERT
================================ */
$sql = "INSERT INTO oris_tb_taxi_estimate (
timestamp,
origin_name,
origin_lat,
origin_lon,
destination_name,
destination_lat,
destination_lon,
main_type,
main_distance_km,
main_time,
main_fare,
no_tollway_type,
no_tollway_distance_km,
no_tollway_time,
no_tollway_fare,
route_used,
route_by,
meter_id,
trip_no
) VALUES (
NOW(),
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
throw new Exception("Prepare failed: " . $conn->error);
}
/* ===============================
แปลงเวลา
================================ */
$main_time = (!empty($data['main_time_seconds']) && $data['main_time_seconds'] > 0)
? gmdate("H:i:s", $data['main_time_seconds'])
: null;
$no_tollway_time = (!empty($data['no_tollway_time_seconds']) && $data['no_tollway_time_seconds'] > 0)
? gmdate("H:i:s", $data['no_tollway_time_seconds'])
: null;
/* ===============================
กำหนด type
================================ */
$main_type = (!empty($data['main_distance_km']) && $data['main_distance_km'] > 0) ? 1 : 0;
$no_tollway_type = (!empty($data['no_tollway_distance_km']) && $data['no_tollway_distance_km'] > 0) ? 1 : 0;
/* ===============================
bind_param
================================ */
$stmt->bind_param(
"sddsddidsdidsdssss",
$data['origin_name'],
$data['origin_lat'],
$data['origin_lon'],
$data['destination_name'],
$data['destination_lat'],
$data['destination_lon'],
$main_type,
$data['main_distance_km'],
$main_time,
$data['main_fare'],
$no_tollway_type,
$data['no_tollway_distance_km'],
$no_tollway_time,
$data['no_tollway_fare'],
$data['route_used'],
$data['route_by'],
$data['meter_id'],
$trip_no
);
/* ===============================
Execute
================================ */
if ($stmt->execute()) {
echo json_encode([
'success' => true,
'message' => 'บันทึกข้อมูลสำเร็จ',
'estimate_id' => $stmt->insert_id,
'trip_no' => $trip_no
]);
} else {
throw new Exception("Execute failed: " . $stmt->error);
}
$stmt->close();
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()
]);
}
$conn->close();