calc_program.js 119 KB

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