revise.js 69 KB

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