rebate_summary.php 19 KB

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