calc_program.js 44 KB

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