revise.js 71 KB

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