cs_tools.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. }
  209. }
  210. };
  211. const locateNext = function () {
  212. if (resultArr.length > 0) {
  213. const sel = sheet.getSelections()[0];
  214. const curRow = sel ? sel.row : 0;
  215. let next = _.find(resultArr, function (d) {
  216. return d.index > curRow;
  217. });
  218. if (!next) next = resultArr[0];
  219. if (next.index !== curRow) {
  220. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  221. sheet.getParent().focus();
  222. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  223. }
  224. }
  225. };
  226. const locatePre = function () {
  227. if (resultArr.length > 0) {
  228. const sel = sheet.getSelections()[0];
  229. const curRow = sel ? sel.row : 0;
  230. let next = _.findLast(resultArr, function (d) {
  231. return d.index < curRow;
  232. });
  233. if (!next) next = resultArr[resultArr.length - 1];
  234. if (next.index !== curRow) {
  235. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  236. sheet.getParent().focus();
  237. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  238. }
  239. }
  240. };
  241. return {search, searchAndLocate, locateNext, locatePre};
  242. })();
  243. // $('#pos-keyword').bind('input propertychange', function () {
  244. // posSearch.search(this.value);
  245. // });
  246. $('#pos-keyword').bind('keydown', function(e){
  247. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  248. });
  249. $('#search-pre-pos').click(function () {
  250. searchObj.locatePre();
  251. });
  252. $('#search-next-pos').click(function () {
  253. searchObj.locateNext();
  254. });
  255. return searchObj;
  256. };
  257. $.billsSearch = function (setting) {
  258. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  259. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  260. const resultId = setting.id + '-search-result';
  261. const obj = $(setting.selector);
  262. let filter = [];
  263. if (setting.searchOver || setting.searchEmpty) {
  264. filter.push('<select class="form-control form-control-sm" id="search-filter">');
  265. filter.push('<option value="">台账</option>');
  266. if (setting.searchOver) filter.push('<option value="over">超计</option>');
  267. if (setting.searchEmpty) filter.push('<option value="less">漏计</option>');
  268. filter.push('</select>');
  269. // filter.push('<div class="input-group-prepend">');
  270. // filter.push('<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">台帐</button>');
  271. // filter.push('<div class="dropdown-menu">');
  272. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-bills">台账</a>');
  273. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-over">超计</a>');
  274. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-less">漏计</a>');
  275. // filter.push('</div>');
  276. // filter.push('</div>');
  277. }
  278. obj.html(
  279. ' <div class="sjs-bar">\n' +
  280. ' <div class="input-group input-group-sm pb-1">\n' +
  281. ' <div class="input-group-prepend">\n' +
  282. filter.join('') +
  283. ' </div>' +
  284. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  285. ' <div class="input-group-append">\n' +
  286. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  287. ' </div>\n' +
  288. ' </div>\n' +
  289. ' </div>\n' +
  290. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  291. ' </div>'
  292. );
  293. autoFlashHeight();
  294. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  295. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  296. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  297. const searchSheet = setting.searchSpread.getActiveSheet();
  298. let searchResult = [];
  299. const search = function () {
  300. const filter = $('#search-filter').val();
  301. if (filter === 'over') {
  302. searchOver();
  303. } else if (filter === 'less') {
  304. searchLess();
  305. } else {
  306. searchBills();
  307. }
  308. };
  309. const searchBills = function () {
  310. const keyword = $('#searchKeyword', obj).val();
  311. searchResult = [];
  312. const sortData = SpreadJsObj.getSortData(searchSheet);
  313. for (const node of sortData) {
  314. if ((node.code && node.code.indexOf(keyword) > -1) ||
  315. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  316. node.name && node.name.indexOf(keyword) > -1) {
  317. const data = JSON.parse(JSON.stringify(node));
  318. data.visible = true;
  319. searchResult.push(data);
  320. }
  321. }
  322. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  323. };
  324. const calculateCompletePercent = function (searchResult) {
  325. if (!searchResult) return;
  326. for (const sr of searchResult) {
  327. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  328. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  329. }
  330. };
  331. const searchOver = function () {
  332. const keyword = $('#searchKeyword', obj).val();
  333. searchResult = [];
  334. const sortData = SpreadJsObj.getSortData(searchSheet);
  335. for (const node of sortData) {
  336. if (node.children && node.children.length > 0) continue;
  337. if (setting.checkOver && setting.checkOver(node)) {
  338. if (!keyword ||
  339. (node.code && node.code.indexOf(keyword) > -1) ||
  340. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  341. (node.name && node.name.indexOf(keyword) > -1)) {
  342. const data = JSON.parse(JSON.stringify(node));
  343. data.visible = true;
  344. searchResult.push(data);
  345. }
  346. }
  347. }
  348. calculateCompletePercent(searchResult);
  349. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  350. };
  351. const searchEmpty = function () {
  352. const keyword = $('#searchKeyword', obj).val();
  353. searchResult = [];
  354. const sortData = SpreadJsObj.getSortData(searchSheet);
  355. for (const node of sortData) {
  356. if (node.children && node.children.length > 0) continue;
  357. if (setting.checkEmpty && setting.checkEmpty(node)) {
  358. if (!keyword ||
  359. (node.code && node.code.indexOf(keyword) > -1) ||
  360. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  361. (node.name && node.name.indexOf(keyword) > -1)) {
  362. const data = JSON.parse(JSON.stringify(node));
  363. data.visible = true;
  364. searchResult.push(data);
  365. }
  366. }
  367. }
  368. calculateCompletePercent(searchResult);
  369. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  370. };
  371. const searchLess = function () {
  372. const keyword = $('#searchKeyword', obj).val();
  373. searchResult = [];
  374. const sortData = SpreadJsObj.getSortData(searchSheet);
  375. for (const node of sortData) {
  376. if (node.children && node.children.length > 0) continue;
  377. if (setting.checkLess && setting.checkLess(node)) {
  378. if (!keyword ||
  379. (node.code && node.code.indexOf(keyword) > -1) ||
  380. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  381. (node.name && node.name.indexOf(keyword) > -1)) {
  382. const data = JSON.parse(JSON.stringify(node));
  383. data.visible = true;
  384. searchResult.push(data);
  385. }
  386. }
  387. }
  388. calculateCompletePercent(searchResult);
  389. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  390. };
  391. $('input', obj).bind('keydown', function (e) {
  392. if (e.keyCode == 13) search();
  393. });
  394. $('button', obj).bind('click', () => {search()});
  395. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  396. const sheet = info.sheet;
  397. const data = sheet.zh_data;
  398. if (!data) { return }
  399. const curBills = data[info.row];
  400. if (!curBills) { return }
  401. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  402. if (setting.afterLocated) {
  403. setting.afterLocated();
  404. }
  405. });
  406. return {spread: resultSpread};
  407. };
  408. })(jQuery);