rebate_redeem.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 获取客户ID
  5. $customerId = isset($_GET['customer_id']) ? intval($_GET['customer_id']) : 0;
  6. if ($customerId <= 0) {
  7. echo "<script>alert('请选择一个有效的客户'); window.location.href='rebate_summary.php';</script>";
  8. exit;
  9. }
  10. // 获取客户信息
  11. $customerQuery = $conn->query("SELECT c.id, c.cs_code, c.cs_company
  12. FROM customer c
  13. WHERE c.id = " . $customerId);
  14. if (!$customerQuery || $customerQuery->num_rows == 0) {
  15. echo "<script>alert('找不到指定的客户'); window.location.href='rebate_summary.php';</script>";
  16. exit;
  17. }
  18. $customerInfo = $customerQuery->fetch_assoc();
  19. // 获取可兑换的返点信息 - 从订单项目中获取
  20. $rebateQuery = "
  21. SELECT
  22. o.id AS order_id,
  23. o.order_code,
  24. o.order_date,
  25. oi.id AS order_item_id,
  26. oi.product_id,
  27. p.ProductName,
  28. oi.quantity,
  29. oi.unit,
  30. (
  31. SELECT rr.id
  32. FROM rebate_rules rr
  33. WHERE rr.product_id = oi.product_id
  34. AND rr.min_quantity <= (
  35. SELECT SUM(oi2.quantity)
  36. FROM order_items oi2
  37. JOIN orders o2 ON oi2.order_id = o2.id
  38. WHERE o2.customer_id = o.customer_id
  39. AND o2.is_deleted = 0
  40. AND oi2.is_deleted = 0
  41. AND o2.order_type = 1
  42. AND oi2.product_id = oi.product_id
  43. AND o2.order_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')
  44. AND NOT EXISTS (
  45. SELECT 1
  46. FROM rebate_redemption_items rri
  47. WHERE rri.order_item_id = oi2.id
  48. )
  49. )
  50. ORDER BY rr.min_quantity DESC
  51. LIMIT 1
  52. ) AS rebate_rule_id,
  53. (
  54. SELECT rr.rebate_amount
  55. FROM rebate_rules rr
  56. WHERE rr.product_id = oi.product_id
  57. AND rr.min_quantity <= (
  58. SELECT SUM(oi2.quantity)
  59. FROM order_items oi2
  60. JOIN orders o2 ON oi2.order_id = o2.id
  61. WHERE o2.customer_id = o.customer_id
  62. AND o2.is_deleted = 0
  63. AND oi2.is_deleted = 0
  64. AND o2.order_type = 1
  65. AND oi2.product_id = oi.product_id
  66. AND o2.order_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')
  67. AND NOT EXISTS (
  68. SELECT 1
  69. FROM rebate_redemption_items rri
  70. WHERE rri.order_item_id = oi2.id
  71. )
  72. )
  73. ORDER BY rr.min_quantity DESC
  74. LIMIT 1
  75. ) AS rebate_amount,
  76. oi.quantity * (
  77. SELECT rr.rebate_amount
  78. FROM rebate_rules rr
  79. WHERE rr.product_id = oi.product_id
  80. AND rr.min_quantity <= (
  81. SELECT SUM(oi2.quantity)
  82. FROM order_items oi2
  83. JOIN orders o2 ON oi2.order_id = o2.id
  84. WHERE o2.customer_id = o.customer_id
  85. AND o2.is_deleted = 0
  86. AND oi2.is_deleted = 0
  87. AND o2.order_type = 1
  88. AND oi2.product_id = oi.product_id
  89. AND o2.order_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')
  90. AND NOT EXISTS (
  91. SELECT 1
  92. FROM rebate_redemption_items rri
  93. WHERE rri.order_item_id = oi2.id
  94. )
  95. )
  96. ORDER BY rr.min_quantity DESC
  97. LIMIT 1
  98. ) AS item_rebate_total
  99. FROM
  100. orders o
  101. JOIN
  102. order_items oi ON o.id = oi.order_id
  103. JOIN
  104. products p ON oi.product_id = p.id
  105. WHERE
  106. o.customer_id = $customerId
  107. AND o.is_deleted = 0
  108. AND oi.is_deleted = 0
  109. AND o.order_type = 1
  110. AND o.no_cashback = 0
  111. AND o.order_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')
  112. AND p.rebate = 1
  113. AND NOT EXISTS (
  114. SELECT 1
  115. FROM rebate_redemption_items rri
  116. WHERE rri.order_item_id = oi.id
  117. )
  118. -- 确保该订单项有返点规则可用
  119. AND EXISTS (
  120. SELECT 1
  121. FROM rebate_rules rr
  122. WHERE rr.product_id = oi.product_id
  123. AND rr.min_quantity <= (
  124. SELECT SUM(oi2.quantity)
  125. FROM order_items oi2
  126. JOIN orders o2 ON oi2.order_id = o2.id
  127. WHERE o2.customer_id = o.customer_id
  128. AND o2.is_deleted = 0
  129. AND oi2.is_deleted = 0
  130. AND o2.order_type = 1
  131. AND oi2.product_id = oi.product_id
  132. AND o2.order_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')
  133. AND NOT EXISTS (
  134. SELECT 1
  135. FROM rebate_redemption_items rri
  136. WHERE rri.order_item_id = oi2.id
  137. )
  138. )
  139. )
  140. ORDER BY
  141. o.order_date DESC, p.ProductName ASC";
  142. $rebateResult = $conn->query($rebateQuery);
  143. if (!$rebateResult) {
  144. echo "<script>alert('获取返点信息失败: " . $conn->error . "'); window.location.href='rebate_summary.php';</script>";
  145. exit;
  146. }
  147. // 计算总返点金额
  148. $totalRebateAmount = 0;
  149. $rebateItems = [];
  150. while ($row = $rebateResult->fetch_assoc()) {
  151. $rebateItems[] = $row;
  152. $totalRebateAmount += floatval($row['item_rebate_total']);
  153. }
  154. // 如果没有可兑换的返点,返回列表页
  155. if (count($rebateItems) == 0) {
  156. echo "<script>alert('该客户当前没有可兑换的返点'); window.location.href='rebate_summary.php';</script>";
  157. exit;
  158. }
  159. ?>
  160. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  161. <html xmlns="http://www.w3.org/1999/xhtml">
  162. <head>
  163. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  164. <title>返点兑换</title>
  165. <link rel="stylesheet" href="css/common.css" type="text/css" />
  166. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  167. <script src="js/jquery-1.7.2.min.js"></script>
  168. <script src="js/js.js"></script>
  169. <style>
  170. body {
  171. margin: 0;
  172. padding: 20px;
  173. background: #fff;
  174. }
  175. #man_zone {
  176. margin-left: 0;
  177. }
  178. .rebate-item-row {
  179. position: relative;
  180. padding: 12px 15px;
  181. margin-bottom: 8px;
  182. border-radius: 4px;
  183. display: flex;
  184. align-items: center;
  185. flex-wrap: wrap;
  186. gap: 15px;
  187. background-color: #f9f9f9;
  188. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  189. }
  190. .rebate-item-row:hover {
  191. background-color: #f0f0f0;
  192. }
  193. .order-code {
  194. flex: 1;
  195. min-width: 100px;
  196. }
  197. .order-date {
  198. flex: 1;
  199. min-width: 120px;
  200. }
  201. .product-name {
  202. flex: 2;
  203. min-width: 150px;
  204. }
  205. .item-quantity {
  206. flex: 0.8;
  207. min-width: 80px;
  208. text-align: center;
  209. }
  210. .rebate-rate {
  211. flex: 0.8;
  212. min-width: 100px;
  213. text-align: right;
  214. }
  215. .item-total {
  216. flex: 1;
  217. min-width: 100px;
  218. text-align: right;
  219. font-weight: bold;
  220. color: #e74c3c;
  221. }
  222. .customer-info {
  223. background-color: #f5f5f5;
  224. padding: 15px;
  225. border-radius: 5px;
  226. margin-bottom: 20px;
  227. border: 1px solid #ddd;
  228. display: flex;
  229. justify-content: space-between;
  230. align-items: center;
  231. }
  232. .customer-info .customer-details {
  233. flex: 1;
  234. }
  235. .customer-info h2 {
  236. margin-top: 0;
  237. margin-bottom: 10px;
  238. font-size: 18px;
  239. color: #333;
  240. }
  241. .customer-info p {
  242. margin: 5px 0;
  243. color: #555;
  244. }
  245. .customer-info .actions {
  246. text-align: right;
  247. }
  248. .btn-history {
  249. background-color: #3498db;
  250. color: white;
  251. border: none;
  252. padding: 8px 16px;
  253. font-size: 14px;
  254. border-radius: 4px;
  255. cursor: pointer;
  256. text-decoration: none;
  257. display: inline-block;
  258. }
  259. .btn-history:hover {
  260. background-color: #2980b9;
  261. }
  262. .list-header {
  263. display: flex;
  264. background-color: #eee;
  265. padding: 10px 15px;
  266. margin-bottom: 10px;
  267. border-radius: 4px;
  268. font-weight: bold;
  269. color: #555;
  270. font-size: 13px;
  271. gap: 15px;
  272. }
  273. .total-section {
  274. margin-top: 20px;
  275. padding: 15px;
  276. background-color: #f5f5f5;
  277. border-radius: 4px;
  278. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  279. }
  280. .btn-redeem {
  281. background-color: #e74c3c;
  282. color: white;
  283. border: none;
  284. padding: 10px 20px;
  285. font-size: 16px;
  286. border-radius: 4px;
  287. cursor: pointer;
  288. margin-top: 20px;
  289. }
  290. .btn-redeem:hover {
  291. background-color: #c0392b;
  292. }
  293. .btn-cancel {
  294. background-color: #7f8c8d;
  295. color: white;
  296. border: none;
  297. padding: 10px 20px;
  298. font-size: 16px;
  299. border-radius: 4px;
  300. cursor: pointer;
  301. margin-right: 10px;
  302. }
  303. .btn-cancel:hover {
  304. background-color: #6c7a7a;
  305. }
  306. .rebate-notes {
  307. margin-top: 15px;
  308. width: 100%;
  309. padding: 8px;
  310. border: 1px solid #ddd;
  311. border-radius: 4px;
  312. resize: vertical;
  313. min-height: 60px;
  314. }
  315. @media (max-width: 768px) {
  316. .rebate-item-row {
  317. flex-direction: column;
  318. align-items: flex-start;
  319. }
  320. .list-header {
  321. display: none;
  322. }
  323. .order-code, .order-date, .product-name, .item-quantity,
  324. .rebate-rate, .item-total {
  325. width: 100%;
  326. min-width: 100%;
  327. margin-bottom: 5px;
  328. }
  329. }
  330. </style>
  331. </head>
  332. <body>
  333. <div id="man_zone">
  334. <div class="customer-info">
  335. <div class="customer-details">
  336. <h2>客户返点兑换</h2>
  337. <p><strong>客户编码:</strong> <?= htmlspecialcharsFix($customerInfo['cs_code']) ?></p>
  338. <p><strong>客户名称:</strong> <?= htmlspecialcharsFix($customerInfo['cs_company']) ?></p>
  339. </div>
  340. <div class="actions">
  341. <a href="rebate_history.php" class="btn-history">查看返点历史</a>
  342. </div>
  343. </div>
  344. <form name="redeemForm" id="redeemForm" method="post" action="rebate_redeem_save.php" onsubmit="return validateRedeemForm()">
  345. <input type="hidden" name="customer_id" value="<?= $customerId ?>">
  346. <input type="hidden" name="total_rebate_amount" id="total-rebate-amount" value="<?= $totalRebateAmount ?>">
  347. <div class="list-header">
  348. <div class="order-code">订单编号</div>
  349. <div class="order-date">订单日期</div>
  350. <div class="product-name">产品名称</div>
  351. <div class="item-quantity">数量</div>
  352. <div class="rebate-rate">返点单价</div>
  353. <div class="item-total">返点金额</div>
  354. </div>
  355. <div id="rebate-items-container">
  356. <?php foreach ($rebateItems as $index => $item): ?>
  357. <div class="rebate-item-row">
  358. <!-- 隐藏字段,所有项目都会被提交 -->
  359. <input type="hidden" name="items[<?= $index ?>][selected]" value="1">
  360. <input type="hidden" name="items[<?= $index ?>][order_id]" value="<?= $item['order_id'] ?>">
  361. <input type="hidden" name="items[<?= $index ?>][order_item_id]" value="<?= $item['order_item_id'] ?>">
  362. <input type="hidden" name="items[<?= $index ?>][product_id]" value="<?= $item['product_id'] ?>">
  363. <input type="hidden" name="items[<?= $index ?>][quantity]" value="<?= $item['quantity'] ?>">
  364. <input type="hidden" name="items[<?= $index ?>][rebate_amount]" value="<?= $item['rebate_amount'] ?>">
  365. <input type="hidden" name="items[<?= $index ?>][rebate_rule_id]" value="<?= $item['rebate_rule_id'] ?>">
  366. <input type="hidden" name="items[<?= $index ?>][item_rebate_total]" value="<?= $item['item_rebate_total'] ?>">
  367. <div class="order-code"><?= htmlspecialcharsFix($item['order_code']) ?></div>
  368. <div class="order-date"><?= date('Y-m-d', strtotime($item['order_date'])) ?></div>
  369. <div class="product-name"><?= htmlspecialcharsFix($item['ProductName']) ?></div>
  370. <div class="item-quantity"><?= $item['quantity'] ?> <?= $item['unit'] ?></div>
  371. <div class="rebate-rate"><?= number_format($item['rebate_amount'], 2) ?> 元/件</div>
  372. <div class="item-total"><?= number_format($item['item_rebate_total'], 2) ?> 元</div>
  373. </div>
  374. <?php endforeach; ?>
  375. </div>
  376. <div class="total-section">
  377. <div style="display: flex; justify-content: space-between; align-items: center;">
  378. <div>
  379. <label for="notes">兑换备注:</label>
  380. <textarea name="notes" id="notes" class="rebate-notes" placeholder="可选备注内容..."></textarea>
  381. </div>
  382. <div style="display: flex; flex-direction: column; align-items: flex-end;">
  383. <div style="font-size: 16px; margin-bottom: 10px;">
  384. <label>总返点金额:</label>
  385. <span id="total-rebate-display" style="font-size: 18px; font-weight: bold; color: #e74c3c;"><?= number_format($totalRebateAmount, 2) ?> 元</span>
  386. </div>
  387. <div>
  388. <button type="button" class="btn-cancel" onclick="window.location.href='rebate_summary.php'">取消</button>
  389. <button type="submit" class="btn-redeem">确认兑换</button>
  390. </div>
  391. </div>
  392. </div>
  393. </div>
  394. </form>
  395. <script>
  396. // 验证表单
  397. function validateRedeemForm() {
  398. return confirm('确定要兑换所有返点项目吗?此操作不可逆!');
  399. }
  400. </script>
  401. </div>
  402. </body>
  403. </html>