order_edit.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. FROM order_items oi
  27. LEFT JOIN products p ON oi.product_id = p.id
  28. LEFT JOIN product_categories pc ON p.category_id = pc.id
  29. WHERE oi.order_id = $id";
  30. $itemsResult = mysqli_query($conn, $sql);
  31. $orderItems = [];
  32. while ($itemRow = mysqli_fetch_assoc($itemsResult)) {
  33. $orderItems[] = $itemRow;
  34. }
  35. } else {
  36. echo "<script>alert('订单不存在!');history.back();</script>";
  37. exit;
  38. }
  39. ?>
  40. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  41. <html xmlns="http://www.w3.org/1999/xhtml">
  42. <head>
  43. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  44. <title>编辑订单</title>
  45. <link rel="stylesheet" href="css/common.css" type="text/css" />
  46. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  47. <script src="js/jquery-1.7.2.min.js"></script>
  48. <script src="js/js.js"></script>
  49. <style>
  50. body {
  51. margin: 0;
  52. padding: 20px;
  53. background: #fff;
  54. }
  55. #man_zone {
  56. margin-left: 0;
  57. }
  58. .product-row {
  59. border: 1px solid #ddd;
  60. padding: 10px;
  61. margin-bottom: 10px;
  62. background-color: #f9f9f9;
  63. position: relative;
  64. }
  65. .delete-product {
  66. position: absolute;
  67. right: 10px;
  68. top: 10px;
  69. color: red;
  70. cursor: pointer;
  71. }
  72. .add-product-btn {
  73. background-color: #4CAF50;
  74. color: white;
  75. padding: 8px 12px;
  76. border: none;
  77. cursor: pointer;
  78. margin-bottom: 15px;
  79. }
  80. .total-section {
  81. margin-top: 20px;
  82. padding: 10px;
  83. background-color: #eee;
  84. font-weight: bold;
  85. }
  86. #product-container {
  87. margin-bottom: 20px;
  88. }
  89. .productlist {
  90. display: none;
  91. position: absolute;
  92. background: white;
  93. border: 1px solid #ccc;
  94. max-height: 200px;
  95. overflow-y: auto;
  96. width: 100%;
  97. z-index: 1000;
  98. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  99. }
  100. .productlist ul {
  101. list-style: none;
  102. padding: 0;
  103. margin: 0;
  104. }
  105. .productlist li {
  106. padding: 8px 10px;
  107. cursor: pointer;
  108. border-bottom: 1px solid #eee;
  109. }
  110. .productlist li:hover {
  111. background-color: #f5f5f5;
  112. }
  113. .productinput {
  114. position: relative;
  115. }
  116. .customerlist ul {
  117. list-style: none;
  118. padding: 0;
  119. margin: 0;
  120. }
  121. .customerlist li {
  122. padding: 8px 10px;
  123. cursor: pointer;
  124. border-bottom: 1px solid #eee;
  125. }
  126. .customerlist li:hover {
  127. background-color: #f5f5f5;
  128. }
  129. .productlist li .category-tag {
  130. color: #777;
  131. font-size: 12px;
  132. font-style: italic;
  133. margin-left: 5px;
  134. }
  135. .selected-product-info .category-tag {
  136. color: #777;
  137. font-size: 12px;
  138. font-style: italic;
  139. margin-left: 5px;
  140. }
  141. </style>
  142. </head>
  143. <body>
  144. <div id="man_zone">
  145. <form name="form1" id="form1" method="post" action="order_save.php?<?= $hrefstr ?>" onsubmit="return validateOrderForm()">
  146. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  147. <tbody>
  148. <tr>
  149. <th width="8%">订单编号</th>
  150. <td>
  151. <input type="text" id="order_code" name="order_code" value="<?= htmlspecialcharsFix($order['order_code']) ?>" class="txt1" />
  152. <input type="hidden" name="id" value="<?= $id ?>" />
  153. </td>
  154. </tr>
  155. <tr>
  156. <th width="8%">客户选择</th>
  157. <td>
  158. <div style="display: inline-block; width: 60%;" class="customerinput">
  159. <input type="text" class="customer-search fastsearch" placeholder="输入客户编码或名称搜索..." style="width: 100%;" value="<?= htmlspecialcharsFix(isset($order['cs_code']) && $order['cs_code'] ? $order['cs_code'] . ' - ' . $order['cs_company'] : $order['cs_company']) ?>">
  160. <div class="customerlist" style="display: none; position: absolute; background: white; border: 1px solid #ccc; max-height: 200px; overflow-y: auto; width: 100%; z-index: 1000; box-shadow: 0 2px 5px rgba(0,0,0,0.2);">
  161. <ul style="list-style: none; padding: 0; margin: 0;"></ul>
  162. </div>
  163. <input type="hidden" name="customer_id" id="customer_id" value="<?= $order['customer_id'] ?>">
  164. <div class="selected-customer-info" style="margin-top: 5px; font-weight: bold;"></div>
  165. </div>
  166. </td>
  167. </tr>
  168. <tr>
  169. <th width="8%">联系人</th>
  170. <td>
  171. <select id="contact_id" name="contact_id">
  172. <option value="">请选择联系人</option>
  173. <?php
  174. if ($order['customer_id']) {
  175. $contactSql = "SELECT id, contact_name FROM customer_contact WHERE customer_id = " . $order['customer_id'];
  176. $contactResult = mysqli_query($conn, $contactSql);
  177. while ($contactRow = mysqli_fetch_assoc($contactResult)) {
  178. $selected = ($order['contact_id'] == $contactRow['id']) ? ' selected' : '';
  179. echo "<option value=\"{$contactRow['id']}\"$selected>" . htmlspecialcharsFix($contactRow['contact_name']) . "</option>";
  180. }
  181. }
  182. ?>
  183. </select>
  184. </td>
  185. </tr>
  186. <tr>
  187. <th width="8%">订单日期</th>
  188. <td>
  189. <input type="date" id="order_date" name="order_date" value="<?= substr($order['order_date'], 0, 10) ?>" class="txt1" />
  190. </td>
  191. </tr>
  192. <tr>
  193. <th width="8%" valign="top">产品列表</th>
  194. <td>
  195. <div id="product-container">
  196. <?php foreach ($orderItems as $index => $item): ?>
  197. <div class="product-row" data-index="<?= $index ?>">
  198. <span class="delete-product">×</span>
  199. <div>
  200. <label>产品:</label>
  201. <div style="display: inline-block; width: 60%;">
  202. <input type="hidden" name="items[<?= $index ?>][product_id]" class="product-id-input" value="<?= $item['product_id'] ?>">
  203. <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;">
  204. <?= htmlspecialcharsFix($item['ProductName'] ?? '') ?>
  205. <?php if (!empty($item['category_name'])): ?>
  206. <span class="category-tag">(<?= htmlspecialcharsFix($item['category_name']) ?>)</span>
  207. <?php endif; ?>
  208. </div>
  209. </div>
  210. </div>
  211. <div style="margin-top: 5px;">
  212. <label>数量:</label>
  213. <input type="number" name="items[<?= $index ?>][quantity]" value="<?= $item['quantity'] ?>" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  214. <label style="margin-left: 10px;">单位:</label>
  215. <input type="text" name="items[<?= $index ?>][unit]" value="<?= htmlspecialcharsFix($item['unit']) ?>" class="unit-input">
  216. <label style="margin-left: 10px;">单价:</label>
  217. <input type="number" step="0.01" name="items[<?= $index ?>][unit_price]" value="<?= $item['unit_price'] ?>" class="price-input" placeholder="请输入单价" onchange="calculateItemTotal(this)">
  218. <label style="margin-left: 10px;">总价:</label>
  219. <input type="number" step="0.01" name="items[<?= $index ?>][total_price]" value="<?= $item['total_price'] ?>" class="total-price-input" readonly>
  220. </div>
  221. <div style="margin-top: 5px;">
  222. <label>备注:</label>
  223. <input type="text" name="items[<?= $index ?>][notes]" value="<?= htmlspecialcharsFix($item['notes']) ?>" style="width: 90%;">
  224. <!-- 保留隐藏的折扣字段以便后端兼容 -->
  225. <input type="hidden" name="items[<?= $index ?>][discount_amount]" value="0" class="discount-amount-input">
  226. <input type="hidden" name="items[<?= $index ?>][discount_percent]" value="0" class="discount-percent-input">
  227. </div>
  228. </div>
  229. <?php endforeach; ?>
  230. <!-- 产品行将通过搜索添加 -->
  231. <?php if (count($orderItems) == 0): ?>
  232. <div id="no-products-message" style="padding: 20px; text-align: center; color: #777; background-color: #f9f9f9; border: 1px dashed #ddd; display: block;">
  233. 请使用下方的产品搜索框添加产品
  234. </div>
  235. <?php endif; ?>
  236. </div>
  237. <div class="total-section">
  238. <div>
  239. <label>订单总额:</label>
  240. <span id="total-amount"><?= number_format($order['total_amount'], 2) ?></span>
  241. <input type="hidden" name="total_amount" id="total-amount-input" value="<?= $order['total_amount'] ?>">
  242. <input type="hidden" name="subtotal" id="subtotal-input" value="<?= $order['subtotal'] ?>">
  243. <input type="hidden" name="discount_amount" id="order-discount" value="0">
  244. </div>
  245. </div>
  246. <div style="margin-top: 15px; border-top: 1px dashed #ccc; padding-top: 15px;">
  247. <div class="productinput" style="position: relative; width: 60%;">
  248. <label>产品搜索:</label>
  249. <input type="text" id="product-search-single" class="fastsearch" placeholder="输入产品名称搜索并添加..." style="width: 100%;">
  250. <div id="product-list-single" class="productlist" style="width: 100%;"><ul></ul></div>
  251. </div>
  252. </div>
  253. </td>
  254. </tr>
  255. <tr>
  256. <th width="8%">订单备注</th>
  257. <td>
  258. <textarea name="notes" rows="3" style="width: 90%;"><?= htmlspecialcharsFix($order['notes']) ?></textarea>
  259. </td>
  260. </tr>
  261. <tr>
  262. <th></th>
  263. <td>
  264. <input type="submit" name="save" value="保存订单" class="btn1">
  265. <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
  266. </td>
  267. </tr>
  268. </tbody>
  269. </table>
  270. </form>
  271. <script>
  272. var productIndex = <?= count($orderItems) ?>;
  273. $(document).ready(function() {
  274. // 初始化计算
  275. calculateOrderTotal();
  276. // 初始化无产品消息
  277. updateNoProductsMessage();
  278. // 删除产品行
  279. $(document).on('click', '.delete-product', function() {
  280. if ($('.product-row').length > 1) {
  281. $(this).closest('.product-row').remove();
  282. reindexProductRows();
  283. calculateOrderTotal();
  284. updateNoProductsMessage();
  285. } else {
  286. // 如果只剩最后一个产品行,则清空它而不是删除
  287. $(this).closest('.product-row').remove();
  288. updateNoProductsMessage();
  289. calculateOrderTotal();
  290. }
  291. });
  292. // 客户搜索功能
  293. var customerSearchTimeout = null;
  294. $(document).on('keyup', '.customer-search', function() {
  295. var $this = $(this);
  296. var searchTerm = $this.val().trim();
  297. var customerList = $('.customerlist');
  298. // 清除之前的超时
  299. clearTimeout(customerSearchTimeout);
  300. // 隐藏之前的结果
  301. customerList.hide();
  302. // 如果搜索词少于2个字符,不执行搜索
  303. if (searchTerm.length < 2) {
  304. return;
  305. }
  306. // 设置一个300毫秒的超时,以减少请求数量
  307. customerSearchTimeout = setTimeout(function() {
  308. $.ajax({
  309. url: 'get_customer_search.php',
  310. type: 'GET',
  311. data: {search: searchTerm},
  312. dataType: 'json',
  313. success: function(data) {
  314. var ul = customerList.find('ul');
  315. ul.empty();
  316. if (data && data.customers && data.customers.length > 0) {
  317. $.each(data.customers, function(i, customer) {
  318. var displayText = customer.cs_company;
  319. if (customer.cs_code) {
  320. displayText = customer.cs_code + ' - ' + displayText;
  321. }
  322. ul.append('<li data-id="' + customer.id + '" data-code="' + (customer.cs_code || '') + '">' +
  323. displayText + '</li>');
  324. });
  325. customerList.show();
  326. }
  327. },
  328. error: function() {
  329. console.log('搜索客户失败,请重试');
  330. }
  331. });
  332. }, 300);
  333. });
  334. // 点击选择客户
  335. $(document).on('click', '.customerlist li', function() {
  336. var $this = $(this);
  337. var customerId = $this.data('id');
  338. var customerName = $this.text();
  339. var customerCode = $this.data('code') || '';
  340. // 设置选中的客户ID和名称
  341. $('#customer_id').val(customerId);
  342. $('.customer-search').val(customerName);
  343. $('.selected-customer-info').text(customerName);
  344. $('.customerlist').hide();
  345. // 加载客户联系人
  346. loadCustomerContacts(customerId);
  347. });
  348. // 单一产品搜索功能 - 使用防抖动以减少请求数量
  349. var productSearchTimeout = null;
  350. $(document).on('keyup', '#product-search-single', function() {
  351. var $this = $(this);
  352. var searchTerm = $this.val().trim();
  353. var productList = $('#product-list-single');
  354. // 清除之前的超时
  355. clearTimeout(productSearchTimeout);
  356. // 隐藏之前的结果
  357. productList.hide();
  358. // 如果搜索词少于2个字符,不执行搜索
  359. if (searchTerm.length < 2) {
  360. return;
  361. }
  362. // 设置一个300毫秒的超时,以减少请求数量
  363. productSearchTimeout = setTimeout(function() {
  364. $.ajax({
  365. url: 'get_product_info.php',
  366. type: 'GET',
  367. data: {search: searchTerm},
  368. dataType: 'json',
  369. success: function(data) {
  370. var ul = productList.find('ul');
  371. ul.empty();
  372. if (data && data.products && data.products.length > 0) {
  373. $.each(data.products, function(i, product) {
  374. ul.append('<li data-id="' + product.id + '">' +
  375. product.ProductName +
  376. (product.category_name ? ' <span class="category-tag">(' + product.category_name + ')</span>' : '') +
  377. '</li>');
  378. });
  379. productList.show();
  380. }
  381. },
  382. error: function() {
  383. console.log('搜索产品失败,请重试');
  384. }
  385. });
  386. }, 300);
  387. });
  388. // 点击选择产品 - 从单一搜索框
  389. $(document).on('click', '#product-list-single li', function() {
  390. var $this = $(this);
  391. var productId = $this.data('id');
  392. var productName = $this.text(); // 这会包含分类名,需要清理
  393. var categoryTag = $this.find('.category-tag').text();
  394. // 清理产品名称,移除分类信息
  395. if (categoryTag) {
  396. productName = productName.replace(categoryTag, '').trim();
  397. }
  398. // 检查产品是否已经添加
  399. var isDuplicate = false;
  400. $('.product-id-input').each(function() {
  401. if ($(this).val() == productId) {
  402. isDuplicate = true;
  403. return false; // 跳出循环
  404. }
  405. });
  406. if (isDuplicate) {
  407. alert('该产品已添加,不能重复添加');
  408. $('#product-search-single').val('');
  409. $('#product-list-single').hide();
  410. return;
  411. }
  412. // 添加产品行
  413. addProductRowWithProduct(productId, productName, categoryTag);
  414. // 清空搜索框并隐藏结果
  415. $('#product-search-single').val('');
  416. $('#product-list-single').hide();
  417. });
  418. // 点击其他地方隐藏下拉列表
  419. $(document).on('click', function(e) {
  420. if (!$(e.target).closest('#product-search-single').length) {
  421. $('#product-list-single').hide();
  422. }
  423. if (!$(e.target).closest('.customerinput').length) {
  424. $('.customerlist').hide();
  425. }
  426. });
  427. });
  428. function addEmptyProductRow() {
  429. var html = `
  430. <div class="product-row" data-index="${productIndex}">
  431. <span class="delete-product">×</span>
  432. <div>
  433. <label>产品:</label>
  434. <div style="display: inline-block; width: 60%;">
  435. <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
  436. <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
  437. </div>
  438. </div>
  439. <div style="margin-top: 5px;">
  440. <label>数量:</label>
  441. <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  442. <label style="margin-left: 10px;">单位:</label>
  443. <input type="text" name="items[${productIndex}][unit]" value="" class="unit-input">
  444. <label style="margin-left: 10px;">单价:</label>
  445. <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="" class="price-input" placeholder="请输入单价" onchange="calculateItemTotal(this)">
  446. <label style="margin-left: 10px;">总价:</label>
  447. <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
  448. </div>
  449. <div style="margin-top: 5px;">
  450. <label>备注:</label>
  451. <input type="text" name="items[${productIndex}][notes]" value="" style="width: 90%;">
  452. <!-- 保留隐藏的折扣字段以便后端兼容 -->
  453. <input type="hidden" name="items[${productIndex}][discount_amount]" value="0" class="discount-amount-input">
  454. <input type="hidden" name="items[${productIndex}][discount_percent]" value="0" class="discount-percent-input">
  455. </div>
  456. </div>
  457. `;
  458. $('#product-container').append(html);
  459. productIndex++;
  460. return productIndex - 1; // 返回新添加行的索引
  461. }
  462. function addProductRowWithProduct(productId, productName, categoryTag) {
  463. // 添加空行
  464. var rowIndex = addEmptyProductRow();
  465. var row = $('.product-row[data-index="' + rowIndex + '"]');
  466. // 设置产品ID和名称
  467. row.find('.product-id-input').val(productId);
  468. // 显示包含分类的完整产品信息
  469. var displayName = productName;
  470. if (categoryTag) {
  471. displayName += ' <span class="category-tag">' + categoryTag + '</span>';
  472. }
  473. row.find('.selected-product-info').html(displayName);
  474. // 获取产品信息
  475. getProductInfo(productId, row);
  476. // 更新无产品消息显示
  477. updateNoProductsMessage();
  478. }
  479. function reindexProductRows() {
  480. $('.product-row').each(function(index) {
  481. $(this).attr('data-index', index);
  482. $(this).find('select, input').each(function() {
  483. var name = $(this).attr('name');
  484. if (name) {
  485. name = name.replace(/items\[\d+\]/, 'items[' + index + ']');
  486. $(this).attr('name', name);
  487. }
  488. });
  489. });
  490. productIndex = $('.product-row').length;
  491. }
  492. function getProductInfo(productId, row) {
  493. if (!productId) return;
  494. $.ajax({
  495. url: 'get_product_info.php',
  496. type: 'GET',
  497. data: {id: productId},
  498. dataType: 'json',
  499. success: function(data) {
  500. if (data) {
  501. row.find('.unit-input').val(data.unit);
  502. // 只有当数据库中有价格信息时才设置价格
  503. if (data.price && data.price !== '0' && data.price !== '0.00') {
  504. //默认不设置单价
  505. //row.find('.price-input').val(data.price);
  506. }
  507. calculateItemTotal(row.find('.price-input')[0]);
  508. }
  509. }
  510. });
  511. }
  512. function calculateItemTotal(element) {
  513. var row = $(element).closest('.product-row');
  514. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  515. var priceInput = row.find('.price-input');
  516. var priceValue = priceInput.val().trim();
  517. var price = (priceValue === '') ? 0 : (parseFloat(priceValue) || 0);
  518. var total = quantity * price;
  519. if (total < 0) total = 0;
  520. row.find('.total-price-input').val(total.toFixed(2));
  521. calculateOrderTotal();
  522. }
  523. function calculateOrderTotal() {
  524. var total = 0;
  525. $('.total-price-input').each(function() {
  526. total += parseFloat($(this).val()) || 0;
  527. });
  528. // 更新总额和隐藏的小计字段
  529. $('#total-amount').text(total.toFixed(2));
  530. $('#total-amount-input').val(total.toFixed(2));
  531. $('#subtotal-input').val(total.toFixed(2)); // 为兼容性保留
  532. }
  533. function loadCustomerContacts(customerId) {
  534. if (customerId) {
  535. $.ajax({
  536. url: 'get_customer_contacts.php',
  537. type: 'GET',
  538. data: {customer_id: customerId},
  539. dataType: 'json',
  540. success: function(data) {
  541. var options = '<option value="">请选择联系人</option>';
  542. if (data && data.length > 0) {
  543. for (var i = 0; i < data.length; i++) {
  544. options += '<option value="' + data[i].id + '">' + data[i].contact_name + '</option>';
  545. }
  546. }
  547. $('#contact_id').html(options);
  548. }
  549. });
  550. } else {
  551. $('#contact_id').html('<option value="">请选择联系人</option>');
  552. }
  553. }
  554. function validateOrderForm() {
  555. var orderCode = $('#order_code').val();
  556. var customerId = $('#customer_id').val();
  557. var customerName = $('.customer-search').val();
  558. var contactId = $('#contact_id').val();
  559. var hasProducts = false;
  560. var allPricesValid = true;
  561. var firstEmptyPriceField = null;
  562. $('.product-id-input').each(function() {
  563. if ($(this).val()) {
  564. hasProducts = true;
  565. // 检查该产品的单价是否已填写
  566. var row = $(this).closest('.product-row');
  567. var priceInput = row.find('.price-input');
  568. var priceValue = priceInput.val().trim();
  569. if (priceValue === '' || isNaN(parseFloat(priceValue))) {
  570. allPricesValid = false;
  571. if (!firstEmptyPriceField) {
  572. firstEmptyPriceField = priceInput;
  573. }
  574. }
  575. }
  576. });
  577. if (!orderCode) {
  578. alert('订单编号不能为空');
  579. $('#order_code').focus();
  580. return false;
  581. }
  582. if (!customerId || customerId <= 0) {
  583. alert('请选择客户');
  584. $('.customer-search').focus();
  585. return false;
  586. }
  587. if (!customerName) {
  588. alert('客户名称不能为空');
  589. $('.customer-search').focus();
  590. return false;
  591. }
  592. if (!contactId) {
  593. alert('请选择联系人');
  594. $('#contact_id').focus();
  595. return false;
  596. }
  597. if (!hasProducts) {
  598. alert('请至少添加一个产品');
  599. $('#product-search-single').focus();
  600. return false;
  601. }
  602. if (!allPricesValid) {
  603. alert('请为所有产品输入有效的单价');
  604. if (firstEmptyPriceField) {
  605. firstEmptyPriceField.focus();
  606. }
  607. return false;
  608. }
  609. return true;
  610. }
  611. function updateNoProductsMessage() {
  612. if ($('.product-row').length > 0) {
  613. $('#no-products-message').hide();
  614. } else {
  615. $('#no-products-message').show();
  616. }
  617. }
  618. </script>
  619. </div>
  620. </body>
  621. </html>