calc_program.js 41 KB

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