spss_gather_ledger.js 14 KB

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