schedule_plan.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. //treeCacheKey: 'ledger_bills_fold' + '_' + getTenderId(),
  26. // markFoldKey: 'bills-fold',
  27. // markFoldSubKey: window.location.pathname.split('/')[2],
  28. };
  29. const ledgerTree = createNewPathTree('filter', treeSetting);
  30. const static_cols = [
  31. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 145, formatter: '@', readOnly: true, cellType: 'tree'},
  32. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', readOnly: true},
  33. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  34. {title: '经济指标', colSpan: '1', rowSpan: '2', field: 'dgn_price', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  35. {title: '总设计|工程量', colSpan: '2|1', rowSpan: '1|1', field: 'dgn_qty1', hAlign: 2, width: 70, type: 'Number', readOnly: true},
  36. {title: '|金额(万元)', colSpan: '|1', rowSpan: '|1', field: 'total_price', hAlign: 2, width: 70, type: 'Number', readOnly: true},
  37. ];
  38. const ledgerSpreadSetting = {
  39. emptyRows: 0,
  40. headRows: 2,
  41. headRowHeight: [25, 25],
  42. defaultRowHeight: 21,
  43. headerFont: '12px 微软雅黑',
  44. font: '12px 微软雅黑',
  45. // readOnly: true,
  46. localCache: {
  47. key: 'ledger-bills',
  48. colWidth: true,
  49. }
  50. };
  51. const monthsCols = [];
  52. if(scheduleMonth.length > 0) {
  53. for (const sm of scheduleMonth) {
  54. const readOnly = sm.stage_used !== 0;
  55. const yearmonth = sm.yearmonth.split('-')[0] + '年' + parseInt(sm.yearmonth.split('-')[1]) + '月';
  56. const cols = {title: yearmonth + '|计划工程量', colSpan: '2|1', rowSpan: '1|1', field: sm.yearmonth+'_gcl', hAlign: 2, width: 90, type: 'Number', readOnly: readOnly ? readOnly : 'readOnly.gcl'};
  57. const cols2 = {title: '|计划金额(万元)', colSpan: '|1', rowSpan: '|1', field: sm.yearmonth+'_tp', hAlign: 2, width: 90, type: 'Number', readOnly: readOnly ? readOnly : 'readOnly.tp'};
  58. monthsCols.push(cols);
  59. monthsCols.push(cols2);
  60. }
  61. }
  62. const spreadHeaderCols = static_cols.concat(monthsCols);
  63. ledgerSpreadSetting.cols = spreadHeaderCols;
  64. const ledgerCol = {
  65. readOnly: {
  66. tp: function (data) {
  67. let flag = data.is_leaf;
  68. if (data.is_leaf) {
  69. flag = schedule && schedule.mode === mode.tp;
  70. }
  71. return !flag;
  72. },
  73. gcl: function (data) {
  74. let flag = data.is_leaf;
  75. if (data.is_leaf) {
  76. flag = schedule && schedule.mode === mode.gcl;
  77. }
  78. return !flag;
  79. },
  80. },
  81. };
  82. sjsSettingObj.setFxTreeStyle(ledgerSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  83. if (thousandth) sjsSettingObj.setTpThousandthFormat(ledgerSpreadSetting);
  84. SpreadJsObj.initSpreadSettingEvents(ledgerSpreadSetting, ledgerCol);
  85. SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
  86. SpreadJsObj.selChangedRefreshBackColor(ledgerSpread.getActiveSheet());
  87. postData('/tender/' + getTenderId() + '/schedule/ledger/load', {}, function (data) {
  88. // let treeData = [];
  89. // for(const sl of selectedLedgerList) {
  90. // const one = _.find(data, { 'ledger_id' : sl });
  91. // treeData.push(one);
  92. // }
  93. // treeData = setLeafData(treeData);
  94. // console.log(treeData);
  95. // let treeData = data;
  96. const calcList = ['total_price'];
  97. const showList = ['ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path',
  98. 'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'dgn_price', 'quantity', 'total_price'];
  99. for (const m of monthList) {
  100. showList.push(m + '_tp');
  101. showList.push(m + '_gcl');
  102. // calcList.push(m + '_tp');
  103. // calcList.push(m + '_gcl');
  104. }
  105. const baseLedgerTree = createNewPathTree('base', {
  106. id: 'ledger_id',
  107. pid: 'ledger_pid',
  108. order: 'order',
  109. level: 'level',
  110. rootId: -1,
  111. fullPath: 'full_path',
  112. calcFields: calcList,
  113. calcFun: function (node) {
  114. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  115. }
  116. });
  117. const newLedgerList = setMonthToLedger(data.bills, data.slm);
  118. baseLedgerTree.loadDatas(newLedgerList);
  119. treeCalc.calculateAll(baseLedgerTree);
  120. console.log(baseLedgerTree);
  121. for (const d of baseLedgerTree.nodes) {
  122. if (!d.b_code) {
  123. const one = _.find(selectedLedgerList, function (item) {
  124. return item === d.ledger_id;
  125. });
  126. if(one) {
  127. ledgerTree.addData(d, showList);
  128. }
  129. }
  130. }
  131. console.log(ledgerTree);
  132. ledgerTree.sortTreeNode(true);
  133. // console.log(ledgerTree);
  134. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Tree, ledgerTree);
  135. }, null, true);
  136. const ledgerSpreadObj = {
  137. refreshTree: function (sheet, data) {
  138. SpreadJsObj.massOperationSheet(sheet, function () {
  139. const tree = sheet.zh_tree;
  140. // 处理删除
  141. if (data.delete) {
  142. data.delete.sort(function (x, y) {
  143. return y.deleteIndex - x.deleteIndex;
  144. });
  145. for (const d of data.delete) {
  146. sheet.deleteRows(d.deleteIndex, 1);
  147. }
  148. }
  149. // 处理新增
  150. if (data.create) {
  151. const newNodes = data.create;
  152. if (newNodes) {
  153. newNodes.sort(function (a, b) {
  154. return a.index - b.index;
  155. });
  156. for (const node of newNodes) {
  157. sheet.addRows(node.index, 1);
  158. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  159. }
  160. }
  161. }
  162. // 处理更新
  163. if (data.update) {
  164. const rows = [];
  165. for (const u of data.update) {
  166. rows.push(tree.nodes.indexOf(u));
  167. }
  168. SpreadJsObj.reLoadRowsData(sheet, rows);
  169. }
  170. // 处理展开
  171. if (data.expand) {
  172. const expanded = [];
  173. for (const e of data.expand) {
  174. if (expanded.indexOf(e) === -1) {
  175. const posterity = tree.getPosterity(e);
  176. for (const p of posterity) {
  177. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  178. expanded.push(p);
  179. }
  180. }
  181. }
  182. }
  183. });
  184. },
  185. editEnded: function (e, info) {
  186. if (info.sheet.zh_setting) {
  187. const select = SpreadJsObj.getSelectObject(info.sheet);
  188. const col = info.sheet.zh_setting.cols[info.col];
  189. const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  190. const orgValue = select[col.field];
  191. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  192. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  193. return;
  194. }
  195. if (isNaN(validText)) {
  196. toastr.error('不能输入其它非数字类型字符');
  197. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  198. return;
  199. }
  200. const yearmonth = col.field.split('_')[0];
  201. const mode = col.field.split('_')[1];
  202. let plan_gcl = 0;
  203. let plan_tp = 0;
  204. // 判断输入位数,提示
  205. if (mode === 'tp') {
  206. const reg = new RegExp('^([-]?)\\d+(\\.\\d{0,'+ parseInt(tenderInfo.decimal.tp) +'})?$');
  207. if (validText !== null && (!reg.test(validText))) {
  208. toastr.error('输入金额小数位数不能大于' + tenderInfo.decimal.tp + '位');
  209. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  210. return;
  211. }
  212. plan_gcl = select.dgn_price && select.dgn_price !== 0 ? ZhCalc.round(ZhCalc.div(validText, select.dgn_price), tenderInfo.decimal.up) : 0;
  213. plan_tp = validText;
  214. } else {
  215. const reg = new RegExp('^([-]?)\\d+(\\.\\d{0,'+ parseInt(tenderInfo.decimal.up) +'})?$');
  216. if (validText !== null && (!reg.test(validText))) {
  217. toastr.error('输入工程量小数位数不能大于' + tenderInfo.decimal.up + '位');
  218. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  219. return;
  220. }
  221. plan_gcl = validText;
  222. plan_tp = select.dgn_price && select.dgn_price !== 0 ? ZhCalc.round(ZhCalc.mul(validText, select.dgn_price), tenderInfo.decimal.tp) : 0;
  223. }
  224. select[col.field] = validText;
  225. const updateData = {
  226. lid: select.ledger_id,
  227. yearmonth,
  228. plan_gcl,
  229. plan_tp,
  230. };
  231. console.log(updateData);
  232. postData(window.location.pathname + '/save', {type: 'ledger_edit', postData: updateData}, function (result) {
  233. if (mode === 'tp') {
  234. select[yearmonth + '_gcl'] = plan_gcl;
  235. } else {
  236. select[yearmonth + '_tp'] = plan_tp;
  237. }
  238. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  239. },function () {
  240. select[col.field] = orgValue;
  241. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  242. })
  243. }
  244. },
  245. deletePress: function (sheet) {
  246. return;
  247. },
  248. clipboardPasted(e, info) {
  249. const hint = {
  250. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  251. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  252. };
  253. const range = info.cellRange;
  254. if (range.rowCount > 1 || range.colCount > 1) {
  255. toastMessageUniq(hint.cellError);
  256. SpreadJsObj.reLoadSheetHeader(ledgerSpread.getActiveSheet());
  257. SpreadJsObj.reLoadSheetData(ledgerSpread.getActiveSheet());
  258. return;
  259. }
  260. const select = SpreadJsObj.getSelectObject(info.sheet);
  261. const col = info.sheet.zh_setting.cols[range.col];
  262. const validText = is_numeric(info.pasteData.text) ? parseFloat(info.pasteData.text) : (info.pasteData.text ? trimInvalidChar(info.pasteData.text) : null);
  263. const orgValue = select[col.field];
  264. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  265. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  266. return;
  267. }
  268. if (isNaN(validText)) {
  269. toastr.error('不能粘贴其它非数字类型字符');
  270. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  271. return;
  272. }
  273. const yearmonth = col.field.split('_')[0];
  274. const mode = col.field.split('_')[1];
  275. let plan_gcl = 0;
  276. let plan_tp = 0;
  277. // 判断输入位数,提示
  278. if (mode === 'tp') {
  279. const reg = new RegExp('^([-]?)\\d+(\\.\\d{0,'+ parseInt(tenderInfo.decimal.tp) +'})?$');
  280. if (validText !== null && (!reg.test(validText))) {
  281. toastr.error('粘贴的金额小数位数不能大于' + tenderInfo.decimal.tp + '位');
  282. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  283. return;
  284. }
  285. plan_gcl = select.dgn_price && select.dgn_price !== 0 ? ZhCalc.round(ZhCalc.div(validText, select.dgn_price), tenderInfo.decimal.up) : 0;
  286. plan_tp = validText;
  287. } else {
  288. const reg = new RegExp('^([-]?)\\d+(\\.\\d{0,'+ parseInt(tenderInfo.decimal.up) +'})?$');
  289. if (validText !== null && (!reg.test(validText))) {
  290. toastr.error('粘贴的工程量小数位数不能大于' + tenderInfo.decimal.up + '位');
  291. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  292. return;
  293. }
  294. plan_gcl = validText;
  295. plan_tp = select.dgn_price && select.dgn_price !== 0 ? ZhCalc.round(ZhCalc.mul(validText, select.dgn_price), tenderInfo.decimal.tp) : 0;
  296. }
  297. select[col.field] = validText;
  298. const updateData = {
  299. lid: select.ledger_id,
  300. yearmonth,
  301. plan_gcl,
  302. plan_tp,
  303. };
  304. postData(window.location.pathname + '/save', {type: 'ledger_edit', postData: updateData}, function (result) {
  305. if (mode === 'tp') {
  306. select[yearmonth + '_gcl'] = plan_gcl;
  307. } else {
  308. select[yearmonth + '_tp'] = plan_tp;
  309. }
  310. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  311. },function () {
  312. select[col.field] = orgValue;
  313. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  314. })
  315. },
  316. };
  317. ledgerSpread.bind(spreadNS.Events.EditEnded, ledgerSpreadObj.editEnded);
  318. SpreadJsObj.addDeleteBind(ledgerSpread, ledgerSpreadObj.deletePress);
  319. ledgerSpread.bind(spreadNS.Events.ClipboardPasted, ledgerSpreadObj.clipboardPasted);
  320. // 进度计算方式选择
  321. $('.mode-select').on('click', function () {
  322. const _self = $(this);
  323. postData(window.location.pathname + '/save', {type: 'mode', postData: $(this).data('mode')}, function (result) {
  324. _self.addClass('disabled').attr('disabled', true);
  325. _self.parents('.col-6').siblings('.col-6').find('button').removeClass('disabled').removeAttr('disabled');
  326. $('#mode-tips').show();
  327. $('#mode-cancel').show();
  328. $('#mode').modal('hide');
  329. schedule.mode = _self.data('mode');
  330. SpreadJsObj.reLoadSheetData(ledgerSpread.getActiveSheet());
  331. })
  332. });
  333. // 月份添加
  334. $('#add-month').click(function () {
  335. const range = $('#month-range').val();
  336. if(range === '') {
  337. toastr.error('请选择计划周期时间');
  338. return;
  339. }
  340. const addMonthList = [];
  341. const cycle = range.split(' ~ ');
  342. if(cycle.length === 1) {
  343. addMonthList.push(cycle[0]);
  344. } else {
  345. // 多个月份
  346. const back_year = parseInt(cycle[1].split('-')[0]);
  347. const back_month = parseInt(cycle[1].split('-')[1]);
  348. const front_year = parseInt(cycle[0].split('-')[0]);
  349. const front_month = parseInt(cycle[0].split('-')[1]);
  350. if(back_year > front_year) {
  351. const num = getDistanceMonth(cycle[0], cycle[1]);
  352. let j = 1;
  353. for (let i = 0; i <= num; i++) {
  354. if(front_month + i > 12*j) {
  355. j = j + 1;
  356. }
  357. const m = (front_month + i)%12 === 0 ? 12 : (front_month + i)%12;
  358. addMonthList.push((front_year + (j-1)) + '-' + (m < 10 ? '0' + m : m));
  359. }
  360. } else if (back_year === front_year) {
  361. // 小于1年并没有跨年
  362. for (let i = front_month; i <= back_month; i++) {
  363. addMonthList.push(back_year + '-' + (i < 10 ? '0' + i : i));
  364. }
  365. }
  366. }
  367. // 判断是否已添加本月份
  368. if (addMonthList.length > 0) {
  369. const hadmonth = [];
  370. for (const m of addMonthList) {
  371. const one = _.find(scheduleMonth, { yearmonth: m });
  372. console.log(one, m);
  373. if (one) {
  374. hadmonth.push(m);
  375. }
  376. }
  377. if (hadmonth.length > 0) {
  378. let html = '';
  379. for (const hm of hadmonth) {
  380. html += `<div class="alert alert-danger">${hm} 已创建</div>`;
  381. }
  382. $('#add-month-error-list').html(html);
  383. $('#add-month-error-list').show();
  384. return;
  385. }
  386. } else {
  387. toastr.error('请选择计划周期时间');
  388. return;
  389. }
  390. $('#add-month-error-list').html('');
  391. $('#add-month-error-list').hide();
  392. const _self = $(this);
  393. postData(window.location.pathname + '/save', {type: 'addmonth', postData: addMonthList}, function (result) {
  394. _self.addClass('disabled').attr('disabled', true);
  395. toastr.success('新增成功');
  396. setTimeout(function () {
  397. window.location.reload();
  398. }, 500)
  399. })
  400. });
  401. $('#month-table input[type="checkbox"]').click(function () {
  402. const selectedMonth = [];
  403. $('#month-table input:checkbox:checked').each(function () {
  404. selectedMonth.push('「' + $(this).parents('td').siblings('td').text() + '」');
  405. });
  406. if(selectedMonth.length > 0) {
  407. $('#del-month-list').text(selectedMonth.join(''));
  408. $('#del-month-list').parent().show();
  409. $('#del-month').removeAttr('disabled');
  410. } else {
  411. $('#del-month-list').parent().hide();
  412. $('#del-month').attr('disabled', true);
  413. }
  414. });
  415. $('#del-month').click(function () {
  416. const selectedMonth = [];
  417. $('#month-table input:checkbox:checked').each(function () {
  418. selectedMonth.push($(this).parents('td').siblings().text());
  419. });
  420. if (selectedMonth.length === 0) {
  421. toastr.error('请选择删除的计划周期');
  422. return;
  423. }
  424. const _self = $(this);
  425. postData(window.location.pathname + '/save', {type: 'delmonth', postData: selectedMonth}, function (result) {
  426. _self.addClass('disabled').attr('disabled', true);
  427. toastr.success('删除成功');
  428. setTimeout(function () {
  429. window.location.reload();
  430. }, 500)
  431. })
  432. });
  433. $.subMenu({
  434. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  435. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  436. key: 'menu.1.0.0',
  437. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  438. callback: function (info) {
  439. if (info.mini) {
  440. $('.panel-title').addClass('fluid');
  441. $('#sub-menu').removeClass('panel-sidebar');
  442. } else {
  443. $('.panel-title').removeClass('fluid');
  444. $('#sub-menu').addClass('panel-sidebar');
  445. }
  446. ledgerSpread.refresh();
  447. autoFlashHeight();
  448. }
  449. });
  450. });
  451. // 月份间隔
  452. function getDistanceMonth(startTime,endTime){
  453. startTime = new Date(startTime);
  454. endTime = new Date(endTime);
  455. var dateToMonth = 0;
  456. var startDate=startTime.getDate() + startTime.getHours()/24 + startTime.getMinutes()/24/60;
  457. var endDate=endTime.getDate() +endTime.getHours()/24 + endTime.getMinutes()/24/60;
  458. if(endDate >= startDate){
  459. dateToMonth = 0;
  460. }else{
  461. dateToMonth = -1;
  462. }
  463. let yearToMonth = (endTime.getYear() - startTime.getYear()) * 12;
  464. let monthToMonth = endTime.getMonth() - startTime.getMonth();
  465. return yearToMonth + monthToMonth + dateToMonth;
  466. }
  467. function setLeafData(tree) {
  468. const newtree = [];
  469. for (const t of tree) {
  470. const child = _.find(tree, { 'ledger_pid': t.ledger_id });
  471. if (!child && !t.is_leaf) {
  472. t.is_leaf = true;
  473. }
  474. newtree.push(t);
  475. }
  476. return newtree;
  477. }
  478. function setMonthToLedger(ledgerList, slm) {
  479. if (slm.length > 0) {
  480. for(const s of slm) {
  481. const index = _.findIndex(ledgerList, { 'ledger_id': s.lid });
  482. if (index && index !== -1) {
  483. ledgerList[index][s.yearmonth + '_tp'] = s.plan_tp;
  484. ledgerList[index][s.yearmonth + '_gcl'] = s.plan_gcl;
  485. }
  486. }
  487. }
  488. return ledgerList;
  489. }
  490. const is_numeric = (value) => {
  491. if (typeof(value) === 'object') {
  492. return false;
  493. } else {
  494. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  495. }
  496. };