schedule_plan.js 22 KB

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