ledger.js 120 KB

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