calc_program.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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(me.digit);
  314. mdSum = (mdSum).toDecimal(me.digitDefault);
  315. }
  316. };
  317. result = result + (glj["quantity"] * mdSum).toDecimal(me.digitDefault);
  318. result = (result).toDecimal(me.digitDefault);
  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 == baseCalc){ price = parseFloat(glj["basePrice"]);}
  330. else if (base.calcType == adjustCalc){price = parseFloat(glj["adjustPrice"]);}
  331. else if (base.calcType == budgetCalc){price = parseFloat(glj["marketPrice"]);}
  332. else if (base.calcType == diffCalc){
  333. let aprice = glj["adjustPrice"] ? glj["adjustPrice"] : 0;
  334. let mprice = glj["marketPrice"] ? glj["marketPrice"] : 0;
  335. price = (parseFloat(mprice) - parseFloat(aprice)).toDecimal(me.digitDefault);
  336. };
  337. result = result + (glj["quantity"] * price).toDecimal(me.digitDefault);
  338. result = (result).toDecimal(me.digitDefault);
  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.calcBase ? me.treeNode.data.calcBase : 0;
  370. let q = me.treeNode.data.quantity ? me.treeNode.data.quantity : 1;
  371. let u = (p / q).toDecimal(me.digit);
  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. };
  388. doAfterUpdate (err, data) {
  389. if(!err){
  390. // do
  391. }
  392. };
  393. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  394. compileAllTemps(){
  395. let me = this;
  396. me.digit = 2;
  397. me.digitDefault = 6;
  398. me.compiledFeeRates = {};
  399. me.compiledLabourCoes = {};
  400. me.compiledTemplates = {};
  401. me.compiledTemplateMaps = {};
  402. me.compiledTemplateNames = [];
  403. me.compiledFeeTypeMaps = {};
  404. me.compiledFeeTypeNames = [];
  405. me.compiledCalcBases = {};
  406. me.saveForReports = [];
  407. me.feeRates = this.project.FeeRate.datas.rates;
  408. me.labourCoes = this.project.labourCoe.datas.coes;
  409. me.feeTypes = feeType;
  410. me.calcBases = rationCalcBase;
  411. me.templates = this.project.calcProgram.datas.templates;
  412. me.templates.push(defaultBillTemplate);
  413. // 先编译公用的基础数据
  414. me.compilePublics();
  415. for (let t of me.templates){
  416. me.compileTemplate(t);
  417. };
  418. // 存储费率临时数据,报表用。
  419. if (me.saveForReports.length > 0){
  420. let saveDatas = {};
  421. saveDatas.projectID = projectInfoObj.projectInfo.ID;
  422. saveDatas.calcItems = me.saveForReports;
  423. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (data) {
  424. me.saveForReports = [];
  425. });
  426. };
  427. };
  428. compilePublics(){
  429. let me = this;
  430. for (let rate of me.feeRates) {
  431. me.compiledFeeRates[rate.ID] = rate;
  432. }
  433. for (let coe of me.labourCoes) {
  434. me.compiledLabourCoes[coe.ID] = coe;
  435. }
  436. for (let ft of me.feeTypes) {
  437. me.compiledFeeTypeMaps[ft.type] = ft.name;
  438. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  439. me.compiledFeeTypeNames.push(ft.name);
  440. }
  441. for (let cb of me.calcBases) {
  442. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  443. }
  444. };
  445. compileTemplate(template){
  446. let me = this;
  447. me.compiledTemplates[template.ID] = template;
  448. me.compiledTemplateMaps[template.ID] = template.name;
  449. me.compiledTemplateMaps[template.name] = template.ID;
  450. me.compiledTemplateNames.push(template.name);
  451. template.hasCompiled = false;
  452. template.errs = [];
  453. let private_extract_ID = function(str, idx){
  454. let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  455. for (let i = idx; i < str.length; i++) {
  456. if (str[i] === '(') {
  457. lBracket++;
  458. if (lBracket == 1) firstIdx = i + 1;
  459. }
  460. if (str[i] === ')') {
  461. rBracket++;
  462. if (lBracket == rBracket) {
  463. lastIdx = i - 1;
  464. if (lastIdx > firstIdx) {
  465. if (str[firstIdx] === "'") firstIdx++;
  466. if (str[lastIdx] !== "'") lastIdx++;
  467. if (lastIdx > firstIdx) {
  468. rst = str.slice(firstIdx, lastIdx);
  469. }
  470. }
  471. break;
  472. }
  473. }
  474. }
  475. return rst;
  476. };
  477. let private_parse_ref = function(item, itemIdx){
  478. let idx = item.expression.indexOf('@(', 0);
  479. while (idx >= 0) {
  480. let ID = private_extract_ID(item.expression, idx);
  481. if (ID.length > 0) {
  482. let subItem = template.compiledCalcItems[ID];
  483. if (subItem) {
  484. if (subItem.ID !== item.ID) {
  485. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  486. } else {
  487. template.errs.push("There exists the self refer ID: " + ID);
  488. }
  489. } else {
  490. template.errs.push("There exists the invalid ID by which could not find the item: " + ID);
  491. console.log('invalid ID: ' + ID);
  492. }
  493. }
  494. idx = item.expression.indexOf('@(', idx + ID.length + 3);
  495. }
  496. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  497. template.compiledSeq.push(itemIdx);
  498. }
  499. };
  500. let private_setup_seq = function(item, itemIdx){
  501. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  502. private_parse_ref(item, itemIdx);
  503. }
  504. };
  505. let private_compile_items = function() {
  506. for (let idx of template.compiledSeq) {
  507. let item = template.calcItems[idx];
  508. item.dispExprUser = item.dispExpr; // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  509. if (item.expression == 'HJ')
  510. item.compiledExpr = '$CE.HJ()'
  511. else{
  512. item.compiledExpr = item.expression.split('@(').join('$CE.at(');
  513. item.compiledExpr = item.compiledExpr.split('base(').join('$CE.base(');
  514. };
  515. if (item.labourCoeID){
  516. let lc = me.compiledLabourCoes[item.labourCoeID].coe;
  517. item.dispExprUser = item.dispExpr.replace(/L/gi, lc.toString());
  518. item.compiledExpr = item.compiledExpr.replace(/L/gi, lc.toString());
  519. };
  520. if (item.feeRateID) {
  521. let orgFeeRate = item.feeRate;
  522. let cmf = me.compiledFeeRates[item.feeRateID];
  523. item.feeRate = cmf?cmf.rate:100;
  524. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  525. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  526. }
  527. };
  528. // 字段名映射
  529. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  530. }
  531. };
  532. if (template && template.calcItems && template.calcItems.length > 0) {
  533. template.compiledSeq = [];
  534. template.compiledCalcItems = {};
  535. for (let i = 0; i < template.calcItems.length; i++) {
  536. let item = template.calcItems[i];
  537. template.compiledCalcItems[item.ID] = item;
  538. template.compiledCalcItems[item.ID + "_idx"] = i;
  539. }
  540. for (let i = 0; i < template.calcItems.length; i++) {
  541. private_setup_seq(template.calcItems[i], i);
  542. }
  543. if (template.errs.length == 0) {
  544. private_compile_items();
  545. template.hasCompiled = true;
  546. } else {
  547. console.log('errors: ' + template.errs.toString());
  548. }
  549. };
  550. };
  551. isLeafBill(treeNode){
  552. let me = this;
  553. return treeNode.sourceType === me.project.Bills.getSourceType() &&
  554. treeNode.source.children &&
  555. treeNode.source.children.length === 0;
  556. };
  557. // 仅内部调用。注意:外部不能直接使用,因为这里传入的树节点必须有一定的初始化。
  558. InnerCalc(treeNode){
  559. let me = this;
  560. let project = me.project;
  561. function initFees(treeNode){
  562. if (!treeNode.data.fees) {
  563. treeNode.data.fees = [];
  564. treeNode.data.feesIndex = {};
  565. treeNode.changed = true;
  566. };
  567. };
  568. function checkFee(treeNode, feeObj){
  569. if (feeObj.fieldName == '') return;
  570. if (!treeNode.data.feesIndex[feeObj.fieldName]){
  571. let fee = {
  572. 'fieldName': feeObj.fieldName,
  573. 'unitFee': feeObj.unitFee,
  574. 'totalFee': feeObj.totalFee,
  575. 'tenderUnitFee': 0,
  576. 'tenderTotalFee': 0
  577. };
  578. treeNode.data.fees.push(fee);
  579. treeNode.data.feesIndex[feeObj.fieldName] = fee;
  580. treeNode.changed = true;
  581. }
  582. else{
  583. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  584. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  585. treeNode.changed = true;
  586. };
  587. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  588. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  589. treeNode.changed = true;
  590. };
  591. };
  592. };
  593. // 汇总定额或子清单的费用类别
  594. if (treeNode.calcType == treeNodeCalcType.ctGatherRations || treeNode.calcType == treeNodeCalcType.ctGatherBills){
  595. treeNode.data.programID = null;
  596. initFees(treeNode);
  597. let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRations) ? project.Ration.getRationsByNode(treeNode) : treeNode.children;
  598. let rst = [];
  599. for (let ft of feeType) {
  600. let ftObj = {};
  601. ftObj.fieldName = ft.type;
  602. ftObj.name = ft.name;
  603. let uf = 0, tf = 0, tuf = 0, ttf = 0;
  604. for (let item of objsArr) {
  605. let data = (treeNode.calcType == treeNodeCalcType.ctGatherRations) ? item : item.data;
  606. if (data.feesIndex && data.feesIndex[ft.type]) {
  607. uf = (uf + parseFloat(data.feesIndex[ft.type].unitFee)).toDecimal(me.digitDefault);
  608. tf = (tf + parseFloat(data.feesIndex[ft.type].totalFee)).toDecimal(me.digitDefault);
  609. tuf = (tuf + parseFloat(data.feesIndex[ft.type].tenderUnitFee)).toDecimal(me.digitDefault);
  610. ttf = (ttf + parseFloat(data.feesIndex[ft.type].tenderTotalFee)).toDecimal(me.digitDefault);
  611. };
  612. };
  613. ftObj.unitFee = uf.toDecimal(me.digit);
  614. ftObj.totalFee = tf.toDecimal(me.digit);
  615. ftObj.tenderUnitFee = tuf.toDecimal(me.digit);
  616. ftObj.tenderTotalFee = ttf.toDecimal(me.digit);
  617. checkFee(treeNode, ftObj);
  618. rst.push(ftObj);
  619. };
  620. treeNode.data.calcTemplate = {"calcItems": rst};
  621. }
  622. else{
  623. // 叶子清单的公式计算:使用缺省清单计算程序。需要提供总金额作为计算基数(不需要工料机),然后每条按比例(费率)计算,不需要工料机明细。
  624. if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  625. delete treeNode.data.gljList;
  626. if (treeNode.data.programID == undefined){
  627. treeNode.data.programID = defaultBillTemplate.ID;
  628. };
  629. }
  630. else if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  631. if (treeNode.data.type == rationType.volumePrice){
  632. delete treeNode.data.gljList;
  633. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  634. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  635. treeNode.data.marketTotalFee = (muf * q).toDecimal(me.digit);
  636. }
  637. else if (treeNode.data.type == rationType.gljRation){
  638. }
  639. else{
  640. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
  641. };
  642. if (treeNode.data.programID == undefined){
  643. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  644. };
  645. }
  646. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  647. let rations = project.Ration.getBillsSortRation(treeNode.source.getID());
  648. treeNode.data.gljList = project.ration_glj.getGatherGljArrByRations(rations);
  649. if (treeNode.data.programID == undefined || treeNode.data.programID == defaultBillTemplate.ID){
  650. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  651. }
  652. };
  653. let template = me.compiledTemplates[treeNode.data.programID];
  654. treeNode.data.calcTemplate = template;
  655. if (treeNode && template.hasCompiled) {
  656. let $CE = executeObj;
  657. $CE.treeNode = treeNode;
  658. $CE.template = template;
  659. $CE.calcBase = me.compiledCalcBases;
  660. initFees(treeNode);
  661. for (let idx of template.compiledSeq) {
  662. let calcItem = template.calcItems[idx];
  663. let feeRate = calcItem.feeRate;
  664. if (!feeRate) feeRate = 100; // 100%
  665. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(me.digit); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  666. let quantity = treeNode.data.quantity;
  667. if (!quantity) quantity = 0;
  668. calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(me.digit);
  669. checkFee(treeNode, calcItem);
  670. };
  671. }
  672. };
  673. };
  674. // 计算本节点(默认同时递归计算所有父节点,可选)
  675. calculate(treeNode, calcParents = true){
  676. let me = this;
  677. let isRation = treeNode.sourceType === me.project.Ration.getSourceType();
  678. let isBill = treeNode.sourceType === me.project.Bills.getSourceType();
  679. let isBillPriceCalc = me.project.projSetting.billsCalcMode === leafBillGetFeeType.billsPrice;
  680. if (isRation){
  681. treeNode.calcType = treeNodeCalcType.ctRationCalcProgram;
  682. }
  683. else if (me.isLeafBill(treeNode)) {
  684. if (treeNode.children && treeNode.children.length > 0){
  685. me.calcLeafBillChildren(treeNode);
  686. if (isBillPriceCalc) // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算
  687. treeNode.calcType = treeNodeCalcType.ctBillCalcProgram;
  688. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  689. treeNode.calcType = treeNodeCalcType.ctGatherRations;
  690. }
  691. else{ // 公式计算
  692. treeNode.calcType = treeNodeCalcType.ctCalcBaseValue;
  693. };
  694. }
  695. else if (isBill) // 父清单:汇总子清单的费用类别
  696. treeNode.calcType = treeNodeCalcType.ctGatherBills;
  697. me.InnerCalc(treeNode);
  698. // 计算所有父结点
  699. if (treeNode.changed && calcParents && treeNode.parent) {
  700. me.calculate(treeNode.parent);
  701. };
  702. };
  703. // 存储、刷新本节点(默认存储刷新所有父节点,可选)
  704. saveNode(treeNode, saveParents = true) {
  705. if (!treeNode.changed) return;
  706. let me = this;
  707. let nodesArr = [];
  708. let curNode = treeNode;
  709. while (curNode) {
  710. if (curNode.changed){nodesArr.push(curNode)};
  711. if (saveParents) curNode = curNode.parent
  712. else break;
  713. };
  714. me.saveNodes(nodesArr);
  715. };
  716. // 多个树结点入库存储,刷新界面显示。
  717. saveNodes(treeNodes){
  718. if (treeNodes.length < 1) return;
  719. let me = this;
  720. me.project.beginUpdate('');
  721. for (let node of treeNodes){
  722. if (node.changed){
  723. let data = {
  724. ID: node.data.ID,
  725. projectID: me.project.ID(),
  726. subType: node.data.subType,
  727. quantity: node.data.quantity,
  728. calcBase: node.data.calcBase,
  729. programID: node.data.programID,
  730. marketUnitFee: node.data.marketUnitFee,
  731. marketTotalFee: node.data.marketTotalFee,
  732. fees: node.data.fees,
  733. isFromDetail:node.data.isFromDetail
  734. };
  735. let newData = {'updateType': 'ut_update', 'updateData': data};
  736. me.project.push(node.sourceType, [newData]);
  737. }
  738. };
  739. me.project.endUpdate();
  740. for (let node of treeNodes){delete node.changed};
  741. projectObj.mainController.refreshTreeNode(treeNodes);
  742. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  743. calcProgramObj.showData(me.project.mainTree.selected, false);
  744. };
  745. };
  746. /* 计算所有树结点(分3种情况),并将发生计算改动的结点入库存储。
  747. 参数取值如下:
  748. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  749. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  750. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到) */
  751. calcAllNodes(calcType = calcAllType.catAll){
  752. let me = this;
  753. let needSaveNodes = [];
  754. function calcNodes(nodes) {
  755. for (let node of nodes) {
  756. if (node.children.length > 0) {
  757. calcNodes(node.children);
  758. };
  759. if ((calcType == calcAllType.catAll) || (calcType == node.sourceType)) {
  760. me.calculate(node, false);
  761. if (node.changed) needSaveNodes.push(node);
  762. };
  763. }
  764. };
  765. calcNodes(me.project.mainTree.roots);
  766. me.saveNodes(needSaveNodes);
  767. };
  768. // 重新计算叶子清单下的所有子结点:如定额、工料机定额等(calculate算法基于定额、工料机定额的计算结果是正确的,实际上有时它们的计算结果并不是最新的)
  769. calcLeafBillChildren(treeNode){
  770. let me = this;
  771. if (treeNode.children && treeNode.children.length > 0) {
  772. let needSaveNodes = [];
  773. for (let child of treeNode.children){
  774. me.calculate(child, false);
  775. if (child.changed) needSaveNodes.push(child);
  776. };
  777. me.saveNodes(needSaveNodes);
  778. };
  779. };
  780. }