get_rebate_details.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. header('Content-Type: application/json');
  5. // 获取返点兑换ID
  6. $redemptionId = isset($_GET['redemption_id']) ? intval($_GET['redemption_id']) : 0;
  7. if ($redemptionId <= 0) {
  8. echo json_encode(['success' => false, 'message' => '无效的兑换ID']);
  9. exit;
  10. }
  11. // 验证权限
  12. $employee_id = $_SESSION['employee_id'];
  13. $isAdmin = checkIfAdmin();
  14. // 检查是否为财务角色或管理员
  15. $isFinance = false;
  16. $checkRoleSql = "SELECT em_permission_role_id FROM employee WHERE id = $employee_id";
  17. $roleResult = mysqli_query($conn, $checkRoleSql);
  18. if ($roleResult && $row = mysqli_fetch_assoc($roleResult)) {
  19. $isFinance = ($row['em_permission_role_id'] == 6 || $row['em_permission_role_id'] == 1);
  20. }
  21. // 获取返点兑换详情
  22. $sql = "SELECT
  23. rri.id,
  24. rri.order_id,
  25. rri.order_item_id,
  26. rri.product_id,
  27. rri.quantity,
  28. rri.rebate_amount,
  29. rri.rebate_rule_id,
  30. p.ProductName AS product_name,
  31. o.order_code,
  32. oi.unit,
  33. (SELECT rr.rebate_amount FROM rebate_rules rr WHERE rr.id = rri.rebate_rule_id) AS rule_amount
  34. FROM
  35. rebate_redemption_items rri
  36. JOIN
  37. products p ON rri.product_id = p.id
  38. JOIN
  39. orders o ON rri.order_id = o.id
  40. JOIN
  41. order_items oi ON rri.order_item_id = oi.id
  42. JOIN
  43. rebate_redemptions rr ON rri.redemption_id = rr.id
  44. JOIN
  45. customer c ON rr.customer_id = c.id
  46. WHERE
  47. rri.redemption_id = ?";
  48. // 非管理员和非财务只能查看自己客户的数据
  49. if (!$isAdmin && !$isFinance) {
  50. $sql .= " AND c.cs_belong = $employee_id";
  51. }
  52. $sql .= " ORDER BY o.order_code, p.ProductName";
  53. // 使用预处理语句防止SQL注入
  54. $stmt = $conn->prepare($sql);
  55. $stmt->bind_param("i", $redemptionId);
  56. $stmt->execute();
  57. $result = $stmt->get_result();
  58. if (!$result) {
  59. echo json_encode(['success' => false, 'message' => '查询失败: ' . $conn->error]);
  60. exit;
  61. }
  62. // 获取所有返点项目
  63. $items = [];
  64. while ($row = $result->fetch_assoc()) {
  65. // 使用规则表中的单位返点金额,而不是存储的总返点金额
  66. $unitRebate = isset($row['rule_amount']) ? $row['rule_amount'] : $row['rebate_amount'];
  67. // 计算每项的总返点金额
  68. $totalRebate = $row['quantity'] * $unitRebate;
  69. $items[] = [
  70. 'id' => $row['id'],
  71. 'order_id' => $row['order_id'],
  72. 'order_code' => $row['order_code'],
  73. 'product_id' => $row['product_id'],
  74. 'product_name' => htmlspecialcharsFix($row['product_name']),
  75. 'quantity' => $row['quantity'],
  76. 'unit' => $row['unit'],
  77. 'rebate_amount' => number_format($unitRebate, 2),
  78. 'total_rebate' => number_format($totalRebate, 2)
  79. ];
  80. }
  81. // 返回JSON数据
  82. echo json_encode([
  83. 'success' => true,
  84. 'redemption_id' => $redemptionId,
  85. 'items' => $items,
  86. 'count' => count($items)
  87. ]);
  88. ?>