Browse Source

Merge branch 'master' of http://smartcost.f3322.net:3000/SmartCost/ConstructionCost

zhongzewei 7 years ago
parent
commit
5559ebf008

+ 21 - 4
modules/main/controllers/ration_controller.js

@@ -44,8 +44,27 @@ module.exports = {
         });
     },
     insertGLJAsRation:insertGLJAsRation,
-    replaceRations:replaceRations
+    replaceRations:replaceRations,
+    addNewRation:addNewRation
 };
+
+async function addNewRation(req,res) {
+    let result={
+        error:0
+    }
+    try {
+        let data = req.body.data;
+        data = JSON.parse(data);
+        console.log(data);
+        result.data = await ration_facade.addNewRation(data);
+    }catch (err){
+        logger.err(err);
+        result.error=1;
+        result.message = err.message;
+    }
+    res.json(result);
+}
+
 async function replaceRations(req,res) {
     let result={
         error:0
@@ -54,9 +73,7 @@ async function replaceRations(req,res) {
         let data = req.body.data;
         data = JSON.parse(data);
         let userID = req.session.sessionUser.ssoId;
-        console.log(data);
-        await ration_facade.replaceRations(userID,data);
-        result.data={};
+        result.data = await ration_facade.replaceRations(userID,data);
     }catch (err){
         logger.err(err);
         result.error=1;

+ 146 - 44
modules/main/facade/ration_facade.js

@@ -13,16 +13,82 @@ var bill_model = require('../models/bills');
 let decimal_facade = require('./decimal_facade');
 const uuidV1 = require('uuid/v1');
 let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
+let coeMolde = mongoose.model('std_ration_lib_coe_list');
+
 
 module.exports = {
-    replaceRations: replaceRations
+    replaceRations: replaceRations,
+    addNewRation:addNewRation
 };
+async function addNewRation(data) {
+    let query = data.itemQuery;
+    let stdRation = null;
+    if(query){
+        let searchDao = new SearchDao();
+        stdRation = await searchDao.getRationItem(query.userID,query.rationRepId,query.code);
+        data.newData.code = query.code;
+    }
+    if(data.brUpdate.length>0){
+        await updateSerialNo(data.brUpdate);
+    }
+    let newRation =await insertNewRation(data.newData,stdRation,data.calQuantity);
+    if(stdRation){
+        return await addRationSubList(stdRation,newRation);
+    }else {
+        return {ration:newRation};
+    }
+}
+
+async function  updateSerialNo(serialNoUpdate){
+    let tasks=[];
+    for(let data of serialNoUpdate){
+        let task={
+            updateOne:{
+                filter:{
+                    ID:data.ID,
+                    projectID:data.projectID
+                },
+                update :{
+                    serialNo:data.serialNo
+                }
+            }
+        };
+        tasks.push(task);
+    }
+    await ration_model.model.bulkWrite(tasks);
+
+}
 
-async function replaceRations(uerID,data) {
+async function insertNewRation(newData,std,calQuantity) {//插入新的定额
+    if(std){
+        newData.name = std.name;
+        newData.caption = std.caption;
+        newData.unit = std.unit;
+        newData.libID = std.rationRepId;
+        newData.content = std.jobContent;
+        if (std.chapter) {
+            newData.comments = std.chapter.explanation;
+            newData.ruleText = std.chapter.ruleText;
+        }
+        newData.from = std.type === 'complementary' ? 'cpt' : 'std';
+        newData.programID = std.feeType;
+        newData.rationAssList =  createRationAss(std);
+        // calculate ration Quantity
+    }
+    if(calQuantity){
+        await CalculateQuantity(newData,newData.billsItemID,newData.projectID);
+    }
+
+    let newRation = await ration_model.model.create(newData);
+     return newRation;
+
+}
+
+async function replaceRations(userID,data) {
     let searchDao = new SearchDao();
     let recodes = [];
     for(let recode of data.nodeInfo){
-        let stdRation = await searchDao.getRationItem(uerID,data.libID,recode.newCode);
+        let stdRation = await searchDao.getRationItem(userID,data.libID,recode.newCode);
         let newRecode = await replaceRation(recode,stdRation,data.projectID,data.calQuantity);
         if(newRecode){
             recodes.push(newRecode);
@@ -37,48 +103,69 @@ async function replaceRation(nodeInfo,stdRation,projectID,calQuantity) {
     if(stdRation){
         await deleRationSubRecode(projectID,nodeInfo.ID);
         let newRation = await updateRation(stdRation,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
-        let ration_gljs = await addRationGLJ(stdRation,newRation);
-        //console.log(newRation);
-        //ration_model.model.bulkWrite(rationTask);//删除定额
-        console.log(newRation);
-        return newRation;
+        return await addRationSubList(stdRation,newRation);
     }else {
         return null;
     }
 }
 
-async function addRationCoe(std,newRation) {
-    let ration_coe_list = [];
-    if(std.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
-
-    }
+async function addRationSubList(stdRation,newRation) {
+    let ration_gljs = await addRationGLJ(stdRation,newRation);
+    let ration_coes = await addRationCoe(stdRation,newRation);
+    //todo  添加增加安装费
 
+    return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes};
+}
 
-    var criteria= {};
-    criteria.ration_coe_list = [];
-    var dataLength = 0;
-    if(data.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
-        dataLength = data.rationCoeList.length
-    }
-    for(var i=0;i<=dataLength;i++){;
-        var newCoe = {
-            libID :data.rationRepId,
-            rationID:newRation.ID,
-            projectID :  newRation.projectID
-        }
-        if(i==dataLength){
-            newCoe.coeID=-1;
-            newCoe.name = '自定义系数';
-            newCoe.content='人工×1,材料×1,机械×1,主材×1,设备×1';
-            newCoe.isAdjust=0;
-            newCoe.coes = this.getCustomerCoeData();
-        }else {
-            newCoe.coeID= data.rationCoeList[i].ID;
+async function addRationCoe(std,newRation) {
+    let ration_coe_list = [];
+    let seq = 0;
+    if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
+        for(let sub of std.rationCoeList){
+            let libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
+            if(libCoe){
+                let newCoe = {};
+                newCoe.ID = uuidV1();
+                newCoe.coeID = sub.ID;
+                newCoe.seq = seq;
+                newCoe.name = libCoe.name;
+                newCoe.content = libCoe.content;
+                newCoe.isAdjust=0;
+                newCoe.coes = libCoe.coes;
+                newCoe.rationID = newRation.ID;
+                newCoe.projectID = newRation.projectID;
+                seq++;
+                ration_coe_list.push(newCoe);
+            }
         }
-        criteria.ration_coe_list.push(newCoe);
     }
+    let lastCoe ={
+        coeID:-1,
+        name : '自定义系数',
+        content:'人工×1,材料×1,机械×1,主材×1,设备×1',
+        isAdjust:0,
+        seq:seq,
+        rationID : newRation.ID,
+        projectID : newRation.projectID
+    };
+    lastCoe.ID = uuidV1();
+    lastCoe.coes = getCustomerCoeData();
+    ration_coe_list.push(lastCoe);
+    await ration_coe.insertMany(ration_coe_list);
+    return ration_coe_list;
+
 }
 
+function getCustomerCoeData() {
+    var coeList = [];
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'材料'});
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'机械'});
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'主材'});
+    coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'设备'});
+    return coeList;
+};
 
 async function addRationGLJ(std,newRation) {
     let newRationGLJList = [];
@@ -94,7 +181,7 @@ async function addRationGLJ(std,newRation) {
             newGLJ.rationItemQuantity = sub.consumeAmt;
             newGLJ.quantity = sub.consumeAmt;
             newGLJ.glj_repository_id = std.rationRepId;
-            let std_glj = await std_glj_lib_gljList_model.findOne({'ID': ration_glj.GLJID});
+            let std_glj = await std_glj_lib_gljList_model.findOne({'ID':sub.gljId});
             if(std_glj){
                 newGLJ.name = std_glj.name;
                 newGLJ.code = std_glj.code;
@@ -139,26 +226,41 @@ async function  updateRation(std,rationID,billsItemID,projectID,calQuantity) {
     ration.libID = std.rationRepId;
     ration.content = std.jobContent;
     ration.adjustState = '';
+    ration.isFromDetail=0;
+    ration.isSubcontract=false;
+    ration.fees=[];
     if (std.chapter) {
         ration.comments = std.chapter.explanation;
         ration.ruleText = std.chapter.ruleText;
     }
     ration.from = std.type === 'complementary' ? 'cpt' : 'std';
     ration.programID = std.feeType;
-    ration.rationAssList = [];//生成辅助定额
-    if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
-        for(var i=0;i<std.rationAssList.length;i++){
-            var ass = std.rationAssList[i];
-            ass.actualValue = ass.stdValue;
-            ration.rationAssList.push(ass);
-        }
-    }
+    ration.rationAssList = createRationAss(std);//生成辅助定额
     if(calQuantity){
        await CalculateQuantity(ration,billsItemID,projectID);
     }
-    let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},ration,{new: true});
+
+ let unsetObject = {
+     "marketUnitFee":1,
+     'marketTotalFee':1,
+     "maskName":1
+ }
+    let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
     return newRation;
 }
+
+function createRationAss(std) {
+    let  rationAssList = [];//生成辅助定额
+    if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
+        for(let i=0;i<std.rationAssList.length;i++){
+            let ass = std.rationAssList[i];
+            ass.actualValue = ass.stdValue;
+            rationAssList.push(ass);
+        }
+    }
+    return rationAssList;
+}
+
 async function CalculateQuantity (ration,billsItemID,projectID) {
     // calculate ration Quantity
     let decimalObject =await decimal_facade.getProjectDecimal(projectID);

+ 1 - 0
modules/main/routes/ration_route.js

@@ -11,6 +11,7 @@ module.exports = function (app) {
     rationRouter.post('/allocIDs', rationController.allocIDs);
     rationRouter.post('/insertGLJAsRation', rationController.insertGLJAsRation);
     rationRouter.post('/replaceRations', rationController.replaceRations);
+    rationRouter.post('/addNewRation', rationController.addNewRation);
 
     app.use('/ration', rationRouter);
 };

+ 1 - 6
web/building_saas/main/js/models/quantity_detail.js

@@ -454,12 +454,7 @@ var quantity_detail = {
         };
         quantity_detail.prototype.deleteByRation = function(ration){
             var detail_list = this.datas;
-            var newList =_.filter(detail_list,(d)=>{
-                return d.rationID!=ration.ID;
-            });
-            if(newList!=undefined){
-                this.datas = newList;
-            }
+            _.remove(detail_list,{'rationID':ration.ID});
         };
         quantity_detail.prototype.deleteByBills=function(deleteData){
             var detail_list = this.datas;

+ 101 - 7
web/building_saas/main/js/models/ration.js

@@ -158,7 +158,7 @@ var Ration = {
             let rationNodes = [];
             for (let ration of rations){
                   rationNodes.push(projectObj.project.mainTree.nodes['id_' + ration.ID]);
-            };
+            }
 
             return rationNodes;
         };
@@ -369,27 +369,121 @@ var Ration = {
         ration.prototype.updateRationCodes = function (recodes) {
             let libID = projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id;
             let projectID = projectInfoObj.projectInfo.ID;
+            let project = projectObj.project;
+            let mainTree = project.mainTree;
             let nodeInfo =[];
+            let refershNodes = [];
             for(let r of recodes){
                 nodeInfo.push({ID:r.node.data.ID,billsItemID:r.node.data.billsItemID,newCode:r.value});
+                refershNodes.push(r.node);
             }
             let calQuantity = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan');
+            $.bootstrapLoading.start();
             CommonAjax.post("/ration/replaceRations",{nodeInfo:nodeInfo,libID:libID,projectID:projectID,calQuantity:calQuantity},function (data) {
-                console.log(data);
-            })
+                for(let recode of data){
+                   let node =  mainTree.getNodeByID(recode.ration.ID);
+                   if(node) {
+                       for(let temkey in recode.ration){//更新缓存
+                           node.data[temkey] = recode.ration[temkey];
+                       }
+                       node.data.feesIndex = {};
+                       project.Ration.deleteSubListOfRation(recode.ration);//删除旧定额下的相关记录
+                       //添加新的记录
+                       project.ration_glj.addDatasToList(recode.ration_gljs);
+                       project.ration_coe.addDatasToList(recode.ration_coes);
+                       //to do 添加增加安装费
+                   }
+                }
+                project.projectGLJ.loadData(function () {
+                    gljOprObj.refreshView();
+                    project.calcProgram.calcRationsAndSave(refershNodes);
+                    projectObj.mainController.refreshTreeNode(refershNodes, true);
+                    $.bootstrapLoading.end();
+                });
 
+                if(data.length<recodes.length){//说明有部分定额编号没找到记录
+                    alert('当前库中找不到定额"' + recodes[data.length-1].value + '"');
+                }
+            })
         };
+        ration.prototype.addNewRation = function (itemQuery,rationType) {
+            let me = this;
+            let project = projectObj.project, sheetController = projectObj.mainController;
+            let selected = project.mainTree.selected, newSource = null, newNode = null,pre=null,br=null;
+            let billItemID = null,serialNo=1,nextID=null;
+            if (selected === null) { return; }
+            if (selected.sourceType === project.Bills.getSourceType() && selected.depth() > 0) {
+                if (selected.source.children.length > 0) {
+                    alert('当前清单已有清单子项,不能套用定额。');
+                } else if (selected.data.calcBase&&selected.data.calcBase!="") {
+                    alert('当前有基数计算不能插入定额/量价/工料机。');
+                } else {
+                    if(selected.data.type === billType.FB){
+                        return;
+                    }
+                    billItemID = selected.source.getID();
+                    nextID = selected.tree.rootID();
+                    br = this.getBillsSortRation(billItemID);
+                    serialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1
+                }
+            } else if (selected.sourceType === project.Ration.getSourceType()) {
+                billItemID = selected.getParentID();
+                br = this.getBillsSortRation(billItemID);
+                serialNo = selected.data.serialNo+1;
+                nextID = selected.getNextSiblingID();
+                pre = selected.source;
+            };
+            if(billItemID){
+                let newData =  me.getTempRationData(me.getNewRationID(), billItemID, serialNo, rationType);
+                let calQuantity = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan');
+                let brUpdate = [];
+                //更新兄弟节点的序列号
+                if (pre) {
+                    let preIndex = br.indexOf(pre), i;
+                    for (i = preIndex + 1; i < br.length; i++) {
+                        br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
+                        brUpdate.push({projectID:newData.projectID,ID:br[i].ID,serialNo:br[i].serialNo});
+                    }
+                }
+                $.bootstrapLoading.start();
+                CommonAjax.post("/ration/addNewRation",{itemQuery:itemQuery,newData:newData,calQuantity:calQuantity,brUpdate:brUpdate},function (data) {
+                    //更新缓存
+
+                    me.datas.push(data.ration);
+                    project.ration_glj.addDatasToList(data.ration_gljs);
+                    project.ration_coe.addDatasToList(data.ration_coes);
+                    //to do 添加增加安装费
+
+                    //插入树节点
+                    newSource = data.ration;
+                    newNode = project.mainTree.insert(billItemID, nextID, newSource.ID);
+                    newNode.source = newSource;
+                    newNode.sourceType = project.Ration.getSourceType();
+                    newNode.data = newSource;
+                    ProjectController.syncDisplayNewNode(sheetController, newNode);
+                    project.projectGLJ.loadData(function () {
+                        project.calcProgram.calcAndSave(newNode);
+                        projectObj.mainController.refreshTreeNode([newNode], false);
+                        $.bootstrapLoading.end();
+                    });
+                    console.log(data);
+                })
+            }
 
+        };
+        ration.prototype.deleteSubListOfRation = function(ration){
+            projectObj.project.ration_glj.deleteByRation(ration);
+            projectObj.project.ration_coe.deleteByRation(ration);
+            projectObj.project.quantity_detail.deleteByRation(ration);
+            //to do 删除安装增加费
+        };
         ration.prototype.replaceRation = function (ration, std) {
             this.project.beginUpdate('replaceRation');
             
             // delete
             let ration_glj =projectObj.project.ration_glj;
             this.project.push(this.project.ration_glj.getSourceType(), this.project.ration_glj.getDeleteDataByRation(ration));
-
-            this.project.ration_glj.deleteByRation(ration);
-            this.project.ration_coe.deleteByRation(ration);
-            this.project.quantity_detail.deleteByRation(ration);
+            this.deleteSubListOfRation(ration);
 
             // insertNewRation
             let updateData = [];

+ 13 - 11
web/building_saas/main/js/models/ration_coe.js

@@ -37,15 +37,22 @@ var ration_coe = {
             }
         };
         ration_coe.prototype.refreshAfterSave=function(data){
-            if(projectObj.project.ration_coe.datas&&Array.isArray(projectObj.project.ration_coe.datas)){
-                projectObj.project.ration_coe.datas = projectObj.project.ration_coe.datas.concat(data);
-            }else {
-                projectObj.project.ration_coe.datas = data;
-            }
+            projectObj.project.ration_coe.addDatasToList(data);
             gljOprObj.showCoeData(gljOprObj.coeSheet,gljOprObj.coeSetting,data);
             gljOprObj.coeSheetData=data;
             // SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
         };
+
+        ration_coe.prototype.addDatasToList = function (datas) {
+            let  me = projectObj.project.ration_coe;
+            if(datas&&datas.length>0){
+                if (me.datas && Array.isArray(me.datas)) {
+                    me.datas = me.datas.concat(datas);
+                } else {
+                    me.datas = datas;
+                }
+            }
+        };
         ration_coe.prototype.refreshAfterUpdate=function(data){
             var coe_list = projectObj.project.ration_coe.datas;
             var coe_index= _.findIndex(coe_list,function(coe){
@@ -138,12 +145,7 @@ var ration_coe = {
         };
         ration_coe.prototype.deleteByRation = function(ration){
             var coe_list = projectObj.project.ration_coe.datas;
-            var newList =_.filter(coe_list,(coe)=>{
-                return coe.rationID!=ration.ID;
-            });
-            if(newList!=undefined){
-                projectObj.project.ration_coe.datas = newList;
-            }
+            _.remove(coe_list,{'rationID':ration.ID});
         };
         return new ration_coe(project);
     }

+ 12 - 13
web/building_saas/main/js/models/ration_glj.js

@@ -111,13 +111,7 @@ var ration_glj = {
                 // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
                 neRecodes = data.showDatas;
                 gljOprObj.sheetData = neRecodes;
-            }
-            if (me.datas && Array.isArray(me.datas)) {
-                if (data) {
-                    me.datas = me.datas.concat(neRecodes);
-                }
-            } else {
-                me.datas = neRecodes;
+                me.addDatasToList(neRecodes);
             }
             project.projectGLJ.loadData(function () {
                 gljOprObj.showRationGLJSheetData(true);
@@ -133,6 +127,16 @@ var ration_glj = {
                 }
             });
         };
+        ration_glj.prototype.addDatasToList = function (datas) {
+            let  me = projectObj.project.ration_glj;
+            if(datas&&datas.length>0){
+                if (me.datas && Array.isArray(me.datas)) {
+                    me.datas = me.datas.concat(datas);
+                } else {
+                    me.datas = datas;
+                }
+            }
+        };
         ration_glj.prototype.addToMainTree = function (datas) {
             datas = sortRationGLJ(datas);
             for (let data of datas) {
@@ -293,12 +297,7 @@ var ration_glj = {
         };
         ration_glj.prototype.deleteByRation = function (ration) {
             var glj_list = projectObj.project.ration_glj.datas;
-            var newList = _.filter(glj_list, (glj) => {
-                return glj.rationID != ration.ID;
-            });
-            if (newList != undefined) {
-                projectObj.project.ration_glj.datas = newList;
-            }
+            _.remove(glj_list,{'rationID':ration.ID});
         };
         ration_glj.prototype.deleteByBills = function (deleteData) {
             var rationList = projectObj.project.Ration.datas;

+ 5 - 3
web/building_saas/main/js/views/project_view.js

@@ -462,7 +462,7 @@ var projectObj = {
                 let node = project.mainTree.items[changedCell.row];
                 let colSetting = setting.cols[changedCell.col];
                 let value = projectObj.checkSpreadEditingText(changedCell.text, colSetting)
-                if(colSetting.data.field=='code'&& node.sourceType == project.Ration.getSourceType() ){//如果是更新定额的编码
+                if(colSetting.data.field=='code'&& node.sourceType == project.Ration.getSourceType()&&node.data.type==rationType.ration){//如果是更新定额的编码
                     updateRationCodes.push({'node':node,value:value});
                 }else {
                     projectObj.updateCellValue(node, value, colSetting,changedCell.text);
@@ -700,7 +700,8 @@ var projectObj = {
                         return project.Ration.addRationChecking(selected);  // Vincent, 2018-01-02
                     },
                     callback: function (key, opt) {
-                        ProjectController.addRation(project, controller, rationType.ration);
+                        project.Ration.addNewRation(null,rationType.ration);
+                       // ProjectController.addRation(project, controller, rationType.ration);
                     },
                     visible: function(key, opt){
                         var selected = project.mainTree.selected;
@@ -722,7 +723,8 @@ var projectObj = {
                         return project.Ration.addRationChecking(selected);  // Vincent, 2018-01-02
                     },
                     callback: function (key, opt) {
-                        ProjectController.addRation(project, controller, rationType.volumePrice);
+                        project.Ration.addNewRation(null,rationType.volumePrice);
+                       // ProjectController.addRation(project, controller, rationType.volumePrice);
                     },
                     visible: function(key, opt){
                         var selected = project.mainTree.selected;

+ 6 - 11
web/building_saas/main/js/views/std_ration_lib.js

@@ -99,13 +99,11 @@ var rationLibObj = {
     onRationSpreadCellDoubleClick: function (sender, args) {
         var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
         if (rationCode !== '') {
-            CommonAjax.post('/complementaryRation/api/getRationItem', {user_id: userID, rationRepId: select.val(), code: rationCode}, function (data) {
-                ProjectController.addRation(projectObj.project, projectObj.mainController, rationType.ration, data);
-            });
+            projectObj.project.Ration.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration);
         }
     },
     loadStdRationContextMenu: function () {
-        let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet();
+        let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(),  rationModel = projectObj.project.Ration;;
         $.contextMenu({
             selector: '#stdSectionRations',
             build: function ($trigger, e) {
@@ -120,9 +118,7 @@ var rationLibObj = {
                         let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
                         let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
                         if (rationCode !== '') {
-                            CommonAjax.post('/complementaryRation/api/getRationItem', {user_id: userID, rationRepId: select.val(), code: rationCode}, function (data) {
-                                ProjectController.addRation(projectObj.project, projectObj.mainController, rationType.ration, data);
-                            });
+                            rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration);
                         }
                     }
                 },
@@ -132,10 +128,9 @@ var rationLibObj = {
                     callback: function (key, opt) {
                         let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
                         let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
-                        if (rationCode !== '') {
-                            CommonAjax.post('/complementaryRation/api/getRationItem', {user_id: userID, rationRepId: select.val(), code: rationCode}, function (data) {
-                                ProjectController.replaceRation(projectObj.project, projectObj.mainController, data);
-                            });
+                        let mainTreeSelected = projectObj.project.mainTree.selected;
+                        if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
+                            rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
                         }
                     }
                 },