revise.js 68 KB

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