rebate_expiring.php 19 KB

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