spss_gather_ledger.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. const billsCompareField = ['quantity', 'total_price', ];
  2. const posCompareField = ['quantity'];
  3. $(document).ready(() => {
  4. autoFlashHeight();
  5. const billsSpreadSetting = {
  6. cols: [
  7. {title: '项目节编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 185, formatter: '@', cellType: 'tree'},
  8. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 100, formatter: '@'},
  9. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 235, formatter: '@'},
  10. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  11. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 60, type: 'Number'},
  12. {title: '合计|数量', colSpan: '2|1', rowSpan: '1|1', field: 'sum_quantity', hAlign: 2, width: 80, type: 'Number'},
  13. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'sum_total_price', hAlign: 2, width: 80, type: 'Number'},
  14. ],
  15. baseCols: [
  16. {title: '项目节编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 185, formatter: '@', cellType: 'tree'},
  17. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 100, formatter: '@'},
  18. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 235, formatter: '@'},
  19. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  20. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 60, type: 'Number'},
  21. ],
  22. extraCols: [
  23. {title: '标段1|数量', colSpan: '2|1', rowSpan: '1|1', field: 'quantity_1', hAlign: 2, width: 80, type: 'Number', formatTitle: '%s|数量', formatField: 'quantity_%d'},
  24. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'total_price_1', hAlign: 2, width: 80, type: 'Number', formatField: 'total_price_%d'},
  25. ],
  26. endCols: [
  27. {title: '合计|数量', colSpan: '2|1', rowSpan: '1|1', field: 'sum_quantity', hAlign: 2, width: 80, type: 'Number'},
  28. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'sum_total_price', hAlign: 2, width: 80, type: 'Number'},
  29. ],
  30. emptyRows: 0,
  31. headRows: 2,
  32. headRowHeight: [32, 25],
  33. defaultRowHeight: 21,
  34. headerFont: '12px 微软雅黑',
  35. font: '12px 微软雅黑',
  36. readOnly: true,
  37. };
  38. const posSpreadSetting = {
  39. cols: [
  40. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 235, formatter: '@'},
  41. {title: '差值', colSpan: '1', rowSpan: '1', field: 'su,_quantity', hAlign: 2, width: 100, type: 'Number'},
  42. ],
  43. baseCols: [
  44. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 235, formatter: '@'},
  45. ],
  46. extraCols: [
  47. {title: '标段1', colSpan: '1', rowSpan: '1', field: 'quantity_1', hAlign: 2, width: 100, type: 'Number', formatTitle: '%s', formatField: 'quantity_%d'},
  48. ],
  49. endCols: [
  50. {title: '合计', colSpan: '1', rowSpan: '1', field: 'sum_quantity', hAlign: 2, width: 100, type: 'Number'},
  51. ],
  52. emptyRows: 0,
  53. headRows: 1,
  54. headRowHeight: [32],
  55. defaultRowHeight: 21,
  56. headerFont: '12px 微软雅黑',
  57. font: '12px 微软雅黑',
  58. readOnly: true,
  59. };
  60. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  61. const billsSheet = billsSpread.getActiveSheet();
  62. sjsSettingObj.setFxTreeStyle(billsSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  63. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  64. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  65. const posSheet = posSpread.getActiveSheet();
  66. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  67. const treeSetting = {
  68. id: 'ledger_id',
  69. pid: 'ledger_pid',
  70. order: 'order',
  71. level: 'level',
  72. full_path: 'full_path',
  73. rootId: -1,
  74. keys: ['id', 'tender_id', 'ledger_id'],
  75. calcFields: ['sum_tp'],
  76. // todo 判断同一清单时,是否应区分 清单是否含计量单元?
  77. // findNode: function(tree, node, parent, source) {
  78. // const siblings = parent ? parent.children : tree.children;
  79. // const checkData = { code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '', unit_price: node.unit_price || 0 };
  80. // return siblings.find(function (x) {
  81. // if (!checkData.b_code) return x.code === checkData.code && x.name === checkData.name;
  82. // const posRange = source.pos.getLedgerPos(node.id);
  83. // const hasPos = posRange && posRange.length > 0;
  84. // const xHasPos = x.pos && x.pos.length > 0;
  85. // return hasPos === xHasPos && x.b_code === checkData.b_code && x.name === checkData.name && x.unit === checkData.unit && x.unit_price === checkData.unit_price;
  86. // });
  87. // },
  88. loadInfo: function (node, sourceNode, index, source) {
  89. for (const f of billsCompareField) {
  90. node[f + '_' + index] = sourceNode[f];
  91. }
  92. const posRange = source.pos.getLedgerPos(sourceNode.id);
  93. if (posRange && posRange.length > 0) {
  94. if (!node.pos) node.pos = [];
  95. for (const p of posRange) {
  96. let nP = _.find(node.pos, {name: p.name || ''});
  97. if (!nP) {
  98. nP = {name: p.name || ''};
  99. node.pos.push(nP);
  100. }
  101. for (const f of posCompareField) {
  102. nP[f + '_' + index] = p[f];
  103. }
  104. }
  105. }
  106. },
  107. calcSum: function(data, count) {
  108. for (const f of billsCompareField) {
  109. data['sum_' + f] = 0;
  110. for (let i = 1; i <= count; i++) {
  111. data['sum_' + f] = ZhCalc.add(data['sum_' + f], data[f + '_' + i]);
  112. }
  113. }
  114. for (const p of data.pos) {
  115. for (const f of posCompareField) {
  116. p['sum_' + f] = 0;
  117. for (let i = 1; i <= count; i++) {
  118. p['sum_' + f] = ZhCalc.add(p['sum_' + f], p[f + '_' + i]);
  119. }
  120. }
  121. }
  122. },
  123. };
  124. const billsTree = createNewPathTree('tree-gather', treeSetting);
  125. const billsTreeSpreadObj = {
  126. selectionChanged: function (e, info) {
  127. if (info.newSelections) {
  128. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  129. posSpreadObj.loadCurPosData();
  130. }
  131. }
  132. },
  133. rebuildSpreadSetting(tenders) {
  134. billsSpreadSetting.cols = [...billsSpreadSetting.baseCols];
  135. posSpreadSetting.cols = [...posSpreadSetting.baseCols];
  136. for (const [i, tender] of tenders.entries()) {
  137. for (const col of billsSpreadSetting.extraCols) {
  138. const newCol = JSON.parse(JSON.stringify(col));
  139. if (newCol.formatTitle) newCol.title = newCol.formatTitle.replace('%s', tender.name);
  140. if (newCol.formatField) newCol.field = newCol.formatField.replace('%d', i + 1);
  141. billsSpreadSetting.cols.push(newCol);
  142. }
  143. for (const col of posSpreadSetting.extraCols) {
  144. const newCol = JSON.parse(JSON.stringify(col));
  145. if (newCol.formatTitle) newCol.title = newCol.formatTitle.replace('%s', tender.name);
  146. if (newCol.formatField) newCol.field = newCol.formatField.replace('%d', i + 1);
  147. posSpreadSetting.cols.push(newCol)
  148. }
  149. }
  150. billsSpreadSetting.cols.push(...billsSpreadSetting.endCols);
  151. posSpreadSetting.cols.push(...posSpreadSetting.endCols);
  152. }
  153. };
  154. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  155. const posSpreadObj = {
  156. loadCurPosData: function () {
  157. const node = SpreadJsObj.getSelectObject(billsSheet);
  158. console.log(node);
  159. if (node) {
  160. SpreadJsObj.loadSheetData(posSheet, 'data', node.pos || []);
  161. } else {
  162. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  163. }
  164. },
  165. };
  166. const tenderSelect = TenderSelectMulti({
  167. title: '汇总标段',
  168. type: 'gather',
  169. dataType: 'ledger',
  170. afterSelect: function(select) {
  171. const data = { filter: 'ledger', tender: select };
  172. postData(`/sp/${spid}/spss/load`, data, function(result) {
  173. billsTreeSpreadObj.rebuildSpreadSetting(result);
  174. SpreadJsObj.reLoadSheetHeader(billsSheet);
  175. SpreadJsObj.reLoadSheetHeader(posSheet);
  176. treeSetting.calcFields = ['sum_total_price'];
  177. for (let i = 1; i <= result.length; i++) {
  178. treeSetting.calcFields.push('total_price_' + i);
  179. }
  180. const tenderTreeSetting = {
  181. id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level',
  182. rootId: -1,
  183. keys: ['id', 'tender_id', 'ledger_id'],
  184. calcFields: ['total_price'],
  185. };
  186. const posSetting = { id: 'id', ledgerId: 'lid' };
  187. const tenders = [];
  188. for (const t of result) {
  189. const tender = {
  190. billsTree: createNewPathTree('ledger', tenderTreeSetting),
  191. pos: new PosData(posSetting),
  192. };
  193. tender.billsTree.loadDatas(t.bills);
  194. tender.pos.loadDatas(t.pos);
  195. tenders.push(tender);
  196. }
  197. billsTree.clearDatas();
  198. billsTree.loadGatherData(tenders);
  199. treeCalc.calculateAll(billsTree);
  200. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree, true);
  201. posSpreadObj.loadCurPosData();
  202. });
  203. },
  204. });
  205. $('#gather-select').click(tenderSelect.showSelect);
  206. $('#export-excel').click(function() {
  207. const excelData = [];
  208. for (const node of billsTree.nodes) {
  209. const data = {};
  210. for (const c of billsSpreadSetting.cols) {
  211. data[c.field] = node[c.field];
  212. }
  213. excelData.push(data);
  214. for (const p of node.pos) {
  215. const pData = {};
  216. for (const c of posSpreadSetting.cols) {
  217. pData[c.field] = p[c.field];
  218. }
  219. excelData.push(pData);
  220. }
  221. }
  222. SpreadExcelObj.exportSimpleXlsxSheet(billsSpreadSetting, excelData, "台账汇总.xlsx");
  223. });
  224. $.divResizer({
  225. select: '#pos-resize',
  226. callback: function () {
  227. billsSpread.refresh();
  228. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  229. $(".sp-wrap").height(bcontent-30);
  230. posSpread.refresh();
  231. }
  232. });
  233. // 显示层次
  234. (function (select, sheet) {
  235. $(select).click(function () {
  236. if (!sheet.zh_tree) return;
  237. const tag = $(this).attr('tag');
  238. const tree = sheet.zh_tree;
  239. switch (tag) {
  240. case "1":
  241. case "2":
  242. case "3":
  243. case "4":
  244. case "5":
  245. tree.expandByLevel(parseInt(tag));
  246. SpreadJsObj.refreshTreeRowVisible(sheet);
  247. break;
  248. case "last":
  249. tree.expandByCustom(() => { return true; });
  250. SpreadJsObj.refreshTreeRowVisible(sheet);
  251. break;
  252. case "leafXmj":
  253. tree.expandToLeafXmj();
  254. SpreadJsObj.refreshTreeRowVisible(sheet);
  255. break;
  256. case "differ":
  257. tree.expandByCustom(function (x) {
  258. const posterity = tree.getPosterity(x);
  259. if (posterity.length === 0) return x.differ_qty || x.differ_tp;
  260. for (const p of posterity) {
  261. if (x.differ_qty || x.differ_tp) return true;
  262. }
  263. return false;
  264. });
  265. SpreadJsObj.refreshTreeRowVisible(sheet);
  266. }
  267. });
  268. })('a[name=showLevel]', billsSheet);
  269. $.subMenu({
  270. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  271. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  272. key: 'menu.1.0.0',
  273. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  274. callback: function (info) {
  275. if (info.mini) {
  276. $('.panel-title').addClass('fluid');
  277. $('#sub-menu').removeClass('panel-sidebar');
  278. $('.c-body table thead').css('left', '56px');
  279. } else {
  280. $('.panel-title').removeClass('fluid');
  281. $('#sub-menu').addClass('panel-sidebar');
  282. $('.c-body table thead').css('left', '176px');
  283. }
  284. autoFlashHeight();
  285. billsSpread.refresh();
  286. posSpread.refresh();
  287. }
  288. });
  289. });