calc_program.js 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339
  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 父清单汇总子清单的费用类别。
  2052. // 3: 分摊:叶子清单汇总定额的费用类别。4: 清单单价锁定。
  2053. innerCalcBill(treeNode, commonCalcType, tender = tenderTypes.ttCalc) {
  2054. let me = this;
  2055. treeNode.data.programID = null;
  2056. calcTools.initFees(treeNode);
  2057. let nodes = [];
  2058. if (commonCalcType == 1) {
  2059. calcTools.getGLJList(treeNode, true);
  2060. nodes = me.project.Ration.getRationNodes(treeNode);
  2061. } else if (commonCalcType == 2) {
  2062. // 固定清单 "材料(工程设备)暂估价" 比较特殊,不进行父项汇总(需求是这么要求的)
  2063. // nodes = treeNode.children
  2064. nodes = me.project.Bills.getGatherNodes(treeNode);
  2065. } else if (commonCalcType == 3) nodes = treeNode.children;
  2066. function isBaseFeeType(type) {
  2067. return (
  2068. ["labour", "material", "machine", "mainMaterial", "equipment", "labourInc"].indexOf(
  2069. type
  2070. ) > -1
  2071. );
  2072. }
  2073. let nQ = calcTools.uiNodeQty(treeNode);
  2074. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  2075. let bq = nQ ? nQ : 1;
  2076. let btq = nTQ ? nTQ : 1;
  2077. let rst = [];
  2078. for (let ft of cpFeeTypes) {
  2079. let ftObj = {};
  2080. ftObj.fieldName = ft.type;
  2081. ftObj.name = ft.name;
  2082. let buf = 0,
  2083. btf = 0,
  2084. btuf = 0,
  2085. bttf = 0;
  2086. if (commonCalcType == 2) {
  2087. for (let node of nodes) {
  2088. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  2089. btf = (
  2090. btf + parseFloatPlus(node.data.feesIndex[ft.type].totalFee)
  2091. ).toDecimal(decimalObj.process);
  2092. bttf = (
  2093. bttf + parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee)
  2094. ).toDecimal(decimalObj.process);
  2095. }
  2096. }
  2097. }
  2098. else if (commonCalcType == 1 || commonCalcType == 3) {
  2099. let sum_rtf = 0,
  2100. sum_rttf = 0;
  2101. for (let node of nodes) {
  2102. let ruf = 0,
  2103. rtuf = 0,
  2104. rtf = 0,
  2105. rttf = 0;
  2106. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  2107. ruf = parseFloatPlus(
  2108. node.data.feesIndex[ft.type].unitFee
  2109. ).toDecimal(decimalObj.bills.unitPrice);
  2110. rtuf = parseFloatPlus(
  2111. node.data.feesIndex[ft.type].tenderUnitFee
  2112. ).toDecimal(decimalObj.bills.unitPrice);
  2113. rtf = parseFloatPlus(
  2114. node.data.feesIndex[ft.type].totalFee
  2115. ).toDecimal(decimalObj.bills.totalPrice);
  2116. rttf = parseFloatPlus(
  2117. node.data.feesIndex[ft.type].tenderTotalFee
  2118. ).toDecimal(decimalObj.bills.totalPrice);
  2119. }
  2120. // 取费方式为子目含量,清单行/列的XX单价应 =ROUND( ∑ROUND(定额XX单价*含量,清单单价精度),清单单价精度)
  2121. if (
  2122. me.project.property.billsCalcMode ===
  2123. leafBillGetFeeType.rationContent
  2124. ) {
  2125. buf = (
  2126. buf +
  2127. (ruf * parseFloatPlus(node.data.contain)).toDecimal(
  2128. decimalObj.bills.unitPrice
  2129. )
  2130. ).toDecimal(decimalObj.process);
  2131. if (node.data.tenderQuantity)
  2132. node.data.tenderContaion = (
  2133. node.data.tenderQuantity / bq
  2134. ).toDecimal(decimalObj.process);
  2135. else node.data.tenderContaion = node.data.contain;
  2136. btuf = (
  2137. btuf +
  2138. (rtuf * parseFloatPlus(node.data.tenderContaion)).toDecimal(
  2139. decimalObj.bills.unitPrice
  2140. )
  2141. ).toDecimal(decimalObj.process);
  2142. }
  2143. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  2144. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  2145. }
  2146. if (me.project.property.billsCalcMode == leafBillGetFeeType.rationPriceConverse || me.project.property.billsCalcMode == leafBillGetFeeType.rationPrice) {
  2147. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  2148. btuf = (sum_rttf / btq).toDecimal(decimalObj.process);
  2149. }
  2150. if (isBaseFeeType(ft.type) || (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")) {
  2151. btf = sum_rtf;
  2152. bttf = sum_rttf;
  2153. } else {
  2154. btf = (buf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  2155. bttf = (btuf.toDecimal(decimalObj.bills.unitPrice) * btq).toDecimal(decimalObj.process);
  2156. }
  2157. }
  2158. else if (commonCalcType == 4){ // 清单单价锁定
  2159. if (treeNode.data.feesIndex && treeNode.data.feesIndex[ft.type]) {
  2160. buf = parseFloatPlus(treeNode.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  2161. btuf = parseFloatPlus(treeNode.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  2162. btf = (bq * buf).toDecimal(decimalObj.bills.totalPrice);
  2163. bttf = (btq * btuf).toDecimal(decimalObj.bills.totalPrice);
  2164. }
  2165. }
  2166. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  2167. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  2168. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  2169. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  2170. calcTools.checkFeeField(treeNode, ftObj);
  2171. rst.push(ftObj);
  2172. }
  2173. treeNode.data.calcTemplate = { calcItems: rst };
  2174. }
  2175. innerCalcBillExpr(treeNode) {
  2176. delete treeNode.data.gljList;
  2177. let me = this;
  2178. me.deleteProperties(treeNode, ["programID"]);
  2179. me.deleteUselessFees(treeNode, ["common", "rationCommon"]);
  2180. let nQ = calcTools.uiNodeQty(treeNode);
  2181. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  2182. let f = calcTools.getFeeRateByNode(treeNode);
  2183. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  2184. let tb = treeNode.data.tenderCalcBaseValue
  2185. ? treeNode.data.tenderCalcBaseValue
  2186. : 0;
  2187. let q = nQ ? nQ : 1;
  2188. let tq = nTQ ? nTQ : 1;
  2189. let uf = ((b * f * 0.01) / q).toDecimal(decimalObj.bills.unitPrice);
  2190. let tuf = ((tb * f * 0.01) / tq).toDecimal(decimalObj.bills.unitPrice);
  2191. let tf =
  2192. me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice
  2193. ? (b * f) / 100
  2194. : uf * q;
  2195. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  2196. let ttf =
  2197. me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice
  2198. ? (tb * f) / 100
  2199. : tuf * tq;
  2200. ttf = ttf.toDecimal(decimalObj.bills.totalPrice);
  2201. calcTools.checkFeeField(treeNode, {
  2202. fieldName: "common",
  2203. unitFee: uf,
  2204. totalFee: tf,
  2205. tenderUnitFee: tuf,
  2206. tenderTotalFee: ttf,
  2207. });
  2208. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  2209. if (calcTools.isTotalCostBill(treeNode)) {
  2210. // 公式叶子清单没有暂估费,但总造价清单除外。
  2211. calcTools.estimateFee(treeNode);
  2212. calcTools.initSummaryFee(treeNode);
  2213. treeNode.data.summaryFees.totalFee = tf;
  2214. treeNode.data.summaryFees.estimateFee = calcTools.getFee(
  2215. treeNode,
  2216. "estimate.totalFee"
  2217. );
  2218. treeNode.data.summaryFees.safetyFee = calcTools.getFee(
  2219. calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION),
  2220. "common.totalFee"
  2221. );
  2222. treeNode.data.summaryFees.chargeFee = calcTools.getFee(
  2223. calcTools.getNodeByFlag(fixedFlag.CHARGE),
  2224. "common.totalFee"
  2225. );
  2226. }
  2227. treeNode.data.calcTemplate = { calcItems: [] };
  2228. }
  2229. innerCalcBillCustom(treeNode) {
  2230. let me = this;
  2231. delete treeNode.data.gljList;
  2232. me.deleteProperties(treeNode, [
  2233. "calcBase",
  2234. "calcBaseValue",
  2235. "tenderCalcBaseValue",
  2236. "programID",
  2237. ]);
  2238. me.deleteUselessFees(treeNode, ["rationCommon"]);
  2239. // 2017-09-27 需求改了,除了第 1 、 2.2部分以外,都可以手工修改综合单价、综合合价并参与计算
  2240. // 2021-09-14 需求变更,为简化逻辑,“除了第 1 、 2.2部分以外,...” 这里的业务不必保留。增加强制修改综合单价功能。
  2241. // if (calcTools.canCalcToTalFeeByOwn(treeNode)) {
  2242. if (treeNode.data.feesIndex && treeNode.data.feesIndex.common) {
  2243. let ftObj = { fieldName: "common" };
  2244. let nQ = calcTools.uiNodeQty(treeNode);
  2245. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  2246. ftObj.unitFee = parseFloatPlus(treeNode.data.feesIndex.common.unitFee); // 以单价为基础数据。金额从单价计算获得。
  2247. ftObj.totalFee = (ftObj.unitFee * nQ).toDecimal(decimalObj.bills.totalPrice);
  2248. ftObj.tenderUnitFee = ftObj.unitFee;
  2249. ftObj.tenderTotalFee = (ftObj.tenderUnitFee * nTQ).toDecimal(decimalObj.bills.totalPrice);
  2250. calcTools.checkFeeField(treeNode, ftObj);
  2251. } else {
  2252. if (treeNode.data.fees && treeNode.data.fees.length > 0) {
  2253. treeNode.data.fees = null;
  2254. treeNode.data.feesIndex = null;
  2255. treeNode.changed = true;
  2256. }
  2257. }
  2258. treeNode.data.calcTemplate = { calcItems: [] };
  2259. }
  2260. // 定额部分抽取出来,供分摊定额公用。
  2261. innerCalcRation(treeNode, tenderType = tenderTypes.ttCalc) {
  2262. let me = this;
  2263. let fnArr = [];
  2264. // 如果是反算工料机,这里要先保证coe有值。
  2265. if (tenderType == tenderTypes.ttReverseGLJ) {
  2266. let coe = (
  2267. treeNode.data.tender_activeTarget / treeNode.data.tender_activeTotal
  2268. ).toDecimal(decimalObj.process);
  2269. treeNode.data.quantityCoe = {
  2270. labour: coe,
  2271. material: coe,
  2272. machine: coe,
  2273. main: coe,
  2274. equipment: coe,
  2275. };
  2276. }
  2277. calcTools.getGLJList(treeNode, true);
  2278. let nQ = calcTools.uiNodeQty(treeNode);
  2279. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  2280. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  2281. // 量价、工料机类型的定额要求"市场合价"
  2282. if (calcTools.isVP_or_GLJR(treeNode)) {
  2283. let u = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  2284. let t = (u * nQ).toDecimal(decimalObj.ration.totalPrice);
  2285. if (treeNode.data.marketTotalFee != t) {
  2286. treeNode.data.marketTotalFee = t;
  2287. treeNode.changed = true;
  2288. }
  2289. }
  2290. }
  2291. let template = me.compiledTemplates[treeNode.data.programID];
  2292. treeNode.data.calcTemplate = template;
  2293. if (treeNode && template && template.hasCompiled) {
  2294. //2018-08-27 空行的时候,取费专业为空,template也为空,加入template非空判断
  2295. let $CE = executeObj;
  2296. $CE.treeNode = treeNode;
  2297. $CE.template = template;
  2298. calcTools.initFees(treeNode);
  2299. for (let idx of template.compiledSeq) {
  2300. let calcItem = template.calcItems[idx];
  2301. $CE.tempCalcItem = calcItem;
  2302. let feeRate = 100; // 100%
  2303. if (calcItem.feeRate != undefined)
  2304. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  2305. calcItem.unitFee = (
  2306. eval(calcItem.compiledExpr) *
  2307. feeRate *
  2308. 0.01
  2309. ).toDecimal(decimalObj.decimal("unitPrice", treeNode));
  2310. calcItem.totalFee = (calcItem.unitFee * nQ).toDecimal(
  2311. decimalObj.decimal("totalPrice", treeNode)
  2312. );
  2313. let tExpr = analyzer.getCompiledTenderExpr(calcItem.compiledExpr);
  2314. calcItem.tenderUnitFee = (eval(tExpr) * feeRate * 0.01).toDecimal(
  2315. decimalObj.decimal("unitPrice", treeNode)
  2316. );
  2317. calcItem.tenderTotalFee = (calcItem.tenderUnitFee * nTQ).toDecimal(
  2318. decimalObj.decimal("totalPrice", treeNode)
  2319. );
  2320. if (calcItem.fieldName) {
  2321. fnArr.push(calcItem.fieldName);
  2322. calcTools.checkFeeField(treeNode, calcItem);
  2323. }
  2324. }
  2325. if (
  2326. tenderType == tenderTypes.ttReverseRation ||
  2327. tenderType == tenderTypes.ttReverseGLJ
  2328. )
  2329. this.reverseTenderCalc(treeNode, tenderType);
  2330. me.deleteUselessFees(treeNode, fnArr);
  2331. }
  2332. }
  2333. // 存储、刷新零散的多个结点。
  2334. saveNodes(treeNodes, callback) {
  2335. if (treeNodes.length < 1) {
  2336. $.bootstrapLoading.end();
  2337. this.rationMap = null;
  2338. this.pgljMap = null;
  2339. return;
  2340. }
  2341. let me = this;
  2342. let dataArr = [];
  2343. for (let node of treeNodes) {
  2344. if (node.changed) {
  2345. // console.log(node.data.name);
  2346. let data = calcTools.cutNodeForSave(node);
  2347. let newData = { type: node.sourceType, data: data };
  2348. dataArr.push(newData);
  2349. }
  2350. }
  2351. if (dataArr.length < 1) {
  2352. $.bootstrapLoading.end();
  2353. return;
  2354. }
  2355. if (projectObj.project.projectInfo.lastFileVer != VERSION) {
  2356. let data = {
  2357. ID: projectObj.project.ID(),
  2358. lastFileVer: VERSION,
  2359. };
  2360. let newData = { type: "project", data: data };
  2361. dataArr.push(newData);
  2362. }
  2363. $.bootstrapLoading.start();
  2364. let startTime = +new Date();
  2365. me.project.updateNodes(dataArr, function (data) {
  2366. me.rationMap = null;
  2367. me.pgljMap = null;
  2368. if (callback) {
  2369. callback(data);
  2370. }
  2371. for (let node of treeNodes) {
  2372. delete node.changed;
  2373. }
  2374. projectObj.mainController.refreshTreeNode(treeNodes, false, false);
  2375. projectObj.project.projectInfo.lastFileVer = VERSION;
  2376. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。警告:第二个参数千万不能改成3,否则死循环!
  2377. if (activeSubSheetIsCalcProgram())
  2378. calcProgramObj.refreshCalcProgram(
  2379. projectObj.project.mainTree.selected,
  2380. 2
  2381. );
  2382. $.bootstrapLoading.end();
  2383. });
  2384. }
  2385. // 计算本节点及所有会被影响到的节点,如:所有父节点(默认,可选)、公式引用节点(默认,可选)。
  2386. // 修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  2387. calculate(treeNode, calcParents = true, calcFormulas = true, tender) {
  2388. let me = this;
  2389. let changedNodes = [];
  2390. me.innerCalc(treeNode, changedNodes, tender);
  2391. if (treeNode.changed) {
  2392. // 计算父结点
  2393. if (calcParents) {
  2394. let curNode = treeNode.parent;
  2395. if (calcTools.isLockPriceBill(curNode)) curNode = undefined;
  2396. while (curNode) {
  2397. me.innerCalc(curNode, changedNodes, tender);
  2398. curNode = curNode.parent;
  2399. }
  2400. }
  2401. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  2402. if (calcFormulas) {
  2403. me.calcFormulaNodes(changedNodes, tender);
  2404. }
  2405. }
  2406. return changedNodes;
  2407. }
  2408. // 计算并保存一个树节点。(修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点)
  2409. calcAndSave(treeNode, callback, tender) {
  2410. this.calcNodesAndSave([treeNode], callback, tender);
  2411. /* let changedNodes = this.calculate(treeNode, true, true, tender); 统一调用相同的方法
  2412. this.saveNodes(changedNodes, callback);*/
  2413. }
  2414. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  2415. calcAllType.catAll 计算所有树结点 (默认值)
  2416. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  2417. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  2418. (calcAllType.catRations时程序中做了特殊处理,实际上是计算所有树结点!)
  2419. 调价相关参数:
  2420. tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  2421. */
  2422. calcAllNodes(calcType = calcAllType.catAll, tender) {
  2423. let me = this;
  2424. let changedNodes = [];
  2425. function calcNodes(nodes) {
  2426. for (let node of nodes) {
  2427. if (node.children.length > 0) {
  2428. calcNodes(node.children);
  2429. }
  2430. if (calcType == calcAllType.catAll || calcType == node.sourceType) {
  2431. node.calcType = calcTools.getCalcType(node);
  2432. if (node.calcType != treeNodeCalcType.ctCalcBaseValue)
  2433. me.innerCalc(node, changedNodes, tender);
  2434. }
  2435. }
  2436. }
  2437. // calcAllType.catRations 参数情况不会计算父结点(因为父结点是清单),所以这里进行特殊处理。
  2438. if (calcType == calcAllType.catRations) calcType == calcAllType.catAll;
  2439. calcNodes(me.project.mainTree.roots);
  2440. me.calcFormulaNodes(changedNodes, tender);
  2441. if (tender) {
  2442. for (let node of projectObj.project.mainTree.items) {
  2443. this.clearTenderCache(node);
  2444. }
  2445. }
  2446. return changedNodes;
  2447. }
  2448. // tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  2449. calcAllNodesAndSave(calcType = calcAllType.catAll, callback, tender) {
  2450. let changedNodes = this.calcAllNodes(calcType, tender);
  2451. this.saveNodes(changedNodes, callback);
  2452. }
  2453. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  2454. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  2455. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  2456. calcNodesAndSave(nodes, callback, tender) {
  2457. let me = this,
  2458. rationNodes = [],
  2459. billNodes = [],
  2460. leafBills = [],
  2461. allChangedNodes = [];
  2462. for (let node of nodes) {
  2463. if (node.sourceType == ModuleNames.ration) rationNodes.push(node);
  2464. else billNodes.push(node);
  2465. }
  2466. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  2467. for (let ration of rationNodes) {
  2468. me.innerCalc(ration, allChangedNodes, tender);
  2469. let leafBill = ration.parent;
  2470. if (calcTools.isLockPriceBill(leafBill)) leafBill = undefined; // 这里必须打断,否则有循环计算(父变要分摊到子,子变又汇总到父)
  2471. if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  2472. }
  2473. billNodes.merge(leafBills);
  2474. for (let bill of billNodes) {
  2475. let changeBills = me.calculate(bill, true, false, tender);
  2476. allChangedNodes.merge(changeBills);
  2477. }
  2478. me.calcFormulaNodes(allChangedNodes, tender);
  2479. me.saveNodes(allChangedNodes, callback);
  2480. }
  2481. // 计算全部公式项。 (changedArr:将通过本方法后发生改变的节点存入changedArr中)
  2482. calcFormulaNodes(changedArr, tender) {
  2483. let me = this;
  2484. let formulaNodes = cbTools.getFormulaNodes(true);
  2485. if (formulaNodes.length == 0) return;
  2486. for (let formulaNode of formulaNodes) {
  2487. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  2488. projectObj.project.calcBase.calculate(formulaNode, true);
  2489. if (projectObj.project.calcBase.success) {
  2490. // 计算公式结点
  2491. me.innerCalc(formulaNode, changedArr, tender);
  2492. // 计算父结点
  2493. if (formulaNode.changed) {
  2494. let curNode = formulaNode.parent;
  2495. while (curNode) {
  2496. me.innerCalc(curNode, changedArr, tender);
  2497. curNode = curNode.parent;
  2498. }
  2499. }
  2500. }
  2501. }
  2502. }
  2503. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  2504. calcLeafAndSave(treeNode, tender) {
  2505. let me = this;
  2506. if (!calcTools.isLeafBill(treeNode)) return;
  2507. if (treeNode.children && treeNode.children.length > 0) {
  2508. let changedNodes = [];
  2509. for (let child of treeNode.children) {
  2510. me.innerCalc(child, changedNodes);
  2511. }
  2512. let curChangeds = me.calculate(treeNode, true, true, tender);
  2513. changedNodes.merge(curChangeds);
  2514. me.saveNodes(changedNodes);
  2515. }
  2516. }
  2517. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  2518. getTotalFee(baseNodes, excludeNodes, tender) {
  2519. let rst = 0;
  2520. const totalFeeType = tender ? "common.tenderTotalFee" : "common.totalFee";
  2521. function calcNodes(nodes) {
  2522. for (let node of nodes) {
  2523. if (!node) {
  2524. continue;
  2525. }
  2526. if (!excludeNodes.includes(node)) {
  2527. if (
  2528. node.source &&
  2529. node.source.children &&
  2530. node.source.children.length > 0
  2531. ) {
  2532. calcNodes(node.children);
  2533. } else {
  2534. if (node.sourceType == ModuleNames.bills) {
  2535. rst = (rst + calcTools.getFee(node, totalFeeType)).toDecimal(
  2536. decimalObj.decimal("totalPrice", node)
  2537. );
  2538. }
  2539. }
  2540. }
  2541. }
  2542. }
  2543. calcNodes(baseNodes);
  2544. return rst;
  2545. }
  2546. // 税前工程造价 ,adj调价
  2547. getBeforeTaxTotalFee(excludeNodes, tender) {
  2548. let baseNodes = [],
  2549. me = this;
  2550. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  2551. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  2552. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  2553. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  2554. return me.getTotalFee(baseNodes, excludeNodes, tender);
  2555. }
  2556. // 清理调价缓存数据
  2557. clearTenderCache(treeNode) {
  2558. // 这些属性值为什么不定义在一个对象里?因为每次要判断对象是否存在,十分麻烦。不如直接写简单。分散书写,统一处理也很好用。
  2559. if (treeNode.data.tender_activeTotal)
  2560. delete treeNode.data.tender_activeTotal;
  2561. if (treeNode.data.tender_activeTarget)
  2562. delete treeNode.data.tender_activeTarget;
  2563. if (treeNode.data.tender_fullLoad) delete treeNode.data.tender_fullLoad;
  2564. if (treeNode.data.tender_distribute) {
  2565. delete treeNode.data.targetTotalFee;
  2566. delete treeNode.data.targetUnitFee;
  2567. delete treeNode.data.tender_distribute;
  2568. }
  2569. }
  2570. initGljPriceTenderCoe() {
  2571. if (
  2572. projectObj.project.property.tenderSetting &&
  2573. projectObj.project.property.tenderSetting.gljPriceTenderCoe &&
  2574. projectObj.project.property.tenderSetting.gljPriceTenderCoe != 1
  2575. ) {
  2576. projectObj.project.property.tenderSetting.gljPriceTenderCoe = 1; // 修改缓存值,用于计算
  2577. projectObj.project.property.needRestoreGgljPriceTenderCoe = true; // 做个标记,告诉回调函数
  2578. }
  2579. }
  2580. // 反向调价-初始化调价树、清理缓存数据等
  2581. reverseTenderInitDatas() {
  2582. for (let node of tender_obj.tenderTree.items) {
  2583. if (node.data.rationQuantityCoe) node.data.rationQuantityCoe = null;
  2584. let qcObj = node.data.quantityCoe;
  2585. if (qcObj) {
  2586. for (let pn in qcObj) {
  2587. qcObj[pn] = null;
  2588. }
  2589. }
  2590. this.clearTenderCache(node);
  2591. }
  2592. // 反向调价时人材机单价调整系数要归1:因为既可以调量又可以调价,以哪个为基准进行反调?过于复杂,仅以通用的调量逻辑为基准即可满足需求。
  2593. this.initGljPriceTenderCoe();
  2594. }
  2595. // 反向调价简单计算:只计算当前单一结点,由目标金额计算定额量系数或工料机量系数coe,再根据此coe计算定额的调后量、调后合价。
  2596. // ⑴只计算参数指定的一个单一的结点,连父结点、引用结点都不计算。此结点只会是定额、量价、工料机款定额,不会是清单。
  2597. // ⑵执行此方法的前提:已在其它地方把总的目标金额分摊好了,此结点已获取目标金额。
  2598. reverseTenderCalc(treeNode, tender) {
  2599. if (tender == tenderTypes.ttReverseRation) {
  2600. if (
  2601. treeNode.data.feesIndex.common &&
  2602. treeNode.data.feesIndex.common.tenderUnitFee !=
  2603. treeNode.data.feesIndex.common.unitFee
  2604. ) {
  2605. treeNode.data.feesIndex.common.tenderUnitFee =
  2606. treeNode.data.feesIndex.common.unitFee;
  2607. treeNode.changed = true;
  2608. }
  2609. }
  2610. if (!treeNode.data.targetTotalFee) {
  2611. // 没有目标金额、但有输入目标单价的:根据目标单价算出目标金额。
  2612. if (treeNode.data.targetUnitFee) {
  2613. treeNode.data.targetTotalFee = (
  2614. treeNode.data.targetUnitFee * treeNode.data.quantity
  2615. ).toDecimal(decimalObj.decimal("totalPrice", treeNode));
  2616. treeNode.changed = true;
  2617. } else {
  2618. // 既没有目标金额也没有目标单价,此时要初始化使调价合价=原始综合合价,调价单价=原始综合单价。
  2619. if (
  2620. treeNode.data.feesIndex.common &&
  2621. treeNode.data.feesIndex.common.tenderUnitFee !=
  2622. treeNode.data.feesIndex.common.unitFee
  2623. ) {
  2624. treeNode.data.feesIndex.common.tenderUnitFee =
  2625. treeNode.data.feesIndex.common.unitFee;
  2626. treeNode.changed = true;
  2627. }
  2628. if (
  2629. treeNode.data.feesIndex.common &&
  2630. treeNode.data.feesIndex.common.tenderTotalFee !=
  2631. treeNode.data.feesIndex.common.totalFee
  2632. ) {
  2633. treeNode.data.feesIndex.common.tenderTotalFee =
  2634. treeNode.data.feesIndex.common.totalFee;
  2635. treeNode.changed = true;
  2636. }
  2637. return;
  2638. }
  2639. }
  2640. // 经过前面的一通折腾还是没有目标单价的话,那么就要通过目标金额算出来。
  2641. if (
  2642. !treeNode.data.targetUnitFee ||
  2643. parseFloat(treeNode.data.targetUnitFee) == 0
  2644. ) {
  2645. if (calcTools.hasQuantity(treeNode))
  2646. treeNode.data.targetUnitFee = (
  2647. treeNode.data.targetTotalFee / treeNode.data.quantity
  2648. ).toDecimal(decimalObj.decimal("unitPrice", treeNode));
  2649. }
  2650. // 系数=调后单价/调前单价
  2651. let coe = 1;
  2652. if (treeNode.data.feesIndex.common.totalFee != 0)
  2653. coe = (
  2654. treeNode.data.targetUnitFee / treeNode.data.feesIndex.common.unitFee
  2655. ).toDecimal(decimalObj.process);
  2656. // 量价、工料机款式的定额:它们无下挂工料机可调,直接调树结点的消耗量。
  2657. let isVP_RevGLJ =
  2658. tender == tenderTypes.ttReverseGLJ && calcTools.isVP_or_GLJR(treeNode);
  2659. if (tender == tenderTypes.ttReverseRation || isVP_RevGLJ) {
  2660. treeNode.data.tenderQuantity = (treeNode.data.quantity * coe).toDecimal(
  2661. decimalObj.decimal("quantity", treeNode)
  2662. );
  2663. if (treeNode.data.rationQuantityCoe != coe) {
  2664. treeNode.data.rationQuantityCoe = coe;
  2665. treeNode.changed = true;
  2666. }
  2667. if (isVP_RevGLJ) {
  2668. treeNode.data.quantityCoe = {
  2669. labour: 0,
  2670. material: 0,
  2671. machine: 0,
  2672. main: 0,
  2673. equipment: 0,
  2674. };
  2675. treeNode.changed = true;
  2676. }
  2677. let ttf = (
  2678. treeNode.data.tenderQuantity *
  2679. treeNode.data.feesIndex.common.tenderUnitFee
  2680. ).toDecimal(decimalObj.decimal("totalPrice", treeNode));
  2681. if (treeNode.data.feesIndex.common.tenderTotalFee != ttf) {
  2682. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  2683. treeNode.changed = true;
  2684. }
  2685. } else if (tender == tenderTypes.ttReverseGLJ) {
  2686. let qcObj = treeNode.data.quantityCoe;
  2687. if (!qcObj || calcTools.isEmptyObject(qcObj)) {
  2688. treeNode.data.quantityCoe = {
  2689. labour: coe,
  2690. material: coe,
  2691. machine: coe,
  2692. main: coe,
  2693. equipment: coe,
  2694. };
  2695. treeNode.changed = true;
  2696. } else {
  2697. // 这种写法会漏掉属性,导致界面显示不统一。
  2698. /* for (let pn in qcObj){
  2699. if (qcObj[pn] != coe){
  2700. qcObj[pn] = coe;
  2701. treeNode.changed = true;
  2702. }
  2703. };*/
  2704. if (qcObj.labour != coe) {
  2705. qcObj.labour = coe;
  2706. treeNode.changed = true;
  2707. }
  2708. if (qcObj.material != coe) {
  2709. qcObj.material = coe;
  2710. treeNode.changed = true;
  2711. }
  2712. if (qcObj.machine != coe) {
  2713. qcObj.machine = coe;
  2714. treeNode.changed = true;
  2715. }
  2716. if (qcObj.main != coe) {
  2717. qcObj.main = coe;
  2718. treeNode.changed = true;
  2719. }
  2720. if (qcObj.equipment != coe) {
  2721. qcObj.equipment = coe;
  2722. treeNode.changed = true;
  2723. }
  2724. }
  2725. projectObj.project.calcProgram.calculate(
  2726. treeNode,
  2727. false,
  2728. false,
  2729. tenderTypes.ttCalc
  2730. ); // 再正向算
  2731. }
  2732. }
  2733. // 反向调价-分摊前的准备工作。包括:
  2734. // ⑴结点是否满载
  2735. // ⑵若是满载,汇总孩子的金额、目标金额给它
  2736. // ⑶标记此结点的目标金额来自孩子,未来它不往下分摊(汇上来再摊下去无用功)
  2737. prepareForDistribute(treeNode) {
  2738. if (!treeNode) return;
  2739. if (treeNode.firstChild()) this.prepareForDistribute(treeNode.firstChild());
  2740. if (treeNode.children.length == 0) {
  2741. } else {
  2742. let full = true;
  2743. for (let i = 0; i < treeNode.children.length; i++) {
  2744. let child = treeNode.children[i];
  2745. if (
  2746. !child.data.tender_fullLoad &&
  2747. !calcTools.hasTargetTotalFee(child)
  2748. ) {
  2749. full = false;
  2750. break;
  2751. }
  2752. }
  2753. if (full) {
  2754. let total = 0,
  2755. target = 0;
  2756. for (let i = 0; i < treeNode.children.length; i++) {
  2757. let child = treeNode.children[i];
  2758. total = total + parseFloat(child.data.feesIndex["common"].totalFee);
  2759. target = target + parseFloat(child.data.targetTotalFee);
  2760. }
  2761. treeNode.data.tender_activeTotal = total;
  2762. treeNode.data.tender_activeTarget = target;
  2763. treeNode.data.targetTotalFee = target;
  2764. treeNode.data.tender_distribute = 2;
  2765. }
  2766. treeNode.data.tender_fullLoad = full;
  2767. }
  2768. if (treeNode.nextSibling) this.prepareForDistribute(treeNode.nextSibling);
  2769. }
  2770. // 反向调价-分摊目标合价:从父到子往下分摊。
  2771. distributeTargetTotalFee(treeNode) {
  2772. if (!treeNode) return;
  2773. if (treeNode.data.feesIndex && treeNode.data.feesIndex["common"]) {
  2774. // 空清单忽略
  2775. // 默认能够执行到这里时每个节点已经被初始化,缓存已删除
  2776. treeNode.data.tender_activeTotal =
  2777. treeNode.data.feesIndex["common"].totalFee;
  2778. // 开始分摊:只分摊自有目标金额、从父结点分摊到的金额(不分摊子结点汇总上来的金额)
  2779. if (
  2780. calcTools.hasTargetTotalFee(treeNode) &&
  2781. !(
  2782. treeNode.data.tender_distribute &&
  2783. treeNode.data.tender_distribute == 2
  2784. )
  2785. ) {
  2786. treeNode.data.tender_activeTarget = treeNode.data.targetTotalFee;
  2787. // 先把会破坏金额比例关系的孩子排除:1.有目标金额的 2.满载的(孙子全满,没有分摊空间,所以实质上该孩子的金额已被锁死无法分摊)
  2788. for (let i = 0; i < treeNode.children.length; i++) {
  2789. let child = treeNode.children[i];
  2790. if (!child.data.feesIndex || !child.data.feesIndex["common"])
  2791. continue; // 空白行清单、定额
  2792. child.data.tender_activeTotal =
  2793. child.data.feesIndex["common"].totalFee;
  2794. if (calcTools.hasTargetTotalFee(child)) {
  2795. child.data.tender_activeTarget = child.data.targetTotalFee;
  2796. console.log(treeNode.data.tender_activeTotal);
  2797. console.log(child.data.tender_activeTotal);
  2798. treeNode.data.tender_activeTotal =
  2799. treeNode.data.tender_activeTotal - child.data.tender_activeTotal;
  2800. treeNode.data.tender_activeTarget =
  2801. treeNode.data.tender_activeTarget -
  2802. child.data.tender_activeTarget;
  2803. }
  2804. }
  2805. if (treeNode.data.tender_activeTotal != 0) {
  2806. let coe = (
  2807. treeNode.data.tender_activeTarget / treeNode.data.tender_activeTotal
  2808. ).toDecimal(decimalObj.process);
  2809. for (let i = 0; i < treeNode.children.length; i++) {
  2810. let child = treeNode.children[i];
  2811. if (!child.data.feesIndex || !child.data.feesIndex["common"])
  2812. continue; // 空白行清单、定额
  2813. if (!calcTools.hasTargetTotalFee(child)) {
  2814. child.data.tender_activeTarget = (
  2815. coe * child.data.tender_activeTotal
  2816. ).toDecimal(decimalObj.decimal("totalPrice", treeNode));
  2817. child.data.targetTotalFee = child.data.tender_activeTarget;
  2818. child.data.tender_distribute = 1; // 1表示分摊金额来自父结点。2表示分摊金额来自孩子结点。
  2819. }
  2820. }
  2821. }
  2822. }
  2823. }
  2824. if (treeNode.firstChild())
  2825. this.distributeTargetTotalFee(treeNode.firstChild());
  2826. if (treeNode.nextSibling)
  2827. this.distributeTargetTotalFee(treeNode.nextSibling);
  2828. }
  2829. // 强制修改叶子清单的综合单价,
  2830. distributeToRations(treeNode, oldTotlalFee){
  2831. for (let child of treeNode.children){
  2832. let newT = (treeNode.data.feesIndex.common.totalFee * child.data.feesIndex.common.totalFee / oldTotlalFee).toDecimal(decimalObj.ration.totalPrice);
  2833. let newQ = (newT / child.data.feesIndex.common.totalFee).toDecimal(decimalObj.ration.quantity);
  2834. child.data.quantity = newQ;
  2835. child.data.feesIndex.common.totalFee = (newQ * child.data.feesIndex.common.unitFee).toDecimal(decimalObj.ration.totalPrice);
  2836. child.changed = true;
  2837. this.innerCalcRation(child);
  2838. };
  2839. }
  2840. // 反向调价逼近
  2841. reverseTenderApproach(callback, tender) {
  2842. let me = this;
  2843. function root0() {
  2844. return tender_obj.tenderTree.roots[0];
  2845. }
  2846. if (!root0().data.targetTotalFee) return; // 根结点没有指定目标金额时,逼近会带来莫名其妙的结果
  2847. let G_DIGIT = 0.01; // 系数调整步距(0.1最终结果误差大。0.001目标金额与逼前金额差距大时无法有效逼近)
  2848. let MaxDiffValue = 0.1; // 可接受的最大差值。(如果实际差值比这个大:说明列表结点耗尽,无法达到指定精度。公路设为1,建筑设为0.1)
  2849. let times = 300; // 逼近计算的极限次数。正常情况下“单位系数金额”列表中的结点耗尽即退出,这里指定轮数是最后保险阀,防止无限死循环。
  2850. let calcModel = 1; // 计算模式:1 精度优先(差值不接近0不停,直到结点用完或极限次数用完。时间长精度高)2 速度优先(达到指定差值范围即熔断逼近。时间短差值大)
  2851. let diffProp = 0.0001; // 计算模式=2(速度优先)时有效。 通过这个金额比例值计算可接受的最大差值D。差值D = 根结点金额 * diffProp
  2852. let isTest = true; // 测试
  2853. // 按指定的比例获取可接受的差值:如目标金额的万分之一。
  2854. function getPropV(node) {
  2855. let v = parseFloat((node.data.targetTotalFee * diffProp).toFixed(0)); // node.data.feesIndex.common.totalFee
  2856. return Math.max(v, MaxDiffValue);
  2857. }
  2858. // 取根结点的:调后金额跟目标金额差值,看还有多少误差需要处理。
  2859. function getRootDiff() {
  2860. return (
  2861. root0().data.feesIndex.common.tenderTotalFee -
  2862. root0().data.targetTotalFee
  2863. ).toDecimal(3);
  2864. }
  2865. // 生成每单位差值的定额结点列表。
  2866. function getNodeDiffs() {
  2867. let arr = [];
  2868. for (let i = 0; i < tender_obj.tenderTree.items.length; i++) {
  2869. let node = tender_obj.tenderTree.items[i];
  2870. if (calcTools.isInvalidNode(node)) continue;
  2871. // 量价还是要参与,因为它贡献了金额,如果它的金额比重很大,它退出了,会导致其它结点过调。
  2872. // if (calcTools.isRationCategory(node) && (!calcTools.isVP_or_GLJR(node))){
  2873. if (calcTools.isRationCategory(node)) {
  2874. let coe = calcTools.getCoe(node, tender);
  2875. if (coe != 0) {
  2876. let diff = Math.abs(
  2877. node.data.feesIndex.common.tenderTotalFee -
  2878. node.data.feesIndex.common.totalFee
  2879. );
  2880. node.data.tender_diffValuePerCoe = (
  2881. (diff * G_DIGIT) /
  2882. coe
  2883. ).toDecimal(decimalObj.process);
  2884. node.data.tender_rowNo = i + 1; // node在UI上显示的行号
  2885. arr.push(node);
  2886. }
  2887. }
  2888. }
  2889. arr.sort(function sortArr(a, b) {
  2890. return a.data.tender_diffValuePerCoe - b.data.tender_diffValuePerCoe;
  2891. });
  2892. if (isTest) {
  2893. let arr2 = [];
  2894. for (let i = 0; i < arr.length; i++) {
  2895. arr2.push({
  2896. row: arr[i].data.tender_rowNo,
  2897. code: arr[i].data.code + " " + arr[i].data.name,
  2898. diff: arr[i].data.tender_diffValuePerCoe,
  2899. });
  2900. }
  2901. console.log(arr2);
  2902. }
  2903. return arr;
  2904. }
  2905. // 每单位差值的定额结点列表中,取离给定值最近的定额结点。
  2906. function getCloseNode(arr, value) {
  2907. let index = 0;
  2908. let d_value = Number.MAX_VALUE;
  2909. for (let i = 0; i < arr.length; i++) {
  2910. let new_d_value = Math.abs(arr[i].data.tender_diffValuePerCoe - value);
  2911. if (new_d_value <= d_value) {
  2912. if (
  2913. new_d_value === d_value &&
  2914. arr[i].data.tender_diffValuePerCoe <
  2915. arr[index].data.tender_diffValuePerCoe
  2916. ) {
  2917. continue;
  2918. }
  2919. index = i;
  2920. d_value = new_d_value;
  2921. }
  2922. }
  2923. return { idx: index, node: arr[index] };
  2924. }
  2925. // 逼近(单轮)。arr 参考取值列表。返回{type, node, nodeIdx}。
  2926. // type:1正常,2无结点,3结点过调。node:结点。nodeIdx:结点在列表中的索引位置。node.data.tender_rowNo 在UI上的行号。
  2927. function approach(arr) {
  2928. let v = getRootDiff();
  2929. let obj = getCloseNode(arr, Math.abs(v)); // {idx, node} 极端:{0, undefind}
  2930. let closeNode = obj.node;
  2931. if (!closeNode) return { type: 2, node: undefined, nodeIdx: -1 }; // arr 被清空了
  2932. let d = v > 0 ? -G_DIGIT : G_DIGIT;
  2933. let coe = calcTools.getCoe(closeNode, tender);
  2934. if (coe + d < 0) return { type: 3, node: obj.node, nodeIdx: obj.idx }; // 再调的话,系数就变负数了,过调
  2935. if (tender == tenderTypes.ttReverseRation) {
  2936. closeNode.data.tender_previousCoe = closeNode.data.rationQuantityCoe; // tender_previousCoe: 上一次的调整系数,用于撤回
  2937. closeNode.data.rationQuantityCoe = (
  2938. closeNode.data.rationQuantityCoe + d
  2939. ).toDecimal(decimalObj.process);
  2940. } else if (tender == tenderTypes.ttReverseGLJ) {
  2941. if (calcTools.isVP_or_GLJR(closeNode)) {
  2942. closeNode.data.tender_previousCoe = closeNode.data.rationQuantityCoe;
  2943. closeNode.data.rationQuantityCoe = (
  2944. closeNode.data.rationQuantityCoe + d
  2945. ).toDecimal(decimalObj.process);
  2946. } else {
  2947. closeNode.data.tender_previousCoe = closeNode.data.quantityCoe.labour;
  2948. closeNode.data.quantityCoe.labour = (
  2949. closeNode.data.quantityCoe.labour + d
  2950. ).toDecimal(decimalObj.process);
  2951. closeNode.data.quantityCoe.material = (
  2952. closeNode.data.quantityCoe.material + d
  2953. ).toDecimal(decimalObj.process);
  2954. closeNode.data.quantityCoe.machine = (
  2955. closeNode.data.quantityCoe.machine + d
  2956. ).toDecimal(decimalObj.process);
  2957. closeNode.data.quantityCoe.main = (
  2958. closeNode.data.quantityCoe.main + d
  2959. ).toDecimal(decimalObj.process);
  2960. closeNode.data.quantityCoe.equipment = (
  2961. closeNode.data.quantityCoe.equipment + d
  2962. ).toDecimal(decimalObj.process);
  2963. }
  2964. }
  2965. me.calculate(closeNode, true, true, tenderTypes.ttCalc);
  2966. return { type: 1, node: obj.node, nodeIdx: obj.idx };
  2967. }
  2968. // 撤消最后一轮逼近(调过头了,回退一步)
  2969. function undoLastApproach(obj) {
  2970. let closeNode = obj.node;
  2971. let coe = closeNode.data.tender_previousCoe;
  2972. if (tender == tenderTypes.ttReverseRation) {
  2973. closeNode.data.rationQuantityCoe = coe;
  2974. } else if (tender == tenderTypes.ttReverseGLJ) {
  2975. if (calcTools.isVP_or_GLJR(closeNode)) {
  2976. closeNode.data.rationQuantityCoe = coe;
  2977. } else {
  2978. closeNode.data.quantityCoe.labour = coe;
  2979. closeNode.data.quantityCoe.material = coe;
  2980. closeNode.data.quantityCoe.machine = coe;
  2981. closeNode.data.quantityCoe.main = coe;
  2982. closeNode.data.quantityCoe.equipment = coe;
  2983. }
  2984. }
  2985. me.calculate(closeNode, true, true, tenderTypes.ttCalc);
  2986. if (isTest) {
  2987. let _sp = `        `; // 保留空格,打印对齐
  2988. let _node = `[行${obj.node.data.tender_rowNo} 索引${obj.nodeIdx}]`;
  2989. console.log(`${_sp} ${_node} 过调,已回退`);
  2990. }
  2991. }
  2992. let root = tender_obj.tenderTree.roots[0];
  2993. let propV = getPropV(root);
  2994. let vArr = getNodeDiffs();
  2995. let d1 = getRootDiff();
  2996. if (isTest) {
  2997. let _tq = `调前${root.data.feesIndex.common.totalFee}`;
  2998. let _mb = `目标${root.data.targetTotalFee}`;
  2999. let _wbj = `调后无逼近${root.data.feesIndex.common.tenderTotalFee}`;
  3000. let _cz = `差值${d1}`;
  3001. let _js = `轮${times} 系数步距${G_DIGIT}`;
  3002. let _yx =
  3003. calcModel == 2
  3004. ? `速度优先(差比${diffProp} 差域0~${propV})`
  3005. : "精度优先";
  3006. console.log(`${_tq}|${_mb}|${_wbj}|${_cz}|${_yx}|${_js}`);
  3007. }
  3008. // 多轮逼进
  3009. for (let i = 1; i <= times; i++) {
  3010. // 与目标金额差值在1以内,整数部分已相同,结果很棒。很多时候能达到完美的差值0,但不能用0判断,因为金额取小数时有问题。
  3011. if (Math.abs(d1) < MaxDiffValue) break;
  3012. // 速度优先模式下,结果到达差值范围内,即熔断逼近。
  3013. if (calcModel == 2) {
  3014. if (Math.abs(d1) < propV) break;
  3015. }
  3016. let obj = approach(vArr);
  3017. if (obj.type == 2) break; // 结点耗完,列表已清空,无结点可调。
  3018. if (obj.type == 3) {
  3019. // 结点过调,该结点任务已完成,不再参与调节,从列表中清除
  3020. vArr.splice(obj.nodeIdx, 1);
  3021. continue;
  3022. }
  3023. let d2 = getRootDiff();
  3024. if (isTest) {
  3025. let _time = `【第 ${i} 轮】调整`;
  3026. let _node = `[行${obj.node.data.tender_rowNo} 索引${obj.nodeIdx}]`;
  3027. let _coe = `${obj.node.data.tender_previousCoe} → ${calcTools.getCoe(
  3028. obj.node,
  3029. tender
  3030. )}`;
  3031. let _d = `差值${d1} → ${d2}`;
  3032. console.log(`${_time}${_node} ${_coe},${_d}`);
  3033. }
  3034. if (Math.abs(d2) > Math.abs(d1)) {
  3035. // 逼近后差值反而变大,证明此轮调节不合适,回退一步,并将结点从列表清除
  3036. undoLastApproach(obj);
  3037. vArr.splice(obj.nodeIdx, 1);
  3038. } else if (Math.abs(d2) == Math.abs(d1)) {
  3039. // 调前调后差距相同时,会死循环,也从列表中清除
  3040. vArr.splice(obj.nodeIdx, 1);
  3041. } else {
  3042. d1 = d2;
  3043. }
  3044. }
  3045. // 重要注释:到这里,所有系数已处理就绪,最后直接全局正算即可。
  3046. // 到这一步,被处理的定额、及其受影响的父结点、引用结点等均已正算完成(approach方法、undoLastApproach方法),但都未保存。
  3047. // 最后全局正算目的:⑴计算除第一部以外的其它部分 ⑵保存。正算在调用外面完成。
  3048. // this.calcAllNodesAndSave(calcAllType.catAll, callback, tenderTypes.ttCalc);
  3049. }
  3050. setRationMap() {
  3051. if (this.rationMap == null) {
  3052. this.rationMap = {};
  3053. for (let glj of projectObj.project.ration_glj.datas) {
  3054. if (this.rationMap[glj.rationID]) {
  3055. this.rationMap[glj.rationID].push(glj);
  3056. } else {
  3057. this.rationMap[glj.rationID] = [glj];
  3058. }
  3059. }
  3060. }
  3061. if (this.pgljMap == null) {
  3062. this.pgljMap = _.indexBy(
  3063. projectObj.project.projectGLJ.datas.gljList,
  3064. "id"
  3065. );
  3066. }
  3067. }
  3068. getGljArrByRation(ration) {
  3069. if (ration.type == rationType.gljRation) {
  3070. let glj = JSON.parse(JSON.stringify(ration));
  3071. glj.type = glj.subType;
  3072. glj.quantity = 1;
  3073. glj.totalQuantity = parseFloatPlus(ration.quantity);
  3074. return [glj];
  3075. } else {
  3076. if (!this.rationMap) return [];
  3077. let result = this.rationMap[ration.ID];
  3078. if (!result) return [];
  3079. result = gljOprObj.combineWithProjectGlj(
  3080. result,
  3081. false,
  3082. ration,
  3083. this.pgljMap
  3084. );
  3085. return result;
  3086. }
  3087. }
  3088. doTenderCalc(callback) {
  3089. $.bootstrapLoading.start();
  3090. setTimeout(() => {
  3091. let tender = calcTools.getTenderType();
  3092. if (
  3093. tender == tenderTypes.ttReverseGLJ ||
  3094. tender == tenderTypes.ttReverseRation
  3095. ) {
  3096. // 调价计算必须依赖调价树。
  3097. if (!tender_obj.tenderTree) {
  3098. tender_obj.createTree();
  3099. tender_obj.createTreeNodes();
  3100. }
  3101. // 反向调价+逼近原理:
  3102. // 清理调价缓存 → 分摊目标金额(从子往父处理满载、从父往子分摊) → 全局反算 → 逼近 → 全局正算、存储
  3103. this.reverseTenderInitDatas();
  3104. this.prepareForDistribute(tender_obj.tenderTree.roots[0]);
  3105. this.distributeTargetTotalFee(tender_obj.tenderTree.roots[0]);
  3106. this.calcAllNodes(calcAllType.catAll, tender); // 先全局反算:得到每定额的coe、基础调后金额(误差大,需逼近)
  3107. this.reverseTenderApproach(callback, tender); // 逼近上述基础调后金额
  3108. }
  3109. // 全局正算(用的是主树,无需调价树)
  3110. this.calcAllNodesAndSave(
  3111. calcAllType.catAll,
  3112. callback,
  3113. tenderTypes.ttCalc
  3114. );
  3115. });
  3116. }
  3117. }
  3118. // export default analyzer;