revise.js 73 KB

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