stage.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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: ['contract_qty', 'contract_tp'], visible: true },
  20. { title: '本期数量变更', fields: ['qc_qty', 'qc_tp', 'qc_bgl'], visible: true },
  21. { title: '本期完成计量', fields: ['gather_qty', 'gather_tp'], visible: true },
  22. { title: '截止本期计量合同', fields: ['end_contract_qty', 'end_contract_tp'], visible: true },
  23. { title: '截止本期数量变更', fields: ['end_qc_qty', 'end_qc_tp', 'end_qc_bgl'], visible: true },
  24. { title: '截止本期完成计量', fields: ['end_gather_qty', 'end_gather_tp'], visible: true },
  25. { title: '图册号', fields: ['drawing_code'], visible: true },
  26. { title: '累计完成率(%)', fields: ['percent'], visible: true },
  27. { title: '备注', fields: ['memo'], visible: true },
  28. ];
  29. const settingStr = Cookies.get('stage-col-visible-1.0.0');
  30. return settingStr ? JSON.parse(settingStr) : defaultSetting;
  31. }
  32. /**
  33. * 根据列显示设置,调整setting中的列是否显示
  34. * @param setting
  35. * @param customDisplay
  36. */
  37. function customizeStageTreeSetting(setting, customDisplay) {
  38. for (const cd of customDisplay) {
  39. for (const c of setting.cols) {
  40. if (cd.fields.indexOf(c.field) !== -1) {
  41. c.visible = cd.visible;
  42. }
  43. }
  44. }
  45. }
  46. /**
  47. * 初始化 树结构 列事件
  48. * @param setting
  49. */
  50. function initTreeColSettingEvents(setting) {
  51. const Events = {
  52. readOnly: {
  53. measureData: function (node) {
  54. return node.children && node.children.length > 0;
  55. },
  56. },
  57. };
  58. const getEvent = function (eventName) {
  59. const names = eventName.split('.');
  60. let event = Events;
  61. for (let name of names) {
  62. if (event[name]) {
  63. event = event[name];
  64. } else {
  65. return null;
  66. }
  67. }
  68. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  69. return null;
  70. } else {
  71. return event;
  72. }
  73. };
  74. for (const col of setting.cols) {
  75. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  76. col.readOnly = getEvent(col.readOnly);
  77. }
  78. }
  79. }
  80. function needCheckDetail() {
  81. stage.check_detail = true;
  82. $('#check_point').show();
  83. }
  84. $(document).ready(() => {
  85. // 界面布局
  86. autoFlashHeight();
  87. // 初始化 台账树结构 数据结构
  88. const stageTreeSetting = {
  89. id: 'ledger_id',
  90. pid: 'ledger_pid',
  91. order: 'order',
  92. level: 'level',
  93. rootId: -1,
  94. keys: ['id', 'tender_id', 'ledger_id'],
  95. stageId: 'id',
  96. };
  97. // 台账树结构计算相关设置
  98. stageTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'];
  99. stageTreeSetting.calcFields = ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp',
  100. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp'];
  101. stageTreeSetting.calcFun = function (node) {
  102. if (node.children && node.children.length === 0) {
  103. node.pre_gather_qty = ZhCalc.add(node.pre_contract_qty, node.pre_qc_qty);
  104. node.gather_qty = ZhCalc.add(node.contract_qty, node.qc_qty);
  105. node.end_contract_qty = ZhCalc.add(node.pre_contract_qty, node.contract_qty);
  106. node.end_qc_qty = ZhCalc.add(node.pre_qc_qty, node.qc_qty);
  107. node.end_gather_qty = ZhCalc.add(node.pre_gather_qty, node.gather_qty);
  108. }
  109. node.pre_gather_tp = ZhCalc.add(node.pre_contract_tp, node.pre_qc_tp);
  110. node.gather_tp = ZhCalc.add(node.contract_tp, node.qc_tp);
  111. node.end_contract_tp = ZhCalc.add(node.pre_contract_tp, node.contract_tp);
  112. node.end_qc_tp = ZhCalc.add(node.pre_qc_tp, node.qc_tp);
  113. node.end_gather_tp = ZhCalc.add(node.pre_gather_tp, node.gather_tp);
  114. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), tenderInfo.decimal.up);
  115. };
  116. const stageTree = createNewPathTree('stage', stageTreeSetting);
  117. // 初始化 部位明细 数据结构
  118. const stagePosSetting = {
  119. id: 'id', ledgerId: 'lid',
  120. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  121. };
  122. stagePosSetting.calcFun = function (pos) {
  123. pos.pre_gather_qty = ZhCalc.add(pos.pre_contract_qty, pos.pre_qc_qty);
  124. pos.gather_qty = ZhCalc.add(pos.contract_qty, pos.qc_qty);
  125. pos.end_contract_qty = ZhCalc.add(pos.pre_contract_qty, pos.contract_qty);
  126. pos.end_qc_qty = ZhCalc.add(pos.pre_qc_qty, pos.qc_qty);
  127. pos.end_gather_qty = ZhCalc.add(pos.pre_gather_qty, pos.gather_qty);
  128. };
  129. const stagePos = new StagePosData(stagePosSetting);
  130. class Changes {
  131. constructor(obj) {
  132. const self = this;
  133. this.obj = obj;
  134. // 初始化 清单编号窗口 参数
  135. this.spreadSetting = {
  136. cols: [
  137. {title: '已用', field: '', width: 45, formatter: '@', cellType: 'image', readOnly: true, hAlign: 1, indent: 14, img: function (data) {
  138. if (data.uamount && !checkZero(data.uamount)) {
  139. return $('#icon-ok')[0];
  140. } else {
  141. return null;
  142. }
  143. }},
  144. {title: '变更令号', field: 'code', width: 100, formatter: '@', readOnly: true, hAlign: 0, },
  145. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true, hAlign: 0,},
  146. {title: '总数量', field: 'b_amount', width: 60, formatter: '@', readOnly: true, hAlign: 2, },
  147. {title: '可变更数量', field: 'vamount', width: 60, readOnly: true, hAlign: 2, },
  148. {title: '本期计量', field: 'uamount', width: 60, formatter: '@', hAlign: 2, type: 'Number', },
  149. ],
  150. emptyRows: 0,
  151. headRows: 1,
  152. headRowHeight: [40],
  153. getColor: function (data, col, defaultColor) {
  154. if (col.field === 'uamount') {
  155. if (!data.vamount) {
  156. return data.uamount ? '#ff6f5c' : defaultColor;
  157. } else if (data.uamount) {
  158. return data.uamount > data.vamount ? '#ff6f5c' : defaultColor;
  159. } else {
  160. return defaultColor;
  161. }
  162. } else {
  163. return defaultColor;
  164. }
  165. }
  166. };
  167. this.curChangeId = '';
  168. this.spread = SpreadJsObj.createNewSpread($('#change-spread')[0]);
  169. this.firstView = true;
  170. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  171. // 初次显示,需刷新spread界面,保证界面绘制正确
  172. this.obj.bind('shown.bs.modal', function () {
  173. if (self.firstView) {
  174. self.firstView = false;
  175. self.spread.refresh();
  176. }
  177. });
  178. // 切换变更令,加载右侧明细数据
  179. this.spread.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  180. const change = SpreadJsObj.getSelectObject(info.sheet);
  181. self._loadChangeDetail(change);
  182. });
  183. // 填写本期计量
  184. this.spread.bind(spreadNS.Events.EditEnded, function (e, info) {
  185. if (info.sheet.zh_setting) {
  186. const col = info.sheet.zh_setting.cols[info.col];
  187. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  188. const node = sortData[info.row];
  189. node[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  190. // 刷新界面
  191. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  192. }
  193. });
  194. this.spread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  195. if (info.sheet.zh_setting) {
  196. const sortData = SpreadJsObj.getSortData(info.sheet);
  197. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  198. const curRow = iRow + info.cellRange.row;
  199. const curCol = info.cellRange.col;
  200. const col = info.sheet.zh_setting.cols[info.cellRange.col];
  201. sortData[curRow][col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  202. }
  203. SpreadJsObj.reLoadRowData(sheet, info.cellRange.row, sel.cellRange.rowCount);
  204. }
  205. });
  206. SpreadJsObj.addDeleteBind(this.spread, function (sheet) {
  207. if (sheet.zh_setting) {
  208. const sel = sheet.getSelections()[0];
  209. const sortData = SpreadJsObj.getSortData(sheet);
  210. // 仅本期计量可删除
  211. if (sel.col === 5 || sel.colCount === 1) {
  212. const col = sheet.zh_setting.cols[sel.col];
  213. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  214. const data = sortData[iRow];
  215. data[col.field] = null;
  216. }
  217. SpreadJsObj.reLoadRowData(sheet, sel.row, sel.rowCount);
  218. }
  219. }
  220. });
  221. // 过滤可变更数量为0
  222. $('#filterEmpty').click(function () {
  223. self._filterChange(!this.checked, $('#matchPos')[0].checked);
  224. });
  225. // 匹配编号
  226. $('#matchPos').click(function () {
  227. self._filterChange(!$('#filterEmpty')[0].checked, this.checked);
  228. });
  229. // 展开收起 变更令详细信息
  230. $('#show-bgl-detail').bind('click', function () {
  231. const detail = $('#bgl-detail'), bgl=$('#bgl'), obj=$(this);
  232. if (detail.hasClass('col-4')) {
  233. detail.attr('class', 'col').hide();
  234. bgl.attr('class', 'col-12');
  235. $('a', obj).attr('title', '展开侧栏');
  236. $('i', obj).attr('class', 'fa fa-chevron-left');
  237. self.spread.refresh();
  238. } else {
  239. detail.attr('class', 'col-4').show();
  240. bgl.attr('class', 'col-8');
  241. $('a', obj).attr('title', '收起侧栏');
  242. $('i', obj).attr('class', 'fa fa-chevron-right');
  243. self.spread.refresh();
  244. }
  245. });
  246. // 添加调用变更令
  247. $('#usg-bg-ok').click(function () {
  248. const data = { target: self.callData, change: [] };
  249. for (const c of self.changes) {
  250. if (c.uamount) {
  251. if (!c.vamount || checkZero(c.vamount)) {
  252. toast('变更令:' + c.code + ' 当前不可使用', 'error');
  253. return;
  254. } else {
  255. if (c.uamount > c.vamount) {
  256. toast('变更令:' + c.code + ' 超计,请修改本期计量后,再提交', 'error');
  257. return;
  258. }
  259. }
  260. data.change.push({ cid: c.cid, cbid: c.cbid, qty: c.uamount });
  261. }
  262. }
  263. // 提交数据到后端
  264. postData(window.location.pathname + '/use-change', data, function(result) {
  265. if (result.pos) {
  266. stagePos.loadCurStageData(result.pos);
  267. }
  268. const nodes = stageTree.loadPostStageData(result.bills);
  269. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  270. stagePosSpreadObj.loadCurPosData();
  271. needCheckDetail();
  272. self.obj.modal('hide');
  273. });
  274. })
  275. }
  276. _calculateAmount() {
  277. for (const c of this.changes) {
  278. c.bamount = _.toNumber(c.b_amount);
  279. c.vamount = ZhCalc.sub(c.bamount, c.used_amount);
  280. const uc = _.find(this.useChanges, {cid: c.cid, cbid: c.cbid});
  281. if (uc) {
  282. c.uamount = uc.qty;
  283. c.vamount = ZhCalc.add(c.vamount, c.uamount);
  284. }
  285. }
  286. }
  287. _loadChangeDetail(change) {
  288. if (change) {
  289. if (change.cid === this.curChangeId) { return; }
  290. this.curChangeId = change.cid;
  291. const inputs = $('input[type!=checkbox]', this.obj);
  292. for (const i of inputs) {
  293. const field = $(i).attr('name');
  294. const text = (field && change[field]) ? change[field] : '';
  295. $(i).val(text);
  296. }
  297. const textareas = $('textarea', this.obj);
  298. for (const ta of textareas) {
  299. const field = $(ta).attr('name');
  300. const text = (field && change[field]) ? change[field] : '';
  301. ta.innerText = text;
  302. }
  303. const html = [];
  304. for (const a of change.attachments) {
  305. html.push('<tr>');
  306. html.push('<td>', '<a href="/change/download/file/' + a.id + '">', a.filename + a.fileext, '</a>', '</td>');
  307. html.push('<td>', a.u_name, '</td>');
  308. html.push('</tr>');
  309. }
  310. // 变更类型
  311. if (change.type) {
  312. const cType = change.type.split(',');
  313. $('input[name="type"]').prop("checked", false);
  314. for (const c of cType) {
  315. $('input[name="type"][value='+ c +']').prop("checked", true);
  316. }
  317. }
  318. // 变更类别
  319. $('select[name=class]').val(change.class);
  320. // 变更性质
  321. $('select[name=quality]').val(change.quality);
  322. // 变更单位
  323. $('select[name=company]').html('<option>' + change.company + '</option>');
  324. // 费用承担方
  325. $('input[name=charge][value=' + change.charge + ']').prop('checked', true);
  326. // 附件
  327. $('#attachment').html(html.join(''));
  328. } else {
  329. const inputs = $('input', this.obj);
  330. for (const i of inputs) {
  331. $(i).val('');
  332. }
  333. const textareas = $('textarea', this.obj);
  334. for (const ta of textareas) {
  335. ta.innerText = '';
  336. }
  337. $('#attachment').html('');
  338. }
  339. }
  340. _viewChanges() {
  341. const sheet = this.spread.getActiveSheet();
  342. if (this.changes) {
  343. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, this.changes);
  344. sheet.setSelection(0, 0, 1, 1);
  345. this._loadChangeDetail(this.changes[0]);
  346. this._filterChange(!$('#filterEmpty')[0].checked, $('#matchPos')[0].checked);
  347. } else {
  348. toast('查询变更令有误,请刷新页面后重试', 'warning');
  349. }
  350. }
  351. _filterChange(filterEmpty, matchPosName) {
  352. for (const c of this.changes) {
  353. const filterVisible = filterEmpty ? (c.vamount && !checkZero(c.vamount)) : true;
  354. const matchVisible = matchPosName && this.callData.pos ? c.b_detail === this.callData.pos.name : true;
  355. c.visible = filterVisible && matchVisible;
  356. }
  357. SpreadJsObj.refreshTreeRowVisible(this.spread.getActiveSheet());
  358. }
  359. loadChanges(data) {
  360. this.callData = data;
  361. if (this.callData.pos) {
  362. $('#matchPos').parent().show();
  363. } else {
  364. $('#matchPos').parent().hide();
  365. }
  366. const self = this;
  367. $('#b-code-hint').text('当前变更清单:' + data.bills.b_code);
  368. postData(window.location.pathname + '/valid-change', data, function (result) {
  369. self.changes = result.changes;
  370. self.useChanges = result.useChanges;
  371. self._calculateAmount();
  372. self._viewChanges();
  373. self.obj.modal('show');
  374. });
  375. }
  376. }
  377. const changesObj = new Changes($('#use-bg'));
  378. // 初始化 台账 spread
  379. const slSpread = SpreadJsObj.createNewSpread($('#stage-ledger')[0]);
  380. customizeStageTreeSetting(ledgerSpreadSetting, customColDisplay());
  381. // 数量变更列,添加按钮
  382. const col = _.find(ledgerSpreadSetting.cols, {field: 'qc_qty'});
  383. col.readOnly = true;
  384. col.cellType = 'imageBtn';
  385. col.hoverImg = '#ellipsis-icon';
  386. col.indent = 5;
  387. col.showImage = function (data) {
  388. if (!data || (data.children && data.children.length > 0)) {
  389. return false;
  390. } else {
  391. const nodePos = stagePos.getLedgerPos(data.id);
  392. return !(nodePos && nodePos.length > 0);
  393. }
  394. };
  395. ledgerSpreadSetting.imageClick = function (data) {
  396. changesObj.loadChanges({bills: data});
  397. };
  398. //
  399. SpreadJsObj.initSheet(slSpread.getActiveSheet(), ledgerSpreadSetting);
  400. stageTree.loadDatas(ledgerData);
  401. stageTree.loadCurStageData(curStageData);
  402. stageTree.loadPreStageData(preStageData);
  403. // 根据设置 计算 台账树结构
  404. treeCalc.calculateAll(stageTree);
  405. // 绘制界面
  406. SpreadJsObj.loadSheetData(slSpread.getActiveSheet(), 'tree', stageTree);
  407. // 初始化 部位明细 Spread
  408. const spSpread = SpreadJsObj.createNewSpread($('#stage-pos')[0]);
  409. const spCol = _.find(posSpreadSetting.cols, {field: 'qc_qty'});
  410. spCol.readOnly = true;
  411. spCol.cellType = 'imageBtn';
  412. spCol.hoverImg = '#ellipsis-icon';
  413. spCol.indent = 5;
  414. spCol.showImage = function (data) {
  415. return data;
  416. };
  417. posSpreadSetting.imageClick = function (data) {
  418. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  419. changesObj.loadChanges({bills: node, pos: data});
  420. };
  421. SpreadJsObj.initSheet(spSpread.getActiveSheet(), posSpreadSetting);
  422. const stageTreeSpreadObj = {
  423. refreshTreeNodes: function (sheet, nodes) {
  424. const tree = sheet.zh_tree;
  425. if (!tree) { return }
  426. const rows = [];
  427. for (const node of nodes) {
  428. rows.push(tree.nodes.indexOf(node));
  429. }
  430. SpreadJsObj.reLoadRowsData(sheet, rows);
  431. },
  432. editEnded: function (e, info) {
  433. if (info.sheet.zh_setting) {
  434. const col = info.sheet.zh_setting.cols[info.col];
  435. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  436. const node = sortData[info.row];
  437. if (col.field !== 'postil') {
  438. if (node.children && node.children.length > 0) {
  439. toast('清单父项不可计量', 'error');
  440. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  441. return;
  442. } else {
  443. const nodePos = stagePos.getLedgerPos(node.id);
  444. if (nodePos && nodePos.length > 0) {
  445. toast('该清单有部位明细,请在部位明细处计量', 'error');
  446. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  447. return;
  448. }
  449. }
  450. }
  451. const billsData = {
  452. lid: node.id
  453. };
  454. billsData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  455. postData(window.location.href + '/update', { bills: billsData }, function (data) {
  456. const nodes = stageTree.loadPostStageData(data);
  457. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  458. needCheckDetail();
  459. });
  460. }
  461. },
  462. selectionChanged: function (e, info) {
  463. stagePosSpreadObj.loadCurPosData();
  464. },
  465. deletePress(sheet) {
  466. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  467. const tree = sheet.zh_tree;
  468. if (!tree) { return; }
  469. const sel = sheet.getSelections()[0];
  470. const validCols = [];
  471. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  472. if (!sheet.zh_setting.cols[iCol].readOnly) {
  473. validCols.push(iCol);
  474. }
  475. }
  476. if (validCols.length === 0) { return; }
  477. const sortData = sheet.zh_tree.nodes;
  478. const datas = [];
  479. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  480. const node = sortData[iRow];
  481. if (node) {
  482. const data = { lid: node.id };
  483. let filter = true;
  484. for (const iCol of validCols) {
  485. const colSetting = sheet.zh_setting.cols[iCol];
  486. if (colSetting.field !== 'postil') {
  487. if (node.children && node.children.length > 0) { continue; }
  488. const nodePos = stagePos.getLedgerPos(node.id);
  489. if (nodePos && nodePos.length > 0) { continue; }
  490. }
  491. data[colSetting.field] = null;
  492. filter = false;
  493. }
  494. if (!filter) {
  495. datas.push(data);
  496. }
  497. }
  498. }
  499. if (datas.length > 0) {
  500. postData(window.location.href + '/update', {bills: datas}, function (result) {
  501. const nodes = stageTree.loadPostStageData(result);
  502. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  503. needCheckDetail();
  504. });
  505. }
  506. }
  507. },
  508. clipboardPasting(e, info) {
  509. if (info.sheet.zh_setting) {
  510. const sortData = info.sheet.zh_data;
  511. const range = info.cellRange;
  512. const validField = ['contract_qty', 'contract_tp', 'qc_qty', 'postil'];
  513. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  514. const col = info.sheet.zh_setting.cols[iCol];
  515. if (validField.indexOf(col.field) === -1) {
  516. toast('不可修改此数据', 'error');
  517. info.cancel = true;
  518. return;
  519. }
  520. }
  521. }
  522. },
  523. clipboardPasted(e, info) {
  524. if (info.sheet.zh_setting && info.sheet.zh_tree) {
  525. const sheet = info.sheet;
  526. const filterNodes = [], datas = [];
  527. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  528. const curRow = iRow + info.cellRange.row;
  529. const node = sheet.zh_tree.getItemsByIndex(curRow);
  530. const data = {lid: node.id};
  531. let filter = true;
  532. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  533. const curCol = info.cellRange.col + iCol;
  534. const col = info.sheet.zh_setting.cols[curCol];
  535. if (col.field !== 'postil') {
  536. if (node.children && node.children.length > 0) {
  537. continue;
  538. }
  539. const nodePos = stagePos.getLedgerPos(node.id);
  540. if (nodePos && nodePos.length > 0) {
  541. continue;
  542. }
  543. }
  544. data[col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  545. filter = false;
  546. }
  547. if (filter) {
  548. filterNodes.push(node);
  549. } else {
  550. datas.push(data);
  551. }
  552. }
  553. if (datas.length > 0) {
  554. postData(window.location.href + '/update', { bills: datas }, function (data) {
  555. const nodes = stageTree.loadPostStageData(data);
  556. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes.concat(filterNodes));
  557. needCheckDetail();
  558. });
  559. } else {
  560. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), filterNodes);
  561. }
  562. }
  563. }
  564. };
  565. slSpread.bind(spreadNS.Events.EditEnded, stageTreeSpreadObj.editEnded);
  566. slSpread.bind(spreadNS.Events.SelectionChanged, stageTreeSpreadObj.selectionChanged);
  567. slSpread.bind(spreadNS.Events.ClipboardPasting, stageTreeSpreadObj.clipboardPasting);
  568. slSpread.bind(spreadNS.Events.ClipboardPasted, stageTreeSpreadObj.clipboardPasted);
  569. SpreadJsObj.addDeleteBind(slSpread, stageTreeSpreadObj.deletePress);
  570. const stagePosSpreadObj = {
  571. /**
  572. * 加载部位明细 根据当前台账选择节点
  573. */
  574. loadCurPosData: function () {
  575. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  576. if (node) {
  577. const posData = stagePos.ledgerPos[itemsPre + node.id] || [];
  578. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', posData);
  579. } else {
  580. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', []);
  581. }
  582. },
  583. editEnded: function (e, info) {
  584. if (info.sheet.zh_setting) {
  585. // 未改变过,则直接跳过
  586. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  587. const col = info.sheet.zh_setting.cols[info.col];
  588. const orgText = posData ? posData[col.field] : null;
  589. if (orgText === info.editingText || ((!orgText || orgText === '') && (info.editingText === ''))) {
  590. return;
  591. }
  592. // 台账模式下,不可新增
  593. if (checkTzMeasureType() && !posData) {
  594. toast('台账模式不可新增部位明细数据', 'error');
  595. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  596. info.cancel = true;
  597. return ;
  598. }
  599. // 不同节点下,部位明细检查输入
  600. //const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  601. const node = stagePosSpreadObj.stageTreeNode;
  602. if (!node) {
  603. toast('数据错误, 请刷新页面后再试', 'warning');
  604. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  605. return;
  606. } else if (info.editingText !== '' && node.children && node.children > 0) {
  607. toast('父节点不可插入部位明细', 'error');
  608. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  609. return;
  610. } else if (info.editingText !== '' && !node.b_code || node.b_code === '') {
  611. toast('项目节不可插入部位明细', 'error');
  612. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  613. return;
  614. }
  615. // 生成提交数据
  616. const data = {};
  617. if (col.field === 'name') {
  618. if (info.editingText === '' && pos) {
  619. toast('部位名称不可为空', 'error', 'exclamation-circle');
  620. info.cancel = true;
  621. return;
  622. } else if (!posData) {
  623. if (info.editingText !== '') {
  624. data.updateType = 'add';
  625. data.updateData = {name: info.editingText, lid: node.id, tid: tender.id};
  626. } else {
  627. return;
  628. }
  629. } else {
  630. data.updateType = 'update';
  631. data.updateData = {pid: posData.id, lid: posData.lid, name: info.editingText};
  632. }
  633. } else if (!posData) {
  634. toast('新增部位请先输入名称', 'warning');
  635. } else {
  636. data.updateType = 'update';
  637. data.updateData = {pid: posData.id, lid: posData.lid};
  638. data.updateData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  639. }
  640. // 提交数据到服务器
  641. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  642. if (result.pos) {
  643. stagePos.updateDatas(result.pos.pos);
  644. stagePos.loadCurStageData(result.pos.curStageData);
  645. }
  646. const refreshData = stageTree.loadPostStageData(result.ledger);
  647. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), refreshData);
  648. stagePosSpreadObj.loadCurPosData();
  649. needCheckDetail();
  650. }, function () {
  651. stagePosSpreadObj.loadCurPosData();
  652. });
  653. }
  654. },
  655. editStarting: function (e, info) {
  656. stagePosSpreadObj.stageTreeNode = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  657. },
  658. clipboardPasting: function (e, info) {
  659. if (info.sheet.zh_setting) {
  660. const sortData = info.sheet.zh_data;
  661. const range = info.cellRange;
  662. const validField = ['contract_qty', 'qc_qty', 'postil'];
  663. if (!checkTzMeasureType()) {
  664. validField.push('name', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty');
  665. }
  666. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  667. const col = info.sheet.zh_setting.cols[iCol];
  668. if (validField.indexOf(col.field) === -1) {
  669. if (checkTzMeasureType()) {
  670. toast('不可修改此数据', 'error');
  671. info.cancel = true;
  672. return;
  673. } else {
  674. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow) {
  675. const pos = sortData(iRow);
  676. if (pos.add_stage !== stage.id || pos.add_times !== stage.times) {
  677. toast('不可修改此数据', 'error');
  678. info.cancel = true;
  679. return;
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. },
  687. clipboardPasted: function (e, info) {
  688. const self = this;
  689. if (info.sheet.zh_setting) {
  690. const data = { updateType: '', updateData: [], };
  691. const sortData = info.sheet.zh_data;
  692. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  693. if (sortData && (info.cellRange.row >= sortData.length)) {
  694. data.updateType = 'add';
  695. if (info.cellRange.col !== 0) {
  696. toast('新增部位请先输入名称', 'warning');
  697. self.loadCurPosData();
  698. return;
  699. }
  700. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  701. const curRow = info.cellRange.row + iRow;
  702. const newData = {lid: node.id};
  703. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  704. const curCol = info.cellRange.col + iCol;
  705. const colSetting = info.sheet.zh_setting.cols[curCol];
  706. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  707. if (colSetting.type === 'Number') {
  708. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  709. }
  710. }
  711. data.updateData.push(newData);
  712. }
  713. } else {
  714. data.updateType = 'update';
  715. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  716. const curRow = info.cellRange.row + iRow;
  717. const curPos = sortData[curRow];
  718. if (curPos) {
  719. const newData = {pid: curPos.id, lid: curPos.lid};
  720. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  721. const curCol = info.cellRange.col + iCol;
  722. const colSetting = info.sheet.zh_setting.cols[curCol];
  723. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  724. if (colSetting.type === 'Number') {
  725. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  726. }
  727. }
  728. data.updateData.push(newData);
  729. }
  730. }
  731. }
  732. console.log(data);
  733. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  734. if (result.pos) {
  735. stagePos.updateDatas(result.pos.pos);
  736. stagePos.loadCurStageData(result.pos.curStageData);
  737. }
  738. const nodes = stageTree.loadPostStageData(result.ledger);
  739. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  740. stagePosSpreadObj.loadCurPosData();
  741. needCheckDetail();
  742. }, function () {
  743. stagePosSpreadObj.loadCurPosData();
  744. });
  745. }
  746. },
  747. deletePress: function (sheet) {
  748. if (sheet.zh_setting && sheet.zh_data) {
  749. const sortData = sheet.zh_data;
  750. if (!sortData || sortData.length === 0) { return; }
  751. const sel = sheet.getSelections()[0];
  752. const validCols = [];
  753. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  754. if (!sheet.zh_setting.cols[iCol].readOnly) {
  755. validCols.push(iCol);
  756. }
  757. }
  758. if (validCols.length === 0) { return; }
  759. const datas = [], posSelects = [];
  760. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  761. const node = sortData[iRow];
  762. if (node) {
  763. const data = {pid: node.id, lid: node.lid};
  764. for (const iCol of validCols) {
  765. const colSetting = sheet.zh_setting.cols[iCol];
  766. if (colSetting.field === 'name') {
  767. toast('部位名称不能为空', 'error');
  768. return;
  769. }
  770. data[colSetting.field] = null;
  771. }
  772. datas.push(data);
  773. posSelects.push(node);
  774. }
  775. }
  776. if (datas.length > 0) {
  777. postData(window.location.pathname + '/update', {pos: {updateType: 'update', updateData: datas} }, function (result) {
  778. if (result.pos) {
  779. stagePos.updateDatas(result.pos.pos);
  780. stagePos.loadCurStageData(result.pos.curStageData);
  781. }
  782. const nodes = stageTree.loadPostStageData(result.ledger);
  783. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  784. needCheckDetail();
  785. // todo 只加载改变项
  786. stagePosSpreadObj.loadCurPosData();
  787. });
  788. }
  789. }
  790. },
  791. leaveCell: function (e, info) {
  792. console.log(1);
  793. },
  794. };
  795. // 加载上下窗口resizer
  796. $.divResizer({
  797. select: '#main-resize',
  798. callback: function () {
  799. slSpread.refresh();
  800. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  801. $(".sp-wrap").height(bcontent-40);
  802. spSpread.refresh();
  803. }
  804. });
  805. // 加载部位明细数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  806. postData(window.location.pathname + '/pos', null, function (result) {
  807. stagePos.loadDatas(result.pos);
  808. if (result.curStageData) {
  809. stagePos.loadCurStageData(result.curStageData);
  810. }
  811. if (result.preStageData) {
  812. stagePos.loadPreStageData(result.preStageData);
  813. }
  814. stagePos.calculateAll();
  815. stagePosSpreadObj.loadCurPosData();
  816. });
  817. spSpread.bind(spreadNS.Events.EditEnded, stagePosSpreadObj.editEnded);
  818. spSpread.bind(spreadNS.Events.ClipboardPasting, stagePosSpreadObj.clipboardPasting);
  819. spSpread.bind(spreadNS.Events.ClipboardPasted, stagePosSpreadObj.clipboardPasted);
  820. spSpread.bind(spreadNS.Events.EditStarting, stagePosSpreadObj.editStarting);
  821. SpreadJsObj.addDeleteBind(spSpread, stagePosSpreadObj.deletePress);
  822. if (!checkTzMeasureType()) {
  823. }
  824. $('#row-view').on('show.bs.modal', function () {
  825. const html = [], customDisplay = customColDisplay();
  826. for (const cd of customDisplay) {
  827. html.push('<tr>');
  828. html.push('<td>', cd.title, '</td>');
  829. html.push('<td>', '<input type="checkbox"' + (cd.visible ? ' checked=""' : '') + '>', '</td>');
  830. html.push('</tr>');
  831. }
  832. $('#row-view-list').html(html.join(''));
  833. });
  834. $('#row-view-ok').click(function () {
  835. const customDisplay = customColDisplay();
  836. const cvl = $('#row-view-list').children();
  837. for (const cv of cvl) {
  838. const title = $(cv).children()[0].innerHTML;
  839. const check = $('input', cv)[0].checked;
  840. const cd = customDisplay.find(function (c) {
  841. return c.title === title;
  842. });
  843. cd.visible = check;
  844. }
  845. customizeStageTreeSetting(ledgerSpreadSetting, customDisplay);
  846. SpreadJsObj.refreshColumnVisible(slSpread.getActiveSheet());
  847. Cookies.set('stage-col-visible', JSON.stringify(customDisplay), 7*24*60*60*1000);
  848. $('#row-view').modal('hide');
  849. });
  850. class SearchLedger {
  851. constructor(obj, mainSpread) {
  852. const self = this;
  853. this.obj = obj;
  854. this.mainSpread = mainSpread;
  855. this.spreadSetting = {
  856. cols: [
  857. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@'},
  858. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  859. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@'},
  860. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@'},
  861. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  862. {title: '数量', field: 'quantity', hAlign: 2, width: 50},
  863. {title: '完成率(%)', field: 'complete_percent', hAlign: 2, width: 70},
  864. ],
  865. emptyRows: 3,
  866. headRows: 1,
  867. headRowHeight: [40],
  868. defaultRowHeight: 21,
  869. readOnly: true
  870. };
  871. this.spread = SpreadJsObj.createNewSpread($('#search-result', this.obj)[0]);
  872. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  873. $('#searchLedger', this.obj).bind('click', function () {
  874. const keyword = $('#keyword', self.obj).val();
  875. if (keyword !== '') {
  876. $('#over', obj)[0].checked = false;
  877. $('#empty', obj)[0].checked = false;
  878. this.searchResult = [];
  879. const sortData = SpreadJsObj.getSortData(self.mainSpread.getActiveSheet());
  880. for (const node of sortData) {
  881. if ((node.code && node.code.indexOf(keyword) > -1) ||
  882. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  883. node.name && node.name.indexOf(keyword) > -1) {
  884. this.searchResult.push(node);
  885. }
  886. }
  887. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', this.searchResult);
  888. }
  889. });
  890. $('#over', this.obj).bind('change', function () {
  891. this.searchResult = [];
  892. const sortData = SpreadJsObj.getSortData(self.mainSpread.getActiveSheet());
  893. for (const node of sortData) {
  894. if (node.children && node.children.length > 0) continue;
  895. if (node.end_gather_qty) {
  896. if (!node.quantity || Math.abs(node.end_gather_qty) > Math.abs(node.quantity)) {
  897. this.searchResult.push(node);
  898. }
  899. } else if (node.end_gather_tp) {
  900. if (!node.total_price || Math.abs(node.end_gather_tp) > Math.abs(node.total_price)) {
  901. this.searchResult.push(node);
  902. }
  903. }
  904. }
  905. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', this.searchResult);
  906. });
  907. $('#empty', this.obj).bind('change', function () {
  908. this.searchResult = [];
  909. const sortData = SpreadJsObj.getSortData(self.mainSpread.getActiveSheet());
  910. for (const node of sortData) {
  911. if (node.children && node.children.length > 0) continue;
  912. if (node.quantity) {
  913. if (!node.end_gather_qty || checkZero(node.end_gather_qty)) {
  914. this.searchResult.push(node);
  915. }
  916. } else if (node.total_price) {
  917. if (!node.end_gather_tp || checkZero(node.end_gather_tp)) {
  918. this.searchResult.push(node);
  919. }
  920. }
  921. }
  922. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', this.searchResult);
  923. });
  924. this.spread.getActiveSheet().bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  925. const sheet = info.sheet;
  926. const data = sheet.zh_data;
  927. if (!data) { return }
  928. const curBills = data[info.row];
  929. if (!curBills) { return }
  930. SpreadJsObj.locateTreeNode(self.mainSpread.getActiveSheet(), curBills.ledger_id);
  931. });
  932. }
  933. }
  934. let searchLedger;
  935. $.divResizer({
  936. select: '#right-spr',
  937. callback: function () {
  938. slSpread.refresh();
  939. spSpread.refresh();
  940. if (searchLedger) {
  941. searchLedger.spread.refresh();
  942. }
  943. }
  944. });
  945. // 展开收起附件
  946. $('a', '.side-menu').bind('click', function () {
  947. //const main = $('#main-view'), tool = $('#tools-view');
  948. const tab = $(this), tabPanel = $(tab.attr('content'));
  949. const showTools = function (show) {
  950. const left = $('#main-view'), right = $('#tools-view'), parent = left.parent();
  951. if (show) {
  952. right.show();
  953. autoFlashHeight();
  954. /**
  955. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  956. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  957. * 故需要通过最终的parent.width再计算一次left.width
  958. *
  959. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  960. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  961. *
  962. */
  963. //left.css('width', parent.width() - right.outerWidth());
  964. //left.css('width', parent.width() - right.outerWidth());
  965. const percent = 100 - right.outerWidth() /parent.width() * 100;
  966. left.css('width', percent + '%');
  967. } else {
  968. right.hide();
  969. left.css('width', '100%');
  970. }
  971. };
  972. if (!tab.hasClass('active')) {
  973. $('a', '.side-menu').removeClass('active');
  974. $('.tab-content .tab-pane').removeClass('active');
  975. tab.addClass('active');
  976. tabPanel.addClass('active');
  977. showTools(tab.hasClass('active'));
  978. if (tab.attr('content') === '#search' && !searchLedger) {
  979. searchLedger = new SearchLedger($('#search'), slSpread);
  980. searchLedger.spread.refresh();
  981. }
  982. } else {
  983. tab.removeClass('active');
  984. tabPanel.removeClass('active');
  985. showTools(tab.hasClass('active'));
  986. }
  987. slSpread.refresh();
  988. spSpread.refresh();
  989. });
  990. });