revise.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. 'use strict';
  2. /**
  3. * 台账修订页面js
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ckBillsSpread = window.location.pathname + '-billsSelect';
  10. function transExpr(expr) {
  11. return expr.replace('=', '').replace('%', '/100');
  12. }
  13. $(document).ready(() => {
  14. autoFlashHeight();
  15. // 初始化spread
  16. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  17. const billsSheet = billsSpread.getActiveSheet();
  18. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  19. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  20. const posSheet = posSpread.getActiveSheet();
  21. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  22. $.subMenu({
  23. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  24. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  25. key: 'revise.info.memu.1.0.0',
  26. callback: function (info) {
  27. if (info.mini) {
  28. $('.panel-title').addClass('fluid');
  29. $('#sub-menu').removeClass('panel-sidebar');
  30. } else {
  31. $('.panel-title').removeClass('fluid');
  32. $('#sub-menu').addClass('panel-sidebar');
  33. }
  34. autoFlashHeight();
  35. billsSpread.refresh();
  36. posSpread.refresh();
  37. }
  38. });
  39. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  40. // 初始化 清单树结构
  41. const treeSetting = {
  42. id: 'ledger_id',
  43. pid: 'ledger_pid',
  44. order: 'order',
  45. level: 'level',
  46. rootId: -1,
  47. keys: ['id', 'tender_id', 'ledger_id'],
  48. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  49. };
  50. if (!isTz) {
  51. treeSetting.calcFields.push('deal_tp');
  52. }
  53. treeSetting.calcFun = function (node) {
  54. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  55. };
  56. const billsTree = createNewPathTree('revise', treeSetting);
  57. billsTree.loadDatas(billsData);
  58. treeCalc.calculateAll(billsTree);
  59. // 加载至spread
  60. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree);
  61. SpreadJsObj.loadTopAndSelect(billsSheet, ckBillsSpread);
  62. // 初始化 部位明细
  63. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  64. pos.loadDatas(posData);
  65. const billsTreeSpreadObj = {
  66. loadExprToInput(sheet) {
  67. const sel = sheet.getSelections()[0];
  68. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  69. if (col.type === 'Number') {
  70. const data = SpreadJsObj.getSelectObject(sheet);
  71. if (data) {
  72. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  73. .attr('readOnly', readOnly || cell.locked());
  74. } else {
  75. $('#bills-expr').val('').attr('readOnly', true);
  76. }
  77. } else {
  78. $('#bills-expr').val('').attr('readOnly', true);
  79. }
  80. },
  81. getDefaultSelectInfo: function (sheet) {
  82. const tree = sheet.zh_tree;
  83. if (!tree) return;
  84. const sel = sheet.getSelections()[0];
  85. const node = sheet.zh_tree.nodes[sel.row];
  86. if (!node) return;
  87. let count = 1;
  88. if (sel.rowCount > 1) {
  89. for (let r = 1; r < sel.rowCount; r++) {
  90. const rNode = sheet.zh_tree.nodes[sel.row + r];
  91. if (rNode.level > node.level) continue;
  92. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  93. toastr.warning('请选择同一清单下的节点,进行该操作');
  94. return;
  95. }
  96. count += 1;
  97. }
  98. }
  99. return [tree, node, count];
  100. },
  101. /**
  102. * 刷新顶部按钮是否可用
  103. * @param sheet
  104. * @param selections
  105. */
  106. refreshOperationValid: function (sheet, selection) {
  107. const setObjEnable = function (obj, enable) {
  108. if (enable) {
  109. obj.removeClass('disabled');
  110. } else {
  111. obj.addClass('disabled');
  112. }
  113. };
  114. const sel = selection ? selection[0] : sheet.getSelections()[0];
  115. const row = sel ? sel.row : -1;
  116. const tree = sheet.zh_tree;
  117. if (!tree) return;
  118. const first = sheet.zh_tree.nodes[row];
  119. let last = first, sameParent = true, nodeUsed = first.used;
  120. if (sel.rowCount > 1) {
  121. for (let r = 1; r < sel.rowCount; r++) {
  122. const rNode = tree.nodes[sel.row + r];
  123. nodeUsed = nodeUsed || rNode.used;
  124. if (rNode.level > first.level) continue;
  125. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  126. sameParent = false;
  127. break;
  128. }
  129. last = rNode;
  130. }
  131. }
  132. const preNode = tree.getPreSiblingNode(first);
  133. const valid = !sheet.zh_setting.readOnly;
  134. setObjEnable($('a[type=insert]'), valid && first && first.level > 1);
  135. setObjEnable($('a[type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed);
  136. setObjEnable($('a[type=up-move]'), valid && first && sameParent && first.level > 1 && preNode);
  137. setObjEnable($('a[type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last));
  138. if (isTz) {
  139. const posRange = last ? pos.getLedgerPos(last.id) : [];
  140. setObjEnable($('a[type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  141. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)));
  142. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  143. setObjEnable($('a[type=down-level]'), valid && first && sameParent && !nodeUsed
  144. && first.level > 1 && preNode && (!preNodePosRange || preNodePosRange.length === 0));
  145. } else {
  146. setObjEnable($('#up-level'), valid && first && sameParent && first.level > 2 && tree.getParent(first));
  147. setObjEnable($('#down-level'), valid && first && sameParent && first.level > 1 && preNode);
  148. }
  149. setObjEnable($('#cut'), valid);
  150. setObjEnable($('#paste'), valid);
  151. },
  152. /**
  153. *
  154. * @param sheet
  155. * @param data
  156. */
  157. refreshTree: function (sheet, data) {
  158. SpreadJsObj.massOperationSheet(sheet, function () {
  159. const tree = sheet.zh_tree;
  160. // 处理删除
  161. if (data.delete) {
  162. data.delete.sort(function (a, b) {
  163. return b.deleteIndex - a.deleteIndex;
  164. });
  165. for (const d of data.delete) {
  166. sheet.deleteRows(d.deleteIndex, 1);
  167. }
  168. }
  169. // 处理新增
  170. if (data.create) {
  171. const newNodes = data.create;
  172. if (newNodes) {
  173. newNodes.sort(function (a, b) {
  174. return a.index - b.index;
  175. });
  176. for (const node of newNodes) {
  177. sheet.addRows(node.index, 1);
  178. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  179. }
  180. }
  181. }
  182. // 处理更新
  183. if (data.update) {
  184. const rows = [];
  185. for (const u of data.update) {
  186. rows.push(tree.nodes.indexOf(u));
  187. }
  188. SpreadJsObj.reLoadRowsData(sheet, rows);
  189. }
  190. // 处理展开
  191. if (data.expand) {
  192. const expanded = [];
  193. for (const e of data.expand) {
  194. if (expanded.indexOf(e) === -1) {
  195. const posterity = tree.getPosterity(e);
  196. for (const p of posterity) {
  197. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  198. expanded.push(p);
  199. }
  200. }
  201. }
  202. }
  203. });
  204. },
  205. selectionChanged: function (e, info) {
  206. if (info.newSelections[0].row !== info.oldSelections[0].row) {
  207. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  208. posSpreadObj.loadCurPosData();
  209. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  210. posSearch.search($('#pos-keyword').val());
  211. }
  212. billsTreeSpreadObj.loadExprToInput(info.sheet);
  213. },
  214. /**
  215. * 新增节点
  216. * @param spread
  217. */
  218. baseOpr: function (sheet, type) {
  219. const self = this;
  220. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  221. if (!tree || !node || !count) return;
  222. if (type === 'delete') {
  223. const parent = tree.getParent(node);
  224. const children = parent ? parent.children : tree.children;
  225. const index = children.indexOf(node);
  226. for (let i = 0; i < count; i++) {
  227. const child = children[i+index];
  228. if (tree.checkNodeUsed(child, pos)) {
  229. toastr.warning('选中的清单已计量,不可删除');
  230. return;
  231. }
  232. }
  233. } else if (type === 'up-level') {
  234. const parent = tree.getParent(node);
  235. const children = parent ? parent.children : tree.children;
  236. const index = children.indexOf(node);
  237. for (let i = index; i < children.length; i++) {
  238. const child = children[index];
  239. if (tree.checkNodeUsed(child, pos)) {
  240. if (i >= index + count) {
  241. toastr.warning('其后清单已计量,选中的清单不可升级');
  242. } else {
  243. toastr.warning('选中的清单已计量,不可升级');
  244. }
  245. return;
  246. }
  247. }
  248. } else if (type === 'down-level') {
  249. const parent = tree.getParent(node);
  250. const children = parent ? parent.children : tree.children;
  251. const index = children.indexOf(node);
  252. for (let i = 0; i < count; i++) {
  253. const child = children[i+index];
  254. if (tree.checkNodeUsed(child, pos)) {
  255. toastr.warning('选中的清单已计量,不可降级');
  256. return;
  257. }
  258. }
  259. }
  260. postData(window.location.pathname + '/update', {
  261. postType: type,
  262. postData: {
  263. id: node.ledger_id,
  264. count: count,
  265. }
  266. }, function (result) {
  267. const refreshData = tree.loadPostData(result);
  268. self.refreshTree(sheet, refreshData);
  269. if (type === 'delete') {
  270. const sel = sheet.getSelections()[0];
  271. if (sel) {
  272. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  273. }
  274. } else if (['up-move', 'down-move'].indexOf(type) > -1) {
  275. const sel = sheet.getSelections()[0];
  276. if (sel) {
  277. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  278. }
  279. } else if (type === 'add') {
  280. const sel = sheet.getSelections()[0];
  281. if (sel) {
  282. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  283. }
  284. }
  285. self.refreshOperationValid(sheet);
  286. });
  287. },
  288. /**
  289. * 编辑单元格响应事件
  290. * @param {Object} e
  291. * @param {Object} info
  292. */
  293. editEnded: function (e, info) {
  294. if (info.sheet.zh_setting) {
  295. const col = info.sheet.zh_setting.cols[info.col];
  296. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  297. const node = sortData[info.row];
  298. const data = {
  299. id: node.id,
  300. tender_id: node.tender_id,
  301. ledger_id: node.ledger_id
  302. };
  303. // 未改变值则不提交
  304. const orgValue = node[col.field];
  305. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (info.editingText === ''))) {
  306. return;
  307. }
  308. // 台账模式,检查部位明细相关
  309. if (isTz) {
  310. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  311. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  312. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  313. if (!node.children || node.children.length ===0) {
  314. const lPos = pos.getLedgerPos(node.id);
  315. if (lPos && lPos.length > 0) {
  316. toastr.error('清单含有部位明细,不可修改施工图复核数量');
  317. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  318. return;
  319. }
  320. }
  321. }
  322. if (col.field === 'b_code' && (info.editingText === '' || !info.editingText)) {
  323. const lPos = pos.getLedgerPos(node.id);
  324. if (lPos && lPos.length > 0) {
  325. toastr.error('清单含有部位明细,请先删除部位明细,再删除清单编号');
  326. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  327. return;
  328. }
  329. }
  330. }
  331. // 获取更新数据
  332. if (info.editingText) {
  333. const text = info.editingText.replace('\n', '');
  334. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  335. toastr.error('清单已计量,请删除编号');
  336. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  337. return;
  338. }
  339. if (col.type === 'Number') {
  340. const num = _.toNumber(text);
  341. if (num) {
  342. data[col.field] = num;
  343. } else {
  344. try {
  345. data[col.field] = math.evaluate(transExpr(text));
  346. } catch(err) {
  347. toastr.error('输入的表达式非法');
  348. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  349. return;
  350. }
  351. }
  352. } else {
  353. data[col.field] = text;
  354. }
  355. } else {
  356. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  357. toastr.error('清单已计量,请删除编号');
  358. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  359. return;
  360. }
  361. data[col.field] = null;
  362. }
  363. // 更新至服务器
  364. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  365. const refreshNode = billsTree.loadPostData(result);
  366. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  367. });
  368. }
  369. },
  370. clipboardPasting: function (e, info) {
  371. if (info.sheet.zh_setting) {
  372. const range = info.cellRange;
  373. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  374. const node = info.sheet.zh_tree.nodes[iRow];
  375. if (info.sheet.zh_tree.checkNodeUsed(node, pos)) {
  376. toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  377. info.cancel = true;
  378. return;
  379. }
  380. }
  381. }
  382. },
  383. clipboardPasted: function (e, info) {
  384. const hint = {
  385. usedUp: {type: 'error', msg: '清单已计量,不可修改单价'},
  386. usedCode: {type: 'error', msg: '清单已计量,编号不可修改为空值'},
  387. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  388. posCode: {type: 'error', msg: '清单含有部位明细,请先删除部位明细,再修改清单编号为空'},
  389. posQty: {type: 'error', msg: '清单含有部位明细,数量金额根据部位明细汇总计算所得,不可修改'},
  390. };
  391. const tree = info.sheet.zh_tree;
  392. if (!tree) { return; }
  393. const sortData = info.sheet.zh_tree.nodes;
  394. const datas = [], filterNodes = [];
  395. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  396. let bPaste = false;
  397. const curRow = info.cellRange.row + iRow;
  398. const node = sortData[curRow];
  399. if (node) {
  400. const data = info.sheet.zh_tree.getNodeKeyData(node);
  401. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  402. const curCol = info.cellRange.col + iCol;
  403. const colSetting = info.sheet.zh_setting.cols[curCol];
  404. const value = info.sheet.getText(curRow, curCol).replace('\n', '');
  405. const lPos = pos.getLedgerPos(node.id);
  406. if (lPos && lPos.length > 0) {
  407. if (value === '' && colSetting.field === 'b_code') {
  408. toastMessageUniq(hint.posCode);
  409. continue;
  410. }
  411. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  412. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  413. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  414. toastMessageUniq(hint.posQty);
  415. continue;
  416. }
  417. }
  418. if (node.used && col.field === 'unit_price') {
  419. toastMessageUniq (hint.usedUp);
  420. continue;
  421. }
  422. if (colSetting.type === 'Number') {
  423. const num = _.toNumber(value);
  424. if (num) {
  425. data[colSetting.field] = num;
  426. } else {
  427. try {
  428. data[colSetting.field] = math.evaluate(transExpr(value));
  429. } catch (err) {
  430. toastMessageUniq(hint.invalidExpr);
  431. continue;
  432. }
  433. }
  434. } else {
  435. if (node.used && (col.field === 'code' || col.field ==='b_code')
  436. && data[colSetting.field] !== '' && value === '') {
  437. toastMessageUniq(hint.usedCode);
  438. continue;
  439. }
  440. data[colSetting.field] = value;
  441. }
  442. bPaste = true;
  443. }
  444. if (bPaste) {
  445. datas.push(data);
  446. } else {
  447. filterNodes.push(node);
  448. }
  449. }
  450. }
  451. if (datas.length > 0) {
  452. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  453. const refreshNode = tree.loadPostData(result);
  454. if (refreshNode.update) {
  455. refreshNode.update = refreshNode.update.concat(filterNodes);
  456. }
  457. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  458. }, function () {
  459. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  460. });
  461. } else {
  462. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  463. }
  464. },
  465. deletePress: function (sheet) {
  466. if (!sheet.zh_setting) return;
  467. const sel = sheet.getSelections()[0], datas = [];
  468. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  469. const node = sheet.zh_tree.nodes[iRow];
  470. if (sheet.zh_tree.checkNodeUsed(node, pos)) {
  471. toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  472. return;
  473. }
  474. const data = sheet.zh_tree.getNodeKeyData(node);
  475. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  476. const col = sheet.zh_setting.cols[iCol];
  477. data[col.field] = null;
  478. }
  479. datas.push(data);
  480. }
  481. if (datas.length > 0) {
  482. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  483. const refreshNode = sheet.zh_tree.loadPostData(result);
  484. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  485. });
  486. }
  487. },
  488. pasteBlock: function (spread, copyInfo) {
  489. const self = this;
  490. const sheet = spread.getActiveSheet();
  491. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  492. postData(window.location.pathname + '/update', {
  493. postType: 'paste-block',
  494. postData: {
  495. id: tree.getNodeKey(node),
  496. tid: copyInfo.tid,
  497. block: copyInfo.block,
  498. }
  499. }, function (data) {
  500. pos.updateDatas(data.pos);
  501. const result = tree.loadPostData(data.ledger);
  502. self.refreshTree(sheet, result);
  503. self.refreshOperationValid(sheet);
  504. removeLocalCache(copyBlockTag);
  505. }, null, true);
  506. },
  507. topRowChanged: function (e, info) {
  508. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  509. },
  510. editStarting(e, info) {
  511. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  512. const col = info.sheet.zh_setting.cols[info.col];
  513. const node = info.sheet.zh_tree.nodes[info.row];
  514. switch (col.field) {
  515. case 'unit_price':
  516. info.cancel = (node.children && node.children.length > 0) || node.used;
  517. break;
  518. case 'sgfh_qty':
  519. case 'sgfh_tp':
  520. case 'sjcl_qty':
  521. case 'sjcl_tp':
  522. case 'qtcl_qty':
  523. case 'qtcl_tp':
  524. case 'deal_qty':
  525. case 'deal_tp':
  526. info.cancel = (node.children && node.children.length > 0);
  527. break;
  528. case 'dgn_qty1':
  529. case 'dgn_qty2':
  530. info.cancel = !_.isEmpty(node.b_code);
  531. break;
  532. }
  533. }
  534. };
  535. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  536. billsTreeSpreadObj.loadExprToInput(billsSheet);
  537. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  538. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  539. if (!readOnly) {
  540. // 增删上下移升降级
  541. $('a[name="base-opr"]').click(function () {
  542. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  543. });
  544. $('#bills-expr').bind('change mouseleave', function () {
  545. const expr = $(this);
  546. const select = SpreadJsObj.getSelectObject(billsSheet);
  547. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val();
  548. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  549. const data = {
  550. id: select.id,
  551. tender_id: select.tender_id,
  552. ledger_id: select.ledger_id
  553. };
  554. const num = _.toNumber(newValue);
  555. if (num) {
  556. data[field] = num;
  557. } else {
  558. try {
  559. data[field] = math.evaluate(transExpr(newValue));
  560. } catch (err) {
  561. toastr.error('输入的表达式非法');
  562. return;
  563. }
  564. }
  565. // 更新至服务器
  566. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  567. const refreshNode = billsTree.loadPostData(result);
  568. expr.val(select[field]);
  569. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  570. });
  571. });
  572. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  573. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  574. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  575. billsSpread.bind(spreadNS.Events.ClipboardPasted, billsTreeSpreadObj.clipboardPasted);
  576. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  577. let batchInsertObj;
  578. // 右键菜单
  579. $.contextMenu({
  580. selector: '#bills-spread',
  581. build: function ($trigger, e) {
  582. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  583. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  584. },
  585. items: {
  586. 'batchInsertBillsPos': {
  587. name: '批量插入清单-部位',
  588. icon: 'fa-sign-in',
  589. disabled: function (key, opt) {
  590. const select = SpreadJsObj.getSelectObject(billsSheet);
  591. if (select) {
  592. if (select.code && select.code !== '') {
  593. return !billsTree.isLeafXmj(select);
  594. } else {
  595. const parent = billsTree.getParent(select);
  596. return !(parent && billsTree.isLeafXmj(parent));
  597. }
  598. } else {
  599. return false;
  600. }
  601. },
  602. callback: function (key, opt) {
  603. if (!batchInsertObj) {
  604. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  605. } else {
  606. batchInsertObj.initView();
  607. }
  608. $('#batch').modal('show');
  609. }
  610. },
  611. 'debug': {
  612. name: 'debug',
  613. callback: function (key, opt) {
  614. console.log(SpreadJsObj.getSelectObject(billsSheet));
  615. }
  616. }
  617. }
  618. });
  619. }
  620. const posSpreadObj = {
  621. /**
  622. * 加载部位明细 根据当前台账选择节点
  623. */
  624. loadCurPosData: function () {
  625. const node = SpreadJsObj.getSelectObject(billsSheet);
  626. if (node) {
  627. const posData = pos.getLedgerPos(node.id) || [];
  628. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  629. } else {
  630. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  631. }
  632. SpreadJsObj.resetFieldReadOnly(posSheet);
  633. },
  634. editStarting: function (e, info) {
  635. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  636. },
  637. /**
  638. * 编辑单元格响应事件
  639. * @param {Object} e
  640. * @param {Object} info
  641. */
  642. editEnded: function (e, info) {
  643. if (!info.sheet.zh_setting) {
  644. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  645. return;
  646. }
  647. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  648. const col = info.sheet.zh_setting.cols[info.col];
  649. const orgText = posData ? posData[col.field] : null;
  650. const newText = info.sheet.getCell(info.row, info.col).text();
  651. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  652. const node = posSpreadObj.billsNode;
  653. if (!node) {
  654. toastr.error('数据错误,请选择台账节点后再试');
  655. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  656. return;
  657. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  658. toastr.error('父节点不可插入部位明细');
  659. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  660. return;
  661. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  662. toastr.error('项目节不可插入部位明细');
  663. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  664. return;
  665. }
  666. const data = {postType: 'pos'};
  667. if (col.field === 'name') {
  668. if (newText === '' && posData) {
  669. toastr.error('部位名称不可为空', 'error');
  670. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  671. return;
  672. } else if (!posData) {
  673. if (newText && newText !== '') {
  674. data.posPostType = 'add';
  675. const sortData = info.sheet.zh_data;
  676. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  677. data.postData = { name: newText, lid: node.id, porder: order};
  678. } else {
  679. return;
  680. }
  681. } else {
  682. data.posPostType = 'update';
  683. data.postData = {id: posData.id, name: newText};
  684. }
  685. } else if (!posData) {
  686. toastr.warning('新增部位请先输入名称');
  687. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  688. return;
  689. } else {
  690. data.posPostType = 'update';
  691. data.postData = {id: posData.id};
  692. if (col.type === 'Number') {
  693. const num = _.toNumber(newText);
  694. if (num) {
  695. data.postData[col.field] = num;
  696. } else {
  697. try {
  698. data.postData[col.field] = math.evaluate(transExpr(newText));
  699. } catch(err) {
  700. toastr.error('输入的表达式非法');
  701. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  702. return;
  703. }
  704. }
  705. } else {
  706. data.postData[col.field] = newText;
  707. }
  708. }
  709. postData(window.location.pathname + '/update', data, function (result) {
  710. const updateRst = pos.updateDatas(result.pos);
  711. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  712. if (updateRst.create.length > 0) {
  713. posSpreadObj.loadCurPosData();
  714. } else {
  715. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  716. }
  717. const loadResult = billsTree.loadPostData(result.ledger);
  718. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  719. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  720. }, function () {
  721. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  722. });
  723. },
  724. /**
  725. * 删除按钮响应事件
  726. * @param sheet
  727. */
  728. deletePress: function (sheet) {
  729. if (!sheet.zh_settting) return;
  730. const sortData = sheet.zh_data;
  731. const datas = [], posSelects = [];
  732. const sel = sheet.getSelections()[0];
  733. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  734. let bDel = false;
  735. const node = sortData[iRow];
  736. if (node) {
  737. const data = {id: node.id};
  738. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  739. const colSetting = sheet.zh_setting.cols[iCol];
  740. if (colSetting.field === 'name') {
  741. toastr.error('部位名称不能为空');
  742. return;
  743. }
  744. const style = sheet.getStyle(iRow, iCol);
  745. if (!style.locked) {
  746. const colSetting = sheet.zh_setting.cols[iCol];
  747. data[colSetting.field] = null;
  748. bDel = true;
  749. }
  750. }
  751. if (bDel) {
  752. datas.push(data);
  753. posSelects.push(node);
  754. }
  755. }
  756. }
  757. if (datas.length > 0) {
  758. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  759. pos.updateDatas(result.pos);
  760. posSpreadObj.loadCurPosData();
  761. const loadResult = billsTree.loadPostData(result.ledger);
  762. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  763. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  764. }, function () {
  765. posSpreadObj.loadCurPosData();
  766. });
  767. }
  768. },
  769. /**
  770. * 删除 部位明细
  771. * @param sheet
  772. */
  773. deletePos: function (sheet) {
  774. const selection = sheet.getSelections();
  775. const data = {
  776. postType: 'pos',
  777. posPostType: 'delete',
  778. postData: [],
  779. };
  780. const row = selection[0].row, count = selection[0].rowCount;
  781. const sortData = sheet.zh_data;
  782. for (let iRow = 0; iRow < count; iRow++) {
  783. const posData = sortData[iRow + row];
  784. if (posData) {
  785. if (posData.used) {
  786. toastr.error('"' + posData.name + '"已计量,请勿删除');
  787. return;
  788. }
  789. data.postData.push(sortData[iRow + row].id);
  790. }
  791. }
  792. if (data.postData.length > 0) {
  793. postData(window.location.pathname + '/update', data, function (result) {
  794. pos.removeDatas(result.pos);
  795. sheet.deleteRows(row, count);
  796. const loadResult = billsTree.loadPostData(result.ledger);
  797. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  798. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  799. });
  800. }
  801. },
  802. clipboardPasting: function (e, info) {
  803. if (!info.sheet.zh_setting) {
  804. info.cancel = true;
  805. return;
  806. }
  807. const range = info.cellRange;
  808. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  809. const posData = info.sheet.zh_data[iRow];
  810. if (posData && posData.used) {
  811. toastr.warning('"' + pos.name +'"已计量,请勿修改');
  812. info.cancel = true;
  813. return;
  814. }
  815. }
  816. },
  817. /**
  818. * 粘贴单元格响应事件
  819. * @param e
  820. * @param info
  821. */
  822. clipboardPasted: function (e, info) {
  823. const node = SpreadJsObj.getSelectObject(billsSheet);
  824. if (node.code && (node.code !== '')) {
  825. toastr.error('项目节不可含有清单明细');
  826. posSpreadObj.loadCurPosData();
  827. return;
  828. }
  829. if (node.children && (node.children.length > 0)) {
  830. toastr.error('仅清单子项可以含有部位明细');
  831. posSpreadObj.loadCurPosData();
  832. return;
  833. }
  834. if (!info.sheet.zh_setting) {
  835. posSpreadObj.loadCurPosData();
  836. return;
  837. }
  838. const data = [];
  839. const sortData = info.sheet.zh_data || [];
  840. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  841. if (info.cellRange.col !== 0) {
  842. toastr.warning('新增部位请先输入名称');
  843. posSpreadObj.loadCurPosData();
  844. return;
  845. }
  846. }
  847. let bHint = false;
  848. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  849. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  850. let bPaste = true;
  851. const curRow = info.cellRange.row + iRow;
  852. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  853. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  854. const curCol = info.cellRange.col + iCol;
  855. const colSetting = info.sheet.zh_setting.cols[curCol];
  856. posData[colSetting.field] = info.sheet.getText(curRow, curCol);
  857. if (colSetting.type === 'Number') {
  858. const num = _.toNumber(posData[colSetting.field]);
  859. if (num) {
  860. posData[colSetting.field] = num;
  861. } else {
  862. try {
  863. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  864. } catch (err) {
  865. if (!bHint) {
  866. toastr.warning('粘贴了非法表达式,已过滤');
  867. bHint = true;
  868. }
  869. bPaste = false;
  870. }
  871. }
  872. }
  873. }
  874. if (bPaste) {
  875. data.push(posData);
  876. }
  877. }
  878. if (data.length === 0) {
  879. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  880. return;
  881. }
  882. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  883. pos.updateDatas(result.pos);
  884. posSpreadObj.loadCurPosData();
  885. const loadResult = billsTree.loadPostData(result.ledger);
  886. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  887. posSpreadObj.loadCurPosData();
  888. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  889. }, function () {
  890. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  891. });
  892. },
  893. selectionChanged: function (e, info) {
  894. const col = info.sheet.zh_setting.cols[info.newSelections[0].col];
  895. const cell = info.sheet.getCell(info.newSelections[0].col, info.newSelections[0].col);
  896. if (col.type === 'Number') {
  897. const data = SpreadJsObj.getSelectObject(info.sheet);
  898. if (data) {
  899. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  900. .attr('row', info.newSelections[0].row).attr('readOnly', readOnly || cell.locked());
  901. } else {
  902. $('#pos-expr').val('').attr('readOnly', true);
  903. }
  904. } else {
  905. $('#pos-expr').val('').attr('readOnly', true);
  906. }
  907. },
  908. };
  909. posSpreadObj.loadCurPosData();
  910. SpreadJsObj.resetTopAndSelect(posSheet);
  911. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  912. if (!readOnly) {
  913. $('#pos-expr').bind('change mouseleave', function () {
  914. const expr = $(this);
  915. const select = SpreadJsObj.getSelectObject(posSheet);
  916. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val();
  917. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  918. const data = {id: select.id};
  919. const num = _.toNumber(newValue);
  920. if (num) {
  921. data[field] = num;
  922. } else {
  923. try {
  924. data[field] = math.evaluate(transExpr(newValue));
  925. } catch (err) {
  926. toastr.error('输入的表达式非法');
  927. return;
  928. }
  929. }
  930. // 更新至服务器
  931. postData(window.location.pathname + '/update', {posPostType: 'update', posData: data}, function (result) {
  932. const updateRst = pos.updateDatas(result.pos);
  933. expr.val(select[field]);
  934. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  935. SpreadJsObj.reLoadRowData(info.sheet, _.toNumber(row));
  936. const loadResult = billsTree.loadPostData(result.ledger);
  937. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  938. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  939. });
  940. });
  941. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  942. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  943. posSpread.bind(spreadNS.Events.ClipboardPasting, posSpreadObj.clipboardPasting);
  944. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  945. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  946. $.contextMenu({
  947. selector: '#pos-spread',
  948. build: function ($trigger, e) {
  949. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  950. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  951. },
  952. items: {
  953. 'delete': {
  954. name: '删除',
  955. icon: 'fa-remove',
  956. disabled: function (key, opt) {
  957. if (posSheet.zh_data) {
  958. const selection = posSheet.getSelections();
  959. return posSheet.zh_data.length < selection[0].row + selection[0].rowCount;
  960. } else {
  961. return true;
  962. }
  963. },
  964. callback: function (key, opt) {
  965. posSpreadObj.deletePos(posSheet);
  966. }
  967. },
  968. 'debug': {
  969. name: 'debug',
  970. callback: function (key, opt) {
  971. console.log(SpreadJsObj.getSelectObject(posSheet));
  972. }
  973. }
  974. }
  975. });
  976. }
  977. $.divResizer({
  978. select: '#revise-resize',
  979. callback: function () {
  980. billsSpread.refresh();
  981. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  982. $(".sp-wrap").height(bcontent-40);
  983. posSpread.refresh();
  984. }
  985. });
  986. class stdLib {
  987. constructor(selector, stdType, treeSetting, spreadSetting) {
  988. const self = this;
  989. this.obj = $(selector + '-spread')[0];
  990. this.stdType = stdType;
  991. this.treeSetting = treeSetting;
  992. treeSetting.preUrl = this.url;
  993. this.spreadSetting = spreadSetting;
  994. this.spread = SpreadJsObj.createNewSpread(this.obj);
  995. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  996. if (!readOnly) {
  997. this.spread.getActiveSheet().bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  998. const stdSheet = info.sheet;
  999. const mainSheet = billsSheet;
  1000. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1001. const stdTree = stdSheet.zh_tree;
  1002. const stdNode = stdTree.nodes[info.row];
  1003. const mainTree = mainSheet.zh_tree;
  1004. const sel = mainSheet.getSelections()[0];
  1005. const mainNode = mainTree.nodes[sel.row];
  1006. if (!stdNode) return;
  1007. if (stdType === 'gcl') {
  1008. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1009. toastr.warning('非最底层项目下,不应添加清单');
  1010. return;
  1011. }
  1012. }
  1013. postData(window.location.pathname + '/update', {
  1014. postType: 'add-std',
  1015. postData: {
  1016. id: mainTree.getNodeKey(mainNode),
  1017. tender_id: mainNode.tender_id,
  1018. stdType: stdType,
  1019. stdLibId: stdNode.list_id,
  1020. stdNode: stdTree.getNodeKey(stdNode)
  1021. }
  1022. }, function (result) {
  1023. const refreshNode = mainTree.loadPostData(result);
  1024. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  1025. if (sel && refreshNode.create[0]) {
  1026. mainSheet.setSelection(mainTree.nodes.indexOf(refreshNode.create[0]), sel.col, sel.rowCount, sel.colCount);
  1027. }
  1028. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1029. });
  1030. });
  1031. }
  1032. this.pathTree = createNewPathTree('base', this.treeSetting);
  1033. this.cacheLib = [];
  1034. $('select', selector).change(function () {
  1035. self.loadLib(parseInt(this.value));
  1036. });
  1037. }
  1038. loadLib (listId) {
  1039. const cacheData = this.cacheLib.find(function (lib) {
  1040. return lib.id === listId;
  1041. });
  1042. if (cacheData) {
  1043. this.pathTree.loadDatas(cacheData.data);
  1044. SpreadJsObj.loadSheetData(this.spread.getActiveSheet(), 'tree', this.pathTree);
  1045. } else {
  1046. const self = this;
  1047. postData('/std-lib/get-data', {stdType: this.stdType, list_id: listId}, function (data) {
  1048. self.cacheLib.push({id: listId, data: data});
  1049. self.pathTree.loadDatas(data);
  1050. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'tree', self.pathTree);
  1051. });
  1052. }
  1053. }
  1054. }
  1055. class DealBills {
  1056. constructor (selector, spreadSetting) {
  1057. const self = this;
  1058. this.loaded = false;
  1059. this.obj = $(selector)[0];
  1060. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1061. this.spreadSetting = spreadSetting;
  1062. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1063. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1064. if (!readOnly) {
  1065. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1066. const dealSheet = info.sheet;
  1067. const mainSheet = billsSheet;
  1068. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1069. if (!dealBills) { return; }
  1070. const mainTree = mainSheet.zh_tree;
  1071. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1072. if (!mainNode || !mainTree) { return; }
  1073. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1074. toastr.warning('非最底层项目下,不应添加清单');
  1075. return;
  1076. }
  1077. postData(window.location.pathname + '/update', {
  1078. postType: 'add-deal',
  1079. postData: {
  1080. id: mainNode.ledger_id,
  1081. type: mainNode.code ? 'child' : 'next',
  1082. dealBills: {
  1083. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1084. unit_price: dealBills.unit_price,
  1085. }
  1086. },
  1087. }, function (result) {
  1088. const refreshData = mainTree.loadPostData(result);
  1089. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1090. const sel = mainSheet.getSelections()[0];
  1091. if (sel && refreshData.create[0]) {
  1092. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1093. }
  1094. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1095. });
  1096. });
  1097. }
  1098. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1099. }
  1100. loadData () {
  1101. if (this.loaded) return;
  1102. const self = this;
  1103. postData(this.url+'/get-data', {}, function (data) {
  1104. self.data = data;
  1105. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1106. self.loaded = true;
  1107. });
  1108. }
  1109. calculateData () {
  1110. if (this.data) {
  1111. for (const d of this.data) {
  1112. d.total_price = _.multiply(d.quantity, d.unit_price);
  1113. }
  1114. }
  1115. }
  1116. }
  1117. class BatchInsertBillsPosObj {
  1118. constructor (obj) {
  1119. const self = this;
  1120. this.obj = obj;
  1121. this.billsCount = 6;
  1122. this.posCount = 1000;
  1123. // 初始化 清单编号窗口 参数
  1124. this.qdSpreadSetting = {
  1125. cols: [
  1126. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1127. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1128. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1129. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1130. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1131. ],
  1132. emptyRows: this.billsCount,
  1133. headRows: 1,
  1134. headRowHeight: [40],
  1135. };
  1136. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1137. // 初始化 部位数量复核表 参数
  1138. this.posSpreadSetting = {
  1139. cols: [
  1140. {title: '部位', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1141. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1142. ],
  1143. emptyRows: this.posCount,
  1144. headRows: 1,
  1145. headRowHeight: [40],
  1146. };
  1147. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1148. this.posSpreadSetting.cols.push(
  1149. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1150. )
  1151. }
  1152. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1153. // 初始化 签约清单 参数
  1154. this.dealSpreadSetting = {
  1155. cols: [
  1156. {title: '清单编号', field: 'code', width: 80, formatter: '@', readOnly: true},
  1157. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true},
  1158. {title: '单位', field: 'unit', width: 50, formatter: '@', readOnly: true},
  1159. {title: '单价', field: 'unit_price', width: 50, readOnly: true},
  1160. ],
  1161. emptyRows: 0,
  1162. headRows: 1,
  1163. headRowHeight: [40],
  1164. };
  1165. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1166. // 初始化 清单编号、部位数量复核表 表格
  1167. this.initView();
  1168. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1169. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1170. // 拉取签约清单数据
  1171. dealBills.loadData();
  1172. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1173. // 双击签约清单,自动添加到清单编号窗口
  1174. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1175. const deal = info.sheet.zh_data[info.row];
  1176. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1177. const sel = qdSheet.getSelections()[0];
  1178. qdSheet.getCell(sel.row, 0).value(deal.code);
  1179. qdSheet.getCell(sel.row, 1).value(deal.name);
  1180. qdSheet.getCell(sel.row, 2).value(deal.unit);
  1181. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  1182. if (sel.row + 1 === qdSheet.getRowCount()) {
  1183. const count = sel.row + 2;
  1184. qdSheet.setRowCount(count);
  1185. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  1186. const colCount = posSheet.getColumnCount() + 1
  1187. posSheet.setColumnCount(colCount);
  1188. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('数量' + count);
  1189. }
  1190. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  1191. });
  1192. this.obj.bind('shown.bs.modal', function () {
  1193. self.qdSpread.refresh();
  1194. self.posSpread.refresh();
  1195. self.dealSpread.refresh();
  1196. });
  1197. $('#batch-ok').click(function () {
  1198. const selection = billsSheet.getSelections();
  1199. const row = selection[0].row;
  1200. const select = billsTree.nodes[row];
  1201. if (select) {
  1202. const insertData = {};
  1203. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  1204. insertData.id = select[billsTree.setting.id];
  1205. insertData.batchData = self.getBatchData();
  1206. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  1207. pos.updateDatas(data.pos);
  1208. const result = billsTree.loadPostData(data.ledger);
  1209. billsTreeSpreadObj.refreshTree(sheet, result);
  1210. billsTreeSpreadObj.refreshOperationValid(sheet, selection);
  1211. self.obj.modal('hide');
  1212. }, null, true);
  1213. }
  1214. });
  1215. }
  1216. // 初始化左侧表格
  1217. initView () {
  1218. // 初始化 清单编号
  1219. const qdSheet = this.qdSpread.getActiveSheet();
  1220. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  1221. SpreadJsObj.refreshColumnAlign(qdSheet);
  1222. // 清理原有数据
  1223. SpreadJsObj.beginMassOperation(qdSheet);
  1224. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1225. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  1226. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  1227. }
  1228. qdSheet.setSelection(0, 0, 1 ,1);
  1229. SpreadJsObj.endMassOperation(qdSheet);
  1230. // 初始化 部位数量复核表
  1231. const posSheet = this.posSpread.getActiveSheet();
  1232. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  1233. SpreadJsObj.refreshColumnAlign(posSheet);
  1234. // 清理原有数据
  1235. SpreadJsObj.beginMassOperation(posSheet);
  1236. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  1237. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1238. posSheet.setSelection(0, 0, 1 ,1);
  1239. SpreadJsObj.endMassOperation(posSheet);
  1240. // 检查签约清单数据,以工具栏数据为准
  1241. if (dealBills) {
  1242. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1243. }
  1244. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  1245. }
  1246. // 获取界面数据
  1247. getBatchData () {
  1248. const result = [];
  1249. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  1250. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  1251. if (qdSheet.getText(iRow, 0) === '') { continue; }
  1252. const qd = {
  1253. b_code: qdSheet.getText(iRow, 0),
  1254. name: qdSheet.getText(iRow, 1),
  1255. unit: qdSheet.getText(iRow, 2),
  1256. price: _.toNumber(qdSheet.getText(iRow, 3)),
  1257. pos: [],
  1258. };
  1259. result.push(qd);
  1260. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  1261. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  1262. if (value !== 0 && !isNaN(value)) {
  1263. qd.pos.push({
  1264. name: posSheet.getText(iPosRow, 0),
  1265. drawing_code: posSheet.getText(iPosRow, 1),
  1266. quantity: value,
  1267. });
  1268. }
  1269. }
  1270. }
  1271. return result;
  1272. }
  1273. }
  1274. let stdXmj, stdGcl, searchLedger;
  1275. const dealBills = new DealBills('#deal-bills-spread', {
  1276. cols: [
  1277. {title: '清单编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1278. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1279. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1280. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1281. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1282. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1283. ],
  1284. emptyRows: 0,
  1285. headRows: 1,
  1286. headRowHeight: [40],
  1287. defaultRowHeight: 21,
  1288. });
  1289. $.divResizer({
  1290. select: '#revise-right-spr',
  1291. callback: function () {
  1292. billsSpread.refresh();
  1293. if (posSpread) {
  1294. posSpread.refresh();
  1295. }
  1296. if (stdXmj) {
  1297. stdXmj.spread.refresh();
  1298. }
  1299. if (stdGcl) {
  1300. stdGcl.spread.refresh();
  1301. }
  1302. }
  1303. });
  1304. // 展开收起标准清单
  1305. $('a', '#side-menu').bind('click', function (e) {
  1306. e.preventDefault();
  1307. const tab = $(this), tabPanel = $(tab.attr('content'));
  1308. const showSideTools = function (show) {
  1309. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1310. if (show) {
  1311. right.show();
  1312. autoFlashHeight();
  1313. /**
  1314. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1315. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1316. * 故需要通过最终的parent.width再计算一次left.width
  1317. *
  1318. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1319. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1320. *
  1321. */
  1322. //left.css('width', parent.width() - right.outerWidth());
  1323. //left.css('width', parent.width() - right.outerWidth());
  1324. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1325. left.css('width', percent + '%');
  1326. } else {
  1327. left.width(parent.width());
  1328. right.hide();
  1329. }
  1330. };
  1331. // 展开工具栏、切换标签
  1332. if (!tab.hasClass('active')) {
  1333. const close = $('.active', '#side-menu').length === 0;
  1334. $('a', '#side-menu').removeClass('active');
  1335. tab.addClass('active');
  1336. $('.tab-content .tab-pane').removeClass('active');
  1337. tabPanel.addClass('active');
  1338. showSideTools(tab.hasClass('active'));
  1339. if (tab.attr('content') === '#std-xmj') {
  1340. if (!stdXmj) {
  1341. stdXmj = new stdLib('#std-xmj', 'xmj', {
  1342. id: 'chapter_id',
  1343. pid: 'pid',
  1344. order: 'order',
  1345. level: 'level',
  1346. rootId: -1,
  1347. keys: ['id', 'list_id', 'chapter_id'],
  1348. }, {
  1349. cols: [
  1350. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1351. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1352. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1353. ],
  1354. treeCol: 0,
  1355. emptyRows: 0,
  1356. headRows: 1,
  1357. headRowHeight: [40],
  1358. defaultRowHeight: 21,
  1359. });
  1360. stdXmj.loadLib($('select', '#std-xmj').val());
  1361. }
  1362. stdXmj.spread.refresh();
  1363. } else if (tab.attr('content') === '#std-gcl') {
  1364. if (!stdGcl) {
  1365. stdGcl = new stdLib('#std-gcl', 'gcl', {
  1366. id: 'bill_id',
  1367. pid: 'pid',
  1368. order: 'order',
  1369. level: 'level',
  1370. rootId: -1,
  1371. keys: ['id', 'list_id', 'bill_id']
  1372. }, {
  1373. cols: [
  1374. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1375. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1376. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1377. ],
  1378. treeCol: 0,
  1379. emptyRows: 0,
  1380. headRows: 1,
  1381. headRowHeight: [40],
  1382. defaultRowHeight: 21,
  1383. });
  1384. stdGcl.loadLib($('select', '#std-gcl').val());
  1385. }
  1386. stdGcl.spread.refresh();
  1387. } else if (tab.attr('content') === '#deal-bills') {
  1388. dealBills.loadData();
  1389. dealBills.spread.refresh();
  1390. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1391. if (!searchLedger) {
  1392. searchLedger = $.billsSearch({
  1393. selector: '#search',
  1394. searchSpread: billsSpread,
  1395. resultSpreadSetting: {
  1396. cols: [
  1397. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1398. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1399. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  1400. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1401. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1402. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1403. ],
  1404. emptyRows: 0,
  1405. headRows: 1,
  1406. headRowHeight: [40],
  1407. defaultRowHeight: 21,
  1408. },
  1409. afterLocated: function () {
  1410. posSpreadObj.loadCurPosData();
  1411. }
  1412. });
  1413. }
  1414. searchLedger.spread.refresh();
  1415. }
  1416. }
  1417. else {// 收起工具栏
  1418. tab.removeClass('active');
  1419. tabPanel.removeClass('active');
  1420. showSideTools(tab.hasClass('active'));
  1421. }
  1422. billsSpread.refresh();
  1423. if (posSpread) {
  1424. posSpread.refresh();
  1425. }
  1426. });
  1427. if (!readOnly) {
  1428. // 修订详情 保存
  1429. $('#save').click(function () {
  1430. const content = $('#content').val();
  1431. postData('save', { content: content });
  1432. });
  1433. }
  1434. // 显示层次
  1435. (function (select, sheet) {
  1436. $(select).click(function () {
  1437. if (!sheet.zh_tree) return;
  1438. const tag = $(this).attr('tag');
  1439. const tree = sheet.zh_tree;
  1440. switch (tag) {
  1441. case "1":
  1442. case "2":
  1443. case "3":
  1444. case "4":
  1445. case "5":
  1446. tree.expandByLevel(parseInt(tag));
  1447. SpreadJsObj.refreshTreeRowVisible(sheet);
  1448. break;
  1449. case "last":
  1450. tree.expandByCustom(() => { return true; });
  1451. SpreadJsObj.refreshTreeRowVisible(sheet);
  1452. break;
  1453. case "leafXmj":
  1454. tree.expandToLeafXmj();
  1455. SpreadJsObj.refreshTreeRowVisible(sheet);
  1456. break;
  1457. }
  1458. });
  1459. })('a[name=showLevel]', billsSheet);
  1460. });