cs_tools.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. $.ledger_checkList = function (setting) {
  163. const checkTypeText = [];
  164. for (const ct in setting.checkType) {
  165. checkTypeText[setting.checkType[ct].value] = setting.checkType[ct].text;
  166. }
  167. if (!setting.spreadSetting) {
  168. setting.spreadSetting = {
  169. cols: [
  170. {
  171. title: '类型', field: 'type', width: 150, formatter: '@',
  172. getValue: function (data){
  173. if (setting.checkType) {
  174. return checkTypeText[data.type] || '';
  175. } else {
  176. return '';
  177. }
  178. }
  179. },
  180. {title: '行号', field: 'serialNo', hAlign: 1, width: 40, formatter: '@'},
  181. {title: '项目节编号', field: 'code', width: 80, formatter: '@'},
  182. {title: '清单编号', field: 'b_code', width: 80, formatter: '@'},
  183. {title: '清单名称', field: 'name', width: 150, formatter: '@'},
  184. ],
  185. emptyRows: 0,
  186. headRows: 1,
  187. headRowHeight: [32],
  188. defaultRowHeight: 21,
  189. headerFont: '12px 微软雅黑',
  190. font: '12px 微软雅黑',
  191. selectedBackColor: '#fffacd',
  192. readOnly: true,
  193. };
  194. }
  195. const clearCheckData = function () {
  196. if (setting.storeKey) removeLocalCache(setting.storeKey);
  197. };
  198. const autoShowHistory = function (show) {
  199. if (setting.storeKey) {
  200. setLocalCache(setting.storeKey + '-showHis', show.toString());
  201. }
  202. };
  203. if (setting.selector && setting.relaSpread) {
  204. const resultId = setting.id + '-spread';
  205. const obj = $(setting.selector);
  206. const dropdown = [];
  207. if (setting.checkType) {
  208. dropdown.push('<div class="dropdown">');
  209. dropdown.push('<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" id="'+ setting.id + 'drop" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">所有类型</button>');
  210. dropdown.push('<div class="dropdown-menu" aria-labelledby="'+ setting.id + 'drop">');
  211. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="all">所有类型</a>');
  212. for (const ct in setting.checkType) {
  213. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="' + setting.checkType[ct].value +'">' + setting.checkType[ct].text + '</a>');
  214. }
  215. dropdown.push('</div>');
  216. dropdown.push('</div>');
  217. }
  218. obj.html(
  219. '<div class="sjs-bar">\n' +
  220. ' <div class="pb-1 d-flex">\n' + dropdown.join('') +
  221. ' <span class="ml-auto pr-2" id="' + setting.id + '-time">检查时间:2020-08-01 13:20:25</span>\n' +
  222. ' </div>\n' +
  223. '</div>' +
  224. '<div id="' + resultId + '" class="sjs-sh">\n' +
  225. '</div>'
  226. );
  227. autoFlashHeight();
  228. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  229. const sheet = spread.getActiveSheet();
  230. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  231. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, spread);
  232. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  233. const sheet = info.sheet;
  234. const data = sheet.zh_data;
  235. if (!data) { return }
  236. const curBills = data[info.row];
  237. if (!curBills) { return }
  238. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  239. if (setting.afterLocated) {
  240. setting.afterLocated();
  241. }
  242. });
  243. const hideCheckData = function () {
  244. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  245. if (tab.hasClass('active')) {
  246. $('a', '#side-menu').removeClass('active');
  247. tab.addClass('active');
  248. $('.tab-content .tab-pane').removeClass('active');
  249. tabPanel.addClass('active');
  250. showSideTools(false);
  251. if (spread) spread.refresh();
  252. if (setting.afterShow) setting.afterShow();
  253. tab.hide();
  254. }
  255. };
  256. const loadCheckData = function (data, his = false) {
  257. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  258. if (!sourceTree) return;
  259. for (const d of data.warning_data) {
  260. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  261. }
  262. $('#' + setting.id + '-time').html('检查时间:' + data.check_time.toLocaleString());
  263. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data.warning_data);
  264. if (!his && setting.storeKey) {
  265. setLocalCache(setting.storeKey, JSON.stringify(data));
  266. }
  267. $(setting.tabSelector).show();
  268. };
  269. const showCheckList = function () {
  270. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  271. $('a', '#side-menu').removeClass('active');
  272. tab.addClass('active');
  273. $('.tab-content .tab-pane').removeClass('active');
  274. tabPanel.addClass('active');
  275. showSideTools(true);
  276. spread.refresh();
  277. if (setting.afterShow) setting.afterShow();
  278. };
  279. const loadHisCheckData = function () {
  280. if (setting.storeKey) {
  281. const storeStr = getLocalCache(setting.storeKey);
  282. const storeData = storeStr ? JSON.parse(storeStr) : null;
  283. if (storeData) {
  284. loadCheckData(storeData, true);
  285. const showHis = getLocalCache(setting.storeKey + '-showHis');
  286. if (showHis === 'true') {
  287. showCheckList();
  288. removeLocalCache(setting.storeKey + '-showHis');
  289. }
  290. }
  291. }
  292. };
  293. return {
  294. spread: spread,
  295. loadCheckData: loadCheckData,
  296. clearCheckData: clearCheckData,
  297. loadHisCheckData: loadHisCheckData,
  298. show: showCheckList,
  299. hide: hideCheckData,
  300. autoShowHistory: autoShowHistory,
  301. };
  302. } else {
  303. const loadCheckData = function (data) {
  304. if (setting.storeKey) {
  305. setLocalCache(setting.storeKey, JSON.stringify(data));
  306. }
  307. };
  308. return {
  309. loadCheckData: loadCheckData,
  310. clearCheckData: clearCheckData,
  311. autoShowHistory: autoShowHistory,
  312. };
  313. }
  314. };
  315. $.posSearch = function (setting) {
  316. if (!setting.selector || !setting.searchSpread) return;
  317. const searchHtml =
  318. ' <div class="ml-2">\n' +
  319. ' <div class="input-group input-group-sm">\n' +
  320. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  321. ' <div class="input-group-append">\n' +
  322. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  323. ' </div>\n' +
  324. ' <div class="input-group-append" >\n' +
  325. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  326. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  327. ' </div>\n' +
  328. ' </div>\n' +
  329. ' </div>\n';
  330. $(setting.selector).html(searchHtml);
  331. const sheet = setting.searchSpread.getActiveSheet();
  332. const searchObj = (function () {
  333. let resultArr = [];
  334. const search = function (keyword) {
  335. if (keyword && keyword !== '') {
  336. resultArr = [];
  337. const sortData = sheet.zh_data;
  338. if (sortData) {
  339. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  340. const sd = sortData[i];
  341. if (sd.name && sd.name.indexOf(keyword) > -1) {
  342. resultArr.push({index: i, data: sd});
  343. }
  344. }
  345. }
  346. } else {
  347. resultArr = [];
  348. }
  349. $('#pos-search-hint').html('结果:' + resultArr.length);
  350. };
  351. const searchAndLocate = function (keyword) {
  352. search(keyword);
  353. if (resultArr.length > 0) {
  354. const sel = sheet.getSelections()[0];
  355. const curRow = sel ? sel.row : 0;
  356. const pos = resultArr[0];
  357. if (pos.index !== curRow) {
  358. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  359. sheet.getParent().focus();
  360. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  361. SpreadJsObj.reloadRowsBackColor(sheet, [pos.index, curRow]);
  362. }
  363. }
  364. };
  365. const locateNext = function () {
  366. if (resultArr.length > 0) {
  367. const sel = sheet.getSelections()[0];
  368. const curRow = sel ? sel.row : 0;
  369. let next = _.find(resultArr, function (d) {
  370. return d.index > curRow;
  371. });
  372. if (!next) next = resultArr[0];
  373. if (next.index !== curRow) {
  374. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  375. sheet.getParent().focus();
  376. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  377. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  378. }
  379. }
  380. };
  381. const locatePre = function () {
  382. if (resultArr.length > 0) {
  383. const sel = sheet.getSelections()[0];
  384. const curRow = sel ? sel.row : 0;
  385. let next = _.findLast(resultArr, function (d) {
  386. return d.index < curRow;
  387. });
  388. if (!next) next = resultArr[resultArr.length - 1];
  389. if (next.index !== curRow) {
  390. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  391. sheet.getParent().focus();
  392. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  393. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  394. }
  395. }
  396. };
  397. return {search, searchAndLocate, locateNext, locatePre};
  398. })();
  399. // $('#pos-keyword').bind('input propertychange', function () {
  400. // posSearch.search(this.value);
  401. // });
  402. $('#pos-keyword').bind('keydown', function(e){
  403. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  404. });
  405. $('#search-pre-pos').click(function () {
  406. searchObj.locatePre();
  407. });
  408. $('#search-next-pos').click(function () {
  409. searchObj.locateNext();
  410. });
  411. return searchObj;
  412. };
  413. $.billsSearch = function (setting) {
  414. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  415. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  416. const resultId = setting.id + '-search-result';
  417. const obj = $(setting.selector);
  418. let filter = [];
  419. if (setting.searchOver || setting.searchEmpty) {
  420. filter.push('<select class="form-control form-control-sm" id="search-filter">');
  421. filter.push('<option value="">台账</option>');
  422. if (setting.customSearch) {
  423. for (const cs of setting.customSearch) {
  424. if (cs.valid) filter.push('<option value="' + cs.key + '">' + cs.title + '</option>');
  425. }
  426. }
  427. filter.push('</select>');
  428. // filter.push('<div class="input-group-prepend">');
  429. // filter.push('<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">台帐</button>');
  430. // filter.push('<div class="dropdown-menu">');
  431. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-bills">台账</a>');
  432. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-over">超计</a>');
  433. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-less">漏计</a>');
  434. // filter.push('</div>');
  435. // filter.push('</div>');
  436. }
  437. obj.html(
  438. ' <div class="sjs-bar">\n' +
  439. ' <div class="input-group input-group-sm pb-1">\n' +
  440. ' <div class="input-group-prepend">\n' +
  441. filter.join('') +
  442. ' </div>' +
  443. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  444. ' <div class="input-group-append">\n' +
  445. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  446. ' </div>\n' +
  447. ' </div>\n' +
  448. ' </div>\n' +
  449. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  450. ' </div>'
  451. );
  452. autoFlashHeight();
  453. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  454. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  455. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  456. const searchSheet = setting.searchSpread.getActiveSheet();
  457. let searchResult = [];
  458. const search = function () {
  459. const filter = $('#search-filter').val();
  460. if (filter) {
  461. searchCustom(filter);
  462. } else {
  463. searchBills();
  464. }
  465. };
  466. const searchBills = function () {
  467. const keyword = $('#searchKeyword', obj).val();
  468. searchResult = [];
  469. const sortData = SpreadJsObj.getSortData(searchSheet);
  470. for (const node of sortData) {
  471. if ((node.code && node.code.indexOf(keyword) > -1) ||
  472. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  473. node.name && node.name.indexOf(keyword) > -1) {
  474. const data = JSON.parse(JSON.stringify(node));
  475. data.visible = true;
  476. searchResult.push(data);
  477. }
  478. }
  479. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  480. };
  481. const getCheckFun = function (key) {
  482. const cs = setting.customSearch.find(function (x) {return x.key === key});
  483. return cs ? cs.check : null;
  484. };
  485. const searchCustom = function (key) {
  486. const keyword = $('#searchKeyword', obj).val();
  487. const checkFun = getCheckFun(key);
  488. searchResult = [];
  489. const sortData = SpreadJsObj.getSortData(searchSheet);
  490. for (const node of sortData) {
  491. if (node.children && node.children.length > 0) continue;
  492. if (checkFun && checkFun(node)) {
  493. if (!keyword ||
  494. (node.code && node.code.indexOf(keyword) > -1) ||
  495. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  496. (node.name && node.name.indexOf(keyword) > -1)) {
  497. const data = JSON.parse(JSON.stringify(node));
  498. data.visible = true;
  499. searchResult.push(data);
  500. }
  501. }
  502. }
  503. calculateCompletePercent(searchResult);
  504. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  505. };
  506. const calculateCompletePercent = function (searchResult) {
  507. if (!searchResult) return;
  508. for (const sr of searchResult) {
  509. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  510. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  511. }
  512. };
  513. $('input', obj).bind('keydown', function (e) {
  514. if (e.keyCode == 13) search();
  515. });
  516. $('button', obj).bind('click', () => {search()});
  517. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  518. const sheet = info.sheet;
  519. const data = sheet.zh_data;
  520. if (!data) { return }
  521. const curBills = data[info.row];
  522. if (!curBills) { return }
  523. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  524. if (setting.afterLocated) {
  525. setting.afterLocated();
  526. }
  527. });
  528. return {spread: resultSpread};
  529. };
  530. })(jQuery);