calc_program.js 74 KB

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