order_edit.php 45 KB

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