chenshilong 7 years ago
parent
commit
49e1e1813c
1 changed files with 139 additions and 135 deletions
  1. 139 135
      web/building_saas/main/js/models/calc_program.js

+ 139 - 135
web/building_saas/main/js/models/calc_program.js

@@ -433,7 +433,7 @@ let treeNodeTools = {
     isNullBill: function (treeNode) {
         return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
     },
-    isEngineeringCostBill: function (treeNode) {
+    isTotalCostBill: function (treeNode) {
         return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
             treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
     },
@@ -451,11 +451,15 @@ let treeNodeTools = {
         return treeNode.sourceType === ModuleNames.ration && treeNode.data.type === rationType.gljRation;
     },
 
-    initFeeField(treeNode, fieldName){
+    initFees(treeNode){
         if (!treeNode.data.fees) {
             treeNode.data.fees = [];
             treeNode.data.feesIndex = {};
+            treeNode.changed = true;
         };
+    },
+    initFeeField(treeNode, fieldName){
+        this.initFees(treeNode);
         if (!treeNode.data.feesIndex[fieldName]) {
             let fee = {
                 'fieldName': fieldName,
@@ -466,8 +470,29 @@ let treeNodeTools = {
             };
             treeNode.data.fees.push(fee);
             treeNode.data.feesIndex[fieldName] = fee;
+            treeNode.changed = true;
+        };
+    },
+    checkFeeField(treeNode, feeObj){
+        if (!feeObj) return;
+        if (feeObj.fieldName == '') return;
+
+        // 初始化前先拦截末定义的情况
+        if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
+            if (feeObj.unitFee == 0 && feeObj.totalFee == 0) return;
+        }
+
+        this.initFeeField(treeNode, feeObj.fieldName);
+
+        if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
+            treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
+            treeNode.changed = true;
+        };
+
+        if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
+            treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
+            treeNode.changed = true;
         };
-        treeNode.changed = true;
     },
     initSummaryFee(treeNode){
         if (!treeNode.data.summaryFees){
@@ -765,112 +790,111 @@ class CalcProgram {
         treeNode.calcType = treeNodeTools.getCalcType(treeNode);
         // if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue) return;
 
-        function initFees(treeNode){
-            if (!treeNode.data.fees) {
-                treeNode.data.fees = [];
-                treeNode.data.feesIndex = {};
-                treeNode.changed = true;
-            };
-        };
-
-        function checkFee(treeNode, feeObj){
-            if (!feeObj) return;
-            if (feeObj.fieldName == '') return;
-
-            if (!treeNode.data.feesIndex[feeObj.fieldName]){
-                let fee = {
-                    'fieldName': feeObj.fieldName,
-                    'unitFee': feeObj.unitFee,
-                    'totalFee': feeObj.totalFee,
-                    'tenderUnitFee': 0,
-                    'tenderTotalFee': 0
-                };
-                treeNode.data.fees.push(fee);
-                treeNode.data.feesIndex[feeObj.fieldName] = fee;
-                treeNode.changed = true;
-            }
-            else{
-                if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
-                    treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
-                    treeNode.changed = true;
-                };
-
-                if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
-                    treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
-                    treeNode.changed = true;
-                };
-            };
-        };
-
         function isBaseFeeType(type){
             return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
         };
 
+        // 计算暂估费用
         function estimateFee(treeNode){
-            if (!treeNode.data.gljList) return undefined;
-
-            let eTypes = [
-                gljType.GENERAL_MATERIAL, gljType.MAIN_MATERIAL, gljType.EQUIPMENT,
-                gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO,
-                gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR];
-            let eDetailTypes = [gljType.MAIN_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO];
-
-            let GLJObjs = [];
-            for (let glj of treeNode.data.gljList) {
-                if (eTypes.indexOf(glj.type) >= 0) {
-                    if (glj.isEstimate){
-                        GLJObjs.push({code: glj.code, quantity: glj.quantity, marketPrice: glj.marketPrice});
+            let sumU = 0, sumT = 0;
+
+            if (treeNodeTools.isTotalCostBill(treeNode)){
+                let nodes = projectObj.project.mainTree.roots;
+                for (let node of nodes){
+                    if (treeNodeTools.isTotalCostBill(node)) break;
+                    let eU = 0, eT = 0;
+                    if (node.data.feesIndex && node.data.feesIndex.estimate){
+                        eU = node.data.feesIndex.estimate.unitFee;
+                        eT = node.data.feesIndex.estimate.totalFee;
                     }
-                    else{   // 组成物
-                        if (eDetailTypes.indexOf(glj.type) >= 0){
-                            let mds = projectObj.project.composition.getCompositionByGLJ(glj);
-                            if (!mds) mds = [];
-                            for (let md of mds){
-                                if (md.isEstimate){
-                                    let isExist = false;
-                                    let mdQ = (parseFloatPlus(glj.quantity) * parseFloatPlus(md.consumption)).toDecimal(decimalObj.process);
-
-                                    for (let obj of GLJObjs){
-                                        if (md.code == obj.code){
-                                            isExist = true;
-                                            obj.quantity = (parseFloatPlus(obj.quantity) + mdQ).toDecimal(decimalObj.process);
-                                            break;
-                                        }
-                                    };
-                                    if (!isExist)
-                                        GLJObjs.push({code: md.code, quantity: mdQ, marketPrice: md.marketPrice});
+                    else {
+                        eU = 0, eT = 0;
+                    };
+                    sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
+                    sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
+                };
+                sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
+                sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
+            }
+            else{
+                if (!treeNode.data.gljList) return;
+
+                let eTypes = [
+                    gljType.GENERAL_MATERIAL, gljType.MAIN_MATERIAL, gljType.EQUIPMENT,
+                    gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO,
+                    gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR];
+                let eDetailTypes = [gljType.MAIN_MATERIAL, gljType.CONCRETE, gljType.MORTAR, gljType.MIX_RATIO];
+
+                let GLJObjs = [];
+                for (let glj of treeNode.data.gljList) {
+                    if (eTypes.indexOf(glj.type) >= 0) {
+                        if (glj.isEstimate){
+                            GLJObjs.push({code: glj.code, quantity: glj.quantity, marketPrice: glj.marketPrice});
+                        }
+                        else{   // 组成物
+                            if (eDetailTypes.indexOf(glj.type) >= 0){
+                                let mds = projectObj.project.composition.getCompositionByGLJ(glj);
+                                if (!mds) mds = [];
+                                for (let md of mds){
+                                    if (md.isEstimate){
+                                        let isExist = false;
+                                        let mdQ = (parseFloatPlus(glj.quantity) * parseFloatPlus(md.consumption)).toDecimal(decimalObj.process);
+
+                                        for (let obj of GLJObjs){
+                                            if (md.code == obj.code){
+                                                isExist = true;
+                                                obj.quantity = (parseFloatPlus(obj.quantity) + mdQ).toDecimal(decimalObj.process);
+                                                break;
+                                            }
+                                        };
+                                        if (!isExist)
+                                            GLJObjs.push({code: md.code, quantity: mdQ, marketPrice: md.marketPrice});
+                                    }
                                 }
                             }
                         }
-                    }
+                    };
                 };
-            };
 
-            let sumU = 0, sumT = 0, unitFee = 0, totalFee = 0;
-            for (let obj of GLJObjs){
-                sumU = sumU + (parseFloatPlus(obj.quantity) * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
-                sumU = sumU.toDecimal(decimalObj.process);
+                for (let obj of GLJObjs){
+                    sumU = sumU + (parseFloatPlus(obj.quantity) * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
+                    sumU = sumU.toDecimal(decimalObj.process);
 
-                let q = (parseFloatPlus(obj.quantity) * parseFloatPlus(treeNode.data.quantity)).toDecimal(decimalObj.process);
-                sumT = sumT + (q * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
-                sumT = sumT.toDecimal(decimalObj.process);
-            };
-            unitFee = sumU.toDecimal(decimalObj.bills.unitPrice);
-            if (projectObj.project.property.zanguCalcMode == zanguCalcType.common){
-                totalFee = (parseFloatPlus(treeNode.data.quantity) * unitFee).toDecimal(decimalObj.bills.totalPrice);
-            }
-            else if (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial){
-                totalFee = sumT.toDecimal(decimalObj.bills.totalPrice);
+                    let q = (parseFloatPlus(obj.quantity) * parseFloatPlus(treeNode.data.quantity)).toDecimal(decimalObj.process);
+                    sumT = sumT + (q * parseFloatPlus(obj.marketPrice)).toDecimal(decimalObj.process);
+                    sumT = sumT.toDecimal(decimalObj.process);
+                };
+                sumU = sumU.toDecimal(decimalObj.bills.unitPrice);
+                if (projectObj.project.property.zanguCalcMode == zanguCalcType.common){
+                    sumT = (parseFloatPlus(treeNode.data.quantity) * sumU).toDecimal(decimalObj.bills.totalPrice);
+                }
+                else if (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial){
+                    sumT = sumT.toDecimal(decimalObj.bills.totalPrice);
+                };
             };
 
-            return {'fieldName': 'estimate', 'unitFee': unitFee, 'totalFee': totalFee};
+            treeNodeTools.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
+        };
+
+        // 删掉多余的费用。例如:从其它计算方式切换到公式计算方式,会多出其它的费(不光是common、estimate)
+        function deleteUselessFees(treeNode){
+            if (treeNode.data.fees && treeNode.data.fees.length > 0){
+                let feesArr = treeNode.data.fees;
+                for (let i = 0; i < feesArr.length; i++) {
+                    if (feesArr[i].fieldName != 'common' && feesArr[i].fieldName != 'estimate') {
+                        delete treeNode.data.feesIndex[feesArr[i].fieldName];
+                        feesArr.splice(i, 1);
+                        treeNode.changed = true;
+                    }
+                }
+            };
         };
 
         // 父清单汇总子项(定额或子清单)的费用类别
         if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees ||
             treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
             treeNode.data.programID = null;
-            initFees(treeNode);
+            treeNodeTools.initFees(treeNode);
 
             let objsArr = (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees) ? me.project.Ration.getRationsByNode(treeNode) : treeNode.children;
             let rst = [];
@@ -933,7 +957,7 @@ class CalcProgram {
                 ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
                 ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
                 ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
-                checkFee(treeNode, ftObj);
+                treeNodeTools.checkFeeField(treeNode, ftObj);
 
                 rst.push(ftObj);
             };
@@ -947,19 +971,14 @@ class CalcProgram {
             if (treeNode.data.programID) treeNode.data.programID = null;
 
             let uf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.unitFee) ? treeNode.data.feesIndex.common.unitFee : 0;
+            uf = uf.toDecimal(decimalObj.bills.unitPrice);
             let tuf = (treeNode.data.feesIndex && treeNode.data.feesIndex.common && treeNode.data.feesIndex.common.tenderUnitFee) ? treeNode.data.feesIndex.common.tenderUnitFee : 0;
+            tuf = tuf.toDecimal(decimalObj.bills.unitPrice);
             let q = treeNode.data.quantity ? treeNode.data.quantity : 0;
             let tf = (uf * q).toDecimal(decimalObj.bills.totalPrice);
             let ttf = (tuf * q).toDecimal(decimalObj.bills.totalPrice);
-
-            delete treeNode.data.fees;    // 直接删掉再新增,不用一个个费判断更新,效率更高。
-            delete treeNode.data.feesIndex;
-            treeNodeTools.initFeeField(treeNode, 'common');
-            treeNode.data.feesIndex.common.unitFee = uf.toDecimal(decimalObj.bills.unitPrice);
-            treeNode.data.feesIndex.common.totalFee = tf.toDecimal(decimalObj.bills.totalPrice);
-            treeNode.data.feesIndex.common.tenderUnitFee = tuf.toDecimal(decimalObj.bills.unitPrice);
-            treeNode.data.feesIndex.common.tenderTotalFee = ttf.toDecimal(decimalObj.bills.totalPrice);
-            treeNode.changed = true;
+            deleteUselessFees(treeNode);
+            treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
             treeNode.data.calcTemplate = {"calcItems": []};
         }
         // 叶子清单公式计算
@@ -975,38 +994,21 @@ class CalcProgram {
             let tuf = uf;
             let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100).toDecimal(decimalObj.bills.totalPrice) : (uf * q).toDecimal(decimalObj.bills.totalPrice);
             let ttf = tf;
+            deleteUselessFees(treeNode);
+            treeNodeTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
+
+            // 如果是总造价清单,还要做单项工程、建设项目金额汇总
+            if (treeNodeTools.isTotalCostBill(treeNode)){
+                estimateFee(treeNode);
+                treeNodeTools.initSummaryFee(treeNode);
+                treeNode.data.summaryFees.totalFee = tf;
+                treeNode.data.summaryFees.estimateFee = treeNode.data.feesIndex.estimate.totalFee;
+                let bill_safe = cbTools.findBill(fixedFlag.SAFETY_CONSTRUCTION);
+                treeNode.data.summaryFees.safetyFee = (bill_safe && bill_safe.feesIndex && bill_safe.feesIndex.common)?bill_safe.feesIndex.common.totalFee:0;
+                let bill_charge = cbTools.findBill(fixedFlag.CHARGE);
+                treeNode.data.summaryFees.chargeFee = (bill_charge && bill_charge.feesIndex && bill_charge.feesIndex.common)?bill_charge.feesIndex.common.totalFee:0;
+            }
 
-            function newCommonFee(){
-                treeNodeTools.initFeeField(treeNode, 'common');
-                treeNode.data.feesIndex.common.unitFee = uf;
-                treeNode.data.feesIndex.common.totalFee = tf;
-                treeNode.data.feesIndex.common.tenderUnitFee = tuf;
-                treeNode.data.feesIndex.common.tenderTotalFee = ttf;
-                treeNode.changed = true;
-                // 如果是总造价清单,还要做单项工程、建设项目金额汇总
-                if (treeNodeTools.isEngineeringCostBill(treeNode)){
-                    treeNodeTools.initSummaryFee(treeNode);
-                    treeNode.data.summaryFees.totalFee = tf;
-                    treeNode.data.summaryFees.estimateFee = 0;//treeNode.data.feesIndex.zangu.totalFee;
-                    let bill_safe = cbTools.findBill(fixedFlag.SAFETY_CONSTRUCTION);
-                    treeNode.data.summaryFees.safetyFee = (bill_safe && bill_safe.feesIndex && bill_safe.feesIndex.common)?bill_safe.feesIndex.common.totalFee:0;
-                    let bill_charge = cbTools.findBill(fixedFlag.CHARGE);
-                    treeNode.data.summaryFees.chargeFee = (bill_charge && bill_charge.feesIndex && bill_charge.feesIndex.common)?bill_charge.feesIndex.common.totalFee:0;
-                }
-            };
-
-            if (treeNode.data.feesIndex && treeNode.data.feesIndex.common){
-                if (treeNode.data.feesIndex.common.unitFee != uf ||
-                    treeNode.data.feesIndex.common.totalFee != tf ||
-                    treeNode.data.feesIndex.common.tenderUnitFee != tuf ||
-                    treeNode.data.feesIndex.common.tenderTotalFee != ttf ){
-                    delete treeNode.data.fees;    // 直接删掉再新增。从其它计算方式切换到公式计算方式,会多出其它的费(不光是common)所以这里直接删掉,不用一个个费判断更新,效率更高。
-                    delete treeNode.data.feesIndex;
-                    newCommonFee();
-                }
-            }else{
-                newCommonFee();
-            };
             treeNode.data.calcTemplate = {"calcItems": []};
         }
         // 定额或叶子清单自己的计算程序计算
@@ -1023,6 +1025,8 @@ class CalcProgram {
                 }
                 else{
                     treeNode.data.gljList = me.project.ration_glj.getGljArrByRation(treeNode.data.ID);
+                    // 计算程序里没有暂估费的计算规则,会漏掉,所以这里要专门算。
+                    estimateFee(treeNode);
                 };
 
                 if (treeNode.data.programID == undefined){
@@ -1036,6 +1040,9 @@ class CalcProgram {
                 if (treeNode.data.programID == undefined){
                     treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
                 }
+
+                // 叶子清单自己的计算程序计算,其暂估费也要汇总算。
+                estimateFee(treeNode);
             };
 
             let template = me.compiledTemplates[treeNode.data.programID];
@@ -1047,7 +1054,7 @@ class CalcProgram {
                 $CE.template = template;
                 $CE.calcBase = me.compiledCalcBases;
 
-                initFees(treeNode);
+                treeNodeTools.initFees(treeNode);
 
                 for (let idx of template.compiledSeq) {
                     let calcItem = template.calcItems[idx];
@@ -1062,12 +1069,9 @@ class CalcProgram {
                     else quantity = parseFloat(quantity).toDecimal(decimalObj.decimal('quantity', treeNode));
                     calcItem.totalFee = (calcItem.unitFee * quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
 
-                    checkFee(treeNode, calcItem);
+                    treeNodeTools.checkFeeField(treeNode, calcItem);
                 };
             };
-
-            checkFee(treeNode, estimateFee(treeNode));
-
         };
 
         if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);