order_add.php 37 KB

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