ledger.js 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /**
  2. * 台账相关js
  3. *
  4. * @author Mai
  5. * @date 2018/02/05
  6. * @version
  7. */
  8. const ckBillsSpread = window.location.pathname + '-billsSelect';
  9. function checkTzMeasureType () {
  10. return tender.measure_type === measureType.tz.value;
  11. }
  12. function getTenderId() {
  13. return window.location.pathname.split('/')[2];
  14. }
  15. const copyBlockTag = 'zh.calc.copyBlock';
  16. const invalidFields = {
  17. parent: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'unit_price'],
  18. gcl: ['dgn_qty1', 'dgn_qty2'],
  19. posCode: ['b_code'],
  20. posCalc: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp'],
  21. };
  22. function transExpr(expr) {
  23. return expr.replace('=', '').replace('%', '/100');
  24. }
  25. $(document).ready(function() {
  26. autoFlashHeight();
  27. // 初始化台账
  28. const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]);
  29. SpreadJsObj.addDeleteBind(ledgerSpread);
  30. const treeSetting = {
  31. id: 'ledger_id',
  32. pid: 'ledger_pid',
  33. order: 'order',
  34. level: 'level',
  35. rootId: -1,
  36. keys: ['id', 'tender_id', 'ledger_id'],
  37. preUrl: '/tender/' + getTenderId() + '/ledger',
  38. };
  39. if (checkTzMeasureType()) {
  40. treeSetting.calcFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'];
  41. } else {
  42. treeSetting.calcFields = ['deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'];
  43. }
  44. treeSetting.calcFun = function (node) {
  45. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  46. };
  47. const ledgerTree = createNewPathTree('ledger', treeSetting);
  48. // 初始化 部位明细
  49. const pos = new PosData({
  50. id: 'id', ledgerId: 'lid',
  51. });
  52. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  53. $.subMenu({
  54. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  55. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  56. //key: 'ledger.memu.1.0.0',
  57. callback: function (info) {
  58. if (info.mini) {
  59. $('.panel-title').addClass('fluid');
  60. $('#sub-menu').removeClass('panel-sidebar');
  61. } else {
  62. $('.panel-title').removeClass('fluid');
  63. $('#sub-menu').addClass('panel-sidebar');
  64. }
  65. autoFlashHeight();
  66. ledgerSpread.refresh();
  67. posSpread.refresh();
  68. }
  69. });
  70. // 定义事件
  71. const treeOperationObj = {
  72. loadExprToInput(sheet) {
  73. const sel = sheet.getSelections()[0];
  74. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  75. if (col.type === 'Number') {
  76. const data = SpreadJsObj.getSelectObject(sheet);
  77. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  78. .attr('readOnly', readOnly || cell.locked());
  79. } else {
  80. $('#bills-expr').val('').attr('readOnly', true);
  81. }
  82. },
  83. getSelectNode: function (sheet) {
  84. if (!sheet || !sheet.zh_tree) {
  85. return null;
  86. } else {
  87. const sel = sheet.getSelections()[0];
  88. return sheet.zh_tree.nodes[sel.row];
  89. }
  90. },
  91. getDefaultSelectInfo: function (sheet) {
  92. const tree = sheet.zh_tree;
  93. if (!tree) return;
  94. const sel = sheet.getSelections()[0];
  95. const node = sheet.zh_tree.nodes[sel.row];
  96. if (!node) return;
  97. let count = 1;
  98. if (sel.rowCount > 1) {
  99. for (let r = 1; r < sel.rowCount; r++) {
  100. const rNode = sheet.zh_tree.nodes[sel.row + r];
  101. if (rNode.level > node.level) continue;
  102. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  103. toastr.error('请选择同一清单下的节点,进行该操作');
  104. return;
  105. }
  106. count += 1;
  107. }
  108. }
  109. return [tree, node, count];
  110. },
  111. /**
  112. * 刷新顶部按钮是否可用
  113. * @param sheet
  114. * @param selections
  115. */
  116. refreshOperationValid: function (sheet, selection) {
  117. const setObjEnable = function (obj, enable) {
  118. if (enable) {
  119. obj.removeClass('disabled');
  120. } else {
  121. obj.addClass('disabled');
  122. }
  123. };
  124. const sel = selection ? selection[0] : sheet.getSelections()[0];
  125. const row = sel ? sel.row : -1;
  126. const tree = sheet.zh_tree;
  127. if (!tree) return;
  128. const first = sheet.zh_tree.nodes[row];
  129. let last = first, sameParent = true;
  130. if (sel.rowCount > 1) {
  131. for (let r = 1; r < sel.rowCount; r++) {
  132. const rNode = tree.nodes[sel.row + r];
  133. if (rNode.level > first.level) continue;
  134. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  135. sameParent = false;
  136. break;
  137. }
  138. last = rNode;
  139. }
  140. }
  141. const preNode = tree.getPreSiblingNode(first);
  142. const valid = !sheet.zh_setting.readOnly;
  143. setObjEnable($('#insert'), valid && first && first.level > 1);
  144. setObjEnable($('#delete'), valid && first && sameParent && !(first.level === 1 && first.node_type));
  145. setObjEnable($('#up-move'), valid && first && sameParent && first.level > 1 && preNode);
  146. setObjEnable($('#down-move'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last));
  147. if (checkTzMeasureType()) {
  148. const posRange = last ? pos.getLedgerPos(last.id) : [];
  149. setObjEnable($('#up-level'), valid && first && sameParent && tree.getParent(first) && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)));
  150. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  151. setObjEnable($('#down-level'), valid && first && sameParent && first.level > 1 && preNode && (!preNodePosRange || preNodePosRange.length === 0));
  152. } else {
  153. setObjEnable($('#up-level'), valid && first && sameParent && first.level > 2 && tree.getParent(first));
  154. setObjEnable($('#down-level'), valid && first && sameParent && first.level > 1 && preNode);
  155. }
  156. setObjEnable($('#cut'), valid);
  157. setObjEnable($('#paste'), valid);
  158. },
  159. refreshTree: function (sheet, data) {
  160. SpreadJsObj.massOperationSheet(sheet, function () {
  161. const tree = sheet.zh_tree;
  162. // 处理删除
  163. if (data.delete) {
  164. data.delete.sort(function (x, y) {
  165. return y.deleteIndex - x.deleteIndex;
  166. });
  167. for (const d of data.delete) {
  168. sheet.deleteRows(d.deleteIndex, 1);
  169. }
  170. }
  171. // 处理新增
  172. if (data.create) {
  173. const newNodes = data.create;
  174. if (newNodes) {
  175. newNodes.sort(function (a, b) {
  176. return a.index - b.index;
  177. });
  178. for (const node of newNodes) {
  179. sheet.addRows(node.index, 1);
  180. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  181. }
  182. }
  183. }
  184. // 处理更新
  185. if (data.update) {
  186. const rows = [];
  187. for (const u of data.update) {
  188. rows.push(tree.nodes.indexOf(u));
  189. }
  190. SpreadJsObj.reLoadRowsData(sheet, rows);
  191. }
  192. // 处理展开
  193. if (data.expand) {
  194. const expanded = [];
  195. for (const e of data.expand) {
  196. if (expanded.indexOf(e) === -1) {
  197. const posterity = tree.getPosterity(e);
  198. for (const p of posterity) {
  199. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  200. expanded.push(p);
  201. }
  202. }
  203. }
  204. }
  205. });
  206. },
  207. /**
  208. * 新增节点
  209. * @param spread
  210. */
  211. addNode: function (sheet) {
  212. const self = this;
  213. const row = sheet.getSelections()[0].row;
  214. const tree = sheet.zh_tree;
  215. if (!tree) { return; }
  216. const node = sheet.zh_tree.nodes[row];
  217. if (!node) { return; }
  218. postData(window.location.pathname + '/update', {
  219. postType: 'add',
  220. postData: {
  221. id: tree.getNodeKey(node),
  222. }
  223. }, function (result) {
  224. const refreshNode = tree.loadPostData(result);
  225. self.refreshTree(sheet, refreshNode);
  226. self.refreshOperationValid(sheet);
  227. });
  228. },
  229. /**
  230. * 删除选中节点
  231. * @param spread
  232. */
  233. deleteNode: function (sheet) {
  234. const self = this;
  235. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  236. if (tree && node && count) {
  237. const data = {
  238. postType: 'delete',
  239. postData: {
  240. id: tree.getNodeKey(node),
  241. count: count
  242. }
  243. };
  244. postData(window.location.pathname + '/update', data, function (result) {
  245. const refreshNode = tree.loadPostData(result);
  246. for (const d of refreshNode.delete) pos.removeDatasByMasterId(d.id);
  247. self.refreshTree(sheet, refreshNode);
  248. self.refreshOperationValid(sheet);
  249. posOperationObj.loadCurPosData();
  250. });
  251. }
  252. },
  253. /**
  254. * 上移选中节点
  255. * @param spread
  256. */
  257. upMove: function (sheet) {
  258. const self = this;
  259. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  260. if (tree && node && count) {
  261. const data = {
  262. postType: 'up-move',
  263. postData: {
  264. id: tree.getNodeKey(node),
  265. count: count
  266. }
  267. };
  268. postData(window.location.pathname + '/update', data, function (result) {
  269. const refreshNode = tree.loadPostData(result);
  270. self.refreshTree(sheet, refreshNode);
  271. const sel = sheet.getSelections()[0];
  272. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  273. self.refreshOperationValid(sheet);
  274. });
  275. }
  276. },
  277. /**
  278. * 下移选中节点
  279. * @param spread
  280. */
  281. downMove: function (sheet) {
  282. const self = this;
  283. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  284. if (tree && node && count) {
  285. const data = {
  286. postType: 'down-move',
  287. postData: {
  288. id: tree.getNodeKey(node),
  289. count: count
  290. }
  291. };
  292. postData(window.location.pathname + '/update', data, function (result) {
  293. const refreshNode = tree.loadPostData(result);
  294. self.refreshTree(sheet, refreshNode);
  295. const sel = sheet.getSelections()[0];
  296. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  297. self.refreshOperationValid(sheet);
  298. });
  299. }
  300. },
  301. /**
  302. * 升级选中节点
  303. * @param spread
  304. */
  305. upLevel: function (sheet) {
  306. const self = this;
  307. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  308. if (tree && node && count) {
  309. const data = {
  310. postType: 'up-level',
  311. postData: {
  312. id: tree.getNodeKey(node),
  313. count: count
  314. }
  315. };
  316. postData(window.location.pathname + '/update', data, function (result) {
  317. const refreshNode = tree.loadPostData(result);
  318. self.refreshTree(sheet, refreshNode);
  319. self.refreshOperationValid(sheet);
  320. });
  321. }
  322. },
  323. /**
  324. * 降级选中节点
  325. * @param spread
  326. */
  327. downLevel: function (sheet) {
  328. const self = this;
  329. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  330. if (tree && node && count) {
  331. const data = {
  332. postType: 'down-level',
  333. postData: {
  334. id: tree.getNodeKey(node),
  335. count: count
  336. }
  337. };
  338. postData(window.location.pathname + '/update', data, function (result) {
  339. const refreshNode = tree.loadPostData(result);
  340. self.refreshTree(sheet, refreshNode);
  341. self.refreshOperationValid(sheet);
  342. });
  343. }
  344. },
  345. /**
  346. * 编辑单元格响应事件
  347. * @param {Object} e
  348. * @param {Object} info
  349. */
  350. editEnded: function (e, info) {
  351. if (info.sheet.zh_setting) {
  352. const col = info.sheet.zh_setting.cols[info.col];
  353. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  354. const node = sortData[info.row];
  355. const data = {
  356. id: node.id,
  357. tender_id: node.tender_id,
  358. ledger_id: node.ledger_id
  359. };
  360. // 未改变值则不提交
  361. const orgValue = node[col.field];
  362. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (info.editingText === ''))) {
  363. return;
  364. }
  365. // 台账模式,检查部位明细相关
  366. if (checkTzMeasureType()) {
  367. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  368. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  369. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  370. if (!node.children || node.children.length ===0) {
  371. const lPos = pos.getLedgerPos(node.id);
  372. if (lPos && lPos.length > 0) {
  373. toastr.error('清单含有部位明细,请在部位明细输入数量');
  374. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  375. return;
  376. }
  377. }
  378. }
  379. if (col.field === 'b_code' && (info.editingText === '' || !info.editingText)) {
  380. const lPos = pos.getLedgerPos(node.id);
  381. if (lPos && lPos.length > 0) {
  382. toastr.error('清单含有部位明细,请先删除部位明细,再删除清单编号');
  383. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  384. return;
  385. }
  386. }
  387. }
  388. // 获取更新数据
  389. if (info.editingText) {
  390. const num = _.toNumber(info.editingText);
  391. if (num) {
  392. data[col.field] = num;
  393. } else {
  394. try {
  395. data[col.field] = math.evaluate(transExpr(info.editingText));
  396. } catch(err) {
  397. toastr.error('输入的表达式非法');
  398. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  399. return;
  400. }
  401. }
  402. } else {
  403. data[col.field] = null;
  404. }
  405. // 更新至服务器
  406. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  407. const refreshNode = ledgerTree.loadPostData(result);
  408. treeOperationObj.refreshTree(info.sheet, refreshNode);
  409. });
  410. }
  411. },
  412. /**
  413. * 粘贴单元格响应事件
  414. * @param e
  415. * @param info
  416. */
  417. clipboardPasted: function (e, info) {
  418. if (info.sheet.zh_setting && info.sheet.zh_dataType === 'tree') {
  419. const tree = info.sheet.zh_tree;
  420. if (!tree) { return; }
  421. const sortData = info.sheet.zh_tree.nodes;
  422. const datas = [], filterNodes = [];
  423. let bHint = false, bPaste;
  424. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  425. bPaste = false;
  426. const curRow = info.cellRange.row + iRow;
  427. const node = sortData[curRow];
  428. if (node) {
  429. const data = info.sheet.zh_tree.getNodeKeyData(node);
  430. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  431. const curCol = info.cellRange.col + iCol;
  432. const colSetting = info.sheet.zh_setting.cols[curCol];
  433. const value = info.sheet.getText(curRow, curCol).replace('\n', '');
  434. const lPos = pos.getLedgerPos(node.id);
  435. if (lPos && lPos.length > 0) {
  436. if (value === '' && colSetting.field === 'b_code') {
  437. if (!bHint) {
  438. toastr.warning('清单含有部位明细,请先删除部位明细,再删除清单编号');
  439. bHint = true;
  440. }
  441. continue;
  442. }
  443. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  444. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  445. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  446. if (!bHint) {
  447. toastr.warning('清单含有部位明细,数量金额根据部位明细汇总计算所得,不可编辑');
  448. bHint = true;
  449. }
  450. continue;
  451. }
  452. }
  453. if (colSetting.type === 'Number') {
  454. const num = _.toNumber(value);
  455. if (num) {
  456. data[colSetting.field] = num;
  457. bPaste = true;
  458. } else {
  459. try {
  460. data[colSetting.field] = math.evaluate(transExpr(value));
  461. bPaste = true;
  462. } catch(err) {
  463. if (!bHint) {
  464. toastr.warning('输入的表达式非法');
  465. bHint = true;
  466. }
  467. }
  468. }
  469. } else {
  470. data[colSetting.field] = value;
  471. bPaste = true;
  472. }
  473. }
  474. if (bPaste) {
  475. datas.push(data);
  476. } else {
  477. filterNodes.push(node);
  478. }
  479. }
  480. }
  481. if (datas.length > 0) {
  482. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  483. const refreshNode = tree.loadPostData(result);
  484. if (refreshNode.update) {
  485. refreshNode.update = refreshNode.update.concat(filterNodes);
  486. }
  487. treeOperationObj.refreshTree(info.sheet, refreshNode);
  488. }, function () {
  489. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  490. });
  491. } else {
  492. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  493. }
  494. }
  495. },
  496. clipboardPasting: function (e, info) {
  497. const sheet = info.sheet, tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  498. console.log(info);
  499. info.cancel = true;
  500. if (!setting || !tree) return;
  501. const pasteData = info.pasteData.html
  502. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  503. : (info.pasteData.text === ''
  504. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  505. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  506. let bParentHint = false, bGclHint = false, bCodeHint = false, bQtyHint = false, bNumHint = false;
  507. const datas = [], filterNodes = [];
  508. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  509. const curRow = info.cellRange.row + iRow;
  510. const node = tree.nodes[curRow];
  511. if (!node) continue;
  512. let bPaste = false;
  513. const data = info.sheet.zh_tree.getNodeKeyData(node);
  514. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  515. const curCol = info.cellRange.col + iCol;
  516. const colSetting = info.sheet.zh_setting.cols[curCol];
  517. const value = pasteData[iRow][iCol];
  518. const lPos = pos.getLedgerPos(node.id);
  519. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  520. if (!bParentHint) bParentHint = true;
  521. continue;
  522. }
  523. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  524. if (!bGclHint) bGclHint = true;
  525. continue;
  526. }
  527. if (lPos && lPos.length > 0) {
  528. if (value === '' && invalidFields.posCode.indexOf(colSetting.field) >= 0) {
  529. if (!bCodeHint) bCodeHint = true;
  530. continue;
  531. }
  532. if (invalidFields.posCalc.indexOf(colSetting.field) >= 0) {
  533. if (!bQtyHint) bQtyHint = true;
  534. continue;
  535. }
  536. }
  537. if (colSetting.type === 'Number') {
  538. const num = _.toNumber(value);
  539. if (num) {
  540. data[colSetting.field] = num;
  541. bPaste = true;
  542. } else {
  543. try {
  544. data[colSetting.field] = math.evaluate(transExpr(value));
  545. } catch(err) {
  546. bNumHint = true;
  547. }
  548. }
  549. } else {
  550. data[colSetting.field] = value;
  551. bPaste = true;
  552. }
  553. }
  554. if (bPaste) {
  555. datas.push(data);
  556. } else {
  557. filterNodes.push(node);
  558. }
  559. }
  560. const hint = [];
  561. if (bParentHint) hint.push('含有子项的清单,不可粘贴数量、单价、金额');
  562. if (bGclHint) hint.push('工程量清单,不可粘贴设计数量');
  563. if (bQtyHint) hint.push('含有部位明细的清单,不可粘贴数量');
  564. if (bCodeHint) hint.push('含有部位明细的清单,清单编号不可粘贴空值');
  565. if (bNumHint) hint.push('单价、数量等应输入数字或可计算的表达式');
  566. // if (hint.length > 0) hint.unshift('复制粘贴的数据中,存在非法数据,已过滤:');
  567. // if (hint.length > 0) toastr.warning(hint.join('</br>'));
  568. if (hint.length > 0) toastr.warning('复制粘贴的数据中,存在非法数据,已过滤');
  569. if (datas.length > 0) {
  570. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  571. const refreshNode = tree.loadPostData(result);
  572. if (refreshNode.update) {
  573. refreshNode.update = refreshNode.update.concat(filterNodes);
  574. }
  575. treeOperationObj.refreshTree(info.sheet, refreshNode);
  576. }, function () {
  577. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  578. });
  579. } else {
  580. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  581. }
  582. },
  583. /**
  584. * 删除按钮响应事件
  585. * @param sheet
  586. */
  587. deletePress: function (sheet) {
  588. if (sheet.zh_setting && sheet.zh_dataType === 'tree') {
  589. const tree = sheet.zh_tree;
  590. if (!tree) { return; }
  591. const sortData = sheet.zh_tree.nodes;
  592. const datas = [], nodes = [];
  593. const sel = sheet.getSelections()[0];
  594. let bHint = false;
  595. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  596. const node = sortData[iRow];
  597. if (node) {
  598. let bDel = false;
  599. const data = sheet.zh_tree.getNodeKeyData(node);
  600. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  601. const col = sheet.zh_setting.cols[iCol];
  602. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  603. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  604. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  605. const lPos = pos.getLedgerPos(node.id);
  606. if (lPos && lPos.length > 0) {
  607. if (!bHint) {
  608. if (col.field === 'code') {
  609. toastr.warning('清单含有部位明细,请先删除部位明细,再删除清单编号');
  610. } else {
  611. toastr.warning('清单含有部位明细,数量金额根据部位明细汇总计算所得,不可删除');
  612. }
  613. bHint = true;
  614. }
  615. continue;
  616. }
  617. }
  618. const style = sheet.getStyle(iRow, iCol);
  619. if (!style.locked) {
  620. const colSetting = sheet.zh_setting.cols[iCol];
  621. data[colSetting.field] = null;
  622. bDel = true;
  623. }
  624. }
  625. if (bDel) {
  626. datas.push(data);
  627. nodes.push(node);
  628. }
  629. }
  630. }
  631. if (datas.length > 0) {
  632. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  633. const refreshNode = tree.loadPostData(result);
  634. treeOperationObj.refreshTree(sheet, refreshNode);
  635. });
  636. }
  637. }
  638. },
  639. /**
  640. * 粘贴整块
  641. * @param spread
  642. * @param block
  643. */
  644. pasteBlock: function (spread, copyInfo) {
  645. const self = this;
  646. const sheet = spread.getActiveSheet();
  647. const row = sheet.getSelections()[0].row;
  648. const tree = sheet.zh_tree;
  649. if (!tree) { return; }
  650. const node = tree.nodes[row];
  651. if (!node) { return; }
  652. postData(window.location.pathname + '/update', {
  653. postType: 'paste-block',
  654. postData: {
  655. id: tree.getNodeKey(node),
  656. tid: copyInfo.tid,
  657. block: copyInfo.block,
  658. }
  659. }, function (data) {
  660. pos.updateDatas(data.pos);
  661. const result = tree.loadPostData(data.ledger);
  662. self.refreshTree(sheet, result);
  663. self.refreshOperationValid(sheet);
  664. removeLocalCache(copyBlockTag);
  665. }, null, true);
  666. },
  667. selectionChanged: function (e, info) {
  668. if (!info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  669. posOperationObj.loadCurPosData();
  670. SpreadJsObj.resetTopAndSelect(posSpread.getActiveSheet());
  671. posSearch.search($('#pos-keyword').val());
  672. }
  673. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  674. treeOperationObj.loadExprToInput(info.sheet);
  675. },
  676. topRowChanged(e, info) {
  677. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  678. },
  679. cut: function (sheet, sel, callback) {
  680. if (!sheet || !sel) return;
  681. if (sel.colCount >= sheet.zh_setting.cols.length) {
  682. toastr.warning('请勿选中整行剪切');
  683. return;
  684. }
  685. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  686. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  687. const node = sortData[iRow];
  688. if (node) {
  689. const data = sheet.zh_tree.getNodeKeyData(node);
  690. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  691. const col = sheet.zh_setting.cols[iCol];
  692. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  693. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  694. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  695. const lPos = pos.getLedgerPos(node.id);
  696. if (lPos && lPos.length > 0) {
  697. toastr.error('不可剪切');
  698. return;
  699. }
  700. }
  701. const style = sheet.getStyle(iRow, iCol);
  702. if (style.locked) {
  703. toastr.error('不可剪切');
  704. return;
  705. }
  706. const colSetting = sheet.zh_setting.cols[iCol];
  707. data[colSetting.field] = null;
  708. }
  709. datas.push(data);
  710. }
  711. }
  712. if (datas.length > 0) {
  713. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  714. const refreshNode = sheet.zh_tree.loadPostData(result);
  715. callback();
  716. treeOperationObj.refreshTree(sheet, refreshNode);
  717. });
  718. }
  719. },
  720. editStarting(e, info) {
  721. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  722. const col = info.sheet.zh_setting.cols[info.col];
  723. const node = info.sheet.zh_tree.nodes[info.row];
  724. switch (col.field) {
  725. case 'unit_price':
  726. case 'sgfh_qty':
  727. case 'sgfh_tp':
  728. case 'sjcl_qty':
  729. case 'sjcl_tp':
  730. case 'qtcl_qty':
  731. case 'qtcl_tp':
  732. case 'deal_qty':
  733. case 'deal_tp':
  734. info.cancel = node.children && node.children.length > 0;
  735. break;
  736. case 'dgn_qty1':
  737. case 'dgn_qty2':
  738. info.cancel = !_.isEmpty(node.b_code);
  739. break;
  740. }
  741. }
  742. };
  743. ledgerTreeCol.initSpreadSetting(ledgerSpreadSetting);
  744. SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
  745. // 绑定事件
  746. ledgerSpread.bind(GC.Spread.Sheets.Events.SelectionChanged, treeOperationObj.selectionChanged);
  747. ledgerSpread.bind(spreadNS.Events.TopRowChanged, treeOperationObj.topRowChanged);
  748. if (!ledgerSpreadSetting.readOnly) {
  749. ledgerSpread.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  750. treeOperationObj.refreshOperationValid(info.sheet, info.newSelections);
  751. });
  752. ledgerSpread.bind(GC.Spread.Sheets.Events.EditEnded, treeOperationObj.editEnded);
  753. //ledgerSpread.bind(GC.Spread.Sheets.Events.ClipboardPasted, treeOperationObj.clipboardPasted);
  754. SpreadJsObj.addDeleteBind(ledgerSpread, treeOperationObj.deletePress);
  755. ledgerSpread.bind(GC.Spread.Sheets.Events.ClipboardChanging, function (e, info) {
  756. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  757. //console.log(info.copyData);
  758. SpreadJsObj.Clipboard.setCopyData(copyText);
  759. //copyToClipboard(copyText);
  760. });
  761. ledgerSpread.bind(GC.Spread.Sheets.Events.ClipboardPasting, treeOperationObj.clipboardPasting);
  762. ledgerSpread.bind(spreadNS.Events.EditStarting, treeOperationObj.editStarting);
  763. SpreadJsObj.addCutEvents(ledgerSpread, treeOperationObj.cut);
  764. // 绑定 删除等 顶部按钮
  765. $('#insert').click(() => {
  766. treeOperationObj.addNode(ledgerSpread.getActiveSheet());
  767. });
  768. $('#delete').click(function () {
  769. treeOperationObj.deleteNode(ledgerSpread.getActiveSheet());
  770. });
  771. $('#up-move').click(function () {
  772. treeOperationObj.upMove(ledgerSpread.getActiveSheet());
  773. });
  774. $('#down-move').click(function () {
  775. treeOperationObj.downMove(ledgerSpread.getActiveSheet());
  776. });
  777. $('#up-level').click(function () {
  778. treeOperationObj.upLevel(ledgerSpread.getActiveSheet());
  779. });
  780. $('#down-level').click(function () {
  781. treeOperationObj.downLevel(ledgerSpread.getActiveSheet());
  782. });
  783. $('#copy').click(function () {
  784. ledgerSpread.commandManager().execute({
  785. cmd: 'copy',
  786. sheetName: ledgerSpread.getActiveSheet().name()
  787. });
  788. });
  789. $('#paste').click(function () {
  790. ledgerSpread.commandManager().execute({
  791. cmd: 'paste',
  792. sheetName: ledgerSpread.getActiveSheet().name()
  793. });
  794. });
  795. $('#cut').click(function () {
  796. ledgerSpread.commandManager().execute({
  797. cmd: 'cut',
  798. sheetName: ledgerSpread.getActiveSheet().name()
  799. });
  800. });
  801. $('#bills-expr').bind('change mouseleave', function () {
  802. const expr = $(this);
  803. const sheet = ledgerSpread.getActiveSheet();
  804. const select = SpreadJsObj.getSelectObject(sheet);
  805. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val();
  806. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  807. const data = {
  808. id: select.id,
  809. tender_id: select.tender_id,
  810. ledger_id: select.ledger_id
  811. };
  812. const num = _.toNumber(newValue);
  813. if (num) {
  814. data[field] = num;
  815. } else {
  816. try {
  817. data[field] = math.evaluate(transExpr(newValue));
  818. } catch (err) {
  819. toastr.error('输入的表达式非法');
  820. return;
  821. }
  822. }
  823. // 更新至服务器
  824. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  825. const refreshNode = ledgerTree.loadPostData(result);
  826. expr.val(select[field]);
  827. treeOperationObj.refreshTree(sheet, refreshNode);
  828. });
  829. });
  830. let batchInsertObj;
  831. // 右键菜单
  832. $.contextMenu({
  833. selector: '#ledger-spread',
  834. build: function ($trigger, e) {
  835. const target = SpreadJsObj.safeRightClickSelection($trigger, e, ledgerSpread);
  836. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  837. },
  838. items: {
  839. 'create': {
  840. name: '新增',
  841. icon: 'fa-sign-in',
  842. callback: function (key, opt) {
  843. treeOperationObj.addNode(ledgerSpread.getActiveSheet());
  844. },
  845. disabled: function (key, opt) {
  846. const sheet = ledgerSpread.getActiveSheet();
  847. const selection = sheet.getSelections();
  848. const sel = selection ? selection[0] : sheet.getSelections()[0];
  849. const row = sel ? sel.row : -1;
  850. const tree = sheet.zh_tree;
  851. if (!tree) return true;
  852. const first = sheet.zh_tree.nodes[row];
  853. let last = first, sameParent = true;
  854. if (sel.rowCount > 1) {
  855. for (let r = 1; r < sel.rowCount; r++) {
  856. const rNode = tree.nodes[sel.row + r];
  857. if (rNode.level > first.level) continue;
  858. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  859. sameParent = false;
  860. break;
  861. }
  862. last = rNode;
  863. }
  864. }
  865. const valid = !sheet.zh_setting.readOnly;
  866. return !(valid && first && first.level > 1);
  867. }
  868. },
  869. 'delete': {
  870. name: '删除',
  871. icon: 'fa-remove',
  872. callback: function (key, opt) {
  873. treeOperationObj.deleteNode(ledgerSpread.getActiveSheet());
  874. },
  875. disabled: function (key, opt) {
  876. const sheet = ledgerSpread.getActiveSheet();
  877. const selection = sheet.getSelections();
  878. const sel = selection ? selection[0] : sheet.getSelections()[0];
  879. const row = sel ? sel.row : -1;
  880. const tree = sheet.zh_tree;
  881. if (!tree) return true;
  882. const first = sheet.zh_tree.nodes[row];
  883. let last = first, sameParent = true;
  884. if (sel.rowCount > 1) {
  885. for (let r = 1; r < sel.rowCount; r++) {
  886. const rNode = tree.nodes[sel.row + r];
  887. if (rNode.level > first.level) continue;
  888. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  889. sameParent = false;
  890. break;
  891. }
  892. last = rNode;
  893. }
  894. }
  895. const valid = !sheet.zh_setting.readOnly;
  896. return !(valid && first && sameParent && !(first.level === 1 && first.node_type));
  897. }
  898. },
  899. 'copyBlock': {
  900. name: '复制整块',
  901. icon: 'fa-files-o',
  902. callback: function (key, opt) {
  903. /*ledgerSpread.commandManager().execute({
  904. cmd:"copy",
  905. sheetName:ledgerSpread.getActiveSheet().name()
  906. });*/
  907. treeOperationObj.block = [];
  908. const copyBlockList = [];
  909. const sheet = ledgerSpread.getActiveSheet();
  910. const sel = sheet.getSelections()[0];
  911. let iRow = sel.row;
  912. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  913. while (iRow < sel.row + sel.rowCount) {
  914. const node = sheet.zh_tree.nodes[iRow];
  915. if (node.ledger_pid !== pid) {
  916. toastr.error('仅可同时选中同层节点');
  917. return;
  918. }
  919. copyBlockList.push(node.ledger_id);
  920. iRow += sheet.zh_tree.getPosterity(node).length + 1;
  921. }
  922. const tenderId = _.toInteger(getTenderId());
  923. setLocalCache(copyBlockTag, JSON.stringify({tid: tenderId, block: copyBlockList}));
  924. //treeOperationObj.block = copyBlockList;
  925. },
  926. visible: function (key, opt) {
  927. const sheet = ledgerSpread.getActiveSheet();
  928. const selection = sheet.getSelections();
  929. const row = selection[0].row;
  930. const select = ledgerTree.nodes[row];
  931. return select;
  932. }
  933. },
  934. 'pasteBlock': {
  935. name: '粘贴整块',
  936. icon: 'fa-clipboard',
  937. disabled: function (key, opt) {
  938. //const block = treeOperationObj.block || [];
  939. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  940. return !(copyInfo && copyInfo.tid && copyInfo.tid > 0 && copyInfo.block && copyInfo.block.length > 0);
  941. },
  942. callback: function (key, opt) {
  943. //const block = treeOperationObj.block || [];
  944. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  945. if (copyInfo.block.length > 0) {
  946. treeOperationObj.pasteBlock(ledgerSpread, copyInfo);
  947. } else {
  948. document.execCommand('paste');
  949. }
  950. }
  951. },
  952. 'batchInsertBillsPos': {
  953. name: '批量插入清单-部位',
  954. icon: 'fa-sign-in',
  955. disabled: function (key, opt) {
  956. if (!checkTzMeasureType()) return true;
  957. const sheet = ledgerSpread.getActiveSheet();
  958. const selection = sheet.getSelections();
  959. const row = selection[0].row;
  960. const select = ledgerTree.nodes[row];
  961. if (select) {
  962. if (select.code && select.code !== '') {
  963. return !ledgerTree.isLeafXmj(select);
  964. } else {
  965. const parent = ledgerTree.getParent(select);
  966. return !(parent && ledgerTree.isLeafXmj(parent));
  967. }
  968. } else {
  969. return false;
  970. }
  971. },
  972. callback: function (key, opt) {
  973. if (!batchInsertObj) {
  974. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  975. } else {
  976. batchInsertObj.initView();
  977. }
  978. $('#batch').modal('show');
  979. }
  980. },
  981. 'importExcel': {
  982. name: '导入分项清单Excel',
  983. icon: 'file-excel-o',
  984. disabled: function (key, opt) {
  985. return readOnly;
  986. },
  987. callback: function (key, opt) {
  988. $('#upload-ledger').modal('show');
  989. }
  990. }
  991. }
  992. });
  993. } else {
  994. SpreadJsObj.forbiddenSpreadContextMenu('#ledger-spread', ledgerSpread);
  995. }
  996. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  997. // 台账模式加载部位明细数据
  998. if (checkTzMeasureType()) {
  999. $.divResizer({
  1000. select: '#main-resize',
  1001. callback: function () {
  1002. ledgerSpread.refresh();
  1003. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1004. $(".sp-wrap").height(bcontent-40);
  1005. posSpread.refresh();
  1006. }
  1007. });
  1008. SpreadJsObj.initSheet(posSpread.getActiveSheet(), posSpreadSetting);
  1009. }
  1010. // 绑定部位明细编辑事件
  1011. const posOperationObj = {
  1012. editStarting: function (e, info) {
  1013. posOperationObj.ledgerTreeNode = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1014. },
  1015. /**
  1016. * 加载部位明细 根据当前台账选择节点
  1017. */
  1018. loadCurPosData: function () {
  1019. const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1020. if (node) {
  1021. const posData = pos.ledgerPos[itemsPre + node.id] || [];
  1022. SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', posData);
  1023. } else {
  1024. SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', []);
  1025. }
  1026. },
  1027. /**
  1028. * 编辑单元格响应事件
  1029. * @param {Object} e
  1030. * @param {Object} info
  1031. */
  1032. editEnded: function (e, info) {
  1033. if (info.sheet.zh_setting) {
  1034. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  1035. const col = info.sheet.zh_setting.cols[info.col];
  1036. const orgText = posData ? posData[col.field] : null;
  1037. const newText = info.sheet.getCell(info.row, info.col).text();
  1038. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) {
  1039. return;
  1040. }
  1041. //const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1042. const node = posOperationObj.ledgerTreeNode;
  1043. if (!node) {
  1044. toastr.error('数据错误,请选择台账节点后再试');
  1045. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1046. return;
  1047. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1048. toastr.error('父节点不可插入部位明细');
  1049. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1050. return;
  1051. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1052. toastr.error('项目节不可插入部位明细');
  1053. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1054. return;
  1055. }
  1056. const data = {};
  1057. if (col.field === 'name') {
  1058. if (newText === '' && posData) {
  1059. toastr.error('部位名称不可为空');
  1060. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1061. return;
  1062. } else if (!posData) {
  1063. if (newText && newText !== '') {
  1064. data.updateType = 'add';
  1065. const sortData = info.sheet.zh_data;
  1066. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1067. data.updateData = { name: newText, lid: node.id, tid: tender.id, porder: order};
  1068. } else {
  1069. return;
  1070. }
  1071. } else {
  1072. data.updateType = 'update';
  1073. data.updateData = {id: posData.id, name: newText};
  1074. }
  1075. } else if (!posData) {
  1076. toastr.warning('新增部位请先输入名称');
  1077. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1078. return;
  1079. } else {
  1080. data.updateType = 'update';
  1081. data.updateData = {id: posData.id};
  1082. if (col.type === 'Number') {
  1083. const num = _.toNumber(newText);
  1084. if (num) {
  1085. data.updateData[col.field] = num;
  1086. } else {
  1087. try {
  1088. data.updateData[col.field] = math.evaluate(transExpr(newText));
  1089. } catch(err) {
  1090. toastr.error('输入的表达式非法');
  1091. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1092. return;
  1093. }
  1094. }
  1095. } else {
  1096. data.updateData[col.field] = newText;
  1097. }
  1098. }
  1099. postData('/tender/' + getTenderId() + '/pos/update', data, function (result) {
  1100. const updateRst = pos.updateDatas(result.pos);
  1101. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1102. if (updateRst.create.length > 0) {
  1103. // info.sheet.addRows(info.row, 1);
  1104. // if (info.sheet.zh_data.length === 0) {
  1105. // info.sheet.zh_data = pos.ledgerPos[itemsPre + node.id] || [];
  1106. // }
  1107. // SpreadJsObj.reLoadRowData(info.sheet, info.sheet.zh_data.length - 1);
  1108. posOperationObj.loadCurPosData();
  1109. } else {
  1110. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1111. }
  1112. const loadResult = ledgerTree.loadPostData(result.ledger);
  1113. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1114. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1115. }, function () {
  1116. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1117. });
  1118. }
  1119. },
  1120. /**
  1121. * 删除按钮响应事件
  1122. * @param sheet
  1123. */
  1124. deletePress: function (sheet) {
  1125. const self = this;
  1126. if (sheet.zh_setting) {
  1127. const sortData = sheet.zh_data;
  1128. const datas = [], posSelects = [];
  1129. const sel = sheet.getSelections()[0];
  1130. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1131. let bDel = false;
  1132. const node = sortData[iRow];
  1133. if (node) {
  1134. const data = {id: node.id};
  1135. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1136. const colSetting = sheet.zh_setting.cols[iCol];
  1137. if (colSetting.field === 'name') {
  1138. toastr.error('部位名称不能为空');
  1139. return;
  1140. }
  1141. const style = sheet.getStyle(iRow, iCol);
  1142. if (!style.locked) {
  1143. const colSetting = sheet.zh_setting.cols[iCol];
  1144. data[colSetting.field] = null;
  1145. bDel = true;
  1146. }
  1147. }
  1148. if (bDel) {
  1149. datas.push(data);
  1150. posSelects.push(node);
  1151. }
  1152. }
  1153. }
  1154. if (datas.length > 0) {
  1155. postData('/tender/' + getTenderId() + '/pos/update', {updateType: 'update', updateData: datas}, function (result) {
  1156. pos.updateDatas(result.pos);
  1157. posOperationObj.loadCurPosData();
  1158. const loadResult = ledgerTree.loadPostData(result.ledger);
  1159. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1160. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1161. }, function () {
  1162. posOperationObj.loadCurPosData();
  1163. });
  1164. }
  1165. }
  1166. },
  1167. /**
  1168. * 删除 部位明细
  1169. * @param sheet
  1170. */
  1171. deletePos: function (sheet) {
  1172. const selection = sheet.getSelections();
  1173. const data = {
  1174. updateType: 'delete',
  1175. updateData: [],
  1176. };
  1177. const row = selection[0].row, count = selection[0].rowCount;
  1178. const sortData = sheet.zh_data;
  1179. for (let iRow = 0; iRow < count; iRow++) {
  1180. if (sortData[iRow + row]) {
  1181. data.updateData.push(sortData[iRow + row].id);
  1182. }
  1183. }
  1184. if (data.updateData.length > 0) {
  1185. postData('/tender/' + getTenderId() + '/pos/update', data, function (result) {
  1186. pos.removeDatas(result.pos);
  1187. sheet.deleteRows(row, count);
  1188. const loadResult = ledgerTree.loadPostData(result.ledger);
  1189. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1190. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1191. });
  1192. }
  1193. },
  1194. /**
  1195. * 粘贴单元格响应事件
  1196. * @param e
  1197. * @param info
  1198. */
  1199. clipboardPasted: function (e, info) {
  1200. const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1201. if (node.code && (node.code !== '')) {
  1202. toastr.error('项目节不可含有清单明细');
  1203. posOperationObj.loadCurPosData();
  1204. return;
  1205. }
  1206. if (node.children && (node.children.length > 0)) {
  1207. toastr.error('仅清单子项可以含有部位明细');
  1208. posOperationObj.loadCurPosData();
  1209. return;
  1210. }
  1211. if (info.sheet.zh_setting) {
  1212. const data = [];
  1213. const sortData = info.sheet.zh_data || [];
  1214. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1215. if (info.cellRange.col !== 0) {
  1216. toastr.warning('新增部位请先输入名称');
  1217. posOperationObj.loadCurPosData();
  1218. return;
  1219. }
  1220. }
  1221. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1222. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1223. let bPaste = false;
  1224. const curRow = info.cellRange.row + iRow;
  1225. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1226. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1227. const curCol = info.cellRange.col + iCol;
  1228. const colSetting = info.sheet.zh_setting.cols[curCol];
  1229. posData[colSetting.field] = info.sheet.getText(curRow, curCol);
  1230. if (colSetting.type === 'Number') {
  1231. const num = _.toNumber(posData[colSetting.field]);
  1232. if (num) {
  1233. posData[colSetting.field] = num;
  1234. bPaste = true;
  1235. } else {
  1236. try {
  1237. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1238. bPaste = true;
  1239. } catch(err) {
  1240. toastr.warning('粘贴了表达式非法,已过滤');
  1241. }
  1242. }
  1243. }
  1244. }
  1245. if (bPaste) {
  1246. data.push(posData);
  1247. }
  1248. }
  1249. if (data.length > 0) {
  1250. postData('/tender/' + getTenderId() + '/pos/paste', data, function (result) {
  1251. pos.updateDatas(result.pos);
  1252. posOperationObj.loadCurPosData();
  1253. const loadResult = ledgerTree.loadPostData(result.ledger);
  1254. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1255. posOperationObj.loadCurPosData();
  1256. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1257. }, function () {
  1258. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1259. });
  1260. } else {
  1261. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1262. }
  1263. }
  1264. },
  1265. selectionChanged: function (e, info) {
  1266. const col = info.sheet.zh_setting.cols[info.newSelections[0].col];
  1267. const cell = info.sheet.getCell(info.newSelections[0].row, info.newSelections[0].col);
  1268. if (col.type === 'Number') {
  1269. const data = SpreadJsObj.getSelectObject(info.sheet);
  1270. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  1271. .attr('row', info.newSelections[0].row).attr('readOnly', readOnly || cell.locked());
  1272. } else {
  1273. $('#pos-expr').val('').attr('readOnly', true);
  1274. }
  1275. },
  1276. };
  1277. posSpread.bind(spreadNS.Events.SelectionChanged, posOperationObj.selectionChanged)
  1278. if (!posSpreadSetting.readOnly) {
  1279. $('#pos-expr').bind('change mouseleave', function () {
  1280. const expr = $(this);
  1281. const posSheet = posSpread.getActiveSheet();
  1282. const select = SpreadJsObj.getSelectObject(posSheet);
  1283. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val(), row = expr.attr('row');
  1284. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  1285. const data = {id: select.id};
  1286. const num = _.toNumber(newValue);
  1287. if (num) {
  1288. data[field] = num;
  1289. } else {
  1290. try {
  1291. data[field] = math.evaluate(transExpr(newValue));
  1292. } catch (err) {
  1293. toastr.error('输入的表达式非法');
  1294. return;
  1295. }
  1296. }
  1297. // 更新至服务器
  1298. postData('/tender/' + getTenderId() + '/pos/update', {updateType: 'update', updateData: data}, function (result) {
  1299. const updateRst = pos.updateDatas(result.pos);
  1300. expr.val(select[field]);
  1301. SpreadJsObj.reLoadRowData(posSheet, _.toNumber(row));
  1302. const loadResult = ledgerTree.loadPostData(result.ledger);
  1303. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1304. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1305. });
  1306. });
  1307. SpreadJsObj.addDeleteBind(posSpread, posOperationObj.deletePress);
  1308. posSpread.bind(spreadNS.Events.EditStarting, posOperationObj.editStarting);
  1309. posSpread.bind(spreadNS.Events.EditEnded, posOperationObj.editEnded);
  1310. posSpread.bind(spreadNS.Events.ClipboardPasted, posOperationObj.clipboardPasted);
  1311. // 右键菜单
  1312. $.contextMenu({
  1313. selector: '#pos-spread',
  1314. build: function ($trigger, e) {
  1315. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1316. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1317. },
  1318. items: {
  1319. 'delete': {
  1320. name: '删除',
  1321. icon: 'fa-remove',
  1322. disabled: function (key, opt) {
  1323. const sheet = posSpread.getActiveSheet();
  1324. if (sheet.zh_data) {
  1325. const selection = sheet.getSelections();
  1326. return sheet.zh_data.length < selection[0].row + selection[0].rowCount;
  1327. } else {
  1328. return true;
  1329. }
  1330. },
  1331. callback: function (key, opt) {
  1332. posOperationObj.deletePos(posSpread.getActiveSheet());
  1333. }
  1334. },
  1335. }
  1336. });
  1337. } else {
  1338. SpreadJsObj.forbiddenSpreadContextMenu('#pos-spread', posSpread);
  1339. }
  1340. postData(window.location.pathname + '/load', {}, function (data) {
  1341. ledgerTree.loadDatas(data.bills);
  1342. treeCalc.calculateAll(ledgerTree);
  1343. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
  1344. SpreadJsObj.loadTopAndSelect(ledgerSpread.getActiveSheet(), ckBillsSpread);
  1345. pos.loadDatas(data.pos);
  1346. posOperationObj.loadCurPosData();
  1347. SpreadJsObj.resetTopAndSelect(posSpread.getActiveSheet());
  1348. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1349. treeOperationObj.loadExprToInput(ledgerSpread.getActiveSheet());
  1350. }, null, true);
  1351. let stdXmj, stdGcl, dealBills, searchLedger;
  1352. $.divResizer({
  1353. select: '#right-spr',
  1354. callback: function () {
  1355. ledgerSpread.refresh();
  1356. if (posSpread) {
  1357. posSpread.refresh();
  1358. }
  1359. if (stdXmj) {
  1360. stdXmj.spread.refresh();
  1361. }
  1362. if (stdGcl) {
  1363. stdGcl.spread.refresh();
  1364. }
  1365. if (dealBills) {
  1366. dealBills.spread.refresh();
  1367. }
  1368. if (searchLedger) {
  1369. searchLedger.spread.refresh();
  1370. }
  1371. }
  1372. });
  1373. // 展开收起标准清单
  1374. $('a', '#side-menu').bind('click', function (e) {
  1375. e.preventDefault();
  1376. const tab = $(this), tabPanel = $(tab.attr('content'));
  1377. const showSideTools = function (show) {
  1378. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1379. if (show) {
  1380. right.show();
  1381. autoFlashHeight();
  1382. /**
  1383. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1384. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1385. * 故需要通过最终的parent.width再计算一次left.width
  1386. *
  1387. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1388. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1389. *
  1390. */
  1391. //left.css('width', parent.width() - right.outerWidth());
  1392. //left.css('width', parent.width() - right.outerWidth());
  1393. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1394. left.css('width', percent + '%');
  1395. } else {
  1396. left.width(parent.width());
  1397. right.hide();
  1398. }
  1399. };
  1400. // 展开工具栏、切换标签
  1401. if (!tab.hasClass('active')) {
  1402. const close = $('.active', '#side-menu').length === 0;
  1403. $('a', '#side-menu').removeClass('active');
  1404. tab.addClass('active');
  1405. $('.tab-content .tab-pane').removeClass('active');
  1406. tabPanel.addClass('active');
  1407. showSideTools(tab.hasClass('active'));
  1408. if (tab.attr('content') === '#std-xmj') {
  1409. if (!stdXmj) {
  1410. stdXmj = new stdLib('#std-xmj', 'xmj', {
  1411. id: 'chapter_id',
  1412. pid: 'pid',
  1413. order: 'order',
  1414. level: 'level',
  1415. rootId: -1,
  1416. keys: ['id', 'list_id', 'chapter_id'],
  1417. }, {
  1418. cols: [
  1419. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1420. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1421. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1422. ],
  1423. treeCol: 0,
  1424. emptyRows: 0,
  1425. headRows: 1,
  1426. headRowHeight: [40],
  1427. defaultRowHeight: 21,
  1428. });
  1429. stdXmj.loadLib($('select', '#std-xmj').val());
  1430. }
  1431. stdXmj.spread.refresh();
  1432. } else if (tab.attr('content') === '#std-gcl') {
  1433. if (!stdGcl) {
  1434. stdGcl = new stdLib('#std-gcl', 'gcl', {
  1435. id: 'bill_id',
  1436. pid: 'pid',
  1437. order: 'order',
  1438. level: 'level',
  1439. rootId: -1,
  1440. keys: ['id', 'list_id', 'bill_id']
  1441. }, {
  1442. cols: [
  1443. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1444. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1445. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1446. ],
  1447. treeCol: 0,
  1448. emptyRows: 0,
  1449. headRows: 1,
  1450. headRowHeight: [40],
  1451. defaultRowHeight: 21,
  1452. });
  1453. stdGcl.loadLib($('select', '#std-gcl').val());
  1454. }
  1455. stdGcl.spread.refresh();
  1456. } else if (tab.attr('content') === '#deal-bills') {
  1457. if (!dealBills) {
  1458. dealBills = new DealBills('#deal-bills-spread', {
  1459. cols: [
  1460. {title: '清单编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1461. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1462. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1463. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1464. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1465. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1466. ],
  1467. emptyRows: 0,
  1468. headRows: 1,
  1469. headRowHeight: [40],
  1470. defaultRowHeight: 21,
  1471. });
  1472. dealBills.loadData();
  1473. }
  1474. dealBills.spread.refresh();
  1475. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1476. if (!searchLedger) {
  1477. searchLedger = $.billsSearch({
  1478. selector: '#search',
  1479. searchSpread: ledgerSpread,
  1480. resultSpreadSetting: {
  1481. cols: [
  1482. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1483. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1484. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  1485. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1486. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1487. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1488. ],
  1489. emptyRows: 0,
  1490. headRows: 1,
  1491. headRowHeight: [40],
  1492. defaultRowHeight: 21,
  1493. },
  1494. afterLocated: function () {
  1495. posOperationObj.loadCurPosData();
  1496. }
  1497. });
  1498. }
  1499. searchLedger.spread.refresh();
  1500. }
  1501. } else { // 收起工具栏
  1502. tab.removeClass('active');
  1503. tabPanel.removeClass('active');
  1504. showSideTools(tab.hasClass('active'));
  1505. }
  1506. ledgerSpread.refresh();
  1507. if (posSpread) {
  1508. posSpread.refresh();
  1509. }
  1510. });
  1511. class stdLib {
  1512. constructor(selector, stdType, treeSetting, spreadSetting) {
  1513. const self = this;
  1514. this.obj = $(selector + '-spread')[0];
  1515. this.stdType = stdType;
  1516. this.treeSetting = treeSetting;
  1517. treeSetting.preUrl = this.url;
  1518. this.spreadSetting = spreadSetting;
  1519. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1520. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1521. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1522. this.spread.getActiveSheet().bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1523. const stdSheet = info.sheet;
  1524. const mainSheet = ledgerSpread.getActiveSheet();
  1525. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1526. const stdTree = stdSheet.zh_tree;
  1527. const stdNode = stdTree.nodes[info.row];
  1528. const mainTree = mainSheet.zh_tree;
  1529. const sel = mainSheet.getSelections()[0];
  1530. const mainNode = mainTree.nodes[sel.row];
  1531. if (!stdNode) { return; }
  1532. if (stdType === 'gcl') {
  1533. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1534. toastr.warning('非最底层项目下,不应添加清单');
  1535. return;
  1536. }
  1537. }
  1538. postData(window.location.pathname + '/update', {
  1539. postType: 'add-std',
  1540. postData: {
  1541. id: ledgerTree.getNodeKey(mainNode),
  1542. tender_id: mainNode.tender_id,
  1543. stdType: stdType,
  1544. stdLibId: stdNode.list_id,
  1545. stdNode: stdTree.getNodeKey(stdNode)
  1546. }
  1547. }, function (result) {
  1548. const refreshNode = mainTree.loadPostData(result);
  1549. treeOperationObj.refreshTree(mainSheet, refreshNode);
  1550. treeOperationObj.refreshOperationValid(mainSheet);
  1551. });
  1552. });
  1553. this.pathTree = createNewPathTree('base', this.treeSetting);
  1554. this.cacheLib = [];
  1555. $('select', selector).change(function () {
  1556. self.loadLib(parseInt(this.value));
  1557. });
  1558. }
  1559. loadLib (listId) {
  1560. const cacheData = this.cacheLib.find(function (lib) {
  1561. return lib.id === listId;
  1562. });
  1563. if (cacheData) {
  1564. this.pathTree.loadDatas(cacheData.data);
  1565. SpreadJsObj.loadSheetData(this.spread.getActiveSheet(), 'tree', this.pathTree);
  1566. } else {
  1567. const self = this;
  1568. postData('/std-lib/get-data', {stdType: this.stdType, list_id: listId}, function (data) {
  1569. self.cacheLib.push({id: listId, data: data});
  1570. self.pathTree.loadDatas(data);
  1571. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'tree', self.pathTree);
  1572. });
  1573. }
  1574. }
  1575. }
  1576. class DealBills {
  1577. constructor (selector, spreadSetting) {
  1578. const self = this;
  1579. this.obj = $(selector)[0];
  1580. this.url = '/tender/' + getTenderId() + '/deal';
  1581. this.spreadSetting = spreadSetting;
  1582. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1583. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1584. if (!readOnly) {
  1585. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1586. const dealSheet = info.sheet;
  1587. const mainSheet = ledgerSpread.getActiveSheet();
  1588. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1589. if (!dealBills) { return; }
  1590. const mainTree = mainSheet.zh_tree;
  1591. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1592. if (!mainNode || !mainTree) { return; }
  1593. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1594. toastr.error('非最底层项目下,不应添加清单');
  1595. return;
  1596. }
  1597. postData(window.location.pathname + '/update', {
  1598. postType: 'add-deal',
  1599. postData: {
  1600. id: mainNode.ledger_id,
  1601. type: mainNode.code ? 'child' : 'next',
  1602. dealBills: {
  1603. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1604. unit_price: dealBills.unit_price,
  1605. }
  1606. },
  1607. }, function (result) {
  1608. const refreshData = mainTree.loadPostData(result);
  1609. treeOperationObj.refreshTree(mainSheet, refreshData);
  1610. treeOperationObj.refreshOperationValid(mainSheet);
  1611. });
  1612. });
  1613. }
  1614. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1615. $('#upload-deal-bills').click(function () {
  1616. const file = $('#deal-bills-file')[0];
  1617. const formData = new FormData();
  1618. formData.append('file', file.files[0]);
  1619. postDataWithFile(self.url+'/upload-excel', formData, function (data) {
  1620. self.data = data;
  1621. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1622. $('#upload-deal').modal('hide');
  1623. }, function () {
  1624. $('#upload-deal').modal('hide');
  1625. });
  1626. });
  1627. }
  1628. loadData () {
  1629. const self = this;
  1630. postData(this.url+'/get-data', {}, function (data) {
  1631. self.data = data;
  1632. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1633. });
  1634. }
  1635. calculateData () {
  1636. if (this.data) {
  1637. for (const d of this.data) {
  1638. d.total_price = _.multiply(d.quantity, d.unit_price);
  1639. }
  1640. }
  1641. }
  1642. }
  1643. class BatchInsertBillsPosObj {
  1644. constructor (obj) {
  1645. const self = this;
  1646. this.obj = obj;
  1647. this.billsCount = 6;
  1648. this.posCount = 1000;
  1649. // 初始化 清单编号窗口 参数
  1650. this.qdSpreadSetting = {
  1651. cols: [
  1652. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1653. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1654. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1655. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1656. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1657. ],
  1658. emptyRows: this.billsCount,
  1659. headRows: 1,
  1660. headRowHeight: [40],
  1661. };
  1662. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1663. // 初始化 部位数量复核表 参数
  1664. this.posSpreadSetting = {
  1665. cols: [
  1666. {title: '部位', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1667. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1668. ],
  1669. emptyRows: this.posCount,
  1670. headRows: 1,
  1671. headRowHeight: [40],
  1672. };
  1673. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1674. this.posSpreadSetting.cols.push(
  1675. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1676. )
  1677. }
  1678. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1679. // 初始化 签约清单 参数
  1680. this.dealSpreadSetting = {
  1681. cols: [
  1682. {title: '清单编号', field: 'code', width: 80, formatter: '@', readOnly: true},
  1683. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true},
  1684. {title: '单位', field: 'unit', width: 50, formatter: '@', readOnly: true},
  1685. {title: '单价', field: 'unit_price', width: 50, readOnly: true},
  1686. ],
  1687. emptyRows: 0,
  1688. headRows: 1,
  1689. headRowHeight: [40],
  1690. };
  1691. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1692. // 初始化 清单编号、部位数量复核表 表格
  1693. this.initView();
  1694. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1695. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1696. $.contextMenu({
  1697. selector: '.batch-l-t',
  1698. build: function ($trigger, e) {
  1699. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  1700. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1701. },
  1702. items: {
  1703. 'create': {
  1704. name: '新增行',
  1705. icon: 'fa-sign-in',
  1706. callback: function (key, opt) {
  1707. const qdSheet = self.qdSpread.getActiveSheet();
  1708. const posSheet = self.posSpread.getActiveSheet();
  1709. qdSheet.addRows(qdSheet.getRowCount(), 1);
  1710. const index = qdSheet.getRowCount();
  1711. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  1712. posSheet.addColumns(posSheet.getColumnCount(), 1);
  1713. posSheet.getCell(0, index + 2, spreadNS.SheetArea.colHeader).text('清单' + index);
  1714. },
  1715. },
  1716. 'delete': {
  1717. name: '删除行',
  1718. icon: 'fa-remove',
  1719. callback: function (key, opt) {
  1720. const qdSheet = self.qdSpread.getActiveSheet();
  1721. const posSheet = self.posSpread.getActiveSheet();
  1722. const sel = qdSheet.getSelections()[0];
  1723. qdSheet.deleteRows(sel.row, sel.rowCount);
  1724. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  1725. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  1726. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  1727. }
  1728. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  1729. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  1730. }
  1731. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  1732. },
  1733. },
  1734. }
  1735. });
  1736. // 拉取签约清单数据
  1737. if (dealBills) {
  1738. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1739. } else {
  1740. postData('/tender/' + getTenderId() + '/deal/get-data', {}, function (data) {
  1741. SpreadJsObj.loadSheetData(self.dealSpread.getActiveSheet(), 'data', data);
  1742. });
  1743. }
  1744. // 双击签约清单,自动添加到清单编号窗口
  1745. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1746. const deal = info.sheet.zh_data[info.row];
  1747. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1748. const sel = qdSheet.getSelections()[0];
  1749. qdSheet.getCell(sel.row, 0).value(deal.code);
  1750. qdSheet.getCell(sel.row, 1).value(deal.name);
  1751. qdSheet.getCell(sel.row, 2).value(deal.unit);
  1752. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  1753. if (sel.row + 1 === qdSheet.getRowCount()) {
  1754. const count = sel.row + 2;
  1755. qdSheet.setRowCount(count);
  1756. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  1757. const colCount = posSheet.getColumnCount() + 1
  1758. posSheet.setColumnCount(colCount);
  1759. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('数量' + count);
  1760. }
  1761. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  1762. });
  1763. this.obj.bind('shown.bs.modal', function () {
  1764. self.qdSpread.refresh();
  1765. self.posSpread.refresh();
  1766. self.dealSpread.refresh();
  1767. });
  1768. $('#batch-ok').click(function () {
  1769. const sheet = ledgerSpread.getActiveSheet();
  1770. const selection = sheet.getSelections();
  1771. const row = selection[0].row;
  1772. const select = ledgerTree.nodes[row];
  1773. if (select) {
  1774. const insertData = {};
  1775. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  1776. insertData.id = ledgerTree.getNodeKey(select);
  1777. insertData.batchData = self.getBatchData();
  1778. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  1779. pos.updateDatas(data.pos);
  1780. const result = ledgerTree.loadPostData(data.ledger);
  1781. treeOperationObj.refreshTree(sheet, result);
  1782. treeOperationObj.refreshOperationValid(sheet, selection);
  1783. self.obj.modal('hide');
  1784. }, null, true);
  1785. }
  1786. });
  1787. }
  1788. // 初始化左侧表格
  1789. initView () {
  1790. // 初始化 清单编号
  1791. const qdSheet = this.qdSpread.getActiveSheet();
  1792. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  1793. SpreadJsObj.refreshColumnAlign(qdSheet);
  1794. // 清理原有数据
  1795. SpreadJsObj.beginMassOperation(qdSheet);
  1796. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1797. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  1798. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  1799. }
  1800. qdSheet.setSelection(0, 0, 1 ,1);
  1801. SpreadJsObj.endMassOperation(qdSheet);
  1802. // 初始化 部位数量复核表
  1803. const posSheet = this.posSpread.getActiveSheet();
  1804. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  1805. SpreadJsObj.refreshColumnAlign(posSheet);
  1806. // 清理原有数据
  1807. SpreadJsObj.beginMassOperation(posSheet);
  1808. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  1809. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1810. posSheet.setSelection(0, 0, 1 ,1);
  1811. SpreadJsObj.endMassOperation(posSheet);
  1812. // 检查签约清单数据,以工具栏数据为准
  1813. if (dealBills) {
  1814. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1815. }
  1816. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  1817. }
  1818. // 获取界面数据
  1819. getBatchData () {
  1820. const result = [];
  1821. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  1822. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  1823. if (qdSheet.getText(iRow, 0) === '') { continue; }
  1824. const qd = {
  1825. b_code: qdSheet.getText(iRow, 0),
  1826. name: qdSheet.getText(iRow, 1),
  1827. unit: qdSheet.getText(iRow, 2),
  1828. price: _.toNumber(qdSheet.getText(iRow, 3)),
  1829. pos: [],
  1830. };
  1831. result.push(qd);
  1832. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  1833. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  1834. if (value !== 0 && !isNaN(value)) {
  1835. qd.pos.push({
  1836. name: posSheet.getText(iPosRow, 0),
  1837. drawing_code: posSheet.getText(iPosRow, 1),
  1838. quantity: value, porder: qd.pos.length + 1,
  1839. });
  1840. }
  1841. }
  1842. }
  1843. return result;
  1844. }
  1845. }
  1846. $('#searchAccount').click(() => {
  1847. const data = {
  1848. keyword: $('#searchName').val(),
  1849. };
  1850. postData('/search/user', data, (data) => {
  1851. const resultDiv = $('#searchResult');
  1852. if (data) {
  1853. $('h5>span', resultDiv).text(data.name);
  1854. $('#addAuditor').attr('auditorId', data.id);
  1855. $('h6', resultDiv).text(data.role);
  1856. $('p', resultDiv).text(data.company);
  1857. resultDiv.show();
  1858. } else {
  1859. toastr.info('未查询到该审核人');
  1860. resultDiv.hide();
  1861. }
  1862. }, () => {
  1863. $('#searchResult').hide();
  1864. });
  1865. });
  1866. // 审批人分组选择
  1867. $('#account_group').change(function () {
  1868. let account_html = '<option value="0">选择审批人</option>';
  1869. for (const account of accountList) {
  1870. if (parseInt($(this).val()) === 0 || parseInt($(this).val()) === account.account_group) {
  1871. const role = account.role !== '' ? '(' + account.role + ')' : '';
  1872. const company = account.company !== '' ? ' -' + account.company : '';
  1873. account_html += '<option value="' + account.id + '">' + account.name + role + company + '</option>';
  1874. }
  1875. }
  1876. $('#account_list').html(account_html);
  1877. });
  1878. // 添加到审批流程中
  1879. $('body').on('change', '#account_list', function () {
  1880. let id = $(this).val();
  1881. id = parseInt(id);
  1882. if (id !== 0) {
  1883. const data = {
  1884. auditorId: $(this).val(),
  1885. };
  1886. postData('/tender/' + getTenderId() + '/ledger/audit/add', data, (data) => {
  1887. const html = [];
  1888. html.push('<li class="list-group-item" auditorId="' + data.audit_id + '"><a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
  1889. html.push('<span>');
  1890. html.push(data.audit_order + ' ');
  1891. html.push(data.name + ' ');
  1892. html.push('</span>');
  1893. html.push('<small class="text-muted">');
  1894. html.push(data.role);
  1895. html.push('</small></li>');
  1896. $('#auditors').append(html.join(''));
  1897. // 如果是重新上报,添加到重新上报列表中
  1898. const auditorshtml = [];
  1899. // 重新上报时。令其它的审批人流程图标转换
  1900. $('#auditors-list li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
  1901. // 添加新审批人
  1902. auditorshtml.push('<li class="list-group-item" data-auditid="' + data.audit_id + '">');
  1903. auditorshtml.push('<i class="fa fa-stop-circle"></i> ');
  1904. auditorshtml.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
  1905. auditorshtml.push('</li>');
  1906. $('#auditors-list').append(auditorshtml.join(''));
  1907. const auditorshtml2 = [];
  1908. // 重新上报时。令其它的审批人流程图标转换
  1909. $('#auditors-list2 li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
  1910. // 添加新审批人
  1911. auditorshtml2.push('<li class="list-group-item" data-auditid="' + data.audit_id + '">');
  1912. auditorshtml2.push('<h5 class="card-title"><i class="fa fa-stop-circle"></i> ');
  1913. auditorshtml2.push(data.name + ' <small class="text-muted">' + data.role + '</small></h5>');
  1914. auditorshtml2.push('</li>');
  1915. $('#auditors-list2').append(auditorshtml2.join(''));
  1916. });
  1917. }
  1918. });
  1919. $('body').on('click', '#auditors li>a', function () {
  1920. const li = $(this).parent();
  1921. const data = {
  1922. auditorId: parseInt(li.attr('auditorId')),
  1923. };
  1924. postData('/tender/' + getTenderId() + '/ledger/audit/delete', data, (data2) => {
  1925. li.remove();
  1926. for (const a of data2) {
  1927. const aLi = $('li[auditorId=' + a.audit_id + ']');
  1928. $('span', aLi).text(a.audit_order + ' ' + a.name + ' ');
  1929. }
  1930. // 如果是重新上报
  1931. // 令最后一个图标转换
  1932. $('#auditors-list li[data-auditid="' + data.auditorId + '"]').remove();
  1933. if ($('#auditors-list li').length !== 0 && !$('#auditors-list li i').hasClass('fa-stop-circle')) {
  1934. $('#auditors-list li').eq($('#auditors-list li').length-1).children('i')
  1935. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  1936. }
  1937. $('#auditors-list2 li[data-auditid="' + data.auditorId + '"]').remove();
  1938. if ($('#auditors-list2 li').length !== 0 && !$('#auditors-list2 li i').hasClass('fa-stop-circle')) {
  1939. $('#auditors-list2 li').eq($('#auditors-list2 li').length-1).children('i')
  1940. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  1941. }
  1942. });
  1943. });
  1944. // 选择excel文件后,加载全部sheet
  1945. $('#upload-ledger-file').change(function () {
  1946. if (this.files.length === 0) {
  1947. $('#upload-ledger-sheets').html('').hide();
  1948. return;
  1949. }$('#upload-ledger-sheets').hide();
  1950. try {
  1951. $('#select-excel-loading').show();
  1952. xlsxUtils.import(this.files[0], (excelData) => {
  1953. if (excelData.SheetNames.length > 0) {
  1954. const html = [];
  1955. html.push('<hr></hr>');
  1956. html.push('<h6>选择导入的工作表</h6>');
  1957. for (const iName in excelData.SheetNames) {
  1958. const name = excelData.SheetNames[iName];
  1959. html.push('<div class="card p-2 mb-2">');
  1960. html.push('<div class="form-check">');
  1961. html.push('<input class="form-check-input" type="radio" name="sheetName" id="excel-sheet' + iName + '"', (iName == 0 ? ' checked=""' : ''), ' value="' + name + '"', '>');
  1962. html.push('<label class="form-check-label" for="excel-sheet' + iName + '">', name, '</label>');
  1963. html.push('</div>');
  1964. html.push('</div>');
  1965. }
  1966. $('#upload-ledger-sheets').html(html.join('')).show();
  1967. $('.mb-2.p-2').mouseenter(function () {
  1968. $(this).addClass('border-primary');
  1969. });
  1970. $('.mb-2.p-2').mouseleave(function () {
  1971. $(this).removeClass('border-primary');
  1972. })
  1973. } else {
  1974. toastr.info('选择的Excel无有效数据,请重新选择');
  1975. $('#upload-ledger-sheets').hide();
  1976. }
  1977. $('#select-excel-loading').hide();
  1978. });
  1979. } catch(err) {
  1980. $('#select-excel-loading').hide();
  1981. toastr.error('加载excel异常,请刷新当前页面');
  1982. $('#upload-ledger-sheets').hide();
  1983. }
  1984. });
  1985. // 上传excel内容,并导入
  1986. $('#upload-ledger-ok').click(function () {
  1987. const sheetName = $('input[name=sheetName]:checked').val();
  1988. if (sheetName) {
  1989. const sheet = {
  1990. rows: xlsxUtils.getSheetByName(sheetName, {header: 1}),
  1991. merge: xlsxUtils._wb.Sheets[sheetName]["!merges"]
  1992. };
  1993. postDataCompress(window.location.pathname + '/upload-excel', sheet, function (result) {
  1994. ledgerTree.loadDatas(result.bills);
  1995. treeCalc.calculateAll(ledgerTree);
  1996. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
  1997. pos.loadDatas(result.pos);
  1998. posOperationObj.loadCurPosData();
  1999. $('#upload-ledger').modal('hide');
  2000. }, null, true);
  2001. }
  2002. });
  2003. $('#upload-ledger').bind('hidden.bs.modal', function () {
  2004. $('#upload-ledger-file').val('');
  2005. $('#upload-ledger-sheets').html('');
  2006. });
  2007. $('#hideSp').click(function () {
  2008. $('#sp-list2').modal('hide');
  2009. });
  2010. // 显示层次
  2011. (function (select, sheet) {
  2012. $(select).click(function () {
  2013. const tag = $(this).attr('tag');
  2014. const tree = sheet.zh_tree;
  2015. if (!tree) return;
  2016. switch (tag) {
  2017. case "1":
  2018. case "2":
  2019. case "3":
  2020. case "4":
  2021. case "5":
  2022. tree.expandByLevel(parseInt(tag));
  2023. SpreadJsObj.refreshTreeRowVisible(sheet);
  2024. break;
  2025. case "last":
  2026. tree.expandByCustom(() => { return true; });
  2027. SpreadJsObj.refreshTreeRowVisible(sheet);
  2028. break;
  2029. case "leafXmj":
  2030. tree.expandToLeafXmj();
  2031. SpreadJsObj.refreshTreeRowVisible(sheet);
  2032. break;
  2033. }
  2034. });
  2035. })('a[name=showLevel]', ledgerSpread.getActiveSheet());
  2036. });
  2037. // 检查上报情况
  2038. function checkAuditorFrom () {
  2039. if ($('#auditors li').length === 0) {
  2040. toastr.error('请先选择审批人,再上报数据');
  2041. return false;
  2042. }
  2043. $('#hide-all').show();
  2044. }