rebate_redeem.php 14 KB

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