stage.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. 'use strict';
  2. /**
  3. * 期计量 - 本期计量台账页面 js
  4. *
  5. * @author Mai
  6. * @date 2018/12/7
  7. * @version
  8. */
  9. function checkTzMeasureType () {
  10. return tender.measure_type === measureType.tz.value;
  11. }
  12. /**
  13. * 从cookie中读取缓存的列显示设置,没有则取默认
  14. * @returns {*[]}
  15. */
  16. function customColDisplay () {
  17. const defaultSetting = [
  18. { title: '签约合同', fields: ['deal_qty', 'deal_tp'], visible: true },
  19. { title: '施工图复核', fields: ['quantity', 'total_price'], visible: true },
  20. { title: '本期计量合同', fields: ['contract_qty', 'contract_tp'], visible: true },
  21. { title: '本期数量变更', fields: ['qc_qty', 'qc_tp', 'qc_bgl'], visible: true },
  22. { title: '本期完成计量', fields: ['gather_qty', 'gather_tp'], visible: true },
  23. { title: '截止本期计量合同', fields: ['end_contract_qty', 'end_contract_tp'], visible: true },
  24. { title: '截止本期数量变更', fields: ['end_qc_qty', 'end_qc_tp', 'end_qc_bgl'], visible: true },
  25. { title: '截止本期完成计量', fields: ['end_gather_qty', 'end_gather_tp'], visible: true },
  26. { title: '图册号', fields: ['drawing_code'], visible: true },
  27. { title: '累计完成率(%)', fields: ['percent'], visible: true },
  28. { title: '备注', fields: ['memo'], visible: true },
  29. ];
  30. const settingStr = Cookies.get('stage-col-visible');
  31. return settingStr ? JSON.parse(settingStr) : defaultSetting;
  32. }
  33. /**
  34. * 根据列显示设置,调整setting中的列是否显示
  35. * @param setting
  36. * @param customDisplay
  37. */
  38. function customizeStageTreeSetting(setting, customDisplay) {
  39. for (const cd of customDisplay) {
  40. for (const c of setting.cols) {
  41. if (cd.fields.indexOf(c.field) !== -1) {
  42. c.visible = cd.visible;
  43. }
  44. }
  45. }
  46. }
  47. $(document).ready(() => {
  48. autoFlashHeight();
  49. // 初始化 台账 spread
  50. const slSpread = SpreadJsObj.createNewSpread($('#stage-ledger')[0]);
  51. customizeStageTreeSetting(ledgerSpreadSetting, customColDisplay());
  52. SpreadJsObj.initSheet(slSpread.getActiveSheet(), ledgerSpreadSetting);
  53. const stageTreeSetting = {
  54. id: 'ledger_id',
  55. pid: 'ledger_pid',
  56. order: 'order',
  57. level: 'level',
  58. rootId: -1,
  59. keys: ['id', 'tender_id', 'ledger_id'],
  60. stageId: 'id',
  61. };
  62. // 台账树结构计算相关设置
  63. stageTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'];
  64. stageTreeSetting.calcFields = ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp'];
  65. stageTreeSetting.calcFun = function (node) {
  66. if (node.children && node.children.length === 0) {
  67. node.end_contract_qty = _.toNumber(node.pre_contract_qty) + _.toNumber(node.contract_qty);
  68. node.end_qc_qty = _.toNumber(node.pre_qc_qty) + _.toNumber(node.qc_qty);
  69. node.end_gather_qty = _.toNumber(node.pre_gather_qty) + _.toNumber(node.gather_qty);
  70. }
  71. node.gather_tp = _.toNumber(node.contract_tp) + _.toNumber(node.qc_tp);
  72. node.end_contract_tp = _.toNumber(node.pre_contract_tp) + _.toNumber(node.contract_tp);
  73. node.end_qc_tp = _.toNumber(node.pre_qc_tp) + _.toNumber(node.qc_tp);
  74. node.end_gather_tp = _.toNumber(node.pre_gather_tp) + _.toNumber(node.gather_tp);
  75. if (checkZero(node.dgn_qty1)) {
  76. node.dgn_price = _.round(node.total_price/node.dgn_qty1, 2);
  77. } else {
  78. node.dgn_price = null;
  79. }
  80. };
  81. // 初始化 台账树结构
  82. const stageTree = createNewPathTree('stage', stageTreeSetting);
  83. stageTree.loadDatas(ledgerData);
  84. stageTree.loadCurStageData(curStageData);
  85. // 根据设置 计算 台账树结构
  86. treeCalc.calculateAll(stageTree);
  87. // 绘制界面
  88. SpreadJsObj.loadSheetData(slSpread.getActiveSheet(), 'tree', stageTree);
  89. // 初始化 部位明细
  90. const stagePosSetting = {
  91. id: 'id', masterId: 'lid',
  92. updateFields: ['contract_qty', 'qc_qty'],
  93. };
  94. stagePosSetting.calcFun = function (pos) {
  95. pos.gather_qty = _.toNumber(pos.contract_qty) + _.toNumber(pos.qc_qty);
  96. };
  97. const stagePos = new StagePosData(stagePosSetting);
  98. const spSpread = SpreadJsObj.createNewSpread($('#stage-pos')[0]);
  99. SpreadJsObj.initSheet(spSpread.getActiveSheet(), posSpreadSetting);
  100. const stageTreeSpreadObj = {
  101. refreshTreeNodes: function (sheet, nodes) {
  102. const tree = sheet.zh_tree;
  103. if (!tree) { return }
  104. const rows = [];
  105. for (const node of nodes) {
  106. rows.push(tree.nodes.indexOf(node));
  107. }
  108. SpreadJsObj.reLoadRowsData(sheet, rows);
  109. },
  110. editEnding: function (e, info) {
  111. if (info.sheet.zh_setting) {
  112. const col = info.sheet.zh_setting.cols[info.col];
  113. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  114. const node = sortData[info.row];
  115. if (node.children && node.children.length > 0) {
  116. toast('清单父项不可计量', 'error');
  117. info.cancel = true;
  118. return;
  119. } else {
  120. const nodePos = stagePos.getMasterRange(node.id);
  121. if (nodePos && nodePos.length > 0) {
  122. toast('该清单有部位明细,请在部位明细处计量', 'error');
  123. info.cancel = true;
  124. return;
  125. }
  126. }
  127. const billsData = {
  128. lid: node.id
  129. };
  130. billsData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  131. postData(window.location.href + '/update', { bills: billsData }, function (data) {
  132. const nodes = stageTree.loadPostStageData(data.bills);
  133. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  134. });
  135. }
  136. },
  137. selectionChanged: function (e, info) {
  138. stagePosSpreadObj.loadCurPosData();
  139. },
  140. deletePress(sheet) {
  141. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  142. const tree = sheet.zh_tree;
  143. if (!tree) { return; }
  144. const sel = sheet.getSelections()[0];
  145. const validCols = [];
  146. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  147. if (!sheet.zh_setting.cols[iCol].readOnly) {
  148. validCols.push(iCol);
  149. }
  150. }
  151. if (validCols.length === 0) { return; }
  152. const sortData = sheet.zh_tree.nodes;
  153. const datas = [];
  154. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  155. const node = sortData[iRow];
  156. if (node) {
  157. if (node.children && node.children.length > 0) { continue; }
  158. const nodePos = stagePos.getMasterRange(node.id);
  159. if (nodePos && nodePos.length > 0) { continue; }
  160. const data = { lid: node.id };
  161. for (const iCol of validCols) {
  162. const colSetting = sheet.zh_setting.cols[iCol];
  163. data[colSetting.field] = null;
  164. }
  165. datas.push(data);
  166. }
  167. }
  168. if (datas.length > 0) {
  169. postData(window.location.href + '/update', {bills: datas}, function (result) {
  170. const nodes = stageTree.loadPostStageData(result.bills);
  171. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  172. });
  173. }
  174. }
  175. },
  176. clipboardPasting(e, info) {
  177. if (info.sheet.zh_setting) {
  178. const sortData = info.sheet.zh_data;
  179. const range = info.cellRange;
  180. const validField = ['contract_qty', 'contract_tp', 'qc_qty', 'postil'];
  181. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  182. const col = info.sheet.zh_setting.cols[iCol];
  183. if (validField.indexOf(col.field) === -1) {
  184. toast('不可修改此数据', 'error');
  185. info.cancel = true;
  186. return;
  187. }
  188. }
  189. }
  190. },
  191. clipboardPasted(e, info) {
  192. if (info.sheet.zh_setting && info.sheet.zh_tree) {
  193. const sheet = info.sheet;
  194. const filterNodes = [], datas = [];
  195. console.log(info.cellRange);
  196. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  197. const curRow = iRow + info.cellRange.row;
  198. const node = sheet.zh_tree.getItemsByIndex(curRow);
  199. if (node.children && node.children.length > 0) {
  200. filterNodes.push(node);
  201. continue;
  202. }
  203. const nodePos = stagePos.getMasterRange(node.id);
  204. if (nodePos && nodePos.length > 0) {
  205. filterNodes.push(node);
  206. continue;
  207. }
  208. const data = {lid: node.id};
  209. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  210. const curCol = info.cellRange.col + iCol;
  211. const col = info.sheet.zh_setting.cols[curCol];
  212. data[col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  213. }
  214. datas.push(data);
  215. }
  216. console.log(datas);
  217. if (datas.length > 0) {
  218. postData(window.location.href + '/update', { bills: datas }, function (data) {
  219. const nodes = stageTree.loadPostStageData(data.bills);
  220. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes.concat(filterNodes));
  221. });
  222. } else {
  223. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), filterNodes);
  224. }
  225. }
  226. }
  227. };
  228. slSpread.bind(spreadNS.Events.EditEnding, stageTreeSpreadObj.editEnding);
  229. slSpread.bind(spreadNS.Events.SelectionChanged, stageTreeSpreadObj.selectionChanged);
  230. slSpread.bind(spreadNS.Events.ClipboardPasting, stageTreeSpreadObj.clipboardPasting);
  231. slSpread.bind(spreadNS.Events.ClipboardPasted, stageTreeSpreadObj.clipboardPasted);
  232. SpreadJsObj.addDeleteBind(slSpread, stageTreeSpreadObj.deletePress);
  233. const stagePosSpreadObj = {
  234. /**
  235. * 加载部位明细 根据当前台账选择节点
  236. */
  237. loadCurPosData: function () {
  238. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  239. if (node) {
  240. const posData = stagePos.masterRange[itemsPre + node.id] || [];
  241. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', posData);
  242. } else {
  243. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', []);
  244. }
  245. },
  246. editEnding: function(e, info) {
  247. if (info.sheet.zh_setting) {
  248. // 未改变过,则直接跳过
  249. const orgText = info.sheet.getCell(info.row, info.row).value();
  250. if (orgText === info.editingText || ((!orgText || orgText === '') && (info.editingText === ''))) {
  251. info.cancel = true;
  252. return;
  253. }
  254. // 台账模式下,不可新增
  255. const pos = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  256. if (checkTzMeasureType() && !pos) {
  257. toast('台账模式不可新增部位明细数据', 'error');
  258. info.cancel = true;
  259. return ;
  260. }
  261. // 不同节点下,部位明细检查输入
  262. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  263. if (!node) {
  264. toast('数据错误, 请刷新页面后再试', 'warning');
  265. info.cancel = true;
  266. return;
  267. } else if (info.editingText !== '' && node.children && node.children > 0) {
  268. toast('父节点不可插入部位明细', 'error');
  269. info.cancel = true;
  270. return;
  271. } else if (info.editingText !== '' && node.code || node.code !== '') {
  272. toast('项目节不可插入部位明细', 'error');
  273. info.cancel = true;
  274. return;
  275. }
  276. const col = info.sheet.zh_setting.cols[info.col];
  277. const data = {};
  278. if (col.field === 'name') {
  279. if (info.editingText === '' && pos) {
  280. toast('部位名称不可为空', 'error', 'exclamation-circle');
  281. info.cancel = true;
  282. return;
  283. } else if (!pos) {
  284. if (info.editingText !== '') {
  285. data.updateType = 'add';
  286. data.updateData = {name: info.editingText, lid: node.id, tid: tender.id};
  287. } else {
  288. return;
  289. }
  290. } else {
  291. data.updateType = 'update';
  292. data.updateData = {id: pos.id, name: info.editingText};
  293. }
  294. } else if (!pos) {
  295. toast('新增部位请先输入名称', 'warning');
  296. } else {
  297. data.updateType = 'update';
  298. data.updateData = {pid: pos.id, lid: pos.lid};
  299. data.updateData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  300. }
  301. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  302. if (result.pos) {
  303. stagePos.updateDatas(result.pos.pos);
  304. stagePos.loadCurStageData(result.pos.curStageData);
  305. }
  306. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  307. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  308. stagePosSpreadObj.loadCurPosData();
  309. }, function () {
  310. stagePosSpreadObj.loadCurPosData();
  311. });
  312. }
  313. },
  314. clipboardPasting: function (e, info) {
  315. if (info.sheet.zh_setting) {
  316. const sortData = info.sheet.zh_data;
  317. const range = info.cellRange;
  318. const validField = ['contract_qty', 'qc_qty', 'postil'];
  319. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  320. const col = info.sheet.zh_setting.cols[iCol];
  321. if (validField.indexOf(col.field) === -1) {
  322. if (checkTzMeasureType()) {
  323. toast('不可修改此数据', 'error');
  324. info.cancel = true;
  325. return;
  326. } else {
  327. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow) {
  328. const pos = sortData(iRow);
  329. if (pos.add_stage !== stage.id || pos.add_times !== stage.times) {
  330. toast('不可修改此数据', 'error');
  331. info.cancel = true;
  332. return;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. },
  340. clipboardPasted: function (e, info) {
  341. const self = this;
  342. if (info.sheet.zh_setting) {
  343. const data = { updateType: '', updateData: [], };
  344. const sortData = info.sheet.zh_data;
  345. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  346. if (sortData && (info.cellRange.row >= sortData.length)) {
  347. data.updateType = 'add';
  348. if (info.cellRange.col !== 0) {
  349. toast('新增部位请先输入名称', 'warning');
  350. self.loadCurPosData();
  351. return;
  352. }
  353. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  354. const curRow = info.cellRange.row + iRow;
  355. const newData = {lid: node.id};
  356. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  357. const curCol = info.cellRange.col + iCol;
  358. const colSetting = info.sheet.zh_setting.cols[curCol];
  359. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  360. if (colSetting.type === 'Number') {
  361. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  362. }
  363. }
  364. data.updateData.push(newData);
  365. }
  366. } else {
  367. data.updateType = 'update';
  368. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  369. const curRow = info.cellRange.row + iRow;
  370. const curPos = sortData[curRow];
  371. if (curPos) {
  372. const newData = {pid: curPos.id, lid: curPos.lid};
  373. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  374. const curCol = info.cellRange.col + iCol;
  375. const colSetting = info.sheet.zh_setting.cols[curCol];
  376. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  377. if (colSetting.type === 'Number') {
  378. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  379. }
  380. }
  381. data.updateData.push(newData);
  382. }
  383. }
  384. }
  385. console.log(data);
  386. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  387. if (result.pos) {
  388. stagePos.updateDatas(result.pos.pos);
  389. stagePos.loadCurStageData(result.pos.curStageData);
  390. }
  391. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  392. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  393. stagePosSpreadObj.loadCurPosData();
  394. }, function () {
  395. stagePosSpreadObj.loadCurPosData();
  396. });
  397. }
  398. },
  399. deletePress: function (sheet) {
  400. if (sheet.zh_setting && sheet.zh_data) {
  401. const sortData = sheet.zh_data;
  402. if (!sortData || sortData.length === 0) { return; }
  403. const sel = sheet.getSelections()[0];
  404. const validCols = [];
  405. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  406. if (!sheet.zh_setting.cols[iCol].readOnly) {
  407. validCols.push(iCol);
  408. }
  409. }
  410. if (validCols.length === 0) { return; }
  411. const datas = [], posSelects = [];
  412. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  413. const node = sortData[iRow];
  414. if (node) {
  415. const data = {pid: node.id, lid: node.lid};
  416. for (const iCol of validCols) {
  417. const colSetting = sheet.zh_setting.cols[iCol];
  418. if (colSetting.field === 'name') {
  419. toast('部位名称不能为空', 'error');
  420. return;
  421. }
  422. data[colSetting.field] = null;
  423. }
  424. datas.push(data);
  425. posSelects.push(node);
  426. }
  427. }
  428. if (datas.length > 0) {
  429. postData(window.location.pathname + '/update', {pos: {updateType: 'update', updateData: datas} }, function (result) {
  430. if (result.pos) {
  431. stagePos.updateDatas(result.pos.pos);
  432. stagePos.loadCurStageData(result.pos.curStageData);
  433. }
  434. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  435. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  436. // todo 只加载改变项
  437. stagePosSpreadObj.loadCurPosData();
  438. });
  439. }
  440. }
  441. },
  442. };
  443. // 加载部位明细数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  444. postData(window.location.pathname + '/pos', null, function (result) {
  445. stagePos.loadDatas(result.pos);
  446. if (result.curStageData) {
  447. stagePos.loadCurStageData(result.curStageData);
  448. }
  449. if (result.preStageData) {
  450. stagePos.loadPreStageData(result.preStageData);
  451. }
  452. stagePosSpreadObj.loadCurPosData();
  453. });
  454. spSpread.bind(spreadNS.Events.EditEnding, stagePosSpreadObj.editEnding);
  455. spSpread.bind(spreadNS.Events.ClipboardPasting, stagePosSpreadObj.clipboardPasting);
  456. spSpread.bind(spreadNS.Events.ClipboardPasted, stagePosSpreadObj.clipboardPasted);
  457. SpreadJsObj.addDeleteBind(spSpread, stagePosSpreadObj.deletePress);
  458. $('#row-view').on('show.bs.modal', function () {
  459. const html = [], customDisplay = customColDisplay();
  460. for (const cd of customDisplay) {
  461. html.push('<tr>');
  462. html.push('<td>', cd.title, '</td>');
  463. html.push('<td>', '<input type="checkbox"' + (cd.visible ? ' checked=""' : '') + '>', '</td>');
  464. html.push('</tr>');
  465. }
  466. $('#row-view-list').html(html.join(''));
  467. });
  468. $('#row-view-ok').click(function () {
  469. const customDisplay = customColDisplay();
  470. const cvl = $('#row-view-list').children();
  471. for (const cv of cvl) {
  472. const title = $(cv).children()[0].innerHTML;
  473. const check = $('input', cv)[0].checked;
  474. const cd = customDisplay.find(function (c) {
  475. return c.title === title;
  476. });
  477. cd.visible = check;
  478. }
  479. customizeStageTreeSetting(ledgerSpreadSetting, customDisplay);
  480. SpreadJsObj.refreshColumnVisible(slSpread.getActiveSheet());
  481. Cookies.set('stage-col-visible', JSON.stringify(customDisplay), 7*24*60*60*1000);
  482. $('#row-view').modal('hide');
  483. });
  484. });