| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 | /** * Created by Mai on 2017/4/1. */var Ration = {    createNew: function (project) {        // 用户定义private方法        var tools = {            formatRationUpdateData: function (data) {                let uData = JSON.parse(JSON.stringify(data));                delete uData.feesIndex;                delete uData.flagsIndex;                // if (uData.fees) {                //     for (let fee of uData.fees) {                //         fee.unitFee = fee.unitFee.toFixed(2);                //         fee.totalFee = fee.totalFee.toFixed(2);                //         fee.tenderUnitFee = fee.tenderUnitFee.toFixed(2);                //         fee.tenderTotalFee = fee.tenderTotalFee.toFixed(2);                //     }                // }                return uData;            }        };        // 所有通过this访问的属性,都不应在此单元外部进行写入操作        var ration = function (proj) {            this.project = proj;            this.datas = null;            var sourceType = ModuleNames.ration;            this.getSourceType = function () {                return sourceType;            }            proj.registerModule(ModuleNames.ration, this);            var maxRationID = 0;            this.getNewRationID = function () {                return maxRationID += 1;            };            this.maxRationID = function (maxID) {                if (arguments.length === 0) {                    return maxRationID;                } else {                    maxRationID = Math.max(maxID, maxRationID);                }            };        };        // prototype用于定义public方法        ration.prototype.loadData = function (datas) {            var that = this;            this.datas = datas;            // generate Fees & Flags Index,For View & Calculate            this.datas.forEach(function (data) {                data.feesIndex = {};                data.fees.forEach(function (fee) {                    data.feesIndex[fee.fieldName] = fee;                });                data.flagsIndex = {};                data.flags.forEach(function (flag) {                    data.flagsIndex[flag.fieldName] = flag;                });                that.maxRationID(data.ID);            });        };        ration.prototype.setMaxID = function (ID) {            this.maxRationID(ID);        }        // refresh after update        ration.prototype.doAfterUpdate = function(err, data){            if(data.stateRefresh){                this.refreshAdjustState(data);            }            if(data.quantityRefresh){                this.refreshQuantity(data);            }        };        ration.prototype.refreshAdjustState = function(data){            this.refreshDatas(data,'adjustState');            if(data.hasOwnProperty('name')){              this.refreshDatas(data,'name')            }        };        ration.prototype.refreshQuantity = function(data){            this.refreshDatas(data,'quantity');        };        ration.prototype.refreshDatas = function(data,fieldName){            var dataIndex = _.findIndex(this.datas,function(item) {                return item.ID ==data.rationID;            });            this.datas[dataIndex][fieldName] = data[fieldName];            if(fieldName=='quantity'){                this.datas[dataIndex]['isFromDetail']=1            }            var controller = projectObj.mainController;            var selected = controller.sheet.getSelections();            var col =   _.findIndex(project.projSetting.main_tree_col.cols,function (col) {                return col.data.field ==fieldName;            });            controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);        };        ration.prototype.getTempRationData = function (id, billsID, serialNo) {            var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};            newData[project.masterField.ration] = billsID;            return newData;        };        ration.prototype.getBillsSortRation = function (billsID) {     // 该方法只适用于叶子清单            var rations = this.datas.filter(function (data) {                return data[project.masterField.ration] === billsID;            });            rations.sort(function (x, y) {                return x.serialNo - y.serialNo;            });            return rations;        };        // CSL, 2017-11-13 取任何清单(父清单、叶子清单)下的所有定额        ration.prototype.getRationsByNode = function (billNode) {            let rations = [];            let sBills = 'bills';            if (billNode.sourceType != sBills) return rations;            let IDs = [];            function getSubBillsIDs(node) {                if (!node) return;                if (node.sourceType != sBills) return;                if (!node.children || node.children.length == 0 || node.children[0].sourceType != sBills)                    IDs.push(node.data.ID)                else                    getSubBillsIDs(node.children[0]);                getSubBillsIDs(node.nextSibling);            };            if (billNode.source.children.length == 0)                IDs.push(billNode.data.ID)            else                getSubBillsIDs(billNode.children[0]);            for (let id of IDs){                let subRations = this.datas.filter(function (data) {                    return data[project.masterField.ration] === id;                });                rations.push(...subRations);            };            rations.sort(function (x, y) {                return x.serialNo - y.serialNo;            });            return rations;        };        ration.prototype.getInsertRationData = function (billsID, preRation) {            var br = this.getBillsSortRation(billsID);            var updateData = [];            if (preRation && br.indexOf(preRation) > -1) {  // CSL, 2017-11-28  如果preIndex是-1,表明preRation 是量价。                var preIndex = br.indexOf(preRation), i;                updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});                for (i = preIndex + 1; i < br.length; i++) {                    updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});                }            } else {                updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});            }            return updateData;        };        ration.prototype.getCounterData = function (count) {            var updateData = {'projectID': this.project.ID()};            if (count) {                updateData[this.getSourceType()] = this.maxRationID() + count;            } else {                updateData[this.getSourceType()] = this.maxRationID() + 1;            }            return updateData;        };        ration.prototype.insertRation = function (billsID, preRation) {            var br = this.getBillsSortRation(billsID);            this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],                [this.getInsertRationData(billsID, preRation), this.getCounterData()]);            var newRation = null;            if (preRation) {                var preIndex = br.indexOf(preRation), i;                newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);                this.datas.push(newRation);                for (i = preIndex + 1; i < br.length; i++) {                    br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;                }            } else {                newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);                this.datas.push(newRation);            }            return newRation;        };        ration.prototype.insertStdRation = function (billsID, preRation, std) {            var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null, that = this;            updateData.forEach(function (data) {                if (data.updateType === 'ut_create') {                    data.updateData.code = std.code;                    data.updateData.name = std.name;                    data.updateData.caption = std.caption;                    data.updateData.unit = std.unit;                    data.updateData.libID = std.rationRepId;                    data.updateData.content = std.jobContent;                    if (std.chapter) {                        data.updateData.comments = std.chapter.explanation;                        data.updateData.ruleText = std.chapter.ruleText;                    }                    data.updateData.programID = std.feeType;                    data.updateData.rationAssList =  projectObj.project.ration_ass.CreateNewAss(std);                    // calculate ration Quantity                    that.CalculateQuantity(data.updateData);                    newRation = data.updateData;                }            });            this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()], [updateData, this.getCounterData()]);            newRation.ID = this.getNewRationID();            if (preRation) {                var preIndex = br.indexOf(preRation), i;                this.datas.push(newRation);                for (i = preIndex + 1; i < br.length; i++) {                    br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;                }            } else {                this.datas.push(newRation);            }            return newRation;        };        ration.prototype.getDeleteData = function (rationData) {            var updateData = [];            updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});            return updateData;        };        ration.prototype.delete = function (ration) {            var ration_glj =projectObj.project.ration_glj;            this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);            project.ration_glj.deleteByRation(ration);            project.ration_coe.deleteByRation(ration);            project.quantity_detail.deleteByRation(ration);            this.datas.splice(this.datas.indexOf(ration), 1);        };        ration.prototype.getDeleteDataByBill = function (nodes) {            let updateData = [];            for (let node of nodes) {                if (node.children.length > 0) {                    updateData = updateData.concat(this.getDeleteDataByBills[node.children]);                } else {                    let rations = this.getBillsSortRation(node.getID());                    for (let r of rations) {                        updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': r.ID, 'projectID': this.project.ID()}});                    }                }            }            return updateData;        };        ration.prototype.deleteByBills = function (nodes) {            for (let node of nodes) {                if (node.children.length > 0) {                    this.deleteByBills([node.children]);                } else {                    let rations = this.getBillsSortRation(node.getID());                    for (let r of rations) {                        this.datas.splice(this.datas.indexOf(r), 1);                    }                }            }        }        ration.prototype.getChangePosUpdateData = function (ration1, ration2) {            var updateData = [];            updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});            updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});            return updateData;        };        ration.prototype.changePos = function (ration1, ration2) {            var updateData = this.getChangePosUpdateData(ration1, ration2);            this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);            var preSerialNo = ration1.serialNo;            ration1.serialNo = ration2.serialNo;            ration2.serialNo = preSerialNo;        };        ration.prototype.updateField = function (ration, field, newValue) {            calcFees.setFee(ration, field, newValue);            let updateData = [];            let data = {'ID': ration.ID, 'projectID': this.project.ID()};            if (field === 'quantity') {                data[field] = newValue;                data.isFromDetail=0;                // to do Calculate                if (ration.fees) {                    data.fees = ration.fees;                }            } else if (field === 'feesIndex.common.unitFee') {                // to do Calculate                if (ration.fees) {                    data.fees = ration.fees;                }            } else {                data[field] = newValue;            }            updateData.push({'updateType': 'ut_update', 'updateData': data});            this.project.pushNow('updateBills', this.getSourceType(), updateData);        };        ration.prototype.updateRation = function (ration, updateNow) {            let updateData = [];            updateData.push({'updateType': 'ut_update', 'updateData': tools.formatRationUpdateData(ration)});            if (updateNow) {                this.project.pushNow('updateRations', this.getSourceType(), updateData);            } else {                this.project.push(this.getSourceType(), updateData);            }        };        ration.prototype.FilterNumberFromUnit = function (unit) {            let reg = new RegExp('^[0-9]+');            if (reg.test(unit)) {                return parseInt(unit.match(reg)[0]);            } else {                return 1;            }        };        ration.prototype.CalculateQuantity = function (ration) {            // calculate ration Quantity            if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')) {                let billsNode = this.project.Bills.tree.findNode(ration[this.project.masterField.ration]);                let billsQuantity = billsNode.data.quantity ? billsNode.data.quantity : 0;                if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToRationUnit')) {                    ration.quantity = (billsQuantity / this.FilterNumberFromUnit(ration.unit)).toDecimal(4);                } else {                    ration.quantity = billsQuantity.toDecimal(4);                }            }        };        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);            // insertNewRation            let updateData = [];            ration.code = std.code;            ration.name = std.name;            ration.caption = std.caption;            ration.unit = std.unit;            ration.libID = std.rationRepId;            ration.content = std.jobContent;            if (std.chapter) {                ration.comments = std.chapter.explanation;                ration.ruleText = std.chapter.ruleText;            }            ration.programID = std.feeType;            ration.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);            // calculate ration Quantity            this.CalculateQuantity(ration);            updateData.push({updateType: 'ut_update', updateData: ration});            this.project.push(this.getSourceType(), updateData);            this.project.endUpdate();        };                return new ration(project);    }};
 |