index.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <style>
  2. table tr th, table tr td {
  3. border: thin #ddd dashed;
  4. padding: 5px
  5. }
  6. .select2-container {
  7. width: 400px !important;
  8. }
  9. .select2-dropdown {
  10. width: 400px !important;
  11. }
  12. .select2-results__options {
  13. max-height: 200px;
  14. overflow-y: auto;
  15. }
  16. .select2-container--default .select2-selection--single .select2-selection__clear {
  17. margin-top: -6px;
  18. }
  19. .select2-container--default .select2-selection--single .select2-selection__rendered {
  20. line-height: 35px;
  21. }
  22. .select2-container--default .select2-selection--single {
  23. width: 470px !important;
  24. height: 35px !important;
  25. line-height: 38px;
  26. padding: 4px 10px;
  27. font-size: 14px;
  28. border: 1px solid #ccc;
  29. border-radius: 4px;
  30. box-sizing: border-box;
  31. }
  32. .select2-container--open .select2-dropdown--below {
  33. margin-top: -13px;
  34. }
  35. .selection {
  36. top: -13px;
  37. }
  38. .selected-row {
  39. background-color: #ddefeb !important;
  40. }
  41. #pagination-wrapper {
  42. margin-top: 20px;
  43. font-family: Arial, sans-serif;
  44. text-align: center;
  45. }
  46. #pagination-bar {
  47. display: flex;
  48. justify-content: center;
  49. align-items: center;
  50. flex-wrap: nowrap;
  51. gap: 16px;
  52. white-space: nowrap;
  53. overflow-x: auto;
  54. }
  55. #pagination-info {
  56. color: #444;
  57. }
  58. #pagination-controls {
  59. display: inline-flex;
  60. gap: 8px;
  61. flex-wrap: nowrap;
  62. }
  63. #pagination-controls button {
  64. padding: 6px 12px;
  65. border: 1px solid #ccc;
  66. background-color: #f8f8f8;
  67. cursor: pointer;
  68. border-radius: 4px;
  69. transition: all 0.2s ease;
  70. }
  71. #pagination-controls button:hover:not(:disabled) {
  72. background-color: #e0e0e0;
  73. }
  74. #pagination-controls button:disabled {
  75. background-color: #f0f0f0;
  76. cursor: not-allowed;
  77. color: #aaa;
  78. }
  79. #pagination-controls button.active {
  80. background-color: #007bff;
  81. color: white;
  82. font-weight: bold;
  83. border-color: #007bff;
  84. }
  85. #table-container {
  86. height: 200px;
  87. overflow-y: auto;
  88. border: 1px solid #ccc;
  89. margin-bottom: 10px;
  90. }
  91. #table thead th {
  92. position: sticky;
  93. top: 0;
  94. background-color: #f5f5f5;
  95. z-index: 1;
  96. }
  97. .tray-btn {
  98. padding: 5px 10px;
  99. margin-right: 8px;
  100. border: 1px solid #ccc;
  101. background-color: white;
  102. color: #000;
  103. cursor: pointer;
  104. border-radius: 4px;
  105. }
  106. .tray-btn.active {
  107. background-color: #007bff;
  108. color: white;
  109. border-color: #007bff;
  110. }
  111. .remove-btn {
  112. color: red;
  113. cursor: pointer;
  114. margin-left: 10px;
  115. }
  116. #tableBody tr {
  117. cursor: pointer;
  118. }
  119. #tableBody tr:hover {
  120. background-color: #f0f0f0;
  121. }
  122. #tableBody1 tr td {
  123. padding: 8px;
  124. }
  125. .total-row {
  126. font-weight: bold;
  127. background-color: #f5f5f5;
  128. }
  129. </style>
  130. <form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  131. <fieldset style="width: 1200px;margin:10px 10px 0px 10px ;padding: 10px 10px 0px 10px ;border:solid thin #c0c0c0">
  132. <legend style="padding: 0px;margin: 0px">生成厂商信息</legend>
  133. <div class="form-group">
  134. <label class="control-label col-xs-2 col-sm-1">生产厂商:</label>
  135. <div class="col-xs-2 col-sm-3">
  136. <input id="c-company_name" data-rule="required" disabled class="form-control" value="{$row['nickname']}" name="company_name" type="text">
  137. </div>
  138. <label class="control-label col-xs-2 col-sm-1">邮政编码:</label>
  139. <div class="col-xs-2 col-sm-3">
  140. <input id="c-postcode" data-rule="required" disabled class="form-control" value="{$row['postcode']}" name="postcode" type="text">
  141. </div>
  142. <label class="control-label col-xs-2 col-sm-1">电&nbsp;&nbsp;话:</label>
  143. <div class="col-xs-2 col-sm-3">
  144. <input id="c-mobile" data-rule="required;mobile" disabled class="form-control" value="{$row['mobile']}" name="mobile" type="text">
  145. </div>
  146. </div>
  147. <div class="form-group">
  148. <label class="control-label col-xs-2 col-sm-1">地&nbsp;&nbsp;&nbsp;&nbsp;址:</label>
  149. <div class="col-xs-2 col-sm-7">
  150. <input id="c-company_address" data-rule="required" disabled class="form-control" value="{$row['company_address']}" name="company_address" type="text">
  151. </div>
  152. <label class="control-label col-xs-2 col-sm-1">厂家编码:</label>
  153. <div class="col-xs-2 col-sm-3">
  154. <input id="c-printer_code" data-rule="required" disabled class="form-control" value="{$row['printer_code']}" name="company_address" type="text">
  155. </div>
  156. </div>
  157. </fieldset>
  158. <fieldset style="width: 1200px;margin:10px 10px 0px 10px ;padding: 10px 10px 0px 10px ;border: solid thin #c0c0c0">
  159. <legend style="padding: 0px;margin: 0px">打托选择列表</legend>
  160. <div style="margin-bottom: 10px;">
  161. <input type="text" id="searchInput" placeholder="请输入关键字搜索..." style="width: 300px;height: 35px; "/>
  162. <button type="text" id="searchBtn" className="btn btn-primary" style="height: 34px">查询</button>
  163. &nbsp;
  164. <button id="sadBtn" class="btn btn-success">
  165. <i class="fas fa-plus"></i> 新增产品
  166. </button>
  167. </div>
  168. <div id="table-container">
  169. <table id="table" style="width: 100%;border-collapse: collapse;">
  170. <thead>
  171. <tr>
  172. <th>销售订单号</th>
  173. <th>工单编号</th>
  174. <th>成品编码</th>
  175. <th>成品名称</th>
  176. <th>入库数</th>
  177. <th>发货数</th>
  178. <th>剩余数</th>
  179. </tr>
  180. </thead>
  181. <tbody id="tableBody">
  182. </tbody>
  183. </table>
  184. </div>
  185. <div id="pagination-wrapper">
  186. <div id="pagination-bar">
  187. <div id="pagination-info">共 0 条记录,第 1 / 1 页</div>
  188. <div id="pagination-controls"></div>
  189. </div>
  190. </div>
  191. </fieldset>
  192. <fieldset style="width: 1200px;margin:10px 10px 0px 10px ;padding: 10px 10px 0px 10px ;border: solid thin #c0c0c0">
  193. <legend style="padding: 0px;margin: 0px">打托选品列表</legend>
  194. <div id="table-container1">
  195. <table id="table1" style="width: 100%;border-collapse: collapse;">
  196. <thead>
  197. <tr>
  198. <th>序号</th>
  199. <th>产品名称</th>
  200. <th>应发货数量</th>
  201. <th>实际发货数量</th>
  202. <th>备注</th>
  203. <th>操作</th>
  204. </tr>
  205. </thead>
  206. <tbody id="tableBody1">
  207. </tbody>
  208. <tfoot id="tableFooter1" style="display: none;">
  209. <tr className="total-row">
  210. <td colSpan="2">合计</td>
  211. <td id="totalQuantity">0</td>
  212. <td colSpan="2"></td>
  213. </tr>
  214. </tfoot>
  215. </table>
  216. </div>
  217. </fieldset>
  218. <div class="form-group layer-footer">
  219. <label class="control-label col-xs-12 col-sm-5"></label>
  220. <div class="col-xs-12 col-sm-5"><br>
  221. <button type="submit" id="submit" class="btn btn-primary btn-embossed">保存</button></div>
  222. </div>
  223. </form>
  224. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  225. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
  226. <script>
  227. let currentPage = 1;
  228. let limit = 10;
  229. let total = 0;
  230. let selectedRow = null;
  231. let selectedProducts = [];
  232. window.selectRow = function (row) {
  233. if (selectedRow) {
  234. $(selectedRow).removeClass('selected-row');
  235. }
  236. $(row).addClass('selected-row');
  237. selectedRow = row;
  238. };
  239. function addToShipment(row) {
  240. console.log($(row).data('remaining_quantity'))
  241. if($(row).data('remaining_quantity') == 0){
  242. layer.msg('库存数量为空!', {icon: 2});
  243. return;
  244. }
  245. const rowData = {
  246. gdbh: $(row).data('gdbh'),
  247. order_ddbh: $(row).data('order_ddbh'),
  248. cpbm: $(row).data('cpbm'),
  249. cpmc: $(row).data('cpmc'),
  250. remaining_quantity: $(row).data('remaining_quantity'),
  251. remark: ''
  252. };
  253. const exists = selectedProducts.some(item => item.gdbh === rowData.gdbh);
  254. if (exists) {
  255. layer.msg('该产品已添加到本次配货中!', {icon: 2});
  256. return;
  257. }
  258. selectedProducts.push(rowData);
  259. renderShipmentTable();
  260. }
  261. function removeFromShipment(index) {
  262. selectedProducts.splice(index, 1);
  263. renderShipmentTable();
  264. }
  265. function calculateTotalQuantity() {
  266. return selectedProducts.reduce((sum, product) => sum + (parseFloat(product.remaining_quantity) || 0), 0);
  267. }
  268. function renderShipmentTable() {
  269. const tbody = $('#tableBody1');
  270. tbody.empty();
  271. if (selectedProducts.length === 0) {
  272. tbody.append('<tr><td colspan="5" style="text-align:center;">暂无配货数据</td></tr>');
  273. $('#tableFooter1').hide();
  274. return;
  275. }
  276. selectedProducts.forEach((product, index) => {
  277. const tr = $(`
  278. <tr>
  279. <td>${index + 1}</td>
  280. <td>${product.cpmc}</td>
  281. <td>${product.remaining_quantity}</td>
  282. <td><input type="text" class="quantity-input" data-index="${index}" value="${product.actual_quantity || product.remaining_quantity || ''}" placeholder="" style="width:100%;"></td>
  283. <td><input type="text" class="remark-input" data-index="${index}" value="${product.remark || ''}" placeholder="请输入备注" style="width:100%;"></td>
  284. <td><span class="remove-btn" onclick="removeFromShipment(${index})">移除</span></td>
  285. </tr>
  286. `);
  287. tbody.append(tr);
  288. });
  289. $('#tableFooter1').show();
  290. $('#totalQuantity').text(calculateTotalQuantity());
  291. // 绑定备注输入事件 - 修正后的实现
  292. $('.remark-input').off('input').on('input', function () {
  293. const index = $(this).data('index');
  294. const remark = $(this).val();
  295. if (selectedProducts[index]) {
  296. selectedProducts[index].remark = remark;
  297. }
  298. });
  299. }
  300. function loadData(page = 1, keyword = '') {
  301. $.ajax({
  302. method: "GET",
  303. url: "/qcode_add/product",
  304. data: {page, limit, search: keyword},
  305. dataType: "json",
  306. success: function (res) {
  307. const data = res.data || [];
  308. total = res.total || 0;
  309. currentPage = res.page || 1;
  310. limit = res.limit || 10;
  311. const tbody = $('#tableBody');
  312. tbody.empty();
  313. selectedRow = null;
  314. if (data.length === 0) {
  315. tbody.append('<tr><td colspan="7">无数据</td></tr>');
  316. return;
  317. }
  318. data.forEach(item => {
  319. const tr = $(`
  320. <tr onclick="selectRow(this)"
  321. data-order_ddbh="${item.order_ddbh}"
  322. data-gdbh="${item.gdbh}"
  323. data-cpbm="${item.cpbm}"
  324. data-cpmc="${item.cpmc}"
  325. data-sl="${item.sl}"
  326. data-total_chu_quantity="${item.total_chu_quantity}"
  327. data-remaining_quantity="${item.remaining_quantity}">
  328. <td>${item.order_ddbh}</td>
  329. <td>${item.gdbh}</td>
  330. <td>${item.cpbm}</td>
  331. <td>${item.cpmc}</td>
  332. <td>${item.sl}</td>
  333. <td>${item.total_chu_quantity}</td>
  334. <td>${item.remaining_quantity}</td>
  335. </tr>`);
  336. tbody.append(tr);
  337. });
  338. renderPagination();
  339. }
  340. });
  341. }
  342. function renderPagination() {
  343. const pageCount = Math.ceil(total / limit);
  344. const info = $('#pagination-info');
  345. const wrapper = $('#pagination-controls');
  346. const keyword = $('#searchInput').val().trim();
  347. wrapper.empty();
  348. info.text(`共 ${total} 条记录,第 ${currentPage} / ${pageCount} 页`);
  349. const prev = $('<button type="button">上一页</button>').prop('disabled', currentPage <= 1);
  350. prev.on('click', () => loadData(currentPage - 1, keyword));
  351. wrapper.append(prev);
  352. const start = Math.max(1, currentPage - 1);
  353. const end = Math.min(pageCount, currentPage + 1);
  354. for (let i = start; i <= end; i++) {
  355. const btn = $('<button type="button"></button>').text(i);
  356. if (i === currentPage) btn.addClass('active');
  357. btn.on('click', () => loadData(i, keyword));
  358. wrapper.append(btn);
  359. }
  360. const next = $('<button type="button">下一页</button>').prop('disabled', currentPage >= pageCount);
  361. next.on('click', () => loadData(currentPage + 1, keyword));
  362. wrapper.append(next);
  363. }
  364. $(function () {
  365. loadData();
  366. $('#searchBtn').on('click', function (e) {
  367. e.preventDefault();
  368. const keyword = $('#searchInput').val().trim();
  369. loadData(1, keyword);
  370. });
  371. $('#searchInput').on('keypress', function (e) {
  372. if (e.which === 13) {
  373. const keyword = $(this).val().trim();
  374. loadData(1, keyword);
  375. }
  376. });
  377. $(document).on('click', '#tableBody tr', function () {
  378. addToShipment(this);
  379. });
  380. // 修改sadBtn的点击事件处理
  381. $('#sadBtn').on('click', function (e) {
  382. e.preventDefault();
  383. // 生成一行表格的HTML
  384. const generateRowHtml = (index) => `
  385. <tr data-row="${index}">
  386. <td><input type="text" name="order_ddbh" class="form-control" required /></td>
  387. <td><input type="text" name="gdbh" class="form-control" required /></td>
  388. <td><input type="text" name="cpbm" class="form-control" required /></td>
  389. <td><input type="text" name="cpmc" class="form-control" required /></td>
  390. <td><input type="number" name="sl" class="form-control" min="1" required /></td>
  391. <td>
  392. <button type="button" class="btn btn-success btn-xs add-row"><i class="fa fa-plus"></i></button>
  393. <button type="button" class="btn btn-danger btn-xs remove-row"><i class="fa fa-minus"></i></button>
  394. </td>
  395. </tr>
  396. `;
  397. layer.open({
  398. type: 1,
  399. title: '新增入库记录',
  400. area: ['90%', '70%'],
  401. shadeClose: true,
  402. resize: true,
  403. maxmin: true,
  404. btn: ['加入选品列表', '取消'],
  405. content: `
  406. <div style="padding: 20px 30px;">
  407. <table class="table table-bordered" id="productTable">
  408. <thead>
  409. <tr>
  410. <th>销售订单号</th>
  411. <th>工单编号</th>
  412. <th>成品编码</th>
  413. <th>成品名称</th>
  414. <th>入库数</th>
  415. <th>操作</th>
  416. </tr>
  417. </thead>
  418. <tbody>
  419. ${generateRowHtml(0)}
  420. </tbody>
  421. </table>
  422. </div>
  423. `,
  424. success: function(layero, index) {
  425. // 添加行事件
  426. $(layero).on('click', '.add-row', function() {
  427. const tbody = $('#productTable tbody');
  428. const newRowIndex = tbody.find('tr').length;
  429. tbody.append(generateRowHtml(newRowIndex));
  430. });
  431. // 删除行事件
  432. $(layero).on('click', '.remove-row', function() {
  433. const row = $(this).closest('tr');
  434. if ($('#productTable tbody tr').length > 1) {
  435. row.remove();
  436. } else {
  437. layer.msg('至少保留一行', {icon: 2});
  438. }
  439. });
  440. },
  441. yes: function(index) {
  442. const newProducts = [];
  443. let isValid = true;
  444. $('#productTable tbody tr').each(function() {
  445. const rowData = {};
  446. let rowValid = true;
  447. $(this).find('[required]').each(function() {
  448. const $input = $(this);
  449. const val = $input.val().trim();
  450. if (!val) {
  451. layer.tips('此项为必填项', $input, {tips: [1, '#FF5722']});
  452. isValid = false;
  453. rowValid = false;
  454. return false; // 退出当前each循环
  455. }
  456. rowData[$input.attr('name')] = val;
  457. });
  458. if (rowValid) {
  459. // 设置剩余数量等于入库数量(因为是新增产品)
  460. rowData.remaining_quantity = rowData.sl;
  461. rowData.total_chu_quantity = 0; // 发货数量初始为0
  462. rowData.isNew = true; // 标记为新增产品
  463. newProducts.push(rowData);
  464. }
  465. });
  466. if (!isValid || newProducts.length === 0) {
  467. if (newProducts.length === 0) {
  468. layer.msg('请至少填写一行有效数据', {icon: 2});
  469. }
  470. return;
  471. }
  472. // 将新产品添加到选品列表
  473. newProducts.forEach(product => {
  474. // 检查是否已存在相同工单编号的产品
  475. const exists = selectedProducts.some(item => item.gdbh === product.gdbh);
  476. if (!exists) {
  477. selectedProducts.push({
  478. gdbh: product.gdbh,
  479. order_ddbh: product.order_ddbh,
  480. cpbm: product.cpbm,
  481. cpmc: product.cpmc,
  482. remaining_quantity: product.remaining_quantity,
  483. total_chu_quantity: product.total_chu_quantity,
  484. isNew: true, // 标记为新增产品
  485. remark: ''
  486. });
  487. }
  488. });
  489. layer.msg(`成功添加 ${newProducts.length} 个产品到选品列表`, {icon: 1});
  490. layer.close(index);
  491. renderShipmentTable();
  492. }
  493. });
  494. });
  495. // 修改提交处理逻辑
  496. $('#submit').off('click').on('click', function (e) {
  497. e.preventDefault();
  498. if (selectedProducts.length === 0) {
  499. layer.msg('请至少选择一个产品添加到打托选品列表!', {icon: 2});
  500. return false;
  501. }
  502. // 确保所有输入数据已更新
  503. $('.quantity-input').each(function() {
  504. const index = $(this).data('index');
  505. const value = $(this).val() || $(this).attr('value') || '';
  506. selectedProducts[index].actual_quantity = value;
  507. });
  508. $('.remark-input').each(function() {
  509. const index = $(this).data('index');
  510. const value = $(this).val() || '';
  511. selectedProducts[index].remark = value;
  512. });
  513. // 分离新增产品和已有产品
  514. const newProducts = selectedProducts.filter(p => p.isNew);
  515. const existingProducts = selectedProducts.filter(p => !p.isNew);
  516. const postData = {
  517. company_name: $('#c-company_name').val(),
  518. postcode: $('#c-postcode').val(),
  519. mobile: $('#c-mobile').val(),
  520. company_address: $('#c-company_address').val(),
  521. printer_code: $('#c-printer_code').val(),
  522. products: selectedProducts.map(p => ({
  523. ...p,
  524. remark: p.remark || '',
  525. actual_quantity: p.actual_quantity || p.remaining_quantity
  526. })),
  527. newProducts: newProducts.map(p => ({
  528. order_ddbh: p.order_ddbh,
  529. gdbh: p.gdbh,
  530. cpbm: p.cpbm,
  531. cpmc: p.cpmc,
  532. sl: p.remaining_quantity,
  533. remark: p.remark || '',
  534. actual_quantity: p.actual_quantity || p.remaining_quantity
  535. }))
  536. };
  537. // 显示加载中
  538. const loadingIndex = layer.msg('处理中...', {icon: 16, shade: 0.3, time: 0});
  539. // 先处理新增产品入库
  540. if (newProducts.length > 0) {
  541. $.ajax({
  542. method: "POST",
  543. url: "Finishedproduct/finished",
  544. contentType: "application/json",
  545. data: JSON.stringify({ data: postData.newProducts }),
  546. success: function(res) {
  547. if (res && res.code === 1) {
  548. // 新增产品入库成功后,提交主表单
  549. submitMainForm(postData, loadingIndex);
  550. } else {
  551. layer.close(loadingIndex);
  552. layer.msg(res?.msg || '新增产品入库失败', {icon: 2});
  553. }
  554. },
  555. error: function(xhr) {
  556. layer.close(loadingIndex);
  557. const errorMsg = xhr.responseJSON?.message || xhr.statusText || '请求失败';
  558. layer.msg('新增产品入库失败: ' + errorMsg, {icon: 2});
  559. }
  560. });
  561. } else {
  562. // 没有新增产品,直接提交主表单
  563. submitMainForm(postData, loadingIndex);
  564. }
  565. });
  566. // 提交主表单的函数
  567. function submitMainForm(postData, loadingIndex) {
  568. Fast.api.ajax({
  569. url: 'qcode_add/add_bach',
  570. type: 'POST',
  571. data: {row: JSON.stringify(postData)},
  572. }, function (data, res) {
  573. layer.close(loadingIndex);
  574. if (res.code == 1) {
  575. layer.msg('保存成功', {icon: 1});
  576. window.location.reload();
  577. // 或者跳转到其他页面
  578. // Backend.api.addtabs('deliver/apply');
  579. } else {
  580. layer.msg(res.msg || '保存失败', {icon: 2});
  581. }
  582. }, function () {
  583. layer.close(loadingIndex);
  584. layer.msg('请求失败', {icon: 2});
  585. });
  586. }
  587. });
  588. </script>