schedule_plan.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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.sj_gcl !== null || sm.sj_tp !== null;
  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. numberCan: {type: 'error', msg: '请粘贴大于0并且小于3位小数的浮点数'},
  253. };
  254. const range = info.cellRange;
  255. const sortData = info.sheet.zh_data || [];
  256. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  257. toastMessageUniq(hint.cellError);
  258. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  259. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  260. return;
  261. }
  262. if (sortData.length > 0 && range.col + range.colCount > 4 + months.length) {
  263. toastMessageUniq(hint.cellError);
  264. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  265. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  266. return;
  267. }
  268. const data = [];
  269. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  270. let bPaste = true;
  271. const curRow = range.row + iRow;
  272. const materialMonthData = sortData[curRow];
  273. const hintRow = range.rowCount > 1 ? curRow : '';
  274. let sameCol = 0;
  275. for (let iCol = 0; iCol < range.colCount; iCol++) {
  276. const curCol = range.col + iCol;
  277. const colSetting = info.sheet.zh_setting.cols[curCol];
  278. if (!colSetting) continue;
  279. let validText = info.sheet.getText(curRow, curCol);
  280. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  281. const orgValue = sortData[curRow][colSetting.field];
  282. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  283. sameCol++;
  284. if (range.colCount === sameCol) {
  285. bPaste = false;
  286. }
  287. continue;
  288. }
  289. const num = parseFloat(validText);
  290. if (isNaN(validText)) {
  291. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  292. bPaste = false;
  293. continue;
  294. }
  295. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  296. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  297. bPaste = false;
  298. continue;
  299. }
  300. materialMonthData[colSetting.field] = validText;
  301. sortData[curRow][colSetting.field] = validText;
  302. }
  303. if (bPaste) {
  304. data.push(materialMonthData);
  305. } else {
  306. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  307. }
  308. }
  309. if (data.length === 0) {
  310. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  311. return;
  312. }
  313. // // 更新至服务器
  314. // postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  315. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  316. // materialBillsData = result.materialBillsData;
  317. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  318. // m_tp = result.m_tp;
  319. // resetTpTable();
  320. // }, function () {
  321. // SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  322. // return;
  323. // });
  324. },
  325. };
  326. ledgerSpread.bind(spreadNS.Events.EditEnded, ledgerSpreadObj.editEnded);
  327. SpreadJsObj.addDeleteBind(ledgerSpread, ledgerSpreadObj.deletePress);
  328. // 进度计算方式选择
  329. $('.mode-select').on('click', function () {
  330. const _self = $(this);
  331. postData(window.location.pathname + '/save', {type: 'mode', postData: $(this).data('mode')}, function (result) {
  332. _self.addClass('disabled').attr('disabled', true);
  333. _self.parents('.col-6').siblings('.col-6').find('button').removeClass('disabled').removeAttr('disabled');
  334. $('#mode-tips').show();
  335. $('#mode-cancel').show();
  336. $('#mode').modal('hide');
  337. schedule.mode = _self.data('mode');
  338. SpreadJsObj.reLoadSheetData(ledgerSpread.getActiveSheet());
  339. })
  340. });
  341. // 月份添加
  342. $('#add-month').click(function () {
  343. const range = $('#month-range').val();
  344. if(range === '') {
  345. toastr.error('请选择计划周期时间');
  346. return;
  347. }
  348. const addMonthList = [];
  349. const cycle = range.split(' ~ ');
  350. if(cycle.length === 1) {
  351. addMonthList.push(cycle[0]);
  352. } else {
  353. // 多个月份
  354. const back_year = parseInt(cycle[1].split('-')[0]);
  355. const back_month = parseInt(cycle[1].split('-')[1]);
  356. const front_year = parseInt(cycle[0].split('-')[0]);
  357. const front_month = parseInt(cycle[0].split('-')[1]);
  358. if(back_year > front_year) {
  359. const num = getDistanceMonth(cycle[0], cycle[1]);
  360. let j = 1;
  361. for (let i = 0; i <= num; i++) {
  362. if(front_month + i > 12*j) {
  363. j = j + 1;
  364. }
  365. const m = (front_month + i)%12 === 0 ? 12 : (front_month + i)%12;
  366. addMonthList.push((front_year + (j-1)) + '-' + (m < 10 ? '0' + m : m));
  367. }
  368. } else if (back_year === front_year) {
  369. // 小于1年并没有跨年
  370. for (let i = front_month; i <= back_month; i++) {
  371. addMonthList.push(back_year + '-' + (i < 10 ? '0' + i : i));
  372. }
  373. }
  374. }
  375. // 判断是否已添加本月份
  376. if (addMonthList.length > 0) {
  377. const hadmonth = [];
  378. for (const m of addMonthList) {
  379. const one = _.find(scheduleMonth, { yearmonth: m });
  380. console.log(one, m);
  381. if (one) {
  382. hadmonth.push(m);
  383. }
  384. }
  385. if (hadmonth.length > 0) {
  386. let html = '';
  387. for (const hm of hadmonth) {
  388. html += `<div class="alert alert-danger">${hm} 已创建</div>`;
  389. }
  390. $('#add-month-error-list').html(html);
  391. $('#add-month-error-list').show();
  392. return;
  393. }
  394. } else {
  395. toastr.error('请选择计划周期时间');
  396. return;
  397. }
  398. $('#add-month-error-list').html('');
  399. $('#add-month-error-list').hide();
  400. const _self = $(this);
  401. postData(window.location.pathname + '/save', {type: 'addmonth', postData: addMonthList}, function (result) {
  402. _self.addClass('disabled').attr('disabled', true);
  403. toastr.success('新增成功');
  404. setTimeout(function () {
  405. window.location.reload();
  406. }, 500)
  407. })
  408. });
  409. $('#month-table input[type="checkbox"]').click(function () {
  410. const selectedMonth = [];
  411. $('#month-table input:checkbox:checked').each(function () {
  412. selectedMonth.push('「' + $(this).parents('td').siblings('td').text() + '」');
  413. });
  414. if(selectedMonth.length > 0) {
  415. $('#del-month-list').text(selectedMonth.join(''));
  416. $('#del-month-list').parent().show();
  417. $('#del-month').removeAttr('disabled');
  418. } else {
  419. $('#del-month-list').parent().hide();
  420. $('#del-month').attr('disabled', true);
  421. }
  422. });
  423. $('#del-month').click(function () {
  424. const selectedMonth = [];
  425. $('#month-table input:checkbox:checked').each(function () {
  426. selectedMonth.push($(this).parents('td').siblings().text());
  427. });
  428. if (selectedMonth.length === 0) {
  429. toastr.error('请选择删除的计划周期');
  430. return;
  431. }
  432. const _self = $(this);
  433. postData(window.location.pathname + '/save', {type: 'delmonth', postData: selectedMonth}, function (result) {
  434. _self.addClass('disabled').attr('disabled', true);
  435. toastr.success('删除成功');
  436. setTimeout(function () {
  437. window.location.reload();
  438. }, 500)
  439. })
  440. });
  441. $.subMenu({
  442. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  443. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  444. key: 'menu.1.0.0',
  445. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  446. callback: function (info) {
  447. if (info.mini) {
  448. $('.panel-title').addClass('fluid');
  449. $('#sub-menu').removeClass('panel-sidebar');
  450. } else {
  451. $('.panel-title').removeClass('fluid');
  452. $('#sub-menu').addClass('panel-sidebar');
  453. }
  454. ledgerSpread.refresh();
  455. autoFlashHeight();
  456. }
  457. });
  458. });
  459. // 月份间隔
  460. function getDistanceMonth(startTime,endTime){
  461. startTime = new Date(startTime);
  462. endTime = new Date(endTime);
  463. var dateToMonth = 0;
  464. var startDate=startTime.getDate() + startTime.getHours()/24 + startTime.getMinutes()/24/60;
  465. var endDate=endTime.getDate() +endTime.getHours()/24 + endTime.getMinutes()/24/60;
  466. if(endDate >= startDate){
  467. dateToMonth = 0;
  468. }else{
  469. dateToMonth = -1;
  470. }
  471. let yearToMonth = (endTime.getYear() - startTime.getYear()) * 12;
  472. let monthToMonth = endTime.getMonth() - startTime.getMonth();
  473. return yearToMonth + monthToMonth + dateToMonth;
  474. }
  475. function setLeafData(tree) {
  476. const newtree = [];
  477. for (const t of tree) {
  478. const child = _.find(tree, { 'ledger_pid': t.ledger_id });
  479. if (!child && !t.is_leaf) {
  480. t.is_leaf = true;
  481. }
  482. newtree.push(t);
  483. }
  484. return newtree;
  485. }
  486. function setMonthToLedger(ledgerList, slm) {
  487. if (slm.length > 0) {
  488. for(const s of slm) {
  489. const index = _.findIndex(ledgerList, { 'ledger_id': s.lid });
  490. if (index && index !== -1) {
  491. ledgerList[index][s.yearmonth + '_tp'] = s.plan_tp;
  492. ledgerList[index][s.yearmonth + '_gcl'] = s.plan_gcl;
  493. }
  494. }
  495. }
  496. return ledgerList;
  497. }
  498. const is_numeric = (value) => {
  499. if (typeof(value) === 'object') {
  500. return false;
  501. } else {
  502. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  503. }
  504. };