stage.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. /**
  48. * 初始化 树结构 列事件
  49. * @param setting
  50. */
  51. function initTreeColSettingEvents(setting) {
  52. const Events = {
  53. readOnly: {
  54. measureData: function (node) {
  55. return node.children && node.children.length > 0;
  56. },
  57. },
  58. };
  59. const getEvent = function (eventName) {
  60. const names = eventName.split('.');
  61. let event = Events;
  62. for (let name of names) {
  63. if (event[name]) {
  64. event = event[name];
  65. } else {
  66. return null;
  67. }
  68. }
  69. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  70. return null;
  71. } else {
  72. return event;
  73. }
  74. };
  75. for (const col of setting.cols) {
  76. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  77. col.readOnly = getEvent(col.readOnly);
  78. }
  79. }
  80. }
  81. $(document).ready(() => {
  82. // 界面布局
  83. autoFlashHeight();
  84. // 初始化 台账树结构 数据结构
  85. const stageTreeSetting = {
  86. id: 'ledger_id',
  87. pid: 'ledger_pid',
  88. order: 'order',
  89. level: 'level',
  90. rootId: -1,
  91. keys: ['id', 'tender_id', 'ledger_id'],
  92. stageId: 'id',
  93. };
  94. // 台账树结构计算相关设置
  95. stageTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'];
  96. stageTreeSetting.calcFields = ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp',
  97. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp'];
  98. stageTreeSetting.calcFun = function (node) {
  99. if (node.children && node.children.length === 0) {
  100. node.pre_gather_qty = _.add(node.pre_contract_qty, node.pre_qc_qty);
  101. node.gather_qty = _.add(node.contract_qty, node.qc_qty);
  102. node.end_contract_qty = _.add(node.pre_contract_qty, node.contract_qty);
  103. node.end_qc_qty = _.add(node.pre_qc_qty, node.qc_qty);
  104. node.end_gather_qty = _.add(node.pre_gather_qty, node.gather_qty);
  105. }
  106. node.pre_gather_tp = _.add(node.pre_contract_tp, node.pre_qc_tp);
  107. node.gather_tp = _.add(node.contract_tp, node.qc_tp);
  108. node.end_contract_tp = _.add(node.pre_contract_tp, node.contract_tp);
  109. node.end_qc_tp = _.add(node.pre_qc_tp, node.qc_tp);
  110. node.end_gather_tp = _.add(node.pre_gather_tp, node.gather_tp);
  111. if (checkZero(node.dgn_qty1)) {
  112. node.dgn_price = _.round(node.total_price/node.dgn_qty1, 2);
  113. } else {
  114. node.dgn_price = null;
  115. }
  116. };
  117. const stageTree = createNewPathTree('stage', stageTreeSetting);
  118. // 初始化 部位明细 数据结构
  119. const stagePosSetting = {
  120. id: 'id', ledgerId: 'lid',
  121. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  122. };
  123. stagePosSetting.calcFun = function (pos) {
  124. pos.pre_gather_qty = _.add(pos.pre_contract_qty, pos.pre_qc_qty);
  125. pos.gather_qty = _.add(pos.contract_qty, pos.qc_qty);
  126. pos.end_contract_qty = _.add(pos.pre_contract_qty, pos.contract_qty);
  127. pos.end_qc_qty = _.add(pos.pre_qc_qty, pos.qc_qty);
  128. pos.end_gather_qty = _.add(pos.pre_gather_qty, pos.gather_qty);
  129. };
  130. const stagePos = new StagePosData(stagePosSetting);
  131. class Changes {
  132. constructor(obj) {
  133. const self = this;
  134. this.obj = obj;
  135. // 初始化 清单编号窗口 参数
  136. this.spreadSetting = {
  137. cols: [
  138. {title: '已用', field: '', width: 45, formatter: '@', cellType: 'image', readOnly: true, hAlign: 1, indent: 14},
  139. {title: '变更令号', field: 'code', width: 100, formatter: '@', readOnly: true, hAlign: 0, },
  140. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true, hAlign: 0,},
  141. {title: '总数量', field: 'b_amount', width: 60, formatter: '@', readOnly: true, hAlign: 2, },
  142. {title: '可变更数量', field: 'vamount', width: 60, readOnly: true, hAlign: 2, },
  143. {title: '本期计量', field: 'uamount', width: 60, formatter: '@', hAlign: 2, },
  144. ],
  145. emptyRows: 0,
  146. headRows: 1,
  147. headRowHeight: [40],
  148. };
  149. this.spreadSetting.cols[0].img = function (data) {
  150. //return $('#icon-ok')[0];
  151. if (data.uamount && !checkZero(data.uamount)) {
  152. return $('#icon-ok')[0];
  153. } else {
  154. return null;
  155. }
  156. };
  157. this.curChangeId = '';
  158. this.spread = SpreadJsObj.createNewSpread($('#change-spread')[0]);
  159. this.firstView = true;
  160. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  161. // 初次显示,需刷新spread界面,保证界面绘制正确
  162. this.obj.bind('shown.bs.modal', function () {
  163. if (self.firstView) {
  164. self.firstView = false;
  165. self.spread.refresh();
  166. }
  167. });
  168. // 切换变更令,加载右侧明细数据
  169. this.spread.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  170. const change = SpreadJsObj.getSelectObject(info.sheet);
  171. self._loadChangeDetail(change);
  172. });
  173. // 填写本期计量
  174. this.spread.bind(spreadNS.Events.EditEnded, function (e, info) {
  175. if (info.sheet.zh_setting) {
  176. const col = info.sheet.zh_setting.cols[info.col];
  177. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  178. const node = sortData[info.row];
  179. node[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  180. // 刷新已用提示
  181. info.sheet.repaint(info.sheet.getCellRect(info.row, 0));
  182. }
  183. });
  184. this.spread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  185. if (info.sheet.zh_setting) {
  186. const sortData = SpreadJsObj.getSortData(info.sheet);
  187. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  188. const curRow = iRow + info.cellRange.row;
  189. const curCol = info.cellRange.col;
  190. const col = info.sheet.zh_setting.cols[info.cellRange.col];
  191. sortData[curRow][col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  192. }
  193. SpreadJsObj.reLoadRowData(sheet, info.cellRange.row, sel.cellRange.rowCount);
  194. }
  195. });
  196. SpreadJsObj.addDeleteBind(this.spread, function (sheet) {
  197. if (sheet.zh_setting) {
  198. const sel = sheet.getSelections()[0];
  199. const sortData = SpreadJsObj.getSortData(sheet);
  200. // 仅本期计量可删除
  201. if (sel.col === 5 || sel.colCount === 1) {
  202. const col = sheet.zh_setting.cols[sel.col];
  203. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  204. const data = sortData[iRow];
  205. data[col.field] = null;
  206. }
  207. SpreadJsObj.reLoadRowData(sheet, sel.row, sel.rowCount);
  208. }
  209. }
  210. });
  211. // 过滤可变更数量为0
  212. $('#customCheckDisabled').click(function () {
  213. self._filterEmptyChange(!this.checked);
  214. });
  215. $('#show-bgl-detail').bind('click', function () {
  216. const detail = $('#bgl-detail'), bgl=$('#bgl'), obj=$(this);
  217. if (detail.hasClass('col-4')) {
  218. detail.attr('class', 'col').hide();
  219. bgl.attr('class', 'col-12');
  220. $('a', obj).attr('title', '展开侧栏');
  221. $('i', obj).attr('class', 'fa fa-chevron-left');
  222. self.spread.refresh();
  223. } else {
  224. detail.attr('class', 'col-4').show();
  225. bgl.attr('class', 'col-8');
  226. $('a', obj).attr('title', '收起侧栏');
  227. $('i', obj).attr('class', 'fa fa-chevron-right');
  228. self.spread.refresh();
  229. }
  230. });
  231. }
  232. _loadChangeDetail(change) {
  233. if (change) {
  234. if (change.cid === this.curChangeId) { return; }
  235. this.curChangeId = change.cid;
  236. const inputs = $('input[type!=checkbox]', this.obj);
  237. for (const i of inputs) {
  238. const field = $(i).attr('name');
  239. const text = (field && change[field]) ? change[field] : '';
  240. $(i).val(text);
  241. }
  242. const textareas = $('textarea', this.obj);
  243. for (const ta of textareas) {
  244. const field = $(ta).attr('name');
  245. const text = (field && change[field]) ? change[field] : '';
  246. ta.innerText = text;
  247. }
  248. const html = [];
  249. for (const a of change.attachments) {
  250. html.push('<tr>');
  251. html.push('<td>', a.filename + a.fileext, '</td>');
  252. html.push('<td>', a.u_name, '</td>');
  253. html.push('</tr>');
  254. }
  255. // 变更类型
  256. const cType = change.type.split(',');
  257. $('input[name="type"]').prop("checked", false);
  258. for (const c of cType) {
  259. $('input[name="type"][value='+ c +']').prop("checked", true);
  260. }
  261. // 变更类别
  262. $('select[name=class]').val(change.class);
  263. // 变更性质
  264. $('select[name=quality]').val(change.quality);
  265. // 变更单位
  266. $('select[name=company]').html('<option>' + change.company + '</option>');
  267. // 费用承担方
  268. $('input[name=charge][value=' + change.charge + ']').prop('checked', true);
  269. // 附件
  270. $('#attachment').html(html.join(''));
  271. } else {
  272. const inputs = $('input', this.obj);
  273. for (const i of inputs) {
  274. $(i).val('');
  275. }
  276. const textareas = $('textarea', this.obj);
  277. for (const ta of textareas) {
  278. ta.innerText = '';
  279. }
  280. $('#attachment').html('');
  281. }
  282. }
  283. _viewChanges() {
  284. const sheet = this.spread.getActiveSheet();
  285. if (this.changes) {
  286. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, this.changes);
  287. sheet.setSelection(0, 0, 1, 1);
  288. this._loadChangeDetail(this.changes[0]);
  289. this._filterEmptyChange(!$('#customCheckDisabled')[0].checked);
  290. } else {
  291. toast('查询变更令有误,请刷新页面后重试', 'warning');
  292. }
  293. }
  294. _filterEmptyChange(isFilter) {
  295. for (const c of this.changes) {
  296. c.visible = isFilter ? (c.vamount || c.vamount === 0) : true;
  297. }
  298. SpreadJsObj.refreshTreeRowVisible(this.spread.getActiveSheet());
  299. }
  300. loadChanges(data, code) {
  301. this.callData = data;
  302. const self = this;
  303. $('#b-code-hint').text('当前变更清单:' + code);
  304. postData(window.location.pathname + '/valid-change', data, function (result) {
  305. self.changes = result;
  306. self._viewChanges();
  307. self.obj.modal('show');
  308. });
  309. }
  310. }
  311. const changesObj = new Changes($('#use-bg'));
  312. // 初始化 台账 spread
  313. const slSpread = SpreadJsObj.createNewSpread($('#stage-ledger')[0]);
  314. customizeStageTreeSetting(ledgerSpreadSetting, customColDisplay());
  315. // 数量变更列,添加按钮
  316. const col = _.find(ledgerSpreadSetting.cols, {field: 'qc_qty'});
  317. col.readOnly = true;
  318. col.cellType = 'imageBtn';
  319. col.hoverImg = '#ellipsis-icon';
  320. col.indent = 5;
  321. col.showImage = function (data) {
  322. if (!data || (data.children && data.children.length > 0)) {
  323. return false;
  324. } else {
  325. const nodePos = stagePos.getLedgerPos(data.id);
  326. return !(nodePos && nodePos.length > 0);
  327. }
  328. };
  329. ledgerSpreadSetting.imageClick = function (data) {
  330. changesObj.loadChanges({bills: data}, data.b_code);
  331. };
  332. //
  333. SpreadJsObj.initSheet(slSpread.getActiveSheet(), ledgerSpreadSetting);
  334. stageTree.loadDatas(ledgerData);
  335. stageTree.loadCurStageData(curStageData);
  336. stageTree.loadPreStageData(preStageData);
  337. // 根据设置 计算 台账树结构
  338. treeCalc.calculateAll(stageTree);
  339. // 绘制界面
  340. SpreadJsObj.loadSheetData(slSpread.getActiveSheet(), 'tree', stageTree);
  341. // 初始化 部位明细 Spread
  342. const spSpread = SpreadJsObj.createNewSpread($('#stage-pos')[0]);
  343. const spCol = _.find(posSpreadSetting.cols, {field: 'qc_qty'});
  344. spCol.readOnly = true;
  345. spCol.cellType = 'imageBtn';
  346. spCol.hoverImg = '#ellipsis-icon';
  347. spCol.indent = 5;
  348. spCol.showImage = function (data) {
  349. return data;
  350. };
  351. posSpreadSetting.imageClick = function (data) {
  352. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  353. changesObj.loadChanges({pos: data}, node.b_code);
  354. };
  355. SpreadJsObj.initSheet(spSpread.getActiveSheet(), posSpreadSetting);
  356. const stageTreeSpreadObj = {
  357. refreshTreeNodes: function (sheet, nodes) {
  358. const tree = sheet.zh_tree;
  359. if (!tree) { return }
  360. const rows = [];
  361. for (const node of nodes) {
  362. rows.push(tree.nodes.indexOf(node));
  363. }
  364. SpreadJsObj.reLoadRowsData(sheet, rows);
  365. },
  366. editEnded: function (e, info) {
  367. if (info.sheet.zh_setting) {
  368. const col = info.sheet.zh_setting.cols[info.col];
  369. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  370. const node = sortData[info.row];
  371. if (node.children && node.children.length > 0) {
  372. toast('清单父项不可计量', 'error');
  373. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  374. return;
  375. } else {
  376. const nodePos = stagePos.getLedgerPos(node.id);
  377. if (nodePos && nodePos.length > 0) {
  378. toast('该清单有部位明细,请在部位明细处计量', 'error');
  379. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  380. return;
  381. }
  382. }
  383. const billsData = {
  384. lid: node.id
  385. };
  386. billsData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  387. postData(window.location.href + '/update', { bills: billsData }, function (data) {
  388. const nodes = stageTree.loadPostStageData(data.bills);
  389. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  390. });
  391. }
  392. },
  393. selectionChanged: function (e, info) {
  394. stagePosSpreadObj.loadCurPosData();
  395. },
  396. deletePress(sheet) {
  397. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  398. const tree = sheet.zh_tree;
  399. if (!tree) { return; }
  400. const sel = sheet.getSelections()[0];
  401. const validCols = [];
  402. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  403. if (!sheet.zh_setting.cols[iCol].readOnly) {
  404. validCols.push(iCol);
  405. }
  406. }
  407. if (validCols.length === 0) { return; }
  408. const sortData = sheet.zh_tree.nodes;
  409. const datas = [];
  410. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  411. const node = sortData[iRow];
  412. if (node) {
  413. if (node.children && node.children.length > 0) { continue; }
  414. const nodePos = stagePos.getLedgerPos(node.id);
  415. if (nodePos && nodePos.length > 0) { continue; }
  416. const data = { lid: node.id };
  417. for (const iCol of validCols) {
  418. const colSetting = sheet.zh_setting.cols[iCol];
  419. data[colSetting.field] = null;
  420. }
  421. datas.push(data);
  422. }
  423. }
  424. if (datas.length > 0) {
  425. postData(window.location.href + '/update', {bills: datas}, function (result) {
  426. const nodes = stageTree.loadPostStageData(result.bills);
  427. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  428. });
  429. }
  430. }
  431. },
  432. clipboardPasting(e, info) {
  433. if (info.sheet.zh_setting) {
  434. const sortData = info.sheet.zh_data;
  435. const range = info.cellRange;
  436. const validField = ['contract_qty', 'contract_tp', 'qc_qty', 'postil'];
  437. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  438. const col = info.sheet.zh_setting.cols[iCol];
  439. if (validField.indexOf(col.field) === -1) {
  440. toast('不可修改此数据', 'error');
  441. info.cancel = true;
  442. return;
  443. }
  444. }
  445. }
  446. },
  447. clipboardPasted(e, info) {
  448. if (info.sheet.zh_setting && info.sheet.zh_tree) {
  449. const sheet = info.sheet;
  450. const filterNodes = [], datas = [];
  451. console.log(info.cellRange);
  452. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  453. const curRow = iRow + info.cellRange.row;
  454. const node = sheet.zh_tree.getItemsByIndex(curRow);
  455. if (node.children && node.children.length > 0) {
  456. filterNodes.push(node);
  457. continue;
  458. }
  459. const nodePos = stagePos.getLedgerPos(node.id);
  460. if (nodePos && nodePos.length > 0) {
  461. filterNodes.push(node);
  462. continue;
  463. }
  464. const data = {lid: node.id};
  465. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  466. const curCol = info.cellRange.col + iCol;
  467. const col = info.sheet.zh_setting.cols[curCol];
  468. data[col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  469. }
  470. datas.push(data);
  471. }
  472. console.log(datas);
  473. if (datas.length > 0) {
  474. postData(window.location.href + '/update', { bills: datas }, function (data) {
  475. const nodes = stageTree.loadPostStageData(data.bills);
  476. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes.concat(filterNodes));
  477. });
  478. } else {
  479. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), filterNodes);
  480. }
  481. }
  482. }
  483. };
  484. slSpread.bind(spreadNS.Events.EditEnded, stageTreeSpreadObj.editEnded);
  485. slSpread.bind(spreadNS.Events.SelectionChanged, stageTreeSpreadObj.selectionChanged);
  486. slSpread.bind(spreadNS.Events.ClipboardPasting, stageTreeSpreadObj.clipboardPasting);
  487. slSpread.bind(spreadNS.Events.ClipboardPasted, stageTreeSpreadObj.clipboardPasted);
  488. SpreadJsObj.addDeleteBind(slSpread, stageTreeSpreadObj.deletePress);
  489. const stagePosSpreadObj = {
  490. /**
  491. * 加载部位明细 根据当前台账选择节点
  492. */
  493. loadCurPosData: function () {
  494. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  495. if (node) {
  496. const posData = stagePos.ledgerPos[itemsPre + node.id] || [];
  497. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', posData);
  498. } else {
  499. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', []);
  500. }
  501. },
  502. editEnded: function (e, info) {
  503. if (info.sheet.zh_setting) {
  504. // 未改变过,则直接跳过
  505. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  506. const col = info.sheet.zh_setting.cols[info.col];
  507. const orgText = posData ? posData[col.field] : null;
  508. if (orgText === info.editingText || ((!orgText || orgText === '') && (info.editingText === ''))) {
  509. return;
  510. }
  511. // 台账模式下,不可新增
  512. if (checkTzMeasureType() && !posData) {
  513. toast('台账模式不可新增部位明细数据', 'error');
  514. info.cancel = true;
  515. return ;
  516. }
  517. // 不同节点下,部位明细检查输入
  518. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  519. if (!node) {
  520. toast('数据错误, 请刷新页面后再试', 'warning');
  521. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  522. return;
  523. } else if (info.editingText !== '' && node.children && node.children > 0) {
  524. toast('父节点不可插入部位明细', 'error');
  525. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  526. return;
  527. } else if (info.editingText !== '' && !node.b_code || node.b_code === '') {
  528. toast('项目节不可插入部位明细', 'error');
  529. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  530. return;
  531. }
  532. // 生成提交数据
  533. const data = {};
  534. if (col.field === 'name') {
  535. if (info.editingText === '' && pos) {
  536. toast('部位名称不可为空', 'error', 'exclamation-circle');
  537. info.cancel = true;
  538. return;
  539. } else if (!pos) {
  540. if (info.editingText !== '') {
  541. data.updateType = 'add';
  542. data.updateData = {name: info.editingText, lid: node.id, tid: tender.id};
  543. } else {
  544. return;
  545. }
  546. } else {
  547. data.updateType = 'update';
  548. data.updateData = {id: posData.id, name: info.editingText};
  549. }
  550. } else if (!posData) {
  551. toast('新增部位请先输入名称', 'warning');
  552. } else {
  553. data.updateType = 'update';
  554. data.updateData = {pid: posData.id, lid: posData.lid};
  555. data.updateData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  556. }
  557. // 提交数据到服务器
  558. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  559. if (result.pos) {
  560. stagePos.updateDatas(result.pos.pos);
  561. stagePos.loadCurStageData(result.pos.curStageData);
  562. }
  563. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  564. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  565. stagePosSpreadObj.loadCurPosData();
  566. }, function () {
  567. stagePosSpreadObj.loadCurPosData();
  568. });
  569. }
  570. },
  571. clipboardPasting: function (e, info) {
  572. if (info.sheet.zh_setting) {
  573. const sortData = info.sheet.zh_data;
  574. const range = info.cellRange;
  575. const validField = ['contract_qty', 'qc_qty', 'postil'];
  576. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  577. const col = info.sheet.zh_setting.cols[iCol];
  578. if (validField.indexOf(col.field) === -1) {
  579. if (checkTzMeasureType()) {
  580. toast('不可修改此数据', 'error');
  581. info.cancel = true;
  582. return;
  583. } else {
  584. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow) {
  585. const pos = sortData(iRow);
  586. if (pos.add_stage !== stage.id || pos.add_times !== stage.times) {
  587. toast('不可修改此数据', 'error');
  588. info.cancel = true;
  589. return;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. },
  597. clipboardPasted: function (e, info) {
  598. const self = this;
  599. if (info.sheet.zh_setting) {
  600. const data = { updateType: '', updateData: [], };
  601. const sortData = info.sheet.zh_data;
  602. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  603. if (sortData && (info.cellRange.row >= sortData.length)) {
  604. data.updateType = 'add';
  605. if (info.cellRange.col !== 0) {
  606. toast('新增部位请先输入名称', 'warning');
  607. self.loadCurPosData();
  608. return;
  609. }
  610. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  611. const curRow = info.cellRange.row + iRow;
  612. const newData = {lid: node.id};
  613. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  614. const curCol = info.cellRange.col + iCol;
  615. const colSetting = info.sheet.zh_setting.cols[curCol];
  616. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  617. if (colSetting.type === 'Number') {
  618. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  619. }
  620. }
  621. data.updateData.push(newData);
  622. }
  623. } else {
  624. data.updateType = 'update';
  625. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  626. const curRow = info.cellRange.row + iRow;
  627. const curPos = sortData[curRow];
  628. if (curPos) {
  629. const newData = {pid: curPos.id, lid: curPos.lid};
  630. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  631. const curCol = info.cellRange.col + iCol;
  632. const colSetting = info.sheet.zh_setting.cols[curCol];
  633. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  634. if (colSetting.type === 'Number') {
  635. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  636. }
  637. }
  638. data.updateData.push(newData);
  639. }
  640. }
  641. }
  642. console.log(data);
  643. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  644. if (result.pos) {
  645. stagePos.updateDatas(result.pos.pos);
  646. stagePos.loadCurStageData(result.pos.curStageData);
  647. }
  648. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  649. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  650. stagePosSpreadObj.loadCurPosData();
  651. }, function () {
  652. stagePosSpreadObj.loadCurPosData();
  653. });
  654. }
  655. },
  656. deletePress: function (sheet) {
  657. if (sheet.zh_setting && sheet.zh_data) {
  658. const sortData = sheet.zh_data;
  659. if (!sortData || sortData.length === 0) { return; }
  660. const sel = sheet.getSelections()[0];
  661. const validCols = [];
  662. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  663. if (!sheet.zh_setting.cols[iCol].readOnly) {
  664. validCols.push(iCol);
  665. }
  666. }
  667. if (validCols.length === 0) { return; }
  668. const datas = [], posSelects = [];
  669. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  670. const node = sortData[iRow];
  671. if (node) {
  672. const data = {pid: node.id, lid: node.lid};
  673. for (const iCol of validCols) {
  674. const colSetting = sheet.zh_setting.cols[iCol];
  675. if (colSetting.field === 'name') {
  676. toast('部位名称不能为空', 'error');
  677. return;
  678. }
  679. data[colSetting.field] = null;
  680. }
  681. datas.push(data);
  682. posSelects.push(node);
  683. }
  684. }
  685. if (datas.length > 0) {
  686. postData(window.location.pathname + '/update', {pos: {updateType: 'update', updateData: datas} }, function (result) {
  687. if (result.pos) {
  688. stagePos.updateDatas(result.pos.pos);
  689. stagePos.loadCurStageData(result.pos.curStageData);
  690. }
  691. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  692. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  693. // todo 只加载改变项
  694. stagePosSpreadObj.loadCurPosData();
  695. });
  696. }
  697. }
  698. },
  699. };
  700. // 加载上下窗口resizer
  701. $.divResizer({
  702. select: '#main-resize',
  703. callback: function () {
  704. slSpread.refresh();
  705. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  706. $(".sp-wrap").height(bcontent-40);
  707. spSpread.refresh();
  708. }
  709. });
  710. // 加载部位明细数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  711. postData(window.location.pathname + '/pos', null, function (result) {
  712. stagePos.loadDatas(result.pos);
  713. if (result.curStageData) {
  714. stagePos.loadCurStageData(result.curStageData);
  715. }
  716. if (result.preStageData) {
  717. stagePos.loadPreStageData(result.preStageData);
  718. }
  719. stagePos.calculateAll();
  720. stagePosSpreadObj.loadCurPosData();
  721. });
  722. spSpread.bind(spreadNS.Events.EditEnded, stagePosSpreadObj.editEnded);
  723. spSpread.bind(spreadNS.Events.ClipboardPasting, stagePosSpreadObj.clipboardPasting);
  724. spSpread.bind(spreadNS.Events.ClipboardPasted, stagePosSpreadObj.clipboardPasted);
  725. SpreadJsObj.addDeleteBind(spSpread, stagePosSpreadObj.deletePress);
  726. $('#row-view').on('show.bs.modal', function () {
  727. const html = [], customDisplay = customColDisplay();
  728. for (const cd of customDisplay) {
  729. html.push('<tr>');
  730. html.push('<td>', cd.title, '</td>');
  731. html.push('<td>', '<input type="checkbox"' + (cd.visible ? ' checked=""' : '') + '>', '</td>');
  732. html.push('</tr>');
  733. }
  734. $('#row-view-list').html(html.join(''));
  735. });
  736. $('#row-view-ok').click(function () {
  737. const customDisplay = customColDisplay();
  738. const cvl = $('#row-view-list').children();
  739. for (const cv of cvl) {
  740. const title = $(cv).children()[0].innerHTML;
  741. const check = $('input', cv)[0].checked;
  742. const cd = customDisplay.find(function (c) {
  743. return c.title === title;
  744. });
  745. cd.visible = check;
  746. }
  747. customizeStageTreeSetting(ledgerSpreadSetting, customDisplay);
  748. SpreadJsObj.refreshColumnVisible(slSpread.getActiveSheet());
  749. Cookies.set('stage-col-visible', JSON.stringify(customDisplay), 7*24*60*60*1000);
  750. $('#row-view').modal('hide');
  751. });
  752. });