ledger.js 98 KB

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