order_add.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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: 8px;
  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: 2;
  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.5;
  95. min-width: 60px;
  96. }
  97. .product-price {
  98. flex: 1;
  99. min-width: 100px;
  100. }
  101. .product-total {
  102. flex: 1;
  103. min-width: 100px;
  104. font-weight: bold;
  105. }
  106. .product-row input[type="number"] {
  107. width: 80px;
  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. </style>
  241. </head>
  242. <body>
  243. <div id="man_zone">
  244. <form name="form1" id="form1" method="post" action="order_save.php?<?= $hrefstr ?>" onsubmit="return validateOrderForm()">
  245. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  246. <tbody>
  247. <tr>
  248. <th width="8%">销售订单号</th>
  249. <td><input type="text" id="order_code" name="order_code" value="" class="txt1" placeholder="请输入销售订单号" /></td>
  250. </tr>
  251. <tr>
  252. <th width="8%">客户选择</th>
  253. <td>
  254. <?php if ($customerInfo): ?>
  255. <!-- 已有客户ID,只显示客户信息 -->
  256. <div style="display: inline-block; width: 60%;">
  257. <input type="hidden" name="customer_id" id="customer_id" value="<?= $customerId ?>">
  258. <!-- 联系人字段设为空 -->
  259. <input type="hidden" name="contact_id" id="contact_id" value="">
  260. <div class="selected-customer-info" style="font-weight: bold; padding: 8px 0; border: 1px solid #ddd; background-color: #f9f9f9; padding-left: 10px;">
  261. <?= htmlspecialcharsFix($customerInfo['cs_code'] . ' - ' . $customerInfo['cs_company']) ?>
  262. </div>
  263. </div>
  264. <?php else: ?>
  265. <!-- 无客户ID,显示搜索框 -->
  266. <div style="display: inline-block; width: 60%;" class="customerinput">
  267. <input type="text" class="customer-search fastsearch" placeholder="输入客户名称搜索..." style="width: 100%;">
  268. <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);">
  269. <ul style="list-style: none; padding: 0; margin: 0;"></ul>
  270. </div>
  271. <input type="hidden" name="customer_id" id="customer_id" value="0">
  272. <input type="hidden" name="contact_id" id="contact_id" value="">
  273. <div class="selected-customer-info" style="margin-top: 5px; font-weight: bold;"></div>
  274. </div>
  275. <?php endif; ?>
  276. </td>
  277. </tr>
  278. <tr>
  279. <th width="8%">订单日期</th>
  280. <td><input type="date" id="order_date" name="order_date" value="<?= date('Y-m-d') ?>" class="txt1" /></td>
  281. </tr>
  282. <tr>
  283. <th width="8%" valign="top">产品列表</th>
  284. <td>
  285. <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
  286. <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: 8px;">
  287. <div style="flex: 2; min-width: 200px;">产品</div>
  288. <div style="flex: 2; min-width: 200px;">规格</div>
  289. <div style="flex: 1; min-width: 80px;">数量</div>
  290. <div style="flex: 0.5; min-width: 60px;">单位</div>
  291. <div style="flex: 1; min-width: 100px;">单价</div>
  292. <div style="flex: 1; min-width: 100px;">总价</div>
  293. </div>
  294. <div id="product-container">
  295. <!-- 产品行将通过添加按钮添加 -->
  296. <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;">
  297. <i class="fa fa-info-circle"></i> 请点击"添加产品"按钮添加产品
  298. </div>
  299. </div>
  300. <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);">
  301. <div style="display: flex; justify-content: flex-end; align-items: center;">
  302. <label style="font-size: 16px; margin-right: 10px;">订单总额:</label>
  303. <span id="total-amount" style="font-size: 18px; font-weight: bold; color: #e74c3c;">0.00</span>
  304. <input type="hidden" name="total_amount" id="total-amount-input" value="0.00">
  305. <input type="hidden" name="subtotal" id="subtotal-input" value="0.00">
  306. </div>
  307. </div>
  308. </td>
  309. </tr>
  310. <tr>
  311. <th width="8%">订单备注</th>
  312. <td>
  313. <textarea name="notes" rows="3" style="width: 90%;"></textarea>
  314. </td>
  315. </tr>
  316. <tr>
  317. <th></th>
  318. <td>
  319. <input type="submit" name="save" value="保存订单" class="btn1">
  320. <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
  321. </td>
  322. </tr>
  323. </tbody>
  324. </table>
  325. </form>
  326. <script>
  327. var productIndex = 1;
  328. $(document).ready(function() {
  329. // 初始化计算
  330. calculateOrderTotal();
  331. // 移除第一个默认产品行(如果不需要的话)
  332. if ($('.product-row').length > 0) {
  333. $('.product-row').first().remove();
  334. }
  335. // 添加默认的产品行
  336. addEmptyProductRow();
  337. updateNoProductsMessage();
  338. // 添加产品行按钮点击事件
  339. $('#add-product-btn').on('click', function() {
  340. addEmptyProductRow();
  341. updateNoProductsMessage();
  342. });
  343. // 删除产品行
  344. $(document).on('click', '.delete-product', function() {
  345. if ($('.product-row').length > 1) {
  346. $(this).closest('.product-row').remove();
  347. reindexProductRows();
  348. calculateOrderTotal();
  349. updateNoProductsMessage();
  350. } else {
  351. // 如果只剩最后一个产品行,则清空它而不是删除
  352. $(this).closest('.product-row').remove();
  353. updateNoProductsMessage();
  354. calculateOrderTotal(); // 确保在删除最后一个产品时也更新总额
  355. }
  356. });
  357. // 客户搜索功能
  358. var customerSearchTimeout = null;
  359. var customerIsComposing = false;
  360. // 监听输入法组合事件
  361. $(document).on('compositionstart', '.customer-search', function() {
  362. customerIsComposing = true;
  363. });
  364. $(document).on('compositionend', '.customer-search', function() {
  365. customerIsComposing = false;
  366. $(this).trigger('input'); // 手动触发一次input事件
  367. });
  368. $(document).on('input', '.customer-search', function() {
  369. // 如果是输入法正在组合中文,不处理
  370. if (customerIsComposing) return;
  371. var $this = $(this);
  372. var searchTerm = $this.val().trim();
  373. var customerList = $('.customerlist');
  374. // 清除之前的超时
  375. clearTimeout(customerSearchTimeout);
  376. // 隐藏之前的结果
  377. customerList.hide();
  378. // 如果搜索词少于2个字符,不执行搜索
  379. if (searchTerm.length < 1) {
  380. return;
  381. }
  382. // 设置一个300毫秒的超时,以减少请求数量
  383. customerSearchTimeout = setTimeout(function() {
  384. $.ajax({
  385. url: 'get_customer_search.php',
  386. type: 'GET',
  387. data: {search: searchTerm},
  388. dataType: 'json',
  389. success: function(data) {
  390. var ul = customerList.find('ul');
  391. ul.empty();
  392. if (data && data.customers && data.customers.length > 0) {
  393. $.each(data.customers, function(i, customer) {
  394. var displayText = customer.cs_company;
  395. if (customer.cs_code) {
  396. displayText = customer.cs_code + ' - ' + displayText;
  397. }
  398. ul.append('<li data-id="' + customer.id + '" data-code="' + (customer.cs_code || '') + '">' +
  399. displayText + '</li>');
  400. });
  401. customerList.show();
  402. }
  403. },
  404. error: function() {
  405. console.log('搜索客户失败,请重试');
  406. }
  407. });
  408. }, 300);
  409. });
  410. // 点击选择客户
  411. $(document).on('click', '.customerlist li', function() {
  412. var $this = $(this);
  413. var customerId = $this.data('id');
  414. var customerName = $this.text();
  415. var customerCode = $this.data('code') || '';
  416. // 设置选中的客户ID和名称
  417. $('#customer_id').val(customerId);
  418. $('.customer-search').val(customerName);
  419. $('.selected-customer-info').text(customerName);
  420. $('.customerlist').hide();
  421. // 加载客户联系人
  422. loadCustomerContacts(customerId);
  423. });
  424. // 产品搜索框聚焦时显示搜索提示
  425. $(document).on('focus', '.product-search', function() {
  426. // 隐藏所有已打开的产品搜索列表
  427. $('.productlist').hide();
  428. // 当前搜索框的值
  429. var searchTerm = $(this).val().trim();
  430. if (searchTerm.length >= 2) {
  431. // 找到当前行的产品列表并显示
  432. $(this).siblings('.productlist').show();
  433. }
  434. });
  435. // 产品搜索功能 - 行内搜索
  436. var productSearchTimeouts = {};
  437. var isComposing = false;
  438. // 监听输入法组合事件
  439. $(document).on('compositionstart', '.product-search', function() {
  440. isComposing = true;
  441. });
  442. $(document).on('compositionend', '.product-search', function() {
  443. isComposing = false;
  444. $(this).trigger('input'); // 手动触发一次input事件
  445. });
  446. $(document).on('input', '.product-search', function() {
  447. // 如果是输入法正在组合中文,不处理
  448. if (isComposing) return;
  449. var $this = $(this);
  450. var rowIndex = $this.closest('.product-row').data('index');
  451. var searchTerm = $this.val().trim();
  452. var productList = $this.siblings('.productlist');
  453. // 清除之前的超时
  454. clearTimeout(productSearchTimeouts[rowIndex]);
  455. // 隐藏之前的结果
  456. productList.hide();
  457. // 如果搜索词少于2个字符,不执行搜索
  458. if (searchTerm.length < 1) {
  459. return;
  460. }
  461. console.warn(searchTerm);
  462. // 设置一个300毫秒的超时,以减少请求数量
  463. productSearchTimeouts[rowIndex] = setTimeout(function() {
  464. $.ajax({
  465. url: 'get_product_info.php',
  466. type: 'GET',
  467. data: {search: searchTerm},
  468. dataType: 'json',
  469. success: function(data) {
  470. var ul = productList.find('ul');
  471. ul.empty();
  472. if (data && data.products && data.products.length > 0) {
  473. $.each(data.products, function(i, product) {
  474. ul.append('<li data-id="' + product.id + '">' +
  475. product.ProductName +
  476. (product.category_name ? ' <span class="category-tag">(' + product.category_name + ')</span>' : '') +
  477. '</li>');
  478. });
  479. productList.show();
  480. }
  481. },
  482. error: function() {
  483. console.log('搜索产品失败,请重试');
  484. }
  485. });
  486. }, 300);
  487. });
  488. // 点击选择产品 - 行内搜索结果
  489. $(document).on('click', '.productlist li', function() {
  490. var $this = $(this);
  491. var productId = $this.data('id');
  492. var productName = $this.text(); // 这会包含分类名,需要清理
  493. var categoryTag = $this.find('.category-tag').text();
  494. var row = $this.closest('.product-row');
  495. // 清理产品名称,移除分类信息
  496. if (categoryTag) {
  497. productName = productName.replace(categoryTag, '').trim();
  498. }
  499. // 设置产品ID和名称
  500. row.find('.product-id-input').val(productId);
  501. row.find('.product-search').hide();
  502. // 显示包含分类的完整产品信息
  503. var displayName = productName;
  504. if (categoryTag) {
  505. displayName += ' <span class="category-tag">' + categoryTag + '</span>';
  506. }
  507. row.find('.selected-product-info').html(displayName).show();
  508. // 隐藏产品搜索结果
  509. row.find('.productlist').hide();
  510. // 获取产品规格信息
  511. getProductSpecifications(productId, row);
  512. });
  513. // 规格选择改变事件
  514. $(document).on('change', '.spec-select', function() {
  515. var $this = $(this);
  516. var row = $this.closest('.product-row');
  517. var specId = $this.val();
  518. var specData = $this.find('option:selected').data();
  519. if (specId) {
  520. // 检查是否已存在相同的产品规格组合
  521. var isDuplicate = false;
  522. $('.spec-select').not($this).each(function() {
  523. if ($(this).val() == specId) {
  524. isDuplicate = true;
  525. return false; // 跳出循环
  526. }
  527. });
  528. if (isDuplicate) {
  529. alert('该产品规格已添加,不能重复添加');
  530. $this.val(''); // 重置选择
  531. return;
  532. }
  533. // 设置规格ID到隐藏字段
  534. row.find('.spec-id-input').val(specId);
  535. // 不再自动设置价格信息
  536. // if (specData.price) {
  537. // row.find('.price-input').val(specData.price);
  538. // } else {
  539. // row.find('.price-input').val('');
  540. // }
  541. // 存储规格价格作为最低价格限制
  542. var minPrice = specData.price || 0;
  543. var priceInput = row.find('.price-input');
  544. priceInput.attr('data-min-price', minPrice);
  545. if (minPrice > 0) {
  546. priceInput.attr('placeholder', '输入单价');
  547. } else {
  548. priceInput.attr('placeholder', '输入单价');
  549. }
  550. // 设置最小订购数量
  551. var minQty = specData.minQty || 1;
  552. var qtyInput = row.find('.quantity-input');
  553. if (parseInt(qtyInput.val()) < minQty) {
  554. qtyInput.val(minQty);
  555. }
  556. qtyInput.attr('min', minQty);
  557. // 更新规格信息显示
  558. // var specInfoHtml = '';
  559. // if (specData.code) {
  560. // specInfoHtml += '<span>编码: ' + specData.code + '</span>';
  561. // }
  562. // if (specData.minQty > 1) {
  563. // specInfoHtml += ' <span>最小订购量: ' + specData.minQty + '</span>';
  564. // }
  565. // if (specData.price) {
  566. // specInfoHtml += ' <span>参考价格: ¥' + specData.price + '</span>';
  567. // }
  568. // row.find('.spec-info').html(specInfoHtml);
  569. // 重新计算总价
  570. calculateItemTotal(row.find('.price-input')[0]);
  571. } else {
  572. // 清除规格相关信息
  573. row.find('.spec-id-input').val('');
  574. // 不再清除价格
  575. // row.find('.price-input').val('');
  576. row.find('.price-input').attr('data-min-price', '0').attr('placeholder', '输入单价');
  577. row.find('.spec-info').html('');
  578. calculateItemTotal(row.find('.price-input')[0]);
  579. }
  580. });
  581. // 点击其他地方隐藏下拉列表
  582. $(document).on('click', function(e) {
  583. if (!$(e.target).closest('.product-search').length && !$(e.target).closest('.productlist').length) {
  584. $('.productlist').hide();
  585. }
  586. if (!$(e.target).closest('.customerinput').length) {
  587. $('.customerlist').hide();
  588. }
  589. });
  590. // 点击已选产品名显示的标签时,切换回搜索模式
  591. $(document).on('click', '.selected-product-info', function() {
  592. var row = $(this).closest('.product-row');
  593. var productId = row.find('.product-id-input').val();
  594. // 只有当已经选择了产品时才允许重新选择
  595. if (productId) {
  596. if (confirm('确定要重新选择产品吗?这将清除当前选择的产品及其规格信息。')) {
  597. // 清空产品ID和选择的规格
  598. row.find('.product-id-input').val('');
  599. row.find('.spec-id-input').val('');
  600. // 隐藏产品信息,显示搜索框
  601. $(this).hide();
  602. row.find('.product-search').val('').show();
  603. // 隐藏并清空规格选择
  604. row.find('.spec-select').hide().empty();
  605. row.find('.spec-info').html('');
  606. // 清除单位信息
  607. row.find('.unit-input').val('');
  608. row.find('.unit-label').text('');
  609. // 清除价格信息并重新计算总额
  610. row.find('.price-input').val('');
  611. calculateItemTotal(row.find('.price-input')[0]);
  612. }
  613. }
  614. });
  615. });
  616. function addEmptyProductRow() {
  617. var html = `
  618. <div class="product-row" data-index="${productIndex}">
  619. <span class="delete-product">×</span>
  620. <div class="row-section product-info">
  621. <div class="row-section-label"><span>产品</span></div>
  622. <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
  623. <input type="text" class="product-search" placeholder="输入产品名称搜索..." style="width: 100%; padding: 5px; border: 1px solid #ddd; border-radius: 4px;">
  624. <div class="productlist" style="width: 100%; max-height: 200px;"><ul></ul></div>
  625. <div class="selected-product-info" style="cursor: pointer; display: none;" title="点击重新选择产品"></div>
  626. </div>
  627. <div class="row-section product-spec">
  628. <div class="row-section-label"><span>规格</span></div>
  629. <input type="hidden" name="items[${productIndex}][spec_id]" class="spec-id-input" value="">
  630. <select class="spec-select" name="items[${productIndex}][spec_select]" style="display: none;">
  631. <option value="">请选择规格</option>
  632. </select>
  633. <div class="spec-info"></div>
  634. </div>
  635. <div class="row-section product-quantity">
  636. <div class="row-section-label"><span>数量</span></div>
  637. <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  638. </div>
  639. <div class="row-section product-unit">
  640. <div class="row-section-label"><span>单位</span></div>
  641. <span class="unit-label" style="display: inline-block; padding: 2px 5px; min-width: 30px;"></span>
  642. <input type="hidden" name="items[${productIndex}][unit]" class="unit-input" value="">
  643. </div>
  644. <div class="row-section product-price">
  645. <div class="row-section-label"><span>单价</span></div>
  646. <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="" class="price-input" placeholder="输入单价" onchange="calculateItemTotal(this)">
  647. </div>
  648. <div class="row-section product-total">
  649. <div class="row-section-label"><span>总价</span></div>
  650. <span class="total-price-display" style="display: inline-block; background-color: #f0f0f0; border: 1px solid #ddd; padding: 2px 8px; min-width: 60px; text-align: right;">0.00</span>
  651. <input type="hidden" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input">
  652. </div>
  653. </div>
  654. `;
  655. $('#product-container').append(html);
  656. productIndex++;
  657. return productIndex - 1; // 返回新添加行的索引
  658. }
  659. function getProductSpecifications(productId, row) {
  660. if (!productId) return;
  661. $.ajax({
  662. url: 'get_product_info.php',
  663. type: 'GET',
  664. data: {product_id: productId},
  665. dataType: 'json',
  666. success: function(data) {
  667. if (data && data.product && data.specifications) {
  668. // 恢复设置单位信息
  669. if (data.product.unit) {
  670. row.find('.unit-input').val(data.product.unit);
  671. row.find('.unit-label').text(data.product.unit);
  672. }
  673. // 处理规格信息
  674. var specifications = data.specifications;
  675. if (specifications.length === 0) {
  676. alert('此产品没有规格信息,请添加规格后再选择');
  677. // 重置产品选择
  678. row.find('.product-id-input').val('');
  679. row.find('.selected-product-info').hide();
  680. row.find('.product-search').val('').show();
  681. return;
  682. }
  683. // 填充规格选择下拉框
  684. var specSelect = row.find('.spec-select');
  685. specSelect.empty();
  686. specSelect.append('<option value="">请选择规格</option>');
  687. $.each(specifications, function(i, spec) {
  688. var displayText = spec.spec_name + ': ' + spec.spec_value;
  689. if (spec.price) {
  690. displayText += ' (¥' + spec.price + ')';
  691. }
  692. var option = $('<option></option>')
  693. .attr('value', spec.id)
  694. .text(displayText)
  695. .data('price', spec.price)
  696. .data('minQty', spec.min_order_quantity)
  697. .data('code', spec.spec_code);
  698. specSelect.append(option);
  699. });
  700. // 显示规格选择下拉框
  701. specSelect.show();
  702. } else {
  703. alert('获取产品规格失败');
  704. // 重置产品选择
  705. row.find('.product-id-input').val('');
  706. row.find('.selected-product-info').hide();
  707. row.find('.product-search').val('').show();
  708. }
  709. },
  710. error: function() {
  711. alert('获取产品规格失败,请重试');
  712. // 重置产品选择
  713. row.find('.product-id-input').val('');
  714. row.find('.selected-product-info').hide();
  715. row.find('.product-search').val('').show();
  716. }
  717. });
  718. }
  719. function reindexProductRows() {
  720. $('.product-row').each(function(index) {
  721. $(this).attr('data-index', index);
  722. $(this).find('select, input').each(function() {
  723. var name = $(this).attr('name');
  724. if (name) {
  725. name = name.replace(/items\[\d+\]/, 'items[' + index + ']');
  726. $(this).attr('name', name);
  727. }
  728. });
  729. });
  730. productIndex = $('.product-row').length;
  731. }
  732. function calculateItemTotal(element) {
  733. var row = $(element).closest('.product-row');
  734. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  735. var priceInput = row.find('.price-input');
  736. var priceValue = priceInput.val().trim();
  737. var price = (priceValue === '') ? 0 : (parseFloat(priceValue) || 0);
  738. var minPrice = parseFloat(priceInput.attr('data-min-price')) || 0;
  739. // 检查价格是否低于规格最低价
  740. if (price > 0 && minPrice > 0 && price < minPrice) {
  741. priceInput.css('color', 'red');
  742. } else {
  743. priceInput.css('color', '');
  744. }
  745. var total = quantity * price;
  746. if (total < 0) total = 0;
  747. // 更新显示元素和隐藏输入框
  748. row.find('.total-price-display').text(total.toFixed(2));
  749. row.find('.total-price-input').val(total.toFixed(2));
  750. calculateOrderTotal();
  751. }
  752. function calculateOrderTotal() {
  753. var total = 0;
  754. $('.total-price-input').each(function() {
  755. total += parseFloat($(this).val()) || 0;
  756. });
  757. // 更新总额和隐藏的小计字段
  758. $('#total-amount').text(total.toFixed(2));
  759. $('#total-amount-input').val(total.toFixed(2));
  760. $('#subtotal-input').val(total.toFixed(2)); // 为兼容性保留
  761. }
  762. function loadCustomerContacts(customerId) {
  763. if (customerId) {
  764. $.ajax({
  765. url: 'get_customer_contacts.php',
  766. type: 'GET',
  767. data: {customer_id: customerId},
  768. dataType: 'json',
  769. success: function(data) {
  770. // 不自动选择第一个联系人,让字段为空
  771. $('#contact_id').val('');
  772. }
  773. });
  774. } else {
  775. $('#contact_id').val('');
  776. }
  777. }
  778. function validateOrderForm() {
  779. var orderCode = $('#order_code').val();
  780. var customerId = $('#customer_id').val();
  781. var customerName = $('.customer-search').val();
  782. var hasProducts = false;
  783. var allSpecsSelected = true;
  784. var allPricesValid = true;
  785. var allPricesHighEnough = true;
  786. var firstInvalidField = null;
  787. // 检查是否有产品行
  788. if ($('.product-row').length === 0) {
  789. alert('请至少添加一个产品');
  790. $('#add-product-btn').focus();
  791. return false;
  792. }
  793. $('.product-row').each(function() {
  794. var productId = $(this).find('.product-id-input').val();
  795. var specId = $(this).find('.spec-id-input').val();
  796. var priceInput = $(this).find('.price-input');
  797. var priceValue = priceInput.val().trim();
  798. var price = parseFloat(priceValue) || 0;
  799. var minPrice = parseFloat(priceInput.attr('data-min-price')) || 0;
  800. var specSelect = $(this).find('.spec-select');
  801. var productSearch = $(this).find('.product-search');
  802. // 检查产品是否已选择
  803. if (!productId) {
  804. if (!firstInvalidField) {
  805. firstInvalidField = productSearch;
  806. }
  807. return true; // continue
  808. }
  809. hasProducts = true;
  810. // 检查规格是否已选择
  811. if (!specId && specSelect.is(':visible')) {
  812. allSpecsSelected = false;
  813. if (!firstInvalidField) {
  814. firstInvalidField = specSelect;
  815. }
  816. }
  817. // 检查单价是否有效
  818. if (priceValue === '' || isNaN(parseFloat(priceValue))) {
  819. allPricesValid = false;
  820. if (!firstInvalidField) {
  821. firstInvalidField = priceInput;
  822. }
  823. }
  824. // 检查单价是否大于等于规格价格
  825. if (minPrice > 0 && price < minPrice) {
  826. allPricesHighEnough = false;
  827. if (!firstInvalidField) {
  828. firstInvalidField = priceInput;
  829. }
  830. }
  831. });
  832. if (!orderCode) {
  833. alert('销售订单号不能为空');
  834. $('#order_code').focus();
  835. return false;
  836. }
  837. if (!customerId || customerId == '0') {
  838. alert('请选择客户');
  839. $('.customer-search').focus();
  840. return false;
  841. }
  842. // 只有当客户ID为0时才检查客户名称
  843. if (customerId == '0' && !customerName) {
  844. alert('请输入并选择有效的客户');
  845. $('.customer-search').focus();
  846. return false;
  847. }
  848. if (!hasProducts) {
  849. alert('请至少添加一个有效的产品');
  850. if (firstInvalidField) {
  851. firstInvalidField.focus();
  852. } else {
  853. $('#add-product-btn').focus();
  854. }
  855. return false;
  856. }
  857. if (!allSpecsSelected) {
  858. alert('请为所有产品选择规格');
  859. if (firstInvalidField) {
  860. firstInvalidField.focus();
  861. }
  862. return false;
  863. }
  864. if (!allPricesValid) {
  865. alert('请为所有产品输入有效的单价');
  866. if (firstInvalidField) {
  867. firstInvalidField.focus();
  868. }
  869. return false;
  870. }
  871. if (!allPricesHighEnough) {
  872. alert('单价不能低于规格设定的参考价格');
  873. if (firstInvalidField) {
  874. firstInvalidField.focus();
  875. }
  876. return false;
  877. }
  878. return true;
  879. }
  880. function updateNoProductsMessage() {
  881. if ($('.product-row').length > 0) {
  882. $('#no-products-message').hide();
  883. } else {
  884. $('#no-products-message').show();
  885. }
  886. }
  887. </script>
  888. </div>
  889. </body>
  890. </html>