revise.js 71 KB

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