calc_program.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。
  720. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  721. calcProgramObj.refreshCurNodeCalcItems(me.project.mainTree.selected);
  722. };
  723. };
  724. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  725. innerCalc(treeNode, changedArr){
  726. let me = this;
  727. // 仅用作树节点显示的工料机不能参与计算。
  728. if (treeNode.sourceType === me.project.ration_glj.getSourceType()) return;
  729. treeNode.calcType = treeNodeTools.getCalcType(treeNode);
  730. // if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue) return;
  731. function isBaseFeeType(type){
  732. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  733. };
  734. // 计算暂估费用
  735. function calcEstimateFee(treeNode){
  736. let sumU = 0, sumT = 0;
  737. if (treeNodeTools.isTotalCostBill(treeNode)){
  738. let nodes = projectObj.project.mainTree.roots;
  739. for (let node of nodes){
  740. if (treeNodeTools.isTotalCostBill(node)) break;
  741. let eU = 0, eT = 0;
  742. if (node.data.feesIndex && node.data.feesIndex.estimate){
  743. eU = node.data.feesIndex.estimate.unitFee;
  744. eT = node.data.feesIndex.estimate.totalFee;
  745. }
  746. else {
  747. eU = 0, eT = 0;
  748. };
  749. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  750. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  751. };
  752. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  753. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  754. }
  755. else{
  756. if (!treeNode.data.gljList) return;
  757. let eTypes = [
  758. gljType.GENERAL_MATERIAL, gljType.MAIN_MATERIAL, gljType.EQUIPMENT,
  759. gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO,
  760. gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR];
  761. let eDetailTypes = [gljType.MAIN_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO];
  762. let GLJObjs = [];
  763. for (let glj of treeNode.data.gljList) {
  764. if (eTypes.indexOf(glj.type) >= 0) {
  765. if (glj.isEstimate){
  766. 'code','name','specs','unit','type'
  767. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type,
  768. quantity: glj.quantity, marketPrice: glj.marketPrice});
  769. }
  770. else{ // 组成物
  771. if (eDetailTypes.indexOf(glj.type) >= 0){
  772. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  773. if (!mds) mds = [];
  774. for (let md of mds){
  775. if (md.isEstimate){
  776. let isExist = false;
  777. let mdQ = (parseFloatPlus(glj.quantity) * parseFloatPlus(md.consumption)).toDecimal(decimalObj.process);
  778. for (let obj of GLJObjs){
  779. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  780. isExist = true;
  781. obj.quantity = (parseFloatPlus(obj.quantity) + mdQ).toDecimal(decimalObj.process);
  782. break;
  783. }
  784. };
  785. if (!isExist)
  786. GLJObjs.push({code: md.code, quantity: mdQ, marketPrice: md.marketPrice});
  787. }
  788. }
  789. }
  790. }
  791. };
  792. };
  793. for (let obj of GLJObjs){
  794. sumU = sumU + (parseFloatPlus(obj.quantity) * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
  795. sumU = sumU.toDecimal(decimalObj.process);
  796. let q = (parseFloatPlus(obj.quantity) * parseFloatPlus(treeNode.data.quantity)).toDecimal(decimalObj.process);
  797. sumT = sumT + (q * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
  798. sumT = sumT.toDecimal(decimalObj.process);
  799. };
  800. sumU = sumU.toDecimal(decimalObj.bills.unitPrice);
  801. if (projectObj.project.property.zanguCalcMode == zanguCalcType.common){
  802. sumT = (parseFloatPlus(treeNode.data.quantity) * sumU).toDecimal(decimalObj.bills.totalPrice);
  803. }
  804. else if (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial){
  805. sumT = sumT.toDecimal(decimalObj.bills.totalPrice);
  806. };
  807. };
  808. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
  809. };
  810. // 删掉多余的费用。例如:从其它计算方式切换到公式计算方式,会多出其它的费(不光是common)
  811. function deleteUselessFees(treeNode){
  812. let reserveArr = treeNodeTools.isTotalCostBill(treeNode)? ['common', 'estimate']:['common'];
  813. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  814. let feesArr = treeNode.data.fees;
  815. for (let i = 0; i < feesArr.length; i++) {
  816. if (!reserveArr.includes(feesArr[i].fieldName)) {
  817. delete treeNode.data.feesIndex[feesArr[i].fieldName];
  818. feesArr.splice(i, 1);
  819. treeNode.changed = true;
  820. }
  821. }
  822. };
  823. };
  824. // 父清单汇总子项(定额或子清单)的费用类别
  825. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees ||
  826. treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  827. treeNode.data.programID = null;
  828. treeNodeTools.initFees(treeNode);
  829. let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees) ? me.project.Ration.getRationsByNode(treeNode) : treeNode.children;
  830. let rst = [];
  831. for (let ft of cpFeeTypes) {
  832. let ftObj = {};
  833. ftObj.fieldName = ft.type;
  834. ftObj.name = ft.name;
  835. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  836. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  837. for (let item of objsArr) {
  838. let data = item.data;
  839. if (data.feesIndex && data.feesIndex[ft.type]) {
  840. buf = (buf + parseFloatPlus(data.feesIndex[ft.type].unitFee)).toDecimal(decimalObj.process);
  841. btf = (btf + parseFloatPlus(data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  842. btuf = (btuf + parseFloatPlus(data.feesIndex[ft.type].tenderUnitFee)).toDecimal(decimalObj.process);
  843. bttf = (bttf + parseFloatPlus(data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  844. };
  845. };
  846. }
  847. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  848. let sum_rtf = 0, sum_rttf = 0;
  849. let bq = parseFloat(treeNode.data.quantity ? treeNode.data.quantity : 1);
  850. for (let data of objsArr) {
  851. let rq = parseFloat(data.quantity ? data.quantity : 0);
  852. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  853. if (data.feesIndex && data.feesIndex[ft.type]) {
  854. ruf = parseFloat(data.feesIndex[ft.type].unitFee);
  855. rtuf = parseFloat(data.feesIndex[ft.type].tenderUnitFee);
  856. rtf = parseFloat(data.feesIndex[ft.type].totalFee);
  857. rttf = parseFloat(data.feesIndex[ft.type].tenderTotalFee);
  858. };
  859. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  860. buf = (buf + (ruf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  861. btuf = (btuf + (rtuf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  862. };
  863. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  864. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  865. };
  866. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse || me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) {
  867. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  868. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  869. };
  870. if (isBaseFeeType(ft.type) || (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  871. btf = sum_rtf;
  872. bttf = sum_rttf;
  873. }
  874. else{
  875. btf = (buf * bq).toDecimal(decimalObj.process);
  876. bttf = (btuf * bq).toDecimal(decimalObj.process);
  877. };
  878. };
  879. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  880. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  881. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  882. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  883. treeNodeTools.checkFeeField(treeNode, ftObj);
  884. rst.push(ftObj);
  885. };
  886. treeNode.data.calcTemplate = {"calcItems": rst};
  887. }
  888. // 叶子清单的手工综合单价计算
  889. else if (treeNode.calcType == treeNodeCalcType.ctCommonUnitFee){
  890. delete treeNode.data.gljList;
  891. if (treeNode.data.calcBase) treeNode.data.calcBase = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  892. if (treeNode.data.calcBaseValue) treeNode.data.calcBaseValue = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  893. if (treeNode.data.programID) treeNode.data.programID = null;
  894. let uf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.unitFee) ? treeNode.data.feesIndex.common.unitFee : 0;
  895. uf = uf.toDecimal(decimalObj.bills.unitPrice);
  896. let tuf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee) ? treeNode.data.feesIndex.common.tenderUnitFee : 0;
  897. tuf = tuf.toDecimal(decimalObj.bills.unitPrice);
  898. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  899. let tf = (uf * q).toDecimal(decimalObj.bills.totalPrice);
  900. let ttf = (tuf * q).toDecimal(decimalObj.bills.totalPrice);
  901. deleteUselessFees(treeNode);
  902. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  903. treeNode.data.calcTemplate = {"calcItems": []};
  904. }
  905. // 叶子清单公式计算
  906. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  907. delete treeNode.data.gljList;
  908. if (treeNode.data.programID) treeNode.data.programID = null;
  909. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  910. if (!treeNode.data.quantity) treeNode.data.quantity = 1;
  911. let q = treeNode.data.quantity;
  912. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  913. let uf = (b * f * q / 100).toDecimal(decimalObj.bills.unitPrice);
  914. let tuf = uf;
  915. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100).toDecimal(decimalObj.bills.totalPrice) : (uf * q).toDecimal(decimalObj.bills.totalPrice);
  916. let ttf = tf;
  917. deleteUselessFees(treeNode);
  918. treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  919. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  920. if (treeNodeTools.isTotalCostBill(treeNode)){
  921. // 公式叶子清单没有暂估费,但总造价清单除外。
  922. calcEstimateFee(treeNode);
  923. treeNodeTools.initSummaryFee(treeNode);
  924. treeNode.data.summaryFees.totalFee = tf;
  925. treeNode.data.summaryFees.estimateFee = (treeNode.data.feesIndex && treeNode.data.feesIndex.estimate) ? treeNode.data.feesIndex.estimate.totalFee:0;
  926. let bill_safe = cbTools.findBill(fixedFlag.SAFETY_CONSTRUCTION);
  927. treeNode.data.summaryFees.safetyFee = (bill_safe && bill_safe.feesIndex && bill_safe.feesIndex.common)?bill_safe.feesIndex.common.totalFee:0;
  928. let bill_charge = cbTools.findBill(fixedFlag.CHARGE);
  929. treeNode.data.summaryFees.chargeFee = (bill_charge && bill_charge.feesIndex && bill_charge.feesIndex.common)?bill_charge.feesIndex.common.totalFee:0;
  930. }
  931. treeNode.data.calcTemplate = {"calcItems": []};
  932. }
  933. // 定额或叶子清单自己的计算程序计算
  934. else{
  935. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  936. if (treeNode.data.type == rationType.volumePrice){
  937. delete treeNode.data.gljList;
  938. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  939. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  940. treeNode.data.marketTotalFee = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  941. }
  942. else if (treeNode.data.type == rationType.gljRation){
  943. }
  944. else{
  945. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
  946. // 计算程序里没有暂估费的计算规则,会漏掉,所以这里要专门算。
  947. calcEstimateFee(treeNode);
  948. };
  949. if (treeNode.data.programID == undefined){
  950. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  951. };
  952. }
  953. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  954. let rations = me.project.Ration.getBillsSortRation(treeNode.source.getID());
  955. treeNode.data.gljList = me.project.ration_glj.getGatherGljArrByRations(rations);
  956. if (treeNode.data.programID == undefined){
  957. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  958. }
  959. // 叶子清单自己的计算程序计算,其暂估费也要汇总算。
  960. calcEstimateFee(treeNode);
  961. };
  962. let template = me.compiledTemplates[treeNode.data.programID];
  963. treeNode.data.calcTemplate = template;
  964. if (treeNode && template.hasCompiled) {
  965. let $CE = executeObj;
  966. $CE.treeNode = treeNode;
  967. $CE.template = template;
  968. $CE.calcBase = me.compiledCalcBases;
  969. treeNodeTools.initFees(treeNode);
  970. for (let idx of template.compiledSeq) {
  971. let calcItem = template.calcItems[idx];
  972. let feeRate = calcItem.feeRate;
  973. if (!feeRate) feeRate = 100; // 100%
  974. feeRate = feeRate.toDecimal(decimalObj.feeRate);
  975. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  976. let quantity = treeNode.data.quantity;
  977. if (!quantity) quantity = 0
  978. else quantity = parseFloat(quantity).toDecimal(decimalObj.decimal('quantity', treeNode));
  979. calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  980. treeNodeTools.checkFeeField(treeNode, calcItem);
  981. };
  982. };
  983. };
  984. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  985. };
  986. // 计算本节点、所有父节点(默认,可选)、公式引用节点(默认,可选)。
  987. calculate(treeNode, calcParents = true, calcFormulas = true){
  988. let me = this;
  989. let changedNodes = [];
  990. me.innerCalc(treeNode, changedNodes);
  991. if (treeNode.changed) {
  992. // 计算父结点
  993. if (calcParents){
  994. let curNode = treeNode.parent;
  995. while (curNode){
  996. me.innerCalc(curNode, changedNodes);
  997. curNode = curNode.parent;
  998. };
  999. };
  1000. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1001. if (calcFormulas) {
  1002. me.calcFormulaNodes(changedNodes);
  1003. };
  1004. };
  1005. return changedNodes;
  1006. };
  1007. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1008. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  1009. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1010. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  1011. */
  1012. calcAllNodes(calcType = calcAllType.catAll){
  1013. let me = this;
  1014. let changedNodes = [];
  1015. function calcNodes(nodes) {
  1016. for (let node of nodes) {
  1017. if (node.children.length > 0) {
  1018. calcNodes(node.children);
  1019. };
  1020. if ((calcType == calcAllType.catAll || calcType == node.sourceType) && node.calcType != treeNodeCalcType.ctCalcBaseValue) {
  1021. me.innerCalc(node, changedNodes);
  1022. };
  1023. }
  1024. };
  1025. calcNodes(me.project.mainTree.roots);
  1026. me.calcFormulaNodes(changedNodes);
  1027. return changedNodes;
  1028. };
  1029. // 计算全部公式项。 (参数意义:将通过本方法后发生改变的节点存入changedNodesArr中)
  1030. calcFormulaNodes(changedArr){
  1031. let me = this;
  1032. let formulaNodes = cbTools.getFormulaNodes(true);
  1033. if (formulaNodes.length == 0) return;
  1034. for (let formulaNode of formulaNodes){
  1035. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  1036. projectObj.project.calcBase.calculate(formulaNode, true);
  1037. if (projectObj.project.calcBase.success){
  1038. // 计算公式结点
  1039. me.innerCalc(formulaNode, changedArr);
  1040. // 计算父结点
  1041. if (formulaNode.changed){
  1042. let curNode = formulaNode.parent;
  1043. while (curNode){
  1044. me.innerCalc(curNode, changedArr);
  1045. curNode = curNode.parent;
  1046. };
  1047. };
  1048. };
  1049. };
  1050. };
  1051. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  1052. calcLeafAndSave(treeNode){
  1053. let me = this;
  1054. if(!treeNodeTools.isLeafBill(treeNode)) return;
  1055. if (treeNode.children && treeNode.children.length > 0) {
  1056. let changedNodes = [];
  1057. for (let child of treeNode.children){
  1058. me.innerCalc(child, changedNodes);
  1059. };
  1060. let curChangeds = me.calculate(treeNode);
  1061. mergeArr(changedNodes, curChangeds);
  1062. me.saveNodes(changedNodes);
  1063. };
  1064. };
  1065. // 计算多条零散的定额,并计算他们所属的清单、父级清单,然后打包存储。如:批量替换工料机后受影响的定额。
  1066. calcRationsAndSave(rationNodes){
  1067. let me = this, leafBills = [], allChangedNodes = [];
  1068. for (let node of rationNodes) {
  1069. me.innerCalc(node, allChangedNodes);
  1070. let leafBill = node.parent;
  1071. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1072. if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  1073. };
  1074. for (let node of leafBills){
  1075. let curChangeds = me.calculate(node);
  1076. mergeArr(allChangedNodes, curChangeds);
  1077. };
  1078. me.saveNodes(allChangedNodes);
  1079. };
  1080. // 计算多条零散的清单,并计算他们的父清单、引用清单,然后打包存储。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1081. calcBillsAndSave(billNodes){
  1082. let me = this, allChangedNodes = [];
  1083. for (let node of billNodes) {
  1084. let curChangeds = me.calculate(node, true, false);
  1085. mergeArr(allChangedNodes, curChangeds);
  1086. };
  1087. me.calcFormulaNodes(allChangedNodes);
  1088. me.saveNodes(allChangedNodes);
  1089. };
  1090. // 计算并保存指定的一个树节点。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1091. // 这个方法实际上封装了calculate()和saveNodes()两个方法,主要目的是为了外部调用方便,少写一点累赘代码。
  1092. calcAndSave(treeNode){
  1093. let changedNodes = this.calculate(treeNode);
  1094. this.saveNodes(changedNodes);
  1095. };
  1096. }