order_edit.php 47 KB

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