rebate_summary.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 辅助函数
  5. $act = $_GET['act'] ?? '';
  6. $urlStr = '';
  7. // 处理筛选条件
  8. $fliterFromDate = $_GET['fliterFromDate'] ?? '';
  9. $fliterToDate = $_GET['fliterToDate'] ?? '';
  10. $fliterStr = "";
  11. if (!empty($fliterFromDate)) {
  12. $fliterStr .= " AND o.order_date >= '" . mysqli_real_escape_string($conn, $fliterFromDate) . "'";
  13. $urlStr .= "&fliterFromDate=" . urlencode($fliterFromDate);
  14. }
  15. if (!empty($fliterToDate)) {
  16. $fliterStr .= " AND o.order_date <= '" . mysqli_real_escape_string($conn, $fliterToDate) . " 23:59:59'";
  17. $urlStr .= "&fliterToDate=" . urlencode($fliterToDate);
  18. }
  19. // 搜索参数
  20. $keys = $_GET['Keys'] ?? '';
  21. $keyscode = mysqli_real_escape_string($conn, $keys);
  22. $page = $_GET['Page'] ?? 1;
  23. // 构建基本条件SQL - 这部分是两个查询共用的
  24. $employee_id = $_SESSION['employee_id'];
  25. $isAdmin = checkIfAdmin();
  26. // 步骤1:查询符合条件的客户ID列表
  27. $customerListSql = "SELECT DISTINCT o.customer_id
  28. FROM orders o
  29. JOIN order_items oi ON o.id = oi.order_id
  30. JOIN customer c ON o.customer_id = c.id
  31. JOIN products p ON oi.product_id = p.id
  32. WHERE
  33. o.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  34. AND o.order_type = 1
  35. AND p.rebate = 1
  36. AND NOT EXISTS (
  37. SELECT 1
  38. FROM rebate_redemption_items rri
  39. WHERE rri.order_item_id = oi.id
  40. )
  41. AND EXISTS (
  42. SELECT 1
  43. FROM rebate_rules rr
  44. WHERE rr.product_id = oi.product_id
  45. )
  46. AND (
  47. SELECT SUM(oi2.quantity)
  48. FROM order_items oi2
  49. JOIN orders o2 ON oi2.order_id = o2.id
  50. WHERE o2.customer_id = o.customer_id
  51. AND o2.order_type = 1
  52. AND oi2.product_id = oi.product_id
  53. AND o2.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  54. AND NOT EXISTS (
  55. SELECT 1
  56. FROM rebate_redemption_items rri
  57. WHERE rri.order_item_id = oi2.id
  58. )
  59. ) >= (
  60. SELECT MIN(rr.min_quantity)
  61. FROM rebate_rules rr
  62. WHERE rr.product_id = oi.product_id
  63. )";
  64. // 非管理员只能查看自己的客户返点
  65. if (!$isAdmin) {
  66. $customerListSql .= " AND c.cs_belong = $employee_id";
  67. }
  68. // 添加搜索条件
  69. if (!empty($keyscode)) {
  70. $customerListSql .= " AND (c.cs_company LIKE '%$keyscode%' OR c.cs_code LIKE '%$keyscode%')";
  71. }
  72. // 添加日期筛选
  73. $customerListSql .= $fliterStr;
  74. // 执行查询获取客户ID列表
  75. $customerResult = mysqli_query($conn, $customerListSql);
  76. if (!$customerResult) {
  77. die("查询客户列表错误: " . mysqli_error($conn));
  78. }
  79. // 获取客户ID并创建IN子句
  80. $customerIds = [];
  81. while ($row = mysqli_fetch_assoc($customerResult)) {
  82. $customerIds[] = $row['customer_id'];
  83. }
  84. // 如果没有找到客户,设置一个不可能的ID以确保查询不返回任何结果
  85. if (empty($customerIds)) {
  86. $customerIds = [-1]; // 不可能的ID
  87. }
  88. $customerIdsStr = implode(',', $customerIds);
  89. // 设置每页显示记录数和分页
  90. $pageSize = 20;
  91. $totalRecords = count($customerIds);
  92. // 计算总页数
  93. $totalPages = ceil($totalRecords / $pageSize);
  94. if ($totalPages < 1) $totalPages = 1;
  95. // 验证当前页码
  96. $page = (int)$page;
  97. if ($page < 1) $page = 1;
  98. if ($page > $totalPages) $page = $totalPages;
  99. // 计算起始记录
  100. $offset = ($page - 1) * $pageSize;
  101. // 步骤2:获取分页后的客户详细信息
  102. // 为防止表结构问题,使用更简单的SQL格式并明确使用id字段
  103. // 先获取客户基本信息
  104. $paginatedCustomerIds = array_slice($customerIds, $offset, $pageSize);
  105. if (empty($paginatedCustomerIds)) {
  106. $paginatedCustomerIds = [-1]; // 确保不会有结果
  107. }
  108. $paginatedIdsStr = implode(',', $paginatedCustomerIds);
  109. $customerDetailSql = "
  110. SELECT
  111. c.id AS customer_id,
  112. c.cs_company AS customer_name,
  113. c.cs_code
  114. FROM
  115. customer c
  116. WHERE
  117. c.id IN ($paginatedIdsStr)";
  118. $result = mysqli_query($conn, $customerDetailSql);
  119. if (!$result) {
  120. die("查询客户基本信息错误: " . mysqli_error($conn));
  121. }
  122. $customers = [];
  123. while ($row = mysqli_fetch_assoc($result)) {
  124. $customers[$row['customer_id']] = $row;
  125. $customers[$row['customer_id']]['total_rebate_amount'] = 0;
  126. $customers[$row['customer_id']]['qualifying_products'] = 0;
  127. $customers[$row['customer_id']]['rebate_details'] = '';
  128. }
  129. // 如果找到客户,获取每个客户的返点详情
  130. if (!empty($customers)) {
  131. $customerIdsForDetails = array_keys($customers);
  132. $customerIdsForDetailsStr = implode(',', $customerIdsForDetails);
  133. // 获取客户返点总金额和产品数量
  134. $rebateDetailsSql = "
  135. SELECT
  136. o.customer_id,
  137. SUM(
  138. oi.quantity * (
  139. SELECT rr.rebate_amount
  140. FROM rebate_rules rr
  141. WHERE rr.product_id = oi.product_id
  142. AND rr.min_quantity <= (
  143. SELECT SUM(oi2.quantity)
  144. FROM order_items oi2
  145. JOIN orders o2 ON oi2.order_id = o2.id
  146. WHERE o2.customer_id = o.customer_id
  147. AND o2.order_type = 1
  148. AND oi2.product_id = oi.product_id
  149. AND o2.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  150. AND NOT EXISTS (
  151. SELECT 1
  152. FROM rebate_redemption_items rri
  153. WHERE rri.order_item_id = oi2.id
  154. )
  155. )
  156. ORDER BY rr.min_quantity DESC
  157. LIMIT 1
  158. )
  159. ) AS total_rebate_amount,
  160. COUNT(DISTINCT oi.product_id) AS qualifying_products
  161. FROM
  162. orders o
  163. JOIN
  164. order_items oi ON o.id = oi.order_id
  165. JOIN
  166. products p ON oi.product_id = p.id
  167. WHERE
  168. o.customer_id IN ($customerIdsForDetailsStr)
  169. AND o.order_type = 1
  170. AND o.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  171. AND p.rebate = 1
  172. AND NOT EXISTS (
  173. SELECT 1
  174. FROM rebate_redemption_items rri
  175. WHERE rri.order_item_id = oi.id
  176. )
  177. GROUP BY
  178. o.customer_id";
  179. $detailsResult = mysqli_query($conn, $rebateDetailsSql);
  180. if (!$detailsResult) {
  181. die("查询返点详情错误: " . mysqli_error($conn));
  182. }
  183. // 填充总金额和产品数量
  184. while ($detailRow = mysqli_fetch_assoc($detailsResult)) {
  185. if (isset($customers[$detailRow['customer_id']])) {
  186. $customers[$detailRow['customer_id']]['total_rebate_amount'] = $detailRow['total_rebate_amount'];
  187. $customers[$detailRow['customer_id']]['qualifying_products'] = $detailRow['qualifying_products'];
  188. }
  189. }
  190. // 获取每个客户的产品返点详情
  191. foreach ($customerIdsForDetails as $customerId) {
  192. $productDetailsSql = "
  193. SELECT
  194. p.ProductName,
  195. SUM(oi.quantity) AS quantity,
  196. (
  197. SELECT rr.rebate_amount
  198. FROM rebate_rules rr
  199. WHERE rr.product_id = oi.product_id
  200. AND rr.min_quantity <= SUM(oi.quantity)
  201. ORDER BY rr.min_quantity DESC
  202. LIMIT 1
  203. ) AS rebate_amount
  204. FROM
  205. order_items oi
  206. JOIN
  207. orders o ON oi.order_id = o.id
  208. JOIN
  209. products p ON oi.product_id = p.id
  210. WHERE
  211. o.customer_id = $customerId
  212. AND o.order_type = 1
  213. AND o.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  214. AND p.rebate = 1
  215. AND NOT EXISTS (
  216. SELECT 1
  217. FROM rebate_redemption_items rri
  218. WHERE rri.order_item_id = oi.id
  219. )
  220. GROUP BY
  221. oi.product_id, p.ProductName";
  222. $productResult = mysqli_query($conn, $productDetailsSql);
  223. if (!$productResult) {
  224. die("查询产品详情错误: " . mysqli_error($conn));
  225. }
  226. // 构建返点详情文本
  227. $details = [];
  228. while ($productRow = mysqli_fetch_assoc($productResult)) {
  229. $details[] = $productRow['ProductName'] . ': ' .
  230. $productRow['quantity'] . ' 件 x ' .
  231. $productRow['rebate_amount'] . ' 元/件';
  232. }
  233. $customers[$customerId]['rebate_details'] = implode('; ', $details);
  234. }
  235. // 按照返点金额排序
  236. usort($customers, function($a, $b) {
  237. return $b['total_rebate_amount'] <=> $a['total_rebate_amount'];
  238. });
  239. }
  240. ?>
  241. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  242. <html xmlns="http://www.w3.org/1999/xhtml">
  243. <head>
  244. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  245. <title>客户返点统计</title>
  246. <link rel="stylesheet" href="css/common.css" type="text/css" />
  247. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  248. <script src="js/jquery-1.7.2.min.js"></script>
  249. <script src="js/js.js"></script>
  250. <style>
  251. body {
  252. margin: 0;
  253. padding: 20px;
  254. background: #fff;
  255. }
  256. #man_zone {
  257. margin-left: 0;
  258. }
  259. /* 表格布局 */
  260. .table2 {
  261. width: 100%;
  262. }
  263. .theader, .tline {
  264. display: flex;
  265. flex-direction: row;
  266. align-items: center;
  267. width: 100%;
  268. border-bottom: 1px solid #ddd;
  269. }
  270. .theader {
  271. background-color: #f2f2f2;
  272. font-weight: bold;
  273. height: 40px;
  274. }
  275. .tline {
  276. height: 45px;
  277. }
  278. .tline:hover {
  279. background-color: #f5f5f5;
  280. }
  281. .col2 { width: 5%; text-align: center; }
  282. .col3 { width: 15%; }
  283. .col4 { width: 25%; }
  284. .col5 { width: 10%; text-align: center; }
  285. .col6 { width: 15%; text-align: right; }
  286. .col7 { width: 10%; text-align: center; }
  287. .col8 { width: 20%; text-align: center; }
  288. /* 表格布局修复,因为 "css/common.css 覆盖了 */
  289. .table2 .col2 { width: 5%; text-align: center; }
  290. .table2 .col3 { width: 15%; }
  291. .table2 .col4 { width: 25%; }
  292. .table2 .col5 { width: 10%; text-align: center; }
  293. .table2 .col6 { width: 15%; text-align: right; }
  294. .table2 .col7 { width: 10%; text-align: center; }
  295. .table2 .col8 { width: 20%; text-align: center; }
  296. .theader > div, .tline > div {
  297. padding: 0 5px;
  298. overflow: hidden;
  299. text-overflow: ellipsis;
  300. white-space: nowrap;
  301. display: flex;
  302. align-items: center;
  303. justify-content: center;
  304. }
  305. .col3, .col4 {
  306. justify-content: flex-start !important;
  307. }
  308. .col6 {
  309. justify-content: flex-end !important;
  310. }
  311. /* 日期选择器样式 */
  312. .date-input {
  313. padding: 5px;
  314. border: 1px solid #ccc;
  315. border-radius: 3px;
  316. }
  317. /* 滑动面板样式 */
  318. .slidepanel {
  319. cursor: pointer;
  320. }
  321. .slidepanel.open {
  322. font-weight: bold;
  323. color: #3366cc;
  324. }
  325. .notepanel {
  326. display: none;
  327. background: #f9f9f9;
  328. padding: 10px;
  329. border: 1px solid #eee;
  330. margin-bottom: 10px;
  331. }
  332. .notepanel .noteItem {
  333. font-weight: bold;
  334. margin-bottom: 5px;
  335. }
  336. .rebate-details {
  337. margin-top: 10px;
  338. border-top: 1px dashed #ddd;
  339. padding-top: 10px;
  340. }
  341. </style>
  342. </head>
  343. <body>
  344. <div id="man_zone">
  345. <div class="fastSelect clear">
  346. <H1>筛选条件</H1>
  347. <div class="selectItem">
  348. <label>订单日期</label>
  349. <input type="date" name="fliterFromDate" class="date-input filterSearch" value="<?= $fliterFromDate ?>">
  350. <label>到</label>
  351. <input type="date" name="fliterToDate" class="date-input filterSearch" value="<?= $fliterToDate ?>">
  352. </div>
  353. <div class="inputSearch">
  354. <input type="text" id="keys" class="inputTxt" placeholder="请输入客户名称或编码"
  355. value="<?= empty($keyscode) ? '' : $keyscode ?>" />
  356. <input type="button" id="searchgo" class="searchgo" value="搜索"
  357. onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)" />
  358. </div>
  359. <div style="text-align: right; margin-top: 10px; clear: both;">
  360. <a href="rebate_expiring.php" class="btn1" style="display: inline-flex; align-items: center; justify-content: center; padding: 5px 15px; margin-top: 0; height: 22px; background-color: #e74c3c; margin-right: 5px;">查看过期预警</a>
  361. <a href="rebate_history.php" class="btn1" style="display: inline-flex; align-items: center; justify-content: center; padding: 5px 15px; margin-top: 0; height: 22px;">查看返点历史</a>
  362. </div>
  363. </div>
  364. <div class="table2 em<?= $_SESSION['employee_id'] ?>">
  365. <div class="theader">
  366. <div class="col2">序号</div>
  367. <div class="col3">客户编码</div>
  368. <div class="col4">客户名称</div>
  369. <div class="col5">返点产品数</div>
  370. <div class="col6">返点金额合计</div>
  371. <div class="col7">查看详情</div>
  372. <div class="col8">操作</div>
  373. </div>
  374. <?php
  375. if (!empty($customers)) {
  376. $tempNum = ($page - 1) * $pageSize;
  377. foreach ($customers as $customer) {
  378. $tempNum++;
  379. ?>
  380. <div class="tline">
  381. <div class="col2"><?= $tempNum ?></div>
  382. <div class="col3 slidepanel "><?= htmlspecialcharsFix($customer['cs_code']) ?></div>
  383. <div class="col4 slidepanel " data-id="<?= $customer['customer_id'] ?>"><?= htmlspecialcharsFix($customer['customer_name']) ?></div>
  384. <div class="col5"><?= $customer['qualifying_products'] ?></div>
  385. <div class="col6"><?= number_format($customer['total_rebate_amount'], 2) ?> 元</div>
  386. <div class="col7">
  387. <a href="javascript:void(0)" class="toggleDetail" data-id="<?= $customer['customer_id'] ?>">展开详情</a>
  388. </div>
  389. <div class="col8">
  390. <a href="rebate_redeem.php?customer_id=<?= $customer['customer_id'] ?>" class="ico_edit ico">处理兑换</a>
  391. </div>
  392. </div>
  393. <div class="notepanel clear" id="detail-<?= $customer['customer_id'] ?>">
  394. <div class="noteItem">返点详情</div>
  395. <div class="rebate-details">
  396. <?= htmlspecialcharsFix($customer['rebate_details']) ?>
  397. </div>
  398. </div>
  399. <?php
  400. }
  401. } else {
  402. if (empty($keys) && empty($fliterStr)) {
  403. echo '<div class="tline"><div align="center" colspan="7">当前没有客户有可用返点</div></div>';
  404. } else {
  405. echo '<div class="tline"><div align="center" colspan="7"><a href="?">没有找到匹配的返点记录,点击返回</a></div></div>';
  406. }
  407. }
  408. ?>
  409. <div class="showpagebox">
  410. <?php
  411. if ($totalPages > 1) {
  412. $pageName = "?Keys=$keys$urlStr&";
  413. $pageLen = 3;
  414. if ($page > 1) {
  415. echo "<a href=\"{$pageName}Page=1\">首页</a>";
  416. echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
  417. }
  418. if ($pageLen * 2 + 1 >= $totalPages) {
  419. $startPage = 1;
  420. $endPage = $totalPages;
  421. } else {
  422. if ($page <= $pageLen + 1) {
  423. $startPage = 1;
  424. $endPage = $pageLen * 2 + 1;
  425. } else {
  426. $startPage = $page - $pageLen;
  427. $endPage = $page + $pageLen;
  428. }
  429. if ($page + $pageLen > $totalPages) {
  430. $startPage = $totalPages - $pageLen * 2;
  431. $endPage = $totalPages;
  432. }
  433. }
  434. for ($i = $startPage; $i <= $endPage; $i++) {
  435. if ($i == $page) {
  436. echo "<a class=\"current\">$i</a>";
  437. } else {
  438. echo "<a href=\"{$pageName}Page=$i\">$i</a>";
  439. }
  440. }
  441. if ($page < $totalPages) {
  442. if ($totalPages - $page > $pageLen) {
  443. echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
  444. }
  445. echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
  446. echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
  447. }
  448. }
  449. ?>
  450. </div>
  451. </div>
  452. <script>
  453. $(document).ready(function() {
  454. // 添加日期验证逻辑
  455. $('input[name="fliterToDate"]').on('change', function() {
  456. var fromDate = $('input[name="fliterFromDate"]').val();
  457. var toDate = $(this).val();
  458. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  459. alert('结束日期不能早于开始日期');
  460. $(this).val(''); // 清空结束日期
  461. return false;
  462. }
  463. });
  464. // 开始日期变更时也进行验证
  465. $('input[name="fliterFromDate"]').on('change', function() {
  466. var fromDate = $(this).val();
  467. var toDate = $('input[name="fliterToDate"]').val();
  468. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  469. alert('开始日期不能晚于结束日期');
  470. $('input[name="fliterToDate"]').val(''); // 清空结束日期
  471. return false;
  472. }
  473. });
  474. // 处理筛选条件改变
  475. $('.filterSearch').change(function() {
  476. var url = '?';
  477. var keys = $('#keys').val();
  478. if (keys && keys != '请输入客户名称或编码') {
  479. url += 'Keys=' + encodeURIComponent(keys) + '&';
  480. }
  481. $('.filterSearch').each(function() {
  482. var name = $(this).attr('name');
  483. var value = $(this).val();
  484. if (value) {
  485. url += name + '=' + encodeURIComponent(value) + '&';
  486. }
  487. });
  488. // 移除末尾的&
  489. if (url.endsWith('&')) {
  490. url = url.substring(0, url.length - 1);
  491. }
  492. location.href = url;
  493. });
  494. // 添加展开详情的点击事件处理
  495. $('.toggleDetail').click(function() {
  496. var customerId = $(this).data('id');
  497. var detailPanel = $('#detail-' + customerId);
  498. if (detailPanel.is(':visible')) {
  499. detailPanel.slideUp();
  500. $(this).text('展开详情');
  501. } else {
  502. detailPanel.slideDown();
  503. $(this).text('收起详情');
  504. }
  505. });
  506. });
  507. </script>
  508. </div>
  509. </body>
  510. </html>