rebate_redeem_save.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 防止非POST提交
  5. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  6. echo "<script>alert('无效的请求方式'); window.location.href='rebate_summary.php';</script>";
  7. exit;
  8. }
  9. // 获取表单数据
  10. $customerId = isset($_POST['customer_id']) ? intval($_POST['customer_id']) : 0;
  11. $totalRebateAmount = isset($_POST['total_rebate_amount']) ? floatval($_POST['total_rebate_amount']) : 0;
  12. $notes = isset($_POST['notes']) ? mysqli_real_escape_string($conn, $_POST['notes']) : '';
  13. $items = isset($_POST['items']) ? $_POST['items'] : [];
  14. // 验证基本数据
  15. if ($customerId <= 0) {
  16. echo "<script>alert('客户信息无效'); window.location.href='rebate_summary.php';</script>";
  17. exit;
  18. }
  19. if ($totalRebateAmount <= 0) {
  20. echo "<script>alert('返点总额必须大于零'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
  21. exit;
  22. }
  23. if (empty($items)) {
  24. echo "<script>alert('没有可兑换的返点项目'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
  25. exit;
  26. }
  27. // 开始事务
  28. $conn->begin_transaction();
  29. try {
  30. // 获取当前登录的员工ID
  31. $employeeId = $_SESSION['employee_id'];
  32. // 1. 创建主兑换记录
  33. $insertRedemption = "INSERT INTO rebate_redemptions
  34. (customer_id, redemption_date, total_rebate_amount, status, notes, created_by)
  35. VALUES (?, NOW(), ?, 1, ?, ?)";
  36. $stmt = $conn->prepare($insertRedemption);
  37. $stmt->bind_param("idsi", $customerId, $totalRebateAmount, $notes, $employeeId);
  38. if (!$stmt->execute()) {
  39. throw new Exception("创建兑换记录失败: " . $stmt->error);
  40. }
  41. $redemptionId = $conn->insert_id;
  42. $stmt->close();
  43. // 2. 创建兑换明细记录
  44. $insertItemStmt = $conn->prepare("INSERT INTO rebate_redemption_items
  45. (redemption_id, order_id, order_item_id, product_id, quantity, rebate_amount, rebate_rule_id)
  46. VALUES (?, ?, ?, ?, ?, ?, ?)");
  47. foreach ($items as $item) {
  48. $orderId = intval($item['order_id']);
  49. $orderItemId = intval($item['order_item_id']);
  50. $productId = intval($item['product_id']);
  51. $quantity = intval($item['quantity']);
  52. $rebateAmount = floatval($item['item_rebate_total']);
  53. $rebateRuleId = intval($item['rebate_rule_id']);
  54. $insertItemStmt->bind_param("iiiiidi", $redemptionId, $orderId, $orderItemId, $productId, $quantity, $rebateAmount, $rebateRuleId);
  55. if (!$insertItemStmt->execute()) {
  56. throw new Exception("创建兑换明细记录失败: " . $insertItemStmt->error);
  57. }
  58. }
  59. $insertItemStmt->close();
  60. // 提交事务
  61. $conn->commit();
  62. //向所有财务发送消息提示
  63. // 获取客户名称
  64. $customerQuery = "SELECT cs_code FROM customer WHERE id = $customerId";
  65. $customerResult = $conn->query($customerQuery);
  66. $customerName = "未知客户";
  67. if ($customerRow = $customerResult->fetch_assoc()) {
  68. $customerName = $customerRow['cs_code'];
  69. }
  70. // 查询所有财务人员
  71. $financeStaffQuery = "SELECT id, em_user FROM employee WHERE em_permission_role_id = 6 AND login_forbidden = 0";
  72. $financeStaffResult = $conn->query($financeStaffQuery);
  73. if ($financeStaffResult && $financeStaffResult->num_rows > 0) {
  74. $financeStaffIds = [];
  75. while ($staff = $financeStaffResult->fetch_assoc()) {
  76. $financeStaffIds[] = $staff['id'];
  77. }
  78. // 构建消息内容
  79. $messageTitle = "新的返点兑换申请";
  80. $messageContent = "客户 {$customerName} 提交了一笔返点兑换申请,金额:¥" . number_format($totalRebateAmount, 2) . "。\n";
  81. $messageContent = textDecode($messageContent);
  82. $messageContent .= "兑换ID:{$redemptionId}\n";
  83. $messageContent .= "处理人:{$_SESSION['employee_name']}\n";
  84. $messageContent .= "申请时间:" . date('Y-m-d H:i:s') . "\n";
  85. $messageContent .= "请及时处理。";
  86. // 发送消息给所有财务人员
  87. sendMessage(
  88. $messageTitle,
  89. $messageContent,
  90. 3, // 订单相关
  91. 1, // 部分群发
  92. $financeStaffIds,
  93. 1 // 重要
  94. );
  95. }
  96. // 返回成功消息
  97. echo "<script>alert('返点兑换成功!'); window.location.href='rebate_summary.php';</script>";
  98. } catch (Exception $e) {
  99. // 回滚事务
  100. $conn->rollback();
  101. // 显示错误信息
  102. echo "<script>alert('处理失败: " . addslashes($e->getMessage()) . "'); window.location.href='rebate_redeem.php?customer_id=$customerId';</script>";
  103. }
  104. ?>