rebate_expiring.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 辅助函数
  5. $urlStr = '';
  6. // 处理筛选条件
  7. $fliterFromDate = $_GET['fliterFromDate'] ?? '';
  8. $fliterToDate = $_GET['fliterToDate'] ?? '';
  9. $expiryDays = $_GET['expiryDays'] ?? 7; // 默认显示7天内过期的返点
  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. // 查询即将过期的返点订单
  27. // 这里我们获取那些在90天期限内,但是已经接近过期的订单(比如只剩7天有效期)
  28. $cutoffDate = date('Y-m-d', strtotime("-" . (90 - $expiryDays) . " days"));
  29. $expiryDate = date('Y-m-d', strtotime("-90 days"));
  30. $customerListSql = "SELECT DISTINCT
  31. o.customer_id,
  32. c.cs_company AS customer_name,
  33. c.cs_code,
  34. MIN(o.order_date) AS oldest_order_date,
  35. DATE_ADD(MIN(o.order_date), INTERVAL 90 DAY) AS expiry_date,
  36. DATEDIFF(DATE_ADD(MIN(o.order_date), INTERVAL 90 DAY), CURRENT_DATE()) AS days_left
  37. FROM orders o
  38. JOIN order_items oi ON o.id = oi.order_id
  39. JOIN customer c ON o.customer_id = c.id
  40. JOIN products p ON oi.product_id = p.id
  41. WHERE
  42. o.order_date BETWEEN '$expiryDate' AND '$cutoffDate'
  43. AND o.order_type = 1
  44. AND p.rebate = 1
  45. AND NOT EXISTS (
  46. SELECT 1
  47. FROM rebate_redemption_items rri
  48. WHERE rri.order_item_id = oi.id
  49. )
  50. AND EXISTS (
  51. SELECT 1
  52. FROM rebate_rules rr
  53. WHERE rr.product_id = oi.product_id
  54. )
  55. AND (
  56. SELECT SUM(oi2.quantity)
  57. FROM order_items oi2
  58. JOIN orders o2 ON oi2.order_id = o2.id
  59. WHERE o2.customer_id = o.customer_id
  60. AND oi2.product_id = oi.product_id
  61. AND o2.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  62. AND NOT EXISTS (
  63. SELECT 1
  64. FROM rebate_redemption_items rri
  65. WHERE rri.order_item_id = oi2.id
  66. )
  67. ) >= (
  68. SELECT MIN(rr.min_quantity)
  69. FROM rebate_rules rr
  70. WHERE rr.product_id = oi.product_id
  71. )";
  72. // 非管理员只能查看自己的客户返点
  73. if (!$isAdmin) {
  74. $customerListSql .= " AND c.cs_belong = $employee_id";
  75. }
  76. // 添加搜索条件
  77. if (!empty($keyscode)) {
  78. $customerListSql .= " AND (c.cs_company LIKE '%$keyscode%' OR c.cs_code LIKE '%$keyscode%')";
  79. }
  80. // 添加日期筛选
  81. $customerListSql .= $fliterStr;
  82. // 分组并按剩余天数排序
  83. $customerListSql .= " GROUP BY o.customer_id, c.cs_company, c.cs_code ORDER BY days_left ASC";
  84. // 执行查询获取客户列表
  85. $result = mysqli_query($conn, $customerListSql);
  86. if (!$result) {
  87. die("查询即将过期返点错误: " . mysqli_error($conn));
  88. }
  89. // 获取客户列表
  90. $customers = [];
  91. while ($row = mysqli_fetch_assoc($result)) {
  92. $customers[] = $row;
  93. }
  94. // 设置每页显示记录数和分页
  95. $pageSize = 20;
  96. $totalRecords = count($customers);
  97. // 计算总页数
  98. $totalPages = ceil($totalRecords / $pageSize);
  99. if ($totalPages < 1) $totalPages = 1;
  100. // 验证当前页码
  101. $page = (int)$page;
  102. if ($page < 1) $page = 1;
  103. if ($page > $totalPages) $page = $totalPages;
  104. // 分页处理
  105. $paginatedCustomers = array_slice($customers, ($page - 1) * $pageSize, $pageSize);
  106. // 获取每个客户的返点金额
  107. foreach ($paginatedCustomers as &$customer) {
  108. $customer_id = $customer['customer_id'];
  109. // 计算客户的总返点金额
  110. $rebateAmountSql = "
  111. SELECT
  112. SUM(
  113. oi.quantity * (
  114. SELECT rr.rebate_amount
  115. FROM rebate_rules rr
  116. WHERE rr.product_id = oi.product_id
  117. AND rr.min_quantity <= (
  118. SELECT SUM(oi2.quantity)
  119. FROM order_items oi2
  120. JOIN orders o2 ON oi2.order_id = o2.id
  121. WHERE o2.customer_id = o.customer_id
  122. AND o2.order_type = 1
  123. AND oi2.product_id = oi.product_id
  124. AND o2.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  125. AND NOT EXISTS (
  126. SELECT 1
  127. FROM rebate_redemption_items rri
  128. WHERE rri.order_item_id = oi2.id
  129. )
  130. )
  131. ORDER BY rr.min_quantity DESC
  132. LIMIT 1
  133. )
  134. ) AS total_rebate_amount,
  135. COUNT(DISTINCT oi.product_id) AS qualifying_products
  136. FROM
  137. orders o
  138. JOIN
  139. order_items oi ON o.id = oi.order_id
  140. JOIN
  141. products p ON oi.product_id = p.id
  142. WHERE
  143. o.customer_id = $customer_id
  144. AND o.order_type = 1
  145. AND o.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
  146. AND p.rebate = 1
  147. AND NOT EXISTS (
  148. SELECT 1
  149. FROM rebate_redemption_items rri
  150. WHERE rri.order_item_id = oi.id
  151. )";
  152. $amountResult = mysqli_query($conn, $rebateAmountSql);
  153. if (!$amountResult) {
  154. die("计算返点金额错误: " . mysqli_error($conn));
  155. }
  156. $amountRow = mysqli_fetch_assoc($amountResult);
  157. $customer['total_rebate_amount'] = $amountRow['total_rebate_amount'] ?? 0;
  158. $customer['qualifying_products'] = $amountRow['qualifying_products'] ?? 0;
  159. }
  160. ?>
  161. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  162. <html xmlns="http://www.w3.org/1999/xhtml">
  163. <head>
  164. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  165. <title>即将过期返点提醒</title>
  166. <link rel="stylesheet" href="css/common.css" type="text/css" />
  167. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  168. <script src="js/jquery-1.7.2.min.js"></script>
  169. <script src="js/js.js"></script>
  170. <style>
  171. body {
  172. margin: 0;
  173. padding: 20px;
  174. background: #fff;
  175. }
  176. #man_zone {
  177. margin-left: 0;
  178. }
  179. /* 表格布局 */
  180. .table2 {
  181. width: 100%;
  182. }
  183. .theader, .tline {
  184. display: flex;
  185. flex-direction: row;
  186. align-items: center;
  187. width: 100%;
  188. border-bottom: 1px solid #ddd;
  189. }
  190. .theader {
  191. background-color: #f2f2f2;
  192. font-weight: bold;
  193. height: 40px;
  194. }
  195. .tline {
  196. height: 45px;
  197. }
  198. .tline:hover {
  199. background-color: #f5f5f5;
  200. }
  201. .col2 { width: 5%; text-align: center; }
  202. .col3 { width: 15%; }
  203. .col4 { width: 20%; }
  204. .col5 { width: 12%; text-align: center; }
  205. .col6 { width: 12%; text-align: center; }
  206. .col7 { width: 8%; text-align: center; }
  207. .col8 { width: 12%; text-align: right; }
  208. .col9 { width: 16%; text-align: center; }
  209. /* 表格布局修复 */
  210. .table2 .col2 { width: 5%; text-align: center; }
  211. .table2 .col3 { width: 15%; }
  212. .table2 .col4 { width: 20%; }
  213. .table2 .col5 { width: 12%; text-align: center; }
  214. .table2 .col6 { width: 12%; text-align: center; }
  215. .table2 .col7 { width: 8%; text-align: center; }
  216. .table2 .col8 { width: 12%; text-align: right; }
  217. .table2 .col9 { width: 16%; text-align: center; }
  218. .theader > div, .tline > div {
  219. padding: 0 5px;
  220. overflow: hidden;
  221. text-overflow: ellipsis;
  222. white-space: nowrap;
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. }
  227. .col3, .col4 {
  228. justify-content: flex-start !important;
  229. }
  230. .col8 {
  231. justify-content: flex-end !important;
  232. }
  233. /* 日期选择器样式 */
  234. .date-input {
  235. padding: 5px;
  236. border: 1px solid #ccc;
  237. border-radius: 3px;
  238. }
  239. /* 警告信息样式 */
  240. .expiry-warning {
  241. color: #ff0000;
  242. font-weight: bold;
  243. }
  244. .days-left-critical {
  245. background-color: #ffeeee;
  246. }
  247. .days-left-warning {
  248. background-color: #fff5e6;
  249. }
  250. /* 选择框样式 */
  251. .select-days {
  252. padding: 5px;
  253. border: 1px solid #ccc;
  254. border-radius: 3px;
  255. margin-left: 5px;
  256. }
  257. /* 顶部提示样式 */
  258. .alert-box {
  259. background-color: #f8d7da;
  260. color: #721c24;
  261. padding: 15px;
  262. margin-bottom: 20px;
  263. border: 1px solid #f5c6cb;
  264. border-radius: 4px;
  265. }
  266. .fastSelect .selectItem
  267. {
  268. width: 50%;
  269. }
  270. .inputSearch {
  271. display: flex;
  272. align-items: center;
  273. }
  274. .inputTxt {
  275. padding: 5px;
  276. border: 1px solid #ccc;
  277. border-radius: 3px;
  278. margin-right: 5px;
  279. width: 180px;
  280. }
  281. .searchgo {
  282. padding: 5px 15px;
  283. background: #3498db;
  284. color: white;
  285. border: none;
  286. border-radius: 3px;
  287. cursor: pointer;
  288. }
  289. .searchgo:hover {
  290. background: #2980b9;
  291. }
  292. .action-buttons {
  293. width: 100%;
  294. display: flex;
  295. justify-content: flex-end;
  296. margin-top: 10px;
  297. }
  298. </style>
  299. </head>
  300. <body>
  301. <div id="man_zone">
  302. <div class="alert-box">
  303. <strong>注意!</strong> 此页面显示即将过期的返点信息。返点订单在创建后90天内有效,过期后将无法兑换。请尽快处理以下客户的返点兑换。
  304. </div>
  305. <div class="fastSelect clear">
  306. <H1>筛选条件</H1>
  307. <div class="selectItem">
  308. <label>订单日期</label>
  309. <input type="date" name="fliterFromDate" class="date-input filterSearch" value="<?= $fliterFromDate ?>">
  310. <label>到</label>
  311. <input type="date" name="fliterToDate" class="date-input filterSearch" value="<?= $fliterToDate ?>">
  312. <label>过期天数</label>
  313. <select name="expiryDays" class="select-days filterSearch">
  314. <option value="3" <?= $expiryDays == 3 ? 'selected' : '' ?>>3天内</option>
  315. <option value="7" <?= $expiryDays == 7 ? 'selected' : '' ?>>7天内</option>
  316. <option value="14" <?= $expiryDays == 14 ? 'selected' : '' ?>>14天内</option>
  317. <option value="30" <?= $expiryDays == 30 ? 'selected' : '' ?>>30天内</option>
  318. </select>
  319. </div>
  320. <div class="inputSearch">
  321. <input type="text" id="keys" class="inputTxt" placeholder="请输入客户名称或编码"
  322. value="<?= empty($keyscode) ? '' : $keyscode ?>" />
  323. <input type="button" id="searchgo" class="searchgo" value="搜索"
  324. onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)+'&expiryDays='+document.getElementsByName('expiryDays')[0].value" />
  325. </div>
  326. </div>
  327. <div align="right" style="margin-bottom: 10px;">
  328. <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; margin-right: 5px;">查看返点历史</a>
  329. <a href="rebate_summary.php" class="btn1" style="display: inline-flex; align-items: center; justify-content: center; padding: 5px 15px; margin-top: 0; height: 22px;">返回返点统计</a>
  330. </div>
  331. <div class="table2 em<?= $_SESSION['employee_id'] ?>">
  332. <div class="theader">
  333. <div class="col2">序号</div>
  334. <div class="col3">客户编码</div>
  335. <div class="col4">客户名称</div>
  336. <div class="col5">最早订单日期</div>
  337. <div class="col6">过期日期</div>
  338. <div class="col7">剩余天数</div>
  339. <div class="col8">返点金额</div>
  340. <div class="col9">操作</div>
  341. </div>
  342. <?php
  343. if (!empty($paginatedCustomers)) {
  344. $tempNum = ($page - 1) * $pageSize;
  345. foreach ($paginatedCustomers as $customer) {
  346. $tempNum++;
  347. $daysLeft = $customer['days_left'];
  348. $rowClass = '';
  349. // 根据剩余天数添加不同的警告样式
  350. if ($daysLeft <= 3) {
  351. $rowClass = 'days-left-critical';
  352. } elseif ($daysLeft <= 7) {
  353. $rowClass = 'days-left-warning';
  354. }
  355. ?>
  356. <div class="tline <?= $rowClass ?>">
  357. <div class="col2"><?= $tempNum ?></div>
  358. <div class="col3"><?= htmlspecialcharsFix($customer['cs_code']) ?></div>
  359. <div class="col4"><?= htmlspecialcharsFix($customer['customer_name']) ?></div>
  360. <div class="col5"><?= date('Y-m-d', strtotime($customer['oldest_order_date'])) ?></div>
  361. <div class="col6"><?= date('Y-m-d', strtotime($customer['expiry_date'])) ?></div>
  362. <div class="col7 <?= $daysLeft <= 3 ? 'expiry-warning' : '' ?>"><?= $daysLeft ?> 天</div>
  363. <div class="col8"><?= number_format($customer['total_rebate_amount'], 2) ?> 元</div>
  364. <div class="col9">
  365. <a href="rebate_redeem.php?customer_id=<?= $customer['customer_id'] ?>" class="ico_edit ico">立即处理兑换</a>
  366. </div>
  367. </div>
  368. <?php
  369. }
  370. } else {
  371. echo '<div class="tline"><div style="width: 100%; text-align: center;">目前没有即将过期的返点订单</div></div>';
  372. }
  373. ?>
  374. <div class="showpagebox">
  375. <?php
  376. if ($totalPages > 1) {
  377. $pageName = "?Keys=$keys$urlStr&expiryDays=$expiryDays&";
  378. $pageLen = 3;
  379. if ($page > 1) {
  380. echo "<a href=\"{$pageName}Page=1\">首页</a>";
  381. echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
  382. }
  383. if ($pageLen * 2 + 1 >= $totalPages) {
  384. $startPage = 1;
  385. $endPage = $totalPages;
  386. } else {
  387. if ($page <= $pageLen + 1) {
  388. $startPage = 1;
  389. $endPage = $pageLen * 2 + 1;
  390. } else {
  391. $startPage = $page - $pageLen;
  392. $endPage = $page + $pageLen;
  393. }
  394. if ($page + $pageLen > $totalPages) {
  395. $startPage = $totalPages - $pageLen * 2;
  396. $endPage = $totalPages;
  397. }
  398. }
  399. for ($i = $startPage; $i <= $endPage; $i++) {
  400. if ($i == $page) {
  401. echo "<a class=\"current\">$i</a>";
  402. } else {
  403. echo "<a href=\"{$pageName}Page=$i\">$i</a>";
  404. }
  405. }
  406. if ($page < $totalPages) {
  407. if ($totalPages - $page > $pageLen) {
  408. echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
  409. }
  410. echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
  411. echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
  412. }
  413. }
  414. ?>
  415. </div>
  416. </div>
  417. <script>
  418. $(document).ready(function() {
  419. // 添加日期验证逻辑
  420. $('input[name="fliterToDate"]').on('change', function() {
  421. var fromDate = $('input[name="fliterFromDate"]').val();
  422. var toDate = $(this).val();
  423. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  424. alert('结束日期不能早于开始日期');
  425. $(this).val(''); // 清空结束日期
  426. return false;
  427. }
  428. });
  429. // 开始日期变更时也进行验证
  430. $('input[name="fliterFromDate"]').on('change', function() {
  431. var fromDate = $(this).val();
  432. var toDate = $('input[name="fliterToDate"]').val();
  433. if (fromDate && toDate && new Date(toDate) < new Date(fromDate)) {
  434. alert('开始日期不能晚于结束日期');
  435. $('input[name="fliterToDate"]').val(''); // 清空结束日期
  436. return false;
  437. }
  438. });
  439. // 处理筛选条件改变
  440. $('.filterSearch').change(function() {
  441. var url = '?';
  442. var keys = $('#keys').val();
  443. if (keys && keys != '请输入客户名称或编码') {
  444. url += 'Keys=' + encodeURIComponent(keys) + '&';
  445. }
  446. url += 'expiryDays=' + $('select[name="expiryDays"]').val() + '&';
  447. $('.date-input.filterSearch').each(function() {
  448. var name = $(this).attr('name');
  449. var value = $(this).val();
  450. if (value) {
  451. url += name + '=' + encodeURIComponent(value) + '&';
  452. }
  453. });
  454. // 移除末尾的&
  455. if (url.endsWith('&')) {
  456. url = url.substring(0, url.length - 1);
  457. }
  458. location.href = url;
  459. });
  460. });
  461. </script>
  462. </div>
  463. </body>
  464. </html>