rebate_summary.php 18 KB

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