فهرست منبع

Merge branch 'master' into olym

olym 7 سال پیش
والد
کامیت
ccfc78d696
1فایلهای تغییر یافته به همراه15 افزوده شده و 5 حذف شده
  1. 15 5
      public/calc_util.js

+ 15 - 5
public/calc_util.js

@@ -4,6 +4,12 @@
  * added by CSL, 2017-09-01 增加公式解析对象analyzer,用于解析用户修改公式、自定义表达式。
  */
 
+// 需求说小数位数固定为2位,这里预留缓冲接口,防止以后需求变卦。
+const Digit_Calc_Program = -2;      // 这里指定计算程序用到的小数位数。
+function round(num) {
+    return scMathUtil.roundTo(num, Digit_Calc_Program);
+};
+
 let executeObj = {
     treeNode: null,
     template: null,
@@ -22,7 +28,7 @@ let executeObj = {
             base = me.calcBase[calcBaseName];
 
         if (base != null) {
-            let price = 0, aprice = 0, mprice = 0, tmpSum = 0;
+            let price = 0, aprice = 0, mprice = 0, tmpSum = 0, mdSum = 0;
 
             function isSubset(sub, arr){
                 // if(!(sub instanceof Array) || !(arr instanceof Array)) return false;
@@ -39,13 +45,15 @@ let executeObj = {
                        if (glj.type == gljType.GENERAL_MACHINE) {
                             // 获取机械组成物
                            let mds = projectObj.project.composition.getCompositionByCode(glj.code);
+                           if (!mds) mds = [];
                            for (let md of mds){
                                if (base.gljTypes.indexOf(md.glj_type) >= 0) {
                                    price = md["base_price"];
                                    if (!price) price = 0;
-                                   tmpSum = tmpSum + glj["quantity"] * md["consumption"] * price;
+                                   mdSum = mdSum +  round(md["consumption"] * price);
                                }
                            };
+                           tmpSum = tmpSum + glj["quantity"] * mdSum;     // 这里不能四舍五入
                        }
                 };
             }else{
@@ -66,8 +74,10 @@ let executeObj = {
                     };
                 };
             };
-            rst = tmpSum;
+
+            rst = round(tmpSum);
         };
+
         return rst;
     }
 };
@@ -385,11 +395,11 @@ class Calculation {
 
                 let feeRate = calcItem.feeRate;
                 if (!feeRate) feeRate = 100;    // 100%
-                calcItem.unitFee = eval(calcItem.compiledExpr) * feeRate * 0.01;   // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
+                calcItem.unitFee = round(eval(calcItem.compiledExpr) * feeRate * 0.01);   // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
 
                 let quantity = $treeNode.data.quantity;
                 if (!quantity) quantity = 0;
-                calcItem.totalFee = calcItem.unitFee * quantity;
+                calcItem.totalFee = round(calcItem.unitFee * quantity);
 
                 // 费用同步到定额
                 // 引入小麦的字段检测后,快速切换定额出现计算卡顿现象,过多的循环造成。这里把她的代码拆出来,减少微循环。