revise.js 65 KB

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