rebate_expiring.php 18 KB

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