get_rebate_details.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. o.order_date,
  33. o.shipping_date,
  34. oi.unit,
  35. (SELECT rr.rebate_amount FROM rebate_rules rr WHERE rr.id = rri.rebate_rule_id) AS rule_amount
  36. FROM
  37. rebate_redemption_items rri
  38. JOIN
  39. products p ON rri.product_id = p.id
  40. JOIN
  41. orders o ON rri.order_id = o.id
  42. JOIN
  43. order_items oi ON rri.order_item_id = oi.id
  44. JOIN
  45. rebate_redemptions rr ON rri.redemption_id = rr.id
  46. JOIN
  47. customer c ON rr.customer_id = c.id
  48. WHERE
  49. rri.redemption_id = ?";
  50. // 非管理员和非财务只能查看自己客户的数据
  51. if (!$isAdmin && !$isFinance) {
  52. $sql .= " AND c.cs_belong = $employee_id";
  53. }
  54. $sql .= " ORDER BY o.order_code, p.ProductName";
  55. // 使用预处理语句防止SQL注入
  56. $stmt = $conn->prepare($sql);
  57. $stmt->bind_param("i", $redemptionId);
  58. $stmt->execute();
  59. $result = $stmt->get_result();
  60. if (!$result) {
  61. echo json_encode(['success' => false, 'message' => '查询失败: ' . $conn->error]);
  62. exit;
  63. }
  64. // 获取所有返点项目
  65. $items = [];
  66. while ($row = $result->fetch_assoc()) {
  67. // 使用规则表中的单位返点金额,而不是存储的总返点金额
  68. $unitRebate = isset($row['rule_amount']) ? $row['rule_amount'] : $row['rebate_amount'];
  69. // 计算每项的总返点金额
  70. $totalRebate = $row['quantity'] * $unitRebate;
  71. // 格式化日期
  72. $orderDate = !empty($row['order_date']) ? date('Y-m-d', strtotime($row['order_date'])) : '';
  73. $shippingDate = !empty($row['shipping_date']) ? date('Y-m-d', strtotime($row['shipping_date'])) : '';
  74. $items[] = [
  75. 'id' => $row['id'],
  76. 'order_id' => $row['order_id'],
  77. 'order_code' => $row['order_code'],
  78. 'order_date' => $orderDate,
  79. 'shipping_date' => $shippingDate,
  80. 'product_id' => $row['product_id'],
  81. 'product_name' => htmlspecialcharsFix($row['product_name']),
  82. 'quantity' => $row['quantity'],
  83. 'unit' => $row['unit'],
  84. 'rebate_amount' => number_format($unitRebate, 2),
  85. 'total_rebate' => number_format($totalRebate, 2)
  86. ];
  87. }
  88. // 返回JSON数据
  89. echo json_encode([
  90. 'success' => true,
  91. 'redemption_id' => $redemptionId,
  92. 'items' => $items,
  93. 'count' => count($items)
  94. ]);
  95. ?>