ledger.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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, tree.nodes.indexOf(node)]);
  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, tree.nodes.indexOf(node)]);
  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, result.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. if (document.activeElement.id !== "bills-expr") return;
  900. const expr = $(this);
  901. const sheet = ledgerSpread.getActiveSheet();
  902. const select = SpreadJsObj.getSelectObject(sheet);
  903. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  904. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  905. const data = {
  906. id: select.id,
  907. tender_id: select.tender_id,
  908. ledger_id: select.ledger_id
  909. };
  910. if (newValue !== '') {
  911. const num = _.toNumber(newValue);
  912. if (num) {
  913. data[field] = num;
  914. } else {
  915. try {
  916. data[field] = math.evaluate(transExpr(newValue));
  917. } catch (err) {
  918. toastr.error('输入的表达式非法');
  919. return;
  920. }
  921. }
  922. } else {
  923. data[field] = null;
  924. }
  925. // 更新至服务器
  926. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  927. const refreshNode = ledgerTree.loadPostData(result);
  928. expr.val(select[field]);
  929. treeOperationObj.refreshTree(sheet, refreshNode);
  930. });
  931. });
  932. }
  933. let batchInsertObj;
  934. $.contextMenu.types.batchInsert = function (item, opt, root) {
  935. const self = this;
  936. if ($.isFunction(item.icon)) {
  937. item._icon = item.icon.call(this, this, $t, key, item);
  938. } else {
  939. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  940. // to enable font awesome
  941. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  942. } else {
  943. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  944. }
  945. }
  946. this.addClass(item._icon);
  947. 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>')
  948. .appendTo(this);
  949. const $input = $obj.find('input');
  950. const event = () => {
  951. if (self.hasClass('context-menu-disabled')) return;
  952. item.batchInsert($input[0], root);
  953. };
  954. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  955. $input.click((e) => {e.stopPropagation();})
  956. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  957. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  958. };
  959. // 右键菜单
  960. const billsContextMenuOptions = {
  961. selector: '#ledger-spread',
  962. build: function ($trigger, e) {
  963. const target = SpreadJsObj.safeRightClickSelection($trigger, e, ledgerSpread);
  964. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  965. },
  966. items: {},
  967. };
  968. if (!readOnly) {
  969. billsContextMenuOptions.items.create = {
  970. name: '新增',
  971. icon: 'fa-sign-in',
  972. callback: function (key, opt) {
  973. treeOperationObj.addNode(ledgerSpread.getActiveSheet());
  974. },
  975. disabled: function (key, opt) {
  976. const sheet = ledgerSpread.getActiveSheet();
  977. const selection = sheet.getSelections();
  978. const sel = selection ? selection[0] : sheet.getSelections()[0];
  979. const row = sel ? sel.row : -1;
  980. const tree = sheet.zh_tree;
  981. if (!tree) return true;
  982. const first = sheet.zh_tree.nodes[row];
  983. let last = first, sameParent = true;
  984. if (sel.rowCount > 1) {
  985. for (let r = 1; r < sel.rowCount; r++) {
  986. const rNode = tree.nodes[sel.row + r];
  987. if (rNode.level > first.level) continue;
  988. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  989. sameParent = false;
  990. break;
  991. }
  992. last = rNode;
  993. }
  994. }
  995. const valid = !sheet.zh_setting.readOnly;
  996. return !(valid && first && first.level > 1);
  997. },
  998. visible: function (key, opt) {
  999. return !readOnly;
  1000. }
  1001. };
  1002. billsContextMenuOptions.items.delete = {
  1003. name: '删除',
  1004. icon: 'fa-remove',
  1005. callback: function (key, opt) {
  1006. treeOperationObj.deleteNode(ledgerSpread.getActiveSheet());
  1007. },
  1008. disabled: function (key, opt) {
  1009. const sheet = ledgerSpread.getActiveSheet();
  1010. const selection = sheet.getSelections();
  1011. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1012. const row = sel ? sel.row : -1;
  1013. const tree = sheet.zh_tree;
  1014. if (!tree) return true;
  1015. const first = sheet.zh_tree.nodes[row];
  1016. let last = first, sameParent = true;
  1017. if (sel.rowCount > 1) {
  1018. for (let r = 1; r < sel.rowCount; r++) {
  1019. const rNode = tree.nodes[sel.row + r];
  1020. if (rNode.level > first.level) continue;
  1021. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  1022. sameParent = false;
  1023. break;
  1024. }
  1025. last = rNode;
  1026. }
  1027. }
  1028. const valid = !sheet.zh_setting.readOnly;
  1029. return !(valid && first && sameParent && !(first.level === 1 && first.node_type));
  1030. },
  1031. visible: function (key, opt) {
  1032. return !readOnly;
  1033. }
  1034. };
  1035. billsContextMenuOptions.items.sprBase = '-----------';
  1036. }
  1037. billsContextMenuOptions.items.copyBlock = {
  1038. name: '复制整块',
  1039. icon: 'fa-files-o',
  1040. callback: function (key, opt) {
  1041. /*ledgerSpread.commandManager().execute({
  1042. cmd:"copy",
  1043. sheetName:ledgerSpread.getActiveSheet().name()
  1044. });*/
  1045. treeOperationObj.block = [];
  1046. const copyBlockList = [];
  1047. const sheet = ledgerSpread.getActiveSheet();
  1048. const sel = sheet.getSelections()[0];
  1049. let iRow = sel.row;
  1050. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1051. while (iRow < sel.row + sel.rowCount) {
  1052. const node = sheet.zh_tree.nodes[iRow];
  1053. if (node.ledger_pid !== pid) {
  1054. toastr.error('仅可同时选中同层节点');
  1055. return;
  1056. }
  1057. copyBlockList.push(node.ledger_id);
  1058. iRow += sheet.zh_tree.getPosterity(node).length + 1;
  1059. }
  1060. const tenderId = _.toInteger(getTenderId());
  1061. setLocalCache(copyBlockTag, JSON.stringify({tid: tenderId, block: copyBlockList}));
  1062. //treeOperationObj.block = copyBlockList;
  1063. },
  1064. visible: function (key, opt) {
  1065. const sheet = ledgerSpread.getActiveSheet();
  1066. const selection = sheet.getSelections();
  1067. const row = selection[0].row;
  1068. const select = ledgerTree.nodes[row];
  1069. return select;
  1070. },
  1071. disabled: function (key, opt) {
  1072. const sheet = ledgerSpread.getActiveSheet();
  1073. const selection = sheet.getSelections();
  1074. const row = selection[0].row;
  1075. const select = ledgerTree.nodes[row];
  1076. return select && select.level <= 1;
  1077. }
  1078. };
  1079. if (!readOnly) {
  1080. billsContextMenuOptions.items.pasteBlock = {
  1081. name: '粘贴整块',
  1082. icon: 'fa-clipboard',
  1083. disabled: function (key, opt) {
  1084. //const block = treeOperationObj.block || [];
  1085. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1086. return !(copyInfo && copyInfo.tid && copyInfo.tid > 0 && copyInfo.block && copyInfo.block.length > 0);
  1087. },
  1088. callback: function (key, opt) {
  1089. //const block = treeOperationObj.block || [];
  1090. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1091. if (copyInfo.block.length > 0) {
  1092. treeOperationObj.pasteBlock(ledgerSpread, copyInfo);
  1093. } else {
  1094. document.execCommand('paste');
  1095. }
  1096. },
  1097. visible: function (key, opt) {
  1098. return !readOnly;
  1099. }
  1100. };
  1101. billsContextMenuOptions.items.sprBlock = '-----------';
  1102. }
  1103. if (!readOnly) {
  1104. billsContextMenuOptions.items.sortChildren = {
  1105. name: '顺序重排子项编号',
  1106. icon: 'fa-sort-numeric-asc',
  1107. disabled: function (key, opt) {
  1108. const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1109. return !node || !node.code || !node.children || node.children === 0;
  1110. },
  1111. callback: function (key, opt) {
  1112. treeOperationObj.sortCode(ledgerSpread.getActiveSheet());
  1113. },
  1114. visible: function (key, opt) {
  1115. return !readOnly;
  1116. }
  1117. };
  1118. billsContextMenuOptions.items.sprSort = '-----------';
  1119. }
  1120. if (!readOnly) {
  1121. billsContextMenuOptions.items.batchInsert = {
  1122. name: '批量插入',
  1123. type: 'batchInsert',
  1124. value: '2',
  1125. icon: 'fa-sign-in',
  1126. batchInsert: function (obj, root) {
  1127. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  1128. obj.value = obj.max;
  1129. toastr.warning('批量插入不可多于' + obj.max);
  1130. } else if(_.toNumber(obj.value) < _.toNumber(obj.min)) {
  1131. obj.value = obj.min;
  1132. toastr.warning('批量插入不可少于' + obj.min);
  1133. } else {
  1134. treeOperationObj.addNode(ledgerSpread.getActiveSheet(), parseInt(obj.value));
  1135. root.$menu.trigger('contextmenu:hide');
  1136. }
  1137. },
  1138. disabled: function (key, opt) {
  1139. const sheet = ledgerSpread.getActiveSheet();
  1140. const selection = sheet.getSelections();
  1141. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1142. const row = sel ? sel.row : -1;
  1143. const tree = sheet.zh_tree;
  1144. if (!tree) return true;
  1145. const first = sheet.zh_tree.nodes[row];
  1146. let last = first, sameParent = true;
  1147. if (sel.rowCount > 1) {
  1148. for (let r = 1; r < sel.rowCount; r++) {
  1149. const rNode = tree.nodes[sel.row + r];
  1150. if (rNode.level > first.level) continue;
  1151. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  1152. sameParent = false;
  1153. break;
  1154. }
  1155. last = rNode;
  1156. }
  1157. }
  1158. const valid = !sheet.zh_setting.readOnly;
  1159. return !(valid && first && first.level > 1);
  1160. },
  1161. visible: function (key, opt) {
  1162. return !readOnly;
  1163. }
  1164. };
  1165. billsContextMenuOptions.items.batchInsertBillsPos = {
  1166. name: '批量插入清单-计量单元',
  1167. icon: 'fa-sign-in',
  1168. disabled: function (key, opt) {
  1169. if (!checkTzMeasureType()) return true;
  1170. const sheet = ledgerSpread.getActiveSheet();
  1171. const selection = sheet.getSelections();
  1172. const row = selection[0].row;
  1173. const select = ledgerTree.nodes[row];
  1174. if (select) {
  1175. if (select.code && select.code !== '') {
  1176. return !ledgerTree.isLeafXmj(select);
  1177. } else {
  1178. const parent = ledgerTree.getParent(select);
  1179. return !(parent && ledgerTree.isLeafXmj(parent));
  1180. }
  1181. } else {
  1182. return false;
  1183. }
  1184. },
  1185. callback: function (key, opt) {
  1186. if (!batchInsertObj) {
  1187. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  1188. } else {
  1189. batchInsertObj.initView();
  1190. }
  1191. $('#batch').modal('show');
  1192. },
  1193. visible: function (key, opt) {
  1194. return !readOnly;
  1195. }
  1196. };
  1197. billsContextMenuOptions.items.sprBatch = '-----------';
  1198. }
  1199. if (!readOnly) {
  1200. billsContextMenuOptions.items.importExcel = {
  1201. name: '导入分项清单Excel',
  1202. icon: 'fa-file-excel-o',
  1203. disabled: function (key, opt) {
  1204. return readOnly;
  1205. },
  1206. callback: function (key, opt) {
  1207. importExcel.doImport({
  1208. template: {
  1209. hint: '0号台账',
  1210. url: '/template/导入分项清单EXCEL格式.xls',
  1211. },
  1212. callback: function (sheet) {
  1213. postDataCompress(window.location.pathname + '/upload-excel/tz', sheet, function (result) {
  1214. ledgerTree.loadDatas(result.bills);
  1215. treeCalc.calculateAll(ledgerTree);
  1216. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
  1217. pos.loadDatas(result.pos);
  1218. posOperationObj.loadCurPosData();
  1219. }, null);
  1220. },
  1221. // callback: function (file, select) {
  1222. // const formData = new FormData();
  1223. // formData.append('file', file.files[0]);
  1224. // postDataWithFileProgress(window.location.pathname + '/upload-excel?ueType=tz&sheetName=' + select, formData, function (result) {
  1225. // ledgerTree.loadDatas(result.bills);
  1226. // treeCalc.calculateAll(ledgerTree);
  1227. // SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
  1228. // pos.loadDatas(result.pos);
  1229. // posOperationObj.loadCurPosData();
  1230. // }, null);
  1231. // }
  1232. //u_type: importExcel.uploadType.file,
  1233. });
  1234. //$('#upload-ledger').modal('show');
  1235. },
  1236. visible: function (key, opt) {
  1237. return !readOnly;
  1238. }
  1239. };
  1240. billsContextMenuOptions.items.importGclBills2Xmj = {
  1241. name: '导入工程量清单至项目节',
  1242. icon: 'fa-file-excel-o',
  1243. disabled: function (key, opt) {
  1244. const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1245. return readOnly
  1246. || (node.children && node.children.length > 0)
  1247. || (!_.isNil(node.b_code) && node.b_code !== '');
  1248. },
  1249. callback: function (key, opt) {
  1250. const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1251. importExcel.doImport({
  1252. template: {
  1253. hint: '工程量清单',
  1254. url: '/template/导入工程量清单EXCEL格式.xls',
  1255. },
  1256. callback: function (sheet) {
  1257. postDataCompress(window.location.pathname + '/upload-excel/gcl2xmj', {id: node.id, sheet: sheet}, function (result) {
  1258. const ledgerSheet = ledgerSpread.getActiveSheet();
  1259. const sel = ledgerSheet.getSelections();
  1260. const refreshNode = ledgerTree.loadPostData(result);
  1261. treeOperationObj.refreshTree(ledgerSheet, refreshNode);
  1262. if (refreshNode.create[0]) {
  1263. if (sel && sel[0]) {
  1264. ledgerSheet.setSelection(refreshNode.create[0].index, sel[0].col, sel[0].rowCount, sel[0].colCount);
  1265. SpreadJsObj.reloadRowsBackColor(ledgerSheet, [sel[0].row, refreshNode.create[0].index]);
  1266. } else {
  1267. ledgerSheet.setSelection(refreshNode.create[0].index, 0, 1, 1);
  1268. SpreadJsObj.reloadRowsBackColor(ledgerSheet, [refreshNode.create[0].index]);
  1269. }
  1270. }
  1271. treeOperationObj.refreshOperationValid(ledgerSheet);
  1272. }, null);
  1273. }
  1274. });
  1275. },
  1276. visible: function (key, opt) {
  1277. return !readOnly;
  1278. }
  1279. };
  1280. billsContextMenuOptions.items.sprImport = '-----------';
  1281. }
  1282. billsContextMenuOptions.items.exportExcel = {
  1283. name: '导出表格数据',
  1284. icon: 'fa-file-excel-o',
  1285. callback: function (key, opt) {
  1286. const fileName = $('.text-truncate').attr('data-original-title') + '.xlsx';
  1287. SpreadExcelObj.exportSpread2XlsxWithHeader(ledgerSpread, fileName);
  1288. },
  1289. visible: function (key, opt) {
  1290. try {
  1291. return is_debug;
  1292. } catch (err) {
  1293. return false;
  1294. }
  1295. }
  1296. };
  1297. $.contextMenu(billsContextMenuOptions);
  1298. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  1299. // 台账模式加载计量单元数据
  1300. if (checkTzMeasureType()) {
  1301. $.divResizer({
  1302. select: '#main-resize',
  1303. callback: function () {
  1304. ledgerSpread.refresh();
  1305. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1306. $(".sp-wrap").height(bcontent-30);
  1307. posSpread.refresh();
  1308. }
  1309. });
  1310. SpreadJsObj.initSheet(posSpread.getActiveSheet(), posSpreadSetting);
  1311. }
  1312. //绑定计量单元编辑事件
  1313. const posOperationObj = {
  1314. editStarting: function (e, info) {
  1315. posOperationObj.ledgerTreeNode = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1316. },
  1317. loadExprToInput: function () {
  1318. const sheet = posSpread.getActiveSheet();
  1319. const sel = sheet.getSelections()[0];
  1320. if (!sel || !sheet.zh_setting) return;
  1321. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  1322. if (col && col.type === 'Number') {
  1323. const data = SpreadJsObj.getSelectObject(sheet);
  1324. if (data) {
  1325. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  1326. .attr('row', sel.row).attr('readOnly', readOnly || cell.locked());
  1327. } else {
  1328. $('#pos-expr').val('').attr('readOnly', true);
  1329. }
  1330. } else {
  1331. $('#pos-expr').val('').attr('readOnly', true);
  1332. }
  1333. },
  1334. /**
  1335. * 加载计量单元 根据当前台账选择节点
  1336. */
  1337. loadCurPosData: function () {
  1338. //spreadJsObj.reinitSheet(posSpread.getActiveSheet());
  1339. const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1340. if (node) {
  1341. const posData = pos.ledgerPos[itemsPre + node.id] || [];
  1342. SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', posData);
  1343. } else {
  1344. SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', []);
  1345. }
  1346. posOperationObj.loadExprToInput();
  1347. },
  1348. /**
  1349. * 编辑单元格响应事件
  1350. * @param {Object} e
  1351. * @param {Object} info
  1352. */
  1353. editEnded: function (e, info) {
  1354. if (info.sheet.zh_setting) {
  1355. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  1356. const col = info.sheet.zh_setting.cols[info.col];
  1357. const orgText = posData ? posData[col.field] : null;
  1358. const newText = trimInvalidChar(info.editingText);
  1359. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) {
  1360. return;
  1361. }
  1362. //const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1363. const node = posOperationObj.ledgerTreeNode;
  1364. if (!node) {
  1365. toastr.error('数据错误,请选择台账节点后再试');
  1366. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1367. return;
  1368. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1369. toastr.error('父节点不可插入计量单元');
  1370. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1371. return;
  1372. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1373. toastr.error('项目节不可插入计量单元');
  1374. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1375. return;
  1376. }
  1377. const data = {};
  1378. if (col.field === 'name') {
  1379. if (newText === '' && posData) {
  1380. toastr.error('部位名称不可为空');
  1381. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1382. return;
  1383. } else if (!posData) {
  1384. if (newText && newText !== '') {
  1385. data.updateType = 'add';
  1386. const sortData = info.sheet.zh_data;
  1387. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1388. data.updateData = { name: newText, lid: node.id, tid: tender.id, porder: order};
  1389. } else {
  1390. return;
  1391. }
  1392. } else {
  1393. data.updateType = 'update';
  1394. data.updateData = {id: posData.id, name: newText};
  1395. }
  1396. } else if (!posData) {
  1397. toastr.warning('新增计量单元请先输入名称');
  1398. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1399. return;
  1400. } else {
  1401. data.updateType = 'update';
  1402. data.updateData = {id: posData.id};
  1403. if (col.type === 'Number') {
  1404. const num = _.toNumber(newText);
  1405. if (_.isFinite(num)) {
  1406. data.updateData[col.field] = num;
  1407. } else {
  1408. try {
  1409. data.updateData[col.field] = math.evaluate(transExpr(newText));
  1410. } catch(err) {
  1411. toastr.error('输入的表达式非法');
  1412. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1413. return;
  1414. }
  1415. }
  1416. } else {
  1417. data.updateData[col.field] = newText;
  1418. }
  1419. }
  1420. postData('/tender/' + getTenderId() + '/pos/update', data, function (result) {
  1421. const updateRst = pos.updateDatas(result.pos);
  1422. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1423. if (updateRst.create.length > 0) {
  1424. // info.sheet.addRows(info.row, 1);
  1425. // if (info.sheet.zh_data.length === 0) {
  1426. // info.sheet.zh_data = pos.ledgerPos[itemsPre + node.id] || [];
  1427. // }
  1428. // SpreadJsObj.reLoadRowData(info.sheet, info.sheet.zh_data.length - 1);
  1429. posOperationObj.loadCurPosData();
  1430. } else {
  1431. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1432. }
  1433. const loadResult = ledgerTree.loadPostData(result.ledger);
  1434. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1435. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1436. }, function () {
  1437. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1438. });
  1439. }
  1440. },
  1441. /**
  1442. * 删除按钮响应事件
  1443. * @param sheet
  1444. */
  1445. deletePress: function (sheet) {
  1446. const self = this;
  1447. if (sheet.zh_setting) {
  1448. const sortData = sheet.zh_data;
  1449. const datas = [], posSelects = [];
  1450. const sel = sheet.getSelections()[0];
  1451. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1452. let bDel = false;
  1453. const node = sortData[iRow];
  1454. if (node) {
  1455. const data = {id: node.id};
  1456. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1457. const colSetting = sheet.zh_setting.cols[iCol];
  1458. if (colSetting.field === 'name') {
  1459. toastr.error('部位名称不能为空');
  1460. return;
  1461. }
  1462. const style = sheet.getStyle(iRow, iCol);
  1463. if (!style.locked) {
  1464. const colSetting = sheet.zh_setting.cols[iCol];
  1465. data[colSetting.field] = null;
  1466. bDel = true;
  1467. }
  1468. }
  1469. if (bDel) {
  1470. datas.push(data);
  1471. posSelects.push(node);
  1472. }
  1473. }
  1474. }
  1475. if (datas.length > 0) {
  1476. postData('/tender/' + getTenderId() + '/pos/update', {updateType: 'update', updateData: datas}, function (result) {
  1477. pos.updateDatas(result.pos);
  1478. posOperationObj.loadCurPosData();
  1479. const loadResult = ledgerTree.loadPostData(result.ledger);
  1480. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1481. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1482. }, function () {
  1483. posOperationObj.loadCurPosData();
  1484. });
  1485. }
  1486. }
  1487. },
  1488. /**
  1489. * 删除 计量单元
  1490. * @param sheet
  1491. */
  1492. deletePos: function (sheet) {
  1493. const selection = sheet.getSelections();
  1494. const data = {
  1495. updateType: 'delete',
  1496. updateData: [],
  1497. };
  1498. const row = selection[0].row, count = selection[0].rowCount;
  1499. const sortData = sheet.zh_data;
  1500. for (let iRow = 0; iRow < count; iRow++) {
  1501. if (sortData[iRow + row]) {
  1502. data.updateData.push(sortData[iRow + row].id);
  1503. }
  1504. }
  1505. if (data.updateData.length > 0) {
  1506. postData('/tender/' + getTenderId() + '/pos/update', data, function (result) {
  1507. pos.removeDatas(result.pos);
  1508. sheet.deleteRows(row, count);
  1509. const loadResult = ledgerTree.loadPostData(result.ledger);
  1510. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1511. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1512. });
  1513. }
  1514. },
  1515. /**
  1516. * 粘贴单元格响应事件
  1517. * @param e
  1518. * @param info
  1519. */
  1520. clipboardPasted: function (e, info) {
  1521. if (info.sheet.getColumnCount() > info.sheet.zh_setting.cols.length) {
  1522. info.sheet.setColumnCount(info.sheet.zh_setting.cols.length);
  1523. }
  1524. const node = treeOperationObj.getSelectNode(ledgerSpread.getActiveSheet());
  1525. if (node.code && (node.code !== '')) {
  1526. toastr.error('项目节不可含有清单明细');
  1527. posOperationObj.loadCurPosData();
  1528. return;
  1529. }
  1530. if (node.children && (node.children.length > 0)) {
  1531. toastr.error('仅清单子项可以含有计量单元');
  1532. posOperationObj.loadCurPosData();
  1533. return;
  1534. }
  1535. if (info.sheet.zh_setting) {
  1536. const data = [];
  1537. const sortData = info.sheet.zh_data || [];
  1538. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1539. if (info.cellRange.col !== 0) {
  1540. toastr.warning('新增计量单元请先输入名称');
  1541. posOperationObj.loadCurPosData();
  1542. return;
  1543. }
  1544. }
  1545. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1546. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1547. let bPaste = false;
  1548. const curRow = info.cellRange.row + iRow;
  1549. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1550. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1551. const curCol = info.cellRange.col + iCol;
  1552. const colSetting = info.sheet.zh_setting.cols[curCol];
  1553. if (!colSetting) continue;
  1554. posData[colSetting.field] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1555. if (colSetting.type === 'Number') {
  1556. const num = _.toNumber(posData[colSetting.field]);
  1557. if (num) {
  1558. posData[colSetting.field] = num;
  1559. bPaste = true;
  1560. } else {
  1561. try {
  1562. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1563. bPaste = true;
  1564. } catch(err) {
  1565. delete posData[colSetting.field];
  1566. toastr.warning('粘贴了表达式非法,已过滤');
  1567. }
  1568. }
  1569. } else {
  1570. bPaste = true;
  1571. }
  1572. }
  1573. if (bPaste) {
  1574. data.push(posData);
  1575. }
  1576. }
  1577. if (data.length > 0) {
  1578. postData('/tender/' + getTenderId() + '/pos/paste', data, function (result) {
  1579. pos.updateDatas(result.pos);
  1580. posOperationObj.loadCurPosData();
  1581. const loadResult = ledgerTree.loadPostData(result.ledger);
  1582. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1583. posOperationObj.loadCurPosData();
  1584. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1585. }, function () {
  1586. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1587. });
  1588. } else {
  1589. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1590. }
  1591. }
  1592. },
  1593. selectionChanged: function (e, info) {
  1594. posOperationObj.loadExprToInput();
  1595. },
  1596. addPegs: function (pegs) {
  1597. if (!pegs || pegs.length <= 0) return;
  1598. const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1599. if (!node) return;
  1600. const sheet = posSpread.getActiveSheet();
  1601. const sortData = sheet.zh_data || [];
  1602. let order = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1603. pegs.forEach(function (p) {p.porder = ++order; p.lid = node.id});
  1604. postData('/tender/' + getTenderId() + '/pos/paste', pegs, function (result) {
  1605. pos.updateDatas(result.pos);
  1606. posOperationObj.loadCurPosData();
  1607. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1608. });
  1609. }
  1610. };
  1611. posSpread.bind(spreadNS.Events.SelectionChanged, posOperationObj.selectionChanged);
  1612. if (!posSpreadSetting.readOnly) {
  1613. $('#pos-expr').bind('change mouseleave', function () {
  1614. if (document.activeElement.id !== "pos-expr") return;
  1615. const expr = $(this);
  1616. const posSheet = posSpread.getActiveSheet();
  1617. const select = SpreadJsObj.getSelectObject(posSheet);
  1618. if (!select) return;
  1619. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = expr.val(), row = trimInvalidChar(expr.attr('row'));
  1620. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  1621. const data = {id: select.id};
  1622. if (newValue !== '') {
  1623. const num = _.toNumber(newValue);
  1624. if (num) {
  1625. data[field] = num;
  1626. } else {
  1627. try {
  1628. data[field] = math.evaluate(transExpr(newValue));
  1629. } catch (err) {
  1630. toastr.error('输入的表达式非法');
  1631. return;
  1632. }
  1633. }
  1634. } else {
  1635. data[field] = null;
  1636. }
  1637. // 更新至服务器
  1638. postData('/tender/' + getTenderId() + '/pos/update', {updateType: 'update', updateData: data}, function (result) {
  1639. const updateRst = pos.updateDatas(result.pos);
  1640. expr.val(select[field]);
  1641. SpreadJsObj.reLoadRowData(posSheet, _.toNumber(row));
  1642. const loadResult = ledgerTree.loadPostData(result.ledger);
  1643. treeOperationObj.refreshTree(ledgerSpread.getActiveSheet(), loadResult);
  1644. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1645. });
  1646. });
  1647. SpreadJsObj.addDeleteBind(posSpread, posOperationObj.deletePress);
  1648. posSpread.bind(spreadNS.Events.EditStarting, posOperationObj.editStarting);
  1649. posSpread.bind(spreadNS.Events.EditEnded, posOperationObj.editEnded);
  1650. posSpread.bind(spreadNS.Events.ClipboardPasted, posOperationObj.clipboardPasted);
  1651. const mergePeg = NewMergePeg({ callback: posOperationObj.addPegs });
  1652. // 右键菜单
  1653. $.contextMenu({
  1654. selector: '#pos-spread',
  1655. build: function ($trigger, e) {
  1656. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1657. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1658. },
  1659. items: {
  1660. 'delete': {
  1661. name: '删除',
  1662. icon: 'fa-remove',
  1663. disabled: function (key, opt) {
  1664. const sheet = posSpread.getActiveSheet();
  1665. if (sheet.zh_data) {
  1666. const selection = sheet.getSelections();
  1667. return sheet.zh_data.length < selection[0].row + selection[0].rowCount;
  1668. } else {
  1669. return true;
  1670. }
  1671. },
  1672. callback: function (key, opt) {
  1673. posOperationObj.deletePos(posSpread.getActiveSheet());
  1674. }
  1675. },
  1676. 'merge-peg': {
  1677. name: '合并起讫桩号',
  1678. disabled: function (key, opt) {
  1679. const node = SpreadJsObj.getSelectObject(ledgerSpread.getActiveSheet());
  1680. return _.isNil(node) || _.isNil(node.b_code) || node.b_code === '';
  1681. },
  1682. callback: function (key, opt) {
  1683. mergePeg.show();
  1684. }
  1685. }
  1686. }
  1687. });
  1688. } else {
  1689. SpreadJsObj.forbiddenSpreadContextMenu('#pos-spread', posSpread);
  1690. }
  1691. postData(window.location.pathname + '/load', {}, function (data) {
  1692. ledgerTree.loadDatas(data.bills);
  1693. treeCalc.calculateAll(ledgerTree);
  1694. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);
  1695. SpreadJsObj.loadTopAndSelect(ledgerSpread.getActiveSheet(), ckBillsSpread);
  1696. pos.loadDatas(data.pos);
  1697. posOperationObj.loadCurPosData();
  1698. SpreadJsObj.resetTopAndSelect(posSpread.getActiveSheet());
  1699. treeOperationObj.refreshOperationValid(ledgerSpread.getActiveSheet());
  1700. treeOperationObj.loadExprToInput(ledgerSpread.getActiveSheet());
  1701. }, null, true);
  1702. $.divResizer({
  1703. select: '#right-spr',
  1704. callback: function () {
  1705. ledgerSpread.refresh();
  1706. if (posSpread) {
  1707. posSpread.refresh();
  1708. }
  1709. if (stdXmj) {
  1710. stdXmj.spread.refresh();
  1711. }
  1712. if (stdGcl) {
  1713. stdGcl.spread.refresh();
  1714. }
  1715. if (dealBills) {
  1716. dealBills.spread.refresh();
  1717. }
  1718. if (searchLedger) {
  1719. searchLedger.spread.refresh();
  1720. }
  1721. }
  1722. });
  1723. const stdLibCellDoubleClick = function (e, info) {
  1724. const stdSheet = info.sheet;
  1725. const mainSheet = ledgerSpread.getActiveSheet();
  1726. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1727. const stdTree = stdSheet.zh_tree;
  1728. const stdNode = stdTree.nodes[info.row];
  1729. const mainTree = mainSheet.zh_tree;
  1730. const sel = mainSheet.getSelections()[0];
  1731. const mainNode = mainTree.nodes[sel.row];
  1732. if (!stdNode) { return; }
  1733. if (info.sheet.zh_setting.stdType === 'gcl') {
  1734. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1735. toastr.warning('非最底层项目下,不应添加清单');
  1736. return;
  1737. }
  1738. }
  1739. postData(window.location.pathname + '/update', {
  1740. postType: 'add-std',
  1741. postData: {
  1742. id: ledgerTree.getNodeKey(mainNode),
  1743. tender_id: mainNode.tender_id,
  1744. stdType: info.sheet.zh_setting.stdType,
  1745. stdLibId: stdNode.list_id,
  1746. stdNode: stdTree.getNodeKey(stdNode)
  1747. }
  1748. }, function (result) {
  1749. const refreshNode = mainTree.loadPostData(result);
  1750. treeOperationObj.refreshTree(mainSheet, refreshNode);
  1751. if (refreshNode.create && refreshNode.create.length > 0) {
  1752. mainSheet.setSelection(refreshNode.create[refreshNode.create.length - 1].index, sel.col, sel.rowCount, sel.colCount);
  1753. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, refreshNode.create[refreshNode.create.length - 1].index]);
  1754. } else {
  1755. const node = _.find(ledgerTree.nodes, {code: stdNode.code, name: stdNode.name});
  1756. if (node) {
  1757. mainSheet.setSelection(ledgerTree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  1758. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, ledgerTree.nodes.indexOf(node)]);
  1759. }
  1760. }
  1761. treeOperationObj.refreshOperationValid(mainSheet);
  1762. ledgerSpread.focus();
  1763. posOperationObj.loadCurPosData();
  1764. });
  1765. };
  1766. const stdXmjSetting = {
  1767. selector: '#std-xmj',
  1768. stdType: 'xmj',
  1769. treeSetting: {
  1770. id: 'chapter_id',
  1771. pid: 'pid',
  1772. order: 'order',
  1773. level: 'level',
  1774. rootId: -1,
  1775. keys: ['id', 'list_id', 'chapter_id'],
  1776. },
  1777. spreadSetting: {
  1778. cols: [
  1779. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1780. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1781. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1782. ],
  1783. treeCol: 0,
  1784. emptyRows: 0,
  1785. headRows: 1,
  1786. headRowHeight: [32],
  1787. defaultRowHeight: 21,
  1788. headerFont: '12px 微软雅黑',
  1789. font: '12px 微软雅黑',
  1790. headColWidth: [0],
  1791. selectedBackColor: '#fffacd',
  1792. },
  1793. cellDoubleClick: stdLibCellDoubleClick,
  1794. page: 'ledger',
  1795. tid: getTenderId(),
  1796. };
  1797. const stdGclSetting = {
  1798. selector: '#std-gcl',
  1799. stdType: 'gcl',
  1800. treeSetting: {
  1801. id: 'bill_id',
  1802. pid: 'pid',
  1803. order: 'order',
  1804. level: 'level',
  1805. rootId: -1,
  1806. keys: ['id', 'list_id', 'bill_id']
  1807. },
  1808. spreadSetting: {
  1809. cols: [
  1810. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1811. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1812. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1813. ],
  1814. treeCol: 0,
  1815. emptyRows: 0,
  1816. headRows: 1,
  1817. headRowHeight: [32],
  1818. defaultRowHeight: 21,
  1819. headerFont: '12px 微软雅黑',
  1820. font: '12px 微软雅黑',
  1821. headColWidth: [0],
  1822. selectedBackColor: '#fffacd',
  1823. },
  1824. cellDoubleClick: stdLibCellDoubleClick,
  1825. page: 'ledger',
  1826. tid: getTenderId(),
  1827. };
  1828. // 展开收起标准清单
  1829. $('a', '#side-menu').bind('click', function (e) {
  1830. e.preventDefault();
  1831. const tab = $(this), tabPanel = $(tab.attr('content'));
  1832. const showSideTools = function (show) {
  1833. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1834. if (show) {
  1835. right.show();
  1836. autoFlashHeight();
  1837. /**
  1838. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1839. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1840. * 故需要通过最终的parent.width再计算一次left.width
  1841. *
  1842. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1843. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1844. *
  1845. */
  1846. //left.css('width', parent.width() - right.outerWidth());
  1847. //left.css('width', parent.width() - right.outerWidth());
  1848. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1849. left.css('width', percent + '%');
  1850. } else {
  1851. left.width(parent.width());
  1852. right.hide();
  1853. }
  1854. };
  1855. // 展开工具栏、切换标签
  1856. if (!tab.hasClass('active')) {
  1857. const close = $('.active', '#side-menu').length === 0;
  1858. $('a', '#side-menu').removeClass('active');
  1859. tab.addClass('active');
  1860. $('.tab-content .tab-pane').removeClass('active');
  1861. tabPanel.addClass('active');
  1862. showSideTools(tab.hasClass('active'));
  1863. if (tab.attr('content') === '#std-xmj') {
  1864. if (!stdXmj) {
  1865. stdXmj = new stdLib(stdXmjSetting);
  1866. //stdXmj.loadLib($('select', '#std-xmj').val());
  1867. }
  1868. stdXmj.spread.refresh();
  1869. } else if (tab.attr('content') === '#std-gcl') {
  1870. if (!stdGcl) {
  1871. stdGcl = new stdLib(stdGclSetting);
  1872. //stdGcl.loadLib($('select', '#std-gcl').val());
  1873. }
  1874. stdGcl.spread.refresh();
  1875. } else if (tab.attr('content') === '#deal-bills') {
  1876. if (!dealBills) {
  1877. dealBills = new DealBills('#deal-bills-spread', {
  1878. cols: [
  1879. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  1880. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1881. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1882. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1883. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1884. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1885. ],
  1886. emptyRows: 0,
  1887. headRows: 1,
  1888. headRowHeight: [32],
  1889. defaultRowHeight: 21,
  1890. headerFont: '12px 微软雅黑',
  1891. font: '12px 微软雅黑',
  1892. headColWidth: [0],
  1893. selectedBackColor: '#fffacd',
  1894. });
  1895. dealBills.loadData();
  1896. }
  1897. dealBills.spread.refresh();
  1898. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1899. if (!searchLedger) {
  1900. searchLedger = $.billsSearch({
  1901. selector: '#search',
  1902. searchSpread: ledgerSpread,
  1903. resultSpreadSetting: {
  1904. cols: [
  1905. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1906. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1907. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  1908. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1909. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1910. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1911. ],
  1912. emptyRows: 0,
  1913. headRows: 1,
  1914. headRowHeight: [32],
  1915. defaultRowHeight: 21,
  1916. headerFont: '12px 微软雅黑',
  1917. font: '12px 微软雅黑',
  1918. selectedBackColor: '#fffacd',
  1919. },
  1920. afterLocated: function () {
  1921. posOperationObj.loadCurPosData();
  1922. }
  1923. });
  1924. }
  1925. searchLedger.spread.refresh();
  1926. }
  1927. } else { // 收起工具栏
  1928. tab.removeClass('active');
  1929. tabPanel.removeClass('active');
  1930. showSideTools(tab.hasClass('active'));
  1931. }
  1932. ledgerSpread.refresh();
  1933. if (posSpread) {
  1934. posSpread.refresh();
  1935. }
  1936. });
  1937. class DealBills {
  1938. constructor (selector, spreadSetting) {
  1939. const self = this;
  1940. this.obj = $(selector)[0];
  1941. this.url = '/tender/' + getTenderId() + '/deal';
  1942. this.spreadSetting = spreadSetting;
  1943. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1944. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1945. if (!readOnly) {
  1946. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1947. const dealSheet = info.sheet;
  1948. const mainSheet = ledgerSpread.getActiveSheet();
  1949. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1950. if (!dealBills) { return; }
  1951. const mainTree = mainSheet.zh_tree;
  1952. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1953. if (!mainNode || !mainTree) { return; }
  1954. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1955. toastr.error('非最底层项目下,不应添加清单');
  1956. return;
  1957. }
  1958. postData(window.location.pathname + '/update', {
  1959. postType: 'add-deal',
  1960. postData: {
  1961. id: mainNode.ledger_id,
  1962. type: mainNode.code ? 'child' : 'next',
  1963. dealBills: {
  1964. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1965. unit_price: dealBills.unit_price,
  1966. }
  1967. },
  1968. }, function (result) {
  1969. const refreshData = mainTree.loadPostData(result);
  1970. treeOperationObj.refreshTree(mainSheet, refreshData);
  1971. const sel = mainSheet.getSelections()[0];
  1972. mainSheet.setSelection(refreshData.create[0].index, sel.col, sel.rowCount, sel.colCount);
  1973. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, refreshData.create[0].index]);
  1974. treeOperationObj.refreshOperationValid(mainSheet);
  1975. ledgerSpread.focus();
  1976. posOperationObj.loadCurPosData();
  1977. });
  1978. });
  1979. }
  1980. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1981. $('#upload-deal-bills').click(function () {
  1982. const file = $('#deal-bills-file')[0];
  1983. const formData = new FormData();
  1984. formData.append('file', file.files[0]);
  1985. postDataWithFileProgress(self.url+'/upload-excel', formData, function (data) {
  1986. self.data = data;
  1987. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1988. $('#upload-deal').modal('hide');
  1989. }, function () {
  1990. $('#upload-deal').modal('hide');
  1991. });
  1992. });
  1993. }
  1994. loadData () {
  1995. const self = this;
  1996. postData(this.url+'/get-data', {}, function (data) {
  1997. self.data = data;
  1998. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1999. });
  2000. }
  2001. calculateData () {
  2002. if (this.data) {
  2003. for (const d of this.data) {
  2004. d.total_price = _.multiply(d.quantity, d.unit_price);
  2005. }
  2006. }
  2007. }
  2008. }
  2009. class BatchInsertBillsPosObj {
  2010. constructor (obj) {
  2011. const self = this;
  2012. this.obj = obj;
  2013. this.billsCount = 6;
  2014. this.posCount = 1000;
  2015. // 初始化 清单编号窗口 参数
  2016. this.qdSpreadSetting = {
  2017. cols: [
  2018. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  2019. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  2020. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  2021. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  2022. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  2023. ],
  2024. emptyRows: this.billsCount,
  2025. headRows: 1,
  2026. headRowHeight: [32],
  2027. headerFont: '12px 微软雅黑',
  2028. font: '12px 微软雅黑',
  2029. };
  2030. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  2031. // 初始化 部位数量复核表 参数
  2032. this.posSpreadSetting = {
  2033. cols: [
  2034. {title: '计量单元', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  2035. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  2036. ],
  2037. emptyRows: this.posCount,
  2038. headRows: 1,
  2039. headRowHeight: [32],
  2040. headerFont: '12px 微软雅黑',
  2041. font: '12px 微软雅黑',
  2042. };
  2043. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  2044. this.posSpreadSetting.cols.push(
  2045. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  2046. )
  2047. }
  2048. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  2049. // 初始化 签约清单 参数
  2050. this.dealSpreadSetting = {
  2051. cols: [
  2052. {title: '清单编号', field: 'code', width: 80, hAlign: 0, formatter: '@', readOnly: true},
  2053. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  2054. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2055. {title: '单价', field: 'unit_price', hAlign: 2, width: 60, readOnly: true},
  2056. ],
  2057. emptyRows: 0,
  2058. headRows: 1,
  2059. headRowHeight: [32],
  2060. headerFont: '12px 微软雅黑',
  2061. font: '12px 微软雅黑',
  2062. };
  2063. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  2064. // 初始化 清单编号、部位数量复核表 表格
  2065. this.initView();
  2066. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  2067. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  2068. $.contextMenu({
  2069. selector: '.batch-l-t',
  2070. build: function ($trigger, e) {
  2071. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  2072. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  2073. },
  2074. items: {
  2075. 'create': {
  2076. name: '新增行',
  2077. icon: 'fa-sign-in',
  2078. callback: function (key, opt) {
  2079. const qdSheet = self.qdSpread.getActiveSheet();
  2080. const posSheet = self.posSpread.getActiveSheet();
  2081. qdSheet.addRows(qdSheet.getRowCount(), 1);
  2082. const index = qdSheet.getRowCount();
  2083. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  2084. posSheet.addColumns(posSheet.getColumnCount(), 1);
  2085. posSheet.getCell(0, index + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + index);
  2086. },
  2087. },
  2088. 'delete': {
  2089. name: '删除行',
  2090. icon: 'fa-remove',
  2091. callback: function (key, opt) {
  2092. const qdSheet = self.qdSpread.getActiveSheet();
  2093. const posSheet = self.posSpread.getActiveSheet();
  2094. const sel = qdSheet.getSelections()[0];
  2095. qdSheet.deleteRows(sel.row, sel.rowCount);
  2096. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  2097. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  2098. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  2099. }
  2100. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  2101. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  2102. }
  2103. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  2104. },
  2105. },
  2106. }
  2107. });
  2108. // 拉取签约清单数据
  2109. if (dealBills) {
  2110. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  2111. } else {
  2112. postData('/tender/' + getTenderId() + '/deal/get-data', {}, function (data) {
  2113. SpreadJsObj.loadSheetData(self.dealSpread.getActiveSheet(), 'data', data);
  2114. });
  2115. }
  2116. // 双击签约清单,自动添加到清单编号窗口
  2117. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  2118. const deal = info.sheet.zh_data[info.row];
  2119. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  2120. const sel = qdSheet.getSelections()[0];
  2121. qdSheet.getCell(sel.row, 0).value(deal.code);
  2122. qdSheet.getCell(sel.row, 1).value(deal.name);
  2123. qdSheet.getCell(sel.row, 2).value(deal.unit);
  2124. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  2125. if (sel.row + 1 === qdSheet.getRowCount()) {
  2126. const count = sel.row + 2;
  2127. qdSheet.setRowCount(count);
  2128. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  2129. const colCount = posSheet.getColumnCount() + 1
  2130. posSheet.setColumnCount(colCount);
  2131. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('清单' + count);
  2132. }
  2133. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  2134. });
  2135. this.qdSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  2136. const billsCount = info.sheet.getRowCount(), posSheet = self.posSpread.getActiveSheet();
  2137. const count = posSheet.getColumnCount() - 2;
  2138. if (billsCount > count) {
  2139. posSheet.setColumnCount(billsCount + 2);
  2140. for (let i = count + 1; i <= billsCount; i++) {
  2141. info.sheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  2142. posSheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  2143. }
  2144. }
  2145. if (info.cellRange.col === 0 && info.cellRange.colCount === 1) {
  2146. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2147. if (dealBills && dealBills.length > 0) {
  2148. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  2149. const curRow = iRow + info.cellRange.row;
  2150. const bills = _.find(dealBills, {code: info.sheet.getText(curRow, 0)});
  2151. if (bills) {
  2152. info.sheet.getCell(curRow, 1).value(bills.name);
  2153. info.sheet.getCell(curRow, 2).value(bills.unit);
  2154. info.sheet.getCell(curRow, 3).value(bills.unit_price);
  2155. }
  2156. }
  2157. }
  2158. }
  2159. });
  2160. this.qdSpread.bind(spreadNS.Events.EditEnded, function (e, info) {
  2161. if (info.col === 0) {
  2162. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2163. if (dealBills && dealBills.length > 0) {
  2164. const bills = _.find(dealBills, {code: info.editingText});
  2165. if (bills) {
  2166. info.sheet.getCell(info.row, 1).value(bills.name);
  2167. info.sheet.getCell(info.row, 2).value(bills.unit);
  2168. info.sheet.getCell(info.row, 3).value(bills.unit_price);
  2169. }
  2170. }
  2171. }
  2172. });
  2173. this.obj.bind('shown.bs.modal', function () {
  2174. self.qdSpread.refresh();
  2175. self.posSpread.refresh();
  2176. self.dealSpread.refresh();
  2177. });
  2178. $('#batch-ok').click(function () {
  2179. const sheet = ledgerSpread.getActiveSheet();
  2180. const sel = sheet.getSelections()[0];
  2181. const row = sel.row;
  2182. const select = ledgerTree.nodes[row];
  2183. if (select) {
  2184. const insertData = {};
  2185. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  2186. insertData.id = ledgerTree.getNodeKey(select);
  2187. insertData.batchData = self.getBatchData();
  2188. if (insertData.batchData.length > 0) {
  2189. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  2190. pos.updateDatas(data.pos);
  2191. const result = ledgerTree.loadPostData(data.ledger);
  2192. treeOperationObj.refreshTree(sheet, result);
  2193. sheet.setSelection(result.create[0].index, sel.col, sel.rowCount, sel.colCount);
  2194. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, result.create[0].index]);
  2195. treeOperationObj.refreshOperationValid(sheet);
  2196. self.obj.modal('hide');
  2197. }, null, true);
  2198. } else {
  2199. self.obj.modal('hide');
  2200. }
  2201. }
  2202. });
  2203. }
  2204. // 初始化左侧表格
  2205. initView () {
  2206. // 初始化 清单编号
  2207. const qdSheet = this.qdSpread.getActiveSheet();
  2208. qdSheet.setColumnWidth(0, 45, spreadNS.SheetArea.rowHeader);
  2209. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  2210. SpreadJsObj.refreshColumnAlign(qdSheet);
  2211. // 清理原有数据
  2212. SpreadJsObj.beginMassOperation(qdSheet);
  2213. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2214. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  2215. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  2216. }
  2217. qdSheet.setSelection(0, 0, 1 ,1);
  2218. SpreadJsObj.endMassOperation(qdSheet);
  2219. // 初始化 部位数量复核表
  2220. const posSheet = this.posSpread.getActiveSheet();
  2221. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  2222. SpreadJsObj.refreshColumnAlign(posSheet);
  2223. // 清理原有数据
  2224. SpreadJsObj.beginMassOperation(posSheet);
  2225. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  2226. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2227. posSheet.setSelection(0, 0, 1 ,1);
  2228. SpreadJsObj.endMassOperation(posSheet);
  2229. // 检查签约清单数据,以工具栏数据为准
  2230. if (dealBills) {
  2231. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  2232. }
  2233. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  2234. }
  2235. // 获取界面数据
  2236. getBatchData () {
  2237. const result = [];
  2238. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  2239. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  2240. if (qdSheet.getText(iRow, 0) === '') { continue; }
  2241. const qd = {
  2242. b_code: qdSheet.getText(iRow, 0),
  2243. name: qdSheet.getText(iRow, 1),
  2244. unit: qdSheet.getText(iRow, 2),
  2245. price: _.toNumber(qdSheet.getText(iRow, 3)),
  2246. pos: [],
  2247. };
  2248. result.push(qd);
  2249. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  2250. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  2251. if (value !== 0 && !isNaN(value)) {
  2252. qd.pos.push({
  2253. name: posSheet.getText(iPosRow, 0),
  2254. drawing_code: posSheet.getText(iPosRow, 1),
  2255. quantity: value, porder: qd.pos.length + 1,
  2256. });
  2257. }
  2258. }
  2259. }
  2260. return result;
  2261. }
  2262. }
  2263. $('#searchAccount').click(() => {
  2264. const data = {
  2265. keyword: $('#searchName').val(),
  2266. };
  2267. postData('/search/user', data, (data) => {
  2268. const resultDiv = $('#searchResult');
  2269. if (data) {
  2270. $('h5>span', resultDiv).text(data.name);
  2271. $('#addAuditor').attr('auditorId', data.id);
  2272. $('h6', resultDiv).text(data.role);
  2273. $('p', resultDiv).text(data.company);
  2274. resultDiv.show();
  2275. } else {
  2276. toastr.info('未查询到该审核人');
  2277. resultDiv.hide();
  2278. }
  2279. }, () => {
  2280. $('#searchResult').hide();
  2281. });
  2282. });
  2283. // 审批人分组选择
  2284. $('#account_group').change(function () {
  2285. let account_html = '<option value="0">选择审批人</option>';
  2286. for (const account of accountList) {
  2287. if (parseInt($(this).val()) === 0 || parseInt($(this).val()) === account.account_group) {
  2288. const role = account.role !== '' ? '(' + account.role + ')' : '';
  2289. const company = account.company !== '' ? ' -' + account.company : '';
  2290. account_html += '<option value="' + account.id + '">' + account.name + role + company + '</option>';
  2291. }
  2292. }
  2293. $('#account_list').html(account_html);
  2294. });
  2295. // 添加到审批流程中
  2296. $('body').on('change', '#account_list', function () {
  2297. let id = $(this).val();
  2298. id = parseInt(id);
  2299. if (id !== 0) {
  2300. const data = {
  2301. auditorId: $(this).val(),
  2302. };
  2303. postData('/tender/' + getTenderId() + '/ledger/audit/add', data, (data) => {
  2304. const html = [];
  2305. html.push('<li class="list-group-item" auditorId="' + data.audit_id + '"><a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
  2306. html.push('<span>');
  2307. html.push(data.audit_order + ' ');
  2308. html.push(data.name + ' ');
  2309. html.push('</span>');
  2310. html.push('<small class="text-muted">');
  2311. html.push(data.role);
  2312. html.push('</small></li>');
  2313. $('#auditors').append(html.join(''));
  2314. // 如果是重新上报,添加到重新上报列表中
  2315. const auditorshtml = [];
  2316. // 重新上报时。令其它的审批人流程图标转换
  2317. $('#auditors-list li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
  2318. for (let i = 0; i < $('#auditors-list li').length; i++) {
  2319. $('#auditors-list li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
  2320. $('#auditors-list2 li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
  2321. }
  2322. // 添加新审批人
  2323. auditorshtml.push('<li class="list-group-item" data-auditid="' + data.audit_id + '">');
  2324. auditorshtml.push('<i class="fa fa-stop-circle"></i> ');
  2325. auditorshtml.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
  2326. auditorshtml.push('<span class="pull-right">终审</span>');
  2327. auditorshtml.push('</li>');
  2328. $('#auditors-list').append(auditorshtml.join(''));
  2329. const auditorshtml2 = [];
  2330. // 重新上报时。令其它的审批人流程图标转换
  2331. $('#auditors-list2 li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
  2332. // 添加新审批人
  2333. auditorshtml2.push('<li class="list-group-item" data-auditid="' + data.audit_id + '">');
  2334. auditorshtml2.push('<h5 class="card-title"><i class="fa fa-stop-circle"></i> ');
  2335. auditorshtml2.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
  2336. auditorshtml2.push('<span class="pull-right">终审</span>');
  2337. auditorshtml2.push('</h5></li>');
  2338. $('#auditors-list2').append(auditorshtml2.join(''));
  2339. });
  2340. }
  2341. });
  2342. $('body').on('click', '#auditors li>a', function () {
  2343. const li = $(this).parent();
  2344. const data = {
  2345. auditorId: parseInt(li.attr('auditorId')),
  2346. };
  2347. postData('/tender/' + getTenderId() + '/ledger/audit/delete', data, (data2) => {
  2348. li.remove();
  2349. for (const a of data2) {
  2350. const aLi = $('li[auditorId=' + a.audit_id + ']');
  2351. $('span', aLi).text(a.audit_order + ' ' + a.name + ' ');
  2352. }
  2353. // 如果是重新上报
  2354. // 令最后一个图标转换
  2355. $('#auditors-list li[data-auditid="' + data.auditorId + '"]').remove();
  2356. if ($('#auditors-list li').length !== 0 && !$('#auditors-list li i').hasClass('fa-stop-circle')) {
  2357. $('#auditors-list li').eq($('#auditors-list li').length-1).children('i')
  2358. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  2359. }
  2360. $('#auditors-list2 li[data-auditid="' + data.auditorId + '"]').remove();
  2361. if ($('#auditors-list2 li').length !== 0 && !$('#auditors-list2 li i').hasClass('fa-stop-circle')) {
  2362. $('#auditors-list2 li').eq($('#auditors-list2 li').length-1).children('i')
  2363. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  2364. }
  2365. for (let i = 0; i < $('#auditors-list li').length; i++) {
  2366. $('#auditors-list li').eq(i).find('.pull-right').text((i+1 === $('#auditors-list li').length ? '终' : transFormToChinese(i+1)) + '审')
  2367. $('#auditors-list2 li').eq(i).find('.pull-right').text((i+1 === $('#auditors-list2 li').length ? '终' : transFormToChinese(i+1)) + '审')
  2368. }
  2369. });
  2370. });
  2371. $('#hideSp').click(function () {
  2372. $('#sp-list2').modal('hide');
  2373. });
  2374. // 多层modal关闭后的滚动bug修复
  2375. $('#sp-list2').on('hidden.bs.modal', function (e) {
  2376. $(document.body).addClass('modal-open');
  2377. });
  2378. // 显示层次
  2379. (function (select, sheet) {
  2380. $(select).click(function () {
  2381. const tag = $(this).attr('tag');
  2382. const tree = sheet.zh_tree;
  2383. if (!tree) return;
  2384. switch (tag) {
  2385. case "1":
  2386. case "2":
  2387. case "3":
  2388. case "4":
  2389. case "5":
  2390. tree.expandByLevel(parseInt(tag));
  2391. SpreadJsObj.refreshTreeRowVisible(sheet);
  2392. break;
  2393. case "last":
  2394. tree.expandByCustom(() => { return true; });
  2395. SpreadJsObj.refreshTreeRowVisible(sheet);
  2396. break;
  2397. case "leafXmj":
  2398. tree.expandToLeafXmj();
  2399. SpreadJsObj.refreshTreeRowVisible(sheet);
  2400. break;
  2401. }
  2402. });
  2403. })('a[name=showLevel]', ledgerSpread.getActiveSheet());
  2404. // $('#exportLedger').click(function () {
  2405. // const data = [];
  2406. // const setting = {
  2407. // header: ['项目节编号', '清单子目号', '部位明细', '名称', '单位', '清单数量', '设计数量1', '设计数量2', '单价', '合价', '图号', '备注'],
  2408. // width: [100, 70, 70, 300, 60, 80, 80, 80, 80, 80, 100, 100],
  2409. // hAlign: ['left', 'left', 'left', 'left','center', 'right', 'right', 'right', 'right', 'right', 'left', 'left'],
  2410. // };
  2411. // for (const node of ledgerTree.nodes) {
  2412. // data.push([node.code, node.b_code, '', node.name, node.unit,
  2413. // node.quantity, node.dgn_qty1, node.dgn_qty2, node.unit_price, node.total_price,
  2414. // node.drawing_code, node.memo
  2415. // ]);
  2416. // const posRange = pos.getLedgerPos(node.id);
  2417. // if (posRange && posRange.length > 0) {
  2418. // for (const [i, p] of posRange.entries()) {
  2419. // data.push(['', '', (i + 1) + '', p.name, '',
  2420. // p.quantity, '', '', '', '',
  2421. // p.drawing_code, p.memo
  2422. // ]);
  2423. // }
  2424. // }
  2425. // }
  2426. //
  2427. // XLSXObj.exportXlsxSheet(setting, data, "台账分解.xlsx");
  2428. // });
  2429. $('#exportLedger').click(function () {
  2430. const data = [];
  2431. const setting = {
  2432. cols: [
  2433. {title: '项目节编号', colSpan: '1', rowSpan: '1', field: 'code', hAlign: 0, width: 100, formatter: '@'},
  2434. {title: '清单子目号', colSpan: '1', rowSpan: '1', field: 'b_code', hAlign: 0, width: 70, formatter: '@'},
  2435. {title: '部位明细', colSpan: '1', rowSpan: '1', field: 'pos_code', hAlign: 1, width: 70, formatter: '@'},
  2436. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 300, formatter: '@'},
  2437. {title: '单位', colSpan: '1', rowSpan: '1', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  2438. {title: '清单数量', colSpan: '1', rowSpan: '1', field: 'quantity', hAlign: 2, width: 80, type: 'Number'},
  2439. {title: '设计数量1', colSpan: '1', rowSpan: '1', field: 'dgn_qty1', hAlign: 2, width: 80, type: 'Number'},
  2440. {title: '设计数量2', colSpan: '1', rowSpan: '1', field: 'dgn_qty2', hAlign: 2, width: 80, type: 'Number'},
  2441. {title: '单价', colSpan: '1', rowSpan: '1', field: 'unit_price', hAlign: 2, width: 80, type: 'Number'},
  2442. {title: '合价', colSpan: '1', rowSpan: '1', field: 'total_price', hAlign: 2, width: 80, type: 'Number'},
  2443. {title: '图号', colSpan: '1', rowSpan: '1', field: 'drawing_code', hAlign: 0, width: 100, formatter: '@'},
  2444. {title: '备注', colSpan: '1', rowSpan: '1', field: 'memo', hAlign: 0, width: 100, formatter: '@'},
  2445. ],
  2446. headRows: 1,
  2447. headRowHeight: [32],
  2448. defaultRowHeight: 21,
  2449. headerFont: 'bold 10px 微软雅黑',
  2450. font: '10px 微软雅黑'
  2451. };
  2452. for (const node of ledgerTree.nodes) {
  2453. data.push({
  2454. code: node.code, b_code: node.b_code, name: node.name, unit: node.unit,
  2455. unit_price: node.unit_price, quantity: node.quantity, total_price: node.total_price,
  2456. dgn_qty1: node.dgn_qty1, dgn_qty2: node.dgn_qty2,
  2457. drawing_code: node.drawing_code, memo: node.memo
  2458. });
  2459. const posRange = pos.getLedgerPos(node.id);
  2460. if (posRange && posRange.length > 0) {
  2461. for (const [i, p] of posRange.entries()) {
  2462. data.push({
  2463. pos_code: (i + 1) + '', name: p.name,
  2464. quantity: p.quantity, drawing_code: p.drawing_code, memo: p.memo
  2465. });
  2466. }
  2467. }
  2468. }
  2469. SpreadExcelObj.exportSimpleXlsxSheet(setting, data, "台账分解.xlsx");
  2470. });
  2471. });
  2472. // 检查上报情况
  2473. function checkAuditorFrom () {
  2474. if ($('#auditors li').length === 0) {
  2475. toastr.error('请先选择审批人,再上报数据');
  2476. return false;
  2477. }
  2478. $('#hide-all').show();
  2479. }