stage.js 24 KB

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