calc_program.js 43 KB

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