alert('订单编号不能为空');history.back();";
exit;
}
if ($customer_id <= 0) {
echo "";
exit;
}
if (empty($items)) {
echo "";
exit;
}
// 检查客户国家和产品销售限制
$customer_query = "SELECT cs_country FROM customer WHERE id = $customer_id LIMIT 1";
$customer_result = mysqli_query($conn, $customer_query);
if ($customer_result && mysqli_num_rows($customer_result) > 0) {
$customer_data = mysqli_fetch_assoc($customer_result);
$customer_country = $customer_data['cs_country'];
if (!empty($customer_country)) {
$restricted_products = [];
foreach ($items as $item) {
if (empty($item['product_id'])) continue;
$product_id = (int)$item['product_id'];
// 获取产品详情,包括nosale字段
$product_query = "SELECT ProductName, nosale FROM products WHERE id = $product_id LIMIT 1";
$product_result = mysqli_query($conn, $product_query);
if ($product_result && mysqli_num_rows($product_result) > 0) {
$product_data = mysqli_fetch_assoc($product_result);
$nosale_countries = $product_data['nosale'];
// 检查客户所在国家是否在销售限制列表中
if (!empty($nosale_countries)) {
$restricted_countries = explode(',', $nosale_countries);
if (in_array($customer_country, $restricted_countries)) {
$restricted_products[] = $product_data['ProductName'];
}
}
}
}
// 如果有限制销售的产品,显示错误并返回
if (!empty($restricted_products)) {
$restricted_product_names = implode('、', $restricted_products);
echo "";
exit;
}
}
}
// 处理保存
if ($isedit) {
//价格判断,不能低于指导价
$price_error = false;
$error_product_name = '';
$error_min_price = 0;
$error_current_price = 0;
foreach ($items as $item) {
if (empty($item['product_id'])) continue;
$product_id = (int)$item['product_id'];
$quantity = (int)$item['quantity'];
$unit_price = (float)$item['unit_price'];
// 查询产品名称,用于错误提示
$product_query = "SELECT ProductName FROM products WHERE id = $product_id";
$product_result = mysqli_query($conn, $product_query);
// 检查产品是否存在
if (mysqli_num_rows($product_result) === 0) {
echo "";
exit;
}
$product_row = mysqli_fetch_assoc($product_result);
$product_name = $product_row['ProductName'];
// 查询该产品在价格表中的最低价格要求
// 根据产品ID和数量查找最接近但不超过当前订单数量的价格记录
$price_query = "SELECT * FROM price
WHERE productId = $product_id
AND num <= $quantity
ORDER BY num DESC
LIMIT 1";
$price_result = mysqli_query($conn, $price_query);
if (mysqli_num_rows($price_result) > 0) {
$price_row = mysqli_fetch_assoc($price_result);
$min_price = (float)$price_row['price'];
// 如果单价低于指导价,标记错误
if ($unit_price < $min_price) {
$price_error = true;
$error_product_name = $product_name;
$error_min_price = $min_price;
$error_current_price = $unit_price;
break;
}
}
}
// 如果价格低于指导价,显示错误并返回
if ($price_error) {
$error_message = "产品 {$error_product_name} 的价格 ({$error_current_price}) 低于指导价 ({$error_min_price})";
echo "";
exit;
}
// 更新订单基本信息
$sql = "UPDATE orders SET
order_code = '$order_code',
customer_id = $customer_id,
contact_id = $contact_id,
employee_id = $employee_id,
order_date = '$order_date',
delivery_date = $delivery_date,
actual_delivery_date = $actual_delivery_date,
order_status = $order_status,
payment_status = $payment_status,
currency = '$currency',
subtotal = $subtotal,
discount_amount = $discount_amount,
total_amount = $total_amount,
notes = '$notes',
internal_notes = '$internal_notes',
updated_at = NOW()
WHERE id = $id";
mysqli_query($conn, $sql);
// 删除旧的订单项
$sql = "DELETE FROM order_items WHERE order_id = $id";
mysqli_query($conn, $sql);
// 添加新的订单项
foreach ($items as $item) {
if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
$product_id = (int)$item['product_id'];
$spec_id = isset($item['spec_id']) ? (int)$item['spec_id'] : 0; // 添加规格ID
$quantity = (int)$item['quantity'];
$unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
$unit_price = (float)$item['unit_price'];
$total_price = $quantity * $unit_price;
$item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
$sql = "INSERT INTO order_items (
order_id, product_id, specification_id, quantity, unit, unit_price,
total_price, notes,
created_at, updated_at
) VALUES (
$id, $product_id, $spec_id, $quantity, '$unit', $unit_price,
$total_price, '$item_notes',
NOW(), NOW()
)";
mysqli_query($conn, $sql);
}
$message = "订单更新成功!";
} else {
//价格判断,不能低于指导价
$price_error = false;
$error_product_name = '';
$error_min_price = 0;
$error_current_price = 0;
foreach ($items as $item) {
if (empty($item['product_id'])) continue;
$product_id = (int)$item['product_id'];
$quantity = (int)$item['quantity'];
$unit_price = (float)$item['unit_price'];
// 查询产品名称,用于错误提示
$product_query = "SELECT ProductName FROM products WHERE id = $product_id";
$product_result = mysqli_query($conn, $product_query);
// 检查产品是否存在
if (mysqli_num_rows($product_result) === 0) {
echo "";
exit;
}
$product_row = mysqli_fetch_assoc($product_result);
$product_name = $product_row['ProductName'];
// 查询该产品在价格表中的最低价格要求
// 根据产品ID和数量查找最接近但不超过当前订单数量的价格记录
$price_query = "SELECT * FROM price
WHERE productId = $product_id
AND num <= $quantity
ORDER BY num DESC
LIMIT 1";
$price_result = mysqli_query($conn, $price_query);
if (mysqli_num_rows($price_result) > 0) {
$price_row = mysqli_fetch_assoc($price_result);
$min_price = (float)$price_row['price'];
// 如果单价低于指导价,标记错误
if ($unit_price < $min_price) {
$price_error = true;
$error_product_name = $product_name;
$error_min_price = $min_price;
$error_current_price = $unit_price;
break;
}
}
}
// 如果价格低于指导价,显示错误并返回
if ($price_error) {
$error_message = "产品 {$error_product_name} 的价格 ({$error_current_price}) 低于指导价 ({$error_min_price})";
echo "";
exit;
}
// 创建新订单
$sql = "INSERT INTO orders (
order_code, customer_id, contact_id, employee_id,
order_date, delivery_date, actual_delivery_date,
order_status, payment_status, currency,
subtotal, discount_amount, total_amount,
notes, internal_notes, created_at, updated_at
) VALUES (
'$order_code', $customer_id, $contact_id, $employee_id,
'$order_date', $delivery_date, $actual_delivery_date,
$order_status, $payment_status, '$currency',
$subtotal, $discount_amount, $total_amount,
'$notes', '$internal_notes', NOW(), NOW()
)";
mysqli_query($conn, $sql);
$order_id = mysqli_insert_id($conn);
// 添加订单项
foreach ($items as $item) {
if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
$product_id = (int)$item['product_id'];
$spec_id = isset($item['spec_id']) ? (int)$item['spec_id'] : 0; // 添加规格ID
$quantity = (int)$item['quantity'];
$unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
$unit_price = (float)$item['unit_price'];
$total_price = $quantity * $unit_price;
$item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
$sql = "INSERT INTO order_items (
order_id, product_id, specification_id, quantity, unit, unit_price,
total_price, notes,
created_at, updated_at
) VALUES (
$order_id, $product_id, $spec_id, $quantity, '$unit', $unit_price,
$total_price, '$item_notes',
NOW(), NOW()
)";
mysqli_query($conn, $sql);
}
$message = "订单创建成功!";
}
// 重定向回订单列表页面
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
echo "";
exit;
?>