rebate_history.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 辅助函数
  5. $urlStr = '';
  6. // 处理筛选条件
  7. $fliterFromDate = $_GET['fliterFromDate'] ?? '';
  8. $fliterToDate = $_GET['fliterToDate'] ?? '';
  9. $fliterStr = "";
  10. if (!empty($fliterFromDate)) {
  11. $fliterStr .= " AND rr.redemption_date >= '" . mysqli_real_escape_string($conn, $fliterFromDate) . "'";
  12. $urlStr .= "&fliterFromDate=" . urlencode($fliterFromDate);
  13. }
  14. if (!empty($fliterToDate)) {
  15. $fliterStr .= " AND rr.redemption_date <= '" . mysqli_real_escape_string($conn, $fliterToDate) . " 23:59:59'";
  16. $urlStr .= "&fliterToDate=" . urlencode($fliterToDate);
  17. }
  18. // 搜索参数
  19. $keys = $_GET['Keys'] ?? '';
  20. $keyscode = mysqli_real_escape_string($conn, $keys);
  21. $page = isset($_GET['Page']) ? intval($_GET['Page']) : 1;
  22. // 构建基本条件SQL
  23. $employee_id = $_SESSION['employee_id'];
  24. $isAdmin = checkIfAdmin();
  25. // 基础查询,计算总记录数
  26. $countSql = "SELECT COUNT(*) AS total
  27. FROM rebate_redemptions rr
  28. JOIN customer c ON rr.customer_id = c.id
  29. WHERE 1=1";
  30. // 非管理员只能查看自己客户的返点历史
  31. if (!$isAdmin) {
  32. $countSql .= " AND c.cs_belong = $employee_id";
  33. }
  34. // 添加搜索条件
  35. if (!empty($keyscode)) {
  36. $countSql .= " AND (c.cs_company LIKE '%$keyscode%' OR c.cs_code LIKE '%$keyscode%')";
  37. }
  38. // 添加日期筛选
  39. $countSql .= $fliterStr;
  40. // 执行查询获取总记录数
  41. $countResult = mysqli_query($conn, $countSql);
  42. if (!$countResult) {
  43. die("查询记录数量错误: " . mysqli_error($conn));
  44. }
  45. $countRow = mysqli_fetch_assoc($countResult);
  46. $totalRecords = $countRow['total'];
  47. // 设置每页显示记录数和分页
  48. $pageSize = 20;
  49. // 计算总页数
  50. $totalPages = ceil($totalRecords / $pageSize);
  51. if ($totalPages < 1) $totalPages = 1;
  52. // 验证当前页码
  53. if ($page < 1) $page = 1;
  54. if ($page > $totalPages) $page = $totalPages;
  55. // 计算起始记录
  56. $offset = ($page - 1) * $pageSize;
  57. // 查询返点兑换历史记录
  58. $sql = "SELECT
  59. rr.id AS redemption_id,
  60. rr.customer_id,
  61. c.cs_code,
  62. c.cs_company AS customer_name,
  63. rr.redemption_date,
  64. rr.total_rebate_amount,
  65. rr.notes,
  66. rr.status,
  67. e.em_user AS employee_name,
  68. (SELECT COUNT(DISTINCT product_id) FROM rebate_redemption_items WHERE redemption_id = rr.id) AS product_count
  69. FROM
  70. rebate_redemptions rr
  71. JOIN
  72. customer c ON rr.customer_id = c.id
  73. LEFT JOIN
  74. employee e ON rr.created_by = e.id
  75. WHERE
  76. 1=1";
  77. // 非管理员只能查看自己客户的返点历史
  78. if (!$isAdmin) {
  79. $sql .= " AND c.cs_belong = $employee_id";
  80. }
  81. // 添加搜索条件
  82. if (!empty($keyscode)) {
  83. $sql .= " AND (c.cs_company LIKE '%$keyscode%' OR c.cs_code LIKE '%$keyscode%')";
  84. }
  85. // 添加日期筛选
  86. $sql .= $fliterStr;
  87. // 添加排序和分页
  88. $sql .= " ORDER BY rr.redemption_date DESC, rr.id DESC LIMIT $offset, $pageSize";
  89. $result = mysqli_query($conn, $sql);
  90. if (!$result) {
  91. die("查询返点历史错误: " . mysqli_error($conn));
  92. }
  93. // 获取返点历史记录
  94. $redemptions = [];
  95. while ($row = mysqli_fetch_assoc($result)) {
  96. $redemptions[] = $row;
  97. }
  98. ?>
  99. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  100. <html xmlns="http://www.w3.org/1999/xhtml">
  101. <head>
  102. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  103. <title>客户返点历史</title>
  104. <link rel="stylesheet" href="css/common.css" type="text/css" />
  105. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  106. <script src="js/jquery-1.7.2.min.js"></script>
  107. <script src="js/js.js"></script>
  108. <style>
  109. body {
  110. margin: 0;
  111. padding: 20px;
  112. background: #fff;
  113. }
  114. #man_zone {
  115. margin-left: 0;
  116. }
  117. /* 表格布局 */
  118. .table2 {
  119. width: 100%;
  120. }
  121. .theader, .tline {
  122. display: flex;
  123. flex-direction: row;
  124. align-items: center;
  125. width: 100%;
  126. border-bottom: 1px solid #ddd;
  127. }
  128. .theader {
  129. background-color: #f2f2f2;
  130. font-weight: bold;
  131. height: 40px;
  132. }
  133. .tline {
  134. height: 45px;
  135. }
  136. .tline:hover {
  137. background-color: #f5f5f5;
  138. }
  139. .col1 { width: 5%; text-align: center; }
  140. .col2 { width: 15%; }
  141. .col3 { width: 20%; }
  142. .col4 { width: 12%; text-align: center; }
  143. .col5 { width: 10%; text-align: center; }
  144. .col6 { width: 15%; text-align: right; }
  145. .col7 { width: 10%; text-align: center; }
  146. .col8 { width: 13%; text-align: center; }
  147. /* 表格布局修复 */
  148. .table2 .col1 { width: 5%; text-align: center; }
  149. .table2 .col2 { width: 15%; }
  150. .table2 .col3 { width: 20%; }
  151. .table2 .col4 { width: 12%; text-align: center; }
  152. .table2 .col5 { width: 10%; text-align: center; }
  153. .table2 .col6 { width: 15%; text-align: right; }
  154. .table2 .col7 { width: 10%; text-align: center; }
  155. .table2 .col8 { width: 13%; text-align: center; }
  156. .theader > div, .tline > div {
  157. padding: 0 5px;
  158. overflow: hidden;
  159. text-overflow: ellipsis;
  160. white-space: nowrap;
  161. }
  162. /* 日期选择器样式 */
  163. .date-input {
  164. padding: 5px;
  165. border: 1px solid #ccc;
  166. border-radius: 3px;
  167. }
  168. /* 滑动面板样式 */
  169. .rb-slidepanel {
  170. cursor: pointer;
  171. }
  172. .rb-slidepanel.open {
  173. font-weight: bold;
  174. color: #3366cc;
  175. }
  176. .notepanel {
  177. display: none;
  178. background: #f9f9f9;
  179. padding: 10px;
  180. border: 1px solid #eee;
  181. margin-bottom: 10px;
  182. }
  183. .notepanel .noteItem {
  184. font-weight: bold;
  185. margin-bottom: 5px;
  186. }
  187. .rebate-details {
  188. margin-top: 10px;
  189. border-top: 1px dashed #ddd;
  190. padding-top: 10px;
  191. }
  192. /* 状态样式 */
  193. .status-active {
  194. color: #27ae60;
  195. font-weight: bold;
  196. }
  197. .status-cancelled {
  198. color: #e74c3c;
  199. text-decoration: line-through;
  200. }
  201. /* 备注文本溢出控制 */
  202. .notes-text {
  203. max-width: 200px;
  204. white-space: nowrap;
  205. overflow: hidden;
  206. text-overflow: ellipsis;
  207. display: inline-block;
  208. }
  209. </style>
  210. </head>
  211. <body>
  212. <div id="man_zone">
  213. <div class="fastSelect clear">
  214. <H1>客户返点历史查询</H1>
  215. <div class="selectItem">
  216. <label>兑换日期</label>
  217. <input type="date" name="fliterFromDate" class="date-input filterSearch" value="<?= $fliterFromDate ?>">
  218. <label>到</label>
  219. <input type="date" name="fliterToDate" class="date-input filterSearch" value="<?= $fliterToDate ?>">
  220. </div>
  221. <div class="inputSearch">
  222. <input type="text" id="keys" class="inputTxt" placeholder="请输入客户名称或编码"
  223. value="<?= empty($keyscode) ? '' : $keyscode ?>" />
  224. <input type="button" id="searchgo" class="searchgo" value="搜索"
  225. onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)" />
  226. <a href="rebate_summary.php" class="btn1" style="float: right; display: inline-block; padding: 5px 15px; margin-top: 0; line-height: 22px; height: 22px;">返回返点统计</a>
  227. </div>
  228. </div>
  229. <div class="table2 em<?= $_SESSION['employee_id'] ?>">
  230. <div class="theader">
  231. <div class="col1">序号</div>
  232. <div class="col2">客户编码</div>
  233. <div class="col3">客户名称</div>
  234. <div class="col4">兑换日期</div>
  235. <div class="col5">产品数</div>
  236. <div class="col6">返点金额</div>
  237. <div class="col7">处理人</div>
  238. <div class="col8">操作</div>
  239. </div>
  240. <?php
  241. if (!empty($redemptions)) {
  242. $tempNum = ($page - 1) * $pageSize;
  243. foreach ($redemptions as $redemption) {
  244. $tempNum++;
  245. $statusClass = $redemption['status'] == 1 ? 'status-active' : 'status-cancelled';
  246. ?>
  247. <div class="tline">
  248. <div class="col1"><?= $tempNum ?></div>
  249. <div class="col2"><?= htmlspecialcharsFix($redemption['cs_code']) ?></div>
  250. <div class="col3 rb-slidepanel" data-id="<?= $redemption['redemption_id'] ?>"><?= htmlspecialcharsFix($redemption['customer_name']) ?></div>
  251. <div class="col4"><?= date('Y-m-d', strtotime($redemption['redemption_date'])) ?></div>
  252. <div class="col5"><?= $redemption['product_count'] ?></div>
  253. <div class="col6 <?= $statusClass ?>"><?= number_format($redemption['total_rebate_amount'], 2) ?> 元</div>
  254. <div class="col7"><?= htmlspecialcharsFix($redemption['employee_name']) ?></div>
  255. <div class="col8">
  256. <a href="javascript:void(0)" class="rb-toggleDetail" data-id="<?= $redemption['redemption_id'] ?>">查看详情</a>
  257. </div>
  258. </div>
  259. <div class="notepanel clear" id="detail-<?= $redemption['redemption_id'] ?>">
  260. <div class="noteItem">返点详情</div>
  261. <?php if (!empty($redemption['notes'])): ?>
  262. <div style="margin-bottom: 10px; color: #666;">
  263. <strong>备注:</strong> <?= htmlspecialcharsFix($redemption['notes']) ?>
  264. </div>
  265. <?php endif; ?>
  266. <div class="rebate-details" id="rebate-details-<?= $redemption['redemption_id'] ?>">
  267. <div style="text-align: center; padding: 10px;">
  268. <img src="images/loading.gif" alt="加载中" style="width: 20px; height: 20px;">
  269. 加载返点详情...
  270. </div>
  271. </div>
  272. </div>
  273. <?php
  274. }
  275. } else {
  276. if (empty($keys) && empty($fliterStr)) {
  277. echo '<div class="tline"><div style="width: 100%; text-align: center;">当前没有返点历史记录</div></div>';
  278. } else {
  279. echo '<div class="tline"><div style="width: 100%; text-align: center;"><a href="?">没有找到匹配的返点历史记录,点击返回</a></div></div>';
  280. }
  281. }
  282. ?>
  283. <div class="showpagebox">
  284. <?php
  285. if ($totalPages > 1) {
  286. $pageName = "?Keys=$keys$urlStr&";
  287. $pageLen = 3;
  288. if ($page > 1) {
  289. echo "<a href=\"{$pageName}Page=1\">首页</a>";
  290. echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
  291. }
  292. if ($pageLen * 2 + 1 >= $totalPages) {
  293. $startPage = 1;
  294. $endPage = $totalPages;
  295. } else {
  296. if ($page <= $pageLen + 1) {
  297. $startPage = 1;
  298. $endPage = $pageLen * 2 + 1;
  299. } else {
  300. $startPage = $page - $pageLen;
  301. $endPage = $page + $pageLen;
  302. }
  303. if ($page + $pageLen > $totalPages) {
  304. $startPage = $totalPages - $pageLen * 2;
  305. $endPage = $totalPages;
  306. }
  307. }
  308. for ($i = $startPage; $i <= $endPage; $i++) {
  309. if ($i == $page) {
  310. echo "<a class=\"current\">$i</a>";
  311. } else {
  312. echo "<a href=\"{$pageName}Page=$i\">$i</a>";
  313. }
  314. }
  315. if ($page < $totalPages) {
  316. if ($totalPages - $page > $pageLen) {
  317. echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
  318. }
  319. echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
  320. echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
  321. }
  322. }
  323. ?>
  324. </div>
  325. </div>
  326. <script>
  327. $(document).ready(function() {
  328. // 添加日期验证逻辑
  329. $('input[name="fliterToDate"]').on('change', function() {
  330. var fromDate = $('input[name="fliterFromDate"]').val();
  331. var toDate = $(this).val();
  332. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  333. alert('结束日期不能早于开始日期');
  334. $(this).val(''); // 清空结束日期
  335. return false;
  336. }
  337. });
  338. // 开始日期变更时也进行验证
  339. $('input[name="fliterFromDate"]').on('change', function() {
  340. var fromDate = $(this).val();
  341. var toDate = $('input[name="fliterToDate"]').val();
  342. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  343. alert('开始日期不能晚于结束日期');
  344. $('input[name="fliterToDate"]').val(''); // 清空结束日期
  345. return false;
  346. }
  347. });
  348. // 处理筛选条件改变
  349. $('.filterSearch').change(function() {
  350. var url = '?';
  351. var keys = $('#keys').val();
  352. if (keys && keys != '请输入客户名称或编码') {
  353. url += 'Keys=' + encodeURIComponent(keys) + '&';
  354. }
  355. $('.filterSearch').each(function() {
  356. var name = $(this).attr('name');
  357. var value = $(this).val();
  358. if (value) {
  359. url += name + '=' + encodeURIComponent(value) + '&';
  360. }
  361. });
  362. // 移除末尾的&
  363. if (url.endsWith('&')) {
  364. url = url.substring(0, url.length - 1);
  365. }
  366. location.href = url;
  367. });
  368. // 切换显示返点详情
  369. $('.rb-toggleDetail').on('click', function(e) {
  370. e.preventDefault();
  371. e.stopPropagation(); // 阻止事件冒泡
  372. var redemptionId = $(this).data('id');
  373. var $detailPanel = $('#detail-' + redemptionId);
  374. var $panel = $(this);
  375. if ($detailPanel.is(':visible')) {
  376. $detailPanel.slideUp();
  377. $panel.text('查看详情');
  378. // 移除打开状态
  379. $panel.closest('.tline').find('.rb-slidepanel').removeClass('open');
  380. } else {
  381. $detailPanel.slideDown();
  382. $panel.text('收起详情');
  383. // 添加打开状态
  384. $panel.closest('.tline').find('.rb-slidepanel').addClass('open');
  385. // 加载详情数据
  386. loadRedemptionDetails(redemptionId);
  387. }
  388. });
  389. // 客户名称点击也可以切换显示
  390. $('.rb-slidepanel').on('click', function(e) {
  391. e.preventDefault();
  392. e.stopPropagation(); // 阻止事件冒泡
  393. var redemptionId = $(this).data('id');
  394. var $detailPanel = $('#detail-' + redemptionId);
  395. var $toggleButton = $('.rb-toggleDetail[data-id="' + redemptionId + '"]');
  396. if ($detailPanel.is(':visible')) {
  397. $detailPanel.slideUp();
  398. $toggleButton.text('查看详情');
  399. $(this).removeClass('open');
  400. } else {
  401. $detailPanel.slideDown();
  402. $toggleButton.text('收起详情');
  403. $(this).addClass('open');
  404. // 加载详情数据
  405. loadRedemptionDetails(redemptionId);
  406. }
  407. });
  408. // 加载返点详情
  409. function loadRedemptionDetails(redemptionId) {
  410. var $detailsContainer = $('#rebate-details-' + redemptionId);
  411. // 检查是否已经加载过
  412. if ($detailsContainer.data('loaded')) {
  413. return;
  414. }
  415. // 异步加载详情
  416. $.ajax({
  417. url: 'get_rebate_details.php',
  418. type: 'GET',
  419. data: { redemption_id: redemptionId },
  420. dataType: 'json',
  421. success: function(data) {
  422. if (data && data.success) {
  423. var html = '<table class="detail-table" style="width:100%; border-collapse: collapse;">';
  424. html += '<tr style="background-color: #eee; font-weight: bold;">' +
  425. '<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">订单编号</th>' +
  426. '<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">产品名称</th>' +
  427. '<th style="padding: 8px; text-align: center; border: 1px solid #ddd;">数量</th>' +
  428. '<th style="padding: 8px; text-align: right; border: 1px solid #ddd;">返点单价</th>' +
  429. '<th style="padding: 8px; text-align: right; border: 1px solid #ddd;">返点金额</th>' +
  430. '</tr>';
  431. $.each(data.items, function(i, item) {
  432. html += '<tr style="border-bottom: 1px solid #ddd;">' +
  433. '<td style="padding: 8px; border: 1px solid #ddd;">' + item.order_code + '</td>' +
  434. '<td style="padding: 8px; border: 1px solid #ddd;">' + item.product_name + '</td>' +
  435. '<td style="padding: 8px; text-align: center; border: 1px solid #ddd;">' + item.quantity + ' ' + item.unit + '</td>' +
  436. '<td style="padding: 8px; text-align: right; border: 1px solid #ddd;">' + item.rebate_amount + ' 元/件</td>' +
  437. '<td style="padding: 8px; text-align: right; border: 1px solid #ddd;">' + item.total_rebate + ' 元</td>' +
  438. '</tr>';
  439. });
  440. html += '</table>';
  441. $detailsContainer.html(html);
  442. $detailsContainer.data('loaded', true);
  443. } else {
  444. $detailsContainer.html('<div style="padding: 10px; color: #e74c3c;">加载详情失败: ' + (data.message || '未知错误') + '</div>');
  445. }
  446. },
  447. error: function(jqXHR, textStatus, errorThrown) {
  448. $detailsContainer.html('<div style="padding: 10px; color: #e74c3c;">加载详情失败: ' + textStatus + '</div>');
  449. }
  450. });
  451. }
  452. });
  453. </script>
  454. </div>
  455. </body>
  456. </html>