change_revise.js 117 KB

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