budget_compare.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. $(document).ready(() => {
  10. const compareTypeKey = 'budget-compareType';
  11. const stackedBarCoverKey = 'budget-stackedBarCover';
  12. const stackedBarKey = 'budget-stackedBar';
  13. autoFlashHeight();
  14. const compareSpread = SpreadJsObj.createNewSpread($('#cost-compare')[0]);
  15. const compareSheet = compareSpread.getActiveSheet();
  16. const spreadSetting = {
  17. cols: [
  18. {title: '项目节编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'},
  19. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 230, formatter: '@'},
  20. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', cellType: 'unit'},
  21. {title: '投资估算|数量1/数量2', colSpan: '3|1', rowSpan: '1|1', field: 'gu_dgn_qty', hAlign: 2, width: 80, bc_type: 'number'},
  22. {title: '|经济指标', colSpan: '|1', rowSpan: '|1', field: 'gu_dgn_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  23. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'gu_tp', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  24. {title: '初步概算|数量1/数量2', colSpan: '3|1', rowSpan: '1|1', field: 'gai_dgn_qty', hAlign: 2, width: 80, bc_type: 'number'},
  25. {title: '|经济指标', colSpan: '|1', rowSpan: '|1', field: 'gai_dgn_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  26. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'gai_tp', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  27. {title: '施工图预算|数量1/数量2', colSpan: '3|1', rowSpan: '1|1', field: 'yu_dgn_qty', hAlign: 2, width: 80, bc_type: 'number'},
  28. {title: '|经济指标', colSpan: '|1', rowSpan: '|1', field: 'yu_dgn_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  29. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'yu_tp', hAlign: 2, width: 80, type: 'Number', bc_type: 'number'},
  30. {title: '台账|数量1/数量2', colSpan: '3|1', rowSpan: '1|1', field: 'dgn_qty', hAlign: 2, width: 80, bc_type: 'number', visible: false},
  31. {title: '|经济指标', colSpan: '|1', rowSpan: '|1', field: 'dgn_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number', visible: false},
  32. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'total_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number', visible: false},
  33. {title: '决算|数量1/数量2', colSpan: '3|1', rowSpan: '1|1', field: 'final_dgn_qty', hAlign: 2, width: 80, bc_type: 'number', visible: false},
  34. {title: '|经济指标', colSpan: '|1', rowSpan: '|1', field: 'final_dgn_price', hAlign: 2, width: 80, type: 'Number', bc_type: 'number', visible: false},
  35. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'final_tp', hAlign: 2, width: 80, type: 'Number', bc_type: 'number', visible: false},
  36. {title: '数据对比', colSpan: '1', rowSpan: '2', field: 'stackedBar', hAlign: 0, width: 300, cellType: 'stackedBar', stackedBarCover: false, bc_type: 'grid', visible: false},
  37. {title: '增幅%|数量1/数量2', colSpan: '2|1', rowSpan: '1|1', field: 'grow_dgn_qty', hAlign: 2, width: 80},
  38. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'grow_tp', hAlign: 2, width: 80, type: 'Number'},
  39. ],
  40. emptyRows: 0,
  41. headRows: 2,
  42. headRowHeight: [25, 25],
  43. defaultRowHeight: 21,
  44. headerFont: '12px 微软雅黑',
  45. font: '12px 微软雅黑',
  46. readOnly: true,
  47. frozenColCount: 3,
  48. frozenLineColor: '#93b5e4',
  49. localCache: { key: 'budget-compare', colWidth: true },
  50. };
  51. sjsSettingObj.setFxTreeStyle(spreadSetting, sjsSettingObj.FxTreeStyle.jz);
  52. let sfSelect;
  53. const compareObj = {
  54. curFinalId() {
  55. return this.finalInfo ? this.finalInfo.id : undefined;
  56. },
  57. initFinalCol() {
  58. const finalColField = ['dgn_qty', 'dgn_price', 'total_price', 'final_dgn_qty', 'final_dgn_price', 'final_tp', 'grow_dgn_qty', 'grow_tp'];
  59. const finalCol = spreadSetting.cols.filter(x => { return finalColField.indexOf(x.field) >= 0; });
  60. if (finalCol.length > 0 && !finalCol[0].visible) {
  61. finalCol.forEach(x => { x.visible = true; });
  62. this.initShowType();
  63. SpreadJsObj.refreshColumnVisible(compareSheet);
  64. }
  65. },
  66. initShowType() {
  67. const type = this.compareType;
  68. spreadSetting.cols.forEach(x => {
  69. if (!x.bc_type) return;
  70. x.visible = x.bc_type === type;
  71. });
  72. const colIndex = spreadSetting.cols.findIndex(x => { return x.field === 'stackedBar'});
  73. spreadSetting.cols[colIndex].stackedBarCover = parseInt(this.stackedBarCover);
  74. },
  75. expand(tree, tag) {
  76. switch (tag) {
  77. case "1":
  78. case "2":
  79. case "3":
  80. case "4":
  81. case "5":
  82. tree.expandByLevel(parseInt(tag));
  83. break;
  84. case "last":
  85. tree.expandByCustom(() => { return true; });
  86. break;
  87. }
  88. },
  89. calcStackedBar(tree) {
  90. const calcField = this.stackedBarField;
  91. const calcFieldColor = { 'gu_tp': '#657798', 'gai_tp': '#EE6666', 'yu_tp': '#74CBED', 'total_price': '#FAC858', 'final_tp': '#62DAAB' };
  92. const calc = function(node, base){
  93. // const parent = tree.getParent(node);
  94. // if (!parent) {
  95. // base = 0;
  96. // for (const cf of calcField) {
  97. // base = Math.max(node[cf], base);
  98. // }
  99. // }
  100. node.stackedBar = [];
  101. for (const cf of calcField) {
  102. node.stackedBar.push({color: calcFieldColor[cf], percent: ZhCalc.div(node[cf], base), field: cf});
  103. }
  104. if (node.children) {
  105. for (const child of node.children) {
  106. calc(child, base);
  107. }
  108. }
  109. };
  110. let commonBase = 0;
  111. tree.children.forEach(x => {
  112. for (const cf of calcField) {
  113. commonBase = Math.max(x[cf], commonBase);
  114. }
  115. });
  116. for (const child of tree.children) {
  117. calc(child, commonBase);
  118. }
  119. },
  120. loadBudgetData(result) {
  121. const compareTree = createNewPathTree('final', {
  122. id: 'id',
  123. pid: 'pid',
  124. order: 'order',
  125. level: 'level',
  126. rootId: -1,
  127. });
  128. const setting = { id: 'tree_id', pid: 'tree_pid', order: 'order', level: 'level', rootId: -1, calcFields: ['total_price'] };
  129. const guTree = createNewPathTree('ledger', setting);
  130. guTree.loadDatas(result.gu);
  131. treeCalc.calculateAll(guTree);
  132. compareTree.loadTree(guTree, function (cur, source) {
  133. cur.base = true;
  134. cur.gu_dgn_qty1 = ZhCalc.add(cur.gu_dgn_qty1, source.dgn_qty1);
  135. cur.gu_dgn_qty2 = ZhCalc.add(cur.gu_dgn_qty2, source.dgn_qty2);
  136. cur.gu_tp = ZhCalc.add(cur.gu_tp, source.total_price);
  137. });
  138. const gaiTree = createNewPathTree('ledger', setting);
  139. gaiTree.loadDatas(result.gai);
  140. treeCalc.calculateAll(gaiTree);
  141. compareTree.loadTree(gaiTree, function (cur, source) {
  142. cur.base = true;
  143. cur.gai_dgn_qty1 = ZhCalc.add(cur.gai_dgn_qty1, source.dgn_qty1);
  144. cur.gai_dgn_qty2 = ZhCalc.add(cur.gai_dgn_qty2, source.dgn_qty2);
  145. cur.gai_tp = ZhCalc.add(cur.gai_tp, source.total_price);
  146. });
  147. const yuTree = createNewPathTree('ledger', setting);
  148. yuTree.loadDatas(result.yu);
  149. treeCalc.calculateAll(yuTree);
  150. compareTree.loadTree(yuTree, function (cur, source) {
  151. cur.base = true;
  152. cur.yu_dgn_qty1 = ZhCalc.add(cur.yu_dgn_qty1, source.dgn_qty1);
  153. cur.yu_dgn_qty2 = ZhCalc.add(cur.yu_dgn_qty2, source.dgn_qty2);
  154. cur.yu_tp = ZhCalc.add(cur.yu_tp, source.total_price);
  155. });
  156. compareTree.afterLoad(node => {
  157. node.gu_dgn_price = ZhCalc.div(node.gu_tp, node.gu_dgn_qty1, 2);
  158. node.gu_dgn_qty = node.gu_dgn_qty1
  159. ? (node.gu_dgn_qty2 ? node.gu_dgn_qty1 + '/' + node.gu_dgn_qty2 : node.gu_dgn_qty1)
  160. : (node.gu_dgn_qty2 ? '/' + node.gu_dgn_qty2 : '');
  161. node.gai_dgn_price = ZhCalc.div(node.gai_tp, node.gai_dgn_qty1, 2);
  162. node.gai_dgn_qty = node.gai_dgn_qty1
  163. ? (node.gai_dgn_qty2 ? node.gai_dgn_qty1 + '/' + node.gai_dgn_qty2 : node.gai_dgn_qty1)
  164. : (node.gai_dgn_qty2 ? '/' + node.gai_dgn_qty2 : '');
  165. node.yu_dgn_price = ZhCalc.div(node.yu_tp, node.yu_dgn_qty1, 2);
  166. node.yu_dgn_qty = node.yu_dgn_qty1
  167. ? (node.yu_dgn_qty2 ? node.yu_dgn_qty1 + '/' + node.yu_dgn_qty2 : node.yu_dgn_qty1)
  168. : (node.yu_dgn_qty2 ? '/' + node.yu_dgn_qty2 : '');
  169. });
  170. compareTree.resortChildrenByCustom(function (x, y) {
  171. const iCode = compareCode(x.code, y.code);
  172. if (iCode) return iCode;
  173. if (!x.name) return -1;
  174. if (!y.name) return 1;
  175. return x.name.localeCompare(y.name);
  176. });
  177. const expandTag = getLocalCache('revise-compare-level');
  178. if (expandTag) compareObj.expand(compareTree, expandTag);
  179. this.calcStackedBar(compareTree);
  180. SpreadJsObj.loadSheetData(compareSheet, SpreadJsObj.DataType.Tree, compareTree);
  181. },
  182. loadFinalData(result, msg) {
  183. if (msg) toastr.warning(msg);
  184. this.finalInfo = result.finalInfo;
  185. $('#final-info').html(`${moment(result.finalInfo.update_time).format('YYYY-MM-DD HH:mm:ss')} ${result.finalInfo.u_name}(${result.finalInfo.u_role})`);
  186. this.initFinalCol();
  187. const finalTree = createNewPathTree('ledger', {
  188. id: 'tree_id',
  189. pid: 'tree_pid',
  190. order: 'order',
  191. level: 'level',
  192. rootId: -1,
  193. });
  194. finalTree.loadDatas(result.final);
  195. const expandTag = getLocalCache('revise-compare-level');
  196. if (expandTag) compareObj.expand(finalTree, expandTag);
  197. this.calcStackedBar(finalTree);
  198. SpreadJsObj.loadSheetData(compareSheet, SpreadJsObj.DataType.Tree, finalTree);
  199. if (sfSelect) sfSelect.reloadSelect(this.finalInfo.tender);
  200. },
  201. loadCacheData(){
  202. let stackedBarCache = getLocalCache(stackedBarKey);
  203. if (stackedBarCache === null) stackedBarCache = 'gai_tp,total_price,final_tp';
  204. this.setStackedBarField(stackedBarCache ? stackedBarCache.split(',') : []);
  205. this.setCompareType(getLocalCache(compareTypeKey));
  206. this.setStackedBarCover(getLocalCache(stackedBarCoverKey));
  207. this.initShowType();
  208. },
  209. setStackedBarField(field){
  210. this.stackedBarField = field;
  211. setLocalCache(stackedBarKey, field.join(','));
  212. if (compareSheet.zh_tree) this.calcStackedBar(compareSheet.zh_tree);
  213. const colIndex = spreadSetting.cols.findIndex(x => { return x.field === 'stackedBar'});
  214. SpreadJsObj.reloadColData(compareSheet, colIndex);
  215. },
  216. setCompareType(type) {
  217. this.compareType = type || 'number';
  218. $('[name=showType]').removeClass('active');
  219. $(`[tag=${this.compareType}]`).addClass('active');
  220. if (this.compareType === 'grid') {
  221. $('.ml-auto').show();
  222. } else {
  223. $('.ml-auto').hide();
  224. }
  225. spreadSetting.cols.forEach(x => {
  226. if (!x.bc_type) return;
  227. x.visible = x.bc_type === type;
  228. });
  229. setLocalCache(compareTypeKey, this.compareType);
  230. },
  231. setStackedBarCover(cover){
  232. this.stackedBarCover = cover || '1';
  233. const colIndex = spreadSetting.cols.findIndex(x => { return x.field === 'stackedBar'});
  234. spreadSetting.cols[colIndex].stackedBarCover = parseInt(cover);
  235. SpreadJsObj.reloadColData(compareSheet, colIndex);
  236. setLocalCache(stackedBarCoverKey, this.stackedBarCover);
  237. }
  238. };
  239. compareObj.loadCacheData();
  240. SpreadJsObj.initSheet(compareSheet, spreadSetting);
  241. function compareCode(str1, str2, symbol = '-') {
  242. if (!str1) {
  243. return 1;
  244. } else if (!str2) {
  245. return -1;
  246. }
  247. function compareSubCode(code1, code2) {
  248. if (numReg.test(code1)) {
  249. if (numReg.test(code2)) {
  250. return parseInt(code1) - parseInt(code2);
  251. } else {
  252. return -1
  253. }
  254. } else {
  255. if (numReg.test(code2)) {
  256. return 1;
  257. } else {
  258. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
  259. }
  260. }
  261. }
  262. const numReg = /^[0-9]+$/;
  263. const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
  264. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  265. const iCompare = compareSubCode(aCodes[i], bCodes[i]);
  266. if (iCompare !== 0) {
  267. return iCompare;
  268. }
  269. }
  270. return aCodes.length - bCodes.length;
  271. }
  272. postData(window.location.pathname + '/load', {}, function (result, msg) {
  273. if (result.final) {
  274. compareObj.loadFinalData(result, msg);
  275. } else {
  276. compareObj.loadBudgetData(result);
  277. }
  278. });
  279. $.subMenu({
  280. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  281. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  282. key: 'menu.1.0.0',
  283. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  284. callback: function (info) {
  285. if (info.mini) {
  286. $('.panel-title').addClass('fluid');
  287. $('#sub-menu').removeClass('panel-sidebar');
  288. } else {
  289. $('.panel-title').removeClass('fluid');
  290. $('#sub-menu').addClass('panel-sidebar');
  291. }
  292. autoFlashHeight();
  293. compareSpread.refresh();
  294. }
  295. });
  296. // 显示层次
  297. (function (select, sheet) {
  298. $(select).click(function () {
  299. if (!sheet.zh_tree) return;
  300. const tag = $(this).attr('tag');
  301. const tree = sheet.zh_tree;
  302. setTimeout(() => {
  303. showWaitingView();
  304. compareObj.expand(tree, tag);
  305. SpreadJsObj.refreshTreeRowVisible(sheet);
  306. setLocalCache('revise-compare-level', tag);
  307. closeWaitingView();
  308. }, 100);
  309. });
  310. })('a[name=showLevel]', compareSheet);
  311. class sfObject {
  312. constructor() {
  313. const self = this;
  314. this.selectTree = Tender2Tree.convert(category, tenderList, null, null, function (node, source) {
  315. node.lastStageOrder =`第${source.lastStageOrder}期`;
  316. node.lastStageStatus = source.lastStageStatus;
  317. });
  318. if (compareObj.finalInfo) {
  319. this.selectTree.datas.forEach(x => {
  320. x.selected = compareObj.finalInfo.tender.indexOf(x.tid + '') >= 0;
  321. })
  322. }
  323. const sfSpreadSetting = {
  324. cols: [
  325. {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
  326. {title: '名称', field: 'name', hAlign: 0, width: 180, formatter: '@', cellType: 'tree'},
  327. {title: '期数', field: 'lastStageOrder', hAlign: 1, width: 60, formatter: '@'},
  328. {title: '审批状态', field: 'lastStageStatus', hAlign: 1, width: 60, formatter: '@'},
  329. ],
  330. emptyRows: 0,
  331. headRows: 1,
  332. headRowHeight: [32],
  333. defaultRowHeight: 21,
  334. headerFont: '12px 微软雅黑',
  335. font: '12px 微软雅黑',
  336. headColWidth: [30],
  337. selectedBackColor: '#fffacd',
  338. readOnly: true,
  339. };
  340. this.spread = SpreadJsObj.createNewSpread($('#sf-spread')[0]);
  341. this.sheet = this.spread.getActiveSheet();
  342. SpreadJsObj.initSheet(this.sheet, sfSpreadSetting);
  343. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.selectTree);
  344. this.spread.bind(spreadNS.Events.ButtonClicked, function (e, info) {
  345. if (!info.sheet.zh_setting) return;
  346. const col = info.sheet.zh_setting.cols[info.col];
  347. if (col.field !== 'selected') return;
  348. const node = SpreadJsObj.getSelectObject(info.sheet);
  349. self.selectNode(node, !node[col.field]);
  350. SpreadJsObj.reloadColData(info.sheet, 0);
  351. });
  352. $('#sf-select-all').click(function() {
  353. for (const n of self.selectTree.nodes) {
  354. n.selected = this.checked;
  355. }
  356. SpreadJsObj.reloadColData(self.sheet, 0);
  357. });
  358. $('#select-final-ok').click(() => {
  359. const rela = self.getSelects();
  360. if (rela.length === 0) return;
  361. postData(window.location.pathname + '/final', {final_id: compareObj.curFinalId(), id: rela}, function(result, msg) {
  362. compareObj.loadFinalData(result, msg);
  363. $('#select-final').modal('hide');
  364. });
  365. });
  366. }
  367. reloadSelect(select) {
  368. if (compareObj.finalInfo) {
  369. this.selectTree.datas.forEach(x => {
  370. x.selected = select.indexOf(x.tid + '') >= 0;
  371. })
  372. }
  373. SpreadJsObj.reloadColData(this.sheet, 0);
  374. }
  375. selectNode(node, select) {
  376. const posterity = this.selectTree.getPosterity(node);
  377. posterity.unshift(node);
  378. for (const p of posterity) {
  379. p.selected = select;
  380. }
  381. }
  382. getSelects() {
  383. const select = [];
  384. for (const n of this.selectTree.nodes) {
  385. if ((!n.children || n.children.length === 0) && n.selected) select.push(n.tid);
  386. }
  387. return select;
  388. }
  389. }
  390. $('#select-final').on('shown.bs.modal', () => {
  391. if (!sfSelect) sfSelect = new sfObject();
  392. });
  393. $('#stackedBar-ok').click(function() {
  394. const checked = $('[name=stackedBar]:checked');
  395. const field = [];
  396. checked.each((i, x) => { field.push(x.value)});
  397. compareObj.setStackedBarField(field);
  398. });
  399. $('#dp-stackedBar').click(function() {
  400. const field = compareObj.stackedBarField;
  401. const checked = $('[name=stackedBar]');
  402. checked.each((i, x) => { x.checked = field.indexOf(x.value) >= 0; });
  403. });
  404. $('a[name=showType]').click(function () {
  405. const type = this.getAttribute('tag');
  406. compareObj.setCompareType(type);
  407. SpreadJsObj.refreshColumnVisible(compareSheet);
  408. });
  409. $('input[name=stackedBarCover]').change(function() {
  410. const cover = this.getAttribute('tag');
  411. compareObj.setStackedBarCover(cover);
  412. });
  413. $('#dp-cover').click(function() {
  414. const cover = compareObj.stackedBarCover;
  415. const checked = $('[name=stackedBarCover]');
  416. checked.each((i, x) => { x.checked = x.getAttribute('tag') === cover; });
  417. });
  418. });