export_rebate_details.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 获取返点兑换ID
  5. $redemptionId = isset($_GET['redemption_id']) ? intval($_GET['redemption_id']) : 0;
  6. if ($redemptionId <= 0) {
  7. die('无效的兑换ID');
  8. }
  9. // 验证权限
  10. $employee_id = $_SESSION['employee_id'];
  11. $isAdmin = checkIfAdmin();
  12. // 检查是否为财务角色或管理员
  13. $isFinance = false;
  14. $checkRoleSql = "SELECT em_permission_role_id FROM employee WHERE id = $employee_id";
  15. $roleResult = mysqli_query($conn, $checkRoleSql);
  16. if ($roleResult && $row = mysqli_fetch_assoc($roleResult)) {
  17. $isFinance = ($row['em_permission_role_id'] == 6 || $row['em_permission_role_id'] == 1);
  18. }
  19. // 获取客户信息和返点兑换基本信息
  20. $customerSql = "SELECT
  21. c.cs_code,
  22. c.cs_company,
  23. rr.redemption_date,
  24. rr.total_rebate_amount
  25. FROM
  26. rebate_redemptions rr
  27. JOIN
  28. customer c ON rr.customer_id = c.id
  29. WHERE
  30. rr.id = ?";
  31. $customerStmt = $conn->prepare($customerSql);
  32. $customerStmt->bind_param("i", $redemptionId);
  33. $customerStmt->execute();
  34. $customerResult = $customerStmt->get_result();
  35. if (!$customerResult || $customerResult->num_rows == 0) {
  36. die('未找到返点兑换记录');
  37. }
  38. $customerInfo = $customerResult->fetch_assoc();
  39. // 获取返点兑换详情
  40. $sql = "SELECT
  41. rri.id,
  42. rri.order_id,
  43. rri.order_item_id,
  44. rri.product_id,
  45. rri.quantity,
  46. rri.rebate_amount,
  47. rri.rebate_rule_id,
  48. p.ProductName AS product_name,
  49. o.order_code,
  50. o.order_date,
  51. o.shipping_date,
  52. oi.unit,
  53. (SELECT rr.rebate_amount FROM rebate_rules rr WHERE rr.id = rri.rebate_rule_id) AS rule_amount
  54. FROM
  55. rebate_redemption_items rri
  56. JOIN
  57. products p ON rri.product_id = p.id
  58. JOIN
  59. orders o ON rri.order_id = o.id
  60. JOIN
  61. order_items oi ON rri.order_item_id = oi.id
  62. JOIN
  63. rebate_redemptions rr ON rri.redemption_id = rr.id
  64. JOIN
  65. customer c ON rr.customer_id = c.id
  66. WHERE
  67. rri.redemption_id = ?";
  68. // 非管理员和非财务只能查看自己客户的数据
  69. if (!$isAdmin && !$isFinance) {
  70. $sql .= " AND c.cs_belong = $employee_id";
  71. }
  72. $sql .= " ORDER BY o.order_code, p.ProductName";
  73. // 使用预处理语句防止SQL注入
  74. $stmt = $conn->prepare($sql);
  75. $stmt->bind_param("i", $redemptionId);
  76. $stmt->execute();
  77. $result = $stmt->get_result();
  78. if (!$result) {
  79. die('查询失败: ' . $conn->error);
  80. }
  81. // 设置响应头,指定文件类型为CSV
  82. header('Content-Type: text/csv; charset=utf-8');
  83. header('Content-Disposition: attachment; filename="返点详情_' . textDecode($customerInfo['cs_code']) . '_' . date('Ymd') . '.csv"');
  84. // 创建一个文件指针,连接到PHP的输出流
  85. $output = fopen('php://output', 'w');
  86. // 添加UTF-8 BOM以确保Excel正确识别中文
  87. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  88. // 写入CSV文件头信息
  89. fputcsv($output, [
  90. '客户编码: ' . $customerInfo['cs_code'],
  91. '客户名称: ' . $customerInfo['cs_company'],
  92. '兑换日期: ' . date('Y-m-d', strtotime($customerInfo['redemption_date'])),
  93. '总返点金额: ' . number_format($customerInfo['total_rebate_amount'], 2) . ' 元'
  94. ]);
  95. // 空行
  96. fputcsv($output, []);
  97. // 写入CSV标题行
  98. fputcsv($output, [
  99. '订单编号',
  100. '订单日期',
  101. '出货日期',
  102. '产品名称',
  103. '数量',
  104. '单位',
  105. '返点单价(元/件)',
  106. '返点金额(元)'
  107. ]);
  108. // 写入数据行
  109. $totalRebate = 0;
  110. while ($row = $result->fetch_assoc()) {
  111. // 使用规则表中的单位返点金额,而不是存储的总返点金额
  112. $unitRebate = isset($row['rule_amount']) ? $row['rule_amount'] : $row['rebate_amount'];
  113. // 计算每项的总返点金额
  114. $itemTotalRebate = $row['quantity'] * $unitRebate;
  115. $totalRebate += $itemTotalRebate;
  116. // 格式化日期
  117. $orderDate = !empty($row['order_date']) ? date('Y-m-d', strtotime($row['order_date'])) : '-';
  118. $shippingDate = !empty($row['shipping_date']) ? date('Y-m-d', strtotime($row['shipping_date'])) : '-';
  119. fputcsv($output, [
  120. $row['order_code'],
  121. $orderDate,
  122. $shippingDate,
  123. $row['product_name'],
  124. $row['quantity'],
  125. $row['unit'],
  126. number_format($unitRebate, 2),
  127. number_format($itemTotalRebate, 2)
  128. ]);
  129. }
  130. // 写入合计行
  131. fputcsv($output, []);
  132. fputcsv($output, ['', '', '', '', '', '', '合计:', number_format($totalRebate, 2)]);
  133. // 关闭文件指针
  134. fclose($output);
  135. exit;
  136. ?>