calc_program.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. expression: "@('2')+@('3')+@('4')",
  16. statement: "人工费+材料费+机械费",
  17. feeRate: null,
  18. memo: ''
  19. },
  20. {
  21. ID: 2,
  22. code: "1.1",
  23. name: "人工费",
  24. dispExpr: "HJ",
  25. expression: "HJ",
  26. statement: "合计",
  27. feeRate: 50,
  28. fieldName: 'labour',
  29. memo: ''
  30. },
  31. {
  32. ID: 3,
  33. code: "1.2",
  34. name: "材料费",
  35. dispExpr: "HJ",
  36. expression: "HJ",
  37. statement: "合计",
  38. feeRate: 30,
  39. fieldName: 'material',
  40. memo: ''
  41. },
  42. {
  43. ID: 4,
  44. code: "1.3",
  45. name: "机械费",
  46. dispExpr: "HJ",
  47. expression: "HJ",
  48. statement: "合计",
  49. feeRate: 20,
  50. fieldName: 'machine',
  51. memo: ''
  52. },
  53. {
  54. ID: 5,
  55. code: "2",
  56. name: "企业管理费",
  57. dispExpr: "F1",
  58. expression: "@('1')",
  59. statement: "定额直接费",
  60. feeRate: null,
  61. fieldName: 'manage',
  62. memo: ''
  63. },
  64. {
  65. ID: 6,
  66. code: "3",
  67. name: "利润",
  68. dispExpr: "F1",
  69. expression: "@('1')",
  70. statement: "定额直接费",
  71. feeRate: null,
  72. fieldName: 'profit',
  73. memo: ''
  74. },
  75. {
  76. ID: 7,
  77. code: "4",
  78. name: "风险费用",
  79. dispExpr: "F1",
  80. expression: "@('1')",
  81. statement: "定额直接费",
  82. feeRate: null,
  83. fieldName: 'risk',
  84. memo: ''
  85. },
  86. {
  87. ID: 8,
  88. code: "5",
  89. name: "综合单价",
  90. dispExpr: "F1+F5+F6+F7",
  91. expression: "@('1')+@('5')+@('6')+@('7')",
  92. statement: "定额直接费+企业管理费+利润+风险费用",
  93. feeRate: null,
  94. fieldName: 'common',
  95. memo: ''
  96. }
  97. ]
  98. };*/
  99. const baseCalcType = {baseCalc: 0, adjustCalc: 1, budgetCalc: 2, diffCalc: 3, offerCalc: 4};
  100. let rationCalcBase = [
  101. {
  102. 'dispName': '定额基价人工费',
  103. 'calcType': baseCalcType.baseCalc,
  104. 'gljTypes': [gljType.LABOUR]
  105. },
  106. {
  107. 'dispName': '定额基价材料费',
  108. 'calcType': baseCalcType.baseCalc,
  109. 'gljTypes': [gljType.GENERAL_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO, gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR]
  110. },
  111. {
  112. 'dispName': '定额基价机械费',
  113. 'calcType': baseCalcType.baseCalc,
  114. 'gljTypes': [gljType.GENERAL_MACHINE]
  115. },
  116. {
  117. 'dispName': '定额基价机上人工费',
  118. 'calcType': baseCalcType.baseCalc,
  119. 'gljTypes': [gljType.MACHINE_LABOUR]
  120. },
  121. {
  122. 'dispName': '人工费价差',
  123. 'calcType': baseCalcType.diffCalc,
  124. 'gljTypes': [gljType.LABOUR]
  125. },
  126. {
  127. 'dispName': '材料费价差',
  128. 'calcType': baseCalcType.diffCalc,
  129. 'gljTypes': [gljType.GENERAL_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO, gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR]
  130. },
  131. {
  132. 'dispName': '机械费价差',
  133. 'calcType': baseCalcType.diffCalc,
  134. 'gljTypes': [gljType.GENERAL_MACHINE]
  135. },
  136. {
  137. 'dispName': '主材费',
  138. 'calcType': baseCalcType.budgetCalc,
  139. 'gljTypes': [gljType.MAIN_MATERIAL]
  140. },
  141. {
  142. 'dispName': '设备费',
  143. 'calcType': baseCalcType.budgetCalc,
  144. 'gljTypes': [gljType.EQUIPMENT]
  145. }
  146. ];
  147. let cpFeeTypes = [
  148. {type: 'direct', name: '直接费'},
  149. {type: 'labour', name: '人工费'},
  150. {type: 'material', name: '材料费'},
  151. {type: 'machine', name: '机械费'},
  152. {type: 'mainMaterial', name: '主材费'},
  153. {type: 'equipment', name: '设备费'},
  154. {type: 'manage', name: '企业管理费'},
  155. {type: 'profit', name: '利润'},
  156. {type: 'risk', name: '风险费'},
  157. {type: 'labourDiff', name: '人工价差'},
  158. {type: 'materialDiff', name: '材料价差'},
  159. {type: 'machineDiff', name: '机械价差'},
  160. {type: 'adjustLabour', name: '调整人工费'},
  161. {type: 'adjustMachineLabour', name: '调整机上人工费'},
  162. {type: 'estimate', name: '暂估费'},
  163. {type: 'fee1', name: '甲供材料费'},
  164. // 模拟用户新增
  165. {type: 'common', name: '工程造价'}
  166. ];
  167. let analyzer = {
  168. calcTemplate: null,
  169. success: true,
  170. standard: function(expr){
  171. let str = expr;
  172. str = str.replace(/\s/g, ""); // 去空格、去中文空格
  173. str = str.replace(/(/g, "("); // 中文括号"("换成英文括号"("
  174. str = str.replace(/)/g, ")"); // 中文括号")"换成英文括号")"
  175. str = str.replace(/f/g, "F"); // f换成F
  176. return str;
  177. },
  178. analyzeCalcBase: function(expr){
  179. // 前提:必须无空格、无特殊符号
  180. function getCalcBase(expr){
  181. let base = '',
  182. iPos1 = -1, iPos2 = -1;
  183. for (let i = 0; i < expr.length; i++) {
  184. if (expr[i] === '['){
  185. iPos1 = i;
  186. }
  187. else if (iPos1 != -1 && expr[i]===']'){
  188. iPos2 = i;
  189. };
  190. if (iPos1 != -1 && iPos2 != -1){
  191. base = expr.slice(iPos1, iPos2 + 1);
  192. break;
  193. }
  194. };
  195. return base;
  196. };
  197. function calcBaseToIDExpr(base){
  198. /*// for test. 公路模式,基数到ID
  199. let id = -1;
  200. if (base == '[人工费]'){
  201. id = 111;
  202. }
  203. else if (base == '[材料费]'){
  204. id = 222;
  205. }
  206. else if (base == '[机械费]'){
  207. id = 333;
  208. }
  209. else id = "错误";
  210. return "@('" + id + "')";*/
  211. let baseValue = base.slice(1, -1);
  212. return "base('" + baseValue + "')";
  213. };
  214. while (expr.indexOf('[') > 0) {
  215. let base = getCalcBase(expr);
  216. let id = calcBaseToIDExpr(base);
  217. let baseValue = base.slice(1, -1); // []会给下面的正则带来干扰,这里去掉
  218. var pattBase =new RegExp(baseValue, "g");
  219. expr = expr.replace(pattBase, id);
  220. expr = expr.replace(/\[base\('/g, "base('"); // [@(' [base('
  221. expr = expr.replace(/'\)\]/g, "')"); // ')]
  222. };
  223. return expr;
  224. },
  225. analyzeLineRef: function(expr){
  226. let me = this;
  227. function isOperator(char){
  228. var operator = "+-*/()";
  229. return operator.indexOf(char) > -1;
  230. };
  231. function lineNumToID(lineNum){
  232. if (lineNum > me.calcTemplate.calcItems.length){
  233. me.success = false;
  234. return '越界';
  235. }
  236. else{
  237. let id = me.calcTemplate.calcItems[lineNum - 1].ID;
  238. return id;
  239. }
  240. };
  241. // 前提:必须无空格、无特殊符号、标准大写F
  242. function getSection(expr){
  243. let section = '',
  244. iPos1 = -1, iPos2 = -1;
  245. for (let i = 0; i < expr.length; i++) {
  246. if (expr[i] === 'F'){
  247. iPos1 = i;
  248. }
  249. else if (iPos1 != -1 && isOperator(expr[i])){
  250. iPos2 = i;
  251. }
  252. else if (iPos1 != -1 && i == expr.length - 1){
  253. iPos2 = i + 1;
  254. };
  255. if (iPos1 != -1 && iPos2 != -1){
  256. section = expr.slice(iPos1, iPos2);
  257. break;
  258. }
  259. };
  260. return section;
  261. };
  262. function sectionToIDExpr(section){
  263. if (section){
  264. let lineNum = section.slice(1);
  265. if (isNaN(lineNum)){
  266. me.success = false;
  267. return '错误'; // 这里的返回提示不能加上section,因为会无限循环
  268. }
  269. else
  270. return "@('" + lineNumToID(lineNum) + "')";
  271. }
  272. else return '';
  273. };
  274. while (expr.indexOf('F') > 0) {
  275. let sec = getSection(expr);
  276. let id = sectionToIDExpr(sec);
  277. var pattSec =new RegExp(sec, "g");
  278. expr = expr.replace(pattSec, id);
  279. };
  280. return expr;
  281. },
  282. analyzeUserExpr: function(calcTemplate, calcItem){
  283. let me = this;
  284. me.calcTemplate = calcTemplate;
  285. let expr = calcItem.dispExpr;
  286. // 标准化:处理特殊字符、中文符号、大小写
  287. expr = me.standard(expr);
  288. calcItem.dispExpr = expr;
  289. // 先换掉计算基数
  290. expr = me.analyzeCalcBase(expr);
  291. // 再换掉行引用
  292. expr = me.analyzeLineRef(expr);
  293. calcItem.expression = expr;
  294. return me.success;
  295. }
  296. };
  297. let executeObj = {
  298. treeNode: null,
  299. template: null,
  300. calcBase: null,
  301. at: function(ID) {
  302. let me = executeObj,
  303. rst = 0;
  304. rst = me.template.compiledCalcItems[ID].unitFee;
  305. rst = parseFloat(rst);
  306. return rst;
  307. },
  308. base: function(calcBaseName) {
  309. let me = executeObj, rst = 0,
  310. base = me.calcBase[calcBaseName];
  311. if (base != null) {
  312. function isSubset(sub, arr){
  313. for(var i = 0, len = sub.length; i < len; i++){
  314. if(arr.indexOf(sub[i]) == -1) return false;
  315. }
  316. return true;
  317. };
  318. // 机上人工费:多一层
  319. function machineLabourFee() {
  320. if (!me.treeNode.data.gljList) return 0;
  321. let result = 0, mdSum = 0;
  322. for (let glj of me.treeNode.data.gljList) {
  323. if (glj.type == gljType.GENERAL_MACHINE) {
  324. // 获取机械组成物
  325. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  326. if (!mds) mds = [];
  327. for (let md of mds){
  328. if (base.gljTypes.indexOf(md.type) >= 0) {
  329. let q = md["consumption"] ? md["consumption"] : 0;
  330. let p = md["basePrice"] ? md["basePrice"] : 0;
  331. mdSum = mdSum + (q * p).toDecimal(decimalObj.process);
  332. mdSum = (mdSum).toDecimal(decimalObj.process);
  333. }
  334. };
  335. result = result + (glj["quantity"] * mdSum).toDecimal(decimalObj.process);
  336. result = (result).toDecimal(decimalObj.process);
  337. };
  338. };
  339. return result;
  340. };
  341. function commonGLJFee(){
  342. if (!me.treeNode.data.gljList) return 0;
  343. let result = 0;
  344. for (let glj of me.treeNode.data.gljList) {
  345. let price = 0, temp = 0;
  346. if (base.gljTypes.indexOf(glj.type) >= 0) {
  347. if (base.calcType == baseCalcType.diffCalc){
  348. let aprice = glj["adjustPrice"] ? glj["adjustPrice"] : 0;
  349. aprice = parseFloat(aprice);
  350. let mprice = glj["marketPrice"] ? glj["marketPrice"] : 0;
  351. mprice = parseFloat(mprice);
  352. temp = (glj["quantity"] * mprice).toDecimal(decimalObj.ration.unitPrice) - (glj["quantity"] * aprice).toDecimal(decimalObj.ration.unitPrice);
  353. temp = temp.toDecimal(decimalObj.ration.unitPrice);
  354. }
  355. else {
  356. if (base.calcType == baseCalcType.baseCalc){ price = parseFloat(glj["basePrice"]);}
  357. else if (base.calcType == baseCalcType.adjustCalc){price = parseFloat(glj["adjustPrice"]);}
  358. else if (base.calcType == baseCalcType.budgetCalc){price = parseFloat(glj["marketPrice"]);}
  359. temp = (glj["quantity"] * price).toDecimal(decimalObj.ration.unitPrice);
  360. };
  361. result = (result + temp).toDecimal(decimalObj.ration.unitPrice);
  362. };
  363. };
  364. return result;
  365. };
  366. // 量价。没有具体的工料机类型,但仍然要用定额的计算程序,所以要给计算基数直接指定。
  367. function volumePriceFee() {
  368. let result = 0;
  369. if (
  370. ( me.treeNode.data.subType === gljType.LABOUR && base.dispName === '定额基价人工费') ||
  371. ( me.treeNode.data.subType === gljType.GENERAL_MATERIAL && base.dispName === '定额基价材料费') ||
  372. ( me.treeNode.data.subType === gljType.GENERAL_MACHINE && base.dispName === '定额基价机械费') ||
  373. ( me.treeNode.data.subType === gljType.MAIN_MATERIAL && base.dispName === '主材费') ||
  374. ( me.treeNode.data.subType === gljType.EQUIPMENT && base.dispName === '设备费')
  375. ) result = me.treeNode.data.marketUnitFee ? me.treeNode.data.marketUnitFee : 0;
  376. return result;
  377. };
  378. if (me.treeNode.data.type == rationType.volumePrice || me.treeNode.data.type == rationType.gljRation){
  379. rst = volumePriceFee();
  380. }
  381. else{
  382. if (isSubset(base.gljTypes, [gljType.MACHINE_LABOUR]))
  383. rst = machineLabourFee()
  384. else
  385. rst = commonGLJFee();
  386. }
  387. };
  388. return rst;
  389. },
  390. HJ: function () {
  391. let me = this;
  392. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  393. let q = me.treeNode.data.quantity ? me.treeNode.data.quantity : 1;
  394. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  395. return u;
  396. }
  397. };
  398. let treeNodeTools = {
  399. isBill: function(treeNode){
  400. return treeNode.sourceType === ModuleNames.bills;
  401. },
  402. isLeafBill: function(treeNode){
  403. return treeNode.sourceType === ModuleNames.bills &&
  404. treeNode.source.children &&
  405. treeNode.source.children.length === 0;
  406. },
  407. isNullBill: function (treeNode) {
  408. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  409. },
  410. isTotalCostBill: function (treeNode) {
  411. return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
  412. treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
  413. },
  414. isRationCategory: function(treeNode){
  415. return treeNode.sourceType === ModuleNames.ration;
  416. },
  417. isRation: function(treeNode){
  418. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.ration;
  419. },
  420. isVolumePrice: function (treeNode) {
  421. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.volumePrice;
  422. },
  423. isGljRation: function (treeNode) {
  424. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.gljRation;
  425. },
  426. initFees(treeNode){
  427. if (!treeNode.data.fees) {
  428. treeNode.data.fees = [];
  429. treeNode.data.feesIndex = {};
  430. treeNode.changed = true;
  431. };
  432. },
  433. initFeeField(treeNode, fieldName){
  434. this.initFees(treeNode);
  435. if (!treeNode.data.feesIndex[fieldName]) {
  436. let fee = {
  437. 'fieldName': fieldName,
  438. 'unitFee': 0,
  439. 'totalFee': 0,
  440. 'tenderUnitFee': 0,
  441. 'tenderTotalFee': 0
  442. };
  443. treeNode.data.fees.push(fee);
  444. treeNode.data.feesIndex[fieldName] = fee;
  445. treeNode.changed = true;
  446. };
  447. },
  448. checkFeeField(treeNode, feeObj){
  449. if (!feeObj) return;
  450. if (feeObj.fieldName == '') return;
  451. // 初始化前先拦截末定义的情况
  452. if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
  453. if (feeObj.unitFee == 0 && feeObj.totalFee == 0) return;
  454. }
  455. this.initFeeField(treeNode, feeObj.fieldName);
  456. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  457. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  458. treeNode.changed = true;
  459. };
  460. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  461. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  462. treeNode.changed = true;
  463. };
  464. },
  465. initSummaryFee(treeNode){
  466. if (!treeNode.data.summaryFees){
  467. treeNode.data.summaryFees = {
  468. totalFee: 0,
  469. estimateFee: 0,
  470. safetyFee: 0,
  471. chargeFee: 0
  472. };
  473. treeNode.changed = true;
  474. };
  475. },
  476. getCalcType(treeNode) {
  477. if (this.isRationCategory(treeNode)){
  478. return treeNodeCalcType.ctRationCalcProgram;
  479. }
  480. else if (this.isNullBill(treeNode)){
  481. return treeNodeCalcType.ctCommonUnitFee;
  482. }
  483. else if (this.isLeafBill(treeNode)) {
  484. if (treeNode.children && treeNode.children.length > 0){
  485. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  486. if (projectObj.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  487. return treeNodeCalcType.ctBillCalcProgram;
  488. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  489. return treeNodeCalcType.ctGatherRationsFees;
  490. }
  491. else{ // 公式计算
  492. return treeNodeCalcType.ctCalcBaseValue;
  493. };
  494. }
  495. else if (this.isBill(treeNode)) { // 父清单:汇总子清单的费用类别
  496. return treeNodeCalcType.ctGatherBillsFees;
  497. }
  498. else {
  499. return treeNodeCalcType.ctRationCalcProgram;
  500. };
  501. }
  502. };
  503. class CalcProgram {
  504. constructor(project){
  505. let me = this;
  506. me.project = project;
  507. me.datas = [];
  508. project.registerModule(ModuleNames.calc_program, me);
  509. };
  510. // 兼容Project框架方法
  511. getSourceType () {
  512. return ModuleNames.calc_program;
  513. };
  514. // 兼容Project框架方法
  515. loadData (datas) {
  516. this.datas = datas;
  517. this.compileAllTemps();
  518. };
  519. // 兼容Project框架方法
  520. doAfterUpdate (err, data) {
  521. if(!err){
  522. $.bootstrapLoading.end();
  523. }
  524. };
  525. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  526. compileAllTemps(){
  527. let me = this;
  528. me.compiledFeeRates = {};
  529. me.compiledLabourCoes = {};
  530. me.compiledTemplates = {};
  531. me.compiledTemplateMaps = {};
  532. me.compiledTemplateNames = [];
  533. me.compiledFeeTypeMaps = {};
  534. me.compiledFeeTypeNames = [];
  535. me.compiledCalcBases = {};
  536. me.saveForReports = [];
  537. me.feeRates = this.project.FeeRate.datas.rates;
  538. me.labourCoes = this.project.labourCoe.datas.coes;
  539. me.feeTypes = cpFeeTypes;
  540. me.calcBases = rationCalcBase;
  541. me.templates = this.project.calcProgram.datas.templates;
  542. // me.templates.push(defaultBillTemplate);
  543. // 先编译公用的基础数据
  544. me.compilePublics();
  545. for (let t of me.templates){
  546. me.compileTemplate(t);
  547. };
  548. // 存储费率临时数据,报表用。
  549. if (me.saveForReports.length > 0){
  550. let saveDatas = {};
  551. saveDatas.projectID = projectInfoObj.projectInfo.ID;
  552. saveDatas.calcItems = me.saveForReports;
  553. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (data) {
  554. me.saveForReports = [];
  555. });
  556. };
  557. };
  558. compilePublics(){
  559. let me = this;
  560. for (let rate of me.feeRates) {
  561. me.compiledFeeRates[rate.ID] = rate;
  562. }
  563. for (let coe of me.labourCoes) {
  564. me.compiledLabourCoes[coe.ID] = coe;
  565. }
  566. for (let ft of me.feeTypes) {
  567. me.compiledFeeTypeMaps[ft.type] = ft.name;
  568. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  569. me.compiledFeeTypeNames.push(ft.name);
  570. }
  571. for (let cb of me.calcBases) {
  572. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  573. }
  574. };
  575. compileTemplate(template){
  576. let me = this;
  577. me.compiledTemplates[template.ID] = template;
  578. me.compiledTemplateMaps[template.ID] = template.name;
  579. me.compiledTemplateMaps[template.name] = template.ID;
  580. me.compiledTemplateNames.push(template.name);
  581. template.hasCompiled = false;
  582. template.errs = [];
  583. let private_extract_ID = function(str, idx){
  584. let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  585. for (let i = idx; i < str.length; i++) {
  586. if (str[i] === '(') {
  587. lBracket++;
  588. if (lBracket == 1) firstIdx = i + 1;
  589. }
  590. if (str[i] === ')') {
  591. rBracket++;
  592. if (lBracket == rBracket) {
  593. lastIdx = i - 1;
  594. if (lastIdx > firstIdx) {
  595. if (str[firstIdx] === "'") firstIdx++;
  596. if (str[lastIdx] !== "'") lastIdx++;
  597. if (lastIdx > firstIdx) {
  598. rst = str.slice(firstIdx, lastIdx);
  599. }
  600. }
  601. break;
  602. }
  603. }
  604. }
  605. return rst;
  606. };
  607. let private_parse_ref = function(item, itemIdx){
  608. let idx = item.expression.indexOf('@(', 0);
  609. while (idx >= 0) {
  610. let ID = private_extract_ID(item.expression, idx);
  611. if (ID.length > 0) {
  612. let subItem = template.compiledCalcItems[ID];
  613. if (subItem) {
  614. if (subItem.ID !== item.ID) {
  615. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  616. } else {
  617. template.errs.push("There exists the self refer ID: " + ID);
  618. }
  619. } else {
  620. template.errs.push("There exists the invalid ID by which could not find the item: " + ID);
  621. console.log('invalid ID: ' + ID);
  622. }
  623. }
  624. idx = item.expression.indexOf('@(', idx + ID.length + 3);
  625. }
  626. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  627. template.compiledSeq.push(itemIdx);
  628. }
  629. };
  630. let private_compile_items = function() {
  631. for (let idx of template.compiledSeq) {
  632. let item = template.calcItems[idx];
  633. item.dispExprUser = item.dispExpr; // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  634. if (item.expression == 'HJ')
  635. item.compiledExpr = '$CE.HJ()'
  636. else{
  637. item.compiledExpr = item.expression.split('@(').join('$CE.at(');
  638. item.compiledExpr = item.compiledExpr.split('base(').join('$CE.base(');
  639. };
  640. if (item.labourCoeID){
  641. let lc = me.compiledLabourCoes[item.labourCoeID].coe;
  642. item.dispExprUser = item.dispExpr.replace(/L/gi, lc.toString());
  643. item.compiledExpr = item.compiledExpr.replace(/L/gi, lc.toString());
  644. };
  645. if (item.feeRateID) {
  646. let orgFeeRate = item.feeRate;
  647. let cmf = me.compiledFeeRates[item.feeRateID];
  648. item.feeRate = cmf?cmf.rate:100;
  649. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  650. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  651. }
  652. };
  653. // 字段名映射
  654. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  655. }
  656. };
  657. if (template && template.calcItems && template.calcItems.length > 0) {
  658. template.compiledSeq = [];
  659. template.compiledCalcItems = {};
  660. for (let i = 0; i < template.calcItems.length; i++) {
  661. let item = template.calcItems[i];
  662. template.compiledCalcItems[item.ID] = item;
  663. template.compiledCalcItems[item.ID + "_idx"] = i;
  664. }
  665. for (let i = 0; i < template.calcItems.length; i++) {
  666. let item = template.calcItems[i];
  667. if (template.compiledSeq.indexOf(i) < 0) {
  668. private_parse_ref(item, i);
  669. }
  670. }
  671. if (template.errs.length == 0) {
  672. private_compile_items();
  673. template.hasCompiled = true;
  674. } else {
  675. console.log('errors: ' + template.errs.toString());
  676. }
  677. };
  678. };
  679. // 存储、刷新零散的多个结点。
  680. saveNodes(treeNodes){
  681. if (treeNodes.length < 1) return;
  682. let me = this;
  683. me.project.beginUpdate('');
  684. for (let node of treeNodes){
  685. if (node.changed){
  686. let data = {
  687. ID: node.data.ID,
  688. projectID: me.project.ID(),
  689. unit:node.data.unit,//对清单来说,改变单位,工程量精度会跟着改变从而影响计算。
  690. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  691. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  692. subType: node.data.subType,
  693. quantity: node.data.quantity,
  694. calcBase: node.data.calcBase,
  695. calcBaseValue: node.data.calcBaseValue,
  696. programID: node.data.programID,
  697. marketUnitFee: node.data.marketUnitFee,
  698. marketTotalFee: node.data.marketTotalFee,
  699. fees: node.data.fees,
  700. isFromDetail:node.data.isFromDetail,
  701. feeRate: node.data.feeRate,
  702. feeRateID: node.data.feeRateID,
  703. contain:node.data.contain,
  704. quantityEXP:node.data.quantityEXP
  705. };
  706. if (node.data.summaryFees) data.summaryFees = node.data.summaryFees;
  707. if(node.sourceType==ModuleNames.ration && node.data.type==rationType.gljRation){//定额类型的工料机做特殊处理
  708. data.code=node.data.code;
  709. data.projectGLJID = node.data.projectGLJID;
  710. delete data.marketUnitFee;
  711. }
  712. let newData = {'updateType': 'ut_update', 'updateData': data};
  713. me.project.push(node.sourceType, [newData]);
  714. }
  715. };
  716. me.project.endUpdate();
  717. for (let node of treeNodes){delete node.changed};
  718. projectObj.mainController.refreshTreeNode(treeNodes);
  719. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) { // 批量树结点计算后,计算程序早已今非昔比,所以这里要重新计算一下。
  720. calcProgramObj.showData(me.project.mainTree.selected, true);
  721. };
  722. };
  723. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  724. innerCalc(treeNode, changedArr){
  725. let me = this;
  726. // 仅用作树节点显示的工料机不能参与计算。
  727. if (treeNode.sourceType === me.project.ration_glj.getSourceType()) return;
  728. treeNode.calcType = treeNodeTools.getCalcType(treeNode);
  729. // if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue) return;
  730. function isBaseFeeType(type){
  731. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  732. };
  733. // 计算暂估费用
  734. function calcEstimateFee(treeNode){
  735. let sumU = 0, sumT = 0;
  736. if (treeNodeTools.isTotalCostBill(treeNode)){
  737. let nodes = projectObj.project.mainTree.roots;
  738. for (let node of nodes){
  739. if (treeNodeTools.isTotalCostBill(node)) break;
  740. let eU = 0, eT = 0;
  741. if (node.data.feesIndex && node.data.feesIndex.estimate){
  742. eU = node.data.feesIndex.estimate.unitFee;
  743. eT = node.data.feesIndex.estimate.totalFee;
  744. }
  745. else {
  746. eU = 0, eT = 0;
  747. };
  748. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  749. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  750. };
  751. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  752. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  753. }
  754. else{
  755. if (!treeNode.data.gljList) return;
  756. let eTypes = [
  757. gljType.GENERAL_MATERIAL, gljType.MAIN_MATERIAL, gljType.EQUIPMENT,
  758. gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO,
  759. gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR];
  760. let eDetailTypes = [gljType.MAIN_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO];
  761. let GLJObjs = [];
  762. for (let glj of treeNode.data.gljList) {
  763. if (eTypes.indexOf(glj.type) >= 0) {
  764. if (glj.isEstimate){
  765. 'code','name','specs','unit','type'
  766. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type,
  767. quantity: glj.quantity, marketPrice: glj.marketPrice});
  768. }
  769. else{ // 组成物
  770. if (eDetailTypes.indexOf(glj.type) >= 0){
  771. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  772. if (!mds) mds = [];
  773. for (let md of mds){
  774. if (md.isEstimate){
  775. let isExist = false;
  776. let mdQ = (parseFloatPlus(glj.quantity) * parseFloatPlus(md.consumption)).toDecimal(decimalObj.process);
  777. for (let obj of GLJObjs){
  778. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  779. isExist = true;
  780. obj.quantity = (parseFloatPlus(obj.quantity) + mdQ).toDecimal(decimalObj.process);
  781. break;
  782. }
  783. };
  784. if (!isExist)
  785. GLJObjs.push({code: md.code, quantity: mdQ, marketPrice: md.marketPrice});
  786. }
  787. }
  788. }
  789. }
  790. };
  791. };
  792. for (let obj of GLJObjs){
  793. sumU = sumU + (parseFloatPlus(obj.quantity) * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
  794. sumU = sumU.toDecimal(decimalObj.process);
  795. let q = (parseFloatPlus(obj.quantity) * parseFloatPlus(treeNode.data.quantity)).toDecimal(decimalObj.process);
  796. sumT = sumT + (q * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
  797. sumT = sumT.toDecimal(decimalObj.process);
  798. };
  799. sumU = sumU.toDecimal(decimalObj.bills.unitPrice);
  800. if (projectObj.project.property.zanguCalcMode == zanguCalcType.common){
  801. sumT = (parseFloatPlus(treeNode.data.quantity) * sumU).toDecimal(decimalObj.bills.totalPrice);
  802. }
  803. else if (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial){
  804. sumT = sumT.toDecimal(decimalObj.bills.totalPrice);
  805. };
  806. };
  807. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
  808. };
  809. // 删掉多余的费用。例如:从其它计算方式切换到公式计算方式,会多出其它的费(不光是common)
  810. function deleteUselessFees(treeNode){
  811. let reserveArr = treeNodeTools.isTotalCostBill(treeNode)? ['common', 'estimate']:['common'];
  812. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  813. let feesArr = treeNode.data.fees;
  814. for (let i = 0; i < feesArr.length; i++) {
  815. if (!reserveArr.includes(feesArr[i].fieldName)) {
  816. delete treeNode.data.feesIndex[feesArr[i].fieldName];
  817. feesArr.splice(i, 1);
  818. treeNode.changed = true;
  819. }
  820. }
  821. };
  822. };
  823. // 父清单汇总子项(定额或子清单)的费用类别
  824. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees ||
  825. treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  826. treeNode.data.programID = null;
  827. treeNodeTools.initFees(treeNode);
  828. let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees) ? me.project.Ration.getRationsByNode(treeNode) : treeNode.children;
  829. let rst = [];
  830. for (let ft of cpFeeTypes) {
  831. let ftObj = {};
  832. ftObj.fieldName = ft.type;
  833. ftObj.name = ft.name;
  834. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  835. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  836. for (let item of objsArr) {
  837. let data = item.data;
  838. if (data.feesIndex && data.feesIndex[ft.type]) {
  839. buf = (buf + parseFloatPlus(data.feesIndex[ft.type].unitFee)).toDecimal(decimalObj.process);
  840. btf = (btf + parseFloatPlus(data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  841. btuf = (btuf + parseFloatPlus(data.feesIndex[ft.type].tenderUnitFee)).toDecimal(decimalObj.process);
  842. bttf = (bttf + parseFloatPlus(data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  843. };
  844. };
  845. }
  846. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  847. let sum_rtf = 0, sum_rttf = 0;
  848. let bq = parseFloat(treeNode.data.quantity ? treeNode.data.quantity : 1);
  849. for (let data of objsArr) {
  850. let rq = parseFloat(data.quantity ? data.quantity : 0);
  851. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  852. if (data.feesIndex && data.feesIndex[ft.type]) {
  853. ruf = parseFloat(data.feesIndex[ft.type].unitFee);
  854. rtuf = parseFloat(data.feesIndex[ft.type].tenderUnitFee);
  855. rtf = parseFloat(data.feesIndex[ft.type].totalFee);
  856. rttf = parseFloat(data.feesIndex[ft.type].tenderTotalFee);
  857. };
  858. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  859. buf = (buf + (ruf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  860. btuf = (btuf + (rtuf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  861. };
  862. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  863. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  864. };
  865. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse || me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) {
  866. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  867. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  868. };
  869. if (isBaseFeeType(ft.type) || (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  870. btf = sum_rtf;
  871. bttf = sum_rttf;
  872. }
  873. else{
  874. btf = (buf * bq).toDecimal(decimalObj.process);
  875. bttf = (btuf * bq).toDecimal(decimalObj.process);
  876. };
  877. };
  878. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  879. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  880. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  881. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  882. treeNodeTools.checkFeeField(treeNode, ftObj);
  883. rst.push(ftObj);
  884. };
  885. treeNode.data.calcTemplate = {"calcItems": rst};
  886. }
  887. // 叶子清单的手工综合单价计算
  888. else if (treeNode.calcType == treeNodeCalcType.ctCommonUnitFee){
  889. delete treeNode.data.gljList;
  890. if (treeNode.data.calcBase) treeNode.data.calcBase = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  891. if (treeNode.data.calcBaseValue) treeNode.data.calcBaseValue = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  892. if (treeNode.data.programID) treeNode.data.programID = null;
  893. let uf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.unitFee) ? treeNode.data.feesIndex.common.unitFee : 0;
  894. uf = uf.toDecimal(decimalObj.bills.unitPrice);
  895. let tuf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee) ? treeNode.data.feesIndex.common.tenderUnitFee : 0;
  896. tuf = tuf.toDecimal(decimalObj.bills.unitPrice);
  897. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  898. let tf = (uf * q).toDecimal(decimalObj.bills.totalPrice);
  899. let ttf = (tuf * q).toDecimal(decimalObj.bills.totalPrice);
  900. deleteUselessFees(treeNode);
  901. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  902. treeNode.data.calcTemplate = {"calcItems": []};
  903. }
  904. // 叶子清单公式计算
  905. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  906. delete treeNode.data.gljList;
  907. if (treeNode.data.programID) treeNode.data.programID = null;
  908. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  909. if (!treeNode.data.quantity) treeNode.data.quantity = 1;
  910. let q = treeNode.data.quantity;
  911. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  912. let uf = (b * f * q / 100).toDecimal(decimalObj.bills.unitPrice);
  913. let tuf = uf;
  914. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100).toDecimal(decimalObj.bills.totalPrice) : (uf * q).toDecimal(decimalObj.bills.totalPrice);
  915. let ttf = tf;
  916. deleteUselessFees(treeNode);
  917. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  918. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  919. if (treeNodeTools.isTotalCostBill(treeNode)){
  920. // 公式叶子清单没有暂估费,但总造价清单除外。
  921. calcEstimateFee(treeNode);
  922. treeNodeTools.initSummaryFee(treeNode);
  923. treeNode.data.summaryFees.totalFee = tf;
  924. treeNode.data.summaryFees.estimateFee = (treeNode.data.feesIndex && treeNode.data.feesIndex.estimate) ? treeNode.data.feesIndex.estimate.totalFee:0;
  925. let bill_safe = cbTools.findBill(fixedFlag.SAFETY_CONSTRUCTION);
  926. treeNode.data.summaryFees.safetyFee = (bill_safe && bill_safe.feesIndex && bill_safe.feesIndex.common)?bill_safe.feesIndex.common.totalFee:0;
  927. let bill_charge = cbTools.findBill(fixedFlag.CHARGE);
  928. treeNode.data.summaryFees.chargeFee = (bill_charge && bill_charge.feesIndex && bill_charge.feesIndex.common)?bill_charge.feesIndex.common.totalFee:0;
  929. }
  930. treeNode.data.calcTemplate = {"calcItems": []};
  931. }
  932. // 定额或叶子清单自己的计算程序计算
  933. else{
  934. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  935. if (treeNode.data.type == rationType.volumePrice){
  936. delete treeNode.data.gljList;
  937. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  938. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  939. treeNode.data.marketTotalFee = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  940. }
  941. else if (treeNode.data.type == rationType.gljRation){
  942. }
  943. else{
  944. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
  945. // 计算程序里没有暂估费的计算规则,会漏掉,所以这里要专门算。
  946. calcEstimateFee(treeNode);
  947. };
  948. if (treeNode.data.programID == undefined){
  949. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  950. };
  951. }
  952. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  953. let rations = me.project.Ration.getBillsSortRation(treeNode.source.getID());
  954. treeNode.data.gljList = me.project.ration_glj.getGatherGljArrByRations(rations);
  955. if (treeNode.data.programID == undefined){
  956. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  957. }
  958. // 叶子清单自己的计算程序计算,其暂估费也要汇总算。
  959. calcEstimateFee(treeNode);
  960. };
  961. let template = me.compiledTemplates[treeNode.data.programID];
  962. treeNode.data.calcTemplate = template;
  963. if (treeNode && template.hasCompiled) {
  964. let $CE = executeObj;
  965. $CE.treeNode = treeNode;
  966. $CE.template = template;
  967. $CE.calcBase = me.compiledCalcBases;
  968. treeNodeTools.initFees(treeNode);
  969. for (let idx of template.compiledSeq) {
  970. let calcItem = template.calcItems[idx];
  971. let feeRate = calcItem.feeRate;
  972. if (!feeRate) feeRate = 100; // 100%
  973. feeRate = feeRate.toDecimal(decimalObj.feeRate);
  974. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  975. let quantity = treeNode.data.quantity;
  976. if (!quantity) quantity = 0
  977. else quantity = parseFloat(quantity).toDecimal(decimalObj.decimal('quantity', treeNode));
  978. calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  979. treeNodeTools.checkFeeField(treeNode, calcItem);
  980. };
  981. };
  982. };
  983. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  984. };
  985. // 计算本节点、所有父节点(默认,可选)、公式引用节点(默认,可选)。
  986. calculate(treeNode, calcParents = true, calcFormulas = true){
  987. let me = this;
  988. let changedNodes = [];
  989. me.innerCalc(treeNode, changedNodes);
  990. if (treeNode.changed) {
  991. // 计算父结点
  992. if (calcParents){
  993. let curNode = treeNode.parent;
  994. while (curNode){
  995. me.innerCalc(curNode, changedNodes);
  996. curNode = curNode.parent;
  997. };
  998. };
  999. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1000. if (calcFormulas) {
  1001. me.calcFormulaNodes(changedNodes);
  1002. };
  1003. };
  1004. return changedNodes;
  1005. };
  1006. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1007. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  1008. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1009. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  1010. */
  1011. calcAllNodes(calcType = calcAllType.catAll){
  1012. let me = this;
  1013. let changedNodes = [];
  1014. function calcNodes(nodes) {
  1015. for (let node of nodes) {
  1016. if (node.children.length > 0) {
  1017. calcNodes(node.children);
  1018. };
  1019. if ((calcType == calcAllType.catAll || calcType == node.sourceType) && node.calcType != treeNodeCalcType.ctCalcBaseValue) {
  1020. me.innerCalc(node, changedNodes);
  1021. };
  1022. }
  1023. };
  1024. calcNodes(me.project.mainTree.roots);
  1025. me.calcFormulaNodes(changedNodes);
  1026. return changedNodes;
  1027. };
  1028. // 计算全部公式项。 (参数意义:将通过本方法后发生改变的节点存入changedNodesArr中)
  1029. calcFormulaNodes(changedArr){
  1030. let me = this;
  1031. let formulaNodes = cbTools.getFormulaNodes(true);
  1032. if (formulaNodes.length == 0) return;
  1033. for (let formulaNode of formulaNodes){
  1034. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  1035. projectObj.project.calcBase.calculate(formulaNode, true);
  1036. if (projectObj.project.calcBase.success){
  1037. // 计算公式结点
  1038. me.innerCalc(formulaNode, changedArr);
  1039. // 计算父结点
  1040. if (formulaNode.changed){
  1041. let curNode = formulaNode.parent;
  1042. while (curNode){
  1043. me.innerCalc(curNode, changedArr);
  1044. curNode = curNode.parent;
  1045. };
  1046. };
  1047. };
  1048. };
  1049. };
  1050. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  1051. calcLeafAndSave(treeNode){
  1052. let me = this;
  1053. if(!treeNodeTools.isLeafBill(treeNode)) return;
  1054. if (treeNode.children && treeNode.children.length > 0) {
  1055. let changedNodes = [];
  1056. for (let child of treeNode.children){
  1057. me.innerCalc(child, changedNodes);
  1058. };
  1059. let curChangeds = me.calculate(treeNode);
  1060. mergeArr(changedNodes, curChangeds);
  1061. me.saveNodes(changedNodes);
  1062. };
  1063. };
  1064. // 计算多条零散的定额,并计算他们所属的清单、父级清单,然后打包存储。如:批量替换工料机后受影响的定额。
  1065. calcRationsAndSave(rationNodes){
  1066. let me = this, leafBills = [], allChangedNodes = [];
  1067. for (let node of rationNodes) {
  1068. me.innerCalc(node, allChangedNodes);
  1069. let leafBill = node.parent;
  1070. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1071. if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  1072. };
  1073. for (let node of leafBills){
  1074. let curChangeds = me.calculate(node);
  1075. mergeArr(allChangedNodes, curChangeds);
  1076. };
  1077. me.saveNodes(allChangedNodes);
  1078. };
  1079. // 计算多条零散的清单,并计算他们的父清单、引用清单,然后打包存储。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1080. calcBillsAndSave(billNodes){
  1081. let me = this, allChangedNodes = [];
  1082. for (let node of billNodes) {
  1083. let curChangeds = me.calculate(node, true, false);
  1084. mergeArr(allChangedNodes, curChangeds);
  1085. };
  1086. me.calcFormulaNodes(allChangedNodes);
  1087. me.saveNodes(allChangedNodes);
  1088. };
  1089. // 计算并保存指定的一个树节点。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1090. // 这个方法实际上封装了calculate()和saveNodes()两个方法,主要目的是为了外部调用方便,少写一点累赘代码。
  1091. calcAndSave(treeNode){
  1092. let changedNodes = this.calculate(treeNode);
  1093. this.saveNodes(changedNodes);
  1094. };
  1095. }