1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- require_once 'conn.php';
- checkLogin();
- header('Content-Type: application/json');
- // 获取返点兑换ID
- $redemptionId = isset($_GET['redemption_id']) ? intval($_GET['redemption_id']) : 0;
- if ($redemptionId <= 0) {
- echo json_encode(['success' => false, 'message' => '无效的兑换ID']);
- exit;
- }
- // 验证权限
- $employee_id = $_SESSION['employee_id'];
- $isAdmin = checkIfAdmin();
- // 获取返点兑换详情
- $sql = "SELECT
- rri.id,
- rri.order_id,
- rri.order_item_id,
- rri.product_id,
- rri.quantity,
- rri.rebate_amount,
- p.ProductName AS product_name,
- o.order_code,
- oi.unit
- FROM
- rebate_redemption_items rri
- JOIN
- products p ON rri.product_id = p.id
- JOIN
- orders o ON rri.order_id = o.id
- JOIN
- order_items oi ON rri.order_item_id = oi.id
- JOIN
- rebate_redemptions rr ON rri.redemption_id = rr.id
- JOIN
- customer c ON rr.customer_id = c.id
- WHERE
- rri.redemption_id = ?";
- // 非管理员只能查看自己客户的数据
- if (!$isAdmin) {
- $sql .= " AND c.cs_belong = $employee_id";
- }
- $sql .= " ORDER BY o.order_code, p.ProductName";
- // 使用预处理语句防止SQL注入
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("i", $redemptionId);
- $stmt->execute();
- $result = $stmt->get_result();
- if (!$result) {
- echo json_encode(['success' => false, 'message' => '查询失败: ' . $conn->error]);
- exit;
- }
- // 获取所有返点项目
- $items = [];
- while ($row = $result->fetch_assoc()) {
- // 计算每项的总返点金额
- $totalRebate = $row['quantity'] * $row['rebate_amount'];
-
- $items[] = [
- 'id' => $row['id'],
- 'order_id' => $row['order_id'],
- 'order_code' => $row['order_code'],
- 'product_id' => $row['product_id'],
- 'product_name' => htmlspecialcharsFix($row['product_name']),
- 'quantity' => $row['quantity'],
- 'unit' => $row['unit'],
- 'rebate_amount' => number_format($row['rebate_amount'], 2),
- 'total_rebate' => number_format($totalRebate, 2)
- ];
- }
- // 返回JSON数据
- echo json_encode([
- 'success' => true,
- 'redemption_id' => $redemptionId,
- 'items' => $items,
- 'count' => count($items)
- ]);
- ?>
|