rebate_redeem.php 14 KB

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