905bbc5934
- 在多个文件中添加调试日志记录功能 - 将数据库连接统一迁移到db_connect.php - 改进预约时间冲突检测逻辑 - 优化VIP客户数据处理 - 增强套餐查询的健壮性 - 更新预约状态处理流程
167 lines
6.6 KiB
PHP
167 lines
6.6 KiB
PHP
<?php
|
|
// 加载数据库配置
|
|
require_once 'db_connect.php';
|
|
// #region agent log
|
|
$log_data = json_encode(['location' => 'process_booking.php:3', 'message' => 'Database connection check', 'data' => ['has_pdo' => isset($pdo), 'using_db_connect' => true], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'D']);
|
|
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
|
|
// #endregion
|
|
|
|
// 检查表单提交
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
try {
|
|
// 获取表单数据
|
|
$customer_name = $_POST['customer_name'] ?? '';
|
|
$phone = $_POST['phone'] ?? '';
|
|
$car_model = $_POST['car_model'] ?? '';
|
|
$car_number = $_POST['car_number'] ?? '';
|
|
$booking_date = $_POST['booking_date'] ?? '';
|
|
$time_slot = $_POST['time_slot'] ?? '';
|
|
$package_id = $_POST['package_id'] ?? '';
|
|
$notes = $_POST['notes'] ?? '';
|
|
$source = $_POST['source'] ?? '其他'; // 来源标识,用于确定返回页面
|
|
$duration = isset($_POST['duration']) ? (int)$_POST['duration'] : 60; // 默认60分钟
|
|
$total_price = isset($_POST['total_price']) ? (float)$_POST['total_price'] : 0;
|
|
|
|
// 验证必填字段
|
|
if (empty($customer_name)) {
|
|
throw new Exception('请输入客户姓名');
|
|
}
|
|
|
|
if (empty($phone)) {
|
|
throw new Exception('请输入手机号码');
|
|
} elseif (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
|
|
throw new Exception('请输入正确的手机号码');
|
|
}
|
|
|
|
if (empty($car_number)) {
|
|
throw new Exception('请输入车牌号');
|
|
}
|
|
|
|
if (empty($booking_date)) {
|
|
throw new Exception('请选择预约日期');
|
|
}
|
|
|
|
if (empty($time_slot)) {
|
|
throw new Exception('请选择预约时间');
|
|
}
|
|
|
|
if (empty($package_id)) {
|
|
throw new Exception('请选择洗车套餐');
|
|
}
|
|
|
|
// 验证日期是否为过去
|
|
$current_date = date('Y-m-d');
|
|
if ($booking_date < $current_date) {
|
|
throw new Exception('不能选择过去的日期');
|
|
}
|
|
|
|
// 解析时间段,转换为start_time和end_time
|
|
// time_slot格式可能是 "09:00-10:00" 或 "09:00"
|
|
$start_time_str = '';
|
|
$end_time_str = '';
|
|
|
|
if (strpos($time_slot, '-') !== false) {
|
|
// 格式:09:00-10:00
|
|
list($start_time_str, $end_time_str) = explode('-', $time_slot);
|
|
$start_time = $booking_date . ' ' . trim($start_time_str) . ':00';
|
|
$end_time = $booking_date . ' ' . trim($end_time_str) . ':00';
|
|
} else {
|
|
// 格式:09:00,使用默认时长
|
|
$start_time = $booking_date . ' ' . trim($time_slot) . ':00';
|
|
$end_time = date('Y-m-d H:i:s', strtotime($start_time) + $duration * 60);
|
|
}
|
|
|
|
// #region agent log
|
|
$log_data = json_encode(['location' => 'process_booking.php:70', 'message' => 'Time conversion', 'data' => ['booking_date' => $booking_date, 'time_slot' => $time_slot, 'start_time' => $start_time, 'end_time' => $end_time, 'duration' => $duration], 'timestamp' => time() * 1000, 'sessionId' => 'debug-session', 'runId' => 'run1', 'hypothesisId' => 'E']);
|
|
file_put_contents('.cursor/debug.log', $log_data . "\n", FILE_APPEND);
|
|
// #endregion
|
|
|
|
// 获取套餐信息以获取价格和默认时长
|
|
$stmt = $pdo->prepare("SELECT * FROM packages WHERE id = ? AND is_active = 1");
|
|
$stmt->execute([$package_id]);
|
|
$package = $stmt->fetch();
|
|
|
|
if (!$package) {
|
|
throw new Exception('选择的套餐无效');
|
|
}
|
|
|
|
// 如果未提供价格,使用套餐价格
|
|
if ($total_price <= 0) {
|
|
$total_price = $package['price'];
|
|
}
|
|
|
|
// 如果未提供时长,使用套餐默认时长
|
|
if ($duration <= 0) {
|
|
$duration = $package['base_duration'];
|
|
// 重新计算结束时间
|
|
$end_time = date('Y-m-d H:i:s', strtotime($start_time) + $duration * 60);
|
|
}
|
|
|
|
// 检查时间冲突
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM bookings
|
|
WHERE status != '已取消'
|
|
AND (
|
|
(start_time <= ? AND end_time > ?)
|
|
OR (start_time < ? AND end_time >= ?)
|
|
OR (start_time >= ? AND end_time <= ?)
|
|
)");
|
|
$stmt->execute([$start_time, $start_time, $end_time, $end_time, $start_time, $end_time]);
|
|
|
|
if ($stmt->fetchColumn() > 0) {
|
|
throw new Exception('该时间段已被预约,请选择其他时间');
|
|
}
|
|
|
|
// 对于0元订单,自动标记为已付款
|
|
$payment_status = ($total_price <= 0) ? '已付款' : '未付款';
|
|
|
|
// 插入预约记录(使用新的数据库结构)
|
|
$stmt = $pdo->prepare("INSERT INTO bookings
|
|
(customer_name, phone, car_model, car_number, package_id, custom_services,
|
|
start_time, end_time, duration, total_price, notes, member_type, source, payment_status, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
|
|
$member_type = '普通客户'; // 默认普通客户,VIP客户应该通过index.php添加
|
|
|
|
$stmt->execute([
|
|
$customer_name,
|
|
$phone,
|
|
$car_model,
|
|
$car_number,
|
|
$package_id,
|
|
'', // custom_services
|
|
$start_time,
|
|
$end_time,
|
|
$duration,
|
|
$total_price,
|
|
$notes,
|
|
$member_type,
|
|
$source,
|
|
$payment_status,
|
|
'待确认' // status
|
|
]);
|
|
|
|
// 预约成功
|
|
$success_message = '预约添加成功!';
|
|
|
|
// 根据来源决定返回页面
|
|
$redirect_url = $source === 'vip_page' ? 'vip.php' : 'index.php';
|
|
|
|
echo "<script>
|
|
alert('$success_message');
|
|
window.location.href = '$redirect_url';
|
|
</script>";
|
|
|
|
} catch (Exception $e) {
|
|
$error_message = $e->getMessage();
|
|
echo "<script>
|
|
alert('$error_message');
|
|
window.history.back();
|
|
</script>";
|
|
}
|
|
} else {
|
|
// 不是POST请求,重定向到首页
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
?>
|