stage.js 23 KB

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