calc_program.js 73 KB

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