stage.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 = _.toNumber(c.b_amount);
  276. const uc = _.find(this.useChanges, {cid: c.cid, cbid: c.cbid});
  277. if (uc) {
  278. c.uamount = uc.qty;
  279. }
  280. }
  281. }
  282. _loadChangeDetail(change) {
  283. if (change) {
  284. if (change.cid === this.curChangeId) { return; }
  285. this.curChangeId = change.cid;
  286. const inputs = $('input[type!=checkbox]', this.obj);
  287. for (const i of inputs) {
  288. const field = $(i).attr('name');
  289. const text = (field && change[field]) ? change[field] : '';
  290. $(i).val(text);
  291. }
  292. const textareas = $('textarea', this.obj);
  293. for (const ta of textareas) {
  294. const field = $(ta).attr('name');
  295. const text = (field && change[field]) ? change[field] : '';
  296. ta.innerText = text;
  297. }
  298. const html = [];
  299. for (const a of change.attachments) {
  300. html.push('<tr>');
  301. html.push('<td>', a.filename + a.fileext, '</td>');
  302. html.push('<td>', a.u_name, '</td>');
  303. html.push('</tr>');
  304. }
  305. // 变更类型
  306. const cType = change.type.split(',');
  307. $('input[name="type"]').prop("checked", false);
  308. for (const c of cType) {
  309. $('input[name="type"][value='+ c +']').prop("checked", true);
  310. }
  311. // 变更类别
  312. $('select[name=class]').val(change.class);
  313. // 变更性质
  314. $('select[name=quality]').val(change.quality);
  315. // 变更单位
  316. $('select[name=company]').html('<option>' + change.company + '</option>');
  317. // 费用承担方
  318. $('input[name=charge][value=' + change.charge + ']').prop('checked', true);
  319. // 附件
  320. $('#attachment').html(html.join(''));
  321. } else {
  322. const inputs = $('input', this.obj);
  323. for (const i of inputs) {
  324. $(i).val('');
  325. }
  326. const textareas = $('textarea', this.obj);
  327. for (const ta of textareas) {
  328. ta.innerText = '';
  329. }
  330. $('#attachment').html('');
  331. }
  332. }
  333. _viewChanges() {
  334. const sheet = this.spread.getActiveSheet();
  335. if (this.changes) {
  336. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, this.changes);
  337. sheet.setSelection(0, 0, 1, 1);
  338. this._loadChangeDetail(this.changes[0]);
  339. this._filterEmptyChange(!$('#customCheckDisabled')[0].checked);
  340. } else {
  341. toast('查询变更令有误,请刷新页面后重试', 'warning');
  342. }
  343. }
  344. _filterEmptyChange(isFilter) {
  345. for (const c of this.changes) {
  346. c.visible = isFilter ? (c.vamount && !checkZero(c.vamount)) : true;
  347. }
  348. SpreadJsObj.refreshTreeRowVisible(this.spread.getActiveSheet());
  349. }
  350. loadChanges(data, code) {
  351. this.callData = data;
  352. const self = this;
  353. $('#b-code-hint').text('当前变更清单:' + code);
  354. postData(window.location.pathname + '/valid-change', data, function (result) {
  355. self.changes = result.changes;
  356. self.useChanges = result.useChanges;
  357. self._calculateAmount();
  358. self._viewChanges();
  359. self.obj.modal('show');
  360. });
  361. }
  362. }
  363. const changesObj = new Changes($('#use-bg'));
  364. // 初始化 台账 spread
  365. const slSpread = SpreadJsObj.createNewSpread($('#stage-ledger')[0]);
  366. customizeStageTreeSetting(ledgerSpreadSetting, customColDisplay());
  367. // 数量变更列,添加按钮
  368. const col = _.find(ledgerSpreadSetting.cols, {field: 'qc_qty'});
  369. col.readOnly = true;
  370. col.cellType = 'imageBtn';
  371. col.hoverImg = '#ellipsis-icon';
  372. col.indent = 5;
  373. col.showImage = function (data) {
  374. if (!data || (data.children && data.children.length > 0)) {
  375. return false;
  376. } else {
  377. const nodePos = stagePos.getLedgerPos(data.id);
  378. return !(nodePos && nodePos.length > 0);
  379. }
  380. };
  381. ledgerSpreadSetting.imageClick = function (data) {
  382. changesObj.loadChanges({bills: data}, data.b_code);
  383. };
  384. //
  385. SpreadJsObj.initSheet(slSpread.getActiveSheet(), ledgerSpreadSetting);
  386. stageTree.loadDatas(ledgerData);
  387. stageTree.loadCurStageData(curStageData);
  388. stageTree.loadPreStageData(preStageData);
  389. // 根据设置 计算 台账树结构
  390. treeCalc.calculateAll(stageTree);
  391. // 绘制界面
  392. SpreadJsObj.loadSheetData(slSpread.getActiveSheet(), 'tree', stageTree);
  393. // 初始化 部位明细 Spread
  394. const spSpread = SpreadJsObj.createNewSpread($('#stage-pos')[0]);
  395. const spCol = _.find(posSpreadSetting.cols, {field: 'qc_qty'});
  396. spCol.readOnly = true;
  397. spCol.cellType = 'imageBtn';
  398. spCol.hoverImg = '#ellipsis-icon';
  399. spCol.indent = 5;
  400. spCol.showImage = function (data) {
  401. return data;
  402. };
  403. posSpreadSetting.imageClick = function (data) {
  404. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  405. changesObj.loadChanges({pos: data}, node.b_code);
  406. };
  407. SpreadJsObj.initSheet(spSpread.getActiveSheet(), posSpreadSetting);
  408. const stageTreeSpreadObj = {
  409. refreshTreeNodes: function (sheet, nodes) {
  410. const tree = sheet.zh_tree;
  411. if (!tree) { return }
  412. const rows = [];
  413. for (const node of nodes) {
  414. rows.push(tree.nodes.indexOf(node));
  415. }
  416. SpreadJsObj.reLoadRowsData(sheet, rows);
  417. },
  418. editEnded: function (e, info) {
  419. if (info.sheet.zh_setting) {
  420. const col = info.sheet.zh_setting.cols[info.col];
  421. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  422. const node = sortData[info.row];
  423. if (node.children && node.children.length > 0) {
  424. toast('清单父项不可计量', 'error');
  425. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  426. return;
  427. } else {
  428. const nodePos = stagePos.getLedgerPos(node.id);
  429. if (nodePos && nodePos.length > 0) {
  430. toast('该清单有部位明细,请在部位明细处计量', 'error');
  431. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  432. return;
  433. }
  434. }
  435. const billsData = {
  436. lid: node.id
  437. };
  438. billsData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  439. postData(window.location.href + '/update', { bills: billsData }, function (data) {
  440. const nodes = stageTree.loadPostStageData(data.bills);
  441. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  442. });
  443. }
  444. },
  445. selectionChanged: function (e, info) {
  446. stagePosSpreadObj.loadCurPosData();
  447. },
  448. deletePress(sheet) {
  449. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  450. const tree = sheet.zh_tree;
  451. if (!tree) { return; }
  452. const sel = sheet.getSelections()[0];
  453. const validCols = [];
  454. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  455. if (!sheet.zh_setting.cols[iCol].readOnly) {
  456. validCols.push(iCol);
  457. }
  458. }
  459. if (validCols.length === 0) { return; }
  460. const sortData = sheet.zh_tree.nodes;
  461. const datas = [];
  462. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  463. const node = sortData[iRow];
  464. if (node) {
  465. if (node.children && node.children.length > 0) { continue; }
  466. const nodePos = stagePos.getLedgerPos(node.id);
  467. if (nodePos && nodePos.length > 0) { continue; }
  468. const data = { lid: node.id };
  469. for (const iCol of validCols) {
  470. const colSetting = sheet.zh_setting.cols[iCol];
  471. data[colSetting.field] = null;
  472. }
  473. datas.push(data);
  474. }
  475. }
  476. if (datas.length > 0) {
  477. postData(window.location.href + '/update', {bills: datas}, function (result) {
  478. const nodes = stageTree.loadPostStageData(result.bills);
  479. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  480. });
  481. }
  482. }
  483. },
  484. clipboardPasting(e, info) {
  485. if (info.sheet.zh_setting) {
  486. const sortData = info.sheet.zh_data;
  487. const range = info.cellRange;
  488. const validField = ['contract_qty', 'contract_tp', 'qc_qty', 'postil'];
  489. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  490. const col = info.sheet.zh_setting.cols[iCol];
  491. if (validField.indexOf(col.field) === -1) {
  492. toast('不可修改此数据', 'error');
  493. info.cancel = true;
  494. return;
  495. }
  496. }
  497. }
  498. },
  499. clipboardPasted(e, info) {
  500. if (info.sheet.zh_setting && info.sheet.zh_tree) {
  501. const sheet = info.sheet;
  502. const filterNodes = [], datas = [];
  503. console.log(info.cellRange);
  504. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  505. const curRow = iRow + info.cellRange.row;
  506. const node = sheet.zh_tree.getItemsByIndex(curRow);
  507. if (node.children && node.children.length > 0) {
  508. filterNodes.push(node);
  509. continue;
  510. }
  511. const nodePos = stagePos.getLedgerPos(node.id);
  512. if (nodePos && nodePos.length > 0) {
  513. filterNodes.push(node);
  514. continue;
  515. }
  516. const data = {lid: node.id};
  517. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  518. const curCol = info.cellRange.col + iCol;
  519. const col = info.sheet.zh_setting.cols[curCol];
  520. data[col.field] = col.type === 'Number' ? _.toNumber(info.sheet.getText(curRow, curCol)) : info.sheet.getText(curRow, curCol);
  521. }
  522. datas.push(data);
  523. }
  524. console.log(datas);
  525. if (datas.length > 0) {
  526. postData(window.location.href + '/update', { bills: datas }, function (data) {
  527. const nodes = stageTree.loadPostStageData(data.bills);
  528. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes.concat(filterNodes));
  529. });
  530. } else {
  531. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), filterNodes);
  532. }
  533. }
  534. }
  535. };
  536. slSpread.bind(spreadNS.Events.EditEnded, stageTreeSpreadObj.editEnded);
  537. slSpread.bind(spreadNS.Events.SelectionChanged, stageTreeSpreadObj.selectionChanged);
  538. slSpread.bind(spreadNS.Events.ClipboardPasting, stageTreeSpreadObj.clipboardPasting);
  539. slSpread.bind(spreadNS.Events.ClipboardPasted, stageTreeSpreadObj.clipboardPasted);
  540. SpreadJsObj.addDeleteBind(slSpread, stageTreeSpreadObj.deletePress);
  541. const stagePosSpreadObj = {
  542. /**
  543. * 加载部位明细 根据当前台账选择节点
  544. */
  545. loadCurPosData: function () {
  546. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  547. if (node) {
  548. const posData = stagePos.ledgerPos[itemsPre + node.id] || [];
  549. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', posData);
  550. } else {
  551. SpreadJsObj.loadSheetData(spSpread.getActiveSheet(), 'data', []);
  552. }
  553. },
  554. editEnded: function (e, info) {
  555. if (info.sheet.zh_setting) {
  556. // 未改变过,则直接跳过
  557. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  558. const col = info.sheet.zh_setting.cols[info.col];
  559. const orgText = posData ? posData[col.field] : null;
  560. if (orgText === info.editingText || ((!orgText || orgText === '') && (info.editingText === ''))) {
  561. return;
  562. }
  563. // 台账模式下,不可新增
  564. if (checkTzMeasureType() && !posData) {
  565. toast('台账模式不可新增部位明细数据', 'error');
  566. info.cancel = true;
  567. return ;
  568. }
  569. // 不同节点下,部位明细检查输入
  570. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  571. if (!node) {
  572. toast('数据错误, 请刷新页面后再试', 'warning');
  573. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  574. return;
  575. } else if (info.editingText !== '' && node.children && node.children > 0) {
  576. toast('父节点不可插入部位明细', 'error');
  577. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  578. return;
  579. } else if (info.editingText !== '' && !node.b_code || node.b_code === '') {
  580. toast('项目节不可插入部位明细', 'error');
  581. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  582. return;
  583. }
  584. // 生成提交数据
  585. const data = {};
  586. if (col.field === 'name') {
  587. if (info.editingText === '' && pos) {
  588. toast('部位名称不可为空', 'error', 'exclamation-circle');
  589. info.cancel = true;
  590. return;
  591. } else if (!pos) {
  592. if (info.editingText !== '') {
  593. data.updateType = 'add';
  594. data.updateData = {name: info.editingText, lid: node.id, tid: tender.id};
  595. } else {
  596. return;
  597. }
  598. } else {
  599. data.updateType = 'update';
  600. data.updateData = {id: posData.id, name: info.editingText};
  601. }
  602. } else if (!posData) {
  603. toast('新增部位请先输入名称', 'warning');
  604. } else {
  605. data.updateType = 'update';
  606. data.updateData = {pid: posData.id, lid: posData.lid};
  607. data.updateData[col.field] = col.type === 'Number' ? parseFloat(info.editingText) : info.editingText;
  608. }
  609. // 提交数据到服务器
  610. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  611. if (result.pos) {
  612. stagePos.updateDatas(result.pos.pos);
  613. stagePos.loadCurStageData(result.pos.curStageData);
  614. }
  615. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  616. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  617. stagePosSpreadObj.loadCurPosData();
  618. }, function () {
  619. stagePosSpreadObj.loadCurPosData();
  620. });
  621. }
  622. },
  623. clipboardPasting: function (e, info) {
  624. if (info.sheet.zh_setting) {
  625. const sortData = info.sheet.zh_data;
  626. const range = info.cellRange;
  627. const validField = ['contract_qty', 'qc_qty', 'postil'];
  628. for (let iCol = range.col; iCol < range.col + range.colCount; iCol++) {
  629. const col = info.sheet.zh_setting.cols[iCol];
  630. if (validField.indexOf(col.field) === -1) {
  631. if (checkTzMeasureType()) {
  632. toast('不可修改此数据', 'error');
  633. info.cancel = true;
  634. return;
  635. } else {
  636. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow) {
  637. const pos = sortData(iRow);
  638. if (pos.add_stage !== stage.id || pos.add_times !== stage.times) {
  639. toast('不可修改此数据', 'error');
  640. info.cancel = true;
  641. return;
  642. }
  643. }
  644. }
  645. }
  646. }
  647. }
  648. },
  649. clipboardPasted: function (e, info) {
  650. const self = this;
  651. if (info.sheet.zh_setting) {
  652. const data = { updateType: '', updateData: [], };
  653. const sortData = info.sheet.zh_data;
  654. const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
  655. if (sortData && (info.cellRange.row >= sortData.length)) {
  656. data.updateType = 'add';
  657. if (info.cellRange.col !== 0) {
  658. toast('新增部位请先输入名称', 'warning');
  659. self.loadCurPosData();
  660. return;
  661. }
  662. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  663. const curRow = info.cellRange.row + iRow;
  664. const newData = {lid: node.id};
  665. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  666. const curCol = info.cellRange.col + iCol;
  667. const colSetting = info.sheet.zh_setting.cols[curCol];
  668. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  669. if (colSetting.type === 'Number') {
  670. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  671. }
  672. }
  673. data.updateData.push(newData);
  674. }
  675. } else {
  676. data.updateType = 'update';
  677. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  678. const curRow = info.cellRange.row + iRow;
  679. const curPos = sortData[curRow];
  680. if (curPos) {
  681. const newData = {pid: curPos.id, lid: curPos.lid};
  682. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  683. const curCol = info.cellRange.col + iCol;
  684. const colSetting = info.sheet.zh_setting.cols[curCol];
  685. newData[colSetting.field] = info.sheet.getText(curRow, curCol);
  686. if (colSetting.type === 'Number') {
  687. newData[colSetting.field] = _.toNumber(newData[colSetting.field]);
  688. }
  689. }
  690. data.updateData.push(newData);
  691. }
  692. }
  693. }
  694. console.log(data);
  695. postData(window.location.pathname + '/update', {pos: data}, function (result) {
  696. if (result.pos) {
  697. stagePos.updateDatas(result.pos.pos);
  698. stagePos.loadCurStageData(result.pos.curStageData);
  699. }
  700. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  701. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  702. stagePosSpreadObj.loadCurPosData();
  703. }, function () {
  704. stagePosSpreadObj.loadCurPosData();
  705. });
  706. }
  707. },
  708. deletePress: function (sheet) {
  709. if (sheet.zh_setting && sheet.zh_data) {
  710. const sortData = sheet.zh_data;
  711. if (!sortData || sortData.length === 0) { return; }
  712. const sel = sheet.getSelections()[0];
  713. const validCols = [];
  714. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  715. if (!sheet.zh_setting.cols[iCol].readOnly) {
  716. validCols.push(iCol);
  717. }
  718. }
  719. if (validCols.length === 0) { return; }
  720. const datas = [], posSelects = [];
  721. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  722. const node = sortData[iRow];
  723. if (node) {
  724. const data = {pid: node.id, lid: node.lid};
  725. for (const iCol of validCols) {
  726. const colSetting = sheet.zh_setting.cols[iCol];
  727. if (colSetting.field === 'name') {
  728. toast('部位名称不能为空', 'error');
  729. return;
  730. }
  731. data[colSetting.field] = null;
  732. }
  733. datas.push(data);
  734. posSelects.push(node);
  735. }
  736. }
  737. if (datas.length > 0) {
  738. postData(window.location.pathname + '/update', {pos: {updateType: 'update', updateData: datas} }, function (result) {
  739. if (result.pos) {
  740. stagePos.updateDatas(result.pos.pos);
  741. stagePos.loadCurStageData(result.pos.curStageData);
  742. }
  743. const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
  744. stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
  745. // todo 只加载改变项
  746. stagePosSpreadObj.loadCurPosData();
  747. });
  748. }
  749. }
  750. },
  751. };
  752. // 加载上下窗口resizer
  753. $.divResizer({
  754. select: '#main-resize',
  755. callback: function () {
  756. slSpread.refresh();
  757. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  758. $(".sp-wrap").height(bcontent-40);
  759. spSpread.refresh();
  760. }
  761. });
  762. // 加载部位明细数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  763. postData(window.location.pathname + '/pos', null, function (result) {
  764. stagePos.loadDatas(result.pos);
  765. if (result.curStageData) {
  766. stagePos.loadCurStageData(result.curStageData);
  767. }
  768. if (result.preStageData) {
  769. stagePos.loadPreStageData(result.preStageData);
  770. }
  771. stagePos.calculateAll();
  772. stagePosSpreadObj.loadCurPosData();
  773. });
  774. spSpread.bind(spreadNS.Events.EditEnded, stagePosSpreadObj.editEnded);
  775. spSpread.bind(spreadNS.Events.ClipboardPasting, stagePosSpreadObj.clipboardPasting);
  776. spSpread.bind(spreadNS.Events.ClipboardPasted, stagePosSpreadObj.clipboardPasted);
  777. SpreadJsObj.addDeleteBind(spSpread, stagePosSpreadObj.deletePress);
  778. $('#row-view').on('show.bs.modal', function () {
  779. const html = [], customDisplay = customColDisplay();
  780. for (const cd of customDisplay) {
  781. html.push('<tr>');
  782. html.push('<td>', cd.title, '</td>');
  783. html.push('<td>', '<input type="checkbox"' + (cd.visible ? ' checked=""' : '') + '>', '</td>');
  784. html.push('</tr>');
  785. }
  786. $('#row-view-list').html(html.join(''));
  787. });
  788. $('#row-view-ok').click(function () {
  789. const customDisplay = customColDisplay();
  790. const cvl = $('#row-view-list').children();
  791. for (const cv of cvl) {
  792. const title = $(cv).children()[0].innerHTML;
  793. const check = $('input', cv)[0].checked;
  794. const cd = customDisplay.find(function (c) {
  795. return c.title === title;
  796. });
  797. cd.visible = check;
  798. }
  799. customizeStageTreeSetting(ledgerSpreadSetting, customDisplay);
  800. SpreadJsObj.refreshColumnVisible(slSpread.getActiveSheet());
  801. Cookies.set('stage-col-visible', JSON.stringify(customDisplay), 7*24*60*60*1000);
  802. $('#row-view').modal('hide');
  803. });
  804. });