order_add.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
  132. <div id="product-container">
  133. <div class="product-row" data-index="0">
  134. <span class="delete-product">×</span>
  135. <div>
  136. <label>产品:</label>
  137. <div style="display: inline-block; width: 60%;" class="productinput">
  138. <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;">
  139. <div class="productlist"><ul></ul></div>
  140. <input type="hidden" name="items[0][product_id]" class="product-id-input" value="">
  141. <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
  142. </div>
  143. </div>
  144. <div style="margin-top: 5px;">
  145. <label>数量:</label>
  146. <input type="number" name="items[0][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  147. <label style="margin-left: 10px;">单位:</label>
  148. <input type="text" name="items[0][unit]" value="" class="unit-input">
  149. <label style="margin-left: 10px;">单价:</label>
  150. <input type="number" step="0.01" name="items[0][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
  151. </div>
  152. <div style="margin-top: 5px;">
  153. <label>总价:</label>
  154. <input type="number" step="0.01" name="items[0][total_price]" value="0.00" class="total-price-input" readonly>
  155. <label style="margin-left: 10px;">备注:</label>
  156. <input type="text" name="items[0][notes]" value="" style="width: 60%;">
  157. </div>
  158. </div>
  159. </div>
  160. <div class="total-section">
  161. <div>
  162. <label>订单小计:</label>
  163. <span id="subtotal">0.00</span>
  164. <input type="hidden" name="subtotal" id="subtotal-input" value="0.00">
  165. </div>
  166. <div style="margin-top: 5px;">
  167. <label>订单折扣:</label>
  168. <input type="number" step="0.01" name="discount_amount" id="order-discount" value="0.00" onchange="calculateOrderTotal()">
  169. </div>
  170. <div style="margin-top: 5px;">
  171. <label>订单总额:</label>
  172. <span id="total-amount">0.00</span>
  173. <input type="hidden" name="total_amount" id="total-amount-input" value="0.00">
  174. </div>
  175. </div>
  176. </td>
  177. </tr>
  178. <tr>
  179. <th width="8%">订单备注</th>
  180. <td>
  181. <textarea name="notes" rows="3" style="width: 90%;"></textarea>
  182. </td>
  183. </tr>
  184. <tr>
  185. <th></th>
  186. <td>
  187. <input type="submit" name="save" value="保存订单" class="btn1">
  188. <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
  189. </td>
  190. </tr>
  191. </tbody>
  192. </table>
  193. </form>
  194. <script>
  195. var productIndex = 1;
  196. $(document).ready(function() {
  197. // 初始化计算
  198. calculateOrderTotal();
  199. // 添加产品行
  200. $('#add-product-btn').click(function() {
  201. addProductRow();
  202. });
  203. // 删除产品行
  204. $(document).on('click', '.delete-product', function() {
  205. if ($('.product-row').length > 1) {
  206. $(this).closest('.product-row').remove();
  207. reindexProductRows();
  208. calculateOrderTotal();
  209. } else {
  210. alert('订单至少需要一个产品');
  211. }
  212. });
  213. // 客户搜索功能
  214. var customerSearchTimeout = null;
  215. $(document).on('keyup', '.customer-search', function() {
  216. var $this = $(this);
  217. var searchTerm = $this.val().trim();
  218. var customerList = $('.customerlist');
  219. // 清除之前的超时
  220. clearTimeout(customerSearchTimeout);
  221. // 隐藏之前的结果
  222. customerList.hide();
  223. // 如果搜索词少于2个字符,不执行搜索
  224. if (searchTerm.length < 2) {
  225. return;
  226. }
  227. // 设置一个300毫秒的超时,以减少请求数量
  228. customerSearchTimeout = setTimeout(function() {
  229. $.ajax({
  230. url: 'get_customer_search.php',
  231. type: 'GET',
  232. data: {search: searchTerm},
  233. dataType: 'json',
  234. success: function(data) {
  235. var ul = customerList.find('ul');
  236. ul.empty();
  237. if (data && data.customers && data.customers.length > 0) {
  238. $.each(data.customers, function(i, customer) {
  239. var displayText = customer.cs_company;
  240. if (customer.cs_code) {
  241. displayText = customer.cs_code + ' - ' + displayText;
  242. }
  243. ul.append('<li data-id="' + customer.id + '" data-code="' + (customer.cs_code || '') + '">' +
  244. displayText + '</li>');
  245. });
  246. customerList.show();
  247. }
  248. },
  249. error: function() {
  250. console.log('搜索客户失败,请重试');
  251. }
  252. });
  253. }, 300);
  254. });
  255. // 点击选择客户
  256. $(document).on('click', '.customerlist li', function() {
  257. var $this = $(this);
  258. var customerId = $this.data('id');
  259. var customerName = $this.text();
  260. var customerCode = $this.data('code') || '';
  261. // 设置选中的客户ID和名称
  262. $('#customer_id').val(customerId);
  263. $('.customer-search').val(customerName);
  264. $('.selected-customer-info').text(customerName);
  265. $('.customerlist').hide();
  266. // 加载客户联系人
  267. loadCustomerContacts(customerId);
  268. });
  269. // 实时产品搜索功能 - 使用防抖动以减少请求数量
  270. var searchTimeout = null;
  271. $(document).on('keyup', '.product-search', function() {
  272. var $this = $(this);
  273. var row = $this.closest('.product-row');
  274. var searchTerm = $this.val().trim();
  275. var productList = row.find('.productlist');
  276. // 清除之前的超时
  277. clearTimeout(searchTimeout);
  278. // 隐藏之前的结果
  279. productList.hide();
  280. // 如果搜索词少于2个字符,不执行搜索
  281. if (searchTerm.length < 2) {
  282. return;
  283. }
  284. // 设置一个300毫秒的超时,以减少请求数量
  285. searchTimeout = setTimeout(function() {
  286. $.ajax({
  287. url: 'get_product_info.php',
  288. type: 'GET',
  289. data: {search: searchTerm},
  290. dataType: 'json',
  291. success: function(data) {
  292. var productList = row.find('.productlist');
  293. var ul = productList.find('ul');
  294. ul.empty();
  295. if (data && data.products && data.products.length > 0) {
  296. $.each(data.products, function(i, product) {
  297. ul.append('<li data-id="' + product.id + '">' +
  298. product.ProductName +
  299. (product.category_name ? ' <span class="category-tag">(' + product.category_name + ')</span>' : '') +
  300. '</li>');
  301. });
  302. productList.show();
  303. }
  304. },
  305. error: function() {
  306. console.log('搜索产品失败,请重试');
  307. }
  308. });
  309. }, 300);
  310. });
  311. // 点击选择产品
  312. $(document).on('click', '.productlist li', function() {
  313. var $this = $(this);
  314. var productId = $this.data('id');
  315. var productName = $this.text(); // 这会包含分类名,需要清理
  316. var categoryTag = $this.find('.category-tag').text();
  317. // 清理产品名称,移除分类信息
  318. if (categoryTag) {
  319. productName = productName.replace(categoryTag, '').trim();
  320. }
  321. var row = $this.closest('.product-row');
  322. // 设置选中的产品ID和名称
  323. row.find('.product-id-input').val(productId);
  324. row.find('.product-search').val(productName);
  325. // 显示包含分类的完整产品信息
  326. var displayName = productName;
  327. if (categoryTag) {
  328. displayName += ' <span class="category-tag">' + categoryTag + '</span>';
  329. }
  330. row.find('.selected-product-info').html(displayName);
  331. row.find('.productlist').hide();
  332. // 获取产品信息
  333. getProductInfo(productId, row);
  334. });
  335. // 点击其他地方隐藏下拉列表
  336. $(document).on('click', function(e) {
  337. if (!$(e.target).closest('.productinput').length) {
  338. $('.productlist').hide();
  339. }
  340. if (!$(e.target).closest('.customerinput').length) {
  341. $('.customerlist').hide();
  342. }
  343. });
  344. });
  345. function addProductRow() {
  346. var html = `
  347. <div class="product-row" data-index="${productIndex}">
  348. <span class="delete-product">×</span>
  349. <div>
  350. <label>产品:</label>
  351. <div style="display: inline-block; width: 60%;" class="productinput">
  352. <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;">
  353. <div class="productlist"><ul></ul></div>
  354. <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
  355. <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
  356. </div>
  357. </div>
  358. <div style="margin-top: 5px;">
  359. <label>数量:</label>
  360. <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  361. <label style="margin-left: 10px;">单位:</label>
  362. <input type="text" name="items[${productIndex}][unit]" value="" class="unit-input">
  363. <label style="margin-left: 10px;">单价:</label>
  364. <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
  365. </div>
  366. <div style="margin-top: 5px;">
  367. <label>总价:</label>
  368. <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
  369. <label style="margin-left: 10px;">备注:</label>
  370. <input type="text" name="items[${productIndex}][notes]" value="" style="width: 60%;">
  371. </div>
  372. </div>
  373. `;
  374. $('#product-container').append(html);
  375. productIndex++;
  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. row.find('.price-input').val(data.price);
  401. calculateItemTotal(row.find('.price-input')[0]);
  402. }
  403. }
  404. });
  405. }
  406. function calculateItemTotal(element) {
  407. var row = $(element).closest('.product-row');
  408. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  409. var price = parseFloat(row.find('.price-input').val()) || 0;
  410. var total = quantity * price;
  411. if (total < 0) total = 0;
  412. row.find('.total-price-input').val(total.toFixed(2));
  413. calculateOrderTotal();
  414. }
  415. function calculateOrderTotal() {
  416. var subtotal = 0;
  417. $('.total-price-input').each(function() {
  418. subtotal += parseFloat($(this).val()) || 0;
  419. });
  420. var orderDiscount = parseFloat($('#order-discount').val()) || 0;
  421. var total = subtotal - orderDiscount;
  422. if (total < 0) total = 0;
  423. $('#subtotal').text(subtotal.toFixed(2));
  424. $('#subtotal-input').val(subtotal.toFixed(2));
  425. $('#total-amount').text(total.toFixed(2));
  426. $('#total-amount-input').val(total.toFixed(2));
  427. }
  428. function loadCustomerContacts(customerId) {
  429. if (customerId) {
  430. $.ajax({
  431. url: 'get_customer_contacts.php',
  432. type: 'GET',
  433. data: {customer_id: customerId},
  434. dataType: 'json',
  435. success: function(data) {
  436. var options = '<option value="">请选择联系人</option>';
  437. if (data && data.length > 0) {
  438. for (var i = 0; i < data.length; i++) {
  439. options += '<option value="' + data[i].id + '">' + data[i].contact_name + '</option>';
  440. }
  441. }
  442. $('#contact_id').html(options);
  443. }
  444. });
  445. } else {
  446. $('#contact_id').html('<option value="">请选择联系人</option>');
  447. }
  448. }
  449. function validateOrderForm() {
  450. var orderCode = $('#order_code').val();
  451. var customerId = $('#customer_id').val();
  452. var customerName = $('.customer-search').val();
  453. var hasProducts = false;
  454. $('.product-id-input').each(function() {
  455. if ($(this).val()) {
  456. hasProducts = true;
  457. return false; // break the loop
  458. }
  459. });
  460. if (!orderCode) {
  461. alert('订单编号不能为空');
  462. $('#order_code').focus();
  463. return false;
  464. }
  465. if (!customerId || customerId == '0') {
  466. alert('请选择客户');
  467. $('.customer-search').focus();
  468. return false;
  469. }
  470. if (!customerName) {
  471. alert('请输入并选择有效的客户');
  472. $('.customer-search').focus();
  473. return false;
  474. }
  475. if (!hasProducts) {
  476. alert('请至少选择一个产品');
  477. return false;
  478. }
  479. return true;
  480. }
  481. </script>
  482. </div>
  483. </body>
  484. </html>