revise.js 70 KB

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