123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- require_once 'conn.php';
- checkLogin();
- // 防止非POST提交
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- echo "<script>alert('无效的请求方式'); window.location.href='rebate_summary.php';</script>";
- 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 "<script>alert('客户信息无效'); window.location.href='rebate_summary.php';</script>";
- exit;
- }
- if ($totalRebateAmount <= 0) {
- echo "<script>alert('返点总额必须大于零'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
- exit;
- }
- if (empty($items)) {
- echo "<script>alert('没有可兑换的返点项目'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
- 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();
-
- //向所有财务发送消息提示
- // 获取客户名称
- $customerQuery = "SELECT cs_code FROM customer WHERE id = $customerId";
- $customerResult = $conn->query($customerQuery);
- $customerName = "未知客户";
- if ($customerRow = $customerResult->fetch_assoc()) {
- $customerName = $customerRow['cs_code'];
- }
-
- // 查询所有财务人员
- $financeStaffQuery = "SELECT id, em_user FROM employee WHERE em_permission_role_id = 6 AND login_forbidden = 0";
- $financeStaffResult = $conn->query($financeStaffQuery);
-
- if ($financeStaffResult && $financeStaffResult->num_rows > 0) {
- $financeStaffIds = [];
- while ($staff = $financeStaffResult->fetch_assoc()) {
- $financeStaffIds[] = $staff['id'];
- }
-
- // 构建消息内容
- $messageTitle = "新的返点兑换申请";
- $messageContent = "客户 {$customerName} 提交了一笔返点兑换申请,金额:¥" . number_format($totalRebateAmount, 2) . "。\n";
- $messageContent = textDecode($messageContent);
- $messageContent .= "兑换ID:{$redemptionId}\n";
- $messageContent .= "处理人:{$_SESSION['employee_name']}\n";
- $messageContent .= "申请时间:" . date('Y-m-d H:i:s') . "\n";
- $messageContent .= "请及时处理。";
-
- // 发送消息给所有财务人员
- sendMessage(
- $messageTitle,
- $messageContent,
- 3, // 订单相关
- 1, // 部分群发
- $financeStaffIds,
- 1 // 重要
- );
- }
- // 返回成功消息
- echo "<script>alert('返点兑换成功!'); window.location.href='rebate_summary.php';</script>";
-
- } catch (Exception $e) {
- // 回滚事务
- $conn->rollback();
-
- // 显示错误信息
- echo "<script>alert('处理失败: " . addslashes($e->getMessage()) . "'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
- }
- ?>
|