schedule_plan.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * 进度台账相关js
  3. *
  4. * @author Ellisran
  5. * @date 2020/11/6
  6. * @version
  7. */
  8. function getTenderId() {
  9. return window.location.pathname.split('/')[2];
  10. }
  11. $(function () {
  12. autoFlashHeight();
  13. if(schedule && !schedule.mode) {
  14. $('#mode').modal('show');
  15. }
  16. // 初始化台账
  17. const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]);
  18. const treeSetting = {
  19. id: 'ledger_id',
  20. pid: 'ledger_pid',
  21. order: 'order',
  22. level: 'level',
  23. rootId: -1,
  24. fullPath: 'full_path',
  25. calcFields: ['total_price']
  26. //treeCacheKey: 'ledger_bills_fold' + '_' + getTenderId(),
  27. // markFoldKey: 'bills-fold',
  28. // markFoldSubKey: window.location.pathname.split('/')[2],
  29. };
  30. treeSetting.calcFun = function (node) {
  31. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  32. };
  33. const ledgerTree = createNewPathTree('base', treeSetting);
  34. const static_cols = [
  35. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 145, formatter: '@', readOnly: true, cellType: 'tree'},
  36. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', readOnly: true},
  37. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  38. {title: '经济指标', colSpan: '1', rowSpan: '2', field: 'dgn_price', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  39. {title: '总设计|工程量', colSpan: '2|1', rowSpan: '1|1', field: 'dgn_qty1', hAlign: 2, width: 70, type: 'Number', readOnly: true},
  40. {title: '|金额(万元)', colSpan: '|1', rowSpan: '|1', field: 'total_price', hAlign: 2, width: 70, type: 'Number', readOnly: true},
  41. ];
  42. const ledgerSpreadSetting = {
  43. emptyRows: 0,
  44. headRows: 2,
  45. headRowHeight: [25, 25],
  46. defaultRowHeight: 21,
  47. headerFont: '12px 微软雅黑',
  48. font: '12px 微软雅黑',
  49. // readOnly: true,
  50. localCache: {
  51. key: 'ledger-bills',
  52. colWidth: true,
  53. }
  54. };
  55. const monthsCols = [];
  56. if(scheduleMonth.length > 0) {
  57. for (const sm of scheduleMonth) {
  58. const yearmonth = sm.yearmonth.split('-')[0] + '年' + parseInt(sm.yearmonth.split('-')[1]) + '月';
  59. const cols = {title: yearmonth + '|计划工程量', colSpan: '2|1', rowSpan: '1|1', field: sm.yearmonth+'_gcl', hAlign: 2, width: 90, type: 'Number', readOnly: 'readOnly.gcl'};
  60. const cols2 = {title: '|计划金额(万元)', colSpan: '|1', rowSpan: '|1', field: sm.yearmonth+'_tp', hAlign: 2, width: 90, type: 'Number', readOnly: 'readOnly.tp'};
  61. monthsCols.push(cols);
  62. monthsCols.push(cols2);
  63. }
  64. }
  65. const spreadHeaderCols = static_cols.concat(monthsCols);
  66. ledgerSpreadSetting.cols = spreadHeaderCols;
  67. const ledgerCol = {
  68. readOnly: {
  69. tp: function (data) {
  70. let flag = data.is_leaf;
  71. if (data.is_leaf) {
  72. flag = schedule && schedule.mode === mode.tp;
  73. }
  74. return !flag;
  75. },
  76. gcl: function (data) {
  77. let flag = data.is_leaf;
  78. if (data.is_leaf) {
  79. flag = schedule && schedule.mode === mode.gcl;
  80. }
  81. return !flag;
  82. },
  83. },
  84. };
  85. sjsSettingObj.setFxTreeStyle(ledgerSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  86. if (thousandth) sjsSettingObj.setTpThousandthFormat(ledgerSpreadSetting);
  87. SpreadJsObj.initSpreadSettingEvents(ledgerSpreadSetting, ledgerCol);
  88. SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
  89. SpreadJsObj.selChangedRefreshBackColor(ledgerSpread.getActiveSheet());
  90. postData('/tender/' + getTenderId() + '/schedule/ledger/load', {}, function (data) {
  91. let treeData = [];
  92. for(const sl of selectedLedgerList) {
  93. const one = _.find(data, { 'ledger_id' : sl });
  94. treeData.push(one);
  95. }
  96. treeData = setLeafData(treeData);
  97. ledgerTree.loadDatas(treeData);
  98. treeCalc.calculateAll(ledgerTree);
  99. console.log(ledgerTree);
  100. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Tree, ledgerTree);
  101. }, null, true);
  102. const ledgerSpreadObj = {
  103. refreshTree: function (sheet, data) {
  104. SpreadJsObj.massOperationSheet(sheet, function () {
  105. const tree = sheet.zh_tree;
  106. // 处理删除
  107. if (data.delete) {
  108. data.delete.sort(function (x, y) {
  109. return y.deleteIndex - x.deleteIndex;
  110. });
  111. for (const d of data.delete) {
  112. sheet.deleteRows(d.deleteIndex, 1);
  113. }
  114. }
  115. // 处理新增
  116. if (data.create) {
  117. const newNodes = data.create;
  118. if (newNodes) {
  119. newNodes.sort(function (a, b) {
  120. return a.index - b.index;
  121. });
  122. for (const node of newNodes) {
  123. sheet.addRows(node.index, 1);
  124. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  125. }
  126. }
  127. }
  128. // 处理更新
  129. if (data.update) {
  130. const rows = [];
  131. for (const u of data.update) {
  132. rows.push(tree.nodes.indexOf(u));
  133. }
  134. SpreadJsObj.reLoadRowsData(sheet, rows);
  135. }
  136. // 处理展开
  137. if (data.expand) {
  138. const expanded = [];
  139. for (const e of data.expand) {
  140. if (expanded.indexOf(e) === -1) {
  141. const posterity = tree.getPosterity(e);
  142. for (const p of posterity) {
  143. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  144. expanded.push(p);
  145. }
  146. }
  147. }
  148. }
  149. });
  150. },
  151. };
  152. // 进度计算方式选择
  153. $('.mode-select').on('click', function () {
  154. const _self = $(this);
  155. postData(window.location.pathname + '/save', {type: 'mode', postData: $(this).data('mode')}, function (result) {
  156. _self.addClass('disabled').attr('disabled', true);
  157. _self.parents('.col-6').siblings('.col-6').find('button').removeClass('disabled').removeAttr('disabled');
  158. $('#mode-tips').show();
  159. $('#mode-cancel').show();
  160. $('#mode').modal('hide');
  161. schedule.mode = _self.data('mode');
  162. SpreadJsObj.reLoadSheetData(ledgerSpread.getActiveSheet());
  163. })
  164. });
  165. // 月份添加
  166. $('#add-month').click(function () {
  167. const range = $('#month-range').val();
  168. if(range === '') {
  169. toastr.error('请选择计划周期时间');
  170. return;
  171. }
  172. const addMonthList = [];
  173. const cycle = range.split(' ~ ');
  174. if(cycle.length === 1) {
  175. addMonthList.push(cycle[0]);
  176. } else {
  177. // 多个月份
  178. const back_year = parseInt(cycle[1].split('-')[0]);
  179. const back_month = parseInt(cycle[1].split('-')[1]);
  180. const front_year = parseInt(cycle[0].split('-')[0]);
  181. const front_month = parseInt(cycle[0].split('-')[1]);
  182. if(back_year > front_year) {
  183. const num = getDistanceMonth(cycle[0], cycle[1]);
  184. let j = 1;
  185. for (let i = 0; i <= num; i++) {
  186. if(front_month + i > 12*j) {
  187. j = j + 1;
  188. }
  189. const m = (front_month + i)%12 === 0 ? 12 : (front_month + i)%12;
  190. addMonthList.push((front_year + (j-1)) + '-' + (m < 10 ? '0' + m : m));
  191. }
  192. } else if (back_year === front_year) {
  193. // 小于1年并没有跨年
  194. for (let i = front_month; i <= back_month; i++) {
  195. addMonthList.push(back_year + '-' + (i < 10 ? '0' + i : i));
  196. }
  197. }
  198. }
  199. // 判断是否已添加本月份
  200. if (addMonthList.length > 0) {
  201. const hadmonth = [];
  202. for (const m of addMonthList) {
  203. const one = _.find(scheduleMonth, { yearmonth: m });
  204. console.log(one, m);
  205. if (one) {
  206. hadmonth.push(m);
  207. }
  208. }
  209. if (hadmonth.length > 0) {
  210. let html = '';
  211. for (const hm of hadmonth) {
  212. html += `<div class="alert alert-danger">${hm} 已创建</div>`;
  213. }
  214. $('#add-month-error-list').html(html);
  215. $('#add-month-error-list').show();
  216. return;
  217. }
  218. } else {
  219. toastr.error('请选择计划周期时间');
  220. return;
  221. }
  222. $('#add-month-error-list').html('');
  223. $('#add-month-error-list').hide();
  224. const _self = $(this);
  225. postData(window.location.pathname + '/save', {type: 'addmonth', postData: addMonthList}, function (result) {
  226. _self.addClass('disabled').attr('disabled', true);
  227. toastr.success('新增成功');
  228. setTimeout(function () {
  229. window.location.reload();
  230. }, 500)
  231. })
  232. });
  233. $('#month-table input[type="checkbox"]').click(function () {
  234. const selectedMonth = [];
  235. $('#month-table input:checkbox:checked').each(function () {
  236. selectedMonth.push('「' + $(this).parents('td').siblings('td').text() + '」');
  237. });
  238. if(selectedMonth.length > 0) {
  239. $('#del-month-list').text(selectedMonth.join(''));
  240. $('#del-month-list').parent().show();
  241. $('#del-month').removeAttr('disabled');
  242. } else {
  243. $('#del-month-list').parent().hide();
  244. $('#del-month').attr('disabled', true);
  245. }
  246. });
  247. $('#del-month').click(function () {
  248. const selectedMonth = [];
  249. $('#month-table input:checkbox:checked').each(function () {
  250. selectedMonth.push($(this).parents('td').siblings().text());
  251. });
  252. if (selectedMonth.length === 0) {
  253. toastr.error('请选择删除的计划周期');
  254. return;
  255. }
  256. const _self = $(this);
  257. postData(window.location.pathname + '/save', {type: 'delmonth', postData: selectedMonth}, function (result) {
  258. _self.addClass('disabled').attr('disabled', true);
  259. toastr.success('删除成功');
  260. setTimeout(function () {
  261. window.location.reload();
  262. }, 500)
  263. })
  264. });
  265. $.subMenu({
  266. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  267. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  268. key: 'menu.1.0.0',
  269. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  270. callback: function (info) {
  271. if (info.mini) {
  272. $('.panel-title').addClass('fluid');
  273. $('#sub-menu').removeClass('panel-sidebar');
  274. } else {
  275. $('.panel-title').removeClass('fluid');
  276. $('#sub-menu').addClass('panel-sidebar');
  277. }
  278. ledgerSpread.refresh();
  279. autoFlashHeight();
  280. }
  281. });
  282. });
  283. // 月份间隔
  284. function getDistanceMonth(startTime,endTime){
  285. startTime = new Date(startTime);
  286. endTime = new Date(endTime);
  287. var dateToMonth = 0;
  288. var startDate=startTime.getDate() + startTime.getHours()/24 + startTime.getMinutes()/24/60;
  289. var endDate=endTime.getDate() +endTime.getHours()/24 + endTime.getMinutes()/24/60;
  290. if(endDate >= startDate){
  291. dateToMonth = 0;
  292. }else{
  293. dateToMonth = -1;
  294. }
  295. let yearToMonth = (endTime.getYear() - startTime.getYear()) * 12;
  296. let monthToMonth = endTime.getMonth() - startTime.getMonth();
  297. return yearToMonth + monthToMonth + dateToMonth;
  298. }
  299. function setLeafData(tree) {
  300. const newtree = [];
  301. for (const t of tree) {
  302. const child = _.find(tree, { 'ledger_pid': t.ledger_id });
  303. if (!child && !t.is_leaf) {
  304. t.is_leaf = true;
  305. }
  306. for(const m of scheduleMonth) {
  307. t[m.yearmonth] = m.sj_gcl !== null || m.sj_tp !== null;
  308. }
  309. newtree.push(t);
  310. }
  311. return newtree;
  312. }