revise.js 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. 'use strict';
  2. /**
  3. * 台账修订页面js
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ckBillsSpread = window.location.pathname + '-billsSelect';
  10. const invalidFields = {
  11. parent: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'unit_price'],
  12. gcl: ['dgn_qty1', 'dgn_qty2'],
  13. posCode: ['b_code'],
  14. posCalc: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp'],
  15. posXmj: ['code'],
  16. };
  17. function transExpr(expr) {
  18. return $.trim(expr).replace('\t', '').replace('=', '').replace('%', '/100');
  19. }
  20. $(document).ready(() => {
  21. let stdXmj, stdGcl, searchLedger;
  22. autoFlashHeight();
  23. // 初始化spread
  24. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  25. const billsSheet = billsSpread.getActiveSheet();
  26. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  27. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  28. const posSheet = posSpread.getActiveSheet();
  29. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  30. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  31. // 初始化 节点树结构
  32. const treeSetting = {
  33. id: 'ledger_id',
  34. pid: 'ledger_pid',
  35. order: 'order',
  36. level: 'level',
  37. rootId: -1,
  38. keys: ['id', 'tender_id', 'ledger_id'],
  39. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  40. };
  41. if (!isTz) {
  42. treeSetting.calcFields.push('deal_tp');
  43. }
  44. treeSetting.calcFun = function (node) {
  45. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  46. };
  47. const billsTree = createNewPathTree('revise', treeSetting);
  48. // 初始化 计量单元
  49. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  50. // 清单 相关方法&绑定spreadjs事件
  51. const billsTreeSpreadObj = {
  52. loadExprToInput(sheet) {
  53. const sel = sheet.getSelections()[0];
  54. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  55. if (col && col.type === 'Number') {
  56. const data = SpreadJsObj.getSelectObject(sheet);
  57. if (data) {
  58. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field]);
  59. if (col.field.indexOf('dgn') >= 0) {
  60. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (_.isString(data.b_code) && data.b_code !== ''));
  61. } else if (col.field === 'unit_price') {
  62. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || (_.isBoolean(data.used) && data.used === true));
  63. } else {
  64. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0));
  65. }
  66. } else {
  67. $('#bills-expr').val('').attr('readOnly', true);
  68. }
  69. } else {
  70. $('#bills-expr').val('').attr('readOnly', true);
  71. }
  72. },
  73. getDefaultSelectInfo: function (sheet) {
  74. const tree = sheet.zh_tree;
  75. if (!tree) return;
  76. const sel = sheet.getSelections()[0];
  77. const node = sheet.zh_tree.nodes[sel.row];
  78. if (!node) return;
  79. let count = 1;
  80. if (sel.rowCount > 1) {
  81. for (let r = 1; r < sel.rowCount; r++) {
  82. const rNode = sheet.zh_tree.nodes[sel.row + r];
  83. if (rNode.level > node.level) continue;
  84. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  85. toastr.warning('请选择同一节点下的节点,进行该操作');
  86. return;
  87. }
  88. count += 1;
  89. }
  90. }
  91. return [tree, node, count];
  92. },
  93. /**
  94. * 刷新顶部按钮是否可用
  95. * @param sheet
  96. * @param selections
  97. */
  98. refreshOperationValid: function (sheet, selection) {
  99. const setObjEnable = function (obj, enable) {
  100. if (enable) {
  101. obj.removeClass('disabled');
  102. } else {
  103. obj.addClass('disabled');
  104. }
  105. };
  106. const invalidAll = function () {
  107. setObjEnable($('a[type=add]'), false);
  108. setObjEnable($('a[type=delete]'), false);
  109. setObjEnable($('a[type=up-move]'), false);
  110. setObjEnable($('a[type=down-move]'), false);
  111. setObjEnable($('a[type=up-level]'), false);
  112. setObjEnable($('a[type=down-level]'), false);
  113. };
  114. const sel = selection ? selection[0] : sheet.getSelections()[0];
  115. const row = sel ? sel.row : -1;
  116. const tree = sheet.zh_tree;
  117. if (!tree) {
  118. invalidAll();
  119. return;
  120. }
  121. const first = sheet.zh_tree.nodes[row];
  122. if (!first) {
  123. invalidAll();
  124. return;
  125. }
  126. let last = first, sameParent = true, nodeUsed = first.used;
  127. if (sel.rowCount > 1 && first) {
  128. for (let r = 1; r < sel.rowCount; r++) {
  129. const rNode = tree.nodes[sel.row + r];
  130. if (!rNode) {
  131. sameParent = false;
  132. break;
  133. }
  134. nodeUsed = nodeUsed || rNode.used;
  135. if (rNode.level > first.level) continue;
  136. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  137. sameParent = false;
  138. break;
  139. }
  140. last = rNode;
  141. }
  142. }
  143. const preNode = tree.getPreSiblingNode(first);
  144. const valid = !sheet.zh_setting.readOnly;
  145. setObjEnable($('a[type=add]'), valid && first && first.level > 1);
  146. setObjEnable($('a[type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed);
  147. setObjEnable($('a[type=up-move]'), valid && first && sameParent && first.level > 1 && preNode);
  148. setObjEnable($('a[type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last));
  149. if (isTz) {
  150. const posRange = last ? pos.getLedgerPos(last.id) : [];
  151. setObjEnable($('a[type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  152. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)));
  153. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  154. setObjEnable($('a[type=down-level]'), valid && first && sameParent && !nodeUsed
  155. && first.level > 1 && preNode && (!preNodePosRange || preNodePosRange.length === 0));
  156. } else {
  157. setObjEnable($('#up-level'), valid && first && sameParent && first.level > 2 && tree.getParent(first));
  158. setObjEnable($('#down-level'), valid && first && sameParent && first.level > 1 && preNode);
  159. }
  160. setObjEnable($('#cut'), valid);
  161. setObjEnable($('#paste'), valid);
  162. },
  163. /**
  164. *
  165. * @param sheet
  166. * @param data
  167. */
  168. refreshTree: function (sheet, data) {
  169. SpreadJsObj.massOperationSheet(sheet, function () {
  170. const tree = sheet.zh_tree;
  171. // 处理删除
  172. if (data.delete) {
  173. data.delete.sort(function (a, b) {
  174. return b.deleteIndex - a.deleteIndex;
  175. });
  176. for (const d of data.delete) {
  177. sheet.deleteRows(d.deleteIndex, 1);
  178. }
  179. }
  180. // 处理新增
  181. if (data.create) {
  182. const newNodes = data.create;
  183. if (newNodes) {
  184. newNodes.sort(function (a, b) {
  185. return a.index - b.index;
  186. });
  187. for (const node of newNodes) {
  188. sheet.addRows(node.index, 1);
  189. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  190. }
  191. }
  192. }
  193. // 处理更新
  194. if (data.update) {
  195. const rows = [];
  196. for (const u of data.update) {
  197. rows.push(tree.nodes.indexOf(u));
  198. }
  199. SpreadJsObj.reLoadRowsData(sheet, rows);
  200. }
  201. // 处理展开
  202. if (data.expand) {
  203. const expanded = [];
  204. for (const e of data.expand) {
  205. if (expanded.indexOf(e) === -1) {
  206. const posterity = tree.getPosterity(e);
  207. for (const p of posterity) {
  208. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  209. expanded.push(p);
  210. }
  211. }
  212. }
  213. }
  214. });
  215. },
  216. selectionChanged: function (e, info) {
  217. if (info.newSelections[0].row !== info.oldSelections[0].row) {
  218. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  219. posSpreadObj.loadCurPosData();
  220. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  221. posSearch.search($('#pos-keyword').val());
  222. }
  223. billsTreeSpreadObj.loadExprToInput(info.sheet);
  224. },
  225. /**
  226. * 新增节点
  227. * @param spread
  228. */
  229. baseOpr: function (sheet, type, addCount = 1) {
  230. const self = this;
  231. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  232. if (!tree || !node || !count) return;
  233. if (type === 'delete') {
  234. const parent = tree.getParent(node);
  235. const children = parent ? parent.children : tree.children;
  236. const index = children.indexOf(node);
  237. for (let i = 0; i < count; i++) {
  238. const child = children[i+index];
  239. if (tree.checkNodeUsed(child, pos)) {
  240. toastr.warning('选中的节点已计量,不可删除');
  241. return;
  242. }
  243. }
  244. } else if (type === 'up-level') {
  245. const parent = tree.getParent(node);
  246. const children = parent ? parent.children : tree.children;
  247. const index = children.indexOf(node);
  248. for (let i = index; i < children.length; i++) {
  249. const child = children[index];
  250. if (tree.checkNodeUsed(child, pos)) {
  251. if (i >= index + count) {
  252. toastr.warning('其后节点已计量,选中的节点不可升级');
  253. } else {
  254. toastr.warning('选中的节点已计量,不可升级');
  255. }
  256. return;
  257. }
  258. }
  259. } else if (type === 'down-level') {
  260. const parent = tree.getParent(node);
  261. const children = parent ? parent.children : tree.children;
  262. const index = children.indexOf(node);
  263. for (let i = 0; i < count; i++) {
  264. const child = children[i+index];
  265. if (tree.checkNodeUsed(child, pos)) {
  266. toastr.warning('选中的节点已计量,不可降级');
  267. return;
  268. }
  269. }
  270. }
  271. postData(window.location.pathname + '/update', {
  272. postType: type,
  273. postData: {
  274. id: node.ledger_id,
  275. count: type === 'add' ? addCount : count,
  276. }
  277. }, function (result) {
  278. const refreshData = tree.loadPostData(result);
  279. self.refreshTree(sheet, refreshData);
  280. if (type === 'delete') {
  281. const sel = sheet.getSelections()[0];
  282. if (sel) {
  283. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  284. }
  285. } else if (['up-move', 'down-move'].indexOf(type) > -1) {
  286. const sel = sheet.getSelections()[0];
  287. if (sel) {
  288. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  289. }
  290. } else if (type === 'add') {
  291. const sel = sheet.getSelections()[0];
  292. if (sel) {
  293. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  294. }
  295. }
  296. self.refreshOperationValid(sheet);
  297. });
  298. },
  299. /**
  300. * 编辑单元格响应事件
  301. * @param {Object} e
  302. * @param {Object} info
  303. */
  304. editEnded: function (e, info) {
  305. if (info.sheet.zh_setting) {
  306. const col = info.sheet.zh_setting.cols[info.col];
  307. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  308. const node = sortData[info.row];
  309. const data = {
  310. id: node.id,
  311. tender_id: node.tender_id,
  312. ledger_id: node.ledger_id
  313. };
  314. // 未改变值则不提交
  315. const orgValue = node[col.field];
  316. const newValue = trimInvalidChar(info.editingText);
  317. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  318. return;
  319. }
  320. // 台账模式,检查计量单元相关
  321. if (isTz) {
  322. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  323. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  324. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  325. if (!node.children || node.children.length ===0) {
  326. const lPos = pos.getLedgerPos(node.id);
  327. if (lPos && lPos.length > 0) {
  328. toastr.error('清单含有计量单元,不可修改施工图复核数量');
  329. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  330. return;
  331. }
  332. }
  333. }
  334. if (col.field === 'b_code' && (newValue === '' || !newValue)) {
  335. const lPos = pos.getLedgerPos(node.id);
  336. if (lPos && lPos.length > 0) {
  337. toastr.error('清单含有计量单元,请先删除计量单元,再删除清单编号');
  338. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  339. return;
  340. }
  341. }
  342. }
  343. // 获取更新数据
  344. if (info.editingText) {
  345. const text = newValue;
  346. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  347. toastr.error('节点已计量,请勿删除编号');
  348. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  349. return;
  350. }
  351. if (col.type === 'Number') {
  352. const num = _.toNumber(text);
  353. if (_.isFinite(num)) {
  354. data[col.field] = num;
  355. } else {
  356. try {
  357. data[col.field] = math.evaluate(transExpr(text));
  358. } catch(err) {
  359. toastr.error('输入的表达式非法');
  360. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  361. return;
  362. }
  363. }
  364. } else {
  365. data[col.field] = text;
  366. }
  367. } else {
  368. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  369. toastr.error('节点已计量,请勿删除编号');
  370. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  371. return;
  372. }
  373. data[col.field] = null;
  374. }
  375. // 更新至服务器
  376. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  377. const refreshNode = billsTree.loadPostData(result);
  378. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  379. });
  380. }
  381. },
  382. clipboardPasting: function (e, info) {
  383. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  384. info.cancel = true;
  385. if (!setting || !tree) return;
  386. // const range = info.cellRange;
  387. // for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  388. // const node = tree.nodes[iRow];
  389. // if (tree.checkNodeUsed(node, pos)) {
  390. // toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  391. // return;
  392. // }
  393. // }
  394. const pasteData = info.pasteData.html
  395. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  396. : (info.pasteData.text === ''
  397. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  398. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  399. const hint = {
  400. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  401. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改为空值'},
  402. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  403. posCode: {type: 'warning', msg: '清单含有计量单元,不可粘贴清单编号为空'},
  404. posQty: {type: 'warning', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可粘贴'},
  405. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  406. gcl: {type: 'warning', msg: '工程量清单,不可粘贴项目节数量'},
  407. posXmj: {type: 'warning', msg: '清单含有计量单元,不可粘贴项目节编号'},
  408. };
  409. const datas = [], filterNodes = [];
  410. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  411. const curRow = info.cellRange.row + iRow;
  412. const node = tree.nodes[curRow];
  413. if (!node) continue;
  414. let bPaste = false;
  415. const data = info.sheet.zh_tree.getNodeKeyData(node);
  416. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  417. const curCol = info.cellRange.col + iCol;
  418. const colSetting = info.sheet.zh_setting.cols[curCol];
  419. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  420. toastMessageUniq(hint.parent);
  421. continue;
  422. }
  423. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  424. toastMessageUniq(hint.gcl);
  425. continue;
  426. }
  427. const lPos = pos.getLedgerPos(node.id);
  428. if (lPos && lPos.length > 0) {
  429. if (value === '' && colSetting.field === 'b_code') {
  430. toastMessageUniq(hint.posCode);
  431. continue;
  432. }
  433. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  434. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  435. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  436. toastMessageUniq(hint.posQty);
  437. continue;
  438. }
  439. if (value !== '' && invalidFields.posXmj.indexOf(colSetting.field) >= 0) {
  440. toastMessageUniq(hint.posXmj);
  441. continue;
  442. }
  443. }
  444. const value = trimInvalidChar(pasteData[iRow][iCol]);
  445. if (tree.checkNodeUsed(node, pos) && col.field === 'unit_price') {
  446. toastMessageUniq (hint.usedUp);
  447. continue;
  448. }
  449. if (colSetting.type === 'Number') {
  450. const num = _.toNumber(value);
  451. if (num) {
  452. data[colSetting.field] = num;
  453. } else {
  454. try {
  455. data[colSetting.field] = math.evaluate(transExpr(value));
  456. } catch (err) {
  457. toastMessageUniq(hint.invalidExpr);
  458. continue;
  459. }
  460. }
  461. } else {
  462. if (node.used && (col.field === 'code' || col.field ==='b_code')
  463. && data[colSetting.field] !== '' && value === '') {
  464. toastMessageUniq(hint.usedCode);
  465. continue;
  466. }
  467. data[colSetting.field] = value;
  468. }
  469. bPaste = true;
  470. }
  471. if (bPaste) {
  472. datas.push(data);
  473. } else {
  474. filterNodes.push(node);
  475. }
  476. }
  477. if (datas.length > 0) {
  478. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  479. const refreshNode = tree.loadPostData(result);
  480. if (refreshNode.update) {
  481. refreshNode.update = refreshNode.update.concat(filterNodes);
  482. }
  483. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  484. }, function () {
  485. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  486. });
  487. } else {
  488. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  489. }
  490. },
  491. clipboardPasted: function (e, info) {
  492. const hint = {
  493. usedUp: {type: 'error', msg: '节点已计量,不可修改单价'},
  494. usedCode: {type: 'error', msg: '节点已计量,编号不可修改为空值'},
  495. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  496. posCode: {type: 'error', msg: '清单含有计量单元,请先删除计量单元,再修改清单编号为空'},
  497. posQty: {type: 'error', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可修改'},
  498. };
  499. const tree = info.sheet.zh_tree;
  500. if (!tree) { return; }
  501. const sortData = info.sheet.zh_tree.nodes;
  502. const datas = [], filterNodes = [];
  503. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  504. let bPaste = false;
  505. const curRow = info.cellRange.row + iRow;
  506. const node = sortData[curRow];
  507. if (node) {
  508. const data = info.sheet.zh_tree.getNodeKeyData(node);
  509. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  510. const curCol = info.cellRange.col + iCol;
  511. const colSetting = info.sheet.zh_setting.cols[curCol];
  512. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  513. const lPos = pos.getLedgerPos(node.id);
  514. if (lPos && lPos.length > 0) {
  515. if (value === '' && colSetting.field === 'b_code') {
  516. toastMessageUniq(hint.posCode);
  517. continue;
  518. }
  519. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  520. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  521. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  522. toastMessageUniq(hint.posQty);
  523. continue;
  524. }
  525. }
  526. if (node.used && col.field === 'unit_price') {
  527. toastMessageUniq (hint.usedUp);
  528. continue;
  529. }
  530. if (colSetting.type === 'Number') {
  531. const num = _.toNumber(value);
  532. if (num) {
  533. data[colSetting.field] = num;
  534. } else {
  535. try {
  536. data[colSetting.field] = math.evaluate(transExpr(value));
  537. } catch (err) {
  538. toastMessageUniq(hint.invalidExpr);
  539. continue;
  540. }
  541. }
  542. } else {
  543. if (node.used && (col.field === 'code' || col.field ==='b_code')
  544. && data[colSetting.field] !== '' && value === '') {
  545. toastMessageUniq(hint.usedCode);
  546. continue;
  547. }
  548. data[colSetting.field] = value;
  549. }
  550. bPaste = true;
  551. }
  552. if (bPaste) {
  553. datas.push(data);
  554. } else {
  555. filterNodes.push(node);
  556. }
  557. }
  558. }
  559. if (datas.length > 0) {
  560. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  561. const refreshNode = tree.loadPostData(result);
  562. if (refreshNode.update) {
  563. refreshNode.update = refreshNode.update.concat(filterNodes);
  564. }
  565. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  566. }, function () {
  567. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  568. });
  569. } else {
  570. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  571. }
  572. },
  573. deletePress: function (sheet) {
  574. if (!sheet.zh_setting) return;
  575. const sel = sheet.getSelections()[0], datas = [];
  576. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  577. const node = sheet.zh_tree.nodes[iRow];
  578. if (sheet.zh_tree.checkNodeUsed(node, pos)) {
  579. toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  580. return;
  581. }
  582. const data = sheet.zh_tree.getNodeKeyData(node);
  583. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  584. const col = sheet.zh_setting.cols[iCol];
  585. data[col.field] = null;
  586. }
  587. datas.push(data);
  588. }
  589. if (datas.length > 0) {
  590. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  591. const refreshNode = sheet.zh_tree.loadPostData(result);
  592. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  593. });
  594. }
  595. },
  596. pasteBlock: function (spread, copyInfo) {
  597. const self = this;
  598. const sheet = spread.getActiveSheet();
  599. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  600. postData(window.location.pathname + '/update', {
  601. postType: 'paste-block',
  602. postData: {
  603. id: tree.getNodeKey(node),
  604. tid: copyInfo.tid,
  605. block: copyInfo.block,
  606. }
  607. }, function (data) {
  608. pos.updateDatas(data.pos);
  609. const result = tree.loadPostData(data.ledger);
  610. self.refreshTree(sheet, result);
  611. const sel = sheet.getSelections()[0];
  612. if (sel) {
  613. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  614. }
  615. self.refreshOperationValid(sheet);
  616. removeLocalCache(copyBlockTag);
  617. }, null, true);
  618. },
  619. topRowChanged: function (e, info) {
  620. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  621. },
  622. editStarting(e, info) {
  623. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  624. const col = info.sheet.zh_setting.cols[info.col];
  625. const node = info.sheet.zh_tree.nodes[info.row];
  626. if (!node) {
  627. info.cancel = true;
  628. return;
  629. }
  630. switch (col.field) {
  631. case 'code':
  632. const posRange = pos.getLedgerPos(node.id);
  633. return posRange && posRange.length > 0;
  634. case 'unit_price':
  635. info.cancel = (node.children && node.children.length > 0) || node.used;
  636. break;
  637. case 'sgfh_qty':
  638. case 'sgfh_tp':
  639. case 'sjcl_qty':
  640. case 'sjcl_tp':
  641. case 'qtcl_qty':
  642. case 'qtcl_tp':
  643. case 'deal_qty':
  644. case 'deal_tp':
  645. info.cancel = (node.children && node.children.length > 0);
  646. break;
  647. case 'dgn_qty1':
  648. case 'dgn_qty2':
  649. info.cancel = !_.isEmpty(node.b_code);
  650. break;
  651. }
  652. },
  653. cut: function (sheet, sel, callback) {
  654. if (!sheet || !sel) return;
  655. if (sel.colCount >= sheet.zh_setting.cols.length) {
  656. toastr.warning('请勿选中整行剪切');
  657. return;
  658. }
  659. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  660. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  661. const node = sortData[iRow];
  662. if (node) {
  663. const data = sheet.zh_tree.getNodeKeyData(node);
  664. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  665. const col = sheet.zh_setting.cols[iCol];
  666. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  667. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  668. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  669. const lPos = pos.getLedgerPos(node.id);
  670. if (lPos && lPos.length > 0) {
  671. toastr.error('不可剪切');
  672. return;
  673. }
  674. }
  675. const style = sheet.getStyle(iRow, iCol);
  676. if (style.locked) {
  677. toastr.error('不可剪切');
  678. return;
  679. }
  680. const colSetting = sheet.zh_setting.cols[iCol];
  681. data[colSetting.field] = null;
  682. }
  683. datas.push(data);
  684. }
  685. }
  686. if (datas.length > 0) {
  687. callback();
  688. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  689. const refreshNode = sheet.zh_tree.loadPostData(result);
  690. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  691. });
  692. }
  693. },
  694. sortCode: function (sheet) {
  695. const tree = sheet.zh_tree;
  696. if (!tree) return;
  697. const select = SpreadJsObj.getSelectObject(sheet);
  698. if (!select || !select.code || select.code === '') return;
  699. const recursiveSortCode = function (data, parentCode, children) {
  700. if (!children || children.length === 0) return;
  701. for (const [i, child] of children.entries()) {
  702. if (!child.b_code || child.b_code === '') {
  703. const code = parentCode + '-' + (i + 1);
  704. const cData = tree.getNodeKeyData(child);
  705. cData.code = code;
  706. data.push(cData);
  707. if (!tree.isLeafXmj(child)) {
  708. recursiveSortCode(data, code, child.children);
  709. }
  710. }
  711. }
  712. };
  713. const data = [];
  714. recursiveSortCode(data, select.code, select.children);
  715. if (data.length > 0) {
  716. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  717. const refreshNode = tree.loadPostData(result);
  718. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  719. })
  720. }
  721. }
  722. };
  723. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  724. billsTreeSpreadObj.loadExprToInput(billsSheet);
  725. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  726. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  727. if (!readOnly) {
  728. // 增删上下移升降级
  729. $('a[name="base-opr"]').click(function () {
  730. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  731. });
  732. $('a[name=cpc]').click(function () {
  733. billsSpread.commandManager().execute({
  734. cmd: this.getAttribute('type'),
  735. sheetName: billsSheet.name()
  736. });
  737. });
  738. $('#bills-expr').bind('change mouseleave', function () {
  739. if (this.readOnly) return;
  740. const expr = $(this);
  741. const select = SpreadJsObj.getSelectObject(billsSheet);
  742. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  743. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  744. const data = {
  745. id: select.id,
  746. tender_id: select.tender_id,
  747. ledger_id: select.ledger_id
  748. };
  749. if (newValue !== '') {
  750. const num = _.toNumber(newValue);
  751. if (num) {
  752. data[field] = num;
  753. } else {
  754. try {
  755. data[field] = math.evaluate(transExpr(newValue));
  756. } catch (err) {
  757. toastr.error('输入的表达式非法');
  758. return;
  759. }
  760. }
  761. } else {
  762. data[field] = null;
  763. }
  764. // 更新至服务器
  765. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  766. const refreshNode = billsTree.loadPostData(result);
  767. expr.val(select[field]);
  768. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  769. });
  770. });
  771. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  772. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  773. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  774. billsSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  775. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  776. SpreadJsObj.Clipboard.setCopyData(copyText);
  777. });
  778. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  779. SpreadJsObj.addCutEvents(billsSpread, billsTreeSpreadObj.cut);
  780. let batchInsertObj;
  781. $.contextMenu.types.batchInsert = function (item, opt, root) {
  782. const self = this;
  783. if ($.isFunction(item.icon)) {
  784. item._icon = item.icon.call(this, this, $t, key, item);
  785. } else {
  786. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  787. // to enable font awesome
  788. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  789. } else {
  790. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  791. }
  792. }
  793. this.addClass(item._icon);
  794. const $obj = $('<div>' + item.name + '<input class="text-right ml-1 mr-1" type="tel" max="20" min="1" value="' + item.value + '" style="width: 30px; height: 18px; padding-right: 4px;">行</div>')
  795. .appendTo(this);
  796. const $input = $obj.find('input');
  797. const event = () => {
  798. if (self.hasClass('context-menu-disabled')) return;
  799. item.batchInsert($input[0], root);
  800. };
  801. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  802. $input.click((e) => {e.stopPropagation();})
  803. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  804. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  805. };
  806. // 右键菜单
  807. $.contextMenu({
  808. selector: '#bills-spread',
  809. build: function ($trigger, e) {
  810. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  811. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  812. },
  813. items: {
  814. 'create': {
  815. name: '新增',
  816. icon: 'fa-sign-in',
  817. callback: function (key, opt) {
  818. billsTreeSpreadObj.baseOpr(billsSheet, 'add');
  819. },
  820. disabled: function (key, opt) {
  821. const sheet = billsSheet;
  822. const selection = sheet.getSelections();
  823. const sel = selection ? selection[0] : sheet.getSelections()[0];
  824. const row = sel ? sel.row : -1;
  825. const tree = sheet.zh_tree;
  826. if (!tree) return true;
  827. const first = sheet.zh_tree.nodes[row];
  828. const valid = !sheet.zh_setting.readOnly;
  829. return !(valid && first && first.level > 1);
  830. }
  831. },
  832. 'delete': {
  833. name: '删除',
  834. icon: 'fa-remove',
  835. callback: function (key, opt) {
  836. billsTreeSpreadObj.baseOpr(billsSheet, 'delete');
  837. },
  838. disabled: function (key, opt) {
  839. const sheet = billsSheet;
  840. const selection = sheet.getSelections();
  841. const sel = selection ? selection[0] : sheet.getSelections()[0];
  842. const row = sel ? sel.row : -1;
  843. const tree = sheet.zh_tree;
  844. if (!tree) return true;
  845. const first = sheet.zh_tree.nodes[row];
  846. let last = first, sameParent = true, nodeUsed = first.used;
  847. if (sel.rowCount > 1 && first) {
  848. for (let r = 1; r < sel.rowCount; r++) {
  849. const rNode = tree.nodes[sel.row + r];
  850. if (!rNode) {
  851. sameParent = false;
  852. break;
  853. }
  854. nodeUsed = nodeUsed || rNode.used;
  855. if (rNode.level > first.level) continue;
  856. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  857. sameParent = false;
  858. break;
  859. }
  860. last = rNode;
  861. }
  862. }
  863. const valid = !sheet.zh_setting.readOnly;
  864. return !(valid && first && sameParent && !(first.level === 1 && first.node_type) && !nodeUsed);
  865. }
  866. },
  867. 'sprBase': '-----------',
  868. 'batchInsert': {
  869. name: '批量插入',
  870. type: 'batchInsert',
  871. value: '2',
  872. icon: 'fa-sign-in',
  873. batchInsert: function (obj, root) {
  874. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  875. obj.value = obj.max;
  876. toastr.warning('批量插入不可多于' + obj.max);
  877. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  878. obj.value = obj.min;
  879. toastr.warning('批量插入不可少于' + obj.min);
  880. } else {
  881. billsTreeSpreadObj.baseOpr(billsSheet, 'add', parseInt(obj.value));
  882. root.$menu.trigger('contextmenu:hide');
  883. }
  884. },
  885. disabled: function (key, opt) {
  886. const sheet = billsSheet;
  887. const selection = sheet.getSelections();
  888. const sel = selection ? selection[0] : sheet.getSelections()[0];
  889. const row = sel ? sel.row : -1;
  890. const tree = sheet.zh_tree;
  891. if (!tree) return true;
  892. const first = sheet.zh_tree.nodes[row];
  893. const valid = !sheet.zh_setting.readOnly;
  894. return !(valid && first && first.level > 1);
  895. }
  896. },
  897. 'batchInsertBillsPos': {
  898. name: '批量插入清单-计量单元',
  899. icon: 'fa-sign-in',
  900. disabled: function (key, opt) {
  901. if (!isTz) return true;
  902. const select = SpreadJsObj.getSelectObject(billsSheet);
  903. if (select) {
  904. if (select.code && select.code !== '') {
  905. return !billsTree.isLeafXmj(select);
  906. } else {
  907. const parent = billsTree.getParent(select);
  908. return !(parent && billsTree.isLeafXmj(parent));
  909. }
  910. } else {
  911. return false;
  912. }
  913. },
  914. callback: function (key, opt) {
  915. if (!batchInsertObj) {
  916. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  917. } else {
  918. batchInsertObj.initView();
  919. }
  920. $('#batch').modal('show');
  921. }
  922. },
  923. 'sprBatch': '-----------',
  924. 'importGclBills2Xmj': {
  925. name: '导入工程量清单至项目节',
  926. icon: 'fa-file-excel-o',
  927. disabled: function (key, opt) {
  928. const node = SpreadJsObj.getSelectObject(billsSheet);
  929. return readOnly
  930. || (node.children && node.children.length > 0)
  931. || (!_.isNil(node.b_code) && node.b_code !== '')
  932. || billsTree.checkNodeUsed(node, pos);
  933. },
  934. callback: function (key, opt) {
  935. const node = SpreadJsObj.getSelectObject(billsSheet);
  936. importExcel.doImport({
  937. template: {
  938. hint: '工程量清单',
  939. url: '/template/导入工程量清单EXCEL格式.xls',
  940. },
  941. callback: function (sheet) {
  942. postDataCompress(window.location.pathname + '/upload-excel/gcl2xmj', {id: node.id, sheet: sheet}, function (result) {
  943. const sel = billsSheet.getSelections();
  944. const refreshNode = billsTree.loadPostData(result);
  945. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  946. if (refreshNode.create[0]) {
  947. if (sel && sel[0]) {
  948. billsSheet.setSelection(refreshNode.create[0].index, sel[0].col, sel[0].rowCount, sel[0].colCount);
  949. } else {
  950. billsSheet.setSelection(refreshNode.create[0].index, 0, 1, 1);
  951. }
  952. }
  953. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  954. }, null);
  955. }
  956. });
  957. }
  958. },
  959. }
  960. });
  961. }
  962. // 计量单元 相关方法&绑定spreadjs事件
  963. const posSpreadObj = {
  964. /**
  965. * 加载计量单元 根据当前台账选择节点
  966. */
  967. loadCurPosData: function () {
  968. const node = SpreadJsObj.getSelectObject(billsSheet);
  969. if (node) {
  970. const posData = pos.getLedgerPos(node.id) || [];
  971. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  972. } else {
  973. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  974. }
  975. SpreadJsObj.resetFieldReadOnly(posSheet);
  976. },
  977. editStarting: function (e, info) {
  978. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  979. },
  980. /**
  981. * 编辑单元格响应事件
  982. * @param {Object} e
  983. * @param {Object} info
  984. */
  985. editEnded: function (e, info) {
  986. if (!info.sheet.zh_setting) {
  987. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  988. return;
  989. }
  990. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  991. const col = info.sheet.zh_setting.cols[info.col];
  992. const orgText = posData ? posData[col.field] : null;
  993. const newText = trimInvalidChar(info.editingText);
  994. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  995. const node = posSpreadObj.billsNode;
  996. if (!node) {
  997. toastr.error('数据错误,请选择台账节点后再试');
  998. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  999. return;
  1000. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1001. toastr.error('父节点不可插入计量单元');
  1002. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1003. return;
  1004. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1005. toastr.error('项目节不可插入计量单元');
  1006. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1007. return;
  1008. }
  1009. const data = {postType: 'pos'};
  1010. if (col.field === 'name') {
  1011. if (newText === '' && posData) {
  1012. toastr.error('部位名称不可为空', 'error');
  1013. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1014. return;
  1015. } else if (!posData) {
  1016. if (newText && newText !== '') {
  1017. data.posPostType = 'add';
  1018. const sortData = info.sheet.zh_data;
  1019. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1020. data.postData = { name: newText, lid: node.id, porder: order};
  1021. } else {
  1022. return;
  1023. }
  1024. } else {
  1025. data.posPostType = 'update';
  1026. data.postData = {id: posData.id, name: newText};
  1027. }
  1028. } else if (!posData) {
  1029. toastr.warning('新增计量单元请先输入名称');
  1030. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1031. return;
  1032. } else {
  1033. data.posPostType = 'update';
  1034. data.postData = {id: posData.id};
  1035. if (col.type === 'Number') {
  1036. const num = _.toNumber(newText);
  1037. if (_.isFinite(num)) {
  1038. data.postData[col.field] = num;
  1039. } else {
  1040. try {
  1041. data.postData[col.field] = math.evaluate(transExpr(newText));
  1042. } catch(err) {
  1043. toastr.error('输入的表达式非法');
  1044. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1045. return;
  1046. }
  1047. }
  1048. } else {
  1049. data.postData[col.field] = newText;
  1050. }
  1051. }
  1052. postData(window.location.pathname + '/update', data, function (result) {
  1053. const updateRst = pos.updateDatas(result.pos);
  1054. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1055. if (updateRst.create.length > 0) {
  1056. posSpreadObj.loadCurPosData();
  1057. } else {
  1058. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1059. }
  1060. const loadResult = billsTree.loadPostData(result.ledger);
  1061. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1062. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1063. }, function () {
  1064. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1065. });
  1066. },
  1067. /**
  1068. * 删除按钮响应事件
  1069. * @param sheet
  1070. */
  1071. deletePress: function (sheet) {
  1072. if (!sheet.zh_settting) return;
  1073. const sortData = sheet.zh_data;
  1074. const datas = [], posSelects = [];
  1075. const sel = sheet.getSelections()[0];
  1076. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1077. let bDel = false;
  1078. const node = sortData[iRow];
  1079. if (node) {
  1080. const data = {id: node.id};
  1081. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1082. const colSetting = sheet.zh_setting.cols[iCol];
  1083. if (colSetting.field === 'name') {
  1084. toastr.error('部位名称不能为空');
  1085. return;
  1086. }
  1087. const style = sheet.getStyle(iRow, iCol);
  1088. if (!style.locked) {
  1089. const colSetting = sheet.zh_setting.cols[iCol];
  1090. data[colSetting.field] = null;
  1091. bDel = true;
  1092. }
  1093. }
  1094. if (bDel) {
  1095. datas.push(data);
  1096. posSelects.push(node);
  1097. }
  1098. }
  1099. }
  1100. if (datas.length > 0) {
  1101. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  1102. pos.updateDatas(result.pos);
  1103. posSpreadObj.loadCurPosData();
  1104. const loadResult = billsTree.loadPostData(result.ledger);
  1105. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1106. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1107. }, function () {
  1108. posSpreadObj.loadCurPosData();
  1109. });
  1110. }
  1111. },
  1112. /**
  1113. * 删除 计量单元
  1114. * @param sheet
  1115. */
  1116. deletePos: function (sheet) {
  1117. const selection = sheet.getSelections();
  1118. const data = {
  1119. postType: 'pos',
  1120. posPostType: 'delete',
  1121. postData: [],
  1122. };
  1123. const row = selection[0].row, count = selection[0].rowCount;
  1124. const sortData = sheet.zh_data;
  1125. for (let iRow = 0; iRow < count; iRow++) {
  1126. const posData = sortData[iRow + row];
  1127. if (posData) {
  1128. if (posData.used) {
  1129. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1130. return;
  1131. }
  1132. data.postData.push(sortData[iRow + row].id);
  1133. }
  1134. }
  1135. if (data.postData.length > 0) {
  1136. postData(window.location.pathname + '/update', data, function (result) {
  1137. pos.removeDatas(result.pos);
  1138. sheet.deleteRows(row, count);
  1139. const loadResult = billsTree.loadPostData(result.ledger);
  1140. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1141. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1142. });
  1143. }
  1144. },
  1145. clipboardPasting: function (e, info) {
  1146. if (!info.sheet.zh_setting) {
  1147. info.cancel = true;
  1148. return;
  1149. }
  1150. const range = info.cellRange;
  1151. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  1152. const posData = info.sheet.zh_data[iRow];
  1153. if (posData && posData.used) {
  1154. toastr.warning('"' + pos.name +'"已计量,请勿修改');
  1155. info.cancel = true;
  1156. return;
  1157. }
  1158. }
  1159. },
  1160. /**
  1161. * 粘贴单元格响应事件
  1162. * @param e
  1163. * @param info
  1164. */
  1165. clipboardPasted: function (e, info) {
  1166. const node = SpreadJsObj.getSelectObject(billsSheet);
  1167. if (node.code && (node.code !== '')) {
  1168. toastr.error('项目节不可含有节点明细');
  1169. posSpreadObj.loadCurPosData();
  1170. return;
  1171. }
  1172. if (node.children && (node.children.length > 0)) {
  1173. toastr.error('仅节点子项可以含有计量单元');
  1174. posSpreadObj.loadCurPosData();
  1175. return;
  1176. }
  1177. if (!info.sheet.zh_setting) {
  1178. posSpreadObj.loadCurPosData();
  1179. return;
  1180. }
  1181. const data = [];
  1182. const sortData = info.sheet.zh_data || [];
  1183. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1184. if (info.cellRange.col !== 0) {
  1185. toastr.warning('新增计量单元请先输入名称');
  1186. posSpreadObj.loadCurPosData();
  1187. return;
  1188. }
  1189. }
  1190. let bHint = false;
  1191. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1192. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1193. let bPaste = true;
  1194. const curRow = info.cellRange.row + iRow;
  1195. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1196. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1197. const curCol = info.cellRange.col + iCol;
  1198. const colSetting = info.sheet.zh_setting.cols[curCol];
  1199. posData[colSetting.field] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1200. if (colSetting.type === 'Number') {
  1201. const num = _.toNumber(posData[colSetting.field]);
  1202. if (num) {
  1203. posData[colSetting.field] = num;
  1204. } else {
  1205. try {
  1206. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1207. } catch (err) {
  1208. if (!bHint) {
  1209. toastr.warning('粘贴了非法表达式,已过滤');
  1210. bHint = true;
  1211. }
  1212. bPaste = false;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. if (bPaste) {
  1218. data.push(posData);
  1219. }
  1220. }
  1221. if (data.length === 0) {
  1222. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1223. return;
  1224. }
  1225. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  1226. pos.updateDatas(result.pos);
  1227. posSpreadObj.loadCurPosData();
  1228. const loadResult = billsTree.loadPostData(result.ledger);
  1229. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1230. posSpreadObj.loadCurPosData();
  1231. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1232. }, function () {
  1233. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1234. });
  1235. },
  1236. selectionChanged: function (e, info) {
  1237. const col = info.sheet.zh_setting.cols[info.newSelections[0].col];
  1238. const cell = info.sheet.getCell(info.newSelections[0].col, info.newSelections[0].col);
  1239. if (col && col.type === 'Number') {
  1240. const data = SpreadJsObj.getSelectObject(info.sheet);
  1241. if (data) {
  1242. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  1243. .attr('row', info.newSelections[0].row).attr('readOnly', readOnly || cell.locked());
  1244. } else {
  1245. $('#pos-expr').val('').attr('readOnly', true);
  1246. }
  1247. } else {
  1248. $('#pos-expr').val('').attr('readOnly', true);
  1249. }
  1250. },
  1251. addPegs: function (pegs) {
  1252. if (!pegs || pegs.length <= 0) return;
  1253. const node = SpreadJsObj.getSelectObject(billsSheet);
  1254. if (!node) return;
  1255. const sheet = posSpread.getActiveSheet();
  1256. const sortData = sheet.zh_data || [];
  1257. let order = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1258. pegs.forEach(function (p) {p.porder = ++order; p.lid = node.id});
  1259. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: pegs}, function (result) {
  1260. pos.updateDatas(result.pos);
  1261. posSpreadObj.loadCurPosData();
  1262. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1263. });
  1264. }
  1265. };
  1266. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  1267. if (!readOnly && isTz) {
  1268. $('#pos-expr').bind('change mouseleave', function () {
  1269. if (this.readOnly) return;
  1270. const expr = $(this);
  1271. const select = SpreadJsObj.getSelectObject(posSheet);
  1272. const row = posSheet.getSelections()[0].row;
  1273. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  1274. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  1275. const data = {id: select.id};
  1276. if (newValue !== '') {
  1277. const num = _.toNumber(newValue);
  1278. if (num) {
  1279. data[field] = num;
  1280. } else {
  1281. try {
  1282. data[field] = math.evaluate(transExpr(newValue));
  1283. } catch (err) {
  1284. toastr.error('输入的表达式非法');
  1285. return;
  1286. }
  1287. }
  1288. } else {
  1289. data[field] = null;
  1290. }
  1291. // 更新至服务器
  1292. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: data}, function (result) {
  1293. const updateRst = pos.updateDatas(result.pos);
  1294. expr.val(select[field]);
  1295. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1296. SpreadJsObj.reLoadRowData(posSheet, row);
  1297. const loadResult = billsTree.loadPostData(result.ledger);
  1298. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1299. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1300. });
  1301. });
  1302. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  1303. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  1304. posSpread.bind(spreadNS.Events.ClipboardPasting, posSpreadObj.clipboardPasting);
  1305. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  1306. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  1307. const mergePeg = NewMergePeg({ callback: posSpreadObj.addPegs });
  1308. $.contextMenu({
  1309. selector: '#pos-spread',
  1310. build: function ($trigger, e) {
  1311. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1312. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1313. },
  1314. items: {
  1315. 'delete': {
  1316. name: '删除',
  1317. icon: 'fa-remove',
  1318. disabled: function (key, opt) {
  1319. if (posSheet.zh_data) {
  1320. const selection = posSheet.getSelections();
  1321. return posSheet.zh_data.length < selection[0].row + selection[0].rowCount;
  1322. } else {
  1323. return true;
  1324. }
  1325. },
  1326. callback: function (key, opt) {
  1327. posSpreadObj.deletePos(posSheet);
  1328. }
  1329. },
  1330. 'merge-peg': {
  1331. name: '合并起讫桩号',
  1332. disabled: function (key, opt) {
  1333. const node = SpreadJsObj.getSelectObject(billsSheet);
  1334. return _.isNil(node) || _.isNil(node.b_code) || node.b_code === '';
  1335. },
  1336. callback: function (key, opt) {
  1337. mergePeg.show();
  1338. },
  1339. visible: function (key, opt) {
  1340. return isTz;
  1341. }
  1342. }
  1343. }
  1344. });
  1345. }
  1346. // 加载清单&计量单元数据
  1347. postData(window.location.pathname + '/load', {}, function (result) {
  1348. billsTree.loadDatas(result.bills);
  1349. treeCalc.calculateAll(billsTree);
  1350. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree);
  1351. SpreadJsObj.loadTopAndSelect(billsSheet, ckBillsSpread);
  1352. pos.loadDatas(result.pos);
  1353. posSpreadObj.loadCurPosData();
  1354. SpreadJsObj.resetTopAndSelect(posSheet);
  1355. }, null);
  1356. $.divResizer({
  1357. select: '#revise-resize',
  1358. callback: function () {
  1359. billsSpread.refresh();
  1360. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1361. $(".sp-wrap").height(bcontent-30);
  1362. posSpread.refresh();
  1363. }
  1364. });
  1365. class DealBills {
  1366. constructor (selector, spreadSetting) {
  1367. const self = this;
  1368. this.loaded = false;
  1369. this.obj = $(selector)[0];
  1370. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1371. this.spreadSetting = spreadSetting;
  1372. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1373. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1374. if (!readOnly) {
  1375. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1376. const dealSheet = info.sheet;
  1377. const mainSheet = billsSheet;
  1378. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1379. if (!dealBills) { return; }
  1380. const mainTree = mainSheet.zh_tree;
  1381. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1382. if (!mainNode || !mainTree) { return; }
  1383. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1384. toastr.warning('非最底层项目下,不应添加节点');
  1385. return;
  1386. }
  1387. postData(window.location.pathname + '/update', {
  1388. postType: 'add-deal',
  1389. postData: {
  1390. id: mainNode.ledger_id,
  1391. type: mainNode.code ? 'child' : 'next',
  1392. dealBills: {
  1393. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1394. unit_price: dealBills.unit_price,
  1395. }
  1396. },
  1397. }, function (result) {
  1398. const refreshData = mainTree.loadPostData(result);
  1399. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1400. const sel = mainSheet.getSelections()[0];
  1401. if (sel && refreshData.create[0]) {
  1402. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1403. }
  1404. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1405. billsSpread.focus();
  1406. posSpreadObj.loadCurPosData();
  1407. });
  1408. });
  1409. }
  1410. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1411. }
  1412. loadData (callback) {
  1413. if (this.loaded) {
  1414. if (callback) callback();
  1415. return;
  1416. }
  1417. const self = this;
  1418. postData(this.url+'/get-data', {}, function (data) {
  1419. self.data = data;
  1420. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1421. self.loaded = true;
  1422. if (callback) callback();
  1423. });
  1424. }
  1425. calculateData () {
  1426. if (this.data) {
  1427. for (const d of this.data) {
  1428. d.total_price = _.multiply(d.quantity, d.unit_price);
  1429. }
  1430. }
  1431. }
  1432. }
  1433. class BgBills {
  1434. constructor (selector, spreadSetting) {
  1435. this.loaded = false;
  1436. this.obj = $(selector)[0];
  1437. this.spreadSetting = spreadSetting;
  1438. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1439. this.sheet = this.spread.getActiveSheet();
  1440. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  1441. if (!readOnly) {
  1442. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1443. const dealSheet = info.sheet;
  1444. const mainSheet = billsSheet;
  1445. const bgBills = SpreadJsObj.getSelectObject(dealSheet);
  1446. if (!bgBills) { return; }
  1447. const mainTree = mainSheet.zh_tree;
  1448. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1449. if (!mainNode || !mainTree) { return; }
  1450. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1451. toastr.warning('非最底层项目下,不应添加变更清单');
  1452. return;
  1453. }
  1454. postData(window.location.pathname + '/update', {
  1455. postType: 'add-bg',
  1456. postData: {
  1457. id: mainNode.ledger_id,
  1458. type: mainNode.code ? 'child' : 'next',
  1459. dealBills: {
  1460. b_code: bgBills.code, name: bgBills.name, unit: bgBills.unit,
  1461. unit_price: bgBills.unit_price,
  1462. }
  1463. },
  1464. }, function (result) {
  1465. const refreshData = mainTree.loadPostData(result);
  1466. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1467. const sel = mainSheet.getSelections()[0];
  1468. if (sel && refreshData.create[0]) {
  1469. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1470. }
  1471. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1472. billsSpread.focus();
  1473. posSpreadObj.loadCurPosData();
  1474. });
  1475. });
  1476. }
  1477. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1478. }
  1479. loadData () {
  1480. if (this.loaded) return;
  1481. const self = this;
  1482. postData('/tender/' + window.location.pathname.split('/')[2] +'/change/bills', {type: 'gather'}, function (data) {
  1483. self.data = data;
  1484. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1485. self.loaded = true;
  1486. });
  1487. }
  1488. }
  1489. class BatchInsertBillsPosObj {
  1490. constructor (obj) {
  1491. const self = this;
  1492. this.obj = obj;
  1493. this.billsCount = 6;
  1494. this.posCount = 1000;
  1495. // 初始化 清单编号窗口 参数
  1496. this.qdSpreadSetting = {
  1497. cols: [
  1498. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1499. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1500. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1501. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1502. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1503. ],
  1504. emptyRows: this.billsCount,
  1505. headRows: 1,
  1506. headRowHeight: [32],
  1507. headerFont: '12px 微软雅黑',
  1508. font: '12px 微软雅黑',
  1509. };
  1510. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1511. // 初始化 部位数量复核表 参数
  1512. this.posSpreadSetting = {
  1513. cols: [
  1514. {title: '名称', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1515. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1516. ],
  1517. emptyRows: this.posCount,
  1518. headRows: 1,
  1519. headRowHeight: [32],
  1520. headerFont: '12px 微软雅黑',
  1521. font: '12px 微软雅黑',
  1522. };
  1523. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1524. this.posSpreadSetting.cols.push(
  1525. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1526. )
  1527. }
  1528. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1529. // 初始化 签约节点 参数
  1530. this.dealSpreadSetting = {
  1531. cols: [
  1532. {title: '清单编号', field: 'code', width: 80, hAlign: 0, formatter: '@', readOnly: true},
  1533. {title: '名称', field: 'name', width: 120, hAlign: 0, formatter: '@', readOnly: true},
  1534. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1535. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1536. ],
  1537. emptyRows: 0,
  1538. headRows: 1,
  1539. headRowHeight: [32],
  1540. headerFont: '12px 微软雅黑',
  1541. font: '12px 微软雅黑',
  1542. };
  1543. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1544. // 初始化 清单编号、部位数量复核表 表格
  1545. this.initView();
  1546. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1547. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1548. // 拉取签约节点数据
  1549. dealBills.loadData(() => {
  1550. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1551. });
  1552. // 双击签约节点,自动添加到清单编号窗口
  1553. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1554. const deal = info.sheet.zh_data[info.row];
  1555. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1556. const sel = qdSheet.getSelections()[0];
  1557. qdSheet.getCell(sel.row, 0).value(deal.code);
  1558. qdSheet.getCell(sel.row, 1).value(deal.name);
  1559. qdSheet.getCell(sel.row, 2).value(deal.unit);
  1560. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  1561. if (sel.row + 1 === qdSheet.getRowCount()) {
  1562. const count = sel.row + 2;
  1563. qdSheet.setRowCount(count);
  1564. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  1565. const colCount = posSheet.getColumnCount() + 1;
  1566. posSheet.setColumnCount(colCount);
  1567. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('清单' + count);
  1568. }
  1569. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  1570. qdSheet.getParent().focus();
  1571. });
  1572. this.qdSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  1573. const billsCount = info.sheet.getRowCount(), posSheet = self.posSpread.getActiveSheet();
  1574. const count = posSheet.getColumnCount() - 2;
  1575. if (billsCount > count) {
  1576. posSheet.setColumnCount(billsCount + 2);
  1577. for (let i = count + 1; i <= billsCount; i++) {
  1578. info.sheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  1579. posSheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  1580. }
  1581. }
  1582. });
  1583. this.qdSpread.bind(spreadNS.Events.EditEnded, function (e, info) {
  1584. if (info.col === 0) {
  1585. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  1586. if (dealBills && dealBills.length > 0) {
  1587. const bills = _.find(dealBills, {code: info.editingText});
  1588. if (bills) {
  1589. info.sheet.getCell(info.row, 1).value(bills.name);
  1590. info.sheet.getCell(info.row, 2).value(bills.unit);
  1591. info.sheet.getCell(info.row, 3).value(bills.unit_price);
  1592. }
  1593. }
  1594. }
  1595. });
  1596. this.obj.bind('shown.bs.modal', function () {
  1597. self.qdSpread.refresh();
  1598. self.posSpread.refresh();
  1599. self.dealSpread.refresh();
  1600. });
  1601. $('#batch-ok').click(function () {
  1602. const selection = billsSheet.getSelections();
  1603. const row = selection[0].row;
  1604. const select = billsTree.nodes[row];
  1605. if (select) {
  1606. const insertData = {};
  1607. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  1608. insertData.id = select[billsTree.setting.id];
  1609. insertData.batchData = self.getBatchData();
  1610. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  1611. pos.updateDatas(data.pos);
  1612. const result = billsTree.loadPostData(data.ledger);
  1613. billsTreeSpreadObj.refreshTree(billsSheet, result);
  1614. billsTreeSpreadObj.refreshOperationValid(billsSheet, selection);
  1615. self.obj.modal('hide');
  1616. }, null, true);
  1617. }
  1618. });
  1619. $.contextMenu({
  1620. selector: '.batch-l-t',
  1621. build: function ($trigger, e) {
  1622. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  1623. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1624. },
  1625. items: {
  1626. 'create': {
  1627. name: '新增行',
  1628. icon: 'fa-sign-in',
  1629. callback: function (key, opt) {
  1630. const qdSheet = self.qdSpread.getActiveSheet();
  1631. const posSheet = self.posSpread.getActiveSheet();
  1632. qdSheet.addRows(qdSheet.getRowCount(), 1);
  1633. const index = qdSheet.getRowCount();
  1634. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  1635. posSheet.addColumns(posSheet.getColumnCount(), 1);
  1636. posSheet.getCell(0, index + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + index);
  1637. },
  1638. },
  1639. 'delete': {
  1640. name: '删除行',
  1641. icon: 'fa-remove',
  1642. callback: function (key, opt) {
  1643. const qdSheet = self.qdSpread.getActiveSheet();
  1644. const posSheet = self.posSpread.getActiveSheet();
  1645. const sel = qdSheet.getSelections()[0];
  1646. qdSheet.deleteRows(sel.row, sel.rowCount);
  1647. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  1648. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  1649. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  1650. }
  1651. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  1652. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  1653. }
  1654. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  1655. },
  1656. },
  1657. }
  1658. });
  1659. }
  1660. // 初始化左侧表格
  1661. initView () {
  1662. // 初始化 清单编号
  1663. const qdSheet = this.qdSpread.getActiveSheet();
  1664. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  1665. SpreadJsObj.refreshColumnAlign(qdSheet);
  1666. // 清理原有数据
  1667. SpreadJsObj.beginMassOperation(qdSheet);
  1668. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1669. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  1670. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  1671. }
  1672. qdSheet.setSelection(0, 0, 1 ,1);
  1673. SpreadJsObj.endMassOperation(qdSheet);
  1674. // 初始化 部位数量复核表
  1675. const posSheet = this.posSpread.getActiveSheet();
  1676. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  1677. SpreadJsObj.refreshColumnAlign(posSheet);
  1678. // 清理原有数据
  1679. SpreadJsObj.beginMassOperation(posSheet);
  1680. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  1681. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1682. posSheet.setSelection(0, 0, 1 ,1);
  1683. SpreadJsObj.endMassOperation(posSheet);
  1684. // 检查签约节点数据,以工具栏数据为准
  1685. if (dealBills) {
  1686. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1687. }
  1688. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  1689. }
  1690. // 获取界面数据
  1691. getBatchData () {
  1692. const result = [];
  1693. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  1694. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  1695. if (qdSheet.getText(iRow, 0) === '') { continue; }
  1696. const qd = {
  1697. b_code: qdSheet.getText(iRow, 0),
  1698. name: qdSheet.getText(iRow, 1),
  1699. unit: qdSheet.getText(iRow, 2),
  1700. price: _.toNumber(qdSheet.getText(iRow, 3)),
  1701. pos: [],
  1702. };
  1703. result.push(qd);
  1704. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  1705. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  1706. if (value !== 0 && !isNaN(value)) {
  1707. qd.pos.push({
  1708. name: posSheet.getText(iPosRow, 0),
  1709. drawing_code: posSheet.getText(iPosRow, 1),
  1710. quantity: value,
  1711. });
  1712. }
  1713. }
  1714. }
  1715. return result;
  1716. }
  1717. }
  1718. const dealBills = new DealBills('#deal-bills-spread', {
  1719. cols: [
  1720. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  1721. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1722. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1723. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1724. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1725. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1726. ],
  1727. emptyRows: 0,
  1728. headRows: 1,
  1729. headRowHeight: [32],
  1730. defaultRowHeight: 21,
  1731. headerFont: '12px 微软雅黑',
  1732. font: '12px 微软雅黑',
  1733. });
  1734. const bgBills = new BgBills('#bg-bills-spread', {
  1735. cols: [
  1736. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  1737. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1738. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1739. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1740. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1741. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1742. ],
  1743. emptyRows: 0,
  1744. headRows: 1,
  1745. headRowHeight: [32],
  1746. defaultRowHeight: 21,
  1747. headerFont: '12px 微软雅黑',
  1748. font: '12px 微软雅黑',
  1749. });
  1750. $.divResizer({
  1751. select: '#revise-right-spr',
  1752. callback: function () {
  1753. billsSpread.refresh();
  1754. if (posSpread) {
  1755. posSpread.refresh();
  1756. }
  1757. if (stdXmj) {
  1758. stdXmj.spread.refresh();
  1759. }
  1760. if (stdGcl) {
  1761. stdGcl.spread.refresh();
  1762. }
  1763. if (dealBills) {
  1764. dealBills.spread.refresh();
  1765. }
  1766. if (bgBills) {
  1767. bgBills.spread.refresh();
  1768. }
  1769. if (searchLedger) {
  1770. searchLedger.spread.refresh();
  1771. }
  1772. }
  1773. });
  1774. $.subMenu({
  1775. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1776. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1777. key: 'menu.1.0.0',
  1778. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1779. callback: function (info) {
  1780. if (info.mini) {
  1781. $('.panel-title').addClass('fluid');
  1782. $('#sub-menu').removeClass('panel-sidebar');
  1783. } else {
  1784. $('.panel-title').removeClass('fluid');
  1785. $('#sub-menu').addClass('panel-sidebar');
  1786. }
  1787. autoFlashHeight();
  1788. billsSpread.refresh();
  1789. if (posSpread) {
  1790. posSpread.refresh();
  1791. }
  1792. if (stdXmj) {
  1793. stdXmj.spread.refresh();
  1794. }
  1795. if (stdGcl) {
  1796. stdGcl.spread.refresh();
  1797. }
  1798. if (dealBills) {
  1799. dealBills.spread.refresh();
  1800. }
  1801. if (bgBills) {
  1802. bgBills.spread.refresh();
  1803. }
  1804. if (searchLedger) {
  1805. searchLedger.spread.refresh();
  1806. }
  1807. }
  1808. });
  1809. const showSideTools = function (show) {
  1810. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1811. if (show) {
  1812. right.show();
  1813. autoFlashHeight();
  1814. /**
  1815. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1816. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1817. * 故需要通过最终的parent.width再计算一次left.width
  1818. *
  1819. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1820. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1821. *
  1822. */
  1823. //left.css('width', parent.width() - right.outerWidth());
  1824. //left.css('width', parent.width() - right.outerWidth());
  1825. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1826. left.css('width', percent + '%');
  1827. } else {
  1828. left.width(parent.width());
  1829. right.hide();
  1830. }
  1831. };
  1832. $('#content-tab').addClass('active');
  1833. $('#xd-content').addClass('active');
  1834. showSideTools(true);
  1835. billsSpread.refresh();
  1836. if (posSpread) {
  1837. posSpread.refresh();
  1838. }
  1839. const stdLibCellDoubleClick = function (e, info) {
  1840. const stdSheet = info.sheet;
  1841. const mainSheet = billsSheet;
  1842. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1843. const stdTree = stdSheet.zh_tree;
  1844. const stdNode = stdTree.nodes[info.row];
  1845. const mainTree = mainSheet.zh_tree;
  1846. const sel = mainSheet.getSelections()[0];
  1847. const mainNode = mainTree.nodes[sel.row];
  1848. if (!stdNode) return;
  1849. if (info.sheet.zh_setting.stdType === 'gcl') {
  1850. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1851. toastr.warning('非最底层项目下,不应添加节点');
  1852. return;
  1853. }
  1854. }
  1855. postData(window.location.pathname + '/update', {
  1856. postType: 'add-std',
  1857. postData: {
  1858. id: mainTree.getNodeKey(mainNode),
  1859. tender_id: mainNode.tender_id,
  1860. stdType: info.sheet.zh_setting.stdType,
  1861. stdLibId: stdNode.list_id,
  1862. stdNode: stdTree.getNodeKey(stdNode)
  1863. }
  1864. }, function (result) {
  1865. const refreshNode = mainTree.loadPostData(result);
  1866. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  1867. if (sel) {
  1868. if (refreshNode.create && refreshNode.create.length > 0) {
  1869. mainSheet.setSelection(refreshNode.create[refreshNode.create.length - 1].index, sel.col, sel.rowCount, sel.colCount);
  1870. } else {
  1871. const node = _.find(mainTree.nodes, {code: stdNode.code, name: stdNode.name});
  1872. if (node) {
  1873. mainSheet.setSelection(mainTree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  1874. }
  1875. }
  1876. }
  1877. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1878. billsSpread.focus();
  1879. posSpreadObj.loadCurPosData();
  1880. });
  1881. };
  1882. const stdXmjSetting = {
  1883. selector: '#std-xmj',
  1884. stdType: 'xmj',
  1885. treeSetting: {
  1886. id: 'chapter_id',
  1887. pid: 'pid',
  1888. order: 'order',
  1889. level: 'level',
  1890. rootId: -1,
  1891. keys: ['id', 'list_id', 'chapter_id'],
  1892. },
  1893. spreadSetting: {
  1894. cols: [
  1895. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1896. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1897. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1898. ],
  1899. treeCol: 0,
  1900. emptyRows: 0,
  1901. headRows: 1,
  1902. headRowHeight: [32],
  1903. defaultRowHeight: 21,
  1904. headerFont: '12px 微软雅黑',
  1905. font: '12px 微软雅黑',
  1906. headColWidth: [0],
  1907. },
  1908. cellDoubleClick: stdLibCellDoubleClick,
  1909. page: 'revise',
  1910. tid: window.location.pathname.split('/')[2],
  1911. };
  1912. const stdGclSetting = {
  1913. selector: '#std-gcl',
  1914. stdType: 'gcl',
  1915. treeSetting: {
  1916. id: 'bill_id',
  1917. pid: 'pid',
  1918. order: 'order',
  1919. level: 'level',
  1920. rootId: -1,
  1921. keys: ['id', 'list_id', 'bill_id']
  1922. },
  1923. spreadSetting: {
  1924. cols: [
  1925. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1926. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1927. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1928. ],
  1929. treeCol: 0,
  1930. emptyRows: 0,
  1931. headRows: 1,
  1932. headRowHeight: [32],
  1933. defaultRowHeight: 21,
  1934. headerFont: '12px 微软雅黑',
  1935. font: '12px 微软雅黑',
  1936. headColWidth: [0],
  1937. },
  1938. cellDoubleClick: stdLibCellDoubleClick,
  1939. page: 'revise',
  1940. tid: window.location.pathname.split('/')[2],
  1941. };
  1942. // 展开收起标准节点
  1943. $('a', '#side-menu').bind('click', function (e) {
  1944. e.preventDefault();
  1945. const tab = $(this), tabPanel = $(tab.attr('content'));
  1946. // 展开工具栏、切换标签
  1947. if (!tab.hasClass('active')) {
  1948. const close = $('.active', '#side-menu').length === 0;
  1949. $('a', '#side-menu').removeClass('active');
  1950. tab.addClass('active');
  1951. $('.tab-content .tab-pane').removeClass('active');
  1952. tabPanel.addClass('active');
  1953. showSideTools(tab.hasClass('active'));
  1954. if (tab.attr('content') === '#std-xmj') {
  1955. if (!stdXmj) {
  1956. stdXmj = new stdLib(stdXmjSetting);
  1957. }
  1958. stdXmj.spread.refresh();
  1959. } else if (tab.attr('content') === '#std-gcl') {
  1960. if (!stdGcl) {
  1961. stdGcl = new stdLib(stdGclSetting);
  1962. }
  1963. stdGcl.spread.refresh();
  1964. } else if (tab.attr('content') === '#deal-bills') {
  1965. dealBills.loadData();
  1966. dealBills.spread.refresh();
  1967. } else if (tab.attr('content') === '#bg-bills') {
  1968. bgBills.loadData();
  1969. bgBills.spread.refresh();
  1970. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1971. if (!searchLedger) {
  1972. searchLedger = $.billsSearch({
  1973. selector: '#search',
  1974. searchSpread: billsSpread,
  1975. resultSpreadSetting: {
  1976. cols: [
  1977. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1978. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1979. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  1980. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1981. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1982. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1983. ],
  1984. emptyRows: 0,
  1985. headRows: 1,
  1986. headRowHeight: [32],
  1987. defaultRowHeight: 21,
  1988. headerFont: '12px 微软雅黑',
  1989. font: '12px 微软雅黑',
  1990. },
  1991. afterLocated: function () {
  1992. posSpreadObj.loadCurPosData();
  1993. }
  1994. });
  1995. }
  1996. searchLedger.spread.refresh();
  1997. }
  1998. }
  1999. else {// 收起工具栏
  2000. tab.removeClass('active');
  2001. tabPanel.removeClass('active');
  2002. showSideTools(tab.hasClass('active'));
  2003. }
  2004. billsSpread.refresh();
  2005. if (posSpread) {
  2006. posSpread.refresh();
  2007. }
  2008. });
  2009. if (!readOnly) {
  2010. // 修订详情 保存
  2011. $('#save').click(function () {
  2012. const content = $('#content').val();
  2013. postData('save', { content: content }, function () {
  2014. $('#content').attr('org-value', content);
  2015. });
  2016. });
  2017. }
  2018. // 显示层次
  2019. (function (select, sheet) {
  2020. $(select).click(function () {
  2021. if (!sheet.zh_tree) return;
  2022. const tag = $(this).attr('tag');
  2023. const tree = sheet.zh_tree;
  2024. switch (tag) {
  2025. case "1":
  2026. case "2":
  2027. case "3":
  2028. case "4":
  2029. case "5":
  2030. tree.expandByLevel(parseInt(tag));
  2031. SpreadJsObj.refreshTreeRowVisible(sheet);
  2032. break;
  2033. case "last":
  2034. tree.expandByCustom(() => { return true; });
  2035. SpreadJsObj.refreshTreeRowVisible(sheet);
  2036. break;
  2037. case "leafXmj":
  2038. tree.expandToLeafXmj();
  2039. SpreadJsObj.refreshTreeRowVisible(sheet);
  2040. break;
  2041. }
  2042. });
  2043. })('a[name=showLevel]', billsSheet);
  2044. });