order_edit.php 50 KB

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