order_edit.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. $id = $_GET['id'] ?? '';
  5. $page = $_GET['Page'] ?? '';
  6. $keys = urlencode($_GET['Keys'] ?? '');
  7. $hrefstr = "keys=$keys&Page=$page";
  8. // 验证并获取订单数据
  9. if (!empty($id) && is_numeric($id)) {
  10. // 获取订单基本信息
  11. $employee_id = $_SESSION['employee_id'];
  12. $sql = "SELECT o.*, c.cs_company, c.cs_code, cc.contact_name
  13. FROM orders o
  14. LEFT JOIN customer c ON o.customer_id = c.id
  15. LEFT JOIN customer_contact cc ON o.contact_id = cc.id
  16. WHERE o.id = $id AND o.employee_id = $employee_id";
  17. $result = mysqli_query($conn, $sql);
  18. if ($row = mysqli_fetch_assoc($result)) {
  19. $order = $row;
  20. } else {
  21. echo "<script>alert('订单不存在或您没有权限查看');history.back();</script>";
  22. exit;
  23. }
  24. // 获取订单项信息
  25. $sql = "SELECT oi.*, p.ProductName, pc.name as category_name,
  26. ps.id as product_spec_id, ps.spec_name, ps.spec_value,
  27. oi.specification_id /* 显式获取数据库中的specification_id */
  28. FROM order_items oi
  29. LEFT JOIN products p ON oi.product_id = p.id
  30. LEFT JOIN product_categories pc ON p.category_id = pc.id
  31. LEFT JOIN product_specifications ps ON oi.specification_id = ps.id
  32. WHERE oi.order_id = $id";
  33. $itemsResult = mysqli_query($conn, $sql);
  34. $orderItems = [];
  35. while ($itemRow = mysqli_fetch_assoc($itemsResult)) {
  36. // 调试输出
  37. error_log("订单项数据: " . print_r($itemRow, true));
  38. $orderItems[] = $itemRow;
  39. }
  40. } else {
  41. echo "<script>alert('订单不存在!');history.back();</script>";
  42. exit;
  43. }
  44. ?>
  45. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  46. <html xmlns="http://www.w3.org/1999/xhtml">
  47. <head>
  48. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  49. <title>编辑订单</title>
  50. <link rel="stylesheet" href="css/common.css" type="text/css" />
  51. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  52. <script src="js/jquery-1.7.2.min.js"></script>
  53. <script src="js/js.js"></script>
  54. <style>
  55. body {
  56. margin: 0;
  57. padding: 20px;
  58. background: #fff;
  59. }
  60. #man_zone {
  61. margin-left: 0;
  62. }
  63. .product-row {
  64. position: relative;
  65. padding: 12px 15px;
  66. padding-right: 40px; /* Add right padding to make room for delete button */
  67. margin-bottom: 8px;
  68. border-radius: 4px;
  69. display: flex;
  70. align-items: center;
  71. flex-wrap: wrap;
  72. gap: 20px; /* 增加间距 */
  73. background-color: #f9f9f9;
  74. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  75. }
  76. .delete-product {
  77. position: absolute;
  78. right: 10px;
  79. top: 50%;
  80. transform: translateY(-50%);
  81. color: #e74c3c;
  82. font-weight: bold;
  83. font-size: 18px;
  84. cursor: pointer;
  85. width: 24px;
  86. height: 24px;
  87. line-height: 24px;
  88. text-align: center;
  89. border-radius: 50%;
  90. z-index: 5;
  91. }
  92. .delete-product:hover {
  93. background-color: #e74c3c;
  94. color: white;
  95. }
  96. .product-info {
  97. flex: 3;
  98. min-width: 200px;
  99. }
  100. .product-spec {
  101. flex: 2;
  102. min-width: 200px;
  103. }
  104. .product-quantity {
  105. flex: 1;
  106. min-width: 80px;
  107. }
  108. .product-unit {
  109. flex: 0.8;
  110. min-width: 60px;
  111. }
  112. .product-price {
  113. flex: 1;
  114. min-width: 100px;
  115. }
  116. .product-total {
  117. flex: 1.2;
  118. min-width: 100px;
  119. font-weight: bold;
  120. }
  121. .product-row input[type="number"] {
  122. width: 90%;
  123. padding: 5px;
  124. border: 1px solid #ddd;
  125. border-radius: 4px;
  126. }
  127. .product-row select {
  128. width: 100%;
  129. padding: 5px;
  130. }
  131. .selected-product-info {
  132. font-weight: bold;
  133. margin-bottom: 3px;
  134. }
  135. .row-section {
  136. display: flex;
  137. flex-direction: column;
  138. }
  139. .row-section-label {
  140. font-size: 12px;
  141. color: #666;
  142. margin-bottom: 3px;
  143. }
  144. .row-section-label span {
  145. display: none; /* 在大屏幕上隐藏标签文本 */
  146. }
  147. .spec-info {
  148. margin-top: 2px;
  149. font-size: 12px;
  150. color: #666;
  151. }
  152. /* 在小屏幕上的响应式布局 */
  153. @media (max-width: 768px) {
  154. .product-row {
  155. flex-direction: column;
  156. align-items: flex-start;
  157. }
  158. .product-info, .product-spec, .product-quantity,
  159. .product-unit, .product-price, .product-total {
  160. width: 100%;
  161. min-width: 100%;
  162. margin-bottom: 8px;
  163. }
  164. .row-section-label span {
  165. display: inline; /* 在小屏幕上显示标签文本 */
  166. }
  167. .product-list-header {
  168. display: none !important; /* 在小屏幕上隐藏表头 */
  169. }
  170. }
  171. .add-product-btn {
  172. background-color: #4CAF50;
  173. color: white;
  174. padding: 10px 15px;
  175. border: none;
  176. border-radius: 4px;
  177. cursor: pointer;
  178. margin-bottom: 15px;
  179. font-size: 14px;
  180. display: flex;
  181. align-items: center;
  182. justify-content: center;
  183. gap: 8px;
  184. transition: background-color 0.2s;
  185. box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  186. }
  187. .add-product-btn:hover {
  188. background-color: #45a049;
  189. }
  190. .total-section {
  191. margin-top: 20px;
  192. padding: 10px;
  193. background-color: #eee;
  194. font-weight: bold;
  195. }
  196. #product-container {
  197. margin-bottom: 20px;
  198. }
  199. .productlist {
  200. display: none;
  201. position: absolute;
  202. background: white;
  203. border: 1px solid #ccc;
  204. max-height: 200px;
  205. overflow-y: auto;
  206. width: 100%;
  207. z-index: 1000;
  208. box-shadow: 0 3px 8px rgba(0,0,0,0.25);
  209. border-radius: 0 0 4px 4px;
  210. top: 100%;
  211. left: 0;
  212. }
  213. .productlist ul {
  214. list-style: none;
  215. padding: 0;
  216. margin: 0;
  217. }
  218. .productlist li {
  219. padding: 10px 12px;
  220. cursor: pointer;
  221. border-bottom: 1px solid #eee;
  222. transition: background-color 0.2s;
  223. }
  224. .productlist li:hover {
  225. background-color: #f0f0f0;
  226. }
  227. .productlist li:last-child {
  228. border-bottom: none;
  229. }
  230. .productinput {
  231. position: relative;
  232. }
  233. .productlist li .category-tag {
  234. color: #777;
  235. font-size: 12px;
  236. font-style: italic;
  237. margin-left: 5px;
  238. }
  239. /* Specification select styling */
  240. .spec-select {
  241. width: 100%;
  242. padding: 6px;
  243. margin-top: 5px;
  244. border: 1px solid #ccc;
  245. border-radius: 4px;
  246. }
  247. .spec-select:disabled {
  248. background-color: #f9f9f9;
  249. }
  250. .spec-price {
  251. font-weight: bold;
  252. color: #d9534f;
  253. }
  254. /* 客户选择样式 */
  255. .customer-search-container {
  256. display: flex;
  257. align-items: center;
  258. margin-bottom: 10px;
  259. position: relative;
  260. width: 60%;
  261. }
  262. .customer-dropdown {
  263. display: none;
  264. position: absolute;
  265. background: white;
  266. border: 1px solid #ccc;
  267. max-height: 200px;
  268. overflow-y: auto;
  269. width: 100%;
  270. z-index: 1000;
  271. box-shadow: 0 3px 8px rgba(0,0,0,0.25);
  272. border-radius: 0 0 4px 4px;
  273. top: 100%;
  274. left: 0;
  275. }
  276. .customer-item {
  277. padding: 10px 12px;
  278. cursor: pointer;
  279. border-bottom: 1px solid #eee;
  280. transition: background-color 0.2s;
  281. }
  282. .customer-item:hover {
  283. background-color: #f0f0f0;
  284. }
  285. .customer-item:last-child {
  286. border-bottom: none;
  287. }
  288. .selected-customer-info {
  289. font-weight: bold;
  290. padding: 8px 10px;
  291. border: 1px solid #ddd;
  292. background-color: #f9f9f9;
  293. display: none;
  294. width: 100%;
  295. box-sizing: border-box;
  296. word-break: break-all;
  297. overflow: hidden;
  298. text-overflow: ellipsis;
  299. white-space: normal;
  300. min-height: 38px;
  301. cursor: pointer;
  302. }
  303. .selected-customer-info:hover {
  304. background-color: #f0f0f0;
  305. }
  306. .customer-clear-btn {
  307. margin-left: 5px;
  308. color: #e74c3c;
  309. font-weight: bold;
  310. cursor: pointer;
  311. float: right;
  312. }
  313. .customer-clear-btn:hover {
  314. color: #c0392b;
  315. }
  316. .product-row .total-price-input {
  317. width: 90%;
  318. }
  319. </style>
  320. </head>
  321. <body>
  322. <div id="man_zone">
  323. <form name="form1" id="form1" method="post" action="order_save.php?<?= $hrefstr ?>" onsubmit="return validateOrderForm()">
  324. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  325. <tbody>
  326. <tr>
  327. <th width="8%">销售订单号</th>
  328. <td>
  329. <input type="text" id="order_code" name="order_code" value="<?= htmlspecialcharsFix($order['order_code']) ?>" class="txt1" maxlength="100" />
  330. <input type="hidden" name="id" value="<?= $id ?>" />
  331. </td>
  332. </tr>
  333. <tr>
  334. <th width="8%">订单类型</th>
  335. <td>
  336. <div style="display: flex; gap: 20px;">
  337. <label style="display: flex; align-items: center;">
  338. <input type="radio" name="order_type" id="order_type_1" value="1" <?= ($order['order_type'] == 1) ? 'checked' : '' ?>> 普通订单
  339. </label>
  340. <label style="display: flex; align-items: center;">
  341. <input type="radio" name="order_type" id="order_type_2" value="2" <?= ($order['order_type'] == 2) ? 'checked' : '' ?>> 定制订单
  342. </label>
  343. </div>
  344. </td>
  345. </tr>
  346. <tr>
  347. <th width="8%">客户选择</th>
  348. <td>
  349. <div class="customer-search-container">
  350. <input type="text" id="customer_search" class="customer-search txt1" data-type="customer" placeholder="输入客户编码或名称搜索..." style="width: 100%;" value="<?= htmlspecialcharsFix(isset($order['cs_code']) && $order['cs_code'] ? $order['cs_code'] . ' - ' . $order['cs_company'] : $order['cs_company']) ?>">
  351. <div id="customer_dropdown" class="customer-dropdown"></div>
  352. <div id="customer_selected" class="selected-customer-info" style="display: block;" title="<?= htmlspecialcharsFix(isset($order['cs_code']) && $order['cs_code'] ? $order['cs_code'] . ' - ' . $order['cs_company'] : $order['cs_company']) ?>">
  353. <?= htmlspecialcharsFix(isset($order['cs_code']) && $order['cs_code'] ? $order['cs_code'] . ' - ' . $order['cs_company'] : $order['cs_company']) ?>
  354. <span class="customer-clear-btn" data-type="customer">X</span>
  355. </div>
  356. <input type="hidden" name="customer_id" id="customer_id" value="<?= $order['customer_id'] ?>">
  357. </div>
  358. </td>
  359. </tr>
  360. <tr>
  361. <th width="8%">出货日期</th>
  362. <td>
  363. <input type="date" id="order_date" name="order_date" value="<?= substr($order['order_date'], 0, 10) ?>" class="txt1" />
  364. </td>
  365. </tr>
  366. <tr>
  367. <th width="8%" valign="top">产品列表</th>
  368. <td>
  369. <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
  370. <div class="product-list-header" style="display: flex; background-color: #eee; padding: 8px 15px; margin-bottom: 10px; border-radius: 4px; font-weight: bold; color: #555; font-size: 13px; gap: 20px;">
  371. <div style="flex: 3; min-width: 200px;">产品</div>
  372. <div style="flex: 1; min-width: 80px;">数量</div>
  373. <div style="flex: 0.8; min-width: 60px;">单位</div>
  374. <div style="flex: 1.2; min-width: 100px;">总价</div>
  375. </div>
  376. <div id="product-container">
  377. <?php foreach ($orderItems as $index => $item): ?>
  378. <div class="product-row" data-index="<?= $index ?>">
  379. <span class="delete-product">×</span>
  380. <div class="row-section product-info">
  381. <div class="row-section-label"><span>产品</span></div>
  382. <input type="hidden" name="items[<?= $index ?>][product_id]" class="product-id-input" value="<?= $item['product_id'] ?>">
  383. <input type="text" class="product-search" placeholder="输入产品名称搜索..." style="width: 100%; padding: 5px; border: 1px solid #ddd; border-radius: 4px; display: none;">
  384. <div class="productlist" style="width: 100%; max-height: 200px;"><ul></ul></div>
  385. <div class="selected-product-info" style="cursor: pointer;" title="点击重新选择产品">
  386. <?= htmlspecialcharsFix($item['ProductName'] ?? '') ?>
  387. <?php if (!empty($item['category_name'])): ?>
  388. <span class="category-tag">(<?= htmlspecialcharsFix($item['category_name']) ?>)</span>
  389. <?php endif; ?>
  390. </div>
  391. </div>
  392. <div class="row-section product-quantity">
  393. <div class="row-section-label"><span>数量</span></div>
  394. <input type="number" name="items[<?= $index ?>][quantity]" value="<?= $item['quantity'] ?>" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  395. </div>
  396. <div class="row-section product-unit">
  397. <div class="row-section-label"><span>单位</span></div>
  398. <span class="unit-label"><?= htmlspecialcharsFix($item['unit']) ?></span>
  399. <input type="hidden" name="items[<?= $index ?>][unit]" value="<?= htmlspecialcharsFix($item['unit']) ?>" class="unit-input">
  400. </div>
  401. <div class="row-section product-total">
  402. <div class="row-section-label"><span>总价</span></div>
  403. <input type="number" step="0.01" name="items[<?= $index ?>][total_price]" value="<?= $item['total_price'] ?>" class="total-price-input" placeholder="输入总价" onchange="calculateOrderTotal()">
  404. </div>
  405. <!-- 保留隐藏的折扣字段以便后端兼容 -->
  406. <input type="hidden" name="items[<?= $index ?>][discount_amount]" value="0" class="discount-amount-input">
  407. <input type="hidden" name="items[<?= $index ?>][discount_percent]" value="0" class="discount-percent-input">
  408. <input type="hidden" name="items[<?= $index ?>][notes]" value="<?= htmlspecialcharsFix($item['notes'] ?? '') ?>">
  409. </div>
  410. <?php endforeach; ?>
  411. <!-- 产品行将通过搜索添加 -->
  412. <?php if (count($orderItems) == 0): ?>
  413. <div id="no-products-message" style="padding: 15px; text-align: center; color: #777; background-color: #f5f5f5; border: 1px dashed #ddd; border-radius: 4px; margin-bottom: 15px; display: block;">
  414. <i class="fa fa-info-circle"></i> 请点击"添加产品"按钮添加产品
  415. </div>
  416. <?php endif; ?>
  417. </div>
  418. <div class="total-section" style="margin-top: 20px; padding: 15px; background-color: #f5f5f5; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
  419. <div style="display: flex; justify-content: flex-end; align-items: center;">
  420. <label style="font-size: 16px; margin-right: 10px;">订单总额:</label>
  421. <span id="total-amount" style="font-size: 18px; font-weight: bold; color: #e74c3c;"><?= number_format($order['total_amount'], 2) ?></span>
  422. <input type="hidden" name="total_amount" id="total-amount-input" value="<?= $order['total_amount'] ?>">
  423. <input type="hidden" name="subtotal" id="subtotal-input" value="<?= $order['subtotal'] ?>">
  424. <input type="hidden" name="discount_amount" id="order-discount" value="0">
  425. </div>
  426. </div>
  427. </td>
  428. </tr>
  429. <tr>
  430. <th width="8%">订单备注</th>
  431. <td>
  432. <textarea name="notes" rows="3" style="width: 90%;"><?= htmlspecialcharsFix($order['notes']) ?></textarea>
  433. </td>
  434. </tr>
  435. <tr>
  436. <th></th>
  437. <td>
  438. <input type="submit" name="save" value="保存订单" class="btn1">
  439. <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
  440. </td>
  441. </tr>
  442. </tbody>
  443. </table>
  444. </form>
  445. <script>
  446. var productIndex = <?= count($orderItems) ?>;
  447. $(document).ready(function() {
  448. // 初始化计算
  449. calculateOrderTotal();
  450. // 初始化无产品消息
  451. updateNoProductsMessage();
  452. // 初始化现有产品的规格选择
  453. $('.product-row').each(function() {
  454. var row = $(this);
  455. var productId = row.find('.product-id-input').val();
  456. var specId = row.find('.spec-id-input').val();
  457. console.log("初始化行 - 行索引:", row.data('index'), "产品ID:", productId, "规格ID:", specId);
  458. if (productId) {
  459. // 加载产品规格
  460. getProductSpecifications(productId, row);
  461. }
  462. });
  463. // 隐藏搜索框,因为已经选择了客户
  464. $('#customer_search').hide();
  465. // 客户搜索功能
  466. var customerSearchTimeout = null;
  467. var customerIsComposing = false;
  468. // 监听输入法组合事件
  469. $(document).on('compositionstart', '.customer-search', function() {
  470. customerIsComposing = true;
  471. });
  472. $(document).on('compositionend', '.customer-search', function() {
  473. customerIsComposing = false;
  474. $(this).trigger('input'); // 手动触发一次input事件
  475. });
  476. // 客户搜索输入处理
  477. $(document).on('input', '#customer_search', function() {
  478. // 如果是输入法正在组合中文,不处理
  479. if (customerIsComposing) return;
  480. var $this = $(this);
  481. var searchTerm = $this.val().trim();
  482. var customerDropdown = $('#customer_dropdown');
  483. // 清除之前的超时
  484. clearTimeout(customerSearchTimeout);
  485. // 隐藏之前的结果
  486. customerDropdown.hide().empty();
  487. // 清除之前的选择
  488. $('#customer_id').val('0');
  489. $('#customer_selected').hide();
  490. // 如果搜索词少于1个字符,不执行搜索
  491. if (searchTerm.length < 1) {
  492. return;
  493. }
  494. // 设置一个300毫秒的超时,以减少请求数量
  495. customerSearchTimeout = setTimeout(function() {
  496. $.ajax({
  497. url: 'get_customer_search.php',
  498. type: 'GET',
  499. data: {search: searchTerm},
  500. dataType: 'json',
  501. success: function(data) {
  502. customerDropdown.empty();
  503. if (data && data.customers && data.customers.length > 0) {
  504. $.each(data.customers, function(i, customer) {
  505. var displayText = customer.cs_company;
  506. if (customer.cs_code) {
  507. displayText = customer.cs_code + ' - ' + displayText;
  508. }
  509. var item = $('<div class="customer-item"></div>')
  510. .attr('data-id', customer.id)
  511. .attr('data-company', customer.cs_company)
  512. .attr('data-display', displayText)
  513. .text(displayText);
  514. customerDropdown.append(item);
  515. });
  516. customerDropdown.show();
  517. }
  518. },
  519. error: function() {
  520. console.log('搜索客户失败,请重试');
  521. }
  522. });
  523. }, 300);
  524. });
  525. // 点击选择客户
  526. $(document).on('click', '.customer-item', function() {
  527. var $this = $(this);
  528. var customerId = $this.data('id');
  529. var customerName = $this.data('company');
  530. var displayText = $this.data('display');
  531. // 设置选中的客户ID和名称
  532. $('#customer_id').val(customerId);
  533. $('#customer_search').hide();
  534. $('#customer_selected').text(displayText).append('<span class="customer-clear-btn" data-type="customer">X</span>').show();
  535. // 添加标题属性,便于查看完整信息
  536. $('#customer_selected').attr('title', displayText);
  537. // 隐藏下拉菜单
  538. $('#customer_dropdown').hide();
  539. });
  540. // 点击X按钮清除选择的客户
  541. $(document).on('click', '.customer-clear-btn', function(e) {
  542. e.stopPropagation();
  543. // 显示搜索框,隐藏已选信息
  544. $('#customer_search').val('').show();
  545. $('#customer_selected').hide();
  546. // 清空客户ID
  547. $('#customer_id').val('0');
  548. });
  549. // 点击其他地方隐藏下拉列表
  550. $(document).on('click', function(e) {
  551. if (!$(e.target).closest('.customer-search-container').length) {
  552. $('#customer_dropdown').hide();
  553. }
  554. });
  555. // 添加产品行按钮点击事件
  556. $('#add-product-btn').on('click', function() {
  557. addEmptyProductRow();
  558. updateNoProductsMessage();
  559. });
  560. // 删除产品行
  561. $(document).on('click', '.delete-product', function() {
  562. if ($('.product-row').length > 1) {
  563. $(this).closest('.product-row').remove();
  564. reindexProductRows();
  565. calculateOrderTotal();
  566. updateNoProductsMessage();
  567. } else {
  568. // 如果只剩最后一个产品行,则清空它而不是删除
  569. $(this).closest('.product-row').remove();
  570. updateNoProductsMessage();
  571. calculateOrderTotal(); // 确保在删除最后一个产品时也更新总额
  572. }
  573. });
  574. // 产品搜索框聚焦时显示搜索提示
  575. $(document).on('focus', '.product-search', function() {
  576. // 隐藏所有已打开的产品搜索列表
  577. $('.productlist').hide();
  578. // 当前搜索框的值
  579. var searchTerm = $(this).val().trim();
  580. if (searchTerm.length >= 2) {
  581. // 找到当前行的产品列表并显示
  582. $(this).siblings('.productlist').show();
  583. }
  584. });
  585. // 产品搜索功能 - 行内搜索
  586. var productSearchTimeouts = {};
  587. $(document).on('keyup', '.product-search', function() {
  588. var $this = $(this);
  589. var rowIndex = $this.closest('.product-row').data('index');
  590. var searchTerm = $this.val().trim();
  591. var productList = $this.siblings('.productlist');
  592. // 清除之前的超时
  593. clearTimeout(productSearchTimeouts[rowIndex]);
  594. // 隐藏之前的结果
  595. productList.hide();
  596. // 如果搜索词少于2个字符,不执行搜索
  597. if (searchTerm.length < 2) {
  598. return;
  599. }
  600. // 设置一个300毫秒的超时,以减少请求数量
  601. productSearchTimeouts[rowIndex] = setTimeout(function() {
  602. $.ajax({
  603. url: 'get_product_info.php',
  604. type: 'GET',
  605. data: {search: searchTerm},
  606. dataType: 'json',
  607. success: function(data) {
  608. var ul = productList.find('ul');
  609. ul.empty();
  610. if (data && data.products && data.products.length > 0) {
  611. $.each(data.products, function(i, product) {
  612. ul.append('<li data-id="' + product.id + '">' +
  613. product.ProductName +
  614. (product.category_name ? ' <span class="category-tag">(' + product.category_name + ')</span>' : '') +
  615. '</li>');
  616. });
  617. productList.show();
  618. }
  619. },
  620. error: function() {
  621. console.log('搜索产品失败,请重试');
  622. }
  623. });
  624. }, 300);
  625. });
  626. // 点击选择产品 - 行内搜索结果
  627. $(document).on('click', '.productlist li', function() {
  628. var $this = $(this);
  629. var productId = $this.data('id');
  630. var productName = $this.text(); // 这会包含分类名,需要清理
  631. var categoryTag = $this.find('.category-tag').text();
  632. var row = $this.closest('.product-row');
  633. // 检查是否已存在相同的产品
  634. var isDuplicate = false;
  635. $('.product-id-input').not(row.find('.product-id-input')).each(function() {
  636. if ($(this).val() == productId) {
  637. isDuplicate = true;
  638. return false; // 跳出循环
  639. }
  640. });
  641. if (isDuplicate) {
  642. alert('该产品已添加,不能添加相同的产品');
  643. return;
  644. }
  645. // 清理产品名称,移除分类信息
  646. if (categoryTag) {
  647. productName = productName.replace(categoryTag, '').trim();
  648. }
  649. // 设置产品ID和名称
  650. row.find('.product-id-input').val(productId);
  651. row.find('.product-search').hide();
  652. // 显示包含分类的完整产品信息
  653. var displayName = productName;
  654. if (categoryTag) {
  655. displayName += ' <span class="category-tag">' + categoryTag + '</span>';
  656. }
  657. row.find('.selected-product-info').html(displayName).show();
  658. // 隐藏产品搜索结果
  659. row.find('.productlist').hide();
  660. // 获取产品规格信息
  661. getProductSpecifications(productId, row);
  662. });
  663. // 点击已选产品名显示的标签时,切换回搜索模式
  664. $(document).on('click dblclick', '.selected-product-info', function() {
  665. var row = $(this).closest('.product-row');
  666. var productId = row.find('.product-id-input').val();
  667. // 只有当已经选择了产品时才允许重新选择
  668. if (productId) {
  669. if (confirm('确定要重新选择产品吗?这将清除当前选择的产品信息。')) {
  670. // 清空产品ID
  671. row.find('.product-id-input').val('');
  672. // 隐藏产品信息,显示搜索框
  673. $(this).hide();
  674. row.find('.product-search').val('').show();
  675. // 清除单位信息
  676. row.find('.unit-input').val('');
  677. row.find('.unit-label').text('');
  678. // 清除总价信息并重新计算总额
  679. row.find('.total-price-input').val('0.00');
  680. calculateOrderTotal();
  681. }
  682. }
  683. });
  684. // 规格选择改变事件
  685. $(document).on('change', '.spec-select', function() {
  686. var $this = $(this);
  687. var row = $this.closest('.product-row');
  688. var specId = $this.val();
  689. var specData = $this.find('option:selected').data();
  690. if (specId) {
  691. // 检查是否已存在相同的产品规格组合
  692. var isDuplicate = false;
  693. $('.spec-select').not($this).each(function() {
  694. if ($(this).val() == specId) {
  695. isDuplicate = true;
  696. return false; // 跳出循环
  697. }
  698. });
  699. if (isDuplicate) {
  700. alert('该产品规格已添加,不能重复添加');
  701. $this.val(''); // 重置选择
  702. return;
  703. }
  704. // 设置规格ID到隐藏字段
  705. row.find('.spec-id-input').val(specId);
  706. // 获取选中项的文本内容作为显示信息
  707. var displayText = $this.find('option:selected').text();
  708. row.find('.spec-info').html(displayText).show();
  709. // 隐藏下拉框
  710. $this.hide();
  711. // 存储规格价格作为最低价格限制
  712. var minPrice = specData.price || 0;
  713. var priceInput = row.find('.price-input');
  714. priceInput.attr('data-min-price', minPrice);
  715. if (minPrice > 0) {
  716. priceInput.attr('placeholder', '输入单价');
  717. } else {
  718. priceInput.attr('placeholder', '输入单价');
  719. }
  720. // 设置最小订购数量
  721. var minQty = specData.minQty || 1;
  722. var qtyInput = row.find('.quantity-input');
  723. if (parseInt(qtyInput.val()) < minQty) {
  724. qtyInput.val(minQty);
  725. }
  726. qtyInput.attr('min', minQty);
  727. // 重新计算总价
  728. calculateItemTotal(row.find('.price-input')[0]);
  729. } else {
  730. // 清除规格相关信息
  731. row.find('.spec-id-input').val('');
  732. row.find('.price-input').attr('data-min-price', '0').attr('placeholder', '输入单价');
  733. row.find('.spec-info').html('').hide();
  734. calculateItemTotal(row.find('.price-input')[0]);
  735. }
  736. });
  737. // 点击其他地方隐藏下拉列表
  738. $(document).on('click', function(e) {
  739. if (!$(e.target).closest('.product-search').length && !$(e.target).closest('.productlist').length) {
  740. $('.productlist').hide();
  741. }
  742. if (!$(e.target).closest('.customer-search-container').length) {
  743. $('#customer_dropdown').hide();
  744. }
  745. });
  746. });
  747. function addEmptyProductRow() {
  748. var html = `
  749. <div class="product-row" data-index="${productIndex}">
  750. <span class="delete-product">×</span>
  751. <div class="row-section product-info">
  752. <div class="row-section-label"><span>产品</span></div>
  753. <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
  754. <input type="text" class="product-search" placeholder="输入产品名称搜索..." style="width: 100%; padding: 5px; border: 1px solid #ddd; border-radius: 4px;">
  755. <div class="productlist" style="width: 100%; max-height: 200px;"><ul></ul></div>
  756. <div class="selected-product-info" style="cursor: pointer; display: none;" title="点击重新选择产品"></div>
  757. </div>
  758. <div class="row-section product-quantity">
  759. <div class="row-section-label"><span>数量</span></div>
  760. <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input">
  761. </div>
  762. <div class="row-section product-unit">
  763. <div class="row-section-label"><span>单位</span></div>
  764. <span class="unit-label" style="display: inline-block; padding: 2px 5px; min-width: 30px;"></span>
  765. <input type="hidden" name="items[${productIndex}][unit]" class="unit-input" value="">
  766. </div>
  767. <div class="row-section product-total">
  768. <div class="row-section-label"><span>总价</span></div>
  769. <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" placeholder="输入总价" onchange="calculateOrderTotal()">
  770. </div>
  771. <!-- 保留隐藏的字段以便后端兼容 -->
  772. <input type="hidden" name="items[${productIndex}][discount_amount]" value="0" class="discount-amount-input">
  773. <input type="hidden" name="items[${productIndex}][discount_percent]" value="0" class="discount-percent-input">
  774. <input type="hidden" name="items[${productIndex}][notes]" value="">
  775. </div>
  776. `;
  777. $('#product-container').append(html);
  778. productIndex++;
  779. return productIndex - 1; // 返回新添加行的索引
  780. }
  781. function addProductRowWithProduct(productId, productName, categoryTag) {
  782. // 添加空行
  783. var rowIndex = addEmptyProductRow();
  784. var row = $('.product-row[data-index="' + rowIndex + '"]');
  785. // 设置产品ID和名称
  786. row.find('.product-id-input').val(productId);
  787. // 显示包含分类的完整产品信息
  788. var displayName = productName;
  789. if (categoryTag) {
  790. displayName += ' <span class="category-tag">' + categoryTag + '</span>';
  791. }
  792. row.find('.selected-product-info').html(displayName).show();
  793. row.find('.product-search').hide();
  794. // 获取产品规格信息
  795. getProductSpecifications(productId, row);
  796. // 更新无产品消息显示
  797. updateNoProductsMessage();
  798. }
  799. function reindexProductRows() {
  800. $('.product-row').each(function(index) {
  801. $(this).attr('data-index', index);
  802. $(this).find('select, input').each(function() {
  803. var name = $(this).attr('name');
  804. if (name) {
  805. name = name.replace(/items\[\d+\]/, 'items[' + index + ']');
  806. $(this).attr('name', name);
  807. }
  808. });
  809. });
  810. productIndex = $('.product-row').length;
  811. }
  812. function getProductSpecifications(productId, row) {
  813. if (!productId) return;
  814. // 获取当前选中的规格ID(如果有的话)
  815. var currentSpecId = row.find('.spec-id-input').val();
  816. console.log("加载规格 - 产品ID:", productId, "当前规格ID:", currentSpecId);
  817. $.ajax({
  818. url: 'get_product_info.php',
  819. type: 'GET',
  820. data: {product_id: productId},
  821. dataType: 'json',
  822. success: function(data) {
  823. if (data && data.product) {
  824. // 设置单位信息
  825. if (data.product.unit) {
  826. row.find('.unit-input').val(data.product.unit);
  827. row.find('.unit-label').text(data.product.unit);
  828. }
  829. } else {
  830. alert('获取产品信息失败');
  831. // 重置产品选择
  832. row.find('.product-id-input').val('');
  833. row.find('.selected-product-info').hide();
  834. row.find('.product-search').val('').show();
  835. }
  836. },
  837. error: function() {
  838. alert('获取产品信息失败,请重试');
  839. // 重置产品选择
  840. row.find('.product-id-input').val('');
  841. row.find('.selected-product-info').hide();
  842. row.find('.product-search').val('').show();
  843. }
  844. });
  845. }
  846. function getProductInfo(productId, row) {
  847. if (!productId) return;
  848. $.ajax({
  849. url: 'get_product_info.php',
  850. type: 'GET',
  851. data: {id: productId},
  852. dataType: 'json',
  853. success: function(data) {
  854. if (data) {
  855. row.find('.unit-input').val(data.unit);
  856. row.find('.unit-label').text(data.unit);
  857. // 只有当数据库中有价格信息时才设置价格
  858. if (data.price && data.price !== '0' && data.price !== '0.00') {
  859. //默认不设置单价
  860. //row.find('.price-input').val(data.price);
  861. }
  862. calculateOrderTotal();
  863. }
  864. }
  865. });
  866. }
  867. function calculateOrderTotal() {
  868. var total = 0;
  869. $('.total-price-input').each(function() {
  870. total += parseFloat($(this).val()) || 0;
  871. });
  872. // 更新总额和隐藏的小计字段
  873. $('#total-amount').text(total.toFixed(2));
  874. $('#total-amount-input').val(total.toFixed(2));
  875. $('#subtotal-input').val(total.toFixed(2)); // 为兼容性保留
  876. }
  877. function validateOrderForm() {
  878. var orderCode = $('#order_code').val();
  879. var customerId = $('#customer_id').val();
  880. var customerName = $('#customer_search').val() || $('#customer_selected').text().trim();
  881. var orderType = $('input[name="order_type"]:checked').val();
  882. var hasProducts = false;
  883. var allTotalPricesValid = true;
  884. var firstInvalidField = null;
  885. // 检查是否有产品行
  886. if ($('.product-row').length === 0) {
  887. alert('请至少添加一个产品');
  888. $('#add-product-btn').focus();
  889. return false;
  890. }
  891. // 检查订单类型是否已选择
  892. if (!orderType) {
  893. alert('请选择订单类型');
  894. $('#order_type_1').focus();
  895. return false;
  896. }
  897. $('.product-row').each(function() {
  898. var productId = $(this).find('.product-id-input').val();
  899. var totalPriceInput = $(this).find('.total-price-input');
  900. var totalPrice = parseFloat(totalPriceInput.val()) || 0;
  901. var productSearch = $(this).find('.product-search');
  902. // 检查产品是否已选择
  903. if (!productId) {
  904. if (!firstInvalidField) {
  905. firstInvalidField = productSearch;
  906. }
  907. return true; // continue
  908. }
  909. hasProducts = true;
  910. // 检查总价是否有效
  911. if (totalPrice <= 0) {
  912. allTotalPricesValid = false;
  913. if (!firstInvalidField) {
  914. firstInvalidField = totalPriceInput;
  915. }
  916. }
  917. });
  918. if (!orderCode) {
  919. alert('销售订单号不能为空');
  920. $('#order_code').focus();
  921. return false;
  922. }
  923. if (!customerId || customerId == '0') {
  924. alert('请选择客户');
  925. $('#customer_search').focus();
  926. return false;
  927. }
  928. if (!customerName) {
  929. alert('客户名称不能为空');
  930. $('#customer_search').focus();
  931. return false;
  932. }
  933. if (!hasProducts) {
  934. alert('请至少添加一个有效的产品');
  935. if (firstInvalidField) {
  936. firstInvalidField.focus();
  937. } else {
  938. $('#add-product-btn').focus();
  939. }
  940. return false;
  941. }
  942. if (!allTotalPricesValid) {
  943. alert('请为所有产品输入有效的总价');
  944. if (firstInvalidField) {
  945. firstInvalidField.focus();
  946. }
  947. return false;
  948. }
  949. return true;
  950. }
  951. function updateNoProductsMessage() {
  952. if ($('.product-row').length > 0) {
  953. $('#no-products-message').hide();
  954. } else {
  955. $('#no-products-message').show();
  956. }
  957. }
  958. </script>
  959. </div>
  960. </body>
  961. </html>