stage.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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, img: function (data) {
  139. if (data.uamount && !checkZero(data.uamount)) {
  140. return $('#icon-ok')[0];
  141. } else {
  142. return null;
  143. }
  144. }},
  145. {title: '变更令号', field: 'code', width: 100, formatter: '@', readOnly: true, hAlign: 0, },
  146. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true, hAlign: 0,},
  147. {title: '总数量', field: 'b_amount', width: 60, formatter: '@', readOnly: true, hAlign: 2, },
  148. {title: '可变更数量', field: 'vamount', width: 60, readOnly: true, hAlign: 2, },
  149. {title: '本期计量', field: 'uamount', width: 60, formatter: '@', hAlign: 2, type: 'Number', },
  150. ],
  151. emptyRows: 0,
  152. headRows: 1,
  153. headRowHeight: [40],
  154. getColor: function (data, col, defaultColor) {
  155. if (col.field === 'uamount') {
  156. if (!data.vamount) {
  157. return data.uamount ? '#ff6f5c' : defaultColor;
  158. } else if (data.uamount) {
  159. return data.uamount > data.vamount ? '#ff6f5c' : defaultColor;
  160. } else {
  161. return defaultColor;
  162. }
  163. } else {
  164. return defaultColor;
  165. }
  166. }
  167. };
  168. this.curChangeId = '';
  169. this.spread = SpreadJsObj.createNewSpread($('#change-spread')[0]);
  170. this.firstView = true;
  171. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  172. // 初次显示,需刷新spread界面,保证界面绘制正确
  173. this.obj.bind('shown.bs.modal', function () {
  174. if (self.firstView) {
  175. self.firstView = false;
  176. self.spread.refresh();
  177. }
  178. });
  179. // 切换变更令,加载右侧明细数据
  180. this.spread.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  181. const change = SpreadJsObj.getSelectObject(info.sheet);
  182. self._loadChangeDetail(change);
  183. });
  184. // 填写本期计量
  185. this.spread.bind(spreadNS.Events.EditEnded, function (e, info) {
  186. if (info.sheet.zh_setting) {
  187. const col = info.sheet.zh_setting.cols[info.col];
  188. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  189. const node = sortData[info.row];
  190. node[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  191. // 刷新界面
  192. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  193. }
  194. });
  195. this.spread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  196. if (info.sheet.zh_setting) {
  197. const sortData = SpreadJsObj.getSortData(info.sheet);
  198. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  199. const curRow = iRow + info.cellRange.row;
  200. const curCol = info.cellRange.col;
  201. const col = info.sheet.zh_setting.cols[info.cellRange.col];
  202. sortData[curRow][col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  203. }
  204. SpreadJsObj.reLoadRowData(sheet, info.cellRange.row, sel.cellRange.rowCount);
  205. }
  206. });
  207. SpreadJsObj.addDeleteBind(this.spread, function (sheet) {
  208. if (sheet.zh_setting) {
  209. const sel = sheet.getSelections()[0];
  210. const sortData = SpreadJsObj.getSortData(sheet);
  211. // 仅本期计量可删除
  212. if (sel.col === 5 || sel.colCount === 1) {
  213. const col = sheet.zh_setting.cols[sel.col];
  214. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  215. const data = sortData[iRow];
  216. data[col.field] = null;
  217. }
  218. SpreadJsObj.reLoadRowData(sheet, sel.row, sel.rowCount);
  219. }
  220. }
  221. });
  222. // 过滤可变更数量为0
  223. $('#customCheckDisabled').click(function () {
  224. self._filterEmptyChange(!this.checked);
  225. });
  226. // 展开收起 变更令详细信息
  227. $('#show-bgl-detail').bind('click', function () {
  228. const detail = $('#bgl-detail'), bgl=$('#bgl'), obj=$(this);
  229. if (detail.hasClass('col-4')) {
  230. detail.attr('class', 'col').hide();
  231. bgl.attr('class', 'col-12');
  232. $('a', obj).attr('title', '展开侧栏');
  233. $('i', obj).attr('class', 'fa fa-chevron-left');
  234. self.spread.refresh();
  235. } else {
  236. detail.attr('class', 'col-4').show();
  237. bgl.attr('class', 'col-8');
  238. $('a', obj).attr('title', '收起侧栏');
  239. $('i', obj).attr('class', 'fa fa-chevron-right');
  240. self.spread.refresh();
  241. }
  242. });
  243. // 添加调用变更令
  244. $('#usg-bg-ok').click(function () {
  245. const data = { target: self.callData, change: [] };
  246. for (const c of self.changes) {
  247. if (c.uamount) {
  248. if (!c.vamount || checkZero(c.vamount)) {
  249. toast('变更令:' + c.code + ' 当前不可使用', 'error');
  250. return;
  251. } else {
  252. if (c.uamount > c.vamount) {
  253. toast('变更令:' + c.code + ' 超计,请修改本期计量后,再提交', 'error');
  254. return;
  255. }
  256. }
  257. data.change.push({ cid: c.cid, cbid: c.cbid, qty: c.uamount });
  258. }
  259. }
  260. // 提交数据到后端
  261. postData(window.location.pathname + '/use-change', data, function(result) {
  262. if (result.pos) {
  263. stagePos.loadCurStageData(result.pos);
  264. }
  265. const nodes = stageTree.loadPostStageData(result.bills);
  266. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  267. stagePosSpreadObj.loadCurPosData();
  268. self.obj.modal('hide');
  269. });
  270. })
  271. }
  272. _calculateAmount() {
  273. for (const c of this.changes) {
  274. c.bamount = _.toNumber(c.b_amount);
  275. c.vamount = _.round(_.subtract(c.bamount, c.used_amount), 6);
  276. const uc = _.find(this.useChanges, {cid: c.cid, cbid: c.cbid});
  277. if (uc) {
  278. c.uamount = uc.qty;
  279. c.vamount = _.round(_.add(c.vamount, c.uamount), 6);
  280. }
  281. }
  282. }
  283. _loadChangeDetail(change) {
  284. if (change) {
  285. if (change.cid === this.curChangeId) { return; }
  286. this.curChangeId = change.cid;
  287. const inputs = $('input[type!=checkbox]', this.obj);
  288. for (const i of inputs) {
  289. const field = $(i).attr('name');
  290. const text = (field && change[field]) ? change[field] : '';
  291. $(i).val(text);
  292. }
  293. const textareas = $('textarea', this.obj);
  294. for (const ta of textareas) {
  295. const field = $(ta).attr('name');
  296. const text = (field && change[field]) ? change[field] : '';
  297. ta.innerText = text;
  298. }
  299. const html = [];
  300. for (const a of change.attachments) {
  301. html.push('<tr>');
  302. html.push('<td>', '<a href="/change/download/file/' + a.id + '">', a.filename + a.fileext, '</a>', '</td>');
  303. html.push('<td>', a.u_name, '</td>');
  304. html.push('</tr>');
  305. }
  306. // 变更类型
  307. if (change.type) {
  308. const cType = change.type.split(',');
  309. $('input[name="type"]').prop("checked", false);
  310. for (const c of cType) {
  311. $('input[name="type"][value='+ c +']').prop("checked", true);
  312. }
  313. }
  314. // 变更类别
  315. $('select[name=class]').val(change.class);
  316. // 变更性质
  317. $('select[name=quality]').val(change.quality);
  318. // 变更单位
  319. $('select[name=company]').html('<option>' + change.company + '</option>');
  320. // 费用承担方
  321. $('input[name=charge][value=' + change.charge + ']').prop('checked', true);
  322. // 附件
  323. $('#attachment').html(html.join(''));
  324. } else {
  325. const inputs = $('input', this.obj);
  326. for (const i of inputs) {
  327. $(i).val('');
  328. }
  329. const textareas = $('textarea', this.obj);
  330. for (const ta of textareas) {
  331. ta.innerText = '';
  332. }
  333. $('#attachment').html('');
  334. }
  335. }
  336. _viewChanges() {
  337. const sheet = this.spread.getActiveSheet();
  338. if (this.changes) {
  339. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, this.changes);
  340. sheet.setSelection(0, 0, 1, 1);
  341. this._loadChangeDetail(this.changes[0]);
  342. this._filterEmptyChange(!$('#customCheckDisabled')[0].checked);
  343. } else {
  344. toast('查询变更令有误,请刷新页面后重试', 'warning');
  345. }
  346. }
  347. _filterEmptyChange(isFilter) {
  348. for (const c of this.changes) {
  349. c.visible = isFilter ? (c.vamount && !checkZero(c.vamount)) : true;
  350. }
  351. SpreadJsObj.refreshTreeRowVisible(this.spread.getActiveSheet());
  352. }
  353. loadChanges(data, code) {
  354. this.callData = data;
  355. const self = this;
  356. $('#b-code-hint').text('当前变更清单:' + code);
  357. postData(window.location.pathname + '/valid-change', data, function (result) {
  358. self.changes = result.changes;
  359. self.useChanges = result.useChanges;
  360. self._calculateAmount();
  361. self._viewChanges();
  362. self.obj.modal('show');
  363. });
  364. }
  365. }
  366. const changesObj = new Changes($('#use-bg'));
  367. // 初始化 台账 spread
  368. const slSpread = SpreadJsObj.createNewSpread($('#stage-ledger')[0]);
  369. customizeStageTreeSetting(ledgerSpreadSetting, customColDisplay());
  370. // 数量变更列,添加按钮
  371. const col = _.find(ledgerSpreadSetting.cols, {field: 'qc_qty'});
  372. col.readOnly = true;
  373. col.cellType = 'imageBtn';
  374. col.hoverImg = '#ellipsis-icon';
  375. col.indent = 5;
  376. col.showImage = function (data) {
  377. if (!data || (data.children && data.children.length > 0)) {
  378. return false;
  379. } else {
  380. const nodePos = stagePos.getLedgerPos(data.id);
  381. return !(nodePos && nodePos.length > 0);
  382. }
  383. };
  384. ledgerSpreadSetting.imageClick = function (data) {
  385. changesObj.loadChanges({bills: data}, data.b_code);
  386. };
  387. //
  388. SpreadJsObj.initSheet(slSpread.getActiveSheet(), ledgerSpreadSetting);
  389. stageTree.loadDatas(ledgerData);
  390. stageTree.loadCurStageData(curStageData);
  391. stageTree.loadPreStageData(preStageData);
  392. // 根据设置 计算 台账树结构
  393. treeCalc.calculateAll(stageTree);
  394. // 绘制界面
  395. SpreadJsObj.loadSheetData(slSpread.getActiveSheet(), 'tree', stageTree);
  396. // 初始化 部位明细 Spread
  397. const spSpread = SpreadJsObj.createNewSpread($('#stage-pos')[0]);
  398. const spCol = _.find(posSpreadSetting.cols, {field: 'qc_qty'});
  399. spCol.readOnly = true;
  400. spCol.cellType = 'imageBtn';
  401. spCol.hoverImg = '#ellipsis-icon';
  402. spCol.indent = 5;
  403. spCol.showImage = function (data) {
  404. return data;
  405. };
  406. posSpreadSetting.imageClick = function (data) {
  407. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  408. changesObj.loadChanges({pos: data}, node.b_code);
  409. };
  410. SpreadJsObj.initSheet(spSpread.getActiveSheet(), posSpreadSetting);
  411. const stageTreeSpreadObj = {
  412. refreshTreeNodes: function (sheet, nodes) {
  413. const tree = sheet.zh_tree;
  414. if (!tree) { return }
  415. const rows = [];
  416. for (const node of nodes) {
  417. rows.push(tree.nodes.indexOf(node));
  418. }
  419. SpreadJsObj.reLoadRowsData(sheet, rows);
  420. },
  421. editEnded: function (e, info) {
  422. if (info.sheet.zh_setting) {
  423. const col = info.sheet.zh_setting.cols[info.col];
  424. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  425. const node = sortData[info.row];
  426. if (node.children && node.children.length > 0) {
  427. toast('清单父项不可计量', 'error');
  428. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  429. return;
  430. } else {
  431. const nodePos = stagePos.getLedgerPos(node.id);
  432. if (nodePos && nodePos.length > 0) {
  433. toast('该清单有部位明细,请在部位明细处计量', 'error');
  434. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  435. return;
  436. }
  437. }
  438. const billsData = {
  439. lid: node.id
  440. };
  441. billsData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  442. postData(window.location.href + '/update', { bills: billsData }, function (data) {
  443. const nodes = stageTree.loadPostStageData(data.bills);
  444. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  445. });
  446. }
  447. },
  448. selectionChanged: function (e, info) {
  449. stagePosSpreadObj.loadCurPosData();
  450. },
  451. deletePress(sheet) {
  452. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  453. const tree = sheet.zh_tree;
  454. if (!tree) { return; }
  455. const sel = sheet.getSelections()[0];
  456. const validCols = [];
  457. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  458. if (!sheet.zh_setting.cols[iCol].readOnly) {
  459. validCols.push(iCol);
  460. }
  461. }
  462. if (validCols.length === 0) { return; }
  463. const sortData = sheet.zh_tree.nodes;
  464. const datas = [];
  465. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  466. const node = sortData[iRow];
  467. if (node) {
  468. if (node.children && node.children.length > 0) { continue; }
  469. const nodePos = stagePos.getLedgerPos(node.id);
  470. if (nodePos && nodePos.length > 0) { continue; }
  471. const data = { lid: node.id };
  472. for (const iCol of validCols) {
  473. const colSetting = sheet.zh_setting.cols[iCol];
  474. data[colSetting.field] = null;
  475. }
  476. datas.push(data);
  477. }
  478. }
  479. if (datas.length > 0) {
  480. postData(window.location.href + '/update', {bills: datas}, function (result) {
  481. const nodes = stageTree.loadPostStageData(result.bills);
  482. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  483. });
  484. }
  485. }
  486. },
  487. clipboardPasting(e, info) {
  488. if (info.sheet.zh_setting) {
  489. const sortData = info.sheet.zh_data;
  490. const range = info.cellRange;
  491. const validField = ['contract_qty', 'contract_tp', 'qc_qty', 'postil'];
  492. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  493. const col = info.sheet.zh_setting.cols[iCol];
  494. if (validField.indexOf(col.field) === -1) {
  495. toast('不可修改此数据', 'error');
  496. info.cancel = true;
  497. return;
  498. }
  499. }
  500. }
  501. },
  502. clipboardPasted(e, info) {
  503. if (info.sheet.zh_setting && info.sheet.zh_tree) {
  504. const sheet = info.sheet;
  505. const filterNodes = [], datas = [];
  506. console.log(info.cellRange);
  507. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  508. const curRow = iRow + info.cellRange.row;
  509. const node = sheet.zh_tree.getItemsByIndex(curRow);
  510. if (node.children && node.children.length > 0) {
  511. filterNodes.push(node);
  512. continue;
  513. }
  514. const nodePos = stagePos.getLedgerPos(node.id);
  515. if (nodePos && nodePos.length > 0) {
  516. filterNodes.push(node);
  517. continue;
  518. }
  519. const data = {lid: node.id};
  520. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  521. const curCol = info.cellRange.col + iCol;
  522. const col = info.sheet.zh_setting.cols[curCol];
  523. data[col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  524. }
  525. datas.push(data);
  526. }
  527. console.log(datas);
  528. if (datas.length > 0) {
  529. postData(window.location.href + '/update', { bills: datas }, function (data) {
  530. const nodes = stageTree.loadPostStageData(data.bills);
  531. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes.concat(filterNodes));
  532. });
  533. } else {
  534. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), filterNodes);
  535. }
  536. }
  537. }
  538. };
  539. slSpread.bind(spreadNS.Events.EditEnded, stageTreeSpreadObj.editEnded);
  540. slSpread.bind(spreadNS.Events.SelectionChanged, stageTreeSpreadObj.selectionChanged);
  541. slSpread.bind(spreadNS.Events.ClipboardPasting, stageTreeSpreadObj.clipboardPasting);
  542. slSpread.bind(spreadNS.Events.ClipboardPasted, stageTreeSpreadObj.clipboardPasted);
  543. SpreadJsObj.addDeleteBind(slSpread, stageTreeSpreadObj.deletePress);
  544. const stagePosSpreadObj = {
  545. /**
  546. * 加载部位明细 根据当前台账选择节点
  547. */
  548. loadCurPosData: function () {
  549. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  550. if (node) {
  551. const posData = stagePos.ledgerPos[itemsPre + node.id] || [];
  552. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', posData);
  553. } else {
  554. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', []);
  555. }
  556. },
  557. editEnded: function (e, info) {
  558. if (info.sheet.zh_setting) {
  559. // 未改变过,则直接跳过
  560. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  561. const col = info.sheet.zh_setting.cols[info.col];
  562. const orgText = posData ? posData[col.field] : null;
  563. if (orgText === info.editingText || ((!orgText || orgText === '') && (info.editingText === ''))) {
  564. return;
  565. }
  566. // 台账模式下,不可新增
  567. if (checkTzMeasureType() && !posData) {
  568. toast('台账模式不可新增部位明细数据', 'error');
  569. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  570. info.cancel = true;
  571. return ;
  572. }
  573. // 不同节点下,部位明细检查输入
  574. //const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  575. const node = stagePosSpreadObj.stageTreeNode;
  576. if (!node) {
  577. toast('数据错误, 请刷新页面后再试', 'warning');
  578. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  579. return;
  580. } else if (info.editingText !== '' && node.children && node.children > 0) {
  581. toast('父节点不可插入部位明细', 'error');
  582. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  583. return;
  584. } else if (info.editingText !== '' && !node.b_code || node.b_code === '') {
  585. toast('项目节不可插入部位明细', 'error');
  586. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  587. return;
  588. }
  589. // 生成提交数据
  590. const data = {};
  591. if (col.field === 'name') {
  592. if (info.editingText === '' && pos) {
  593. toast('部位名称不可为空', 'error', 'exclamation-circle');
  594. info.cancel = true;
  595. return;
  596. } else if (!pos) {
  597. if (info.editingText !== '') {
  598. data.updateType = 'add';
  599. data.updateData = {name: info.editingText, lid: node.id, tid: tender.id};
  600. } else {
  601. return;
  602. }
  603. } else {
  604. data.updateType = 'update';
  605. data.updateData = {id: posData.id, name: info.editingText};
  606. }
  607. } else if (!posData) {
  608. toast('新增部位请先输入名称', 'warning');
  609. } else {
  610. data.updateType = 'update';
  611. data.updateData = {pid: posData.id, lid: posData.lid};
  612. data.updateData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  613. }
  614. // 提交数据到服务器
  615. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  616. if (result.pos) {
  617. stagePos.updateDatas(result.pos.pos);
  618. stagePos.loadCurStageData(result.pos.curStageData);
  619. }
  620. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  621. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  622. stagePosSpreadObj.loadCurPosData();
  623. }, function () {
  624. stagePosSpreadObj.loadCurPosData();
  625. });
  626. }
  627. },
  628. editStarting: function (e, info) {
  629. stagePosSpreadObj.stageTreeNode = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  630. },
  631. clipboardPasting: function (e, info) {
  632. if (info.sheet.zh_setting) {
  633. const sortData = info.sheet.zh_data;
  634. const range = info.cellRange;
  635. const validField = ['contract_qty', 'qc_qty', 'postil'];
  636. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  637. const col = info.sheet.zh_setting.cols[iCol];
  638. if (validField.indexOf(col.field) === -1) {
  639. if (checkTzMeasureType()) {
  640. toast('不可修改此数据', 'error');
  641. info.cancel = true;
  642. return;
  643. } else {
  644. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow) {
  645. const pos = sortData(iRow);
  646. if (pos.add_stage !== stage.id || pos.add_times !== stage.times) {
  647. toast('不可修改此数据', 'error');
  648. info.cancel = true;
  649. return;
  650. }
  651. }
  652. }
  653. }
  654. }
  655. }
  656. },
  657. clipboardPasted: function (e, info) {
  658. const self = this;
  659. if (info.sheet.zh_setting) {
  660. const data = { updateType: '', updateData: [], };
  661. const sortData = info.sheet.zh_data;
  662. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  663. if (sortData && (info.cellRange.row >= sortData.length)) {
  664. data.updateType = 'add';
  665. if (info.cellRange.col !== 0) {
  666. toast('新增部位请先输入名称', 'warning');
  667. self.loadCurPosData();
  668. return;
  669. }
  670. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  671. const curRow = info.cellRange.row + iRow;
  672. const newData = {lid: node.id};
  673. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  674. const curCol = info.cellRange.col + iCol;
  675. const colSetting = info.sheet.zh_setting.cols[curCol];
  676. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  677. if (colSetting.type === 'Number') {
  678. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  679. }
  680. }
  681. data.updateData.push(newData);
  682. }
  683. } else {
  684. data.updateType = 'update';
  685. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  686. const curRow = info.cellRange.row + iRow;
  687. const curPos = sortData[curRow];
  688. if (curPos) {
  689. const newData = {pid: curPos.id, lid: curPos.lid};
  690. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  691. const curCol = info.cellRange.col + iCol;
  692. const colSetting = info.sheet.zh_setting.cols[curCol];
  693. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  694. if (colSetting.type === 'Number') {
  695. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  696. }
  697. }
  698. data.updateData.push(newData);
  699. }
  700. }
  701. }
  702. console.log(data);
  703. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  704. if (result.pos) {
  705. stagePos.updateDatas(result.pos.pos);
  706. stagePos.loadCurStageData(result.pos.curStageData);
  707. }
  708. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  709. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  710. stagePosSpreadObj.loadCurPosData();
  711. }, function () {
  712. stagePosSpreadObj.loadCurPosData();
  713. });
  714. }
  715. },
  716. deletePress: function (sheet) {
  717. if (sheet.zh_setting && sheet.zh_data) {
  718. const sortData = sheet.zh_data;
  719. if (!sortData || sortData.length === 0) { return; }
  720. const sel = sheet.getSelections()[0];
  721. const validCols = [];
  722. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  723. if (!sheet.zh_setting.cols[iCol].readOnly) {
  724. validCols.push(iCol);
  725. }
  726. }
  727. if (validCols.length === 0) { return; }
  728. const datas = [], posSelects = [];
  729. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  730. const node = sortData[iRow];
  731. if (node) {
  732. const data = {pid: node.id, lid: node.lid};
  733. for (const iCol of validCols) {
  734. const colSetting = sheet.zh_setting.cols[iCol];
  735. if (colSetting.field === 'name') {
  736. toast('部位名称不能为空', 'error');
  737. return;
  738. }
  739. data[colSetting.field] = null;
  740. }
  741. datas.push(data);
  742. posSelects.push(node);
  743. }
  744. }
  745. if (datas.length > 0) {
  746. postData(window.location.pathname + '/update', {pos: {updateType: 'update', updateData: datas} }, function (result) {
  747. if (result.pos) {
  748. stagePos.updateDatas(result.pos.pos);
  749. stagePos.loadCurStageData(result.pos.curStageData);
  750. }
  751. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  752. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  753. // todo 只加载改变项
  754. stagePosSpreadObj.loadCurPosData();
  755. });
  756. }
  757. }
  758. },
  759. leaveCell: function (e, info) {
  760. console.log(1);
  761. },
  762. };
  763. // 加载上下窗口resizer
  764. $.divResizer({
  765. select: '#main-resize',
  766. callback: function () {
  767. slSpread.refresh();
  768. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  769. $(".sp-wrap").height(bcontent-40);
  770. spSpread.refresh();
  771. }
  772. });
  773. // 加载部位明细数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  774. postData(window.location.pathname + '/pos', null, function (result) {
  775. stagePos.loadDatas(result.pos);
  776. if (result.curStageData) {
  777. stagePos.loadCurStageData(result.curStageData);
  778. }
  779. if (result.preStageData) {
  780. stagePos.loadPreStageData(result.preStageData);
  781. }
  782. stagePos.calculateAll();
  783. stagePosSpreadObj.loadCurPosData();
  784. });
  785. spSpread.bind(spreadNS.Events.EditEnded, stagePosSpreadObj.editEnded);
  786. spSpread.bind(spreadNS.Events.ClipboardPasting, stagePosSpreadObj.clipboardPasting);
  787. spSpread.bind(spreadNS.Events.ClipboardPasted, stagePosSpreadObj.clipboardPasted);
  788. spSpread.bind(spreadNS.Events.EditStarting, stagePosSpreadObj.editStarting);
  789. SpreadJsObj.addDeleteBind(spSpread, stagePosSpreadObj.deletePress);
  790. $('#row-view').on('show.bs.modal', function () {
  791. const html = [], customDisplay = customColDisplay();
  792. for (const cd of customDisplay) {
  793. html.push('<tr>');
  794. html.push('<td>', cd.title, '</td>');
  795. html.push('<td>', '<input type="checkbox"' + (cd.visible ? ' checked=""' : '') + '>', '</td>');
  796. html.push('</tr>');
  797. }
  798. $('#row-view-list').html(html.join(''));
  799. });
  800. $('#row-view-ok').click(function () {
  801. const customDisplay = customColDisplay();
  802. const cvl = $('#row-view-list').children();
  803. for (const cv of cvl) {
  804. const title = $(cv).children()[0].innerHTML;
  805. const check = $('input', cv)[0].checked;
  806. const cd = customDisplay.find(function (c) {
  807. return c.title === title;
  808. });
  809. cd.visible = check;
  810. }
  811. customizeStageTreeSetting(ledgerSpreadSetting, customDisplay);
  812. SpreadJsObj.refreshColumnVisible(slSpread.getActiveSheet());
  813. Cookies.set('stage-col-visible', JSON.stringify(customDisplay), 7*24*60*60*1000);
  814. $('#row-view').modal('hide');
  815. });
  816. // 展开收起附件
  817. $('#fujianTab').click(function () {
  818. const obj = $(this), main = $('#main-view'), tool = $('#tools-view'), fujian = $('#fujian');
  819. if (obj.hasClass('active')) {
  820. main.attr('class', 'c-body col-12');
  821. tool.hide();
  822. slSpread.refresh();
  823. spSpread.refresh();
  824. obj.removeClass('active');
  825. fujian.removeClass('active');
  826. } else {
  827. main.attr('class', 'c-body col-8');
  828. tool.show();
  829. slSpread.refresh();
  830. spSpread.refresh();
  831. obj.addClass('active');
  832. fujian.addClass('active');
  833. }
  834. });
  835. });