calc_program.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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, temp = 0;
  328. if (base.gljTypes.indexOf(glj.type) >= 0) {
  329. if (base.calcType == baseCalcType.diffCalc){
  330. let aprice = glj["adjustPrice"] ? glj["adjustPrice"] : 0;
  331. aprice = parseFloat(aprice);
  332. let mprice = glj["marketPrice"] ? glj["marketPrice"] : 0;
  333. mprice = parseFloat(mprice);
  334. temp = (glj["quantity"] * mprice).toDecimal(decimalObj.ration.unitPrice) - (glj["quantity"] * aprice).toDecimal(decimalObj.ration.unitPrice);
  335. temp = temp.toDecimal(decimalObj.ration.unitPrice);
  336. }
  337. else {
  338. if (base.calcType == baseCalcType.baseCalc){ price = parseFloat(glj["basePrice"]);}
  339. else if (base.calcType == baseCalcType.adjustCalc){price = parseFloat(glj["adjustPrice"]);}
  340. else if (base.calcType == baseCalcType.budgetCalc){price = parseFloat(glj["marketPrice"]);}
  341. temp = (glj["quantity"] * price).toDecimal(decimalObj.ration.unitPrice);
  342. };
  343. result = (result + temp).toDecimal(decimalObj.ration.unitPrice);
  344. };
  345. };
  346. return result;
  347. };
  348. // 量价没有具体的工料机类型,但仍然要用定额的计算程序,所以要给计算基数直接指定。
  349. function volumePriceFee() {
  350. let result = 0;
  351. if (
  352. ( me.treeNode.data.subType === gljType.LABOUR && base.dispName === '定额基价人工费') ||
  353. ( me.treeNode.data.subType === gljType.GENERAL_MATERIAL && base.dispName === '定额基价材料费') ||
  354. ( me.treeNode.data.subType === gljType.GENERAL_MACHINE && base.dispName === '定额基价机械费') ||
  355. ( me.treeNode.data.subType === gljType.MAIN_MATERIAL && base.dispName === '主材费') ||
  356. ( me.treeNode.data.subType === gljType.EQUIPMENT && base.dispName === '设备费')
  357. ) result = me.treeNode.data.marketUnitFee ? me.treeNode.data.marketUnitFee : 0;
  358. return result;
  359. };
  360. if (me.treeNode.data.type == rationType.volumePrice || me.treeNode.data.type == rationType.gljRation){
  361. rst = volumePriceFee();
  362. }
  363. else{
  364. if (isSubset(base.gljTypes, [gljType.MACHINE_LABOUR]))
  365. rst = machineLabourFee()
  366. else
  367. rst = commonGLJFee();
  368. }
  369. };
  370. return rst;
  371. },
  372. HJ: function () {
  373. let me = this;
  374. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  375. let q = me.treeNode.data.quantity ? me.treeNode.data.quantity : 1;
  376. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  377. return u;
  378. }
  379. };
  380. let treeNodeTools = {
  381. getFormulaNodes: function () {
  382. let nodes = [];
  383. for (let item of projectObj.project.mainTree.items){
  384. if (item.data.calcBase && item.data.calcBase != '') nodes.push(item);
  385. };
  386. return nodes;
  387. },
  388. isRation: function(treeNode){
  389. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.ration;
  390. },
  391. isLeafBill: function(treeNode){
  392. return treeNode.sourceType === projectObj.project.Bills.getSourceType() &&
  393. treeNode.source.children &&
  394. treeNode.source.children.length === 0;
  395. },
  396. isNullBill: function (treeNode) {
  397. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  398. },
  399. isVolumePrice: function (treeNode) {
  400. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.volumePrice;
  401. },
  402. isGljRation: function (treeNode) {
  403. return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.gljRation;
  404. }
  405. };
  406. class CalcProgram {
  407. constructor(project){
  408. let me = this;
  409. me.project = project;
  410. me.datas = [];
  411. project.registerModule(ModuleNames.calc_program, me);
  412. };
  413. getSourceType () {
  414. return ModuleNames.calc_program;
  415. };
  416. loadData (datas) {
  417. this.datas = datas;
  418. this.compileAllTemps();
  419. };
  420. doAfterUpdate (err, data) {
  421. if(!err){
  422. $.bootstrapLoading.end();
  423. }
  424. };
  425. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  426. compileAllTemps(){
  427. let me = this;
  428. me.compiledFeeRates = {};
  429. me.compiledLabourCoes = {};
  430. me.compiledTemplates = {};
  431. me.compiledTemplateMaps = {};
  432. me.compiledTemplateNames = [];
  433. me.compiledFeeTypeMaps = {};
  434. me.compiledFeeTypeNames = [];
  435. me.compiledCalcBases = {};
  436. me.saveForReports = [];
  437. me.feeRates = this.project.FeeRate.datas.rates;
  438. me.labourCoes = this.project.labourCoe.datas.coes;
  439. me.feeTypes = feeType;
  440. me.calcBases = rationCalcBase;
  441. me.templates = this.project.calcProgram.datas.templates;
  442. // me.templates.push(defaultBillTemplate);
  443. // 先编译公用的基础数据
  444. me.compilePublics();
  445. for (let t of me.templates){
  446. me.compileTemplate(t);
  447. };
  448. // 存储费率临时数据,报表用。
  449. if (me.saveForReports.length > 0){
  450. let saveDatas = {};
  451. saveDatas.projectID = projectInfoObj.projectInfo.ID;
  452. saveDatas.calcItems = me.saveForReports;
  453. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (data) {
  454. me.saveForReports = [];
  455. });
  456. };
  457. };
  458. compilePublics(){
  459. let me = this;
  460. for (let rate of me.feeRates) {
  461. me.compiledFeeRates[rate.ID] = rate;
  462. }
  463. for (let coe of me.labourCoes) {
  464. me.compiledLabourCoes[coe.ID] = coe;
  465. }
  466. for (let ft of me.feeTypes) {
  467. me.compiledFeeTypeMaps[ft.type] = ft.name;
  468. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  469. me.compiledFeeTypeNames.push(ft.name);
  470. }
  471. for (let cb of me.calcBases) {
  472. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  473. }
  474. };
  475. compileTemplate(template){
  476. let me = this;
  477. me.compiledTemplates[template.ID] = template;
  478. me.compiledTemplateMaps[template.ID] = template.name;
  479. me.compiledTemplateMaps[template.name] = template.ID;
  480. me.compiledTemplateNames.push(template.name);
  481. template.hasCompiled = false;
  482. template.errs = [];
  483. let private_extract_ID = function(str, idx){
  484. let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  485. for (let i = idx; i < str.length; i++) {
  486. if (str[i] === '(') {
  487. lBracket++;
  488. if (lBracket == 1) firstIdx = i + 1;
  489. }
  490. if (str[i] === ')') {
  491. rBracket++;
  492. if (lBracket == rBracket) {
  493. lastIdx = i - 1;
  494. if (lastIdx > firstIdx) {
  495. if (str[firstIdx] === "'") firstIdx++;
  496. if (str[lastIdx] !== "'") lastIdx++;
  497. if (lastIdx > firstIdx) {
  498. rst = str.slice(firstIdx, lastIdx);
  499. }
  500. }
  501. break;
  502. }
  503. }
  504. }
  505. return rst;
  506. };
  507. let private_parse_ref = function(item, itemIdx){
  508. let idx = item.expression.indexOf('@(', 0);
  509. while (idx >= 0) {
  510. let ID = private_extract_ID(item.expression, idx);
  511. if (ID.length > 0) {
  512. let subItem = template.compiledCalcItems[ID];
  513. if (subItem) {
  514. if (subItem.ID !== item.ID) {
  515. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  516. } else {
  517. template.errs.push("There exists the self refer ID: " + ID);
  518. }
  519. } else {
  520. template.errs.push("There exists the invalid ID by which could not find the item: " + ID);
  521. console.log('invalid ID: ' + ID);
  522. }
  523. }
  524. idx = item.expression.indexOf('@(', idx + ID.length + 3);
  525. }
  526. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  527. template.compiledSeq.push(itemIdx);
  528. }
  529. };
  530. let private_compile_items = function() {
  531. for (let idx of template.compiledSeq) {
  532. let item = template.calcItems[idx];
  533. item.dispExprUser = item.dispExpr; // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  534. if (item.expression == 'HJ')
  535. item.compiledExpr = '$CE.HJ()'
  536. else{
  537. item.compiledExpr = item.expression.split('@(').join('$CE.at(');
  538. item.compiledExpr = item.compiledExpr.split('base(').join('$CE.base(');
  539. };
  540. if (item.labourCoeID){
  541. let lc = me.compiledLabourCoes[item.labourCoeID].coe;
  542. item.dispExprUser = item.dispExpr.replace(/L/gi, lc.toString());
  543. item.compiledExpr = item.compiledExpr.replace(/L/gi, lc.toString());
  544. };
  545. if (item.feeRateID) {
  546. let orgFeeRate = item.feeRate;
  547. let cmf = me.compiledFeeRates[item.feeRateID];
  548. item.feeRate = cmf?cmf.rate:100;
  549. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  550. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  551. }
  552. };
  553. // 字段名映射
  554. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  555. }
  556. };
  557. if (template && template.calcItems && template.calcItems.length > 0) {
  558. template.compiledSeq = [];
  559. template.compiledCalcItems = {};
  560. for (let i = 0; i < template.calcItems.length; i++) {
  561. let item = template.calcItems[i];
  562. template.compiledCalcItems[item.ID] = item;
  563. template.compiledCalcItems[item.ID + "_idx"] = i;
  564. }
  565. for (let i = 0; i < template.calcItems.length; i++) {
  566. let item = template.calcItems[i];
  567. if (template.compiledSeq.indexOf(i) < 0) {
  568. private_parse_ref(item, i);
  569. }
  570. }
  571. if (template.errs.length == 0) {
  572. private_compile_items();
  573. template.hasCompiled = true;
  574. } else {
  575. console.log('errors: ' + template.errs.toString());
  576. }
  577. };
  578. };
  579. initFeeField(treeNode, fieldName){
  580. if (!treeNode.data.fees) {
  581. treeNode.data.fees = [];
  582. treeNode.data.feesIndex = {};
  583. };
  584. if (!treeNode.data.feesIndex[fieldName]) {
  585. let fee = {
  586. 'fieldName': fieldName,
  587. 'unitFee': 0,
  588. 'totalFee': 0,
  589. 'tenderUnitFee': 0,
  590. 'tenderTotalFee': 0
  591. };
  592. treeNode.data.fees.push(fee);
  593. treeNode.data.feesIndex[fieldName] = fee;
  594. };
  595. };
  596. // 仅内部调用。注意:外部不能直接使用,因为这里传入的树节点必须有一定的初始化。
  597. InnerCalc(treeNode){
  598. let me = this;
  599. let project = me.project;
  600. function initFees(treeNode){
  601. if (!treeNode.data.fees) {
  602. treeNode.data.fees = [];
  603. treeNode.data.feesIndex = {};
  604. treeNode.changed = true;
  605. };
  606. };
  607. function checkFee(treeNode, feeObj){
  608. if (feeObj.fieldName == '') return;
  609. if (!treeNode.data.feesIndex[feeObj.fieldName]){
  610. let fee = {
  611. 'fieldName': feeObj.fieldName,
  612. 'unitFee': feeObj.unitFee,
  613. 'totalFee': feeObj.totalFee,
  614. 'tenderUnitFee': 0,
  615. 'tenderTotalFee': 0
  616. };
  617. treeNode.data.fees.push(fee);
  618. treeNode.data.feesIndex[feeObj.fieldName] = fee;
  619. treeNode.changed = true;
  620. }
  621. else{
  622. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  623. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  624. treeNode.changed = true;
  625. };
  626. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  627. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  628. treeNode.changed = true;
  629. };
  630. };
  631. };
  632. function isBaseFeeType(type){
  633. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  634. };
  635. // 汇总定额或子清单的费用类别
  636. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees || treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  637. treeNode.data.programID = null;
  638. initFees(treeNode);
  639. let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees) ? project.Ration.getRationsByNode(treeNode) : treeNode.children;
  640. let rst = [];
  641. for (let ft of feeType) {
  642. let ftObj = {};
  643. ftObj.fieldName = ft.type;
  644. ftObj.name = ft.name;
  645. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  646. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  647. for (let item of objsArr) {
  648. let data = item.data;
  649. if (data.feesIndex && data.feesIndex[ft.type]) {
  650. buf = (buf + parseFloatPlus(data.feesIndex[ft.type].unitFee)).toDecimal(decimalObj.process);
  651. btf = (btf + parseFloatPlus(data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  652. btuf = (btuf + parseFloatPlus(data.feesIndex[ft.type].tenderUnitFee)).toDecimal(decimalObj.process);
  653. bttf = (bttf + parseFloatPlus(data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  654. };
  655. };
  656. }
  657. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  658. let sum_rtf = 0, sum_rttf = 0;
  659. let bq = parseFloat(treeNode.data.quantity ? treeNode.data.quantity : 1);
  660. for (let data of objsArr) {
  661. let rq = parseFloat(data.quantity ? data.quantity : 0);
  662. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  663. if (data.feesIndex && data.feesIndex[ft.type]) {
  664. ruf = parseFloat(data.feesIndex[ft.type].unitFee);
  665. rtuf = parseFloat(data.feesIndex[ft.type].tenderUnitFee);
  666. rtf = parseFloat(data.feesIndex[ft.type].totalFee);
  667. rttf = parseFloat(data.feesIndex[ft.type].tenderTotalFee);
  668. };
  669. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  670. buf = (buf + (ruf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  671. btuf = (btuf + (rtuf * rq / bq).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  672. };
  673. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  674. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  675. };
  676. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse || me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) {
  677. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  678. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  679. };
  680. if (isBaseFeeType(ft.type) || (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  681. btf = sum_rtf;
  682. bttf = sum_rttf;
  683. }
  684. else{
  685. btf = (buf * bq).toDecimal(decimalObj.process);
  686. bttf = (btuf * bq).toDecimal(decimalObj.process);
  687. };
  688. };
  689. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  690. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  691. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  692. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  693. checkFee(treeNode, ftObj);
  694. rst.push(ftObj);
  695. };
  696. treeNode.data.calcTemplate = {"calcItems": rst};
  697. }
  698. // 叶子清单的手工综合单价计算
  699. else if (treeNode.calcType == treeNodeCalcType.ctCommonUnitFee){
  700. delete treeNode.data.gljList;
  701. if (treeNode.data.calcBase) treeNode.data.calcBase = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  702. if (treeNode.data.calcBaseValue) treeNode.data.calcBaseValue = null; // 不能直接删除该属性,否则无法冲掉库中已存储的值
  703. if (treeNode.data.programID) treeNode.data.programID = null;
  704. let uf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.unitFee) ? treeNode.data.feesIndex.common.unitFee : 0;
  705. let tuf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee) ? treeNode.data.feesIndex.common.tenderUnitFee : 0;
  706. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  707. let tf = (uf * q).toDecimal(decimalObj.bills.totalPrice);
  708. let ttf = (tuf * q).toDecimal(decimalObj.bills.totalPrice);
  709. delete treeNode.data.fees; // 直接删掉再新增,不用一个个费判断更新,效率更高。
  710. delete treeNode.data.feesIndex;
  711. me.initFeeField(treeNode, 'common');
  712. treeNode.data.feesIndex.common.unitFee = uf.toDecimal(decimalObj.bills.unitPrice);
  713. treeNode.data.feesIndex.common.totalFee = tf.toDecimal(decimalObj.bills.totalPrice);
  714. treeNode.data.feesIndex.common.tenderUnitFee = tuf.toDecimal(decimalObj.bills.unitPrice);
  715. treeNode.data.feesIndex.common.tenderTotalFee = ttf.toDecimal(decimalObj.bills.totalPrice);
  716. treeNode.changed = true;
  717. treeNode.data.calcTemplate = {"calcItems": []};
  718. }
  719. // 叶子清单的计算基数计算
  720. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  721. delete treeNode.data.gljList;
  722. if (treeNode.data.programID) treeNode.data.programID = null;
  723. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  724. // let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  725. if (!treeNode.data.quantity) treeNode.data.quantity = 1;
  726. let q = treeNode.data.quantity;
  727. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  728. let uf = (b * f * q / 100).toDecimal(decimalObj.bills.unitPrice);
  729. let tuf = uf;
  730. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100).toDecimal(decimalObj.bills.totalPrice) : (uf * q).toDecimal(decimalObj.bills.totalPrice);
  731. let ttf = tf;
  732. delete treeNode.data.fees; // 直接删掉再新增,不用一个个费判断更新,效率更高。
  733. delete treeNode.data.feesIndex;
  734. me.initFeeField(treeNode, 'common');
  735. treeNode.data.feesIndex.common.unitFee = uf;
  736. treeNode.data.feesIndex.common.totalFee = tf;
  737. treeNode.data.feesIndex.common.tenderUnitFee = tuf;
  738. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  739. treeNode.changed = true;
  740. treeNode.data.calcTemplate = {"calcItems": []};
  741. }
  742. // 定额或清单自己的计算程序计算
  743. else{
  744. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  745. if (treeNode.data.type == rationType.volumePrice){
  746. delete treeNode.data.gljList;
  747. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  748. let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
  749. treeNode.data.marketTotalFee = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  750. }
  751. else if (treeNode.data.type == rationType.gljRation){
  752. }
  753. else{
  754. treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
  755. };
  756. if (treeNode.data.programID == undefined){
  757. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  758. };
  759. }
  760. else if (treeNode.calcType == treeNodeCalcType.ctBillCalcProgram) {
  761. let rations = project.Ration.getBillsSortRation(treeNode.source.getID());
  762. treeNode.data.gljList = project.ration_glj.getGatherGljArrByRations(rations);
  763. // if (treeNode.data.programID == undefined || treeNode.data.programID == defaultBillTemplate.ID){
  764. if (treeNode.data.programID == undefined){
  765. treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  766. }
  767. };
  768. let template = me.compiledTemplates[treeNode.data.programID];
  769. treeNode.data.calcTemplate = template;
  770. if (treeNode && template.hasCompiled) {
  771. let $CE = executeObj;
  772. $CE.treeNode = treeNode;
  773. $CE.template = template;
  774. $CE.calcBase = me.compiledCalcBases;
  775. initFees(treeNode);
  776. for (let idx of template.compiledSeq) {
  777. let calcItem = template.calcItems[idx];
  778. let feeRate = calcItem.feeRate;
  779. if (!feeRate) feeRate = 100; // 100%
  780. feeRate = feeRate.toDecimal(decimalObj.feeRate);
  781. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  782. let quantity = treeNode.data.quantity;
  783. if (!quantity) quantity = 0
  784. else quantity = parseFloat(quantity).toDecimal(decimalObj.decimal('quantity', treeNode));
  785. calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  786. checkFee(treeNode, calcItem);
  787. };
  788. }
  789. };
  790. };
  791. // 计算本节点(默认同时递归计算所有父节点,可选)
  792. calculate(treeNode, calcParents = true){
  793. let me = this;
  794. if (treeNode.sourceType === me.project.ration_glj.getSourceType()) return; // 仅用作树节点显示的工料机不能参与计算。
  795. let isRation = treeNode.sourceType === me.project.Ration.getSourceType();
  796. let isBill = treeNode.sourceType === me.project.Bills.getSourceType();
  797. if (isRation){
  798. treeNode.calcType = treeNodeCalcType.ctRationCalcProgram;
  799. }
  800. else if (treeNodeTools.isNullBill(treeNode)){
  801. treeNode.calcType = treeNodeCalcType.ctCommonUnitFee;
  802. }
  803. else if (treeNodeTools.isLeafBill(treeNode)) {
  804. if (treeNode.children && treeNode.children.length > 0){
  805. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  806. if (me.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  807. treeNode.calcType = treeNodeCalcType.ctBillCalcProgram;
  808. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  809. treeNode.calcType = treeNodeCalcType.ctGatherRationsFees;
  810. }
  811. else{ // 公式计算
  812. treeNode.calcType = treeNodeCalcType.ctCalcBaseValue;
  813. };
  814. }
  815. else if (isBill) // 父清单:汇总子清单的费用类别
  816. treeNode.calcType = treeNodeCalcType.ctGatherBillsFees;
  817. me.InnerCalc(treeNode);
  818. // 计算所有父结点
  819. if (treeNode.changed && calcParents && treeNode.parent) {
  820. me.calculate(treeNode.parent);
  821. };
  822. };
  823. // 存储、刷新本节点(默认存储刷新所有父节点,可选)
  824. saveNode(treeNode, saveParents = true) {
  825. if (!treeNode.changed) return;
  826. let me = this;
  827. let nodesArr = [];
  828. let curNode = treeNode;
  829. while (curNode) {
  830. if (curNode.changed){nodesArr.push(curNode)};
  831. if (saveParents) curNode = curNode.parent
  832. else break;
  833. };
  834. me.saveNodes(nodesArr);
  835. };
  836. // 存储、刷新零散的多个树结点
  837. saveNodes(treeNodes){
  838. if (treeNodes.length < 1) return;
  839. let me = this;
  840. me.project.beginUpdate('');
  841. for (let node of treeNodes){
  842. if (node.changed){
  843. let data = {
  844. ID: node.data.ID,
  845. projectID: me.project.ID(),
  846. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  847. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  848. subType: node.data.subType,
  849. quantity: node.data.quantity,
  850. calcBase: node.data.calcBase,
  851. calcBaseValue: node.data.calcBaseValue,
  852. programID: node.data.programID,
  853. marketUnitFee: node.data.marketUnitFee,
  854. marketTotalFee: node.data.marketTotalFee,
  855. fees: node.data.fees,
  856. isFromDetail:node.data.isFromDetail,
  857. feeRate: node.data.feeRate,
  858. feeRateID: node.data.feeRateID,
  859. contain:node.data.contain,
  860. quantityEXP:node.data.quantityEXP
  861. };
  862. if(node.sourceType==ModuleNames.ration && node.data.type==rationType.gljRation){//定额类型的工料机做特殊处理
  863. data.code=node.data.code;
  864. data.projectGLJID = node.data.projectGLJID;
  865. delete data.marketUnitFee;
  866. }
  867. let newData = {'updateType': 'ut_update', 'updateData': data};
  868. me.project.push(node.sourceType, [newData]);
  869. }
  870. };
  871. me.project.endUpdate();
  872. for (let node of treeNodes){delete node.changed};
  873. projectObj.mainController.refreshTreeNode(treeNodes);
  874. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  875. calcProgramObj.showData(me.project.mainTree.selected, false);
  876. };
  877. };
  878. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。
  879. 参数取值如下:
  880. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  881. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  882. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到) */
  883. calcAllNodes(calcType = calcAllType.catAll){
  884. let me = this;
  885. let changedNodes = [];
  886. function calcNodes(nodes) {
  887. for (let node of nodes) {
  888. if (node.children.length > 0) {
  889. calcNodes(node.children);
  890. };
  891. if ((calcType == calcAllType.catAll) || (calcType == node.sourceType)) {
  892. me.calculate(node, false);
  893. if (node.changed) changedNodes.push(node);
  894. };
  895. }
  896. };
  897. calcNodes(me.project.mainTree.roots);
  898. // me.saveNodes(changedNodes); 保存要与计算分离,否则实际应用场景中,会产生多次通讯。
  899. return changedNodes;
  900. };
  901. // 计算叶子清单下的所有子结点(如定额、量价、工料机定额等), 并计算自身和所有父结点。最后打包存储。
  902. calcLeafAndSave(treeNode){
  903. let me = this;
  904. if(!treeNodeTools.isLeafBill(treeNode)) return;
  905. if (treeNode.children && treeNode.children.length > 0) {
  906. let changedNodes = [];
  907. for (let child of treeNode.children){
  908. me.calculate(child, false);
  909. if (child.changed) changedNodes.push(child);
  910. };
  911. me.calculate(treeNode);
  912. let cur = treeNode;
  913. while (cur) {
  914. if (cur.changed) changedNodes.push(cur);
  915. cur = cur.parent;
  916. };
  917. me.saveNodes(changedNodes);
  918. };
  919. };
  920. // 计算多条零散的定额,并计算他们所属的清单、父级清单,然后打包存储。如:批量替换工料机后受影响的定额。
  921. calcRationsAndSave(rationNodes){
  922. let me = this, leafBills = [], changedNodes = [];
  923. for (let node of rationNodes) {
  924. me.calculate(node, false);
  925. if (node.changed) changedNodes.push(node);
  926. let leafBill = node.parent;
  927. if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill); // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  928. };
  929. for (let node of leafBills){
  930. me.calculate(node);
  931. let cur = node;
  932. while (cur) {
  933. if (cur.changed && changedNodes.indexOf(cur) < 0) changedNodes.push(cur);
  934. cur = cur.parent;
  935. };
  936. };
  937. me.saveNodes(changedNodes);
  938. };
  939. }