revise.js 71 KB

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