calc_program.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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.getCompositionByCode(glj.code);
  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["base_price"] ? md["base_price"] : 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. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  664. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  665. subType: node.data.subType,
  666. quantity: node.data.quantity,
  667. calcBase: node.data.calcBase,
  668. calcBaseValue: node.data.calcBaseValue,
  669. programID: node.data.programID,
  670. marketUnitFee: node.data.marketUnitFee,
  671. marketTotalFee: node.data.marketTotalFee,
  672. fees: node.data.fees,
  673. isFromDetail:node.data.isFromDetail,
  674. feeRate: node.data.feeRate,
  675. feeRateID: node.data.feeRateID,
  676. contain:node.data.contain,
  677. quantityEXP:node.data.quantityEXP
  678. };
  679. if(node.sourceType==ModuleNames.ration && node.data.type==rationType.gljRation){//定额类型的工料机做特殊处理
  680. data.code=node.data.code;
  681. data.projectGLJID = node.data.projectGLJID;
  682. delete data.marketUnitFee;
  683. }
  684. let newData = {'updateType': 'ut_update', 'updateData': data};
  685. me.project.push(node.sourceType, [newData]);
  686. }
  687. };
  688. me.project.endUpdate();
  689. for (let node of treeNodes){delete node.changed};
  690. projectObj.mainController.refreshTreeNode(treeNodes);
  691. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  692. calcProgramObj.showData(me.project.mainTree.selected, false);
  693. };
  694. };
  695. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  696. innerCalc(treeNode, changedArr){
  697. let me = this;
  698. // 仅用作树节点显示的工料机不能参与计算。
  699. if (treeNode.sourceType === me.project.ration_glj.getSourceType()) return;
  700. treeNode.calcType = treeNodeTools.getCalcType(treeNode);
  701. // if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue) return;
  702. function initFees(treeNode){
  703. if (!treeNode.data.fees) {
  704. treeNode.data.fees = [];
  705. treeNode.data.feesIndex = {};
  706. treeNode.changed = true;
  707. };
  708. };
  709. function checkFee(treeNode, feeObj){
  710. if (feeObj.fieldName == '') return;
  711. if (!treeNode.data.feesIndex[feeObj.fieldName]){
  712. let fee = {
  713. 'fieldName': feeObj.fieldName,
  714. 'unitFee': feeObj.unitFee,
  715. 'totalFee': feeObj.totalFee,
  716. 'tenderUnitFee': 0,
  717. 'tenderTotalFee': 0
  718. };
  719. treeNode.data.fees.push(fee);
  720. treeNode.data.feesIndex[feeObj.fieldName] = fee;
  721. treeNode.changed = true;
  722. }
  723. else{
  724. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  725. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  726. treeNode.changed = true;
  727. };
  728. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  729. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  730. treeNode.changed = true;
  731. };
  732. };
  733. };
  734. function isBaseFeeType(type){
  735. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  736. };
  737. // 父清单汇总子项(定额或子清单)的费用类别
  738. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees ||
  739. treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  740. treeNode.data.programID = null;
  741. initFees(treeNode);
  742. let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees) ? me.project.Ration.getRationsByNode(treeNode) : treeNode.children;
  743. let rst = [];
  744. for (let ft of cpFeeTypes) {
  745. let ftObj = {};
  746. ftObj.fieldName = ft.type;
  747. ftObj.name = ft.name;
  748. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  749. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  750. for (let item of objsArr) {
  751. let data = item.data;
  752. if (data.feesIndex && data.feesIndex[ft.type]) {
  753. buf = (buf + parseFloatPlus(data.feesIndex[ft.type].unitFee)).toDecimal(decimalObj.process);
  754. btf = (btf + parseFloatPlus(data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  755. btuf = (btuf + parseFloatPlus(data.feesIndex[ft.type].tenderUnitFee)).toDecimal(decimalObj.process);
  756. bttf = (bttf + parseFloatPlus(data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  757. };
  758. };
  759. }
  760. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  761. let sum_rtf = 0, sum_rttf = 0;
  762. let bq = parseFloat(treeNode.data.quantity ? treeNode.data.quantity : 1);
  763. for (let data of objsArr) {
  764. let rq = parseFloat(data.quantity ? data.quantity : 0);
  765. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  766. if (data.feesIndex && data.feesIndex[ft.type]) {
  767. ruf = parseFloat(data.feesIndex[ft.type].unitFee);
  768. rtuf = parseFloat(data.feesIndex[ft.type].tenderUnitFee);
  769. rtf = parseFloat(data.feesIndex[ft.type].totalFee);
  770. rttf = parseFloat(data.feesIndex[ft.type].tenderTotalFee);
  771. };
  772. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  773. buf = (buf + (ruf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  774. btuf = (btuf + (rtuf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  775. };
  776. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  777. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  778. };
  779. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse || me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) {
  780. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  781. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  782. };
  783. if (isBaseFeeType(ft.type) || (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  784. btf = sum_rtf;
  785. bttf = sum_rttf;
  786. }
  787. else{
  788. btf = (buf * bq).toDecimal(decimalObj.process);
  789. bttf = (btuf * bq).toDecimal(decimalObj.process);
  790. };
  791. };
  792. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  793. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  794. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  795. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  796. checkFee(treeNode, ftObj);
  797. rst.push(ftObj);
  798. };
  799. treeNode.data.calcTemplate = {"calcItems": rst};
  800. }
  801. // 叶子清单的手工综合单价计算
  802. else if (treeNode.calcType == treeNodeCalcType.ctCommonUnitFee){
  803. delete treeNode.data.gljList;
  804. if (treeNode.data.calcBase) treeNode.data.calcBase = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  805. if (treeNode.data.calcBaseValue) treeNode.data.calcBaseValue = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  806. if (treeNode.data.programID) treeNode.data.programID = null;
  807. let uf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.unitFee) ? treeNode.data.feesIndex.common.unitFee : 0;
  808. let tuf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee) ? treeNode.data.feesIndex.common.tenderUnitFee : 0;
  809. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  810. let tf = (uf * q).toDecimal(decimalObj.bills.totalPrice);
  811. let ttf = (tuf * q).toDecimal(decimalObj.bills.totalPrice);
  812. delete treeNode.data.fees; // 直接删掉再新增,不用一个个费判断更新,效率更高。
  813. delete treeNode.data.feesIndex;
  814. treeNodeTools.initFeeField(treeNode, 'common');
  815. treeNode.data.feesIndex.common.unitFee = uf.toDecimal(decimalObj.bills.unitPrice);
  816. treeNode.data.feesIndex.common.totalFee = tf.toDecimal(decimalObj.bills.totalPrice);
  817. treeNode.data.feesIndex.common.tenderUnitFee = tuf.toDecimal(decimalObj.bills.unitPrice);
  818. treeNode.data.feesIndex.common.tenderTotalFee = ttf.toDecimal(decimalObj.bills.totalPrice);
  819. treeNode.changed = true;
  820. treeNode.data.calcTemplate = {"calcItems": []};
  821. }
  822. // 叶子清单公式计算
  823. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  824. delete treeNode.data.gljList;
  825. if (treeNode.data.programID) treeNode.data.programID = null;
  826. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  827. if (!treeNode.data.quantity) treeNode.data.quantity = 1;
  828. let q = treeNode.data.quantity;
  829. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  830. let uf = (b * f * q / 100).toDecimal(decimalObj.bills.unitPrice);
  831. let tuf = uf;
  832. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100).toDecimal(decimalObj.bills.totalPrice) : (uf * q).toDecimal(decimalObj.bills.totalPrice);
  833. let ttf = tf;
  834. delete treeNode.data.fees; // 直接删掉再新增,不用一个个费判断更新,效率更高。
  835. delete treeNode.data.feesIndex;
  836. treeNodeTools.initFeeField(treeNode, 'common');
  837. treeNode.data.feesIndex.common.unitFee = uf;
  838. treeNode.data.feesIndex.common.totalFee = tf;
  839. treeNode.data.feesIndex.common.tenderUnitFee = tuf;
  840. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  841. treeNode.changed = true;
  842. treeNode.data.calcTemplate = {"calcItems": []};
  843. }
  844. // 定额或叶子清单自己的计算程序计算
  845. else{
  846. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  847. if (treeNode.data.type == rationType.volumePrice){
  848. delete treeNode.data.gljList;
  849. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  850. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  851. treeNode.data.marketTotalFee = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  852. }
  853. else if (treeNode.data.type == rationType.gljRation){
  854. }
  855. else{
  856. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
  857. };
  858. if (treeNode.data.programID == undefined){
  859. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  860. };
  861. }
  862. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  863. let rations = me.project.Ration.getBillsSortRation(treeNode.source.getID());
  864. treeNode.data.gljList = me.project.ration_glj.getGatherGljArrByRations(rations);
  865. if (treeNode.data.programID == undefined){
  866. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  867. }
  868. };
  869. let template = me.compiledTemplates[treeNode.data.programID];
  870. treeNode.data.calcTemplate = template;
  871. if (treeNode && template.hasCompiled) {
  872. let $CE = executeObj;
  873. $CE.treeNode = treeNode;
  874. $CE.template = template;
  875. $CE.calcBase = me.compiledCalcBases;
  876. initFees(treeNode);
  877. for (let idx of template.compiledSeq) {
  878. let calcItem = template.calcItems[idx];
  879. let feeRate = calcItem.feeRate;
  880. if (!feeRate) feeRate = 100; // 100%
  881. feeRate = feeRate.toDecimal(decimalObj.feeRate);
  882. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  883. let quantity = treeNode.data.quantity;
  884. if (!quantity) quantity = 0
  885. else quantity = parseFloat(quantity).toDecimal(decimalObj.decimal('quantity', treeNode));
  886. calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  887. checkFee(treeNode, calcItem);
  888. };
  889. }
  890. };
  891. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  892. };
  893. // 计算本节点、所有父节点(默认,可选)、公式引用节点。
  894. calculate(treeNode, calcParents = true){
  895. let me = this;
  896. let changedNodes = [];
  897. me.innerCalc(treeNode, changedNodes);
  898. if (treeNode.changed) {
  899. // 计算父结点
  900. if (calcParents){
  901. let curNode = treeNode.parent;
  902. while (curNode){
  903. me.innerCalc(curNode, changedNodes);
  904. curNode = curNode.parent;
  905. };
  906. };
  907. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  908. me.calcFormulaNodes(changedNodes);
  909. };
  910. return changedNodes;
  911. };
  912. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。
  913. 参数取值如下:
  914. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  915. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  916. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到) */
  917. calcAllNodes(calcType = calcAllType.catAll){
  918. let me = this;
  919. let changedNodes = [];
  920. function calcNodes(nodes) {
  921. for (let node of nodes) {
  922. if (node.children.length > 0) {
  923. calcNodes(node.children);
  924. };
  925. if ((calcType == calcAllType.catAll) || (calcType == node.sourceType)) {
  926. me.calculate(node, false);
  927. if (node.changed) changedNodes.push(node);
  928. };
  929. }
  930. };
  931. calcNodes(me.project.mainTree.roots);
  932. // me.saveNodes(changedNodes); 保存要与计算分离,否则实际应用场景中,会产生多次通讯。
  933. return changedNodes;
  934. };
  935. // 计算全部公式项。 (参数意义:将通过本方法后发生改变的节点存入changedNodesArr中)
  936. calcFormulaNodes(changedArr){
  937. let me = this;
  938. let formulaNodes = treeNodeTools.getFormulaNodes();
  939. if (formulaNodes.length == 0) return;
  940. for (let formulaNode of formulaNodes){
  941. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  942. projectObj.project.calcBase.calculate(formulaNode, true);
  943. if (projectObj.project.calcBase.success){
  944. // 计算公式结点
  945. me.innerCalc(formulaNode, changedArr);
  946. // 计算父结点
  947. if (formulaNode.changed){
  948. let curNode = formulaNode.parent;
  949. while (curNode){
  950. me.innerCalc(curNode, changedArr);
  951. curNode = curNode.parent;
  952. };
  953. };
  954. };
  955. };
  956. };
  957. // 计算叶子清单下的所有子结点(如定额、量价、工料机定额等), 并计算自身和所有父结点。最后打包存储。
  958. calcLeafAndSave(treeNode){
  959. let me = this;
  960. if(!treeNodeTools.isLeafBill(treeNode)) return;
  961. if (treeNode.children && treeNode.children.length > 0) {
  962. let changedNodes = [];
  963. for (let child of treeNode.children){
  964. me.calculate(child, false);
  965. if (child.changed) changedNodes.push(child);
  966. };
  967. me.calculate(treeNode);
  968. let cur = treeNode;
  969. while (cur) {
  970. if (cur.changed) changedNodes.push(cur);
  971. cur = cur.parent;
  972. };
  973. me.saveNodes(changedNodes);
  974. };
  975. };
  976. // 计算多条零散的定额,并计算他们所属的清单、父级清单,然后打包存储。如:批量替换工料机后受影响的定额。
  977. calcRationsAndSave(rationNodes){
  978. let me = this, leafBills = [], changedNodes = [];
  979. for (let node of rationNodes) {
  980. me.innerCalc(node, changedNodes);
  981. let leafBill = node.parent;
  982. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  983. if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  984. };
  985. for (let node of leafBills){
  986. me.calculate(node);
  987. };
  988. me.saveNodes(changedNodes);
  989. };
  990. // 计算并保存指定的一个树节点。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  991. // 这个方法实际上封装了calculate()和saveNodes()两个方法,主要目的是为了外部调用方便,少写一点累赘代码。
  992. calcAndSave(treeNode){
  993. let changedNodes = this.calculate(treeNode);
  994. this.saveNodes(changedNodes);
  995. };
  996. }