ledger.js 116 KB

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