order_add.php 25 KB

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