cost_gather.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. const ledgerGatherSpreadSetting = {
  4. cols: [
  5. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 230, formatter: '@', cellType: 'tree'},
  6. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', cellType: 'autoTip'},
  7. ],
  8. baseCols: [
  9. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 230, formatter: '@', cellType: 'tree'},
  10. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', cellType: 'autoTip'},
  11. ],
  12. extraCols: [
  13. {title: '%s|付款金额', colSpan: '4|1', rowSpan: '1|1', field: 'pay_tp', hAlign: 2, width: 80, type: 'Number'},
  14. {title: '|扣款金额', colSpan: '|1', rowSpan: '|1', field: 'cut_tp', hAlign: 2, width: 80, type: 'Number'},
  15. {title: '|应付金额', colSpan: '|1', rowSpan: '|1', field: 'yf_tp', hAlign: 2, width: 80, type: 'Number'},
  16. {title: '|实付金额', colSpan: '|1', rowSpan: '|1', field: 'sf_tp', hAlign: 2, width: 80, type: 'Number'},
  17. ],
  18. emptyRows: 0,
  19. headRows: 2,
  20. headRowHeight: [25, 25],
  21. defaultRowHeight: 21,
  22. headerFont: '12px 微软雅黑',
  23. font: '12px 微软雅黑',
  24. readOnly: true,
  25. };
  26. sjsSettingObj.setFxTreeStyle(ledgerGatherSpreadSetting, sjsSettingObj.FxTreeStyle.phasePay);
  27. const ledgerGatherSpread = SpreadJsObj.createNewSpread($('#ledger-gather-spread')[0]);
  28. const ledgerGatherSheet = ledgerGatherSpread.getActiveSheet();
  29. SpreadJsObj.initSheet(ledgerGatherSheet, ledgerGatherSpreadSetting);
  30. const ledgerGatherObj = {
  31. tree: createNewPathTree('ledger', {
  32. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  33. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  34. rootId: -1, calcFields: [],
  35. }),
  36. stages: [],
  37. splitStages (gatherType, gatherStageIds) {
  38. const gatherStages = this.stages.filter(x => { return gatherStageIds.indexOf(x.id) >= 0; });
  39. gatherStages.sort((a, b) => { return a.stage_order - b.stage_order; });
  40. const result = { main: gatherStages[gatherStages.length - 1], gatherPart: [] };
  41. for (const gs of gatherStages) {
  42. if (gatherType === 'stage') {
  43. result.gatherPart.push({title: `第${gs.stage_order}期(${gs.stage_date})`, stages: [gs]});
  44. } else {
  45. let title;
  46. if (gatherType === 'month') title = `${gs.stage_date}`;
  47. if (gatherType === 'quarter') title = `${gs.gather_year} 第${gs.gather_quarter}季度`;
  48. if (gatherType === 'year') title = `${gs.gather_year}`;
  49. let gp = result.gatherPart.find(x => { return x.title === title; });
  50. if (!gp) {
  51. gp = { title, stages: [] };
  52. result.gatherPart.push(gp);
  53. }
  54. gp.stages.push(gs);
  55. }
  56. }
  57. return result;
  58. },
  59. gather (gatherType, gatherStageIds) {
  60. if (gatherStageIds.length === 0) return;
  61. const gatherInfo = this.splitStages(gatherType, gatherStageIds);
  62. this.tree.setting.calcFields.length = 0;
  63. this.tree.loadDatas(gatherInfo.main.detail.bills);
  64. ledgerGatherSpreadSetting.cols.length = 0;
  65. ledgerGatherSpreadSetting.cols.push(...ledgerGatherSpreadSetting.baseCols);
  66. for (const [i, gp] of gatherInfo.gatherPart.entries()) {
  67. const loadFields = [];
  68. for (const ec of ledgerGatherSpreadSetting.extraCols) {
  69. const col = JSON.parse(JSON.stringify(ec));
  70. col.title = col.title.replace('%s', gp.title);
  71. loadFields.push({ sf: col.field, tf: col.field + '_' + i});
  72. col.field = col.field + '_' + i;
  73. ledgerGatherSpreadSetting.cols.push(col);
  74. this.tree.setting.calcFields.push(col.field);
  75. this.tree.nodes.forEach(x => { x[col.field] = 0; });
  76. }
  77. for (const s of gp.stages) {
  78. for (const b of s.detail.bills) {
  79. const node = this.tree.nodes.find(x => { return x.cost_id === b.cost_id; });
  80. if (!node) continue;
  81. for (const lf of loadFields) {
  82. node[lf.tf] = ZhCalc.add(node[lf.tf], b[lf.sf]);
  83. }
  84. }
  85. }
  86. }
  87. treeCalc.calculateAll(this.tree);
  88. SpreadJsObj.initSheet(ledgerGatherSheet, ledgerGatherSpreadSetting);
  89. SpreadJsObj.loadSheetData(ledgerGatherSheet, SpreadJsObj.DataType.Tree, this.tree);
  90. SpreadJsObj.locateRow(ledgerGatherSheet, 0);
  91. },
  92. loadStages (stages) {
  93. for (const s of stages) {
  94. s.gather_stage = s.stage_order;
  95. s.gather_month = s.stage_date;
  96. s.gather_time = moment(new Date(s.stage_date));
  97. s.gather_quarter = s.gather_time.quarter();
  98. s.gather_year = s.gather_time.year();
  99. this.stages.push(s);
  100. }
  101. },
  102. exportExcel() {
  103. if (!this.tree || this.tree.nodes.length === 0) {
  104. toastr.warning('无可导出数据');
  105. return;
  106. }
  107. SpreadExcelObj.exportSimpleXlsxSheet(ledgerGatherSpreadSetting, this.tree.nodes, "成本管理-汇总分析.xlsx");
  108. }
  109. };
  110. $('#select-qi-all').click(function() {
  111. const check = this.checked;
  112. $('input', 'tr[stage-id]').each((i, x) => {
  113. x.checked = check;
  114. })
  115. });
  116. $('#select-qi-ok').click(function() {
  117. let loadData = [], gatherData = [], trs = $('tr[stage-id]');
  118. for (let order = 0, iLength = trs.length; order < iLength; order++) {
  119. const tr = trs[order];
  120. if ($('input[type=checkbox]', tr)[0].checked) {
  121. const sid = tr.getAttribute('stage-id');
  122. const exist = ledgerGatherObj.stages.find(x => { return x.id === sid; });
  123. if (!exist) loadData.push(sid);
  124. gatherData.push(sid);
  125. }
  126. }
  127. const gatherType = $('input[name=gather-type]:checked').val();
  128. if (loadData.length > 0) {
  129. postData(window.location.pathname + '/load', {stages: loadData}, function (result) {
  130. ledgerGatherObj.loadStages(result.stages);
  131. ledgerGatherObj.gather(gatherType, gatherData);
  132. $('#select-qi').modal('hide');
  133. }, null, true);
  134. } else {
  135. ledgerGatherObj.gather(gatherType, gatherData);
  136. $('#select-qi').modal('hide');
  137. }
  138. });
  139. $('#export-excel').click(function() {
  140. ledgerGatherObj.exportExcel();
  141. });
  142. // 显示层次
  143. (function (select, sheet) {
  144. $(select).click(function () {
  145. if (!sheet.zh_tree) return;
  146. const tag = $(this).attr('tag');
  147. const tree = sheet.zh_tree;
  148. switch (tag) {
  149. case "1":
  150. case "2":
  151. tree.expandByLevel(parseInt(tag));
  152. SpreadJsObj.refreshTreeRowVisible(sheet);
  153. break;
  154. case "last":
  155. tree.expandByCustom(() => { return true; });
  156. SpreadJsObj.refreshTreeRowVisible(sheet);
  157. break;
  158. }
  159. });
  160. })('a[name=showLevel]', ledgerGatherSheet);
  161. $.subMenu({
  162. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  163. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  164. key: 'menu.1.0.0',
  165. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  166. callback: function (info) {
  167. if (info.mini) {
  168. $('.panel-title').addClass('fluid');
  169. $('#sub-menu').removeClass('panel-sidebar');
  170. $('.c-body table thead').css('left', '56px');
  171. } else {
  172. $('.panel-title').removeClass('fluid');
  173. $('#sub-menu').addClass('panel-sidebar');
  174. $('.c-body table thead').css('left', '176px');
  175. }
  176. autoFlashHeight();
  177. ledgerGatherSpread.refresh();
  178. }
  179. });
  180. });