revise.js 70 KB

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