get_rebate_details.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $sql = "SELECT
  16. rri.id,
  17. rri.order_id,
  18. rri.order_item_id,
  19. rri.product_id,
  20. rri.quantity,
  21. rri.rebate_amount,
  22. p.ProductName AS product_name,
  23. o.order_code,
  24. oi.unit
  25. FROM
  26. rebate_redemption_items rri
  27. JOIN
  28. products p ON rri.product_id = p.id
  29. JOIN
  30. orders o ON rri.order_id = o.id
  31. JOIN
  32. order_items oi ON rri.order_item_id = oi.id
  33. JOIN
  34. rebate_redemptions rr ON rri.redemption_id = rr.id
  35. JOIN
  36. customer c ON rr.customer_id = c.id
  37. WHERE
  38. rri.redemption_id = ?";
  39. // 非管理员只能查看自己客户的数据
  40. if (!$isAdmin) {
  41. $sql .= " AND c.cs_belong = $employee_id";
  42. }
  43. $sql .= " ORDER BY o.order_code, p.ProductName";
  44. // 使用预处理语句防止SQL注入
  45. $stmt = $conn->prepare($sql);
  46. $stmt->bind_param("i", $redemptionId);
  47. $stmt->execute();
  48. $result = $stmt->get_result();
  49. if (!$result) {
  50. echo json_encode(['success' => false, 'message' => '查询失败: ' . $conn->error]);
  51. exit;
  52. }
  53. // 获取所有返点项目
  54. $items = [];
  55. while ($row = $result->fetch_assoc()) {
  56. // 计算每项的总返点金额
  57. $totalRebate = $row['quantity'] * $row['rebate_amount'];
  58. $items[] = [
  59. 'id' => $row['id'],
  60. 'order_id' => $row['order_id'],
  61. 'order_code' => $row['order_code'],
  62. 'product_id' => $row['product_id'],
  63. 'product_name' => htmlspecialcharsFix($row['product_name']),
  64. 'quantity' => $row['quantity'],
  65. 'unit' => $row['unit'],
  66. 'rebate_amount' => number_format($row['rebate_amount'], 2),
  67. 'total_rebate' => number_format($totalRebate, 2)
  68. ];
  69. }
  70. // 返回JSON数据
  71. echo json_encode([
  72. 'success' => true,
  73. 'redemption_id' => $redemptionId,
  74. 'items' => $items,
  75. 'count' => count($items)
  76. ]);
  77. ?>