cs_tools.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. 'use strict';
  2. /**
  3. * cs_errorList:错误列表
  4. * 使用范围:
  5. * 台账分解(原报)、台账修订(原报)、计量台账(所有角色)
  6. *
  7. * posSearch & billsSearch:台账搜索相关
  8. * 使用范围:
  9. * 0号台账:台账分解、台账审批、台账修订、部位台账;
  10. * 期计量:计量台账、部位台账
  11. *
  12. * 所有工具均基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  13. *
  14. * @author Mai
  15. * @date
  16. * @version
  17. */
  18. const showSideTools = function (show) {
  19. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  20. if (show) {
  21. right.show();
  22. autoFlashHeight();
  23. /**
  24. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  25. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  26. * 故需要通过最终的parent.width再计算一次left.width
  27. *
  28. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  29. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  30. *
  31. */
  32. //left.css('width', parent.width() - right.outerWidth());
  33. //left.css('width', parent.width() - right.outerWidth());
  34. const percent = 100 - right.outerWidth() /parent.width() * 100;
  35. left.css('width', percent + '%');
  36. } else {
  37. left.width(parent.width());
  38. right.hide();
  39. }
  40. };
  41. (function($){
  42. /**
  43. * 错误列表
  44. * @param setting
  45. * {
  46. * tabSelector: 'a[content=#error-list]',
  47. * selector: '#error-list',
  48. * relaSpread: ledgerSpread,
  49. * storeKey: 'ledger-error-' + tenderId,
  50. * }
  51. * @returns {{spread: *}}
  52. */
  53. $.cs_errorList = function (setting) {
  54. if (!setting.spreadSetting) {
  55. setting.spreadSetting = {
  56. cols: [
  57. {title: '行号', field: 'serialNo', width: 80, formatter: '@'},
  58. {title: '清单编号', field: 'b_code', width: 150, formatter: '@'},
  59. {title: '清单名称', field: 'name', width: 230, formatter: '@'},
  60. ],
  61. emptyRows: 0,
  62. headRows: 1,
  63. headRowHeight: [32],
  64. defaultRowHeight: 21,
  65. headerFont: '12px 微软雅黑',
  66. font: '12px 微软雅黑',
  67. selectedBackColor: '#fffacd',
  68. readOnly: true,
  69. };
  70. }
  71. const clearErrorData = function () {
  72. if (setting.storeKey) removeLocalCache(setting.storeKey);
  73. };
  74. const autoShowHistory = function (show) {
  75. if (setting.storeKey) {
  76. setLocalCache(setting.storeKey + '-showHis', show.toString());
  77. }
  78. };
  79. if (setting.selector && setting.relaSpread) {
  80. const resultId = setting.id + '-spread';
  81. const obj = $(setting.selector);
  82. obj.html(
  83. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  84. ' </div>'
  85. );
  86. autoFlashHeight();
  87. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  88. const sheet = spread.getActiveSheet();
  89. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  90. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, spread);
  91. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  92. const sheet = info.sheet;
  93. const data = sheet.zh_data;
  94. if (!data) { return }
  95. const curBills = data[info.row];
  96. if (!curBills) { return }
  97. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  98. if (setting.afterLocated) {
  99. setting.afterLocated();
  100. }
  101. });
  102. const loadErrorData = function (data, his = false) {
  103. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  104. if (!sourceTree) return;
  105. for (const d of data) {
  106. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  107. }
  108. data.sort(function (a, b) {
  109. return a.serialNo - b.serialNo;
  110. });
  111. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  112. if (!his && setting.storeKey) {
  113. setLocalCache(setting.storeKey, JSON.stringify(data));
  114. }
  115. $(setting.tabSelector).show();
  116. };
  117. const showErrorList = function () {
  118. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  119. $('a', '#side-menu').removeClass('active');
  120. tab.addClass('active');
  121. $('.tab-content .tab-pane').removeClass('active');
  122. tabPanel.addClass('active');
  123. showSideTools(true);
  124. spread.refresh();
  125. if (setting.afterShow) setting.afterShow();
  126. };
  127. const loadHisErrorData = function () {
  128. if (setting.storeKey) {
  129. const storeStr = getLocalCache(setting.storeKey);
  130. const storeData = storeStr ? JSON.parse(storeStr) : [];
  131. if (storeData.length > 0) {
  132. loadErrorData(storeData, true);
  133. const showHis = getLocalCache(setting.storeKey + '-showHis');
  134. if (showHis === 'true') {
  135. showErrorList();
  136. removeLocalCache(setting.storeKey + '-showHis');
  137. }
  138. }
  139. }
  140. };
  141. return {
  142. spread: spread,
  143. loadErrorData: loadErrorData,
  144. clearErrorData: clearErrorData,
  145. loadHisErrorData: loadHisErrorData,
  146. show: showErrorList,
  147. autoShowHistory: autoShowHistory,
  148. };
  149. } else {
  150. const loadErrorData = function (data) {
  151. if (setting.storeKey) {
  152. setLocalCache(setting.storeKey, JSON.stringify(data));
  153. }
  154. };
  155. return {
  156. loadErrorData: loadErrorData,
  157. clearErrorData: clearErrorData,
  158. autoShowHistory: autoShowHistory,
  159. };
  160. }
  161. };
  162. $.posSearch = function (setting) {
  163. if (!setting.selector || !setting.searchSpread) return;
  164. const searchHtml =
  165. ' <div class="ml-2">\n' +
  166. ' <div class="input-group input-group-sm">\n' +
  167. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  168. ' <div class="input-group-append">\n' +
  169. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  170. ' </div>\n' +
  171. ' <div class="input-group-append" >\n' +
  172. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  173. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  174. ' </div>\n' +
  175. ' </div>\n' +
  176. ' </div>\n';
  177. $(setting.selector).html(searchHtml);
  178. const sheet = setting.searchSpread.getActiveSheet();
  179. const searchObj = (function () {
  180. let resultArr = [];
  181. const search = function (keyword) {
  182. if (keyword && keyword !== '') {
  183. resultArr = [];
  184. const sortData = sheet.zh_data;
  185. if (sortData) {
  186. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  187. const sd = sortData[i];
  188. if (sd.name && sd.name.indexOf(keyword) > -1) {
  189. resultArr.push({index: i, data: sd});
  190. }
  191. }
  192. }
  193. } else {
  194. resultArr = [];
  195. }
  196. $('#pos-search-hint').html('结果:' + resultArr.length);
  197. };
  198. const searchAndLocate = function (keyword) {
  199. search(keyword);
  200. if (resultArr.length > 0) {
  201. const sel = sheet.getSelections()[0];
  202. const curRow = sel ? sel.row : 0;
  203. const pos = resultArr[0];
  204. if (pos.index !== curRow) {
  205. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  206. sheet.getParent().focus();
  207. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  208. SpreadJsObj.reloadRowsBackColor(sheet, [pos.index, curRow]);
  209. }
  210. }
  211. };
  212. const locateNext = function () {
  213. if (resultArr.length > 0) {
  214. const sel = sheet.getSelections()[0];
  215. const curRow = sel ? sel.row : 0;
  216. let next = _.find(resultArr, function (d) {
  217. return d.index > curRow;
  218. });
  219. if (!next) next = resultArr[0];
  220. if (next.index !== curRow) {
  221. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  222. sheet.getParent().focus();
  223. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  224. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  225. }
  226. }
  227. };
  228. const locatePre = function () {
  229. if (resultArr.length > 0) {
  230. const sel = sheet.getSelections()[0];
  231. const curRow = sel ? sel.row : 0;
  232. let next = _.findLast(resultArr, function (d) {
  233. return d.index < curRow;
  234. });
  235. if (!next) next = resultArr[resultArr.length - 1];
  236. if (next.index !== curRow) {
  237. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  238. sheet.getParent().focus();
  239. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  240. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  241. }
  242. }
  243. };
  244. return {search, searchAndLocate, locateNext, locatePre};
  245. })();
  246. // $('#pos-keyword').bind('input propertychange', function () {
  247. // posSearch.search(this.value);
  248. // });
  249. $('#pos-keyword').bind('keydown', function(e){
  250. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  251. });
  252. $('#search-pre-pos').click(function () {
  253. searchObj.locatePre();
  254. });
  255. $('#search-next-pos').click(function () {
  256. searchObj.locateNext();
  257. });
  258. return searchObj;
  259. };
  260. $.billsSearch = function (setting) {
  261. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  262. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  263. const resultId = setting.id + '-search-result';
  264. const obj = $(setting.selector);
  265. let filter = [];
  266. if (setting.searchOver || setting.searchEmpty) {
  267. filter.push('<select class="form-control form-control-sm" id="search-filter">');
  268. filter.push('<option value="">台账</option>');
  269. if (setting.customSearch) {
  270. for (const cs of setting.customSearch) {
  271. if (cs.valid) filter.push('<option value="' + cs.key + '">' + cs.title + '</option>');
  272. }
  273. }
  274. filter.push('</select>');
  275. // filter.push('<div class="input-group-prepend">');
  276. // filter.push('<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">台帐</button>');
  277. // filter.push('<div class="dropdown-menu">');
  278. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-bills">台账</a>');
  279. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-over">超计</a>');
  280. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-less">漏计</a>');
  281. // filter.push('</div>');
  282. // filter.push('</div>');
  283. }
  284. obj.html(
  285. ' <div class="sjs-bar">\n' +
  286. ' <div class="input-group input-group-sm pb-1">\n' +
  287. ' <div class="input-group-prepend">\n' +
  288. filter.join('') +
  289. ' </div>' +
  290. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  291. ' <div class="input-group-append">\n' +
  292. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  293. ' </div>\n' +
  294. ' </div>\n' +
  295. ' </div>\n' +
  296. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  297. ' </div>'
  298. );
  299. autoFlashHeight();
  300. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  301. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  302. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  303. const searchSheet = setting.searchSpread.getActiveSheet();
  304. let searchResult = [];
  305. const search = function () {
  306. const filter = $('#search-filter').val();
  307. if (filter) {
  308. searchCustom(filter);
  309. } else {
  310. searchBills();
  311. }
  312. };
  313. const searchBills = function () {
  314. const keyword = $('#searchKeyword', obj).val();
  315. searchResult = [];
  316. const sortData = SpreadJsObj.getSortData(searchSheet);
  317. for (const node of sortData) {
  318. if ((node.code && node.code.indexOf(keyword) > -1) ||
  319. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  320. node.name && node.name.indexOf(keyword) > -1) {
  321. const data = JSON.parse(JSON.stringify(node));
  322. data.visible = true;
  323. searchResult.push(data);
  324. }
  325. }
  326. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  327. };
  328. const getCheckFun = function (key) {
  329. const cs = setting.customSearch.find(function (x) {return x.key === key});
  330. return cs ? cs.check : null;
  331. };
  332. const searchCustom = function (key) {
  333. const keyword = $('#searchKeyword', obj).val();
  334. const checkFun = getCheckFun(key);
  335. searchResult = [];
  336. const sortData = SpreadJsObj.getSortData(searchSheet);
  337. for (const node of sortData) {
  338. if (node.children && node.children.length > 0) continue;
  339. if (checkFun && checkFun(node)) {
  340. if (!keyword ||
  341. (node.code && node.code.indexOf(keyword) > -1) ||
  342. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  343. (node.name && node.name.indexOf(keyword) > -1)) {
  344. const data = JSON.parse(JSON.stringify(node));
  345. data.visible = true;
  346. searchResult.push(data);
  347. }
  348. }
  349. }
  350. calculateCompletePercent(searchResult);
  351. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  352. };
  353. const calculateCompletePercent = function (searchResult) {
  354. if (!searchResult) return;
  355. for (const sr of searchResult) {
  356. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  357. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  358. }
  359. };
  360. $('input', obj).bind('keydown', function (e) {
  361. if (e.keyCode == 13) search();
  362. });
  363. $('button', obj).bind('click', () => {search()});
  364. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  365. const sheet = info.sheet;
  366. const data = sheet.zh_data;
  367. if (!data) { return }
  368. const curBills = data[info.row];
  369. if (!curBills) { return }
  370. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  371. if (setting.afterLocated) {
  372. setting.afterLocated();
  373. }
  374. });
  375. return {spread: resultSpread};
  376. };
  377. })(jQuery);