revise.js 71 KB

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