order_add.php 23 KB

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