alert('无效的请求方式'); window.location.href='rebate_summary.php';";
exit;
}
// 获取表单数据
$customerId = isset($_POST['customer_id']) ? intval($_POST['customer_id']) : 0;
$totalRebateAmount = isset($_POST['total_rebate_amount']) ? floatval($_POST['total_rebate_amount']) : 0;
$notes = isset($_POST['notes']) ? mysqli_real_escape_string($conn, $_POST['notes']) : '';
$items = isset($_POST['items']) ? $_POST['items'] : [];
// 验证基本数据
if ($customerId <= 0) {
echo "";
exit;
}
if ($totalRebateAmount <= 0) {
echo "";
exit;
}
if (empty($items)) {
echo "";
exit;
}
// 开始事务
$conn->begin_transaction();
try {
// 获取当前登录的员工ID
$employeeId = $_SESSION['employee_id'];
// 1. 创建主兑换记录
$insertRedemption = "INSERT INTO rebate_redemptions
(customer_id, redemption_date, total_rebate_amount, status, notes, created_by)
VALUES (?, NOW(), ?, 1, ?, ?)";
$stmt = $conn->prepare($insertRedemption);
$stmt->bind_param("idsi", $customerId, $totalRebateAmount, $notes, $employeeId);
if (!$stmt->execute()) {
throw new Exception("创建兑换记录失败: " . $stmt->error);
}
$redemptionId = $conn->insert_id;
$stmt->close();
// 2. 创建兑换明细记录
$insertItemStmt = $conn->prepare("INSERT INTO rebate_redemption_items
(redemption_id, order_id, order_item_id, product_id, quantity, rebate_amount, rebate_rule_id)
VALUES (?, ?, ?, ?, ?, ?, ?)");
foreach ($items as $item) {
$orderId = intval($item['order_id']);
$orderItemId = intval($item['order_item_id']);
$productId = intval($item['product_id']);
$quantity = intval($item['quantity']);
$rebateAmount = floatval($item['item_rebate_total']);
$rebateRuleId = intval($item['rebate_rule_id']);
$insertItemStmt->bind_param("iiiiidi", $redemptionId, $orderId, $orderItemId, $productId, $quantity, $rebateAmount, $rebateRuleId);
if (!$insertItemStmt->execute()) {
throw new Exception("创建兑换明细记录失败: " . $insertItemStmt->error);
}
}
$insertItemStmt->close();
// 提交事务
$conn->commit();
// 返回成功消息
echo "";
} catch (Exception $e) {
// 回滚事务
$conn->rollback();
// 显示错误信息
echo "";
}
?>