ledger_search.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. 'use strict';
  2. /**
  3. * 台账搜索相关(多个页面均使用:台账分解、台账审批、台账修订、部位台账、期-部位台账)
  4. *
  5. * 搜索基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  6. *
  7. * @author Mai
  8. * @date
  9. * @version
  10. */
  11. (function($){
  12. $.posSearch = function (setting) {
  13. if (!setting.selector || !setting.searchSpread) return;
  14. const searchHtml =
  15. ' <div class="ml-2">\n' +
  16. ' <div class="input-group input-group-sm">\n' +
  17. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  18. ' <div class="input-group-append">\n' +
  19. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  20. ' </div>\n' +
  21. ' <div class="input-group-append" >\n' +
  22. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  23. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  24. ' </div>\n' +
  25. ' </div>\n' +
  26. ' </div>\n';
  27. $(setting.selector).html(searchHtml);
  28. const sheet = setting.searchSpread.getActiveSheet();
  29. const searchObj = (function () {
  30. let resultArr = [];
  31. const search = function (keyword) {
  32. if (keyword && keyword !== '') {
  33. resultArr = [];
  34. const sortData = sheet.zh_data;
  35. if (sortData) {
  36. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  37. const sd = sortData[i];
  38. if (sd.name && sd.name.indexOf(keyword) > -1) {
  39. resultArr.push({index: i, data: sd});
  40. }
  41. }
  42. }
  43. } else {
  44. resultArr = [];
  45. }
  46. $('#pos-search-hint').html('结果:' + resultArr.length);
  47. };
  48. const searchAndLocate = function (keyword) {
  49. search(keyword);
  50. if (resultArr.length > 0) {
  51. const sel = sheet.getSelections()[0];
  52. const curRow = sel ? sel.row : 0;
  53. const pos = resultArr[0];
  54. if (pos.index !== curRow) {
  55. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  56. sheet.getParent().focus();
  57. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  58. }
  59. }
  60. };
  61. const locateNext = function () {
  62. if (resultArr.length > 0) {
  63. const sel = sheet.getSelections()[0];
  64. const curRow = sel ? sel.row : 0;
  65. let next = _.find(resultArr, function (d) {
  66. return d.index > curRow;
  67. });
  68. if (!next) next = resultArr[0];
  69. if (next.index !== curRow) {
  70. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  71. sheet.getParent().focus();
  72. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  73. }
  74. }
  75. };
  76. const locatePre = function () {
  77. if (resultArr.length > 0) {
  78. const sel = sheet.getSelections()[0];
  79. const curRow = sel ? sel.row : 0;
  80. let next = _.findLast(resultArr, function (d) {
  81. return d.index < curRow;
  82. });
  83. if (!next) next = resultArr[resultArr.length - 1];
  84. if (next.index !== curRow) {
  85. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  86. sheet.getParent().focus();
  87. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  88. }
  89. }
  90. };
  91. return {search, searchAndLocate, locateNext, locatePre};
  92. })();
  93. // $('#pos-keyword').bind('input propertychange', function () {
  94. // posSearch.search(this.value);
  95. // });
  96. $('#pos-keyword').bind('keydown', function(e){
  97. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  98. });
  99. $('#search-pre-pos').click(function () {
  100. searchObj.locatePre();
  101. });
  102. $('#search-next-pos').click(function () {
  103. searchObj.locateNext();
  104. });
  105. return searchObj;
  106. };
  107. $.billsSearch = function (setting) {
  108. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  109. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  110. const resultId = setting.id + '-search-result';
  111. const obj = $(setting.selector);
  112. obj.html(
  113. ' <div class="sjs-bar">\n' +
  114. ' <div class="input-group input-group-sm pb-1">\n' +
  115. ' <div class="input-group-prepend">\n' +
  116. (setting.searchOver ? ' <div class="input-group-text"><input type="radio" name="searchType" id="over"> 超计</div>\n' : '') +
  117. (setting.searchEmpty ? ' <div class="input-group-text"><input type="radio" name="searchType" id="empty"> 漏计</div>\n' : '') +
  118. ' </div>' +
  119. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  120. ' <div class="input-group-append">\n' +
  121. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  122. ' </div>\n' +
  123. ' </div>\n' +
  124. ' </div>\n' +
  125. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  126. ' </div>'
  127. );
  128. autoFlashHeight();
  129. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  130. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  131. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  132. const searchSheet = setting.searchSpread.getActiveSheet();
  133. let searchResult = [];
  134. const searchBills = function () {
  135. const keyword = $('#searchKeyword', obj).val();
  136. searchResult = [];
  137. const sortData = SpreadJsObj.getSortData(searchSheet);
  138. for (const node of sortData) {
  139. if ((node.code && node.code.indexOf(keyword) > -1) ||
  140. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  141. node.name && node.name.indexOf(keyword) > -1) {
  142. const data = JSON.parse(JSON.stringify(node));
  143. data.visible = true;
  144. searchResult.push(data);
  145. }
  146. }
  147. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  148. };
  149. const calculateCompletePercent = function (searchResult) {
  150. if (!searchResult) return;
  151. for (const sr of searchResult) {
  152. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  153. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  154. }
  155. };
  156. const searchOver = function () {
  157. searchResult = [];
  158. const sortData = SpreadJsObj.getSortData(searchSheet);
  159. for (const node of sortData) {
  160. if (node.children && node.children.length > 0) continue;
  161. if (node.end_gather_qty) {
  162. if (!node.quantity || Math.abs(node.end_gather_qty) > Math.abs(ZhCalc.add(node.quantity, node.end_qc_qty))) {
  163. const data = JSON.parse(JSON.stringify(node));
  164. data.visible = true;
  165. searchResult.push(data);
  166. }
  167. } else if (node.end_gather_tp) {
  168. if (!node.total_price || Math.abs(node.end_gather_tp) > Math.abs(ZhCalc.add(node.total_price, node.end_qc_tp))) {
  169. const data = JSON.parse(JSON.stringify(node));
  170. data.visible = true;
  171. searchResult.push(data);
  172. }
  173. }
  174. }
  175. calculateCompletePercent(searchResult);
  176. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  177. };
  178. const searchEmpty = function () {
  179. searchResult = [];
  180. const sortData = SpreadJsObj.getSortData(searchSheet);
  181. for (const node of sortData) {
  182. if (node.children && node.children.length > 0) continue;
  183. if (node.quantity) {
  184. if (!node.end_gather_qty || checkZero(node.end_gather_qty)) {
  185. const data = JSON.parse(JSON.stringify(node));
  186. data.visible = true;
  187. searchResult.push(data);
  188. }
  189. } else if (node.total_price) {
  190. if (!node.end_gather_tp || checkZero(node.end_gather_tp)) {
  191. const data = JSON.parse(JSON.stringify(node));
  192. data.visible = true;
  193. searchResult.push(data);
  194. }
  195. }
  196. }
  197. calculateCompletePercent(searchResult);
  198. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  199. };
  200. const searchLess = function () {
  201. searchResult = [];
  202. const sortData = SpreadJsObj.getSortData(searchSheet);
  203. for (const node of sortData) {
  204. if (node.children && node.children.length > 0) continue;
  205. if (node.quantity) {
  206. if (ZhCalc.sub(ZhCalc.add(node.quantity, node.end_qc_qty), node.end_gather_qty) > 0) {
  207. const data = JSON.parse(JSON.stringify(node));
  208. data.visible = true;
  209. searchResult.push(data);
  210. }
  211. } else if (node.total_price) {
  212. if (ZhCalc.sub(ZhCalc.add(node.total_price, node.end_qc_tp), node.end_gather_tp) > 0) {
  213. const data = JSON.parse(JSON.stringify(node));
  214. data.visible = true;
  215. searchResult.push(data);
  216. }
  217. }
  218. }
  219. calculateCompletePercent(searchResult);
  220. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  221. }
  222. $('input', this.obj).bind('keydown', function (e) {
  223. if (e.keyCode == 13) searchBills();
  224. });
  225. $('button', this.obj).bind('click', () => {searchBills()});
  226. $('#over', this.obj).bind('change', () => {searchOver()});
  227. $('#empty', this.obj).bind('change', () => {searchLess()});
  228. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  229. const sheet = info.sheet;
  230. const data = sheet.zh_data;
  231. if (!data) { return }
  232. const curBills = data[info.row];
  233. if (!curBills) { return }
  234. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  235. if (setting.afterLocated) {
  236. setting.afterLocated();
  237. }
  238. });
  239. return {spread: resultSpread};
  240. };
  241. })(jQuery);