cs_tools.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. console.log(curBills);
  99. if (setting.afterLocated) {
  100. setting.afterLocated();
  101. }
  102. });
  103. const loadErrorData = function (data, his = false) {
  104. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  105. if (!sourceTree) return;
  106. for (const d of data) {
  107. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  108. }
  109. data.sort(function (a, b) {
  110. return a.serialNo - b.serialNo;
  111. });
  112. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  113. if (!his && setting.storeKey) {
  114. setLocalCache(setting.storeKey, JSON.stringify(data));
  115. }
  116. $(setting.tabSelector).show();
  117. };
  118. const showErrorList = function () {
  119. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  120. $('a', '#side-menu').removeClass('active');
  121. tab.addClass('active');
  122. $('.tab-content .tab-pane').removeClass('active');
  123. tabPanel.addClass('active');
  124. showSideTools(true);
  125. spread.refresh();
  126. if (setting.afterShow) setting.afterShow();
  127. };
  128. const loadHisErrorData = function () {
  129. if (setting.storeKey) {
  130. const storeStr = getLocalCache(setting.storeKey);
  131. const storeData = storeStr ? JSON.parse(storeStr) : [];
  132. if (storeData.length > 0) {
  133. loadErrorData(storeData, true);
  134. const showHis = getLocalCache(setting.storeKey + '-showHis');
  135. if (showHis === 'true') {
  136. showErrorList();
  137. removeLocalCache(setting.storeKey + '-showHis');
  138. }
  139. }
  140. }
  141. };
  142. return {
  143. spread: spread,
  144. loadErrorData: loadErrorData,
  145. clearErrorData: clearErrorData,
  146. loadHisErrorData: loadHisErrorData,
  147. show: showErrorList,
  148. autoShowHistory: autoShowHistory,
  149. };
  150. } else {
  151. const loadErrorData = function (data) {
  152. if (setting.storeKey) {
  153. setLocalCache(setting.storeKey, JSON.stringify(data));
  154. }
  155. };
  156. return {
  157. loadErrorData: loadErrorData,
  158. clearErrorData: clearErrorData,
  159. autoShowHistory: autoShowHistory,
  160. };
  161. }
  162. };
  163. $.posSearch = function (setting) {
  164. if (!setting.selector || !setting.searchSpread) return;
  165. const searchHtml =
  166. ' <div class="ml-2">\n' +
  167. ' <div class="input-group input-group-sm">\n' +
  168. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  169. ' <div class="input-group-append">\n' +
  170. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  171. ' </div>\n' +
  172. ' <div class="input-group-append" >\n' +
  173. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  174. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  175. ' </div>\n' +
  176. ' </div>\n' +
  177. ' </div>\n';
  178. $(setting.selector).html(searchHtml);
  179. const sheet = setting.searchSpread.getActiveSheet();
  180. const searchObj = (function () {
  181. let resultArr = [];
  182. const search = function (keyword) {
  183. if (keyword && keyword !== '') {
  184. resultArr = [];
  185. const sortData = sheet.zh_data;
  186. if (sortData) {
  187. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  188. const sd = sortData[i];
  189. if (sd.name && sd.name.indexOf(keyword) > -1) {
  190. resultArr.push({index: i, data: sd});
  191. }
  192. }
  193. }
  194. } else {
  195. resultArr = [];
  196. }
  197. $('#pos-search-hint').html('结果:' + resultArr.length);
  198. };
  199. const searchAndLocate = function (keyword) {
  200. search(keyword);
  201. if (resultArr.length > 0) {
  202. const sel = sheet.getSelections()[0];
  203. const curRow = sel ? sel.row : 0;
  204. const pos = resultArr[0];
  205. if (pos.index !== curRow) {
  206. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  207. sheet.getParent().focus();
  208. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  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. }
  225. }
  226. };
  227. const locatePre = function () {
  228. if (resultArr.length > 0) {
  229. const sel = sheet.getSelections()[0];
  230. const curRow = sel ? sel.row : 0;
  231. let next = _.findLast(resultArr, function (d) {
  232. return d.index < curRow;
  233. });
  234. if (!next) next = resultArr[resultArr.length - 1];
  235. if (next.index !== curRow) {
  236. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  237. sheet.getParent().focus();
  238. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  239. }
  240. }
  241. };
  242. return {search, searchAndLocate, locateNext, locatePre};
  243. })();
  244. // $('#pos-keyword').bind('input propertychange', function () {
  245. // posSearch.search(this.value);
  246. // });
  247. $('#pos-keyword').bind('keydown', function(e){
  248. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  249. });
  250. $('#search-pre-pos').click(function () {
  251. searchObj.locatePre();
  252. });
  253. $('#search-next-pos').click(function () {
  254. searchObj.locateNext();
  255. });
  256. return searchObj;
  257. };
  258. $.billsSearch = function (setting) {
  259. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  260. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  261. const resultId = setting.id + '-search-result';
  262. const obj = $(setting.selector);
  263. let filter = [];
  264. if (setting.searchOver || setting.searchEmpty) {
  265. filter.push('<select class="form-control form-control-sm" id="search-filter">');
  266. filter.push('<option value="">台账</option>');
  267. if (setting.searchOver) filter.push('<option value="over">超计</option>');
  268. if (setting.searchEmpty) filter.push('<option value="less">漏计</option>');
  269. filter.push('</select>');
  270. // filter.push('<div class="input-group-prepend">');
  271. // filter.push('<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">台帐</button>');
  272. // filter.push('<div class="dropdown-menu">');
  273. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-bills">台账</a>');
  274. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-over">超计</a>');
  275. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-less">漏计</a>');
  276. // filter.push('</div>');
  277. // filter.push('</div>');
  278. }
  279. obj.html(
  280. ' <div class="sjs-bar">\n' +
  281. ' <div class="input-group input-group-sm pb-1">\n' +
  282. ' <div class="input-group-prepend">\n' +
  283. filter.join('') +
  284. ' </div>' +
  285. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  286. ' <div class="input-group-append">\n' +
  287. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  288. ' </div>\n' +
  289. ' </div>\n' +
  290. ' </div>\n' +
  291. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  292. ' </div>'
  293. );
  294. autoFlashHeight();
  295. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  296. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  297. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  298. const searchSheet = setting.searchSpread.getActiveSheet();
  299. let searchResult = [];
  300. const search = function () {
  301. const filter = $('#search-filter').val();
  302. if (filter === 'over') {
  303. searchOver();
  304. } else if (filter === 'less') {
  305. searchLess();
  306. } else {
  307. searchBills();
  308. }
  309. };
  310. const searchBills = function () {
  311. const keyword = $('#searchKeyword', obj).val();
  312. searchResult = [];
  313. const sortData = SpreadJsObj.getSortData(searchSheet);
  314. for (const node of sortData) {
  315. if ((node.code && node.code.indexOf(keyword) > -1) ||
  316. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  317. node.name && node.name.indexOf(keyword) > -1) {
  318. const data = JSON.parse(JSON.stringify(node));
  319. data.visible = true;
  320. searchResult.push(data);
  321. }
  322. }
  323. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  324. };
  325. const calculateCompletePercent = function (searchResult) {
  326. if (!searchResult) return;
  327. for (const sr of searchResult) {
  328. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  329. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  330. }
  331. };
  332. const searchOver = function () {
  333. const keyword = $('#searchKeyword', obj).val();
  334. searchResult = [];
  335. const sortData = SpreadJsObj.getSortData(searchSheet);
  336. for (const node of sortData) {
  337. if (node.children && node.children.length > 0) continue;
  338. if (setting.checkOver && setting.checkOver(node)) {
  339. if (!keyword ||
  340. (node.code && node.code.indexOf(keyword) > -1) ||
  341. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  342. (node.name && node.name.indexOf(keyword) > -1)) {
  343. const data = JSON.parse(JSON.stringify(node));
  344. data.visible = true;
  345. searchResult.push(data);
  346. }
  347. }
  348. }
  349. calculateCompletePercent(searchResult);
  350. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  351. };
  352. const searchEmpty = function () {
  353. const keyword = $('#searchKeyword', obj).val();
  354. searchResult = [];
  355. const sortData = SpreadJsObj.getSortData(searchSheet);
  356. for (const node of sortData) {
  357. if (node.children && node.children.length > 0) continue;
  358. if (setting.checkEmpty && setting.checkEmpty(node)) {
  359. if (!keyword ||
  360. (node.code && node.code.indexOf(keyword) > -1) ||
  361. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  362. (node.name && node.name.indexOf(keyword) > -1)) {
  363. const data = JSON.parse(JSON.stringify(node));
  364. data.visible = true;
  365. searchResult.push(data);
  366. }
  367. }
  368. }
  369. calculateCompletePercent(searchResult);
  370. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  371. };
  372. const searchLess = function () {
  373. const keyword = $('#searchKeyword', obj).val();
  374. searchResult = [];
  375. const sortData = SpreadJsObj.getSortData(searchSheet);
  376. for (const node of sortData) {
  377. if (node.children && node.children.length > 0) continue;
  378. if (setting.checkLess && setting.checkLess(node)) {
  379. if (!keyword ||
  380. (node.code && node.code.indexOf(keyword) > -1) ||
  381. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  382. (node.name && node.name.indexOf(keyword) > -1)) {
  383. const data = JSON.parse(JSON.stringify(node));
  384. data.visible = true;
  385. searchResult.push(data);
  386. }
  387. }
  388. }
  389. calculateCompletePercent(searchResult);
  390. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  391. };
  392. $('input', obj).bind('keydown', function (e) {
  393. if (e.keyCode == 13) search();
  394. });
  395. $('button', obj).bind('click', () => {search()});
  396. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  397. const sheet = info.sheet;
  398. const data = sheet.zh_data;
  399. if (!data) { return }
  400. const curBills = data[info.row];
  401. if (!curBills) { return }
  402. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  403. if (setting.afterLocated) {
  404. setting.afterLocated();
  405. }
  406. });
  407. return {spread: resultSpread};
  408. };
  409. })(jQuery);