calc_program.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. /**
  2. * Created by CSL on 2017-07-19.
  3. * 计算程序。所有定额、清单、父清单的计算都从此入。
  4. */
  5. /* 新版GLD 取消了默认清单模板,所以这里废弃。先留着,预防不时之需。
  6. let defaultBillTemplate = {
  7. ID: 15,
  8. name: "清单公式",
  9. calcItems: [
  10. {
  11. ID: 1,
  12. code: "1",
  13. name: "定额直接费",
  14. dispExpr: "F2+F3+F4",
  15. statement: "人工费+材料费+机械费",
  16. feeRate: null,
  17. memo: ''
  18. },
  19. {
  20. ID: 2,
  21. code: "1.1",
  22. name: "人工费",
  23. dispExpr: "HJ",
  24. statement: "合计",
  25. feeRate: 50,
  26. fieldName: 'labour',
  27. memo: ''
  28. },
  29. {
  30. ID: 3,
  31. code: "1.2",
  32. name: "材料费",
  33. dispExpr: "HJ",
  34. statement: "合计",
  35. feeRate: 30,
  36. fieldName: 'material',
  37. memo: ''
  38. },
  39. {
  40. ID: 4,
  41. code: "1.3",
  42. name: "机械费",
  43. dispExpr: "HJ",
  44. statement: "合计",
  45. feeRate: 20,
  46. fieldName: 'machine',
  47. memo: ''
  48. },
  49. {
  50. ID: 5,
  51. code: "2",
  52. name: "企业管理费",
  53. dispExpr: "F1",
  54. statement: "定额直接费",
  55. feeRate: null,
  56. fieldName: 'manage',
  57. memo: ''
  58. },
  59. {
  60. ID: 6,
  61. code: "3",
  62. name: "利润",
  63. dispExpr: "F1",
  64. statement: "定额直接费",
  65. feeRate: null,
  66. fieldName: 'profit',
  67. memo: ''
  68. },
  69. {
  70. ID: 7,
  71. code: "4",
  72. name: "风险费用",
  73. dispExpr: "F1",
  74. statement: "定额直接费",
  75. feeRate: null,
  76. fieldName: 'risk',
  77. memo: ''
  78. },
  79. {
  80. ID: 8,
  81. code: "5",
  82. name: "综合单价",
  83. dispExpr: "F1+F5+F6+F7",
  84. statement: "定额直接费+企业管理费+利润+风险费用",
  85. feeRate: null,
  86. fieldName: 'common',
  87. memo: ''
  88. }
  89. ]
  90. };*/
  91. let calcTools = {
  92. getNodeByFlag: function (flag) {
  93. let bill = cbTools.findBill(flag);
  94. if (bill) return this.getNodeByID(bill.ID)
  95. else return null;
  96. },
  97. getNodeByID: function (ID){
  98. return cbTools.getNodeByID(ID);
  99. },
  100. isBill: function(treeNode){
  101. return treeNode.sourceType === ModuleNames.bills;
  102. },
  103. isLeafBill: function(treeNode){
  104. return treeNode.sourceType === ModuleNames.bills &&
  105. treeNode.source.children &&
  106. treeNode.source.children.length === 0;
  107. },
  108. isNullBill: function (treeNode) {
  109. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  110. },
  111. isTotalCostBill: function (treeNode) {
  112. return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
  113. treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
  114. },
  115. isRationCategory: function(treeNode){
  116. return treeNode.sourceType === ModuleNames.ration;
  117. },
  118. isRationItem: function(treeNode){
  119. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.ration;
  120. },
  121. isVolumePrice: function (treeNode) {
  122. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.volumePrice;
  123. },
  124. isGljRation: function (treeNode) {
  125. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.gljRation;
  126. },
  127. initFees: function (treeNode){
  128. if (!treeNode.data.fees) {
  129. treeNode.data.fees = [];
  130. treeNode.data.feesIndex = {};
  131. treeNode.changed = true;
  132. }
  133. else if (!treeNode.data.feesIndex){
  134. treeNode.data.feesIndex = {};
  135. for (let fee of treeNode.data.fees){
  136. treeNode.data.feesIndex[fee.fieldName] = fee;
  137. };
  138. treeNode.changed = true;
  139. };
  140. },
  141. initFeeField: function (treeNode, fieldName){
  142. this.initFees(treeNode);
  143. if (!treeNode.data.feesIndex[fieldName]) {
  144. let fee = {
  145. 'fieldName': fieldName,
  146. 'unitFee': 0,
  147. 'totalFee': 0,
  148. 'tenderUnitFee': 0,
  149. 'tenderTotalFee': 0
  150. };
  151. treeNode.data.fees.push(fee);
  152. treeNode.data.feesIndex[fieldName] = fee;
  153. treeNode.changed = true;
  154. };
  155. },
  156. checkFeeField: function (treeNode, feeObj){
  157. if (!feeObj) return;
  158. if (feeObj.fieldName == '') return;
  159. // 初始化前先拦截末定义的情况
  160. if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
  161. if (feeObj.unitFee == 0 && feeObj.totalFee == 0) return;
  162. }
  163. this.initFeeField(treeNode, feeObj.fieldName);
  164. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  165. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  166. treeNode.changed = true;
  167. };
  168. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  169. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  170. treeNode.changed = true;
  171. };
  172. },
  173. initSummaryFee: function (treeNode){
  174. if (!treeNode.data.summaryFees){
  175. treeNode.data.summaryFees = {
  176. totalFee: 0,
  177. estimateFee: 0,
  178. safetyFee: 0,
  179. chargeFee: 0
  180. };
  181. treeNode.changed = true;
  182. };
  183. },
  184. // 参数fieldName值: 'common.totalFee'、'equipment.unitFee'
  185. getFee: function (treeNode, fieldName) {
  186. if (!treeNode) return 0;
  187. let ns = fieldName.split(".");
  188. if (ns.length != 2)
  189. return 0
  190. else if (treeNode.data.feesIndex && treeNode.data.feesIndex[ns[0]] && treeNode.data.feesIndex[ns[0]][ns[1]])
  191. return parseFloat(treeNode.data.feesIndex[ns[0]][ns[1]])
  192. else
  193. return 0;
  194. },
  195. rationBaseFee: function (treeNode, gljTypes, priceType){
  196. if (!treeNode.data.gljList) return 0;
  197. let me = this, result = 0;
  198. let price = 0, temp = 0, temp2 = 0;
  199. for (let glj of treeNode.data.gljList) {
  200. if (gljTypes.indexOf(glj.type) >= 0) {
  201. /* if (priceType == priceTypes.ptDiffPrice){
  202. let aprice = me.uiGLJPrice(glj["adjustPrice"]);
  203. let mprice = me.uiGLJPrice(glj["marketPrice"]);
  204. temp = (me.uiGLJQty(glj["quantity"]) * mprice).toDecimal(decimalObj.process) - (me.uiGLJQty(glj["quantity"]) * aprice).toDecimal(decimalObj.process);
  205. temp = temp.toDecimal(decimalObj.process);
  206. }*/
  207. if (priceType == priceTypes.ptDiffPrice){
  208. let aprice = me.uiGLJPrice(glj["adjustPrice"]);
  209. let mprice = me.uiGLJPrice(glj["marketPrice"]);
  210. if (aprice != mprice){
  211. temp = (temp + (me.uiGLJQty(glj["quantity"]) * mprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  212. temp2 = (temp2 + (me.uiGLJQty(glj["quantity"]) * aprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  213. }
  214. }
  215. else {
  216. if (priceType == priceTypes.ptBasePrice){ price = me.uiGLJPrice(glj["basePrice"]);}
  217. else if (priceType == priceTypes.ptAdjustPrice){price = me.uiGLJPrice(glj["adjustPrice"]);}
  218. else if (priceType == priceTypes.ptMarketPrice){price = me.uiGLJPrice(glj["marketPrice"]);}
  219. temp = (me.uiGLJQty(glj["quantity"]) * price).toDecimal(decimalObj.process);
  220. result = (result + temp).toDecimal(decimalObj.process);
  221. };
  222. };
  223. };
  224. if (priceType == priceTypes.ptDiffPrice){
  225. result = (temp.toDecimal(decimalObj.ration.unitPrice) - temp2.toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  226. }
  227. else{
  228. result = result.toDecimal(decimalObj.ration.unitPrice);
  229. };
  230. return result;
  231. },
  232. machineLabourFee: function (gljArr) {
  233. if (!gljArr) return 0;
  234. let result = 0;
  235. for (let glj of gljArr) {
  236. if (glj.type == gljType.GENERAL_MACHINE) {
  237. // 获取机械组成物
  238. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  239. if (!mds) mds = [];
  240. let mdSum = 0;
  241. for (let md of mds) {
  242. if (md.type == gljType.MACHINE_LABOUR) {
  243. let q = md["consumption"] ? md["consumption"] : 0;
  244. let p = md["basePrice"] ? md["basePrice"] : 0;
  245. mdSum = mdSum + (q * p).toDecimal(decimalObj.ration.unitFee);
  246. mdSum = (mdSum).toDecimal(decimalObj.ration.unitFee);
  247. }
  248. }
  249. result = result + (glj["quantity"] * mdSum).toDecimal(decimalObj.ration.unitFee);
  250. result = (result).toDecimal(decimalObj.ration.unitFee);
  251. }
  252. }
  253. return result;
  254. },
  255. // 父清单暂估费的汇总计算走计算程序逻辑,不在这里。这里的小数取舍比较复杂,必须严格遵循需求,不能随意改动,否则计算结果会有误差:如差1分钱。
  256. estimateFee: function (treeNode, isBase = false){
  257. let me = this, sumU = 0, sumT = 0;
  258. // 总造价暂估费
  259. if (me.isTotalCostBill(treeNode)){
  260. let nodes = projectObj.project.mainTree.roots;
  261. for (let node of nodes){
  262. if (me.isTotalCostBill(node)) break;
  263. let eU = 0, eT = 0;
  264. if (node.data.feesIndex && node.data.feesIndex.estimate){
  265. eU = node.data.feesIndex.estimate.unitFee;
  266. eT = node.data.feesIndex.estimate.totalFee;
  267. }
  268. else {
  269. eU = 0, eT = 0;
  270. };
  271. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  272. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  273. };
  274. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  275. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  276. }
  277. else{
  278. if (!treeNode.data.gljList) return;
  279. // 单价
  280. for (let glj of treeNode.data.gljList) {
  281. if (!allMaterialTypes.includes(glj.type)) continue;
  282. if (glj.isEstimate){
  283. sumU = sumU + (me.uiGLJQty(glj.quantity) * me.uiGLJPrice(glj.marketPrice)).toDecimal(decimalObj.process);
  284. sumU = sumU.toDecimal(decimalObj.process);
  285. }
  286. else{ // 组成物
  287. if (!compositionTypes.includes(glj.type)) continue;
  288. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  289. if (!mds) mds = [];
  290. for (let md of mds){
  291. if (!md.isEstimate) continue;
  292. let mdU = (me.uiGLJQty(md.consumption) * me.uiGLJPrice(md.marketPrice)).toDecimal(decimalObj.glj.unitPrice);
  293. sumU = sumU + (mdU * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.process);
  294. sumU = sumU.toDecimal(decimalObj.process);
  295. }
  296. }
  297. };
  298. sumU = sumU.toDecimal(decimalObj.bills.unitPrice);
  299. if (isBase) return sumU;
  300. // 合价:数量要先累计,然后乘单价,最后汇总
  301. let isGatherEstimate = (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial);
  302. if (isGatherEstimate){
  303. let GLJObjs = [];
  304. let rq = me.uiNodeQty(treeNode);
  305. for (let glj of treeNode.data.gljList) {
  306. if (!allMaterialTypes.includes(glj.type)) continue;
  307. if (glj.isEstimate){
  308. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type,
  309. quantity: (rq * glj.quantity).toDecimal(decimalObj.process),
  310. marketPrice: glj.marketPrice});
  311. }
  312. else{ // 组成物
  313. if (!compositionTypes.includes(glj.type)) continue;
  314. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  315. if (!mds) mds = [];
  316. for (let md of mds){
  317. if (md.isEstimate){
  318. let isExist = false;
  319. let totalQ = (rq * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.glj.quantity);
  320. let mdQ = (totalQ * me.uiGLJQty(md.consumption)).toDecimal(decimalObj.process);
  321. for (let obj of GLJObjs){
  322. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  323. isExist = true;
  324. obj.quantity = (obj.quantity + mdQ).toDecimal(decimalObj.glj.quantity);
  325. break;
  326. }
  327. };
  328. if (!isExist)
  329. GLJObjs.push({code: md.code, name: md.name, specs: md.specs, unit: md.unit, type: md.type,
  330. quantity: mdQ, marketPrice: md.marketPrice});
  331. }
  332. }
  333. }
  334. };
  335. for (let obj of GLJObjs){
  336. let t = (me.uiGLJQty(obj.quantity) * me.uiGLJPrice(obj.marketPrice)).toDecimal(decimalObj.bills.totalPrice);
  337. sumT = sumT + t;
  338. sumT = sumT.toDecimal(decimalObj.bills.totalPrice);
  339. };
  340. sumT = sumT.toDecimal(decimalObj.bills.totalPrice);
  341. }
  342. else
  343. sumT = (me.uiNodeQty(treeNode) * sumU).toDecimal(decimalObj.bills.totalPrice);
  344. };
  345. me.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
  346. },
  347. marketPriceToBase: function (treeNode, baseName) {
  348. if (treeNode.data.type != rationType.volumePrice && treeNode.data.type != rationType.gljRation) return;
  349. let result = 0, me = this;
  350. if (
  351. (treeNode.data.subType === gljType.LABOUR && baseName === calcBaseNames.DEJJRGF) ||
  352. (baseMaterialTypes.includes(treeNode.data.subType) && baseName === calcBaseNames.DEJJCLF) ||
  353. (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.DEJJJXF) ||
  354. (treeNode.data.subType === gljType.MAIN_MATERIAL && baseName === calcBaseNames.ZCF) ||
  355. (treeNode.data.subType === gljType.EQUIPMENT && baseName === calcBaseNames.SBF)) {
  356. if (treeNode.data.type == rationType.volumePrice)
  357. result = treeNode.data.marketUnitFee ? parseFloat(treeNode.data.marketUnitFee).toDecimal(decimalObj.ration.unitPrice) : 0
  358. else if (treeNode.data.type == rationType.gljRation)
  359. // result = treeNode.data.basePrice ? parseFloat(treeNode.data.basePrice).toDecimal(decimalObj.ration.unitPrice) : 0;
  360. // 这里因为是算基数所以要取基价,但不能直接取basePrice,受限于项目属性的三个选项。
  361. result = gljOprObj.getBasePrice(treeNode);
  362. }
  363. else if (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.DEJJJSRGF) {
  364. let glj = {
  365. 'code': treeNode.data.code,
  366. 'name': treeNode.data.name,
  367. 'specs': treeNode.data.specs,
  368. 'unit': treeNode.data.unit,
  369. 'quantity': 1,
  370. 'type': treeNode.data.subType // 注意:这里要取subType
  371. };
  372. result = me.machineLabourFee([glj]);
  373. }
  374. else if (
  375. (treeNode.data.type == rationType.gljRation) &&
  376. ((treeNode.data.subType === gljType.LABOUR && baseName === calcBaseNames.RGFJC) ||
  377. (baseMaterialTypes.includes(treeNode.data.subType) && baseName === calcBaseNames.CLFJC) ||
  378. (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.JXFJC))
  379. ) {
  380. let aprice = me.uiGLJPrice(treeNode.data.adjustPrice);
  381. let mprice = me.uiGLJPrice(treeNode.data.marketUnitFee);
  382. result = (mprice - aprice).toDecimal(decimalObj.ration.unitPrice);
  383. }
  384. return result;
  385. },
  386. partASupplyFee: function (treeNode, baseName) {
  387. if (!treeNode.data.gljList) return 0;
  388. let projectGLJ = projectObj.project.projectGLJ;
  389. let supplyT = [];
  390. if (baseName.includes('甲供'))
  391. supplyT = [supplyType.BFJG, supplyType.WQJG] // 字段中存储的是汉字、数字混杂!
  392. else if (baseName.includes('甲定'))
  393. supplyT = [supplyType.JDYG];
  394. let gljT = [], compT = [];
  395. if (baseName == calcBaseNames.JGDEJJRGF || baseName == calcBaseNames.JDDEJJRGF){
  396. gljT = [gljType.LABOUR];
  397. }
  398. else if (baseName == calcBaseNames.JGDEJJCLF || baseName == calcBaseNames.JDDEJJCLF){
  399. gljT = baseMaterialTypes;
  400. compT = compositionTypes;
  401. }
  402. else if (baseName == calcBaseNames.JGDEJJJXF || baseName == calcBaseNames.JDDEJJJXF){
  403. gljT = baseMachineTypes;
  404. compT = [gljType.GENERAL_MACHINE];
  405. }
  406. else if (baseName == calcBaseNames.JGZCF || baseName == calcBaseNames.JDZCF){
  407. gljT = [gljType.MAIN_MATERIAL];
  408. compT = [gljType.MAIN_MATERIAL];
  409. }
  410. else if (baseName == calcBaseNames.JGSBF || baseName == calcBaseNames.JDSBF){
  411. gljT = [gljType.EQUIPMENT];
  412. };
  413. // alert(JSON.stringify(projectGLJ.testGLJs()));
  414. let supplyGLJs = projectGLJ.getGLJsBySupply(supplyT, gljT);
  415. if (supplyGLJs.length == 0) return 0;
  416. let supplyGLJsIdx = {};
  417. for (let sglj of supplyGLJs){supplyGLJsIdx[sglj.id] = sglj};
  418. function isSupply(composition, supplies) {
  419. for (let supply of supplies){
  420. if(supply.code == composition.code && supply.name == composition.name && supply.unit == composition.unit &&
  421. supply.specs == composition.specs && supply.type == composition.type ){
  422. composition.basePrice = supply.unit_price.base_price;
  423. composition.supply = supply.supply;
  424. composition.supplyX = 1;
  425. if (composition.supply == supplyType.BFJG){
  426. let Q = supply.quantity ? supply.quantity : 1;
  427. composition.supplyX = supply.supply_quantity / Q;
  428. }
  429. return true;
  430. }
  431. };
  432. return false;
  433. };
  434. let sum = 0;
  435. for (let glj of treeNode.data.gljList){
  436. let X = 1; // 部分甲供系数(默认1,即完全甲供)
  437. let tempSGLJ = supplyGLJsIdx[glj.projectGLJID];
  438. // 当前材料是甲供材料:①普通 ②商品硂等不计组成物的母体材料
  439. if (tempSGLJ) {
  440. // 处理部分甲供
  441. if (baseName.includes('甲供') && (tempSGLJ.supply == supplyType.BFJG)){
  442. let Q = tempSGLJ.quantity ? tempSGLJ.quantity : 1;
  443. X = tempSGLJ.supply_quantity / Q;
  444. }
  445. sum = (sum + glj.basePrice * glj.quantity * X).toDecimal(decimalObj.process);
  446. }
  447. else{ // 当前材料不是甲供材料
  448. if (compT.includes(glj.type)) { // 混凝土等。组成物的母体,母体如果有组成物,则母体无法作为甲供材料,无法设置,此时要看其组成物是否是甲供材料;母体如果没有组成物,则母体有可能成为甲供材料。
  449. let pGLJ = projectGLJ.getDataByID(glj.projectGLJID);
  450. let compositions = pGLJ.ratio_data; // 组成物明细
  451. if (compositions.length > 0){
  452. for (let c of compositions){
  453. if (isSupply(c, supplyGLJs)) {
  454. X = 1;
  455. if (baseName.includes('甲供') && (c.supply == supplyType.BFJG)){
  456. X = c.supplyX;
  457. };
  458. sum = (sum + c.basePrice * c.consumption * glj.quantity * X).toDecimal(decimalObj.process);
  459. }
  460. }
  461. };
  462. }
  463. };
  464. };
  465. sum = sum.toDecimal(decimalObj.ration.unitPrice);
  466. return sum;
  467. },
  468. getCalcType: function (treeNode) {
  469. if (this.isRationCategory(treeNode)){
  470. return treeNodeCalcType.ctRationCalcProgram;
  471. }
  472. else if (this.isNullBill(treeNode)){
  473. return treeNodeCalcType.ctNull;
  474. }
  475. else if (this.isLeafBill(treeNode)) {
  476. if (treeNode.children && treeNode.children.length > 0){
  477. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  478. if (projectObj.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  479. return treeNodeCalcType.ctBillCalcProgram;
  480. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  481. return treeNodeCalcType.ctGatherRationsFees;
  482. }
  483. else{ // 公式计算
  484. return treeNodeCalcType.ctCalcBaseValue;
  485. };
  486. }
  487. else if (this.isBill(treeNode)) { // 父清单:汇总子清单的费用类别
  488. return treeNodeCalcType.ctGatherBillsFees;
  489. }
  490. else {
  491. return treeNodeCalcType.ctRationCalcProgram;
  492. };
  493. },
  494. cutNodeForSave(treeNode){
  495. let me = this;
  496. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  497. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  498. let data = {
  499. projectID: projectObj.project.ID(),
  500. ID: treeNode.data.ID,
  501. unit: treeNode.data.unit, //对清单来说,改变单位,工程量精度会跟着改变从而影响计算。
  502. subType: treeNode.data.subType,
  503. quantity: treeNode.data.quantity,
  504. calcBase: treeNode.data.calcBase,
  505. calcBaseValue: treeNode.data.calcBaseValue,
  506. programID: treeNode.data.programID,
  507. marketUnitFee: treeNode.data.marketUnitFee,
  508. marketTotalFee: treeNode.data.marketTotalFee,
  509. fees: treeNode.data.fees,
  510. isFromDetail:treeNode.data.isFromDetail,
  511. feeRate: treeNode.data.feeRate,
  512. feeRateID: treeNode.data.feeRateID,
  513. contain: treeNode.data.contain,
  514. quantityEXP: treeNode.data.quantityEXP,
  515. summaryFees: treeNode.data.summaryFees
  516. };
  517. // 定额大类
  518. if (me.isRationCategory(treeNode)) {
  519. data.isSubcontract = treeNode.data.isSubcontract;
  520. //定额类型的工料机做特殊处理
  521. if(me.isGljRation(treeNode)){
  522. data.code = treeNode.data.code;
  523. data.projectGLJID = treeNode.data.projectGLJID;
  524. delete data.marketUnitFee;
  525. };
  526. };
  527. // 优化掉 undefined 属性
  528. data = JSON.parse(JSON.stringify(data));
  529. return data;
  530. },
  531. uiNodeQty: function (treeNode){
  532. return parseFloatPlus(treeNode.data.quantity).toDecimal(decimalObj.decimal("quantity", treeNode));
  533. },
  534. // 界面显示的工料机价格,包括定额价、市场价等。参数 price 传入一个普通的价格数值即可。
  535. uiGLJPrice: function (price){
  536. if (price)
  537. return parseFloat(price).toDecimal(decimalObj.glj.unitPrice)
  538. else return 0;
  539. },
  540. // 界面显示的工料机数量。参数 quantity 传入一个普通的数量数值即可。
  541. uiGLJQty: function (quantity){
  542. if (quantity)
  543. return parseFloat(quantity).toDecimal(decimalObj.glj.quantity)
  544. else return 0;
  545. },
  546. getRationsByProjectGLJ(PGLJID){
  547. let rationIDs = [];
  548. let RGs = projectObj.project.ration_glj.datas;
  549. for (let rg of RGs){
  550. if (rg.projectGLJID == PGLJID){
  551. rationIDs.push(rg.rationID);
  552. }
  553. };
  554. let rationNodes = [];
  555. let nodes = projectObj.project.mainTree.nodes;
  556. for (let rID of rationIDs){
  557. rationNodes.push(nodes['id_' + rID]);
  558. };
  559. // 工料机形式的定额
  560. let items = projectObj.project.mainTree.items;
  561. for (let item of items){
  562. if (item.data.projectGLJID == PGLJID)
  563. rationNodes.push(item);
  564. };
  565. return rationNodes;
  566. },
  567. getNodesByProgramID(programID){
  568. let discreteNodes = [];
  569. let nodes = projectObj.project.mainTree.items;
  570. for (let node of nodes){
  571. if (node.data.programID == programID)
  572. discreteNodes.push(node);
  573. };
  574. return discreteNodes;
  575. }
  576. };
  577. const calcBaseNames = {
  578. DEJJRGF: '定额基价人工费',
  579. DEJJCLF: '定额基价材料费',
  580. DEJJJXF: '定额基价机械费',
  581. DEJJJSRGF: '定额基价机上人工费',
  582. RGFJC: '人工费价差',
  583. CLFJC: '材料费价差',
  584. JXFJC: '机械费价差',
  585. ZCF: '主材费',
  586. SBF: '设备费',
  587. RGGR: '人工工日',
  588. JGDEJJRGF: '甲供定额基价人工费',
  589. JGDEJJCLF: '甲供定额基价材料费',
  590. JGDEJJJXF: '甲供定额基价机械费',
  591. JGZCF: '甲供主材费',
  592. JGSBF: '甲供设备费',
  593. JDDEJJRGF: '甲定定额基价人工费',
  594. JDDEJJCLF: '甲定定额基价材料费',
  595. JDDEJJJXF: '甲定定额基价机械费',
  596. JDZCF: '甲定主材费',
  597. JDSBF: '甲定设备费',
  598. ZGCLF: '暂估材料费',
  599. FBDEJJRGF: '分包定额基价人工费',
  600. FBDEJJCLF: '分包定额基价材料费',
  601. FBDEJJJXF: '分包定额基价机械费',
  602. FBZCF: '分包主材费',
  603. FBSBF: '分包设备费',
  604. FBRGGR: '分包人工工日'
  605. };
  606. const rationCalcBases = {
  607. '定额基价人工费': function (node) {
  608. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice);
  609. },
  610. '定额基价材料费': function (node) {
  611. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice);
  612. },
  613. '定额基价机械费': function (node) {
  614. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice);
  615. },
  616. '定额基价机上人工费': function (node) {
  617. return calcTools.machineLabourFee(node.data.gljList);
  618. },
  619. '人工费价差': function (node) {
  620. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptDiffPrice);
  621. },
  622. '材料费价差': function (node) {
  623. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptDiffPrice);
  624. },
  625. '机械费价差': function (node) {
  626. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptDiffPrice);
  627. },
  628. '主材费': function (node) {
  629. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptMarketPrice);
  630. },
  631. '设备费': function (node) {
  632. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptMarketPrice);
  633. },
  634. '人工工日': function (node) {
  635. if (!node.data.gljList) return 0;
  636. let rst = 0;
  637. for (let glj of node.data.gljList) {
  638. if (glj.type == gljType.LABOUR) {
  639. rst = rst + (calcTools.uiGLJQty(glj["quantity"]) * calcTools.uiNodeQty(node)).toDecimal(decimalObj.process);
  640. rst = rst.toDecimal(decimalObj.process);
  641. }
  642. };
  643. return rst.toDecimal(decimalObj.glj.quantity);
  644. },
  645. '甲供定额基价人工费': function (node) {
  646. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJRGF);
  647. },
  648. '甲供定额基价材料费': function (node) {
  649. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJCLF);
  650. },
  651. '甲供定额基价机械费': function (node) {
  652. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJJXF);
  653. },
  654. '甲供主材费': function (node) {
  655. return calcTools.partASupplyFee(node, calcBaseNames.JGZCF);
  656. },
  657. '甲供设备费': function (node) {
  658. return calcTools.partASupplyFee(node, calcBaseNames.JGSBF);
  659. },
  660. '甲定定额基价人工费': function (node) {
  661. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJRGF);
  662. },
  663. '甲定定额基价材料费': function (node) {
  664. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJCLF);
  665. },
  666. '甲定定额基价机械费': function (node) {
  667. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJJXF);
  668. },
  669. '甲定主材费': function (node) {
  670. return calcTools.partASupplyFee(node, calcBaseNames.JDZCF);
  671. },
  672. '甲定设备费': function (node) {
  673. return calcTools.partASupplyFee(node, calcBaseNames.JDSBF);
  674. },
  675. '暂估材料费': function (node) {
  676. return calcTools.estimateFee(node, true);
  677. },
  678. '分包定额基价人工费': function (node) {
  679. if (node.data.isSubcontract) return this.定额基价人工费(node)
  680. else return 0;
  681. },
  682. '分包定额基价材料费': function (node) {
  683. if (node.data.isSubcontract) return this.定额基价材料费(node)
  684. else return 0;
  685. },
  686. '分包定额基价机械费': function (node) {
  687. if (node.data.isSubcontract) return this.定额基价机械费(node)
  688. else return 0;
  689. },
  690. '分包主材费': function (node) {
  691. if (node.data.isSubcontract) return this.主材费(node)
  692. else return 0;
  693. },
  694. '分包设备费': function (node) {
  695. if (node.data.isSubcontract) return this.设备费(node)
  696. else return 0;
  697. },
  698. '分包人工工日': function (node) {
  699. if (node.data.isSubcontract) return this.人工工日(node)
  700. else return 0;
  701. }
  702. };
  703. let analyzer = {
  704. standard: function(expr){
  705. let str = expr;
  706. str = str.replace(/\s/g, ""); // 去空格、去中文空格
  707. str = str.replace(/(/g, "("); // 中文括号"("换成英文括号"("
  708. str = str.replace(/)/g, ")"); // 中文括号")"换成英文括号")"
  709. str = str.replace(/f/g, "F"); // f换成F
  710. str = str.replace(/l/g, "L"); // l换成L
  711. return str;
  712. },
  713. getFArr: function (expr) {
  714. let pattF = new RegExp(/F\d+/gi);
  715. let arrF = expr.match(pattF);
  716. return arrF ? arrF : [];
  717. },
  718. getAtIDArr: function (expr) { // ['@1','@2']
  719. let patt = new RegExp(/@\d+/gi);
  720. let arr = expr.match(patt);
  721. return arr ? arr : [];
  722. },
  723. getBaseArr: function (expr) {
  724. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  725. let arrBase = expr.match(pattBase);
  726. return arrBase ? arrBase : [];
  727. },
  728. getID: function (FName, template) { // F13 → 4
  729. let idx = FName.slice(1) - 1;
  730. let id = template.calcItems[idx] ? template.calcItems[idx].ID : null;
  731. return id;
  732. },
  733. getFName: function (atID, template) { // @3 → F7
  734. for (var i = 0; i < template.calcItems.length; i++) {
  735. if (template.calcItems[i].ID == atID.slice(1)) {
  736. return 'F'+ (i + 1);
  737. }
  738. }
  739. return null;
  740. },
  741. getItem: function (FName, template){ // F3 → calcItems[2] → {Object}
  742. let idx = FName.slice(1) - 1;
  743. return template.calcItems[idx];
  744. },
  745. isCycleCalc: function (expression, ID, template) { // 这里判断expression,是ID引用: @5+@6
  746. if (expression.includes(`@${ID}`))
  747. return true;
  748. let atIDs = analyzer.getAtIDArr(expression);
  749. for (let atID of atIDs) {
  750. let item = template.compiledCalcItems[atID.slice(1)];
  751. if (analyzer.isCycleCalc(item.expression, ID, template))
  752. return true;
  753. }
  754. return false;
  755. },
  756. isLegal: function (dispExpr, itemID, template) { // 检测包括:无效字符、基数是否中括号、基数是否定义、行引用、循环计算
  757. function testValue(expr){
  758. try {
  759. expr = expr.replace(/\[[\u4E00-\u9FA5]+\]/gi, '0');
  760. expr = expr.replace(/@\d+/gi, '0');
  761. if (expr.includes('00'))
  762. return false;
  763. eval(expr);
  764. return true;
  765. }
  766. catch (err) {
  767. return false;
  768. }
  769. };
  770. let me = analyzer;
  771. let expr = me.standard(dispExpr);
  772. let invalidChars = /[^0-9\u4e00-\u9fa5\+\-\*\/\(\)\.\[\]FL%]/g;
  773. if (invalidChars.test(expr)){
  774. hintBox.infoBox('错误提示',`表达式中含有${hintBox.font('无效字符')}!`,1);
  775. return false;
  776. };
  777. let i = expr.search(/\df/ig); // 4F3
  778. let j = expr.search(/\d\[/ig); // 4[定额基价人工费]
  779. if (i > -1 || j > -1){
  780. hintBox.infoBox('错误提示', `表达式中${hintBox.font('缺少运算符')}!`, 1);
  781. return false;
  782. }
  783. let pattCn = new RegExp(/[\u4E00-\u9FA5]+/gi);
  784. let arrCn = expr.match(pattCn);
  785. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  786. let arrBase = expr.match(pattBase);
  787. if (arrCn && !arrBase){
  788. hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  789. return false;
  790. };
  791. if (arrCn && arrBase && (arrCn.length != arrBase.length)){
  792. for (let cn of arrCn){
  793. let tempBase = `[${cn}]`;
  794. if (!arrBase.includes(tempBase)){
  795. hintBox.infoBox('错误提示', `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`, 1);
  796. return false;
  797. }
  798. };
  799. // 这里要加一个保险。因为上面的 for 循环在“主材费 + [主材费]” 情况下有Bug
  800. hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  801. return false;
  802. };
  803. if (arrBase){
  804. for (let base of arrBase){
  805. let baseName = base.slice(1, -1);
  806. if (!rationCalcBases[baseName]){
  807. hintBox.infoBox('错误提示', `定额基数${hintBox.font('[' +baseName + ']')}末定义!`, 1);
  808. return false;
  809. }
  810. };
  811. };
  812. let arrF = me.getFArr(expr);
  813. for (let F of arrF){
  814. let num = F.slice(1);
  815. if (num > template.calcItems.length){
  816. let s = hintBox.font('F'+num);
  817. hintBox.infoBox('错误提示', `表达式中 “${hintBox.font(s)}” 行号引用错误!`, 1);
  818. return false;
  819. };
  820. };
  821. let expression = me.getExpression(expr, template);
  822. if (me.isCycleCalc(expression, itemID, template)){
  823. hintBox.infoBox('错误提示', `表达式中有${hintBox.font('循环计算')}!`, 1);
  824. return false;
  825. };
  826. if (!testValue(expression)){
  827. hintBox.infoBox('错误提示', `表达式中有${hintBox.font('语法错误')}!`, 1);
  828. return false;
  829. };
  830. return true; // 表达式合法
  831. },
  832. refIDToLines: function (expression, template) { // @2、@3 → F7、F8
  833. let rst = expression;
  834. let atIDArr = analyzer.getAtIDArr(rst);
  835. let fArr = [];
  836. for (let atID of atIDArr) {
  837. let FN = analyzer.getFName(atID, template);
  838. fArr.push(FN);
  839. };
  840. for (let i = 0; i < atIDArr.length; i++) {
  841. let patt = new RegExp(atIDArr[i]);
  842. let val = fArr[i];
  843. rst = rst.replace(patt, val);
  844. };
  845. return rst;
  846. },
  847. refLineToIDs: function (dispExpr, template) { // F7、F8 → @2、@3
  848. let rst = analyzer.standard(dispExpr);
  849. let fArr = analyzer.getFArr(rst);
  850. let IDArr = [];
  851. for (let F of fArr) {
  852. let ID = analyzer.getID(F, template);
  853. IDArr.push(ID);
  854. };
  855. for (let i = 0; i < fArr.length; i++) {
  856. let patt = new RegExp(fArr[i]);
  857. let val = `@${IDArr[i]}`;
  858. rst = rst.replace(patt, val);
  859. };
  860. return rst;
  861. },
  862. getDispExpr: function (expression, template) {
  863. return analyzer.refIDToLines(expression, template);
  864. },
  865. getExpression: function (dispExpr, template) {
  866. return analyzer.refLineToIDs(dispExpr, template);
  867. },
  868. getDispExprUser: function (dispExpr, labourCoe) { // labourCoe 是 str 类型
  869. let rst = analyzer.standard(dispExpr);
  870. rst = rst.replace(/L/g, labourCoe);
  871. return rst;
  872. },
  873. getCompiledExpr: function (expression, labourCoe) { // labourCoe 是 str 类型
  874. let rst = expression;
  875. let atIDArr = analyzer.getAtIDArr(rst);
  876. let IDArr = atIDArr.map(function (atID) {
  877. return atID.slice(1);
  878. });
  879. for (var i = 0; i < atIDArr.length; i++) {
  880. let patt = new RegExp(atIDArr[i]);
  881. let val = `$CE.at(${IDArr[i]})`;
  882. rst = rst.replace(patt, val);
  883. };
  884. rst = rst.replace(/\[/g, "$CE.base('");
  885. rst = rst.replace(/\]/g, "')");
  886. rst = rst.replace(/L/g, labourCoe);
  887. return rst;
  888. },
  889. getStatement: function (expression, template) {
  890. let rst = expression;
  891. let atIDArr = analyzer.getAtIDArr(rst);
  892. let IDArr = atIDArr.map(function (atID) {
  893. return atID.slice(1);
  894. });
  895. for (var i = 0; i < atIDArr.length; i++) {
  896. let patt = new RegExp(atIDArr[i]);
  897. let val = projectObj.project.calcProgram.compiledTemplates[template.ID].compiledCalcItems[IDArr[i]].name;
  898. rst = rst.replace(patt, val);
  899. };
  900. rst = rst.replace(/\[/g, "");
  901. rst = rst.replace(/\]/g, "");
  902. rst = rst.replace(/L/g, '人工系数');
  903. return rst;
  904. },
  905. calcItemMaxID: function(template){
  906. let MaxID = 0;
  907. for (let item of template.calcItems){
  908. if (item.ID > MaxID)
  909. MaxID = item.ID;
  910. };
  911. return MaxID;
  912. },
  913. calcItemIsUsed: function(template, calcItem){
  914. let atID = '@' + calcItem.ID;
  915. for (var i = 0; i < template.calcItems.length; i++) {
  916. let item = template.calcItems[i];
  917. let atIDArr = analyzer.getAtIDArr(item.expression);
  918. if (atIDArr.indexOf(atID) >= 0){
  919. calcItem.tempUsed = i;
  920. return true;
  921. }
  922. }
  923. return false;
  924. },
  925. calcItemLabourCoe: function(calcItem){
  926. let lc = 0;
  927. if (calcItem.labourCoeID)
  928. lc = projectObj.project.calcProgram.compiledLabourCoes[calcItem.labourCoeID].coe.toString();
  929. return lc;
  930. },
  931. templateRefresh: function(template){
  932. for (let item of template){
  933. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  934. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, me.calcItemLabourCoe(item));
  935. };
  936. },
  937. templateMaxID: function(){
  938. let ts = projectObj.project.calcProgram.templates;
  939. let MaxID = 0;
  940. for (let t of ts){
  941. if (t.ID > MaxID)
  942. MaxID = t.ID;
  943. };
  944. return MaxID;
  945. },
  946. templateNewName: function (name) {
  947. let i = 2;
  948. while (projectObj.project.calcProgram.compiledTemplateMaps[name + i]) {
  949. i++;
  950. };
  951. return name + i;
  952. },
  953. templateNameIsExist: function (name) {
  954. if (projectObj.project.calcProgram.compiledTemplateMaps[name])
  955. return true
  956. else return false;
  957. },
  958. templateIsUsed: function (ID){
  959. let nodes = projectObj.project.mainTree.items;
  960. for (let node of nodes){
  961. if (node.data && node.data.programID && node.data.programID == ID) {
  962. return true;
  963. }
  964. };
  965. return false;
  966. },
  967. fieldNameIsUsed: function(template, fieldName){
  968. let fieldNameEn = projectObj.project.calcProgram.compiledFeeTypeMaps[fieldName];
  969. for (var i = 0; i < template.calcItems.length; i++) {
  970. if (template.calcItems[i].fieldName == fieldNameEn){
  971. template.fieldNameTempUsed = i;
  972. return true;
  973. }
  974. }
  975. return false;
  976. }
  977. };
  978. let executeObj = {
  979. treeNode: null,
  980. template: null,
  981. tempCalcItem: null,
  982. at: function(ID) {
  983. let me = executeObj;
  984. let rst = me.template.compiledCalcItems[ID].unitFee;
  985. rst = parseFloat(rst);
  986. return rst;
  987. },
  988. base: function(baseName) {
  989. let me = executeObj;
  990. // 量价、工料机形式的定额, 要把自己的市场单价用于计算程序中的基数。
  991. if (calcTools.isVolumePrice(me.treeNode) || calcTools.isGljRation(me.treeNode))
  992. return calcTools.marketPriceToBase(me.treeNode, baseName)
  993. else{
  994. if (!rationCalcBases[baseName]){
  995. hintBox.infoBox('系统提示', '定额基数“' + baseName + '”末定义,计算错误。 (模板 ' + me.template.ID + ',规则 ' + me.tempCalcItem.ID +')', 1);
  996. return 0;
  997. }
  998. else
  999. return rationCalcBases[baseName](me.treeNode);
  1000. }
  1001. },
  1002. HJ: function () {
  1003. let me = this;
  1004. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  1005. let q = calcTools.uiNodeQty(me.treeNode) ? calcTools.uiNodeQty(me.treeNode) : 1;
  1006. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  1007. return u;
  1008. }
  1009. };
  1010. class CalcProgram {
  1011. constructor(project){
  1012. let me = this;
  1013. me.project = project;
  1014. me.datas = [];
  1015. project.registerModule(ModuleNames.calc_program, me);
  1016. };
  1017. // 兼容Project框架方法
  1018. getSourceType () {
  1019. return ModuleNames.calc_program;
  1020. };
  1021. // 兼容Project框架方法
  1022. loadData (datas) {
  1023. this.datas = datas;
  1024. this.compileAllTemps();
  1025. };
  1026. // 兼容Project框架方法
  1027. doAfterUpdate (err, data) {
  1028. if(!err){
  1029. $.bootstrapLoading.end();
  1030. }
  1031. };
  1032. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  1033. compileAllTemps(){
  1034. let me = this;
  1035. me.compiledFeeRates = {};
  1036. me.compiledLabourCoes = {};
  1037. me.compiledTemplates = {};
  1038. me.compiledTemplateMaps = {};
  1039. me.compiledTemplateNames = [];
  1040. me.compiledFeeTypeMaps = {};
  1041. me.compiledFeeTypeNames = [];
  1042. me.compiledCalcBases = {};
  1043. me.saveForReports = [];
  1044. me.feeRates = this.project.FeeRate.datas.rates;
  1045. me.labourCoes = this.project.labourCoe.datas.coes;
  1046. me.feeTypes = cpFeeTypes;
  1047. // me.calcBases = rationCalcBase;
  1048. me.templates = this.project.calcProgram.datas.templates;
  1049. // me.templates.push(defaultBillTemplate);
  1050. // 先编译公用的基础数据
  1051. me.compilePublics();
  1052. me.compileTemplateMaps();
  1053. for (let t of me.templates){
  1054. me.compileTemplate(t);
  1055. };
  1056. // 存储费率临时数据,报表用。
  1057. if (me.saveForReports.length > 0){
  1058. let saveDatas = {};
  1059. saveDatas.projectID = projectInfoObj.projectInfo.ID;
  1060. saveDatas.calcItems = me.saveForReports;
  1061. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (result) {
  1062. if (result){
  1063. me.saveForReports = [];
  1064. };
  1065. });
  1066. };
  1067. };
  1068. compilePublics(){
  1069. let me = this;
  1070. for (let rate of me.feeRates) {
  1071. me.compiledFeeRates[rate.ID] = rate;
  1072. }
  1073. for (let coe of me.labourCoes) {
  1074. me.compiledLabourCoes[coe.ID] = coe;
  1075. }
  1076. for (let ft of me.feeTypes) {
  1077. me.compiledFeeTypeMaps[ft.type] = ft.name;
  1078. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  1079. me.compiledFeeTypeNames.push(ft.name);
  1080. }
  1081. /* for (let cb of me.calcBases) {
  1082. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  1083. }*/
  1084. };
  1085. compileTemplateMaps(){
  1086. let me = this;
  1087. function clearObj(obj){
  1088. for (let key in obj) {
  1089. delete obj[key];
  1090. }
  1091. };
  1092. clearObj(me.compiledTemplates);
  1093. clearObj(me.compiledTemplateMaps);
  1094. me.compiledTemplateNames.splice(0, me.compiledTemplateNames.length);
  1095. for (let t of me.templates){
  1096. me.compiledTemplates[t.ID] = t;
  1097. me.compiledTemplateMaps[t.ID] = t.name;
  1098. me.compiledTemplateMaps[t.name] = t.ID;
  1099. me.compiledTemplateNames.push(t.name);
  1100. };
  1101. };
  1102. compileTemplate(template){
  1103. let me = this;
  1104. // me.compiledTemplates[template.ID] = template;
  1105. // me.compiledTemplateMaps[template.ID] = template.name;
  1106. // me.compiledTemplateMaps[template.name] = template.ID;
  1107. // me.compiledTemplateNames.push(template.name);
  1108. template.hasCompiled = false;
  1109. template.errs = [];
  1110. let private_extract_ID = function(str, idx){
  1111. let subStr = str.slice(idx);
  1112. let patt = new RegExp(/@\d+/);
  1113. let atID = subStr.match(patt);
  1114. let ID = atID ? atID[0].slice(1) : null;
  1115. return ID;
  1116. };
  1117. let private_parse_ref = function(item, itemIdx){
  1118. let idx = item.expression.indexOf('@', 0);
  1119. while (idx >= 0) {
  1120. let ID = private_extract_ID(item.expression, idx);
  1121. let len = ID ? ID.toString().length : 0;
  1122. if (len) {
  1123. let subItem = template.compiledCalcItems[ID];
  1124. if (subItem) {
  1125. if (subItem.ID !== item.ID) {
  1126. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  1127. } else {
  1128. template.errs.push("循环引用ID: " + ID);
  1129. }
  1130. } else {
  1131. template.errs.push("找不到ID: " + ID);
  1132. console.log('找不到ID: ' + ID);
  1133. }
  1134. }
  1135. idx = item.expression.indexOf('@', idx + len + 1);
  1136. }
  1137. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  1138. template.compiledSeq.push(itemIdx);
  1139. }
  1140. };
  1141. let private_compile_items = function() {
  1142. for (let idx of template.compiledSeq) {
  1143. let item = template.calcItems[idx];
  1144. let lc = analyzer.calcItemLabourCoe(item);
  1145. // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  1146. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, lc);
  1147. if (item.expression == 'HJ')
  1148. item.compiledExpr = '$CE.HJ()'
  1149. else
  1150. item.compiledExpr = analyzer.getCompiledExpr(item.expression, lc);
  1151. if (item.feeRateID) {
  1152. let orgFeeRate = item.feeRate;
  1153. let cfr = me.compiledFeeRates[item.feeRateID];
  1154. item.feeRate = cfr ? cfr.rate : 100;
  1155. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  1156. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  1157. }
  1158. };
  1159. // 字段名映射
  1160. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  1161. }
  1162. };
  1163. if (template && template.calcItems && template.calcItems.length > 0) {
  1164. template.compiledSeq = [];
  1165. template.compiledCalcItems = {};
  1166. for (let i = 0; i < template.calcItems.length; i++) {
  1167. let item = template.calcItems[i];
  1168. // item.expression = analyzer.getExpression(item.dispExpr, template);
  1169. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1170. template.compiledCalcItems[item.ID] = item;
  1171. template.compiledCalcItems[item.ID + "_idx"] = i;
  1172. }
  1173. for (let i = 0; i < template.calcItems.length; i++) {
  1174. let item = template.calcItems[i];
  1175. if (template.compiledSeq.indexOf(i) < 0) {
  1176. private_parse_ref(item, i);
  1177. }
  1178. }
  1179. if (template.errs.length == 0) {
  1180. private_compile_items();
  1181. template.hasCompiled = true;
  1182. } else {
  1183. console.log('errors: ' + template.errs.toString());
  1184. }
  1185. };
  1186. };
  1187. // 存储、刷新零散的多个结点。
  1188. saveNodes(treeNodes, callback){
  1189. if (treeNodes.length < 1) {
  1190. $.bootstrapLoading.end();
  1191. return;
  1192. }
  1193. let me = this;
  1194. /* me.project.beginUpdate('');
  1195. for (let node of treeNodes){
  1196. if (node.changed){
  1197. let data = calcTools.cutNodeForSave(node);
  1198. let newData = {'updateType': 'ut_update', 'updateData': data};
  1199. me.project.push(node.sourceType, [newData]);
  1200. }
  1201. };
  1202. me.project.endUpdate();*/
  1203. let dataArr = [];
  1204. for (let node of treeNodes){
  1205. if (node.changed){
  1206. let data = calcTools.cutNodeForSave(node);
  1207. let newData = {'type': node.sourceType, 'data': data};
  1208. dataArr.push(newData);
  1209. }
  1210. };
  1211. if (dataArr.length < 1) {
  1212. $.bootstrapLoading.end();
  1213. return;
  1214. };
  1215. $.bootstrapLoading.start();
  1216. let startTime = +new Date();
  1217. me.project.updateNodes(dataArr, function (data) {
  1218. let endShowTime = +new Date();
  1219. console.log(`保存所需时间——${endShowTime - startTime}`);
  1220. if(callback){
  1221. callback(data);
  1222. };
  1223. for (let node of treeNodes){delete node.changed};
  1224. projectObj.mainController.refreshTreeNode(treeNodes);
  1225. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。
  1226. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  1227. calcProgramObj.refreshCurNodeCalcItems(me.project.mainTree.selected);
  1228. };
  1229. $.bootstrapLoading.end();
  1230. });
  1231. };
  1232. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  1233. innerCalc(treeNode, changedArr){
  1234. let me = this;
  1235. // 仅用作树节点显示的工料机不能参与计算。
  1236. if (treeNode.sourceType === ModuleNames.ration_glj) return;
  1237. treeNode.calcType = calcTools.getCalcType(treeNode);
  1238. function isBaseFeeType(type){
  1239. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  1240. };
  1241. // 删掉多余的费用。例如:从其它计算方式切换到公式计算方式,会多出其它的费(不光是common)
  1242. function deleteUselessFees(treeNode, fieldNameArr){
  1243. if (fieldNameArr){ // 用于计算程序没有绑定的费用类别,不要同步到清单,而清单因为以前计算过该类别又有值,需删除。如切换取费类别,旧费要清掉。
  1244. for (var i = 0; i < treeNode.data.fees.length; i++) {
  1245. let fee = treeNode.data.fees[i];
  1246. if (!fieldNameArr.includes(fee.fieldName)){
  1247. treeNode.data.fees.splice(i, 1);
  1248. delete treeNode.data.feesIndex[fee.fieldName];
  1249. treeNode.changed = true;
  1250. }
  1251. }
  1252. }
  1253. else{ // 总造价清单只留common, estimate两个费用类别。其它公式清单只留common。
  1254. let reserveArr = calcTools.isTotalCostBill(treeNode)? ['common', 'estimate']:['common'];
  1255. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1256. let feesArr = treeNode.data.fees;
  1257. for (let i = 0; i < feesArr.length; i++) {
  1258. if (!reserveArr.includes(feesArr[i].fieldName)) {
  1259. delete treeNode.data.feesIndex[feesArr[i].fieldName];
  1260. feesArr.splice(i, 1);
  1261. treeNode.changed = true;
  1262. }
  1263. }
  1264. };
  1265. };
  1266. };
  1267. // 父清单汇总子项(子清单或定额)的费用类别
  1268. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees || treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1269. treeNode.data.programID = null;
  1270. calcTools.initFees(treeNode);
  1271. let nodes = (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees) ? treeNode.children : me.project.Ration.getRationNodes(treeNode);
  1272. let rst = [];
  1273. for (let ft of cpFeeTypes) {
  1274. let isEstimate = ft.type == 'estimate';
  1275. let ftObj = {};
  1276. ftObj.fieldName = ft.type;
  1277. ftObj.name = ft.name;
  1278. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  1279. let bq = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 1;
  1280. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  1281. for (let node of nodes) {
  1282. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1283. // 父清单不要汇总综合单价。
  1284. // buf = (buf + parseFloatPlus(node.data.feesIndex[ft.type].unitFee)).toDecimal(decimalObj.process);
  1285. btf = (btf + parseFloatPlus(node.data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  1286. // btuf = (btuf + parseFloatPlus(node.data.feesIndex[ft.type].tenderUnitFee)).toDecimal(decimalObj.process);
  1287. bttf = (bttf + parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  1288. };
  1289. };
  1290. }
  1291. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  1292. let sum_rtf = 0, sum_rttf = 0;
  1293. for (let node of nodes) {
  1294. let rq = calcTools.uiNodeQty(node) ? calcTools.uiNodeQty(node) : 0;
  1295. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  1296. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1297. ruf = parseFloatPlus(node.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  1298. rtuf = parseFloatPlus(node.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  1299. rtf = parseFloatPlus(node.data.feesIndex[ft.type].totalFee).toDecimal(decimalObj.bills.totalPrice);
  1300. rttf = parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee).toDecimal(decimalObj.bills.totalPrice);
  1301. };
  1302. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  1303. buf = (buf + (ruf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1304. btuf = (btuf + (rtuf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1305. };
  1306. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  1307. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  1308. };
  1309. if (isEstimate){
  1310. btf = sum_rtf;
  1311. bttf = sum_rttf;
  1312. }else{
  1313. if (me.project.property.billsCalcMode == leafBillGetFeeType.rationPriceConverse ||
  1314. me.project.property.billsCalcMode == leafBillGetFeeType.rationPrice) {
  1315. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  1316. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  1317. };
  1318. if (isBaseFeeType(ft.type) ||
  1319. (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  1320. btf = sum_rtf;
  1321. bttf = sum_rttf;
  1322. }
  1323. else{
  1324. btf = (buf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1325. bttf = (btuf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1326. };
  1327. };
  1328. };
  1329. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  1330. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  1331. if (isEstimate){
  1332. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1333. ftObj.unitFee = (ftObj.totalFee / bq).toDecimal(decimalObj.bills.unitPrice);
  1334. ftObj.tenderUnitFee = (ftObj.tenderTotalFee / bq).toDecimal(decimalObj.bills.unitPrice);
  1335. }
  1336. }
  1337. else{
  1338. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  1339. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  1340. }
  1341. calcTools.checkFeeField(treeNode, ftObj);
  1342. rst.push(ftObj);
  1343. };
  1344. treeNode.data.calcTemplate = {"calcItems": rst};
  1345. }
  1346. // 叶子清单无子结点、无公式计算(啥都没有时)
  1347. else if (treeNode.calcType == treeNodeCalcType.ctNull){
  1348. delete treeNode.data.gljList;
  1349. // 不能直接删除该属性,否则无法冲掉库中已存储的值。下同。
  1350. if (treeNode.data.calcBase){
  1351. treeNode.data.calcBase = null;
  1352. treeNode.changed = true;
  1353. }
  1354. if (treeNode.data.calcBaseValue){
  1355. treeNode.data.calcBaseValue = null;
  1356. treeNode.changed = true;
  1357. }
  1358. if (treeNode.data.programID) {
  1359. treeNode.data.programID = null;
  1360. treeNode.changed = true;
  1361. }
  1362. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1363. treeNode.data.fees = null;
  1364. treeNode.data.feesIndex = null;
  1365. treeNode.changed = true;
  1366. }
  1367. treeNode.data.calcTemplate = {"calcItems": []};
  1368. }
  1369. // 叶子清单公式计算
  1370. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  1371. delete treeNode.data.gljList;
  1372. if (treeNode.data.programID) {
  1373. treeNode.data.programID = null;
  1374. treeNode.changed = true;
  1375. }
  1376. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  1377. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  1378. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 1;
  1379. let uf = (b * f * 0.01 / q).toDecimal(decimalObj.bills.unitPrice);
  1380. let tuf = uf;
  1381. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100) : (uf * q);
  1382. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  1383. let ttf = tf;
  1384. deleteUselessFees(treeNode);
  1385. calcTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  1386. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  1387. if (calcTools.isTotalCostBill(treeNode)){
  1388. // 公式叶子清单没有暂估费,但总造价清单除外。
  1389. calcTools.estimateFee(treeNode);
  1390. calcTools.initSummaryFee(treeNode);
  1391. treeNode.data.summaryFees.totalFee = tf;
  1392. treeNode.data.summaryFees.estimateFee = calcTools.getFee(treeNode, 'estimate.totalFee');
  1393. treeNode.data.summaryFees.safetyFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION), 'common.totalFee');
  1394. treeNode.data.summaryFees.chargeFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.CHARGE), 'common.totalFee');
  1395. }
  1396. treeNode.data.calcTemplate = {"calcItems": []};
  1397. }
  1398. // 定额或叶子清单自己的计算程序计算
  1399. else{
  1400. let fnArr = [];
  1401. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1402. // 量价、工料机类型的定额要求市场合价
  1403. if (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation){
  1404. delete treeNode.data.gljList;
  1405. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1406. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 0;
  1407. let mtf = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  1408. if (treeNode.data.marketTotalFee != mtf){
  1409. treeNode.data.marketTotalFee = mtf;
  1410. treeNode.changed = true;
  1411. } ;
  1412. };
  1413. // 工料机类型的定额、定额要算暂估费
  1414. if (treeNode.data.type != rationType.volumePrice){
  1415. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data);
  1416. // 计算程序里没有暂估费的计算规则,会漏掉,所以这里要专门算。
  1417. calcTools.estimateFee(treeNode);
  1418. fnArr.push('estimate');
  1419. };
  1420. if (treeNode.data.programID == undefined){
  1421. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  1422. };
  1423. }
  1424. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  1425. let rations = me.project.Ration.getBillsSortRation(treeNode.source.getID());
  1426. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 1;
  1427. treeNode.data.gljList = me.project.ration_glj.getGatherGljArrByRations(rations, q);
  1428. if (treeNode.data.programID == undefined){
  1429. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  1430. }
  1431. // 叶子清单自己的计算程序计算,其暂估费也要汇总算。
  1432. calcTools.estimateFee(treeNode);
  1433. fnArr.push('estimate');
  1434. };
  1435. let template = me.compiledTemplates[treeNode.data.programID];
  1436. treeNode.data.calcTemplate = template;
  1437. if (treeNode && template.hasCompiled) {
  1438. let $CE = executeObj;
  1439. $CE.treeNode = treeNode;
  1440. $CE.template = template;
  1441. calcTools.initFees(treeNode);
  1442. for (let idx of template.compiledSeq) {
  1443. let calcItem = template.calcItems[idx];
  1444. $CE.tempCalcItem = calcItem;
  1445. let feeRate = parseFloatPlus(calcItem.feeRate) ? parseFloatPlus(calcItem.feeRate).toDecimal(decimalObj.feeRate) : 100; // 100%
  1446. // console.log(`[${calcItem.ID}]: ${calcItem.compiledExpr}`); // for test.
  1447. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  1448. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 0;
  1449. calcItem.totalFee = (calcItem.unitFee * q).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1450. if (calcItem.fieldName) {
  1451. fnArr.push(calcItem.fieldName);
  1452. calcTools.checkFeeField(treeNode, calcItem);
  1453. };
  1454. };
  1455. deleteUselessFees(treeNode, fnArr);
  1456. };
  1457. };
  1458. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  1459. };
  1460. // 计算本节点、所有父节点(默认,可选)、公式引用节点(默认,可选)。
  1461. calculate(treeNode, calcParents = true, calcFormulas = true){
  1462. let me = this;
  1463. let changedNodes = [];
  1464. me.innerCalc(treeNode, changedNodes);
  1465. if (treeNode.changed) {
  1466. // 计算父结点
  1467. if (calcParents){
  1468. let curNode = treeNode.parent;
  1469. while (curNode){
  1470. me.innerCalc(curNode, changedNodes);
  1471. curNode = curNode.parent;
  1472. };
  1473. };
  1474. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1475. if (calcFormulas) {
  1476. me.calcFormulaNodes(changedNodes);
  1477. };
  1478. };
  1479. return changedNodes;
  1480. };
  1481. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1482. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  1483. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1484. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到) 不要用
  1485. 缺陷:calcAllType.catRations 参数情况不会计算父结点。(calcAllType.catBills 可以,因为清单的父结点也是清单会计算)
  1486. */
  1487. calcAllNodes(calcType = calcAllType.catAll){
  1488. let me = this;
  1489. let changedNodes = [];
  1490. function calcNodes(nodes) {
  1491. for (let node of nodes) {
  1492. if (node.children.length > 0) {
  1493. calcNodes(node.children);
  1494. };
  1495. if ((calcType == calcAllType.catAll || calcType == node.sourceType) && node.calcType != treeNodeCalcType.ctCalcBaseValue) {
  1496. me.innerCalc(node, changedNodes);
  1497. };
  1498. }
  1499. };
  1500. calcNodes(me.project.mainTree.roots);
  1501. me.calcFormulaNodes(changedNodes);
  1502. return changedNodes;
  1503. };
  1504. // 计算全部公式项。 (参数意义:将通过本方法后发生改变的节点存入changedArr中)
  1505. calcFormulaNodes(changedArr){
  1506. let me = this;
  1507. let formulaNodes = cbTools.getFormulaNodes(true);
  1508. if (formulaNodes.length == 0) return;
  1509. for (let formulaNode of formulaNodes){
  1510. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  1511. projectObj.project.calcBase.calculate(formulaNode, true);
  1512. if (projectObj.project.calcBase.success){
  1513. // 计算公式结点
  1514. me.innerCalc(formulaNode, changedArr);
  1515. // 计算父结点
  1516. if (formulaNode.changed){
  1517. let curNode = formulaNode.parent;
  1518. while (curNode){
  1519. me.innerCalc(curNode, changedArr);
  1520. curNode = curNode.parent;
  1521. };
  1522. };
  1523. };
  1524. };
  1525. };
  1526. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  1527. calcLeafAndSave(treeNode){
  1528. let me = this;
  1529. if(!calcTools.isLeafBill(treeNode)) return;
  1530. if (treeNode.children && treeNode.children.length > 0) {
  1531. let changedNodes = [];
  1532. for (let child of treeNode.children){
  1533. me.innerCalc(child, changedNodes);
  1534. };
  1535. let curChangeds = me.calculate(treeNode);
  1536. mergeArr(changedNodes, curChangeds);
  1537. me.saveNodes(changedNodes);
  1538. };
  1539. };
  1540. // 计算并保存指定的一个树节点。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1541. // 这个方法实际上封装了calculate()和saveNodes()两个方法,主要目的是为了外部调用方便,少写一点累赘代码。
  1542. calcAndSave(treeNode, callback){
  1543. let changedNodes = this.calculate(treeNode);
  1544. this.saveNodes(changedNodes, callback);
  1545. };
  1546. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  1547. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  1548. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1549. calcNodesAndSave(nodes, callback){
  1550. let me = this, rationNodes = [], billNodes = [], leafBills = [], allChangedNodes = [];
  1551. for (let node of nodes) {
  1552. if (node.sourceType == ModuleNames.ration)
  1553. rationNodes.push(node)
  1554. else
  1555. billNodes.push(node);
  1556. };
  1557. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1558. for (let ration of rationNodes) {
  1559. me.innerCalc(ration, allChangedNodes);
  1560. let leafBill = ration.parent;
  1561. if (leafBill && leafBills.indexOf(leafBill) < 0)
  1562. leafBills.push(leafBill);
  1563. };
  1564. mergeArr(billNodes, leafBills);
  1565. for (let bill of billNodes){
  1566. let changeBills = me.calculate(bill, true, false);
  1567. mergeArr(allChangedNodes, changeBills);
  1568. };
  1569. me.calcFormulaNodes(allChangedNodes);
  1570. me.saveNodes(allChangedNodes, callback);
  1571. };
  1572. calcRationsAndSave(rationNodes, callback){
  1573. // let me = this, leafBills = [], allChangedNodes = [];
  1574. // for (let node of rationNodes) {
  1575. // me.innerCalc(node, allChangedNodes);
  1576. // let leafBill = node.parent;
  1577. // // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1578. // if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  1579. // };
  1580. //
  1581. // for (let node of leafBills){
  1582. // let curChangeds = me.calculate(node);
  1583. // mergeArr(allChangedNodes, curChangeds);
  1584. // };
  1585. // let endShowTime = +new Date();
  1586. // me.saveNodes(allChangedNodes, callback);
  1587. this.calcNodesAndSave(rationNodes, callback);
  1588. };
  1589. calcBillsAndSave(billNodes,callback){
  1590. // let me = this, allChangedNodes = [];
  1591. // for (let node of billNodes) {
  1592. // let curChangeds = me.calculate(node, true, false);
  1593. // mergeArr(allChangedNodes, curChangeds);
  1594. // };
  1595. // me.calcFormulaNodes(allChangedNodes);
  1596. // me.saveNodes(allChangedNodes,callback);
  1597. this.calcNodesAndSave(billNodes, callback);
  1598. };
  1599. calcAllNodesAndSave(calcType = calcAllType.catAll, callback){
  1600. let changedNodes = this.calcAllNodes(calcType);
  1601. this.saveNodes(changedNodes, callback);
  1602. };
  1603. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  1604. getTotalFee(baseNodes, excludeNodes){
  1605. let rst = 0;
  1606. function calcNodes(nodes) {
  1607. for (let node of nodes) {
  1608. if (!excludeNodes.includes(node)){
  1609. if (node.source && node.source.children && node.source.children.length > 0) {
  1610. calcNodes(node.children);
  1611. }
  1612. else{
  1613. if (node.sourceType == ModuleNames.bills) {
  1614. rst = (rst + calcTools.getFee(node, 'common.totalFee')).toDecimal(decimalObj.decimal("totalPrice", node));
  1615. };
  1616. }
  1617. }
  1618. }
  1619. };
  1620. calcNodes(baseNodes);
  1621. return rst;
  1622. };
  1623. // 税前工程造价
  1624. getBeforeTaxTotalFee(excludeNodes){
  1625. let baseNodes = [], me = this;
  1626. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  1627. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  1628. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  1629. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  1630. return me.getTotalFee(baseNodes, excludeNodes);
  1631. };
  1632. };
  1633. // export default analyzer;