revise.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. const sel = sheet.getSelections()[0];
  504. if (sel) {
  505. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  506. }
  507. self.refreshOperationValid(sheet);
  508. removeLocalCache(copyBlockTag);
  509. }, null, true);
  510. },
  511. topRowChanged: function (e, info) {
  512. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  513. },
  514. editStarting(e, info) {
  515. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  516. const col = info.sheet.zh_setting.cols[info.col];
  517. const node = info.sheet.zh_tree.nodes[info.row];
  518. switch (col.field) {
  519. case 'unit_price':
  520. info.cancel = (node.children && node.children.length > 0) || node.used;
  521. break;
  522. case 'sgfh_qty':
  523. case 'sgfh_tp':
  524. case 'sjcl_qty':
  525. case 'sjcl_tp':
  526. case 'qtcl_qty':
  527. case 'qtcl_tp':
  528. case 'deal_qty':
  529. case 'deal_tp':
  530. info.cancel = (node.children && node.children.length > 0);
  531. break;
  532. case 'dgn_qty1':
  533. case 'dgn_qty2':
  534. info.cancel = !_.isEmpty(node.b_code);
  535. break;
  536. }
  537. }
  538. };
  539. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  540. billsTreeSpreadObj.loadExprToInput(billsSheet);
  541. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  542. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  543. if (!readOnly) {
  544. // 增删上下移升降级
  545. $('a[name="base-opr"]').click(function () {
  546. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  547. });
  548. $('#bills-expr').bind('change mouseleave', function () {
  549. const expr = $(this);
  550. const select = SpreadJsObj.getSelectObject(billsSheet);
  551. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val();
  552. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  553. const data = {
  554. id: select.id,
  555. tender_id: select.tender_id,
  556. ledger_id: select.ledger_id
  557. };
  558. const num = _.toNumber(newValue);
  559. if (num) {
  560. data[field] = num;
  561. } else {
  562. try {
  563. data[field] = math.evaluate(transExpr(newValue));
  564. } catch (err) {
  565. toastr.error('输入的表达式非法');
  566. return;
  567. }
  568. }
  569. // 更新至服务器
  570. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  571. const refreshNode = billsTree.loadPostData(result);
  572. expr.val(select[field]);
  573. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  574. });
  575. });
  576. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  577. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  578. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  579. billsSpread.bind(spreadNS.Events.ClipboardPasted, billsTreeSpreadObj.clipboardPasted);
  580. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  581. let batchInsertObj;
  582. // 右键菜单
  583. $.contextMenu({
  584. selector: '#bills-spread',
  585. build: function ($trigger, e) {
  586. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  587. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  588. },
  589. items: {
  590. 'batchInsertBillsPos': {
  591. name: '批量插入清单-部位',
  592. icon: 'fa-sign-in',
  593. disabled: function (key, opt) {
  594. const select = SpreadJsObj.getSelectObject(billsSheet);
  595. if (select) {
  596. if (select.code && select.code !== '') {
  597. return !billsTree.isLeafXmj(select);
  598. } else {
  599. const parent = billsTree.getParent(select);
  600. return !(parent && billsTree.isLeafXmj(parent));
  601. }
  602. } else {
  603. return false;
  604. }
  605. },
  606. callback: function (key, opt) {
  607. if (!batchInsertObj) {
  608. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  609. } else {
  610. batchInsertObj.initView();
  611. }
  612. $('#batch').modal('show');
  613. }
  614. },
  615. 'debug': {
  616. name: 'debug',
  617. callback: function (key, opt) {
  618. console.log(SpreadJsObj.getSelectObject(billsSheet));
  619. }
  620. }
  621. }
  622. });
  623. }
  624. const posSpreadObj = {
  625. /**
  626. * 加载计量单元 根据当前台账选择节点
  627. */
  628. loadCurPosData: function () {
  629. const node = SpreadJsObj.getSelectObject(billsSheet);
  630. if (node) {
  631. const posData = pos.getLedgerPos(node.id) || [];
  632. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  633. } else {
  634. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  635. }
  636. SpreadJsObj.resetFieldReadOnly(posSheet);
  637. },
  638. editStarting: function (e, info) {
  639. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  640. },
  641. /**
  642. * 编辑单元格响应事件
  643. * @param {Object} e
  644. * @param {Object} info
  645. */
  646. editEnded: function (e, info) {
  647. if (!info.sheet.zh_setting) {
  648. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  649. return;
  650. }
  651. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  652. const col = info.sheet.zh_setting.cols[info.col];
  653. const orgText = posData ? posData[col.field] : null;
  654. const newText = info.sheet.getCell(info.row, info.col).text();
  655. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  656. const node = posSpreadObj.billsNode;
  657. if (!node) {
  658. toastr.error('数据错误,请选择台账节点后再试');
  659. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  660. return;
  661. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  662. toastr.error('父节点不可插入计量单元');
  663. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  664. return;
  665. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  666. toastr.error('项目节不可插入计量单元');
  667. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  668. return;
  669. }
  670. const data = {postType: 'pos'};
  671. if (col.field === 'name') {
  672. if (newText === '' && posData) {
  673. toastr.error('部位名称不可为空', 'error');
  674. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  675. return;
  676. } else if (!posData) {
  677. if (newText && newText !== '') {
  678. data.posPostType = 'add';
  679. const sortData = info.sheet.zh_data;
  680. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  681. data.postData = { name: newText, lid: node.id, porder: order};
  682. } else {
  683. return;
  684. }
  685. } else {
  686. data.posPostType = 'update';
  687. data.postData = {id: posData.id, name: newText};
  688. }
  689. } else if (!posData) {
  690. toastr.warning('新增部位请先输入名称');
  691. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  692. return;
  693. } else {
  694. data.posPostType = 'update';
  695. data.postData = {id: posData.id};
  696. if (col.type === 'Number') {
  697. const num = _.toNumber(newText);
  698. if (num) {
  699. data.postData[col.field] = num;
  700. } else {
  701. try {
  702. data.postData[col.field] = math.evaluate(transExpr(newText));
  703. } catch(err) {
  704. toastr.error('输入的表达式非法');
  705. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  706. return;
  707. }
  708. }
  709. } else {
  710. data.postData[col.field] = newText;
  711. }
  712. }
  713. postData(window.location.pathname + '/update', data, function (result) {
  714. const updateRst = pos.updateDatas(result.pos);
  715. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  716. if (updateRst.create.length > 0) {
  717. posSpreadObj.loadCurPosData();
  718. } else {
  719. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  720. }
  721. const loadResult = billsTree.loadPostData(result.ledger);
  722. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  723. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  724. }, function () {
  725. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  726. });
  727. },
  728. /**
  729. * 删除按钮响应事件
  730. * @param sheet
  731. */
  732. deletePress: function (sheet) {
  733. if (!sheet.zh_settting) return;
  734. const sortData = sheet.zh_data;
  735. const datas = [], posSelects = [];
  736. const sel = sheet.getSelections()[0];
  737. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  738. let bDel = false;
  739. const node = sortData[iRow];
  740. if (node) {
  741. const data = {id: node.id};
  742. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  743. const colSetting = sheet.zh_setting.cols[iCol];
  744. if (colSetting.field === 'name') {
  745. toastr.error('部位名称不能为空');
  746. return;
  747. }
  748. const style = sheet.getStyle(iRow, iCol);
  749. if (!style.locked) {
  750. const colSetting = sheet.zh_setting.cols[iCol];
  751. data[colSetting.field] = null;
  752. bDel = true;
  753. }
  754. }
  755. if (bDel) {
  756. datas.push(data);
  757. posSelects.push(node);
  758. }
  759. }
  760. }
  761. if (datas.length > 0) {
  762. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  763. pos.updateDatas(result.pos);
  764. posSpreadObj.loadCurPosData();
  765. const loadResult = billsTree.loadPostData(result.ledger);
  766. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  767. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  768. }, function () {
  769. posSpreadObj.loadCurPosData();
  770. });
  771. }
  772. },
  773. /**
  774. * 删除 计量单元
  775. * @param sheet
  776. */
  777. deletePos: function (sheet) {
  778. const selection = sheet.getSelections();
  779. const data = {
  780. postType: 'pos',
  781. posPostType: 'delete',
  782. postData: [],
  783. };
  784. const row = selection[0].row, count = selection[0].rowCount;
  785. const sortData = sheet.zh_data;
  786. for (let iRow = 0; iRow < count; iRow++) {
  787. const posData = sortData[iRow + row];
  788. if (posData) {
  789. if (posData.used) {
  790. toastr.error('"' + posData.name + '"已计量,请勿删除');
  791. return;
  792. }
  793. data.postData.push(sortData[iRow + row].id);
  794. }
  795. }
  796. if (data.postData.length > 0) {
  797. postData(window.location.pathname + '/update', data, function (result) {
  798. pos.removeDatas(result.pos);
  799. sheet.deleteRows(row, count);
  800. const loadResult = billsTree.loadPostData(result.ledger);
  801. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  802. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  803. });
  804. }
  805. },
  806. clipboardPasting: function (e, info) {
  807. if (!info.sheet.zh_setting) {
  808. info.cancel = true;
  809. return;
  810. }
  811. const range = info.cellRange;
  812. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  813. const posData = info.sheet.zh_data[iRow];
  814. if (posData && posData.used) {
  815. toastr.warning('"' + pos.name +'"已计量,请勿修改');
  816. info.cancel = true;
  817. return;
  818. }
  819. }
  820. },
  821. /**
  822. * 粘贴单元格响应事件
  823. * @param e
  824. * @param info
  825. */
  826. clipboardPasted: function (e, info) {
  827. const node = SpreadJsObj.getSelectObject(billsSheet);
  828. if (node.code && (node.code !== '')) {
  829. toastr.error('项目节不可含有清单明细');
  830. posSpreadObj.loadCurPosData();
  831. return;
  832. }
  833. if (node.children && (node.children.length > 0)) {
  834. toastr.error('仅清单子项可以含有计量单元');
  835. posSpreadObj.loadCurPosData();
  836. return;
  837. }
  838. if (!info.sheet.zh_setting) {
  839. posSpreadObj.loadCurPosData();
  840. return;
  841. }
  842. const data = [];
  843. const sortData = info.sheet.zh_data || [];
  844. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  845. if (info.cellRange.col !== 0) {
  846. toastr.warning('新增部位请先输入名称');
  847. posSpreadObj.loadCurPosData();
  848. return;
  849. }
  850. }
  851. let bHint = false;
  852. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  853. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  854. let bPaste = true;
  855. const curRow = info.cellRange.row + iRow;
  856. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  857. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  858. const curCol = info.cellRange.col + iCol;
  859. const colSetting = info.sheet.zh_setting.cols[curCol];
  860. posData[colSetting.field] = info.sheet.getText(curRow, curCol);
  861. if (colSetting.type === 'Number') {
  862. const num = _.toNumber(posData[colSetting.field]);
  863. if (num) {
  864. posData[colSetting.field] = num;
  865. } else {
  866. try {
  867. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  868. } catch (err) {
  869. if (!bHint) {
  870. toastr.warning('粘贴了非法表达式,已过滤');
  871. bHint = true;
  872. }
  873. bPaste = false;
  874. }
  875. }
  876. }
  877. }
  878. if (bPaste) {
  879. data.push(posData);
  880. }
  881. }
  882. if (data.length === 0) {
  883. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  884. return;
  885. }
  886. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  887. pos.updateDatas(result.pos);
  888. posSpreadObj.loadCurPosData();
  889. const loadResult = billsTree.loadPostData(result.ledger);
  890. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  891. posSpreadObj.loadCurPosData();
  892. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  893. }, function () {
  894. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  895. });
  896. },
  897. selectionChanged: function (e, info) {
  898. const col = info.sheet.zh_setting.cols[info.newSelections[0].col];
  899. const cell = info.sheet.getCell(info.newSelections[0].col, info.newSelections[0].col);
  900. if (col.type === 'Number') {
  901. const data = SpreadJsObj.getSelectObject(info.sheet);
  902. if (data) {
  903. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  904. .attr('row', info.newSelections[0].row).attr('readOnly', readOnly || cell.locked());
  905. } else {
  906. $('#pos-expr').val('').attr('readOnly', true);
  907. }
  908. } else {
  909. $('#pos-expr').val('').attr('readOnly', true);
  910. }
  911. },
  912. };
  913. posSpreadObj.loadCurPosData();
  914. SpreadJsObj.resetTopAndSelect(posSheet);
  915. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  916. if (!readOnly) {
  917. $('#pos-expr').bind('change mouseleave', function () {
  918. const expr = $(this);
  919. const select = SpreadJsObj.getSelectObject(posSheet);
  920. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val();
  921. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  922. const data = {id: select.id};
  923. const num = _.toNumber(newValue);
  924. if (num) {
  925. data[field] = num;
  926. } else {
  927. try {
  928. data[field] = math.evaluate(transExpr(newValue));
  929. } catch (err) {
  930. toastr.error('输入的表达式非法');
  931. return;
  932. }
  933. }
  934. // 更新至服务器
  935. postData(window.location.pathname + '/update', {posPostType: 'update', posData: data}, function (result) {
  936. const updateRst = pos.updateDatas(result.pos);
  937. expr.val(select[field]);
  938. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  939. SpreadJsObj.reLoadRowData(info.sheet, _.toNumber(row));
  940. const loadResult = billsTree.loadPostData(result.ledger);
  941. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  942. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  943. });
  944. });
  945. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  946. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  947. posSpread.bind(spreadNS.Events.ClipboardPasting, posSpreadObj.clipboardPasting);
  948. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  949. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  950. $.contextMenu({
  951. selector: '#pos-spread',
  952. build: function ($trigger, e) {
  953. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  954. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  955. },
  956. items: {
  957. 'delete': {
  958. name: '删除',
  959. icon: 'fa-remove',
  960. disabled: function (key, opt) {
  961. if (posSheet.zh_data) {
  962. const selection = posSheet.getSelections();
  963. return posSheet.zh_data.length < selection[0].row + selection[0].rowCount;
  964. } else {
  965. return true;
  966. }
  967. },
  968. callback: function (key, opt) {
  969. posSpreadObj.deletePos(posSheet);
  970. }
  971. },
  972. 'debug': {
  973. name: 'debug',
  974. callback: function (key, opt) {
  975. console.log(SpreadJsObj.getSelectObject(posSheet));
  976. }
  977. }
  978. }
  979. });
  980. }
  981. $.divResizer({
  982. select: '#revise-resize',
  983. callback: function () {
  984. billsSpread.refresh();
  985. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  986. $(".sp-wrap").height(bcontent-40);
  987. posSpread.refresh();
  988. }
  989. });
  990. class stdLib {
  991. constructor(selector, stdType, treeSetting, spreadSetting) {
  992. const self = this;
  993. this.obj = $(selector + '-spread')[0];
  994. this.stdType = stdType;
  995. this.treeSetting = treeSetting;
  996. treeSetting.preUrl = this.url;
  997. this.spreadSetting = spreadSetting;
  998. this.spread = SpreadJsObj.createNewSpread(this.obj);
  999. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1000. if (!readOnly) {
  1001. this.spread.getActiveSheet().bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1002. const stdSheet = info.sheet;
  1003. const mainSheet = billsSheet;
  1004. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1005. const stdTree = stdSheet.zh_tree;
  1006. const stdNode = stdTree.nodes[info.row];
  1007. const mainTree = mainSheet.zh_tree;
  1008. const sel = mainSheet.getSelections()[0];
  1009. const mainNode = mainTree.nodes[sel.row];
  1010. if (!stdNode) return;
  1011. if (stdType === 'gcl') {
  1012. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1013. toastr.warning('非最底层项目下,不应添加清单');
  1014. return;
  1015. }
  1016. }
  1017. postData(window.location.pathname + '/update', {
  1018. postType: 'add-std',
  1019. postData: {
  1020. id: mainTree.getNodeKey(mainNode),
  1021. tender_id: mainNode.tender_id,
  1022. stdType: stdType,
  1023. stdLibId: stdNode.list_id,
  1024. stdNode: stdTree.getNodeKey(stdNode)
  1025. }
  1026. }, function (result) {
  1027. const refreshNode = mainTree.loadPostData(result);
  1028. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  1029. if (sel && refreshNode.create[0]) {
  1030. mainSheet.setSelection(mainTree.nodes.indexOf(refreshNode.create[0]), sel.col, sel.rowCount, sel.colCount);
  1031. }
  1032. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1033. });
  1034. });
  1035. }
  1036. this.pathTree = createNewPathTree('base', this.treeSetting);
  1037. this.cacheLib = [];
  1038. $('select', selector).change(function () {
  1039. self.loadLib(parseInt(this.value));
  1040. });
  1041. }
  1042. loadLib (listId) {
  1043. const cacheData = this.cacheLib.find(function (lib) {
  1044. return lib.id === listId;
  1045. });
  1046. if (cacheData) {
  1047. this.pathTree.loadDatas(cacheData.data);
  1048. SpreadJsObj.loadSheetData(this.spread.getActiveSheet(), 'tree', this.pathTree);
  1049. } else {
  1050. const self = this;
  1051. postData('/std-lib/get-data', {stdType: this.stdType, list_id: listId}, function (data) {
  1052. self.cacheLib.push({id: listId, data: data});
  1053. self.pathTree.loadDatas(data);
  1054. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'tree', self.pathTree);
  1055. });
  1056. }
  1057. }
  1058. }
  1059. class DealBills {
  1060. constructor (selector, spreadSetting) {
  1061. const self = this;
  1062. this.loaded = false;
  1063. this.obj = $(selector)[0];
  1064. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1065. this.spreadSetting = spreadSetting;
  1066. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1067. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1068. if (!readOnly) {
  1069. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1070. const dealSheet = info.sheet;
  1071. const mainSheet = billsSheet;
  1072. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1073. if (!dealBills) { return; }
  1074. const mainTree = mainSheet.zh_tree;
  1075. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1076. if (!mainNode || !mainTree) { return; }
  1077. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1078. toastr.warning('非最底层项目下,不应添加清单');
  1079. return;
  1080. }
  1081. postData(window.location.pathname + '/update', {
  1082. postType: 'add-deal',
  1083. postData: {
  1084. id: mainNode.ledger_id,
  1085. type: mainNode.code ? 'child' : 'next',
  1086. dealBills: {
  1087. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1088. unit_price: dealBills.unit_price,
  1089. }
  1090. },
  1091. }, function (result) {
  1092. const refreshData = mainTree.loadPostData(result);
  1093. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1094. const sel = mainSheet.getSelections()[0];
  1095. if (sel && refreshData.create[0]) {
  1096. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1097. }
  1098. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1099. });
  1100. });
  1101. }
  1102. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1103. }
  1104. loadData () {
  1105. if (this.loaded) return;
  1106. const self = this;
  1107. postData(this.url+'/get-data', {}, function (data) {
  1108. self.data = data;
  1109. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1110. self.loaded = true;
  1111. });
  1112. }
  1113. calculateData () {
  1114. if (this.data) {
  1115. for (const d of this.data) {
  1116. d.total_price = _.multiply(d.quantity, d.unit_price);
  1117. }
  1118. }
  1119. }
  1120. }
  1121. class BatchInsertBillsPosObj {
  1122. constructor (obj) {
  1123. const self = this;
  1124. this.obj = obj;
  1125. this.billsCount = 6;
  1126. this.posCount = 1000;
  1127. // 初始化 清单编号窗口 参数
  1128. this.qdSpreadSetting = {
  1129. cols: [
  1130. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1131. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1132. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1133. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1134. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1135. ],
  1136. emptyRows: this.billsCount,
  1137. headRows: 1,
  1138. headRowHeight: [32],
  1139. headerFont: '12px 微软雅黑',
  1140. font: '12px 微软雅黑',
  1141. };
  1142. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1143. // 初始化 部位数量复核表 参数
  1144. this.posSpreadSetting = {
  1145. cols: [
  1146. {title: '部位', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1147. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1148. ],
  1149. emptyRows: this.posCount,
  1150. headRows: 1,
  1151. headRowHeight: [32],
  1152. headerFont: '12px 微软雅黑',
  1153. font: '12px 微软雅黑',
  1154. };
  1155. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1156. this.posSpreadSetting.cols.push(
  1157. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1158. )
  1159. }
  1160. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1161. // 初始化 签约清单 参数
  1162. this.dealSpreadSetting = {
  1163. cols: [
  1164. {title: '清单编号', field: 'code', width: 80, formatter: '@', readOnly: true},
  1165. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true},
  1166. {title: '单位', field: 'unit', width: 50, formatter: '@', readOnly: true},
  1167. {title: '单价', field: 'unit_price', width: 50, readOnly: true},
  1168. ],
  1169. emptyRows: 0,
  1170. headRows: 1,
  1171. headRowHeight: [32],
  1172. headerFont: '12px 微软雅黑',
  1173. font: '12px 微软雅黑',
  1174. };
  1175. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1176. // 初始化 清单编号、部位数量复核表 表格
  1177. this.initView();
  1178. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1179. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1180. // 拉取签约清单数据
  1181. dealBills.loadData();
  1182. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1183. // 双击签约清单,自动添加到清单编号窗口
  1184. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1185. const deal = info.sheet.zh_data[info.row];
  1186. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1187. const sel = qdSheet.getSelections()[0];
  1188. qdSheet.getCell(sel.row, 0).value(deal.code);
  1189. qdSheet.getCell(sel.row, 1).value(deal.name);
  1190. qdSheet.getCell(sel.row, 2).value(deal.unit);
  1191. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  1192. if (sel.row + 1 === qdSheet.getRowCount()) {
  1193. const count = sel.row + 2;
  1194. qdSheet.setRowCount(count);
  1195. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  1196. const colCount = posSheet.getColumnCount() + 1
  1197. posSheet.setColumnCount(colCount);
  1198. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('数量' + count);
  1199. }
  1200. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  1201. });
  1202. this.obj.bind('shown.bs.modal', function () {
  1203. self.qdSpread.refresh();
  1204. self.posSpread.refresh();
  1205. self.dealSpread.refresh();
  1206. });
  1207. $('#batch-ok').click(function () {
  1208. const selection = billsSheet.getSelections();
  1209. const row = selection[0].row;
  1210. const select = billsTree.nodes[row];
  1211. if (select) {
  1212. const insertData = {};
  1213. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  1214. insertData.id = select[billsTree.setting.id];
  1215. insertData.batchData = self.getBatchData();
  1216. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  1217. pos.updateDatas(data.pos);
  1218. const result = billsTree.loadPostData(data.ledger);
  1219. billsTreeSpreadObj.refreshTree(sheet, result);
  1220. billsTreeSpreadObj.refreshOperationValid(sheet, selection);
  1221. self.obj.modal('hide');
  1222. }, null, true);
  1223. }
  1224. });
  1225. }
  1226. // 初始化左侧表格
  1227. initView () {
  1228. // 初始化 清单编号
  1229. const qdSheet = this.qdSpread.getActiveSheet();
  1230. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  1231. SpreadJsObj.refreshColumnAlign(qdSheet);
  1232. // 清理原有数据
  1233. SpreadJsObj.beginMassOperation(qdSheet);
  1234. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1235. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  1236. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  1237. }
  1238. qdSheet.setSelection(0, 0, 1 ,1);
  1239. SpreadJsObj.endMassOperation(qdSheet);
  1240. // 初始化 部位数量复核表
  1241. const posSheet = this.posSpread.getActiveSheet();
  1242. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  1243. SpreadJsObj.refreshColumnAlign(posSheet);
  1244. // 清理原有数据
  1245. SpreadJsObj.beginMassOperation(posSheet);
  1246. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  1247. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1248. posSheet.setSelection(0, 0, 1 ,1);
  1249. SpreadJsObj.endMassOperation(posSheet);
  1250. // 检查签约清单数据,以工具栏数据为准
  1251. if (dealBills) {
  1252. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1253. }
  1254. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  1255. }
  1256. // 获取界面数据
  1257. getBatchData () {
  1258. const result = [];
  1259. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  1260. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  1261. if (qdSheet.getText(iRow, 0) === '') { continue; }
  1262. const qd = {
  1263. b_code: qdSheet.getText(iRow, 0),
  1264. name: qdSheet.getText(iRow, 1),
  1265. unit: qdSheet.getText(iRow, 2),
  1266. price: _.toNumber(qdSheet.getText(iRow, 3)),
  1267. pos: [],
  1268. };
  1269. result.push(qd);
  1270. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  1271. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  1272. if (value !== 0 && !isNaN(value)) {
  1273. qd.pos.push({
  1274. name: posSheet.getText(iPosRow, 0),
  1275. drawing_code: posSheet.getText(iPosRow, 1),
  1276. quantity: value,
  1277. });
  1278. }
  1279. }
  1280. }
  1281. return result;
  1282. }
  1283. }
  1284. let stdXmj, stdGcl, searchLedger;
  1285. const dealBills = new DealBills('#deal-bills-spread', {
  1286. cols: [
  1287. {title: '清单编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1288. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1289. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1290. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1291. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1292. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1293. ],
  1294. emptyRows: 0,
  1295. headRows: 1,
  1296. headRowHeight: [32],
  1297. defaultRowHeight: 21,
  1298. headerFont: '12px 微软雅黑',
  1299. font: '12px 微软雅黑',
  1300. });
  1301. $.divResizer({
  1302. select: '#revise-right-spr',
  1303. callback: function () {
  1304. billsSpread.refresh();
  1305. if (posSpread) {
  1306. posSpread.refresh();
  1307. }
  1308. if (stdXmj) {
  1309. stdXmj.spread.refresh();
  1310. }
  1311. if (stdGcl) {
  1312. stdGcl.spread.refresh();
  1313. }
  1314. }
  1315. });
  1316. // 展开收起标准清单
  1317. $('a', '#side-menu').bind('click', function (e) {
  1318. e.preventDefault();
  1319. const tab = $(this), tabPanel = $(tab.attr('content'));
  1320. const showSideTools = function (show) {
  1321. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1322. if (show) {
  1323. right.show();
  1324. autoFlashHeight();
  1325. /**
  1326. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1327. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1328. * 故需要通过最终的parent.width再计算一次left.width
  1329. *
  1330. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1331. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1332. *
  1333. */
  1334. //left.css('width', parent.width() - right.outerWidth());
  1335. //left.css('width', parent.width() - right.outerWidth());
  1336. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1337. left.css('width', percent + '%');
  1338. } else {
  1339. left.width(parent.width());
  1340. right.hide();
  1341. }
  1342. };
  1343. // 展开工具栏、切换标签
  1344. if (!tab.hasClass('active')) {
  1345. const close = $('.active', '#side-menu').length === 0;
  1346. $('a', '#side-menu').removeClass('active');
  1347. tab.addClass('active');
  1348. $('.tab-content .tab-pane').removeClass('active');
  1349. tabPanel.addClass('active');
  1350. showSideTools(tab.hasClass('active'));
  1351. if (tab.attr('content') === '#std-xmj') {
  1352. if (!stdXmj) {
  1353. stdXmj = new stdLib('#std-xmj', 'xmj', {
  1354. id: 'chapter_id',
  1355. pid: 'pid',
  1356. order: 'order',
  1357. level: 'level',
  1358. rootId: -1,
  1359. keys: ['id', 'list_id', 'chapter_id'],
  1360. }, {
  1361. cols: [
  1362. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1363. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1364. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1365. ],
  1366. treeCol: 0,
  1367. emptyRows: 0,
  1368. headRows: 1,
  1369. headRowHeight: [32],
  1370. defaultRowHeight: 21,
  1371. headerFont: '12px 微软雅黑',
  1372. font: '12px 微软雅黑',
  1373. });
  1374. stdXmj.loadLib($('select', '#std-xmj').val());
  1375. }
  1376. stdXmj.spread.refresh();
  1377. } else if (tab.attr('content') === '#std-gcl') {
  1378. if (!stdGcl) {
  1379. stdGcl = new stdLib('#std-gcl', 'gcl', {
  1380. id: 'bill_id',
  1381. pid: 'pid',
  1382. order: 'order',
  1383. level: 'level',
  1384. rootId: -1,
  1385. keys: ['id', 'list_id', 'bill_id']
  1386. }, {
  1387. cols: [
  1388. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1389. {title: '名称', field: 'name', hAlign: 0, width: 230, formatter: '@', readOnly: true},
  1390. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1391. ],
  1392. treeCol: 0,
  1393. emptyRows: 0,
  1394. headRows: 1,
  1395. headRowHeight: [32],
  1396. defaultRowHeight: 21,
  1397. headerFont: '12px 微软雅黑',
  1398. font: '12px 微软雅黑',
  1399. });
  1400. stdGcl.loadLib($('select', '#std-gcl').val());
  1401. }
  1402. stdGcl.spread.refresh();
  1403. } else if (tab.attr('content') === '#deal-bills') {
  1404. dealBills.loadData();
  1405. dealBills.spread.refresh();
  1406. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1407. if (!searchLedger) {
  1408. searchLedger = $.billsSearch({
  1409. selector: '#search',
  1410. searchSpread: billsSpread,
  1411. resultSpreadSetting: {
  1412. cols: [
  1413. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1414. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1415. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  1416. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1417. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1418. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1419. ],
  1420. emptyRows: 0,
  1421. headRows: 1,
  1422. headRowHeight: [32],
  1423. defaultRowHeight: 21,
  1424. headerFont: '12px 微软雅黑',
  1425. font: '12px 微软雅黑',
  1426. },
  1427. afterLocated: function () {
  1428. posSpreadObj.loadCurPosData();
  1429. }
  1430. });
  1431. }
  1432. searchLedger.spread.refresh();
  1433. }
  1434. }
  1435. else {// 收起工具栏
  1436. tab.removeClass('active');
  1437. tabPanel.removeClass('active');
  1438. showSideTools(tab.hasClass('active'));
  1439. }
  1440. billsSpread.refresh();
  1441. if (posSpread) {
  1442. posSpread.refresh();
  1443. }
  1444. });
  1445. if (!readOnly) {
  1446. // 修订详情 保存
  1447. $('#save').click(function () {
  1448. const content = $('#content').val();
  1449. postData('save', { content: content }, function () {
  1450. $('#content').attr('org-value', content);
  1451. });
  1452. });
  1453. }
  1454. // 显示层次
  1455. (function (select, sheet) {
  1456. $(select).click(function () {
  1457. if (!sheet.zh_tree) return;
  1458. const tag = $(this).attr('tag');
  1459. const tree = sheet.zh_tree;
  1460. switch (tag) {
  1461. case "1":
  1462. case "2":
  1463. case "3":
  1464. case "4":
  1465. case "5":
  1466. tree.expandByLevel(parseInt(tag));
  1467. SpreadJsObj.refreshTreeRowVisible(sheet);
  1468. break;
  1469. case "last":
  1470. tree.expandByCustom(() => { return true; });
  1471. SpreadJsObj.refreshTreeRowVisible(sheet);
  1472. break;
  1473. case "leafXmj":
  1474. tree.expandToLeafXmj();
  1475. SpreadJsObj.refreshTreeRowVisible(sheet);
  1476. break;
  1477. }
  1478. });
  1479. })('a[name=showLevel]', billsSheet);
  1480. });