change_revise.js 117 KB

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