budget_compare.js 21 KB

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