123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var Bills = {
- createNew: function (project) {
- var billsTreeSetting = {
- id: 'ID',
- pid: 'ParentID',
- nid: 'NextSiblingID',
- rootId: -1,
- autoUpdate: true
- };
- // 用户定义private方法
- var tools = {
- coverseTreeUpdateData: function (datas, projectID) {
- var updateDatas = [];
- datas.forEach(function (data) {
- var updateData = {};
- data.data.projectID = projectID;
- if (data.type === idTree.updateType.new) {
- updateData.updateType = 'ut_create';
- updateData.updateData = data.data;
- } else if (data.type === idTree.updateType.update) {
- updateData.updateType = 'ut_update';
- updateData.updateData = data.data;
- } else if (data.type === idTree.updateType.delete) {
- updateData.updateType = 'ut_delete';
- updateData.updateData = data.data;
- }
- updateDatas.push(updateData);
- });
- return updateDatas;
- },
- formatBillsUpdateData: function (data) {
- let uData = JSON.parse(JSON.stringify(data));
- delete uData.feesIndex;
- delete uData.flagsIndex;
- if (uData.quantity) {
- uData.quantity = uData.quantity.toFixed(2);
- }
- 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 bills = function (proj) {
- this.project = proj;
- this.datas = null;
- this.tree = idTree.createNew(billsTreeSetting);
- var sourceType = ModuleNames.bills;
- this.getSourceType = function () {
- return sourceType;
- }
- proj.registerModule(ModuleNames.bills, this);
- };
- // 从后台获取数据
- /*bills.prototype.pullData = function (){
- this.project.pullData(
- '/bills/getData',
- {projectID: this.project.ID},
- function(result){
- if (result.error ===0){
- this.loadDatas(result.data);
- }
- else {
- // to do: ?错误处理需要细化
- alert(result.message);
- }
- },
- function (){}// to do: 错误处理需要细化
- )
- };*/
- // prototype用于定义public方法
- bills.prototype.loadData = function (datas) {
- this.datas = datas;
- // generate Fees & Flags Index, For View & Calculate
- this.datas.forEach(function (data) {
- if (data.quantity) {
- data.quantity = parseFloat(data.quantity);
- }
- data.feesIndex = {};
- if (data.fees) {
- data.fees.forEach(function (fee) {
- fee.unitFee = parseFloat(fee.unitFee);
- fee.totalFee = parseFloat(fee.totalFee);
- fee.tenderUnitFee = parseFloat(fee.tenderUnitFee);
- fee.tenderTotalFee = parseFloat(fee.tenderTotalFee);
- data.feesIndex[fee.fieldName] = fee;
- });
- }
- data.flagsIndex = {};
- if (data.flags) {
- data.flags.forEach(function (flag) {
- data.flagsIndex[flag.fieldName] = flag;
- });
- }
- });
- // datas load to Tree
- this.tree.loadDatas(this.datas);
- };
- bills.prototype.setMaxID = function (ID) {
- this.tree.maxNodeID(ID);
- };
- // 提交数据后的错误处理方法
- bills.prototype.doAfterUpdate = function(err, data){
- // console.log(data)
- if(data.quantityRefresh){
- this.refreshDatas(data,'quantity');
- }
- $.bootstrapLoading.end();
- };
- bills.prototype.refreshDatas = function(data,fieldName){
- var dataIndex = _.findIndex(this.datas,function(item) {
- return item.ID ==data.billID;
- });
- 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]);
- };
- bills.prototype.getCounterData = function (count) {
- var updateData = {'projectID': this.project.ID()};
- if (count) {
- updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
- } else {
- updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
- }
- return updateData;
- };
- bills.prototype.insertSpecialBill=function(parentId, nextSiblingId,isUserAdd,type){
- var insertData = this.tree.getInsertData(parentId, nextSiblingId);
- var that = this, newData = null;
- insertData.forEach(function (data) {
- if (data.type === idTree.updateType.new) {
- if(isUserAdd==true){//如果是用户新增的
- data.data.isAdd = 1;
- }
- data.data.type = type;
- newData = data.data;
- }
- });
- this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
- [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
- //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
- this.datas.push(newData);
- return this.tree.insertByData(newData,parentId, nextSiblingId);
- };
- bills.prototype.insertBills = function (parentId, nextSiblingId) {
- var insertData = this.tree.getInsertData(parentId, nextSiblingId);
- var that = this, newData = null;
- insertData.forEach(function (data) {
- if (data.type === idTree.updateType.new) {
- data.data.type = billType.BILL;
- newData = data.data;
- }
- });
- this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
- [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
- //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
- this.datas.push(newData);
- return this.tree.insertByData(newData,parentId, nextSiblingId);
- };
- bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
- var insertData = this.tree.getInsertData(parentId, nextSiblingId);
- var newData = null, that = this;
- insertData.forEach(function (data) {
- if (data.type === idTree.updateType.new) {
- data.data.code = that.newFormatCode(stdBillsData.code);
- data.data.name = stdBillsData.name;
- data.data.unit = stdBillsData.unit;
- // 工程量计算规则
- data.data.ruleText = stdBillsData.ruleText;
- // 说明(补注)
- data.data.comments = stdBillsData.recharge;
- //zhong 特征及内容
- data.data.jobContent = stdBillsData.jobContent;
- data.data.itemCharacter = stdBillsData.itemCharacter;
- data.data.jobContentText = stdBillsData.jobContentText;
- data.data.itemCharacterText = stdBillsData.itemCharacterText;
- data.data.programID = stdBillsData.engineering;
- data.data.type = stdBillsData.type;//插入清单类型
- //zhong
- newData = data.data;
- }
- });
- this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
- [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
- this.datas.push(newData);
- return this.tree.insertByData(newData, parentId, nextSiblingId);
- };
- bills.prototype.deleteBills = function (node) {
- let deleteNode = function (node) {
- this.project.Ration.deleteByBills([node]);
- // this.project.VolumePrice.deleteByBills([node]);
- return this.tree.delete(node);
- }
- var deleteData = this.tree.getDeleteData(node);
- var ration_glj =projectObj.project.ration_glj;
- // let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj, ModuleNames.volume_price];
- let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj];
- let deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()),
- this.project.Ration.getDeleteDataByBill([node]), ration_glj.getDeleteDataByBills(deleteData),
- // this.project.VolumePrice.getDeleteDataByBills([node])
- ];
- project.ration_glj.deleteByBills(deleteData);
- project.quantity_detail.deleteByBills(deleteData);
- project.pushNow('deleteBills', modules, deleteDatas);
- this.datas.splice(this.datas.indexOf(node.data), 1);
- return this.tree.delete(node);
- };
- bills.prototype.singleDeleteBills=function(node,controller){
- let updateData = {};
- let updateNode={};
- let me = this;
- if(node){
- updateData[node.data.ID]=true;
- if(node.children.length>0){//有子项
- if(node.preSibling){//有前兄弟,则子项变成前兄弟的子项
- if(node.preSibling.children.length>0){//前兄弟有子项,
- let preNode = node.preSibling.children[node.preSibling.children.length-1];
- updateData[preNode.data.ID]={
- NextSiblingID:node.children[0].data.ID
- }
- updateNode[preNode.data.ID] = preNode;
- }
- for(let i=0;i<node.children.length;i++){
- updateData[node.children[i].data.ID]={
- ParentID:node.preSibling.data.ID
- };
- updateNode[node.children[i].data.ID]=node.children[i];
- }
- }else {//没有前兄弟,则子项升一级
- let parent = node.nextSibling.parent;
- for(let i=0;i<node.children.length;i++){
- if(i == node.children.length-1&&node.nextSibling){//最后一个子项,在有后兄弟的情况下,作为后兄弟的前兄弟
- updateData[node.children[i].data.ID]={
- ParentID:parent.data.ID,
- NextSiblingID:node.nextSibling.data.ID
- };
- }else {
- updateData[node.children[i].data.ID]={
- ParentID:parent.data.ID
- };
- }
- updateNode[node.children[i].data.ID]=node.children[i];
- }
- }
- }
- $.bootstrapLoading.start();
- CommonAjax.post("/bills/singleDelete", {updateData:updateData,projectID:node.data.projectID,user_id:userID}, function () {
- controller.singleDelete();//删除树节点
- me.tree.singleDelete(node);
- //更新缓存
- console.log(updateNode);
- _.remove(me.datas,{'ID':node.data.ID});
- for(let n_key in updateNode){
- let updateDoc = updateData[n_key];
- for(let u_key in updateDoc){
- updateNode[n_key].data[u_key] =updateDoc[u_key];
- }
- }
- $.bootstrapLoading.end();
- }, function () {
- $.bootstrapLoading.end();
- });
- }
- };
- bills.prototype.upMoveBills = function (node) {
- var upMoveData = node.getUpMoveData();
- project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
- return node.upMove();
- };
- bills.prototype.downMoveBills = function (node) {
- var downMoveData = node.getDownMoveData();
- project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
- return node.downMove();
- };
- bills.prototype.upLevelBills = function (node) {
- var upLevelData = node.getUpLevelData();
- project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
- return node.upLevel();
- };
- bills.prototype.downLevelBills = function (node) {
- var downLevelData = node.getDownLevelData();
- project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
- return node.downLevel();
- };
- bills.prototype.updateField = function (node, field, newValue) {
- calcFees.setFee(node.data, field, newValue);
- let updateData = [];
- let data = {'ID': node.getID(), 'projectID': this.project.ID()};
- data[field] = newValue;
- updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
- this.project.pushNow('updateBills', this.getSourceType(), updateData);
- };
- bills.prototype.getUpdateAllData = function () {
- let updateData = [];
- for (let data of this.datas) {
- updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
- }
- return updateData;
- };
- bills.prototype.updateAll = function () {
- this.project.pushNow('updateAllBills', this.getSourceType(), this.getUpdateAllData());
- };
- bills.prototype.getUpdateNodesData = function (nodes) {
- let updateData = [];
- for (let node of nodes) {
- updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
- }
- return updateData;
- }
- bills.prototype.updateNodes = function (nodes, updateNow) {
- if (updateNow) {
- this.project.pushNow('updateBills', this.getSourceType(), this.getUpdateNodesData(nodes));
- } else {
- this.project.push(this.getSourceType(), this.getUpdateNodesData(nodes));
- }
- };
- bills.prototype.sameStdCode = function (stdCode, filterCode) {
- let reg = new RegExp('^' + stdCode), matchs= [];
- for (let data of this.datas) {
- if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
- matchs.push(data.code);
- }
- }
- return matchs;
- }
- bills.prototype.newFormatCode = function (stdCode, filterCode) {
- let matchs = this.sameStdCode(stdCode, filterCode);
- let format = function (Number) {
- let s = Number + '';
- while (s.length < 3) {
- s = '0' + s;
- }
- return s;
- }
- for (i = 0; i <= matchs.length; i++) {
- let formatCode = stdCode + format(i+1);
- if (matchs.indexOf(formatCode) === -1) {
- return formatCode;
- }
- }
- };
- bills.prototype.replaceBills = function (node, stdBillsData, code) {
- let updateData = [];
- node.data.code = code;
- if (stdBillsData) {
- node.data.name = stdBillsData.name;
- node.data.unit = stdBillsData.unit;
- // 工程量计算规则
- node.data.ruleText = stdBillsData.ruleText;
- // 说明(补注)
- node.data.comments = stdBillsData.recharge;
- // 工作内容
- node.data.jobContent = stdBillsData.jobContent;
- node.data.jobContentText = stdBillsData.jobContentText;
- // 特征
- node.data.itemCharacter = stdBillsData.itemCharacter;
- node.data.itemCharacterText = stdBillsData.itemCharacterText;
- node.data.programID = stdBillsData.engineering;
- }
- updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
- this.project.pushNow('replaceBills', this.getSourceType(), updateData);
- return node;
- };
- bills.prototype.sameStdCodeBillsData = function (stdCode) {
- let reg = new RegExp('^' + stdCode);
- for (let data of this.datas) {
- if (data.code && data.code.length === 12 && reg.test(data.code) && /^[\d]+$/.test(data.code)) {
- return data;
- }
- }
- return null;
- };
- bills.prototype.getSubdivisionProjectLeavesID=function () {//取所有分部分项工程清单叶子节点ID
- let roots = projectObj.project.mainTree.roots;//所有根节点
- let subdivisionNode = null;
- for(let r of roots){
- if(isFlag(r.data)&&r.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING) {
- subdivisionNode = r;
- break;
- }
- }
- let nodes = this.getLeavesBillNodes(subdivisionNode);
- return _.map(nodes,"data.ID");
- };
- bills.prototype.getTechLeavesID=function () {//取所有分计算技术措施项目清单叶子节点ID
- let items = projectObj.project.mainTree.roots;//所有节点;
- let techNode = null;
- for(let item of items){
- if(isFlag(item.data)&&item.data.flagsIndex.fixed.flag==fixedFlag.CONSTRUCTION_TECH){
- techNode = item;
- break;
- }
- }
- let nodes = this.getLeavesBillNodes(techNode);
- return _.map(nodes,"data.ID");
- };
- bills.prototype.getLeavesBillNodes = function (rnode) {//取该节点下的所有清单叶子节点
- let leaves = [];
- getLeaves(rnode,leaves);
- return leaves;
- function getLeaves(node,children) {
- if(node){
- if(node.source.children.length>0){
- for(let c of node.children){
- getLeaves(c,children)
- }
- }else {
- children.push(node);
- }
- }
- }
- };
- bills.prototype.getRootNode = function (node) {
- if(node.parent){
- return this.getRootNode(node.parent);
- }else {
- return node;
- }
- };
- bills.prototype.isFBFX = function (node) {//判读是否属于分部分项
- let rootNode = this.getRootNode(node);
- if(isFlag(rootNode.data)&&rootNode.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING){
- return true;
- }else {
- return false;
- }
- };
- bills.prototype.isEngineeringCost = function (node) {//判断这个节点是否是工程造价节点
- if(isFlag(node.data)&&node.data.flagsIndex.fixed.flag==fixedFlag.ENGINEERINGCOST){
- return true;
- }else {
- return false;
- }
- };
- return new bills(project);
- }
- };
- function isDef(v) {
- return v !== undefined && v !== null;
- }
- function isFlag(v) {
- return this.isDef(v.flagsIndex) && this.isDef(v.flagsIndex.fixed);
- }
- function getRootFixedNode(node) {
- let parent = node.parent;
- if(isFlag(node.data) && (node.data.flagsIndex.fixed.flag === fixedFlag.SUB_ENGINERRING
- || node.data.flagsIndex.fixed.flag === fixedFlag.MEASURE
- || node.data.flagsIndex.fixed.flag === fixedFlag.OTHER
- || node.data.flagsIndex.fixed.flag === fixedFlag.CHARGE
- || node.data.flagsIndex.fixed.flag === fixedFlag.TAX)){
- return node;
- }
- else {
- if(!parent){
- return node;
- }
- else {
- return getRootFixedNode(parent);
- }
- }
- }
|