calc_program.js 42 KB

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