calc_program.js 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  1. /**
  2. * Created by CSL on 2017-07-19.
  3. * 计算程序。所有定额、清单、父清单的计算都从此入。
  4. */
  5. let calcTools = {
  6. getNodeByFlag: function (flag) {
  7. let bill = cbTools.findBill(flag);
  8. if (bill) return this.getNodeByID(bill.ID)
  9. else return null;
  10. },
  11. getNodeByID: function (ID){
  12. return cbTools.getNodeByID(ID);
  13. },
  14. // 是否是标题清单
  15. isTitleBills: function (node) {
  16. let flag = node.getFlag();
  17. return titleFlags.includes(flag);
  18. },
  19. isBill: function(treeNode){
  20. return treeNode.sourceType === ModuleNames.bills;
  21. },
  22. isParentBill: function (treeNode) {
  23. return this.isBill(treeNode) &&
  24. treeNode.source.children &&
  25. treeNode.source.children.length > 0;
  26. },
  27. isLeafBill: function(treeNode){ // 下面挂有定额的清单也是叶子清单
  28. return this.isBill(treeNode) &&
  29. treeNode.source.children &&
  30. treeNode.source.children.length === 0;
  31. },
  32. isLeafNode: function(treeNode){ // 最底层结点,如定额等。
  33. return treeNode.children.length === 0;
  34. },
  35. isBill_DXFY: function(treeNode){
  36. return this.isBill(treeNode) && treeNode.data.type == billType.DXFY;
  37. },
  38. isBill_FB: function(treeNode){
  39. return this.isBill(treeNode) && treeNode.data.type == billType.FB;
  40. },
  41. isBill_FX: function(treeNode){
  42. return this.isBill(treeNode) && treeNode.data.type == billType.FX;
  43. },
  44. isBill_BILL: function(treeNode){
  45. return this.isBill(treeNode) && treeNode.data.type == billType.BILL;
  46. },
  47. isBill_BX: function(treeNode){
  48. return this.isBill(treeNode) && treeNode.data.type == billType.BX;
  49. },
  50. isNullBill: function (treeNode) {
  51. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  52. },
  53. isCalcBaseBill: function(treeNode){
  54. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (treeNode.data.calcBase);
  55. },
  56. isTotalCostBill: function (treeNode) {
  57. return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
  58. treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
  59. },
  60. isRationCategory: function(treeNode){
  61. return treeNode.sourceType === ModuleNames.ration;
  62. },
  63. isRationItem: function(treeNode){
  64. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.ration;
  65. },
  66. isCalcManageRation:function(treeNode){
  67. return this.isRationCategory(treeNode) && (treeNode.data.type === rationType.ration || treeNode.data.type === rationType.install|| treeNode.data.type === rationType.itemIncrease);
  68. },
  69. isVolumePrice: function (treeNode) {
  70. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.volumePrice;
  71. },
  72. isGljRation: function (treeNode) {
  73. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.gljRation;
  74. },
  75. isVP_or_GLJR: function (treeNode) { // 是量价或工料机类型的定额
  76. return this.isRationCategory(treeNode) &&
  77. (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation);
  78. },
  79. isSameTypeNode: function (node1, node2) {
  80. if (node1.parent && node2.parent && (node1.parent === node2.parent) && (node1.sourceType === node1.sourceType)
  81. && (node1.data && node2.data && node1.data.type === node2.data.type)){
  82. return true;
  83. }
  84. return false;
  85. },
  86. isInheritFrom: function (treeNode, flagsArr){
  87. let cur = treeNode;
  88. while (cur.parent) {
  89. cur = cur.parent;
  90. };
  91. let flag = -1;
  92. if (cur.data.flagsIndex && cur.data.flagsIndex.fixed && cur.data.flagsIndex.fixed.flag)
  93. flag = cur.data.flagsIndex.fixed.flag;
  94. return flagsArr.includes(flag);
  95. },
  96. isFBFX:function (treeNode) {
  97. return projectObj.project.Bills.isFBFX(treeNode);
  98. },
  99. isTechMeasure:function(treeNode){
  100. return projectObj.project.Bills.isTechMeasure(treeNode)
  101. },
  102. canCalcToTalFeeByOwn: function (treeNode) {
  103. return !projectObj.project.Bills.cantCalcToTalFeeByOwn(treeNode);
  104. },
  105. getChildrenFormulaNodes: function (self, allFormulaNodesArr, parentNodes){ // 获取结点parentNodes下有公式的子结点
  106. let nodes = [];
  107. for (let pn of parentNodes){
  108. for (let node of allFormulaNodesArr){
  109. let cur = node;
  110. while (cur) {
  111. if (cur == pn && node != self){
  112. nodes.push(node);
  113. break;
  114. }
  115. cur = cur.parent;
  116. };
  117. };
  118. };
  119. return nodes;
  120. },
  121. /* 重要说明:
  122. 此时得到的GLJList,每条glj都有tenderQuantity = glj的quantity * 定额的quantity * glj的消耗量调整系数coe。
  123. 与定额的tenderQuantity无关,与定额的子目工程量调整系数coe无关。例如:
  124. 例:定额AB0003,工程量5,下含工料机“建筑综合工”,消耗量0.202。调价:人材机单价系数4,人工系数3,子目工程量系数2。
  125. 则GLJList中的建筑综合工:quantity 1.01, tenderQuantity 3.03。marketPrice 115, tenderPrice 460。
  126. */
  127. getGLJList: function (treeNode, needOneBill) {
  128. delete treeNode.data.gljList;
  129. if (this.isRationCategory(treeNode)) {
  130. if (!calcTools.isVP_or_GLJR(treeNode)){
  131. treeNode.data.gljList = projectObj.project.calcProgram.getGljArrByRation(treeNode.data);
  132. }
  133. }
  134. else if (this.isBill(treeNode)){
  135. treeNode.data.gljList = projectObj.project.ration_glj.getGljArrByBill(treeNode, needOneBill);
  136. };
  137. },
  138. getGLJListByProgramID: function (programID) {
  139. let rations = calcTools.getRationsByProgramID(programID);
  140. let gljList = projectObj.project.ration_glj.getGljArrByRations(rations);
  141. return gljList;
  142. },
  143. getLeafBills: function (treeNode){
  144. let leafBills = [];
  145. function getBill(node){
  146. if (!node) return;
  147. if (calcTools.isLeafBill(node))
  148. leafBills.push(node);
  149. if (node.firstChild()) getBill(node.firstChild());
  150. if (node.nextSibling) getBill(node.nextSibling);
  151. };
  152. let fc = treeNode.firstChild();
  153. if (fc) getBill(fc);
  154. return leafBills;
  155. },
  156. forceSelect: function (treeNode, rowsCount = 1, colsCount = 2){
  157. projectObj.mainController.tree.selected = treeNode;
  158. let tree = treeNode.tree;
  159. let idx = tree.items.indexOf(treeNode);
  160. let sheet = projectObj.mainSpread.getActiveSheet();
  161. sheet.setSelection(idx, 0, rowsCount, colsCount);
  162. sheet.showRow(idx, GC.Spread.Sheets.VerticalPosition.center);
  163. },
  164. initFees: function (treeNode){
  165. if (!treeNode.data.fees) {
  166. treeNode.data.fees = [];
  167. treeNode.data.feesIndex = {};
  168. treeNode.changed = true;
  169. }
  170. else if (!treeNode.data.feesIndex){
  171. treeNode.data.feesIndex = {};
  172. for (let fee of treeNode.data.fees){
  173. treeNode.data.feesIndex[fee.fieldName] = fee;
  174. };
  175. treeNode.changed = true;
  176. };
  177. },
  178. initFeeField: function (treeNode, fieldName){
  179. this.initFees(treeNode);
  180. if (!treeNode.data.feesIndex[fieldName]) {
  181. let fee = {
  182. 'fieldName': fieldName,
  183. 'unitFee': 0,
  184. 'totalFee': 0,
  185. 'tenderUnitFee': 0,
  186. 'tenderTotalFee': 0
  187. };
  188. treeNode.data.fees.push(fee);
  189. treeNode.data.feesIndex[fieldName] = fee;
  190. treeNode.changed = true;
  191. };
  192. },
  193. checkFeeField: function (treeNode, feeObj){
  194. if (!feeObj) return;
  195. if (feeObj.fieldName == '') return;
  196. // 初始化前,先拦截属性未定义、又要给该属性赋0的情况
  197. if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
  198. if (feeObj.unitFee == 0 && feeObj.totalFee == 0 && feeObj.tenderUnitFee == 0 && feeObj.tenderTotalFee == 0) return;
  199. }
  200. this.initFeeField(treeNode, feeObj.fieldName);
  201. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  202. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  203. treeNode.changed = true;
  204. };
  205. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  206. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  207. treeNode.changed = true;
  208. };
  209. // 不知在何种情况下,tenderUnitFee、tenderTotalFee的值会变成NaN,这里提前处理一下
  210. if (isNaN(treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee))
  211. treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee = undefined;
  212. if (isNaN(treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee))
  213. treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee = undefined;
  214. if (treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee != feeObj.tenderUnitFee){
  215. treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee = feeObj.tenderUnitFee;
  216. treeNode.changed = true;
  217. };
  218. if (treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee != feeObj.tenderTotalFee){
  219. treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee = feeObj.tenderTotalFee;
  220. treeNode.changed = true;
  221. };
  222. },
  223. setFieldValue: function (treeNode, fieldName, value){
  224. if (fieldName.includes('feesIndex')){
  225. let arr = fieldName.split('.');
  226. this.initFeeField(treeNode, arr[1]);
  227. treeNode.data.feesIndex[arr[1]][arr[2]] = value;
  228. if(fieldName == 'feesIndex.common.unitFee'){
  229. let fee = _.find(treeNode.data.fees,{'fieldName':arr[1]});
  230. if(fee) fee[arr[2]] = value;
  231. }
  232. }
  233. else{
  234. treeNode.data[fieldName] = value;
  235. };
  236. treeNode.changed = true;
  237. },
  238. initSummaryFee: function (treeNode){
  239. if (!treeNode.data.summaryFees){
  240. treeNode.data.summaryFees = {
  241. totalFee: 0,
  242. estimateFee: 0,
  243. safetyFee: 0,
  244. chargeFee: 0
  245. };
  246. treeNode.changed = true;
  247. };
  248. },
  249. hasAdjustPrice: function(){ // 编办是否有材料的调整价发文
  250. if (projectObj.project.projSetting.glj_col && projectObj.project.projSetting.glj_col.showAdjustPrice == true)
  251. return true
  252. else
  253. return false;
  254. },
  255. // 参数fieldName值: 'common.totalFee'、'equipment.unitFee'
  256. getFee: function (treeNode, fieldName) {
  257. if (!treeNode) return 0;
  258. let ns = fieldName.split(".");
  259. if (ns.length != 2)
  260. return 0
  261. else if (treeNode.data.feesIndex && treeNode.data.feesIndex[ns[0]] && treeNode.data.feesIndex[ns[0]][ns[1]])
  262. return parseFloat(treeNode.data.feesIndex[ns[0]][ns[1]])
  263. else
  264. return 0;
  265. },
  266. rationBaseFee: function (treeNode, gljTypes, priceType, isTender){
  267. if (!treeNode.data.gljList) return 0;
  268. let me = this, result = 0;
  269. let price = 0, temp = 0, temp2 = 0;
  270. // 机械组成物价差: 机上人工、动力燃料
  271. if ((priceType == priceTypes.ptDiffPrice) && (gljTypes.includes(gljType.MACHINE_LABOUR) || gljTypes.includes(gljType.FUEL_POWER_FEE))){
  272. for (let glj of treeNode.data.gljList) {
  273. if ([gljType.GENERAL_MACHINE, gljType.INSTRUMENT].includes(glj.type)){
  274. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  275. if (!mds) mds = [];
  276. for (let md of mds){
  277. if (gljTypes.includes(md.type)){
  278. let gljQ = isTender ? me.uiGLJQty(glj["tenderQuantity"]) : me.uiGLJQty(glj["quantity"]);
  279. let mdMP = isTender ? md["tenderPrice"] : md["marketPrice"];
  280. let mdQ = me.uiGLJQty(md.consumption);
  281. let mdAP = calcTools.hasAdjustPrice() ? md["adjustPrice"] : md["basePrice"];
  282. // if (aprice != mprice){
  283. temp = (temp + (gljQ * mdQ * mdMP).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  284. temp2 = (temp2 + (gljQ * mdQ * mdAP).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  285. // }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. // 普通基数计算(包括普通价差)
  292. else{
  293. for (let glj of treeNode.data.gljList) {
  294. if (gljTypes.indexOf(glj.type) >= 0) {
  295. let qty = isTender ? me.uiGLJQty(glj["tenderQuantity"]) : me.uiGLJQty(glj["quantity"]);
  296. // let mprice = isTender ? me.uiGLJPrice(glj["tenderPrice"], glj) : me.uiGLJPrice(glj["marketPrice"], glj);
  297. // let aprice = calcTools.hasAdjustPrice() ? me.uiGLJPrice(glj["adjustPrice"], glj) : me.uiGLJPrice(glj["basePrice"], glj);
  298. let mprice = isTender ? glj["tenderPrice"] : glj["marketPrice"];
  299. let aprice = calcTools.hasAdjustPrice() ? glj["adjustPrice"] : glj["basePrice"];
  300. if (priceType == priceTypes.ptDiffPrice){
  301. // if (aprice != mprice){
  302. temp = (temp + (qty * mprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  303. temp2 = (temp2 + (qty * aprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  304. // }
  305. }
  306. else {
  307. // if (priceType == priceTypes.ptBasePrice){ price = me.uiGLJPrice(glj["basePrice"], glj);}
  308. if (priceType == priceTypes.ptBasePrice){ price = glj["basePrice"];}
  309. else if (priceType == priceTypes.ptAdjustPrice){price = aprice;}
  310. else if (priceType == priceTypes.ptMarketPrice){price = mprice;}
  311. /*if (projectObj.project.property.areaSetting && treeNode.data.areaIncreaseFee){
  312. let p;
  313. if ([gljType.LABOUR].includes(glj.type))
  314. p = projectObj.project.property.areaSetting.labour
  315. else if (baseMaterialTypes.includes(glj.type))
  316. p = projectObj.project.property.areaSetting.material
  317. else if ([gljType.GENERAL_MACHINE].includes(glj.type))
  318. p = projectObj.project.property.areaSetting.machine;
  319. qty = qty * (1 + p * 0.01).toDecimal(decimalObj.process);
  320. }*/
  321. temp = (qty * price).toDecimal(decimalObj.process);
  322. result = (result + temp).toDecimal(decimalObj.process);
  323. };
  324. };
  325. };
  326. };
  327. if (priceType == priceTypes.ptDiffPrice){
  328. if (typeof isCQ2018 != 'undefined'){
  329. // 如下这一句十分重要!JS计算误差导致379.08-331.695=47.38499999999999。如果直接取2位会变成47.38。所以先取6位47.385,再取2位47.39。
  330. result = (temp - temp2).toDecimal(decimalObj.process);
  331. result = result.toDecimal(decimalObj.ration.unitPrice) // 重庆2018所有都是先汇总相减后再取舍
  332. }
  333. else{
  334. if (gljTypes == baseMaterialTypes)
  335. result = (temp - temp2).toDecimal(decimalObj.ration.unitPrice)
  336. else
  337. result = (temp.toDecimal(decimalObj.ration.unitPrice) - temp2.toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  338. };
  339. }
  340. else{
  341. result = result.toDecimal(decimalObj.ration.unitPrice);
  342. };
  343. return result;
  344. },
  345. // masterTypeFilter 过滤机械机型:[]全部, [1,2]特大机械 [3,4]中小机械。 detailType 如机上人工费、机械折旧费等
  346. machineDetailFee: function (treeNode, gljArr, masterTypeFilter, detailType, isTender) {
  347. if (!gljArr) return 0;
  348. let result = 0;
  349. for (let glj of gljArr) {
  350. if (baseMachineMasterTypes.includes(glj.type)){
  351. // 机型不符
  352. if ((masterTypeFilter.length > 0) && (glj.model && !masterTypeFilter.includes(glj.model))) continue;
  353. let gljQ = isTender ? glj.tenderQuantity : glj.quantity;
  354. // 获取机械组成物(调价不深入到组成物)
  355. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  356. if (!mds) mds = [];
  357. let mdSum = 0;
  358. for (let md of mds) {
  359. if (md.type == detailType) {
  360. let q = md["consumption"] ? md["consumption"] : 0;
  361. // 下面这句从养护来,建筑暂时用不上,以后可能会用上
  362. // let p = isTender ? (md["tenderPrice"] ? md["tenderPrice"] : 0) : (md["marketPrice"] ? md["marketPrice"] : 0);
  363. let p = md["basePrice"] ? md["basePrice"] : 0;
  364. mdSum = mdSum + (q * p).toDecimal(decimalObj.glj.unitPriceHasMix);
  365. mdSum = (mdSum).toDecimal(decimalObj.glj.unitPriceHasMix);
  366. }
  367. }
  368. if (typeof isCQ2018 != 'undefined')
  369. result = (result + (gljQ * mdSum).toDecimal(decimalObj.process)).toDecimal(decimalObj.process)
  370. else
  371. result = (result + (gljQ * mdSum).toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  372. }
  373. }
  374. result = (result).toDecimal(decimalObj.ration.unitPrice);
  375. return result;
  376. },
  377. // 叶子清单、定额、总造价清单的暂估费。(父级清单是汇总子清单的暂估费,走计算程序逻辑,不在这里)
  378. estimateFee: function (treeNode, isBase, isTender){ // isBase, isTender 这两个参数用于基数计算
  379. let me = this, sumU = 0, sumT = 0, sumTU = 0, sumTT = 0;
  380. let nodeQ = me.uiNodeQty(treeNode);
  381. let nodeTQ = me.uiNodeTenderQty(treeNode);
  382. let isGather = (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial);
  383. // 先汇总数量,再乘市场价。如果是叶子清单,进入这里的gljList中的材料,已经是同类材料跨定额汇总过的了。
  384. function eTFee(){
  385. let rst = {eT: 0, eTT: 0};
  386. if (!treeNode.data.gljList) return rst;
  387. let GLJObjs = [];
  388. for (let glj of treeNode.data.gljList) {
  389. if (!allMaterialTypes.includes(glj.type)) continue;
  390. if (glj.isEstimate){
  391. let q = me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.process);
  392. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type, quantity: q,
  393. marketPrice: glj.marketPrice, tenderQuantity: glj.tenderQuantity, tenderPrice: glj.tenderPrice});
  394. }
  395. else{ // 组成物
  396. if (!compositionTypes.includes(glj.type)) continue;
  397. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  398. if (!mds) mds = [];
  399. for (let md of mds){
  400. if (!md.isEstimate) continue;
  401. let isExist = false;
  402. // let glj_totalQ = (nodeQ * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.glj.quantity);
  403. let glj_totalQ = me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.glj.quantity);
  404. let glj_tender_totalQ = (nodeTQ * me.uiGLJQty(glj.tenderQuantity)).toDecimal(decimalObj.glj.quantity);
  405. let mdQ = (glj_totalQ * me.uiGLJQty(md.consumption)).toDecimal(decimalObj.process);
  406. let mdTQ = (glj_tender_totalQ * me.uiGLJQty(md.consumption)).toDecimal(decimalObj.process);
  407. for (let obj of GLJObjs){
  408. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  409. isExist = true;
  410. obj.quantity = (obj.quantity + mdQ).toDecimal(decimalObj.glj.quantity);
  411. obj.tenderQuantity = (obj.tenderQuantity + mdTQ).toDecimal(decimalObj.glj.quantity);
  412. break;
  413. }
  414. };
  415. if (!isExist)
  416. GLJObjs.push({code: md.code, name: md.name, specs: md.specs, unit: md.unit, type: md.type, quantity: mdQ,
  417. marketPrice: md.marketPrice, tenderQuantity: mdTQ, tenderPrice: md.tenderPrice});
  418. }
  419. }
  420. };
  421. for (let obj of GLJObjs){
  422. let t = (me.uiGLJQty(obj.quantity) * me.uiGLJPrice(obj.marketPrice, obj)).toDecimal(decimalObj.bills.totalPrice);
  423. rst.eT = (rst.eT + t).toDecimal(decimalObj.bills.totalPrice);
  424. let tt = (me.uiGLJQty(obj.tenderQuantity) * me.uiGLJPrice(obj.tenderPrice, obj)).toDecimal(decimalObj.bills.totalPrice);
  425. rst.eTT = (rst.eTT + tt).toDecimal(decimalObj.bills.totalPrice);
  426. };
  427. return rst;
  428. };
  429. // 汇总子结点的暂估合价
  430. function eTFeeByChildren(){
  431. let rst = {eT: 0, eTT: 0};
  432. for (let node of treeNode.children){
  433. if (node.data.feesIndex && node.data.feesIndex['estimate']) {
  434. rst.eT = (rst.eT + parseFloatPlus(node.data.feesIndex['estimate'].totalFee)).toDecimal(decimalObj.process);
  435. rst.eTT = (rst.eTT + parseFloatPlus(node.data.feesIndex['estimate'].tenderTotalFee)).toDecimal(decimalObj.process);
  436. };
  437. };
  438. rst.eT = (rst.eT).toDecimal(decimalObj.bills.totalPrice);
  439. rst.eTT = (rst.eTT).toDecimal(decimalObj.bills.totalPrice);
  440. return rst;
  441. };
  442. // 先数量乘市场价,再汇总
  443. function eUFee(){
  444. if (!treeNode.data.gljList) return 0;
  445. let rst = {eU: 0, eTU: 0};
  446. for (let glj of treeNode.data.gljList) {
  447. if (!allMaterialTypes.includes(glj.type)) continue;
  448. if (glj.isEstimate){
  449. rst.eU = rst.eU + (me.uiGLJQty(glj.quantity) * me.uiGLJPrice(glj.marketPrice, glj)).toDecimal(decimalObj.process);
  450. rst.eU = rst.eU.toDecimal(decimalObj.process);
  451. // 不能直接用glj.tenderPrice,这个值不可靠。当调价界面删除单价系数后,tenderPrice没有实时计算,取得的值为0
  452. rst.eTU = rst.eTU + (me.uiGLJQty(glj.tenderQuantity) * glj.tenderPrice).toDecimal(decimalObj.process);
  453. rst.eTU = rst.eTU.toDecimal(decimalObj.process);
  454. }
  455. else{ // 组成物。
  456. if (!compositionTypes.includes(glj.type)) continue;
  457. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  458. if (!mds) mds = [];
  459. for (let md of mds){
  460. if (!md.isEstimate) continue;
  461. let mdU = (me.uiGLJQty(md.consumption) * me.uiGLJPrice(md.marketPrice)).toDecimal(decimalObj.glj.unitPrice);
  462. rst.eU = rst.eU + (mdU * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.process);
  463. rst.eU = rst.eU.toDecimal(decimalObj.process);
  464. // 数量只调到工料机级别,工料机下的组成物不调量(如机械、混凝土)。调价调的是工料机下的组成物的价。
  465. let mdTU = (me.uiGLJQty(md.consumption) * md.tenderPrice).toDecimal(decimalObj.glj.unitPrice);
  466. rst.eTU = rst.eTU + (mdTU * glj.tenderQuantity).toDecimal(decimalObj.process);
  467. rst.eTU = rst.eTU.toDecimal(decimalObj.process);
  468. }
  469. }
  470. };
  471. rst.eU = rst.eU.toDecimal(decimalObj.bills.unitPrice);
  472. rst.eTU = rst.eTU.toDecimal(decimalObj.bills.unitPrice);
  473. return rst;
  474. };
  475. // 总造价暂估费
  476. if (me.isTotalCostBill(treeNode)){
  477. let nodes = treeNode.tree.roots;
  478. for (let node of nodes){
  479. if (me.isTotalCostBill(node)) break;
  480. let eU = 0, eT = 0, eTU = 0, eTT = 0;
  481. if (node.data.feesIndex && node.data.feesIndex.estimate){
  482. eU = node.data.feesIndex.estimate.unitFee;
  483. eT = node.data.feesIndex.estimate.totalFee;
  484. eTU = node.data.feesIndex.estimate.tenderUnitFee;
  485. eTT = node.data.feesIndex.estimate.tenderTotalFee;
  486. }
  487. else {
  488. eU = 0, eT = 0, eTU = 0, eTT = 0;
  489. };
  490. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  491. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  492. sumTU = (sumTU + parseFloatPlus(eTU)).toDecimal(decimalObj.process);
  493. sumTT = (sumTT + parseFloatPlus(eTT)).toDecimal(decimalObj.process);
  494. };
  495. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  496. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  497. sumTU = (sumTU).toDecimal(decimalObj.bills.unitPrice);
  498. sumTT = (sumTT).toDecimal(decimalObj.bills.totalPrice);
  499. }
  500. else if (me.isParentBill(treeNode)){ // 父清单不要汇总单价。
  501. let eTFBC = eTFeeByChildren();
  502. sumT = eTFBC.eT;
  503. sumTT = eTFBC.eTT;
  504. sumU = undefined;
  505. sumTU = undefined;
  506. }
  507. else if (me.isLeafBill(treeNode)){
  508. if (projectObj.project.Bills.isEngineerEst(treeNode)){
  509. if (treeNode.data.feesIndex['common'] != undefined){
  510. sumT = treeNode.data.feesIndex['common'].totalFee;
  511. sumU = treeNode.data.feesIndex['common'].unitFee;
  512. sumTT = treeNode.data.feesIndex['common'].tenderTotalFee;
  513. sumTU = treeNode.data.feesIndex['common'].tenderUnitFee;
  514. }
  515. }
  516. else{
  517. if (isGather){
  518. me.getGLJList(treeNode, false);
  519. let eTF = eTFee();
  520. sumT = eTF.eT;
  521. sumTT = eTF.eTT;
  522. }
  523. else{
  524. let eTFBC = eTFeeByChildren();
  525. sumT = eTFBC.eT;
  526. sumTT = eTFBC.eTT;
  527. };
  528. let q = nodeQ ? nodeQ : 1;
  529. sumU = (sumT / q).toDecimal(decimalObj.bills.totalPrice);
  530. let tq = nodeTQ ? nodeTQ : 1;
  531. sumTU = (sumTT / tq).toDecimal(decimalObj.bills.totalPrice);
  532. }
  533. }
  534. else if (me.isRationCategory(treeNode)){
  535. me.getGLJList(treeNode, false);
  536. let eUF = eUFee();
  537. sumU = eUF.eU;
  538. sumTU = eUF.eTU;
  539. if (isBase) {
  540. if (isTender)
  541. return sumTU
  542. else
  543. return sumU;
  544. };
  545. if (isGather){
  546. let eTF = eTFee();
  547. sumT = eTF.eT;
  548. sumTT = eTF.eTT;
  549. }
  550. else{
  551. sumT = (nodeQ * sumU).toDecimal(decimalObj.ration.totalPrice);
  552. sumTT = (nodeTQ * sumTU).toDecimal(decimalObj.ration.totalPrice);
  553. }
  554. };
  555. me.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT,
  556. 'tenderUnitFee': sumTU, 'tenderTotalFee': sumTT});
  557. },
  558. marketPriceToBase: function (treeNode, baseName, isTender) {
  559. if (!calcTools.isVP_or_GLJR(treeNode)) return;
  560. let result = 0, me = this;
  561. function isRCJZC(treeNode, baseName) { // 基数名称中是否包含人材机主设,且树结点类型要匹配一致
  562. let rst = (treeNode.data.subType === gljType.LABOUR && baseName.includes('人工')) || // 人工费、市场人工费
  563. (baseMaterialTypes.includes(treeNode.data.subType) && baseName.includes('材料')) ||
  564. // (treeNode.data.subType === gljType.GENERAL_MACHINE && (baseName.includes('机械') || baseName.includes('机具'))) ||
  565. (baseMachineTypes.includes(treeNode.data.subType) && (baseName.includes('机械') || baseName.includes('机具'))) ||
  566. (treeNode.data.subType === gljType.MAIN_MATERIAL && baseName.includes('主材')) ||
  567. (treeNode.data.subType === gljType.EQUIPMENT && baseName.includes('设备'));
  568. return rst;
  569. }
  570. if (baseName.includes('甲供') || baseName.includes('甲定') || baseName.includes('分包')) {
  571. result = 0;
  572. }
  573. else if (baseName.includes('价差')) {
  574. if (treeNode.data.type == rationType.gljRation) {
  575. if (isRCJZC(treeNode, baseName)) {
  576. let aprice = me.uiGLJPrice(treeNode.data.basePrice); // 量价虚拟的工料机不可能有发文,这里直接取定额价。
  577. let mprice = me.uiGLJPrice(treeNode.data.marketUnitFee);
  578. result = (mprice - aprice).toDecimal(decimalObj.ration.unitPrice);
  579. }
  580. }
  581. }
  582. else if (baseName.includes('机上人工')) {
  583. if (treeNode.data.subType === gljType.GENERAL_MACHINE) {
  584. let glj = {
  585. 'code': treeNode.data.code,
  586. 'name': treeNode.data.name,
  587. 'specs': treeNode.data.specs,
  588. 'unit': treeNode.data.unit,
  589. 'quantity': 1,
  590. 'type': treeNode.data.subType // 注意:这里要取subType
  591. };
  592. result = me.machineDetailFee(treeNode, [glj], [], gljType.MACHINE_LABOUR, isTender);
  593. }
  594. }
  595. else {
  596. if (isRCJZC(treeNode, baseName)) {
  597. if (calcTools.isVolumePrice(treeNode)){
  598. if (isTender){
  599. let coe = this.tenderCoe_GLJPrice();
  600. if (treeNode.data.marketUnitFee)
  601. result = (parseFloat(treeNode.data.marketUnitFee) * coe).toDecimal(decimalObj.ration.unitPrice)
  602. else
  603. result = 0;
  604. }
  605. else
  606. result = treeNode.data.marketUnitFee ? parseFloat(treeNode.data.marketUnitFee).toDecimal(decimalObj.ration.unitPrice) : 0
  607. }
  608. else if (calcTools.isGljRation(treeNode)) {
  609. // result = treeNode.data.basePrice ? parseFloat(treeNode.data.basePrice).toDecimal(decimalObj.ration.unitPrice) : 0;
  610. // 这里要取基价或市场价,但不能直接取basePrice,受限于项目属性的三个选项。
  611. if (baseName.includes('定额'))
  612. result = gljOprObj.getBasePrice(treeNode)
  613. else
  614. result = treeNode.data.marketUnitFee ? parseFloat(treeNode.data.marketUnitFee).toDecimal(decimalObj.ration.unitPrice) : gljOprObj.getBasePrice(treeNode);
  615. };
  616. }
  617. };
  618. return result;
  619. },
  620. partASupplyFee: function (treeNode, baseName, isTender, isRationPirce = true) { // isRationPirce 为true时表示取定额价。如:甲供定额材料费
  621. if (!treeNode.data.gljList) return 0;
  622. let projectGLJ = projectObj.project.projectGLJ;
  623. let supplyT = [];
  624. if (baseName.includes('甲供'))
  625. supplyT = [supplyType.BFJG, supplyType.WQJG] // 字段中存储的是汉字、数字混杂!
  626. else if (baseName.includes('甲定'))
  627. supplyT = [supplyType.JDYG];
  628. let gljT = [], compT = [];
  629. if (baseName.includes('人工')) gljT = [gljType.LABOUR]; // 含人工的:如甲供人工、甲定人工
  630. else if (baseName.includes('材料')){
  631. gljT = baseMaterialTypes;
  632. compT = compositionTypes;
  633. }
  634. else if (baseName.includes('机械') || baseName.includes('机具')) {
  635. gljT = baseMachineTypes;
  636. compT = [gljType.GENERAL_MACHINE, gljType.INSTRUMENT, gljType.OTHER_MACHINE_USED]; // 取并集,兼容重庆2018新定额
  637. }
  638. else if (baseName.includes('主材')) {
  639. gljT = [gljType.MAIN_MATERIAL];
  640. compT = [gljType.MAIN_MATERIAL];
  641. }
  642. else if (baseName.includes('设备')) {
  643. gljT = [gljType.EQUIPMENT];
  644. };
  645. // alert(JSON.stringify(projectGLJ.testGLJs()));
  646. let supplyGLJs = projectGLJ.getGLJsBySupply(supplyT, gljT);
  647. if (supplyGLJs.length == 0) return 0;
  648. let supplyGLJsIdx = {};
  649. for (let sglj of supplyGLJs){supplyGLJsIdx[sglj.id] = sglj};
  650. function isSupply(composition, supplies) {
  651. for (let supply of supplies){
  652. if(supply.code == composition.code && supply.name == composition.name && supply.unit == composition.unit &&
  653. supply.specs == composition.specs && supply.type == composition.type ){
  654. composition.basePrice = supply.unit_price.base_price;
  655. composition.marketPrice = supply.unit_price.market_price;
  656. composition.supply = supply.supply;
  657. composition.supplyX = 1;
  658. if (composition.supply == supplyType.BFJG){
  659. let Q = supply.quantity ? supply.quantity : 1;
  660. composition.supplyX = supply.supply_quantity / Q;
  661. }
  662. return true;
  663. }
  664. };
  665. return false;
  666. };
  667. let sum = 0;
  668. for (let glj of treeNode.data.gljList){
  669. let gljQ = isTender ? glj.tenderQuantity : glj.quantity;
  670. let X = 1; // 部分甲供系数(默认1,即完全甲供)
  671. let tempSGLJ = supplyGLJsIdx[glj.projectGLJID];
  672. // 当前材料是甲供材料:①普通 ②商品硂等不计组成物的母体材料
  673. if (tempSGLJ) {
  674. // 处理部分甲供
  675. if (baseName.includes('甲供') && (tempSGLJ.supply == supplyType.BFJG)){
  676. let Q = isTender ? tempSGLJ.tenderQuantity : tempSGLJ.quantity;
  677. // let Q = tempSGLJ.quantity;
  678. Q = Q ? Q : 1;
  679. X = tempSGLJ.supply_quantity / Q;
  680. };
  681. let gljP = isRationPirce ? glj.basePrice : (isTender ? projectGLJ.getTenderMarketPrice(calcTools.getProjectGLJ(glj)) : glj.marketPrice);
  682. sum = (sum + gljP * gljQ * X).toDecimal(decimalObj.process);
  683. }
  684. else{ // 当前材料不是甲供材料
  685. // 混凝土等。组成物的母体,母体如果有组成物,则母体无法作为甲供材料,无法设置,此时要看其组成物是否是甲供材料;母体如果没有组成物,则母体有可能成为甲供材料。
  686. if (compT.includes(glj.type)) {
  687. let pGLJ = projectGLJ.getDataByID(glj.projectGLJID);
  688. let compositions = pGLJ.ratio_data; // 组成物明细
  689. if (compositions.length > 0){
  690. for (let c of compositions){
  691. if (isSupply(c, supplyGLJs)) {
  692. X = 1;
  693. if (baseName.includes('甲供') && (c.supply == supplyType.BFJG)){
  694. X = c.supplyX;
  695. };
  696. let cP = isRationPirce ? c.basePrice : (isTender ? projectGLJ.getTenderMarketPrice(calcTools.getProjectGLJ(c)) : c.marketPrice);
  697. sum = (sum + cP * c.consumption * gljQ * X).toDecimal(decimalObj.process);
  698. }
  699. }
  700. };
  701. }
  702. };
  703. };
  704. sum = sum.toDecimal(decimalObj.ration.unitPrice);
  705. return sum;
  706. },
  707. getCalcType: function (treeNode) {
  708. if (this.isRationCategory(treeNode)){
  709. return treeNodeCalcType.ctRationCalcProgram;
  710. }
  711. else if (this.isNullBill(treeNode)){
  712. return treeNodeCalcType.ctNull;
  713. }
  714. else if (this.isLeafBill(treeNode)) {
  715. if (treeNode.children && treeNode.children.length > 0){
  716. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  717. if (projectObj.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  718. return treeNodeCalcType.ctBillCalcProgram;
  719. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  720. return treeNodeCalcType.ctGatherRationsFees;
  721. }
  722. else{ // 公式计算
  723. return treeNodeCalcType.ctCalcBaseValue;
  724. };
  725. }
  726. else if (this.isBill(treeNode)) { // 父清单:汇总子清单的费用类别
  727. return treeNodeCalcType.ctGatherBillsFees;
  728. }
  729. else {
  730. return treeNodeCalcType.ctRationCalcProgram;
  731. };
  732. },
  733. cutNodeForSave(treeNode){
  734. let me = this;
  735. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  736. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  737. let data = {
  738. projectID: projectObj.project.ID(),
  739. ID: treeNode.data.ID,
  740. unit: treeNode.data.unit, //对清单来说,改变单位,工程量精度会跟着改变从而影响计算。
  741. subType: treeNode.data.subType,
  742. quantity: treeNode.data.quantity,
  743. calcBase: treeNode.data.calcBase,
  744. calcBaseValue: treeNode.data.calcBaseValue,
  745. tenderCalcBaseValue: treeNode.data.tenderCalcBaseValue,
  746. programID: treeNode.data.programID,
  747. marketUnitFee: treeNode.data.marketUnitFee,
  748. marketTotalFee: treeNode.data.marketTotalFee,
  749. fees: treeNode.data.fees,
  750. isFromDetail:treeNode.data.isFromDetail,
  751. feeRate: treeNode.data.feeRate,
  752. feeRateID: treeNode.data.feeRateID,
  753. contain: treeNode.data.contain,
  754. quantityEXP: treeNode.data.quantityEXP,
  755. summaryFees: treeNode.data.summaryFees,
  756. name:treeNode.data.name,
  757. rationQuantityCoe: treeNode.data.rationQuantityCoe,
  758. quantityCoe: treeNode.data.quantityCoe == null ? {} : treeNode.data.quantityCoe,
  759. targetUnitFee: treeNode.data.targetUnitFee,
  760. targetTotalFee: treeNode.data.targetTotalFee
  761. };
  762. // 定额大类
  763. if (me.isRationCategory(treeNode)) {
  764. data.isSubcontract = treeNode.data.isSubcontract;
  765. data.evaluationProject = treeNode.data.evaluationProject;
  766. //定额类型的工料机做特殊处理
  767. if(me.isGljRation(treeNode)){
  768. data.code = treeNode.data.code;
  769. data.projectGLJID = treeNode.data.projectGLJID;
  770. delete data.marketUnitFee;
  771. };
  772. };
  773. // 优化掉 undefined 属性
  774. data = JSON.parse(JSON.stringify(data));
  775. return data;
  776. },
  777. uiNodeQty: function (treeNode){
  778. return parseFloatPlus(treeNode.data.quantity).toDecimal(decimalObj.decimal("quantity", treeNode));
  779. },
  780. // 在项目工料机里检查该工料机是否参与调价
  781. isTenderProjectGLJ: function (glj){
  782. let projGLJ = this.getProjectGLJ(glj);
  783. return !(projGLJ && projGLJ.is_adjust_price == 1);
  784. },
  785. // 取单价调价系数
  786. tenderCoe_GLJPrice: function (){
  787. let coe = 1;
  788. if (projectObj.project.property.tenderSetting && projectObj.project.property.tenderSetting.gljPriceTenderCoe){
  789. coe = projectObj.project.property.tenderSetting.gljPriceTenderCoe;
  790. if (coe == '0') coe = 1; // 这里加个保护
  791. };
  792. return coe;
  793. },
  794. tenderCoe_NodeQty: function (treeNode){
  795. let coe = 1;
  796. if (treeNode.data.rationQuantityCoe){
  797. coe = treeNode.data.rationQuantityCoe;
  798. if (coe == '0') coe = 1; // 这里加个保护
  799. };
  800. return coe;
  801. },
  802. tenderCoe_GLJQty: function (rationData, glj){
  803. let coe = 1;
  804. if (!rationData.quantityCoe) return coe;
  805. if (!calcTools.isTenderProjectGLJ(glj)) return coe;
  806. if (gljType.LABOUR == glj.type){
  807. if (rationData.quantityCoe.labour)
  808. coe = rationData.quantityCoe.labour;
  809. }
  810. else if (baseMaterialTypes.indexOf(glj.type)){
  811. if (rationData.quantityCoe.material)
  812. coe = rationData.quantityCoe.material;
  813. }
  814. else if (baseMachineTypes.indexOf(glj.type)){
  815. if (rationData.quantityCoe.machine)
  816. coe = rationData.quantityCoe.machine;
  817. }
  818. else if (gljType.MAIN_MATERIAL == glj.type){
  819. if (rationData.quantityCoe.main)
  820. coe = rationData.quantityCoe.main;
  821. }
  822. else if (gljType.EQUIPMENT == glj.type){
  823. if (rationData.quantityCoe.equipment)
  824. coe = rationData.quantityCoe.equipment;
  825. };
  826. if (coe == '0') coe = 1; // 这里加个保护
  827. return coe;
  828. },
  829. uiNodeTenderQty: function (treeNode){
  830. return this.calcNodeTenderQty(treeNode);
  831. },
  832. calcNodeTenderQty: function (treeNode){
  833. if (this.isBill(treeNode)) // 清单只有一个工程量,没有调整后工程量。
  834. return this.uiNodeQty(treeNode)
  835. else{
  836. let qCoe = 1;
  837. /* 量价、工料机类型的定额,在反向调价之调整人材机消耗量系数时,因为他们没有工料机可调,调价结果没变,影响汇总后的父结点金额。
  838. 所以要特殊处理:此种情况下仍然要调量价的消耗量,即还是要像"子目工程量调整系数"方式那样调,但系数又不能在"子目工程量调整系数"
  839. 中显示出来(明明是调工料机,你却调到树结点上,这比较搞笑啊),所以可以改变tenderQuantity达到同样的效果以瞒天过海。所以在取系数时,
  840. 无论什么系数,只要能取到就算正确。
  841. 2020-04-05 注: 以上是老黄历。新思路是量价只作为定额调整,不作为工料机调整。两种反调模式下,统一都只调子目消耗量系数。
  842. 为避免歧义,量价的工料机调整系数不允许输入。
  843. */
  844. // if (calcTools.isVP_or_GLJR(treeNode)){
  845. // if (treeNode.data.rationQuantityCoe)
  846. // qCoe = treeNode.data.rationQuantityCoe
  847. // else if (treeNode.data.quantityCoe && treeNode.data.quantityCoe.labour)
  848. // qCoe = treeNode.data.quantityCoe.labour;
  849. // }
  850. // else {
  851. if (treeNode.data.rationQuantityCoe)
  852. qCoe = treeNode.data.rationQuantityCoe;
  853. // };
  854. if (qCoe == '0' || qCoe == 0) qCoe = 1;
  855. treeNode.data.tenderQuantity = (this.uiNodeQty(treeNode) * qCoe).toDecimal(decimalObj.decimal("quantity", treeNode));
  856. return treeNode.data.tenderQuantity;
  857. }
  858. },
  859. calcGLJTenderQty: function (treeNode, glj){
  860. if (treeNode.data.quantityCoe == undefined){
  861. glj.tenderQuantity = glj.quantity;
  862. }
  863. else{
  864. let coe = 1;
  865. if (this.isTenderProjectGLJ(glj))
  866. coe = this.tenderCoe_GLJQty(treeNode, glj);
  867. glj.tenderQuantity = (glj.quantity * coe).toDecimal(decimalObj.glj.quantity);
  868. }
  869. return glj.tenderQuantity;
  870. },
  871. calcGLJTenderPrice: function (glj) {
  872. let projGLJ = calcTools.getProjectGLJ(glj);
  873. if (projGLJ == null){ // 量价定额虚拟出来的工料机,在项目工料机中查不到。
  874. glj.tenderPrice = projectObj.project.projectGLJ.getTenderMarketPrice(projGLJ);
  875. }else{
  876. let pCoe = 1;
  877. // 先从项目工料机里检查该工料机是否参与调价
  878. if (projGLJ.is_adjust_price != 1) pCoe = this.tenderCoe_GLJPrice();
  879. glj.tenderPrice = (glj.marketPrice * pCoe).toDecimal(decimalObj.glj.unitPrice);
  880. };
  881. return glj.tenderPrice;
  882. },
  883. // 界面显示的工料机价格,包括定额价、市场价等。参数 price 传入一个普通的价格数值即可。
  884. uiGLJPrice: function (price, glj){
  885. if (price){
  886. let projGLJ = glj ? calcTools.getProjectGLJ(glj) : null;
  887. let d = (projGLJ && projGLJ.ratio_data.length > 0) ? decimalObj.glj.unitPriceHasMix : decimalObj.glj.unitPrice;
  888. return parseFloat(price).toDecimal(d);
  889. }
  890. else return 0;
  891. },
  892. // 界面显示的工料机数量。参数 quantity 传入一个普通的数量数值即可。
  893. uiGLJQty: function (quantity){
  894. if (quantity)
  895. return parseFloat(quantity).toDecimal(decimalObj.glj.quantity)
  896. else return 0;
  897. },
  898. hasTargetTotalFee: function (treeNode){ // targetTotalFee 有时为字符串“0”,此种情况会执行if 条件引起逻辑错误。所以这里封闭成方法直接调用。
  899. return (treeNode.data.targetTotalFee && parseFloat(treeNode.data.targetTotalFee));
  900. },
  901. hasQuantity: function (treeNode){
  902. return (treeNode.data.quantity && parseFloat(treeNode.data.quantity));
  903. },
  904. getRationsByProjectGLJ(PGLJID, tree = projectObj.project.mainTree){
  905. let rationIDs = [];
  906. let RGs = projectObj.project.ration_glj.datas;
  907. let PGLJIDs = [];
  908. if(Array.isArray(PGLJID)){//为了提高效率,改成支持传入数组
  909. PGLJIDs = PGLJID
  910. }else {
  911. PGLJIDs = [PGLJID];
  912. }
  913. for (let rg of RGs){
  914. if (PGLJIDs.indexOf(rg.projectGLJID) !== -1){
  915. rationIDs.push(rg.rationID);
  916. }
  917. };
  918. let rationNodes = [];
  919. let nodes = tree.nodes;
  920. for (let rID of rationIDs){
  921. rationNodes.push(nodes['id_' + rID]);
  922. };
  923. // 工料机形式的定额
  924. let items = tree.items;
  925. for (let item of items){
  926. if (PGLJIDs.indexOf(item.data.projectGLJID) !== -1)
  927. rationNodes.push(item);
  928. };
  929. return rationNodes;
  930. },
  931. getNodesByProgramID(programID, tree = projectObj.project.mainTree){
  932. let discreteNodes = [];
  933. let nodes = tree.items;
  934. for (let node of nodes){
  935. if (node.data.programID == programID)
  936. discreteNodes.push(node);
  937. };
  938. return discreteNodes;
  939. },
  940. getProjectGLJ(glj){
  941. if (glj.projectGLJID){
  942. return projectObj.project.projectGLJ.getDataByID(glj.projectGLJID);
  943. }
  944. else return null;
  945. },
  946. labourDays(node, isTender){
  947. if (!node.data.gljList) return 0;
  948. let rst = 0;
  949. calcTools.uiNodeQty(node);
  950. for (let glj of node.data.gljList) {
  951. if (glj.type == gljType.LABOUR) {
  952. let gljQ = isTender ? glj.tenderQuantity : glj.quantity;
  953. gljQ = gljQ.toDecimal(decimalObj.glj.quantity);
  954. rst = (rst + gljQ).toDecimal(decimalObj.process);
  955. }
  956. };
  957. return rst.toDecimal(decimalObj.glj.quantity);
  958. },
  959. getProjectFeatureProperty(propertyKey){
  960. for (let o of projectObj.project.property.projectFeature){
  961. if (o.key == propertyKey){
  962. return o.value;
  963. }
  964. };
  965. },
  966. getFeeRateByNode(node){
  967. let decimal = getDecimal("feeRate");
  968. if(node.data.feeRateID) {
  969. let r = projectObj.project.FeeRate.getFeeRateByID(node.data.feeRateID);
  970. if (r) return scMathUtil.roundForObj(r.rate, decimal);
  971. };
  972. if (node.data.feeRate || node.data.feeRate === 0 || node.data.feeRate === '0')
  973. return scMathUtil.roundForObj(node.data.feeRate,decimal);
  974. return 100;
  975. },
  976. isEmptyObject(obj){
  977. let arr = Object.keys(obj);
  978. return arr.length == 0;
  979. },
  980. // 清单价格是否大于最高限价
  981. unitFeeGTMaxPrice: function (node, feeField) {
  982. if (!this.isBill(node)) {
  983. return false;
  984. }
  985. const totalFee = this.getFee(node, feeField);
  986. const maxPrice = node.data.maxPrice;
  987. // 最高限价有值才对比
  988. if (!commonUtil.isNumber(maxPrice)) {
  989. return false;
  990. }
  991. return totalFee > +maxPrice;
  992. },
  993. // 清单价格是否小于最低限价
  994. unitFeeLTMinPrice: function (node, feeField) {
  995. if (!this.isBill(node)) {
  996. return false;
  997. }
  998. const totalFee = this.getFee(node, feeField);
  999. const minPrice = node.data.minPrice;
  1000. // 最低限价有值才对比
  1001. if (!commonUtil.isNumber(minPrice)) {
  1002. return false;
  1003. }
  1004. return totalFee < +minPrice;
  1005. },
  1006. getTenderTypeStr: function () {
  1007. let tenderSetting = projectObj.project.property.tenderSetting;
  1008. let ct = tenderSetting && tenderSetting.calcPriceOption ? tenderSetting.calcPriceOption : "priceBase_RCJ";
  1009. if (ct == 'priceBase') ct = 'priceBase_RCJ'; // 兼容旧项目
  1010. return ct;
  1011. },
  1012. getTenderType: function () {
  1013. let rst;
  1014. let sOption = calcTools.getTenderTypeStr();
  1015. if (sOption == 'coeBase')
  1016. rst = tenderTypes.ttCalc
  1017. else if (sOption == 'priceBase_RCJ')
  1018. rst = tenderTypes.ttReverseGLJ
  1019. else if (sOption == 'priceBase_ZM')
  1020. rst = tenderTypes.ttReverseRation;
  1021. return rst;
  1022. },
  1023. // 取树结点的调价系数。
  1024. getCoe: function (node, tender) {
  1025. if (tender == tenderTypes.ttReverseGLJ)
  1026. return calcTools.isVP_or_GLJR(node) ? node.data.rationQuantityCoe : (node.data.quantityCoe ? node.data.quantityCoe.labour : 0)
  1027. else if (tender == tenderTypes.ttReverseRation)
  1028. return node.data.rationQuantityCoe;
  1029. },
  1030. getUsedTemplates: function(){
  1031. let arr = [];
  1032. let rations = projectObj.project.Ration.datas;
  1033. for (const r of rations) {
  1034. let tpl = projectObj.project.calcProgram.compiledTemplates[r.programID];
  1035. if (tpl && !arr.includes(tpl))
  1036. arr.push(tpl);
  1037. };
  1038. arr.sort(function (x, y) {return x.ID - y.ID});
  1039. return arr;
  1040. },
  1041. getRationsByProgramID: function(programID){
  1042. let rations = [];
  1043. for (const r of projectObj.project.Ration.datas) {
  1044. if (r.programID == programID)
  1045. rations.push(r);
  1046. };
  1047. return rations;
  1048. }
  1049. };
  1050. let rationCalcBases = {
  1051. '定额基价人工费': function (node, isTender) {
  1052. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice, isTender);
  1053. },
  1054. '定额基价材料费': function (node, isTender) {
  1055. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice, isTender);
  1056. },
  1057. '定额基价机械费': function (node, isTender) {
  1058. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice, isTender);
  1059. },
  1060. '定额基价机上人工费': function (node, isTender) {
  1061. return calcTools.machineDetailFee(node, node.data.gljList, [], gljType.MACHINE_LABOUR, isTender);
  1062. },
  1063. '人工费价差': function (node, isTender) {
  1064. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptDiffPrice, isTender);
  1065. },
  1066. '材料费价差': function (node, isTender) {
  1067. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptDiffPrice, isTender);
  1068. },
  1069. '机械费价差': function (node, isTender) {
  1070. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptDiffPrice, isTender);
  1071. },
  1072. '主材费价差': function (node, isTender) {
  1073. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptDiffPrice, isTender);
  1074. },
  1075. '设备费价差': function (node, isTender) {
  1076. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptDiffPrice, isTender);
  1077. },
  1078. '主材费': function (node, isTender) {
  1079. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptBasePrice, isTender);
  1080. },
  1081. '设备费': function (node, isTender) {
  1082. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptBasePrice, isTender);
  1083. },
  1084. '人工工日': function (node, isTender) {
  1085. return calcTools.labourDays(node, isTender);
  1086. },
  1087. '甲供定额基价人工费': function (node, isTender) {
  1088. return calcTools.partASupplyFee(node, '甲供定额基价人工费', isTender, true);
  1089. },
  1090. '甲供定额基价材料费': function (node, isTender) {
  1091. return calcTools.partASupplyFee(node, '甲供定额基价材料费', isTender, true);
  1092. },
  1093. '甲供定额基价机械费': function (node, isTender) {
  1094. return calcTools.partASupplyFee(node, '甲供定额基价机械费', isTender, true);
  1095. },
  1096. '甲供主材费': function (node, isTender) {
  1097. return calcTools.partASupplyFee(node, '甲供主材费', isTender, false);
  1098. },
  1099. '甲供设备费': function (node, isTender) {
  1100. return calcTools.partASupplyFee(node, '甲供设备费', isTender, false);
  1101. },
  1102. '甲定定额基价人工费': function (node, isTender) {
  1103. return calcTools.partASupplyFee(node, '甲定定额基价人工费', isTender, true);
  1104. },
  1105. '甲定定额基价材料费': function (node, isTender) {
  1106. return calcTools.partASupplyFee(node, '甲定定额基价材料费', isTender, true);
  1107. },
  1108. '甲定定额基价机械费': function (node, isTender) {
  1109. return calcTools.partASupplyFee(node, '甲定定额基价机械费', isTender, true);
  1110. },
  1111. '甲定主材费': function (node, isTender) {
  1112. return calcTools.partASupplyFee(node, '甲定主材费', isTender, false);
  1113. },
  1114. '甲定设备费': function (node, isTender) {
  1115. return calcTools.partASupplyFee(node, '甲定设备费', isTender, false);
  1116. },
  1117. '暂估材料费': function (node, isTender) {
  1118. return calcTools.estimateFee(node, true, isTender);
  1119. },
  1120. '分包定额基价人工费': function (node, isTender) {
  1121. if (node.data.isSubcontract)
  1122. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice, isTender)
  1123. else
  1124. return 0;
  1125. },
  1126. '分包定额基价材料费': function (node, isTender) {
  1127. if (node.data.isSubcontract)
  1128. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice, isTender)
  1129. else
  1130. return 0;
  1131. },
  1132. '分包定额基价机械费': function (node, isTender) {
  1133. if (node.data.isSubcontract)
  1134. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice, isTender)
  1135. else
  1136. return 0;
  1137. },
  1138. '分包主材费': function (node, isTender) {
  1139. if (node.data.isSubcontract)
  1140. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptBasePrice, isTender)
  1141. else
  1142. return 0;
  1143. },
  1144. '分包设备费': function (node, isTender) {
  1145. if (node.data.isSubcontract)
  1146. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptBasePrice, isTender)
  1147. else
  1148. return 0;
  1149. },
  1150. '分包人工工日': function (node, isTender) {
  1151. if (node.data.isSubcontract)
  1152. return calcTools.labourDays(node, isTender)
  1153. else
  1154. return 0;
  1155. }
  1156. };
  1157. let analyzer = {
  1158. error: '',
  1159. standard: function(expr){
  1160. let str = expr;
  1161. str = str.replace(/\s/g, ""); // 去空格、去中文空格
  1162. str = str.replace(/(/g, "("); // 中文括号"("换成英文括号"("
  1163. str = str.replace(/)/g, ")"); // 中文括号")"换成英文括号")"
  1164. str = str.replace(/f/g, "F"); // f换成F
  1165. str = str.replace(/l/g, "L"); // l换成L
  1166. return str;
  1167. },
  1168. getFArr: function (expr) {
  1169. let pattF = new RegExp(/F\d+/gi);
  1170. let arrF = expr.match(pattF);
  1171. return arrF ? arrF : [];
  1172. },
  1173. getAtIDArr: function (expr) { // ['@1','@2']
  1174. let patt = new RegExp(/@\d+/gi);
  1175. let arr = expr.match(patt);
  1176. return arr ? arr : [];
  1177. },
  1178. getBaseArr: function (expr) {
  1179. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  1180. let arrBase = expr.match(pattBase);
  1181. return arrBase ? arrBase : [];
  1182. },
  1183. getID: function (FName, template) { // F13 → 4
  1184. let idx = FName.slice(1) - 1;
  1185. let id = template.calcItems[idx] ? template.calcItems[idx].ID : null;
  1186. return id;
  1187. },
  1188. getFName: function (atID, template) { // @3 → F7
  1189. for (var i = 0; i < template.calcItems.length; i++) {
  1190. if (template.calcItems[i].ID == atID.slice(1)) {
  1191. return 'F'+ (i + 1);
  1192. }
  1193. }
  1194. return null;
  1195. },
  1196. getItem: function (FName, template){ // F3 → calcItems[2] → {Object}
  1197. let idx = FName.slice(1) - 1;
  1198. return template.calcItems[idx];
  1199. },
  1200. isCycleCalc: function (expression, ID, template) { // 这里判断expression,是ID引用: @5+@6
  1201. let atID = `@${ID}`;
  1202. // 避免部分匹配,如:@10匹配@1
  1203. let idx = expression.indexOf(atID);
  1204. if (idx >= 0){
  1205. let nextPos = idx + atID.length;
  1206. if (nextPos >= expression.length)
  1207. return true;
  1208. let char = expression.charAt(nextPos);
  1209. if (!char.isNumberStr())
  1210. return true;
  1211. };
  1212. let atIDs = analyzer.getAtIDArr(expression);
  1213. for (let atID of atIDs) {
  1214. let item = template.compiledCalcItems[atID.slice(1)];
  1215. if (analyzer.isCycleCalc(item.expression, ID, template))
  1216. return true;
  1217. }
  1218. return false;
  1219. },
  1220. isLegal: function (dispExpr, itemID, template) { // 检测包括:无效字符、基数是否中括号、基数是否定义、行引用、循环计算
  1221. function testValue(expr){
  1222. try {
  1223. expr = expr.replace(/\[[\u4E00-\u9FA5]+\]/gi, '0');
  1224. expr = expr.replace(/@\d+/gi, '0');
  1225. expr = expr.replace(/L/gi, '0');
  1226. expr = expr.replace(/%/gi, '*0.01');
  1227. if (expr.includes('00'))
  1228. return false;
  1229. eval(expr);
  1230. return true;
  1231. }
  1232. catch (err) {
  1233. return false;
  1234. }
  1235. };
  1236. let me = analyzer;
  1237. let expr = me.standard(dispExpr);
  1238. let invalidChars = /[^0-9\u4e00-\u9fa5\+\-\*\/\(\)\.\[\]FL%]/g;
  1239. if (invalidChars.test(expr)){
  1240. analyzer.error = `表达式中含有${hintBox.font('无效字符')}!`;
  1241. // hintBox.infoBox('错误提示',`表达式中含有${hintBox.font('无效字符')}!`,1);
  1242. return false;
  1243. };
  1244. let i = expr.search(/\df/ig); // 4F3
  1245. let j = expr.search(/\d\[/ig); // 4[定额基价人工费]
  1246. if (i > -1 || j > -1){
  1247. analyzer.error = `表达式中${hintBox.font('缺少运算符')}!`;
  1248. // hintBox.infoBox('错误提示', `表达式中${hintBox.font('缺少运算符')}!`, 1);
  1249. return false;
  1250. }
  1251. let pattCn = new RegExp(/[\u4E00-\u9FA5]+/gi);
  1252. let arrCn = expr.match(pattCn);
  1253. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  1254. let arrBase = expr.match(pattBase);
  1255. if (arrCn && !arrBase){
  1256. analyzer.error = `定额基数必须用中括号${hintBox.font('[]')}括起来!`;
  1257. // hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1258. return false;
  1259. };
  1260. if (arrCn && arrBase && (arrCn.length != arrBase.length)){
  1261. for (let cn of arrCn){
  1262. let tempBase = `[${cn}]`;
  1263. if (!arrBase.includes(tempBase)){
  1264. analyzer.error = `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`;
  1265. // hintBox.infoBox('错误提示', `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1266. return false;
  1267. }
  1268. };
  1269. // 这里要加一个保险。因为上面的 for 循环在“主材费 + [主材费]” 情况下有Bug
  1270. analyzer.error =`定额基数必须用中括号${hintBox.font('[]')}括起来!`;
  1271. // hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1272. return false;
  1273. };
  1274. if (arrBase){
  1275. for (let base of arrBase){
  1276. let baseName = base.slice(1, -1);
  1277. if (!rationCalcBases[baseName]){
  1278. analyzer.error = `定额基数${hintBox.font('[' +baseName + ']')}未定义!`;
  1279. // hintBox.infoBox('错误提示', `定额基数${hintBox.font('[' +baseName + ']')}未定义!`, 1);
  1280. return false;
  1281. }
  1282. };
  1283. };
  1284. let arrF = me.getFArr(expr);
  1285. for (let F of arrF){
  1286. let num = F.slice(1);
  1287. if (num > template.calcItems.length){
  1288. let s = hintBox.font('F'+num);
  1289. analyzer.error = `表达式中 “${hintBox.font(s)}” 行号引用错误!`;
  1290. // hintBox.infoBox('错误提示', `表达式中 “${hintBox.font(s)}” 行号引用错误!`, 1);
  1291. return false;
  1292. };
  1293. };
  1294. let expression = me.getExpression(expr, template);
  1295. if (me.isCycleCalc(expression, itemID, template)){
  1296. analyzer.error = `表达式中有${hintBox.font('循环计算')}!`;
  1297. // hintBox.infoBox('错误提示', `表达式中有${hintBox.font('循环计算')}!`, 1);
  1298. return false;
  1299. };
  1300. if (!testValue(expression)){
  1301. analyzer.error = `表达式中有${hintBox.font('语法错误')}!`;
  1302. // hintBox.infoBox('错误提示', `表达式中有${hintBox.font('语法错误')}!`, 1);
  1303. return false;
  1304. };
  1305. return true; // 表达式合法
  1306. },
  1307. refIDToLines: function (expression, template) { // @2、@3 → F7、F8
  1308. let rst = expression;
  1309. let atIDArr = analyzer.getAtIDArr(rst);
  1310. let fArr = [];
  1311. for (let atID of atIDArr) {
  1312. let FN = analyzer.getFName(atID, template);
  1313. fArr.push(FN);
  1314. };
  1315. for (let i = 0; i < atIDArr.length; i++) {
  1316. let patt = new RegExp(atIDArr[i]);
  1317. let val = fArr[i];
  1318. rst = rst.replace(patt, val);
  1319. };
  1320. return rst;
  1321. },
  1322. refLineToIDs: function (dispExpr, template) { // F7、F8 → @2、@3
  1323. let rst = analyzer.standard(dispExpr);
  1324. let fArr = analyzer.getFArr(rst);
  1325. let IDArr = [];
  1326. for (let F of fArr) {
  1327. let ID = analyzer.getID(F, template);
  1328. IDArr.push(ID);
  1329. };
  1330. for (let i = 0; i < fArr.length; i++) {
  1331. let patt = new RegExp(fArr[i]);
  1332. let val = `@${IDArr[i]}`;
  1333. rst = rst.replace(patt, val);
  1334. };
  1335. return rst;
  1336. },
  1337. getDispExpr: function (expression, template) {
  1338. return analyzer.refIDToLines(expression, template);
  1339. },
  1340. getExpression: function (dispExpr, template) {
  1341. return analyzer.refLineToIDs(dispExpr, template);
  1342. },
  1343. getDispExprUser: function (dispExpr, labourCoe) { // labourCoe 是 str 类型
  1344. let rst = analyzer.standard(dispExpr);
  1345. rst = rst.replace(/L/g, labourCoe);
  1346. return rst;
  1347. },
  1348. getCompiledExpr: function (expression, labourCoe) { // labourCoe 是 str 类型
  1349. let rst = expression;
  1350. let atIDArr = analyzer.getAtIDArr(rst);
  1351. let IDArr = atIDArr.map(function (atID) {
  1352. return atID.slice(1);
  1353. });
  1354. for (var i = 0; i < atIDArr.length; i++) {
  1355. let patt = new RegExp(atIDArr[i]);
  1356. let val = `$CE.at(${IDArr[i]},false)`;
  1357. rst = rst.replace(patt, val);
  1358. };
  1359. rst = rst.replace(/%/g, "*0.01");
  1360. rst = rst.replace(/\[/g, "$CE.base('");
  1361. rst = rst.replace(/\]/g, "',false)");
  1362. rst = rst.replace(/L/g, labourCoe);
  1363. return rst;
  1364. },
  1365. getCompiledTenderExpr: function (compiledExpr) {
  1366. let rst = compiledExpr.replace(/false/g, "true");
  1367. return rst;
  1368. },
  1369. getStatement: function (expression, template) {
  1370. let rst = expression;
  1371. let atIDArr = analyzer.getAtIDArr(rst);
  1372. let IDArr = atIDArr.map(function (atID) {
  1373. return atID.slice(1);
  1374. });
  1375. for (var i = 0; i < atIDArr.length; i++) {
  1376. let patt = new RegExp(atIDArr[i]);
  1377. let val = projectObj.project.calcProgram.compiledTemplates[template.ID].compiledCalcItems[IDArr[i]].name;
  1378. rst = rst.replace(patt, val);
  1379. };
  1380. rst = rst.replace(/\[/g, "");
  1381. rst = rst.replace(/\]/g, "");
  1382. rst = rst.replace(/L/g, '人工系数');
  1383. return rst;
  1384. },
  1385. calcItemMaxID: function(template){
  1386. let MaxID = 0;
  1387. for (let item of template.calcItems){
  1388. if (item.ID > MaxID)
  1389. MaxID = item.ID;
  1390. };
  1391. return MaxID;
  1392. },
  1393. calcItemIsUsed: function(template, calcItem){
  1394. let atID = '@' + calcItem.ID;
  1395. for (var i = 0; i < template.calcItems.length; i++) {
  1396. let item = template.calcItems[i];
  1397. let atIDArr = analyzer.getAtIDArr(item.expression);
  1398. if (atIDArr.indexOf(atID) >= 0){
  1399. calcItem.tempUsed = i;
  1400. return true;
  1401. }
  1402. }
  1403. return false;
  1404. },
  1405. refreshUsedCalcItemsStatement: function(template, calcItem){
  1406. let atID = '@' + calcItem.ID;
  1407. for (var i = 0; i < template.calcItems.length; i++) {
  1408. let item = template.calcItems[i];
  1409. let atIDArr = analyzer.getAtIDArr(item.expression);
  1410. if (atIDArr.indexOf(atID) >= 0){
  1411. item.statement = analyzer.getStatement(item.expression, template);
  1412. };
  1413. }
  1414. },
  1415. calcItemLabourCoe: function(calcItem){
  1416. let lc = 0;
  1417. if (calcItem.labourCoeID)
  1418. lc = projectObj.project.calcProgram.compiledLabourCoes[calcItem.labourCoeID].coe.toString();
  1419. return lc;
  1420. },
  1421. templateRefresh: function(template){
  1422. for (let item of template){
  1423. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1424. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, me.calcItemLabourCoe(item));
  1425. };
  1426. },
  1427. templateMaxID: function(){
  1428. let ts = projectObj.project.calcProgram.templates;
  1429. let MaxID = 0;
  1430. for (let t of ts){
  1431. if (t.ID > MaxID)
  1432. MaxID = t.ID;
  1433. };
  1434. return MaxID;
  1435. },
  1436. templateNewName: function (name) {
  1437. let i = 1;
  1438. while (projectObj.project.calcProgram.compiledTemplateMaps[name + i]) {
  1439. i++;
  1440. };
  1441. return name + i;
  1442. },
  1443. templateNameIsExist: function (name) {
  1444. if (projectObj.project.calcProgram.compiledTemplateMaps[name])
  1445. return true
  1446. else return false;
  1447. },
  1448. templateIsUsed: function (ID, tree = projectObj.project.mainTree){
  1449. let nodes = tree.items;
  1450. for (let node of nodes){
  1451. if (node.data && node.data.programID && node.data.programID == ID) {
  1452. return true;
  1453. }
  1454. };
  1455. return false;
  1456. },
  1457. fieldNameIsUsed: function(template, fieldName){
  1458. let fieldNameEn = projectObj.project.calcProgram.compiledFeeTypeMaps[fieldName];
  1459. for (var i = 0; i < template.calcItems.length; i++) {
  1460. if (template.calcItems[i].fieldName == fieldNameEn){
  1461. template.fieldNameTempUsed = i;
  1462. return true;
  1463. }
  1464. }
  1465. return false;
  1466. }
  1467. };
  1468. let executeObj = {
  1469. treeNode: null,
  1470. template: null,
  1471. tempCalcItem: null,
  1472. at: function(ID, isTender) {
  1473. let item = executeObj.template.compiledCalcItems[ID];
  1474. let rst = isTender ? item.tenderUnitFee : item.unitFee;
  1475. rst = parseFloat(rst);
  1476. return rst;
  1477. },
  1478. base: function(baseName, isTender) {
  1479. let me = executeObj;
  1480. // 量价、工料机形式的定额, 要把自己的市场单价用于计算程序中的基数。
  1481. if (calcTools.isVP_or_GLJR(me.treeNode))
  1482. return calcTools.marketPriceToBase(me.treeNode, baseName, isTender)
  1483. else{
  1484. if (!rationCalcBases[baseName]){
  1485. hintBox.infoBox('系统提示', '定额基数“' + baseName + '”未定义,计算错误。 (模板 ' + me.template.ID + ',规则 ' + me.tempCalcItem.ID +')', 1);
  1486. return 0;
  1487. }
  1488. else
  1489. return rationCalcBases[baseName](me.treeNode, isTender);
  1490. }
  1491. },
  1492. HJ: function () {
  1493. let me = this;
  1494. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  1495. let q = calcTools.uiNodeQty(me.treeNode) ? calcTools.uiNodeQty(me.treeNode) : 1;
  1496. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  1497. return u;
  1498. }
  1499. };
  1500. // 概算版本,定额计算不走计算程序,只用以下五大项简单的相加
  1501. let budgetFields = ['rationLabour', 'rationMaterial', 'rationMachine', 'rationMainMaterial', 'rationEquipment', 'common'];
  1502. let budgetBaseNames = ['定额人工费', '定额材料费', '定额施工机具使用费', '定额主材费', '定额设备费'];
  1503. class CalcProgram {
  1504. constructor(project){
  1505. let me = this;
  1506. me.project = project;
  1507. me.datas = [];
  1508. me.rationMap = null;//定额 - 工料机映射临时变量
  1509. me.pgljMap = null;
  1510. project.registerModule(ModuleNames.calc_program, me);
  1511. };
  1512. // 兼容Project框架方法
  1513. getSourceType () {
  1514. return ModuleNames.calc_program;
  1515. };
  1516. // 兼容Project框架方法
  1517. // isInit:是否初始化,进入单位工程为true,导出接口为false,不需要存储费率临时数据
  1518. loadData (datas, isInit) {
  1519. this.datas = datas;
  1520. this.compileAllTemps(isInit);
  1521. };
  1522. // 兼容Project框架方法
  1523. doAfterUpdate (err, data) {
  1524. if(!err){
  1525. $.bootstrapLoading.end();
  1526. }
  1527. };
  1528. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  1529. compileAllTemps(isInit = false){
  1530. let me = this;
  1531. me.compiledFeeRates = {};
  1532. me.compiledLabourCoes = {};
  1533. me.compiledTemplates = {};
  1534. me.compiledTemplateMaps = {};
  1535. me.compiledTemplateNames = [];
  1536. me.compiledFeeTypeMaps = {};
  1537. me.compiledFeeTypeNames = [];
  1538. me.compiledCalcBases = {};
  1539. me.feeRates = this.project.FeeRate.datas.rates;
  1540. me.labourCoes = this.project.labourCoe.datas.coes;
  1541. me.feeTypes = cpFeeTypes;
  1542. // me.calcBases = rationCalcBase;
  1543. me.templates = this.project.calcProgram.datas.templates;
  1544. // me.templates.push(defaultBillTemplate);
  1545. // 先编译公用的基础数据
  1546. me.compilePublics();
  1547. me.compileTemplateMaps();
  1548. for (let t of me.templates){
  1549. me.compileTemplate(t);
  1550. };
  1551. };
  1552. compilePublics(){
  1553. let me = this;
  1554. for (let rate of me.feeRates) {
  1555. me.compiledFeeRates[rate.ID] = rate;
  1556. }
  1557. for (let coe of me.labourCoes) {
  1558. me.compiledLabourCoes[coe.ID] = coe;
  1559. }
  1560. for (let ft of me.feeTypes) {
  1561. me.compiledFeeTypeMaps[ft.type] = ft.name;
  1562. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  1563. me.compiledFeeTypeNames.push(ft.name);
  1564. }
  1565. /* for (let cb of me.calcBases) {
  1566. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  1567. }*/
  1568. };
  1569. compileTemplateMaps(){
  1570. let me = this;
  1571. function clearObj(obj){
  1572. for (let key in obj) {
  1573. delete obj[key];
  1574. }
  1575. };
  1576. clearObj(me.compiledTemplates);
  1577. clearObj(me.compiledTemplateMaps);
  1578. me.compiledTemplateNames.splice(0, me.compiledTemplateNames.length);
  1579. for (let t of me.templates){
  1580. me.compiledTemplates[t.ID] = t;
  1581. me.compiledTemplateMaps[t.ID] = t.name;
  1582. me.compiledTemplateMaps[t.name] = t.ID;
  1583. me.compiledTemplateNames.push(t.name);
  1584. };
  1585. };
  1586. compileTemplate(template){
  1587. let me = this;
  1588. // me.compiledTemplates[template.ID] = template;
  1589. // me.compiledTemplateMaps[template.ID] = template.name;
  1590. // me.compiledTemplateMaps[template.name] = template.ID;
  1591. // me.compiledTemplateNames.push(template.name);
  1592. template.hasCompiled = false;
  1593. template.errs = [];
  1594. let private_extract_ID = function(str, idx){
  1595. let subStr = str.slice(idx);
  1596. let patt = new RegExp(/@\d+/);
  1597. let atID = subStr.match(patt);
  1598. let ID = atID ? atID[0].slice(1) : null;
  1599. return ID;
  1600. };
  1601. let private_parse_ref = function(item, itemIdx){
  1602. let idx = item.expression.indexOf('@', 0);
  1603. while (idx >= 0) {
  1604. let ID = private_extract_ID(item.expression, idx);
  1605. let len = ID ? ID.toString().length : 0;
  1606. if (len) {
  1607. let subItem = template.compiledCalcItems[ID];
  1608. if (subItem) {
  1609. if (subItem.ID !== item.ID) {
  1610. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  1611. } else {
  1612. template.errs.push("循环引用ID: " + ID);
  1613. }
  1614. } else {
  1615. template.errs.push("找不到ID: " + ID);
  1616. console.log('找不到ID: ' + ID);
  1617. }
  1618. }
  1619. idx = item.expression.indexOf('@', idx + len + 1);
  1620. }
  1621. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  1622. template.compiledSeq.push(itemIdx);
  1623. }
  1624. };
  1625. let private_compile_items = function() {
  1626. for (let idx of template.compiledSeq) {
  1627. let item = template.calcItems[idx];
  1628. let lc = analyzer.calcItemLabourCoe(item);
  1629. // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  1630. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, lc);
  1631. if (item.expression == 'HJ')
  1632. item.compiledExpr = '$CE.HJ()'
  1633. else
  1634. item.compiledExpr = analyzer.getCompiledExpr(item.expression, lc);
  1635. if (item.feeRateID) {
  1636. let orgFeeRate = item.feeRate;
  1637. let cfr = me.compiledFeeRates[item.feeRateID];
  1638. item.feeRate = cfr ? cfr.rate : 100;
  1639. // if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  1640. // me.saveForReports.push({templatesID: template.ID, calcItem: item});
  1641. // }
  1642. };
  1643. // 字段名映射
  1644. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  1645. }
  1646. };
  1647. if (template && template.calcItems && template.calcItems.length > 0) {
  1648. template.compiledSeq = [];
  1649. template.compiledCalcItems = {};
  1650. for (let i = 0; i < template.calcItems.length; i++) {
  1651. let item = template.calcItems[i];
  1652. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1653. template.compiledCalcItems[item.ID] = item;
  1654. template.compiledCalcItems[item.ID + "_idx"] = i;
  1655. }
  1656. for (let i = 0; i < template.calcItems.length; i++) {
  1657. let item = template.calcItems[i];
  1658. if (template.compiledSeq.indexOf(i) < 0) {
  1659. private_parse_ref(item, i);
  1660. }
  1661. }
  1662. if (template.errs.length == 0) {
  1663. private_compile_items();
  1664. template.hasCompiled = true;
  1665. } else {
  1666. console.log('errors: ' + template.errs.toString());
  1667. }
  1668. };
  1669. };
  1670. // 删掉多余的费用。例如:①切换取费类别 ②从其它计算方式(有很多费)切换到公式计算方式(只需要common费),多出来的费要删除。
  1671. // 如果指定了保留字段,则按用户指定的来。如果没指定保留字段,则按默认的来:总造价清单只留common, estimate两个费用类别。其它公式清单只留common。
  1672. deleteUselessFees(treeNode, fieldNameArr){
  1673. if (!(treeNode.data.fees && treeNode.data.fees.length > 0)) return;
  1674. let keeps = fieldNameArr ? fieldNameArr : [];
  1675. // 这两个默认是要保留的
  1676. if (!keeps.includes('common')) keeps.push('common');
  1677. if (!keeps.includes('estimate')) keeps.push('estimate');
  1678. for (let i = 0; i < treeNode.data.fees.length; i++) {
  1679. if (!keeps.includes(treeNode.data.fees[i].fieldName)) {
  1680. delete treeNode.data.feesIndex[treeNode.data.fees[i].fieldName];
  1681. treeNode.data.fees.splice(i, 1);
  1682. treeNode.changed = true;
  1683. }
  1684. };
  1685. };
  1686. // 不能直接删除该属性,否则无法冲掉库中已存储的值。下同。
  1687. deleteProperties (treeNode, propNamesArr){
  1688. for (let pn of propNamesArr){
  1689. if (treeNode.data[pn]){
  1690. treeNode.data[pn] = null;
  1691. treeNode.changed = true;
  1692. }
  1693. };
  1694. };
  1695. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  1696. innerCalc(treeNode, changedArr, tenderType){
  1697. if (treeNode.sourceType === ModuleNames.ration_glj) return; // 仅用作树节点显示的工料机不能参与计算。
  1698. let me = this;
  1699. //设置定额工料机映射表
  1700. me.setRationMap();
  1701. treeNode.calcType = calcTools.getCalcType(treeNode);
  1702. // 父清单汇总子清单的费用类别
  1703. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees)
  1704. me.innerCalcBill(treeNode, 2)
  1705. // 叶子清单汇总定额的费用类别
  1706. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees)
  1707. me.innerCalcBill(treeNode, 1)
  1708. // 叶子清单:公式计算
  1709. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue)
  1710. me.innerCalcBillExpr(treeNode)
  1711. // 叶子清单:手工修改单价或金额(无定额、无公式计算,什么都没有时)。
  1712. else if (treeNode.calcType == treeNodeCalcType.ctNull)
  1713. me.innerCalcBillCustom(treeNode)
  1714. // 定额:计算程序
  1715. else
  1716. me.innerCalcRation2(treeNode, tenderType);
  1717. if (!calcTools.isTotalCostBill(treeNode)) // 已在上面的分支中计算过
  1718. calcTools.estimateFee(treeNode);
  1719. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  1720. };
  1721. // 清单部分抽取出来,供分摊清单公用。commonCalcType:1 叶子清单汇总定额的费用类别; 2 父清单汇总子清单的费用类别。3: 分摊:叶子清单汇总定额的费用类别。
  1722. innerCalcBill(treeNode, commonCalcType, tender = tenderTypes.ttCalc){
  1723. let me = this;
  1724. treeNode.data.programID = null;
  1725. calcTools.initFees(treeNode);
  1726. let nodes = [];
  1727. if (commonCalcType == 1){
  1728. calcTools.getGLJList(treeNode, true);
  1729. nodes = me.project.Ration.getRationNodes(treeNode);
  1730. }
  1731. else if (commonCalcType == 2){ // 固定清单 "材料(工程设备)暂估价" 比较特殊,不进行父项汇总(需求是这么要求的)
  1732. // nodes = treeNode.children
  1733. nodes = me.project.Bills.getGatherNodes(treeNode);
  1734. }
  1735. else if (commonCalcType == 3)
  1736. nodes = treeNode.children;
  1737. function isBaseFeeType(type){
  1738. // return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  1739. return budgetFields.indexOf(type) > -1;
  1740. };
  1741. let nQ = calcTools.uiNodeQty(treeNode);
  1742. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  1743. let bq = nQ ? nQ : 1;
  1744. let btq = nTQ ? nTQ : 1;
  1745. let rst = [];
  1746. for (let ft of cpFeeTypes) {
  1747. let ftObj = {};
  1748. ftObj.fieldName = ft.type;
  1749. ftObj.name = ft.name;
  1750. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  1751. if (commonCalcType == 2){
  1752. for (let node of nodes) {
  1753. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1754. btf = (btf + parseFloatPlus(node.data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  1755. bttf = (bttf + parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  1756. };
  1757. };
  1758. }
  1759. else if ((commonCalcType == 1) || (commonCalcType == 3)){
  1760. if (treeNode.data.lockUnitPrice == true){ // 清单单价锁定
  1761. if (treeNode.data.feesIndex && treeNode.data.feesIndex[ft.type]) {
  1762. buf = parseFloatPlus(treeNode.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  1763. btuf = parseFloatPlus(treeNode.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  1764. btf = (bq * buf).toDecimal(decimalObj.bills.totalPrice);
  1765. bttf = (btq * btuf).toDecimal(decimalObj.bills.totalPrice);
  1766. }
  1767. }
  1768. else{
  1769. let sum_rtf = 0, sum_rttf = 0;
  1770. for (let node of nodes) {
  1771. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  1772. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1773. ruf = parseFloatPlus(node.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  1774. rtuf = parseFloatPlus(node.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  1775. rtf = parseFloatPlus(node.data.feesIndex[ft.type].totalFee).toDecimal(decimalObj.bills.totalPrice);
  1776. rttf = parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee).toDecimal(decimalObj.bills.totalPrice);
  1777. };
  1778. // 取费方式为子目含量,清单行/列的XX单价应 =ROUND( ∑ROUND(定额XX单价*含量,清单单价精度),清单单价精度)
  1779. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  1780. buf = (buf + (ruf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1781. node.data.tenderContaion = (node.data.tenderQuantity / bq).toDecimal(decimalObj.process);
  1782. btuf = (btuf + (rtuf * parseFloatPlus(node.data.tenderContaion)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1783. };
  1784. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  1785. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  1786. };
  1787. if (me.project.property.billsCalcMode == leafBillGetFeeType.rationPriceConverse ||
  1788. me.project.property.billsCalcMode == leafBillGetFeeType.rationPrice) {
  1789. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  1790. btuf = (sum_rttf / btq).toDecimal(decimalObj.process);
  1791. };
  1792. if (isBaseFeeType(ft.type) ||
  1793. (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  1794. btf = sum_rtf;
  1795. bttf = sum_rttf;
  1796. }
  1797. else{
  1798. btf = (buf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1799. bttf = (btuf.toDecimal(decimalObj.bills.unitPrice) * btq).toDecimal(decimalObj.process);
  1800. };
  1801. }
  1802. };
  1803. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  1804. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  1805. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  1806. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  1807. calcTools.checkFeeField(treeNode, ftObj);
  1808. rst.push(ftObj);
  1809. };
  1810. me.deleteUselessFees(treeNode, budgetFields);
  1811. treeNode.data.calcTemplate = {"calcItems": rst};
  1812. };
  1813. innerCalcBillExpr(treeNode){
  1814. delete treeNode.data.gljList;
  1815. let me = this;
  1816. me.deleteProperties(treeNode, ['programID']);
  1817. me.deleteUselessFees(treeNode, ['common', 'rationCommon']);
  1818. let nQ = calcTools.uiNodeQty(treeNode);
  1819. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  1820. let f = calcTools.getFeeRateByNode(treeNode);
  1821. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  1822. let tb = treeNode.data.tenderCalcBaseValue ? treeNode.data.tenderCalcBaseValue : 0;
  1823. let q = nQ ? nQ : 1;
  1824. let tq = nTQ ? nTQ : 1;
  1825. let uf = (b * f * 0.01 / q).toDecimal(decimalObj.bills.unitPrice);
  1826. let tuf = (tb * f * 0.01 / tq).toDecimal(decimalObj.bills.unitPrice);
  1827. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100) : (uf * q);
  1828. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  1829. let ttf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (tb * f / 100) : (tuf * tq);
  1830. ttf = ttf.toDecimal(decimalObj.bills.totalPrice);
  1831. calcTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf, 'tenderUnitFee': tuf, 'tenderTotalFee': ttf});
  1832. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  1833. if (calcTools.isTotalCostBill(treeNode)){
  1834. // 公式叶子清单没有暂估费,但总造价清单除外。
  1835. calcTools.estimateFee(treeNode);
  1836. calcTools.initSummaryFee(treeNode);
  1837. treeNode.data.summaryFees.totalFee = tf;
  1838. treeNode.data.summaryFees.estimateFee = calcTools.getFee(treeNode, 'estimate.totalFee');
  1839. treeNode.data.summaryFees.safetyFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION), 'common.totalFee');
  1840. treeNode.data.summaryFees.chargeFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.CHARGE), 'common.totalFee');
  1841. }
  1842. treeNode.data.calcTemplate = {"calcItems": []};
  1843. };
  1844. innerCalcBillCustom(treeNode){
  1845. let me = this;
  1846. delete treeNode.data.gljList;
  1847. me.deleteProperties(treeNode, ['calcBase', 'calcBaseValue', 'tenderCalcBaseValue', 'programID']);
  1848. me.deleteUselessFees(treeNode, ['rationCommon']);
  1849. // 2017-09-27 需求改了,除了第 1 、 2.2部分以外,都可以手工修改综合单价、综合合价并参与计算
  1850. // 在没有公式的情况下可以手工修改综合单价并参与计算
  1851. if(calcTools.canCalcToTalFeeByOwn(treeNode)){
  1852. if (treeNode.data.feesIndex && treeNode.data.feesIndex.common){
  1853. let ftObj = {fieldName: 'common'};
  1854. let nQ = calcTools.uiNodeQty(treeNode);
  1855. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  1856. ftObj.unitFee = parseFloatPlus(treeNode.data.feesIndex.common.unitFee);
  1857. ftObj.totalFee = (ftObj.unitFee * nQ).toDecimal(decimalObj.bills.totalPrice);
  1858. ftObj.tenderUnitFee = ftObj.unitFee;
  1859. ftObj.tenderTotalFee = (ftObj.tenderUnitFee * nTQ).toDecimal(decimalObj.bills.totalPrice);
  1860. calcTools.checkFeeField(treeNode, ftObj);
  1861. }
  1862. } else{
  1863. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1864. treeNode.data.fees = null;
  1865. treeNode.data.feesIndex = null;
  1866. treeNode.changed = true;
  1867. }
  1868. };
  1869. treeNode.data.calcTemplate = {"calcItems": []};
  1870. };
  1871. innerCalcRation2(treeNode){
  1872. let me = this;
  1873. calcTools.getGLJList(treeNode, true);
  1874. let nQ = calcTools.uiNodeQty(treeNode);
  1875. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1876. // 量价、工料机类型的定额要求"市场合价"
  1877. if (calcTools.isVP_or_GLJR(treeNode)){
  1878. let u = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1879. let t = (u * nQ).toDecimal(decimalObj.ration.totalPrice);
  1880. if ((treeNode.data.marketTotalFee != t) ||
  1881. (!treeNode.data.feesIndex || !treeNode.data.feesIndex.common || (treeNode.data.feesIndex.common.totalFee != t))
  1882. ){
  1883. treeNode.data.marketTotalFee = t;
  1884. let obj = {};
  1885. obj.fieldName = 'common';
  1886. obj.unitFee = u;
  1887. obj.totalFee = t;
  1888. obj.tenderUnitFee = obj.unitFee;
  1889. obj.tenderTotalFee = obj.totalFee;
  1890. calcTools.checkFeeField(treeNode, obj);
  1891. me.deleteUselessFees(treeNode, budgetFields);
  1892. treeNode.changed = true;
  1893. }
  1894. return;
  1895. };
  1896. };
  1897. calcTools.initFees(treeNode);
  1898. // 只有人、材、机、主、设。
  1899. let sum = 0;
  1900. for (let i = 0; i < 5; i++) {
  1901. if (!rationCalcBases[budgetBaseNames[i]]){
  1902. hintBox.infoBox('错误提示', `定额基数${hintBox.font('[' +funcNames[i] + ']')}未定义!`, 1);
  1903. return;
  1904. }
  1905. let obj = {};
  1906. obj.fieldName = budgetFields[i];
  1907. obj.unitFee = (rationCalcBases[budgetBaseNames[i]](treeNode)).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1908. obj.totalFee = (obj.unitFee * nQ).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1909. obj.tenderUnitFee = obj.unitFee;
  1910. obj.tenderTotalFee = obj.totalFee;
  1911. sum = sum + obj.unitFee;
  1912. calcTools.checkFeeField(treeNode, obj);
  1913. };
  1914. let obj = {};
  1915. obj.fieldName = 'common';
  1916. obj.unitFee = obj.unitFee = sum.toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1917. obj.totalFee = (obj.unitFee * nQ).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1918. obj.tenderUnitFee = obj.unitFee;
  1919. obj.tenderTotalFee = obj.totalFee;
  1920. calcTools.checkFeeField(treeNode, obj);
  1921. me.deleteUselessFees(treeNode, budgetFields);
  1922. };
  1923. // 定额部分抽取出来,供分摊定额公用。
  1924. innerCalcRation(treeNode, tenderType = tenderTypes.ttCalc){
  1925. let me = this;
  1926. let fnArr = [];
  1927. calcTools.getGLJList(treeNode, true);
  1928. let nQ = calcTools.uiNodeQty(treeNode);
  1929. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  1930. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1931. // 量价、工料机类型的定额要求"市场合价"
  1932. if (calcTools.isVP_or_GLJR(treeNode)){
  1933. let u = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1934. let t = (u * nQ).toDecimal(decimalObj.ration.totalPrice);
  1935. if (treeNode.data.marketTotalFee != t){
  1936. treeNode.data.marketTotalFee = t;
  1937. treeNode.changed = true;
  1938. } ;
  1939. };
  1940. };
  1941. let template = me.compiledTemplates[treeNode.data.programID];
  1942. treeNode.data.calcTemplate = template;
  1943. if (treeNode && template && template.hasCompiled) {//2018-08-27 空行的时候,取费专业为空,template也为空,加入template非空判断
  1944. let $CE = executeObj;
  1945. $CE.treeNode = treeNode;
  1946. $CE.template = template;
  1947. calcTools.initFees(treeNode);
  1948. for (let idx of template.compiledSeq) {
  1949. let calcItem = template.calcItems[idx];
  1950. $CE.tempCalcItem = calcItem;
  1951. let feeRate = 100; // 100%
  1952. if (calcItem.feeRate != undefined)
  1953. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  1954. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1955. calcItem.totalFee = (calcItem.unitFee * nQ).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1956. let tExpr = analyzer.getCompiledTenderExpr(calcItem.compiledExpr);
  1957. calcItem.tenderUnitFee = (eval(tExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1958. calcItem.tenderTotalFee = (calcItem.tenderUnitFee * nTQ).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1959. if (calcItem.fieldName) {
  1960. fnArr.push(calcItem.fieldName);
  1961. calcTools.checkFeeField(treeNode, calcItem);
  1962. };
  1963. };
  1964. if (tenderType == tenderTypes.ttReverseRation || tenderType == tenderTypes.ttReverseGLJ)
  1965. this.reverseTenderCalc(treeNode, tenderType);
  1966. me.deleteUselessFees(treeNode, fnArr);
  1967. };
  1968. };
  1969. // 存储、刷新零散的多个结点。
  1970. saveNodes(treeNodes, callback){
  1971. if (treeNodes.length < 1) {
  1972. $.bootstrapLoading.end();
  1973. this.rationMap = null;
  1974. this.pgljMap = null;
  1975. return;
  1976. }
  1977. let me = this;
  1978. let dataArr = [];
  1979. for (let node of treeNodes){
  1980. if (node.changed){
  1981. // console.log(node.data.name);
  1982. let data = calcTools.cutNodeForSave(node);
  1983. let newData = {'type': node.sourceType, 'data': data};
  1984. dataArr.push(newData);
  1985. }
  1986. };
  1987. if (dataArr.length < 1) {
  1988. $.bootstrapLoading.end();
  1989. return;
  1990. };
  1991. if (projectObj.project.projectInfo.lastFileVer != VERSION){
  1992. let data = {
  1993. ID: projectObj.project.ID(),
  1994. lastFileVer: VERSION
  1995. };
  1996. let newData = {'type': 'project', 'data': data};
  1997. dataArr.push(newData);
  1998. };
  1999. $.bootstrapLoading.start();
  2000. let startTime = +new Date();
  2001. me.project.updateNodes(dataArr, function (data) {
  2002. me.rationMap = null;
  2003. me.pgljMap = null;
  2004. if(callback){
  2005. callback(data);
  2006. };
  2007. for (let node of treeNodes) { delete node.changed };
  2008. projectObj.mainController.refreshTreeNode(treeNodes,false,false);
  2009. projectObj.project.projectInfo.lastFileVer = VERSION;
  2010. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。警告:第二个参数千万不能改成3,否则死循环!
  2011. if (activeSubSheetIsCalcProgram())
  2012. calcProgramObj.refreshCalcProgram(treeNodes[0].tree.selected, 2);
  2013. $.bootstrapLoading.end();
  2014. });
  2015. };
  2016. // 计算本节点及所有会被影响到的节点,如:所有父节点(默认,可选)、公式引用节点(默认,可选)。
  2017. // 修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  2018. calculate(treeNode, calcParents = true, calcFormulas = true, tender){
  2019. let me = this;
  2020. let changedNodes = [];
  2021. me.innerCalc(treeNode, changedNodes, tender);
  2022. if (treeNode.changed) {
  2023. // 计算父结点
  2024. if (calcParents){
  2025. let curNode = treeNode.parent;
  2026. while (curNode){
  2027. me.innerCalc(curNode, changedNodes, tender);
  2028. curNode = curNode.parent;
  2029. };
  2030. };
  2031. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  2032. if (calcFormulas) {
  2033. me.calcFormulaNodes(changedNodes, tender);
  2034. };
  2035. };
  2036. return changedNodes;
  2037. };
  2038. // 计算并保存一个树节点。(修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点)
  2039. calcAndSave(treeNode, callback, tender){
  2040. this.calcNodesAndSave([treeNode],callback,tender);
  2041. /* let changedNodes = this.calculate(treeNode, true, true, tender); 统一调用相同的方法
  2042. this.saveNodes(changedNodes, callback);*/
  2043. };
  2044. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  2045. calcAllType.catAll 计算所有树结点 (默认值)
  2046. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  2047. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  2048. (calcAllType.catRations时程序中做了特殊处理,实际上是计算所有树结点!)
  2049. 调价相关参数:
  2050. tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  2051. */
  2052. calcAllNodes(calcType = calcAllType.catAll, tender, tree = projectObj.project.mainTree){
  2053. let me = this;
  2054. let changedNodes = [];
  2055. function calcNodes(nodes) {
  2056. for (let node of nodes) {
  2057. if (node.children.length > 0) {
  2058. calcNodes(node.children);
  2059. };
  2060. if (calcType == calcAllType.catAll || calcType == node.sourceType) {
  2061. node.calcType = calcTools.getCalcType(node);
  2062. if (node.calcType != treeNodeCalcType.ctCalcBaseValue)
  2063. me.innerCalc(node, changedNodes, tender);
  2064. };
  2065. }
  2066. };
  2067. // calcAllType.catRations 参数情况不会计算父结点(因为父结点是清单),所以这里进行特殊处理。
  2068. if (calcType == calcAllType.catRations)
  2069. calcType == calcAllType.catAll;
  2070. calcNodes(tree.roots);
  2071. me.calcFormulaNodes(changedNodes, tender);
  2072. if (tender){
  2073. for(let node of tree.items){
  2074. this.clearTenderCache(node);
  2075. };
  2076. };
  2077. return changedNodes;
  2078. };
  2079. // tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  2080. calcAllNodesAndSave(calcType = calcAllType.catAll, callback, tender){
  2081. let changedNodes = this.calcAllNodes(calcType, tender);
  2082. this.saveNodes(changedNodes, callback);
  2083. };
  2084. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  2085. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  2086. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  2087. calcNodes(nodes, tender, tree){
  2088. let me = this, rationNodes = [], billNodes = [], leafBills = [], allChangedNodes = [];
  2089. for (let node of nodes) {
  2090. if (node.sourceType == ModuleNames.ration)
  2091. rationNodes.push(node)
  2092. else
  2093. billNodes.push(node);
  2094. };
  2095. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  2096. for (let ration of rationNodes) {
  2097. me.innerCalc(ration, allChangedNodes, tender);
  2098. let leafBill = ration.parent;
  2099. if (leafBill && leafBills.indexOf(leafBill) < 0)
  2100. leafBills.push(leafBill);
  2101. };
  2102. billNodes.merge(leafBills);
  2103. for (let bill of billNodes){
  2104. let changeBills = me.calculate(bill, true, false, tender);
  2105. allChangedNodes.merge(changeBills);
  2106. };
  2107. me.calcFormulaNodes(allChangedNodes, tender, tree);
  2108. return allChangedNodes;
  2109. };
  2110. calcNodesAndSave(nodes, callback, tender){
  2111. let me = this;
  2112. let chgNodes = me.calcNodes(nodes, tender);
  2113. me.saveNodes(chgNodes, callback);
  2114. };
  2115. // 计算全部公式项。 (changedArr:将通过本方法后发生改变的节点存入changedArr中)
  2116. calcFormulaNodes(changedArr, tender, tree = projectObj.project.mainTree){
  2117. let me = this;
  2118. let formulaNodes = cbTools.getFormulaNodes(true, tree);
  2119. if (formulaNodes.length == 0) return;
  2120. for (let formulaNode of formulaNodes){
  2121. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  2122. projectObj.project.calcBase.calculate(formulaNode, true);
  2123. if (projectObj.project.calcBase.success){
  2124. // 计算公式结点
  2125. me.innerCalc(formulaNode, changedArr, tender);
  2126. // 计算父结点
  2127. if (formulaNode.changed){
  2128. let curNode = formulaNode.parent;
  2129. while (curNode){
  2130. me.innerCalc(curNode, changedArr, tender);
  2131. curNode = curNode.parent;
  2132. };
  2133. };
  2134. };
  2135. };
  2136. };
  2137. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  2138. calcLeafAndSave(treeNode, tender){
  2139. let me = this;
  2140. if(!calcTools.isLeafBill(treeNode)) return;
  2141. if (treeNode.children && treeNode.children.length > 0) {
  2142. let changedNodes = [];
  2143. for (let child of treeNode.children){
  2144. me.innerCalc(child, changedNodes);
  2145. };
  2146. let curChangeds = me.calculate(treeNode, true, true, tender);
  2147. changedNodes.merge(curChangeds);
  2148. me.saveNodes(changedNodes);
  2149. };
  2150. };
  2151. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  2152. getTotalFee(baseNodes, excludeNodes, tender){
  2153. let rst = 0;
  2154. const totalFeeType = tender ? 'common.tenderTotalFee' : 'common.totalFee';
  2155. function calcNodes(nodes) {
  2156. for (let node of nodes) {
  2157. if(!node){
  2158. continue;
  2159. }
  2160. if (!excludeNodes.includes(node)){
  2161. if (node.source && node.source.children && node.source.children.length > 0) {
  2162. calcNodes(node.children);
  2163. }
  2164. else{
  2165. if (node.sourceType == ModuleNames.bills) {
  2166. rst = (rst + calcTools.getFee(node, totalFeeType)).toDecimal(decimalObj.decimal("totalPrice", node));
  2167. };
  2168. }
  2169. }
  2170. }
  2171. };
  2172. calcNodes(baseNodes);
  2173. return rst;
  2174. };
  2175. // 税前工程造价 ,adj调价
  2176. getBeforeTaxTotalFee(excludeNodes, tender){
  2177. let baseNodes = [], me = this;
  2178. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  2179. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  2180. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  2181. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  2182. return me.getTotalFee(baseNodes, excludeNodes, tender);
  2183. };
  2184. // 清理调价缓存数据
  2185. clearTenderCache(treeNode){
  2186. // 这些属性值为什么不定义在一个对象里?因为每次要判断对象是否存在,十分麻烦。不如直接写简单。分散书写,统一处理也很好用。
  2187. if (treeNode.data.tender_activeTotal) delete treeNode.data.tender_activeTotal;
  2188. if (treeNode.data.tender_activeTarget) delete treeNode.data.tender_activeTarget;
  2189. if (treeNode.data.tender_fullLoad) delete treeNode.data.tender_fullLoad;
  2190. if (treeNode.data.tender_distribute){
  2191. delete treeNode.data.targetTotalFee;
  2192. delete treeNode.data.targetUnitFee;
  2193. delete treeNode.data.tender_distribute;
  2194. };
  2195. };
  2196. initGljPriceTenderCoe (){
  2197. if (projectObj.project.property.tenderSetting && projectObj.project.property.tenderSetting.gljPriceTenderCoe
  2198. && (projectObj.project.property.tenderSetting.gljPriceTenderCoe != 1)){
  2199. projectObj.project.property.tenderSetting.gljPriceTenderCoe = 1; // 修改缓存值,用于计算
  2200. projectObj.project.property.needRestoreGgljPriceTenderCoe = true; // 做个标记,告诉回调函数
  2201. }
  2202. };
  2203. // 反向调价-初始化调价树、清理缓存数据等
  2204. reverseTenderInitDatas(){
  2205. for(let node of tender_obj.tenderTree.items){
  2206. if (node.data.rationQuantityCoe) node.data.rationQuantityCoe = null;
  2207. let qcObj = node.data.quantityCoe;
  2208. if (qcObj){
  2209. for (let pn in qcObj){
  2210. qcObj[pn] = null;
  2211. };
  2212. };
  2213. this.clearTenderCache(node);
  2214. };
  2215. // 反向调价时人材机单价调整系数要归1:因为既可以调量又可以调价,以哪个为基准进行反调?过于复杂,仅以通用的调量逻辑为基准即可满足需求。
  2216. this.initGljPriceTenderCoe();
  2217. };
  2218. // 反向调价简单计算:只计算当前单一结点,由目标金额计算定额量系数或工料机量系数coe,再根据此coe计算定额的调后量、调后合价。
  2219. // ⑴只计算参数指定的一个单一的结点,连父结点、引用结点都不计算。此结点只会是定额、量价、工料机款定额,不会是清单。
  2220. // ⑵执行此方法的前提:已在其它地方把总的目标金额分摊好了,此结点已获取目标金额。
  2221. reverseTenderCalc(treeNode, tender) {
  2222. if (tender == tenderTypes.ttReverseRation) {
  2223. if (treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2224. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2225. treeNode.changed = true;
  2226. }
  2227. };
  2228. if (!treeNode.data.targetTotalFee){ // 没有目标金额、但有输入目标单价的:根据目标单价算出目标金额。
  2229. if (treeNode.data.targetUnitFee){
  2230. treeNode.data.targetTotalFee = (treeNode.data.targetUnitFee * treeNode.data.quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2231. treeNode.changed = true;
  2232. }
  2233. else{ // 既没有目标金额也没有目标单价,此时要初始化使调价合价=原始综合合价,调价单价=原始综合单价。
  2234. if (treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2235. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2236. treeNode.changed = true;
  2237. };
  2238. if (treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderTotalFee != treeNode.data.feesIndex.common.totalFee) {
  2239. treeNode.data.feesIndex.common.tenderTotalFee = treeNode.data.feesIndex.common.totalFee;
  2240. treeNode.changed = true;
  2241. };
  2242. return;
  2243. }
  2244. };
  2245. // 经过前面的一通折腾还是没有目标单价的话,那么就要通过目标金额算出来。
  2246. if (!treeNode.data.targetUnitFee || (parseFloat(treeNode.data.targetUnitFee) == 0)){
  2247. if (calcTools.hasQuantity(treeNode))
  2248. treeNode.data.targetUnitFee = (treeNode.data.targetTotalFee / treeNode.data.quantity).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  2249. }
  2250. // 系数=调后单价/调前单价
  2251. let coe = 1;
  2252. if (treeNode.data.feesIndex.common.totalFee != 0)
  2253. coe = (treeNode.data.targetUnitFee / treeNode.data.feesIndex.common.unitFee).toDecimal(decimalObj.process);
  2254. // 量价、工料机款式的定额:它们无下挂工料机可调,直接调树结点的消耗量。
  2255. let isVP_RevGLJ =(tender == tenderTypes.ttReverseGLJ) && calcTools.isVP_or_GLJR(treeNode);
  2256. if ((tender == tenderTypes.ttReverseRation) || isVP_RevGLJ){
  2257. treeNode.data.tenderQuantity = (treeNode.data.quantity * coe).toDecimal(decimalObj.decimal("quantity", treeNode));
  2258. if (treeNode.data.rationQuantityCoe != coe){
  2259. treeNode.data.rationQuantityCoe = coe;
  2260. treeNode.changed = true;
  2261. };
  2262. if (isVP_RevGLJ){
  2263. treeNode.data.quantityCoe = {labour: 0, material: 0, machine: 0, main: 0, equipment: 0};
  2264. treeNode.changed = true;
  2265. };
  2266. let ttf = (treeNode.data.tenderQuantity * treeNode.data.feesIndex.common.tenderUnitFee).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2267. if (treeNode.data.feesIndex.common.tenderTotalFee != ttf){
  2268. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  2269. treeNode.changed = true;
  2270. };
  2271. }else if (tender == tenderTypes.ttReverseGLJ){
  2272. let qcObj = treeNode.data.quantityCoe;
  2273. if (!qcObj || calcTools.isEmptyObject(qcObj)){
  2274. treeNode.data.quantityCoe = {labour: coe, material: coe, machine: coe, main: coe, equipment: coe};
  2275. treeNode.changed = true;
  2276. }else{
  2277. // 这种写法会漏掉属性,导致界面显示不统一。
  2278. /* for (let pn in qcObj){
  2279. if (qcObj[pn] != coe){
  2280. qcObj[pn] = coe;
  2281. treeNode.changed = true;
  2282. }
  2283. };*/
  2284. if (qcObj.labour != coe) {
  2285. qcObj.labour = coe;
  2286. treeNode.changed = true;
  2287. };
  2288. if (qcObj.material != coe) {
  2289. qcObj.material = coe;
  2290. treeNode.changed = true;
  2291. };
  2292. if (qcObj.machine != coe) {
  2293. qcObj.machine = coe;
  2294. treeNode.changed = true;
  2295. };
  2296. if (qcObj.main != coe) {
  2297. qcObj.main = coe;
  2298. treeNode.changed = true;
  2299. };
  2300. if (qcObj.equipment != coe) {
  2301. qcObj.equipment = coe;
  2302. treeNode.changed = true;
  2303. };
  2304. };
  2305. projectObj.project.calcProgram.calculate(treeNode, false, false, tenderTypes.ttCalc); // 再正向算
  2306. };
  2307. };
  2308. // 反向调价-分摊前的准备工作。包括:
  2309. // ⑴结点是否满载
  2310. // ⑵若是满载,汇总孩子的金额、目标金额给它
  2311. // ⑶标记此结点的目标金额来自孩子,未来它不往下分摊(汇上来再摊下去无用功)
  2312. prepareForDistribute(treeNode){
  2313. if (!treeNode) return;
  2314. if (treeNode.firstChild())
  2315. this.prepareForDistribute(treeNode.firstChild());
  2316. if (treeNode.children.length == 0){}
  2317. else{
  2318. let full = true;
  2319. for (let i = 0; i < treeNode.children.length; i++) {
  2320. let child = treeNode.children[i];
  2321. if (!child.data.tender_fullLoad && !calcTools.hasTargetTotalFee(child)){
  2322. full = false;
  2323. break;
  2324. };
  2325. }
  2326. if (full) {
  2327. let total = 0, target = 0;
  2328. for (let i = 0; i < treeNode.children.length; i++) {
  2329. let child = treeNode.children[i];
  2330. total = total + parseFloat(child.data.feesIndex['common'].totalFee);
  2331. target = target + parseFloat(child.data.targetTotalFee);
  2332. };
  2333. treeNode.data.tender_activeTotal = total;
  2334. treeNode.data.tender_activeTarget = target;
  2335. treeNode.data.targetTotalFee = target;
  2336. treeNode.data.tender_distribute = 2;
  2337. };
  2338. treeNode.data.tender_fullLoad = full;
  2339. };
  2340. if (treeNode.nextSibling)
  2341. this.prepareForDistribute(treeNode.nextSibling);
  2342. };
  2343. // 反向调价-分摊目标合价:从父到子往下分摊。
  2344. distributeTargetTotalFee(treeNode){
  2345. if (!treeNode) return;
  2346. if (treeNode.data.feesIndex && treeNode.data.feesIndex['common']){ // 空清单忽略
  2347. // 默认能够执行到这里时每个节点已经被初始化,缓存已删除
  2348. treeNode.data.tender_activeTotal = treeNode.data.feesIndex['common'].totalFee;
  2349. // 开始分摊:只分摊自有目标金额、从父结点分摊到的金额(不分摊子结点汇总上来的金额)
  2350. if (calcTools.hasTargetTotalFee(treeNode) && !(treeNode.data.tender_distribute && treeNode.data.tender_distribute == 2)){
  2351. treeNode.data.tender_activeTarget = treeNode.data.targetTotalFee;
  2352. // 先把会破坏金额比例关系的孩子排除:1.有目标金额的 2.满载的(孙子全满,没有分摊空间,所以实质上该孩子的金额已被锁死无法分摊)
  2353. for (let i = 0; i < treeNode.children.length; i++) {
  2354. let child = treeNode.children[i];
  2355. if (!child.data.feesIndex || !child.data.feesIndex['common']) continue; // 空白行清单、定额
  2356. child.data.tender_activeTotal = child.data.feesIndex['common'].totalFee;
  2357. if (calcTools.hasTargetTotalFee(child)){
  2358. child.data.tender_activeTarget = child.data.targetTotalFee;
  2359. console.log(treeNode.data.tender_activeTotal);
  2360. console.log(child.data.tender_activeTotal);
  2361. treeNode.data.tender_activeTotal = treeNode.data.tender_activeTotal - child.data.tender_activeTotal;
  2362. treeNode.data.tender_activeTarget = treeNode.data.tender_activeTarget - child.data.tender_activeTarget;
  2363. };
  2364. }
  2365. if (treeNode.data.tender_activeTotal != 0){
  2366. let coe = (treeNode.data.tender_activeTarget / treeNode.data.tender_activeTotal).toDecimal(decimalObj.process);
  2367. for (let i = 0; i < treeNode.children.length; i++) {
  2368. let child = treeNode.children[i];
  2369. if (!child.data.feesIndex || !child.data.feesIndex['common']) continue; // 空白行清单、定额
  2370. if (!calcTools.hasTargetTotalFee(child)){
  2371. child.data.tender_activeTarget = (coe * child.data.tender_activeTotal).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2372. child.data.targetTotalFee = child.data.tender_activeTarget;
  2373. child.data.tender_distribute = 1; // 1表示分摊金额来自父结点。2表示分摊金额来自孩子结点。
  2374. }
  2375. }
  2376. };
  2377. };
  2378. }
  2379. if (treeNode.firstChild())
  2380. this.distributeTargetTotalFee(treeNode.firstChild());
  2381. if (treeNode.nextSibling)
  2382. this.distributeTargetTotalFee(treeNode.nextSibling);
  2383. };
  2384. // 反向调价逼近
  2385. reverseTenderApproach(callback, tender){
  2386. let me = this;
  2387. let G_DIGIT = 0.01; // 系数调整步距(0.1最终结果误差大。0.001目标金额与逼前金额差距大时无法有效逼近)
  2388. let MaxDiffValue = 0.1; // 可接受的最大差值。(如果实际差值比这个大:说明列表结点耗尽,无法达到指定精度。公路设为1,建筑设为0.1)
  2389. let times = 300; // 逼近计算的极限次数。正常情况下“单位系数金额”列表中的结点耗尽即退出,这里指定轮数是最后保险阀,防止无限死循环。
  2390. let calcModel = 1; // 计算模式:1 精度优先(差值不接近0不停,直到结点用完或极限次数用完。时间长精度高)2 速度优先(达到指定差值范围即熔断逼近。时间短差值大)
  2391. let diffProp = 0.0001; // 计算模式=2(速度优先)时有效。 通过这个金额比例值计算可接受的最大差值D。差值D = 根结点金额 * diffProp
  2392. let isTest = false; // 测试
  2393. // 按指定的比例获取可接受的差值:如目标金额的万分之一。
  2394. function getPropV(node){
  2395. let v = parseFloat((node.data.targetTotalFee * diffProp).toFixed(0)); // node.data.feesIndex.common.totalFee
  2396. return Math.max(v, MaxDiffValue);
  2397. }
  2398. // 取根结点的:调后金额跟目标金额差值,看还有多少误差需要处理。
  2399. function getRootDiff() {
  2400. let root = tender_obj.tenderTree.roots[0];
  2401. return (root.data.feesIndex.common.tenderTotalFee - root.data.targetTotalFee).toDecimal(3);
  2402. }
  2403. // 生成每单位差值的定额结点列表。
  2404. function getNodeDiffs() {
  2405. let arr = [];
  2406. for (let i = 0; i < tender_obj.tenderTree.items.length; i++) {
  2407. let node = tender_obj.tenderTree.items[i];
  2408. // 量价还是要参与,因为它贡献了金额,如果它的金额比重很大,它退出了,会导致其它结点过调。
  2409. // if (calcTools.isRationCategory(node) && (!calcTools.isVP_or_GLJR(node))){
  2410. if (calcTools.isRationCategory(node)){
  2411. let coe = calcTools.getCoe(node, tender);
  2412. if (coe!= 0) {
  2413. let diff = Math.abs(node.data.feesIndex.common.tenderTotalFee - node.data.feesIndex.common.totalFee);
  2414. node.data.tender_diffValuePerCoe = (diff * G_DIGIT / coe).toDecimal(decimalObj.process);
  2415. node.data.tender_rowNo = i + 1; // node在UI上显示的行号
  2416. arr.push(node);
  2417. }
  2418. }
  2419. };
  2420. arr.sort(function sortArr(a, b) { return a.data.tender_diffValuePerCoe - b.data.tender_diffValuePerCoe});
  2421. if (isTest){
  2422. let arr2 = [];
  2423. for (let i = 0; i < arr.length; i++) {
  2424. arr2.push({row: arr[i].data.tender_rowNo, code: arr[i].data.code + ' ' + arr[i].data.name, diff: arr[i].data.tender_diffValuePerCoe});
  2425. }
  2426. console.log(arr2);
  2427. }
  2428. return arr;
  2429. }
  2430. // 每单位差值的定额结点列表中,取离给定值最近的定额结点。
  2431. function getCloseNode(arr, value) {
  2432. let index = 0;
  2433. let d_value = Number.MAX_VALUE;
  2434. for (let i = 0; i < arr.length; i++) {
  2435. let new_d_value = Math.abs(arr[i].data.tender_diffValuePerCoe - value);
  2436. if (new_d_value <= d_value) {
  2437. if (new_d_value === d_value && arr[i].data.tender_diffValuePerCoe < arr[index].data.tender_diffValuePerCoe) {
  2438. continue;
  2439. }
  2440. index = i;
  2441. d_value = new_d_value;
  2442. }
  2443. }
  2444. return {idx: index, node: arr[index]}
  2445. }
  2446. // 逼近(单轮)。arr 参考取值列表。返回{type, node, nodeIdx}。
  2447. // type:1正常,2无结点,3结点过调。node:结点。nodeIdx:结点在列表中的索引位置。node.data.tender_rowNo 在UI上的行号。
  2448. function approach(arr){
  2449. let v = getRootDiff();
  2450. let obj = getCloseNode(arr, Math.abs(v)); // {idx, node} 极端:{0, undefind}
  2451. let closeNode = obj.node;
  2452. if (!closeNode)
  2453. return {type: 2, node: undefined, nodeIdx: -1}; // arr 被清空了
  2454. let d = (v > 0) ? -G_DIGIT : G_DIGIT;
  2455. let coe = calcTools.getCoe(closeNode, tender);
  2456. if ((coe + d) < 0)
  2457. return {type: 3, node: obj.node, nodeIdx: obj.idx}; // 再调的话,系数就变负数了,过调
  2458. if (tender == tenderTypes.ttReverseRation){
  2459. closeNode.data.tender_previousCoe = closeNode.data.rationQuantityCoe; // tender_previousCoe: 上一次的调整系数,用于撤回
  2460. closeNode.data.rationQuantityCoe = (closeNode.data.rationQuantityCoe + d).toDecimal(decimalObj.process);
  2461. }
  2462. else if (tender == tenderTypes.ttReverseGLJ){
  2463. if (calcTools.isVP_or_GLJR(closeNode)){
  2464. closeNode.data.tender_previousCoe = closeNode.data.rationQuantityCoe;
  2465. closeNode.data.rationQuantityCoe = (closeNode.data.rationQuantityCoe + d).toDecimal(decimalObj.process);
  2466. }
  2467. else {
  2468. closeNode.data.tender_previousCoe = closeNode.data.quantityCoe.labour;
  2469. closeNode.data.quantityCoe.labour = (closeNode.data.quantityCoe.labour + d).toDecimal(decimalObj.process)
  2470. closeNode.data.quantityCoe.material = (closeNode.data.quantityCoe.material + d).toDecimal(decimalObj.process)
  2471. closeNode.data.quantityCoe.machine = (closeNode.data.quantityCoe.machine + d).toDecimal(decimalObj.process)
  2472. closeNode.data.quantityCoe.main = (closeNode.data.quantityCoe.main + d).toDecimal(decimalObj.process)
  2473. closeNode.data.quantityCoe.equipment = (closeNode.data.quantityCoe.equipment + d).toDecimal(decimalObj.process)
  2474. };
  2475. }
  2476. me.calculate(closeNode, true, true, tenderTypes.ttCalc);
  2477. return {type: 1, node: obj.node, nodeIdx: obj.idx};
  2478. }
  2479. // 撤消最后一轮逼近(调过头了,回退一步)
  2480. function undoLastApproach(obj){
  2481. let closeNode = obj.node;
  2482. let coe = closeNode.data.tender_previousCoe;
  2483. if (tender == tenderTypes.ttReverseRation){
  2484. closeNode.data.rationQuantityCoe = coe
  2485. } else if (tender == tenderTypes.ttReverseGLJ){
  2486. if (calcTools.isVP_or_GLJR(closeNode)){
  2487. closeNode.data.rationQuantityCoe = coe
  2488. }
  2489. else {
  2490. closeNode.data.quantityCoe.labour = coe
  2491. closeNode.data.quantityCoe.material = coe
  2492. closeNode.data.quantityCoe.machine = coe
  2493. closeNode.data.quantityCoe.main = coe
  2494. closeNode.data.quantityCoe.equipment = coe
  2495. };
  2496. }
  2497. me.calculate(closeNode, true, true, tenderTypes.ttCalc);
  2498. if (isTest) {
  2499. let _sp = `        `; // 保留空格,打印对齐
  2500. let _node = `[行${obj.node.data.tender_rowNo} 索引${obj.nodeIdx}]`;
  2501. console.log(`${_sp} ${_node} 过调,已回退`);
  2502. }
  2503. }
  2504. let root = tender_obj.tenderTree.roots[0];
  2505. let propV = getPropV(root);
  2506. let vArr = getNodeDiffs();
  2507. let d1 = getRootDiff();
  2508. if (isTest){
  2509. let _tq = `调前${root.data.feesIndex.common.totalFee}`;
  2510. let _mb = `目标${root.data.targetTotalFee}`;
  2511. let _wbj = `调后无逼近${root.data.feesIndex.common.tenderTotalFee}`;
  2512. let _cz = `差值${d1}`;
  2513. let _js = `轮${times} 系数步距${G_DIGIT}`;
  2514. let _yx = (calcModel == 2) ? `速度优先(差比${diffProp} 差域0~${propV})` : "精度优先";
  2515. console.log(`${_tq}|${_mb}|${_wbj}|${_cz}|${_yx}|${_js}`);
  2516. }
  2517. // 多轮逼进
  2518. for (let i = 1; i <= times; i++) {
  2519. // 与目标金额差值在1以内,整数部分已相同,结果很棒。很多时候能达到完美的差值0,但不能用0判断,因为金额取小数时有问题。
  2520. if (Math.abs(d1) < MaxDiffValue) break;
  2521. // 速度优先模式下,结果到达差值范围内,即熔断逼近。
  2522. if (calcModel == 2){
  2523. if (Math.abs(d1) < propV) break;
  2524. };
  2525. let obj = approach(vArr);
  2526. if (obj.type == 2) break; // 结点耗完,列表已清空,无结点可调。
  2527. if (obj.type == 3) { // 结点过调,该结点任务已完成,不再参与调节,从列表中清除
  2528. vArr.splice(obj.nodeIdx, 1);
  2529. continue;
  2530. };
  2531. let d2 = getRootDiff();
  2532. if (isTest){
  2533. let _time = `【第 ${i} 轮】调整`;
  2534. let _node = `[行${obj.node.data.tender_rowNo} 索引${obj.nodeIdx}]`;
  2535. let _coe = `${obj.node.data.tender_previousCoe} → ${calcTools.getCoe(obj.node, tender)}`;
  2536. let _d = `差值${d1} → ${d2}`;
  2537. console.log(`${_time}${_node} ${_coe},${_d}`);
  2538. }
  2539. if (Math.abs(d2) > Math.abs(d1)){ // 逼近后差值反而变大,证明此轮调节不合适,回退一步,并将结点从列表清除
  2540. undoLastApproach(obj);
  2541. vArr.splice(obj.nodeIdx, 1);
  2542. }
  2543. else if (Math.abs(d2) == Math.abs(d1)){ // 调前调后差距相同时,会死循环,也从列表中清除
  2544. vArr.splice(obj.nodeIdx, 1);
  2545. }
  2546. else {d1 = d2}
  2547. };
  2548. // 重要注释:到这里,所有系数已处理就绪,最后直接全局正算即可。
  2549. // 到这一步,被处理的定额、及其受影响的父结点、引用结点等均已正算完成(approach方法、undoLastApproach方法),但都未保存。
  2550. // 最后全局正算目的:⑴计算除第一部以外的其它部分 ⑵保存。正算在调用外面完成。
  2551. // this.calcAllNodesAndSave(calcAllType.catAll, callback, tenderTypes.ttCalc);
  2552. };
  2553. setRationMap(){
  2554. if(this.rationMap == null){
  2555. this.rationMap = {};
  2556. for(let glj of projectObj.project.ration_glj.datas){
  2557. if(this.rationMap[glj.rationID]){
  2558. this.rationMap[glj.rationID].push(glj)
  2559. }else {
  2560. this.rationMap[glj.rationID] = [glj];
  2561. }
  2562. }
  2563. }
  2564. if(this.pgljMap == null ){
  2565. this.pgljMap = _.indexBy(projectObj.project.projectGLJ.datas.gljList, 'id');
  2566. }
  2567. };
  2568. getGljArrByRation(ration){
  2569. if (ration.type == rationType.gljRation){
  2570. let glj = JSON.parse(JSON.stringify(ration));
  2571. glj.type = glj.subType;
  2572. glj.quantity = 1;
  2573. glj.totalQuantity = parseFloatPlus(ration.quantity);
  2574. return [glj];
  2575. } else{
  2576. if(!this.rationMap) return [];
  2577. let result = this.rationMap[ration.ID];
  2578. if(!result) return [];
  2579. result = gljOprObj.combineWithProjectGlj(result,false,ration,this.pgljMap);
  2580. return result;
  2581. }
  2582. };
  2583. doTenderCalc(callback) {
  2584. $.bootstrapLoading.start();
  2585. setTimeout(()=>{
  2586. let tender = calcTools.getTenderType();
  2587. if (tender == tenderTypes.ttReverseGLJ || tender == tenderTypes.ttReverseRation) {
  2588. // 调价计算必须依赖调价树。
  2589. if (!tender_obj.tenderTree){
  2590. tender_obj.createTree();
  2591. tender_obj.createTreeNodes();
  2592. }
  2593. // 反向调价+逼近原理:
  2594. // 清理调价缓存 → 分摊目标金额(从子往父处理满载、从父往子分摊) → 全局反算 → 逼近 → 全局正算、存储
  2595. this.reverseTenderInitDatas();
  2596. this.prepareForDistribute(tender_obj.tenderTree.roots[0]);
  2597. this.distributeTargetTotalFee(tender_obj.tenderTree.roots[0]);
  2598. this.calcAllNodes(calcAllType.catAll, tender); // 先全局反算:得到每定额的coe、基础调后金额(误差大,需逼近)
  2599. this.reverseTenderApproach(callback, tender); // 逼近上述基础调后金额
  2600. }
  2601. // 全局正算(用的是主树,无需调价树)
  2602. this.calcAllNodesAndSave(calcAllType.catAll, callback, tenderTypes.ttCalc);
  2603. })
  2604. };
  2605. // 算法1:直接累计同取费多定额的各个费
  2606. gatherFee(template){
  2607. let rations = calcTools.getRationsByProgramID(template.ID);
  2608. let rst = 0;
  2609. for (let ft of cpFeeTypes) {
  2610. let sum_uf = 0, sum_tuf = 0, sum_tf = 0, sum_ttf = 0;
  2611. for (const r of rations) {
  2612. let uf = 0, tuf = 0, tf = 0, ttf = 0;
  2613. if (r.feesIndex && r.feesIndex[ft.type]) {
  2614. uf = parseFloatPlus(r.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  2615. tuf = parseFloatPlus(r.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  2616. tf = parseFloatPlus(r.feesIndex[ft.type].totalFee).toDecimal(decimalObj.bills.totalPrice);
  2617. ttf = parseFloatPlus(r.feesIndex[ft.type].tenderTotalFee).toDecimal(decimalObj.bills.totalPrice);
  2618. };
  2619. sum_uf = (sum_uf + uf).toDecimal(decimalObj.process);
  2620. sum_tuf = (sum_tuf + tuf).toDecimal(decimalObj.process);
  2621. sum_tf = (sum_tf + tf).toDecimal(decimalObj.process);
  2622. sum_ttf = (sum_ttf + ttf).toDecimal(decimalObj.process);
  2623. };
  2624. for (let i = 0; i < template.calcItems.length; i++) {
  2625. let ci = template.calcItems[i];
  2626. if (ci.fieldName == ft.type){
  2627. ci.totalFee = sum_tf.toDecimal(decimalObj.bills.totalPrice);
  2628. ci.tenderTotalFee = sum_ttf.toDecimal(decimalObj.bills.totalPrice);
  2629. ci.unitFee = sum_uf.toDecimal(decimalObj.bills.unitPrice);
  2630. ci.tenderUnitFee = sum_tuf.toDecimal(decimalObj.bills.unitPrice);
  2631. if (ft.type =='common') {
  2632. template.totalFee = ci.totalFee;
  2633. rst = ci.totalFee;
  2634. }
  2635. break;
  2636. }
  2637. };
  2638. };
  2639. return rst;
  2640. };
  2641. // 算法2:先汇总同取费多定额下的工料机,虚拟成一个大定额对象,走计算程序。
  2642. gatherFee2(template){
  2643. let data = {gljList: null};
  2644. let obj = {data: data};
  2645. obj.data.gljList = calcTools.getGLJListByProgramID(template.ID);
  2646. let $CE = executeObj;
  2647. $CE.treeNode = obj;
  2648. $CE.template = template;
  2649. let rst = 0;
  2650. for (let idx of template.compiledSeq) {
  2651. let calcItem = template.calcItems[idx];
  2652. $CE.tempCalcItem = calcItem;
  2653. let feeRate = 100; // 100%
  2654. if (calcItem.feeRate != undefined)
  2655. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  2656. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(2);
  2657. // calcItem.totalFee = (calcItem.unitFee * nQ).toDecimal(2);
  2658. calcItem.totalFee = (calcItem.unitFee * 1).toDecimal(2);
  2659. if (calcItem.fieldName =='common') {
  2660. template.totalFee = calcItem.totalFee;
  2661. rst = calcItem.totalFee;
  2662. }
  2663. };
  2664. let tobj = {
  2665. ID: template.ID,
  2666. name: template.name,
  2667. totalFee: template.totalFee,
  2668. calcItems: template.calcItems
  2669. };
  2670. // let tobj = JSON.parse(JSON.stringify(template));
  2671. return {value: rst, template: tobj};
  2672. };
  2673. //报表用取费类别汇总。调用:let obj = projectObj.project.calcProgram.getGatherFeeData();
  2674. async getGatherFeeData(){
  2675. let rst = {projectID: projectObj.project.ID(), totalFee: 0, calcPrograms: []};
  2676. let tpls = calcTools.getUsedTemplates();
  2677. let temp = 0;
  2678. for (const t of tpls) {
  2679. let obj = this.gatherFee2(t);
  2680. rst.calcPrograms.push(obj.template);
  2681. temp = temp + obj.value;
  2682. }
  2683. rst.totalFee = temp.toDecimal(decimalObj.bills.totalPrice);
  2684. let oldValue = projectObj.project.mainTree.items[0].data.totalFee;
  2685. if (oldValue != rst.totalFee){
  2686. projectObj.project.mainTree.items[0].data.totalFee = rst.totalFee;
  2687. await ajaxPost('/calcProgram/saveGatherCalcPrograms', rst);
  2688. }
  2689. console.log(rst);
  2690. return rst;
  2691. };
  2692. };