calc_program.js 75 KB

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