123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var ration_glj = {
- createNew: function (project) {
- // 用户定义private方法
- var tools = {};
- // 所有通过this访问的属性,都不应在此单元外部进行写入操作
- var ration_glj = function (proj) {
- this.gljTree = cacheTree.createNew(this);
- // this.project = proj;
- this.datas = [];
- var sourceType = ModuleNames.ration_glj;
- this.getSourceType = function () {
- return sourceType;
- }
- proj.registerModule(ModuleNames.ration_glj, this);
- };
- // 从后台获取数据
- /*glj.prototype.pullData = function (){
- this.project.pullData(
- '/glj/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方法
- ration_glj.prototype.loadData = function (datas) {
- this.datas = datas;
- };
- ration_glj.prototype.getGljArrByRation = function (rationID) {
- return this.datas.filter(function (data) {
- return data.rationID === rationID;
- })
- };
- ration_glj.prototype.getGatherGljArrByRations = function (rations) {
- let result = [];
- let clone = function (obj) {
- if (obj === null) return null;
- var o = Object.prototype.toString.apply(obj) === "[object Array]" ? [] : {};
- for (var i in obj) {
- o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? clone(obj[i]) : obj[i]);
- }
- return o;
- }
- let findGlj = function (sourceGlj, gljArr) {
- for (let glj of gljArr) {
- if (glj.projectGLJID === sourceGlj.projectGLJID) {
- return glj;
- }
- }
- return null;
- }
- for (let ration of rations) {
- let rationGljs = this.getGljArrByRation(ration.ID);
- for (let glj of rationGljs) {
- let sameGlj = findGlj(glj, result);
- if (!sameGlj) {
- sameGlj = clone(glj);
- sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
- result.push(sameGlj);
- } else {
- sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
- }
- }
- };
- result = gljOprObj.combineWithProjectGlj(result);
- return result;
- }
- // 提交数据后返回数据处理
- ration_glj.prototype.doAfterUpdate = function(err, data){
- if(!err){
- if(data.updateTpye=='ut_update'){
- this.refreshAfterUpdate(data);
- }else if(data.updateTpye=='ut_delete'){
- this.refreshAfterDelete(data);
- } else {
- this.refreshAfterSave(data);
- }
- projectObj.project.projectGLJ.loadData();
- }
- };
- ration_glj.prototype.refreshAfterSave=function(data){
- let neRecodes=[];
- if(data){
- // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
- neRecodes = data.showDatas;
- gljOprObj.sheetData=neRecodes;
- }
- if(projectObj.project.ration_glj.datas&&Array.isArray(projectObj.project.ration_glj.datas)){
- if(data){
- projectObj.project.ration_glj.datas = projectObj.project.ration_glj.datas.concat(neRecodes);
- }
- }else {
- projectObj.project.ration_glj.datas = neRecodes;
- }
- gljOprObj.showRationGLJSheetData(true);
- };
- ration_glj.prototype.refreshAfterUpdate=function(data){
- var me = this;
- if(data.quantityRefresh){
- data.glj_result.forEach(function (item) {
- me.refreshEachItme(item.doc,item.query);
- })
- }else {
- me.refreshEachItme(data.doc,data.query);
- }
- gljOprObj.showRationGLJSheetData(true);
- };
- ration_glj.prototype.refreshEachItme = function (doc,query) {
- var glj_list = projectObj.project.ration_glj.datas;
- var glj_index= _.findIndex(glj_list,(glj)=>{
- return glj.ID==query.ID;
- })
- var sheet_index= _.findIndex(gljOprObj.sheetData,(sd)=>{
- return sd.ID==query.ID;
- })
- _.forEach(doc, function(n, key) {
- glj_list[glj_index][key] = n;
- gljOprObj.sheetData[sheet_index][key]=n;
- });
- return glj_list[glj_index].rationID;
- };
- ration_glj.prototype.refreshAfterDelete=function(data){
- var glj_list = projectObj.project.ration_glj.datas;
- _.remove(glj_list,data.query);
- _.remove(gljOprObj.sheetData,data.query);
- gljOprObj.showRationGLJSheetData();
- projectObj.project.projectGLJ.loadData();
- var selected = projectObj.project.mainTree.selected;
- selected.data.adjustState=data.adjustState;
- projectObj.mainController.refreshTreeNode([selected]);
- };
- // CSL,2017.05.09
- ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
- this.project.beginUpdate('modifyQuantity');
- data.quantity = newQuantity;
- data.customQuantity = newQuantity;
- data.updateType = 'ut_update';
- this.project.push(this.getSourceType, data);
- this.project.endUpdate();
- };
- ration_glj.prototype.modifyPrice = function (data, newPrice) {
- this.project.beginUpdate('modifyPrice');
- data.price = newPrice;
- data.updateType = 'ut_update';
- this.project.push(this.getSourceType, data);
- this.project.endUpdate();
- };
- ration_glj.prototype.deleteGLJ = function (data) {
- this.project.beginUpdate('deleteGLJ');
- data.customQuantity = 0;
- data.quantity = 0;
- data.rationItemQuantity = 0;
- data.updateType = 'ut_update';
- this.project.push(this.getSourceType, data);
- this.project.endUpdate();
- };
- ration_glj.prototype.addRationGLJ = function (newRation,data) {
- var souceTypeList=[];
- var criteriaDataList = [];
- if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
- let criteria= {};
- criteria.ration_glj_list = [];
- for(let i=0;i<data.rationGljList.length;i++){
- let temdata = data.rationGljList[i];
- let newGLJ = {};
- newGLJ.projectID = parseInt(newRation.projectID);
- newGLJ.GLJID = temdata.gljId;
- newGLJ.rationID = newRation.ID;
- newGLJ.billsItemID=newRation.billsItemID,
- newGLJ.rationItemQuantity= temdata.consumeAmt;
- newGLJ.quantity=temdata.consumeAmt;
- newGLJ.glj_repository_id=data.rationRepId;
- criteria.ration_glj_list.push(newGLJ);
- }
- criteria.updateType = 'ut_create';
- souceTypeList.push(this.getSourceType());
- criteriaDataList.push(criteria);
- }
- var ration_coe = projectObj.project.ration_coe;
- var rationCoeData = ration_coe.getRationCoedata(newRation,data);
- souceTypeList.push(ration_coe.getSourceType());
- criteriaDataList.push(rationCoeData);
- project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
- };
- ration_glj.prototype.getDeleteDataByRation=function(rationData){
- var updateData = [];
- updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
- return updateData;
- };
- ration_glj.prototype.getDeleteDataByBills=function(datas){
- var updateData = [];
- datas.forEach(function (deleteData) {
- if(deleteData.type=='delete'){
- var billData = deleteData.data;
- updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
- }
- })
- return updateData;
- };
- 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;
- }
- };
- ration_glj.prototype.deleteByBills=function(deleteData){
- var rationList = projectObj.project.Ration.datas;
- var deleteRationList = [];
- for(var i=0;i<deleteData.length;i++){
- if(deleteData[i].type=='delete'){
- var billID = deleteData[i].data.ID;
- var raList =_.filter(rationList,(ration)=>{
- return ration.billsItemID==billID;
- });
- deleteRationList = deleteRationList.concat(raList);
- }
- }
- for(var i=0;i<deleteRationList.length;i++){
- this.deleteByRation(deleteRationList[i]);
- projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
- projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
- projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
- }
- }
- ration_glj.prototype.updataOrdelete=function(row){
- var updateData = null;
- if(row.rationItemQuantity==0){
- updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID},{rationID:row.rationID});
- project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
- }else {
- this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
- }
- };
- ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
- var updateData = [];
- var newobj = {
- 'updateType': type,
- 'query': query,
- }
- if(doc){
- newobj['doc']=doc;
- }
- if(callfunction){
- newobj['updateFunction']=callfunction;
- }
- updateData.push(newobj);
- return updateData;
- };
- ration_glj.prototype.updateRationGLJByEdit=function (recode,updateField,newval) {
- var me=this;
- $.bootstrapLoading.start();
- var callback=function (data) {
- let initShow = false;//是否需要表格初始化显示
- if(updateField=='customQuantity'){
- me.refreshAfterQuantityUpdate(data);
- }else {
- var doc = data.doc;
- for(var key in doc){
- recode[key] = doc[key];
- }
- if(data.hasOwnProperty('adjustState')){//更新定额调整状态
- me.updateRationAdjustState(data.adjustState);
- }
- if(recode.subList&&recode.subList.length>0){
- initShow = true;
- }
- }
- if(initShow==false){//不需要初始化,只需耍新当前显示就可以了
- gljOprObj.showRationGLJSheetData();
- }
- projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
- if(initShow==true){
- gljOprObj.refreshView();
- }
- $.bootstrapLoading.end();
- });
- }
- var query = {
- 'ID':recode.ID,
- 'projectID': recode.projectID,
- 'rationID':recode.rationID
- };
- var priceInfo={
- base_price: recode.basePrice,
- market_price: recode.marketPrice
- }
- var doc = {};
- doc[updateField]=newval;
- CommonAjax.post("/rationGlj/updateRationGLJByEdit",{query:query,doc:doc,priceInfo:priceInfo},callback,function (err) {
- $.bootstrapLoading.end();
- });
- }
- ration_glj.prototype.refreshAfterQuantityUpdate=function (data) {
- var me=this;
- data.glj_result.forEach(function (item) {
- me.refreshEachItme(item.doc,item.query);
- })
- me.updateRationAdjustState(data.adjustState);
- };
- ration_glj.prototype.updateRationAdjustState=function(adjustState){
- var selected = projectObj.project.mainTree.selected;
- selected.data.adjustState=adjustState;
- projectObj.mainController.refreshTreeNode([selected]);
- };
- ration_glj.prototype.getGLJData = function(cb){
- CommonAjax.get('/rationGlj/getGLJData', function (data) {
- cb(data);
- })
- };
- ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
- var gljList=[];
- var allGLJ=gljOprObj.AllRecode;
- GLJSelection.sort();
- _.forEach(GLJSelection,function (g) {
- var glj=_.find(allGLJ,{'code':g});
- var ration_glj ={
- projectID:ration.projectID,
- GLJID:glj.ID,
- rationID:ration.ID,
- billsItemID:ration.billsItemID,
- rationItemQuantity:0,
- quantity:0,
- name:glj.name,
- code:glj.code,
- original_code:glj.code,
- unit:glj.unit,
- specs:glj.specs,
- basePrice:glj.basePrice,
- shortName:glj.shortName,
- type:glj.gljType,
- createType:'add',
- repositoryId:glj.repositoryId
- }
- if(glj.hasOwnProperty("compilationId")){
- ration_glj.from="cpt";
- if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
- ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
- }
- }
- gljList.push(ration_glj);
- });
- $.bootstrapLoading.start();
- CommonAjax.post("/rationGlj/addGLJ",gljList,callback,function () {
- $.bootstrapLoading.end();
- });
- };
- ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
- var allGLJ=gljOprObj.AllRecode;
- var glj=_.find(allGLJ,{'code':selectCode});
- if(selectCode==oldData.code){
- return callback(null);
- }
- if(oldData.createType!='replace'){
- oldData.rcode=oldData.code;
- oldData.createType='replace';
- }
- oldData.GLJID=glj.ID;
- oldData.rationItemQuantity=0;
- oldData.name=glj.name;
- oldData.code=glj.code;
- oldData.original_code=glj.code;
- oldData.unit=glj.unit;
- oldData.specs=glj.specs;
- oldData.basePrice=glj.basePrice;
- oldData.repositoryId=glj.repositoryId;
- if(glj.hasOwnProperty("compilationId")){
- oldData.from="cpt";
- if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
- oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
- }
- }else {
- oldData.from="std";
- }
- $.bootstrapLoading.start();
- CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback,function () {
- $.bootstrapLoading.end();
- });
- };
- ration_glj.prototype.mReplaceGLJ=function (selectCode,oldData,callback) {
- var allGLJ=gljOprObj.AllRecode;
- var glj=_.find(allGLJ,{'code':selectCode});
- if(selectCode==oldData.code){
- return callback(null);
- }
- var query={
- projectID:oldData.projectID,
- code:oldData.code,
- name:oldData.name,
- unit:oldData.unit,
- type:oldData.type
- }
- if(oldData.specs&&oldData.specs!=''){
- query.specs=oldData.specs;
- }
- var doc={
- GLJID:glj.ID,
- createType:'replace',
- rationItemQuantity:0,
- name:glj.name,
- code:glj.code,
- original_code:glj.code,
- unit:glj.unit,
- specs:glj.specs,
- type:glj.gljType,
- basePrice:glj.basePrice,
- repositoryId:glj.repositoryId,
- projectID:oldData.projectID
- }
- if(oldData.createType=='replace'){
- doc.rcode=oldData.rcode;
- }else {
- doc.rcode=oldData.code;
- }
- if(glj.hasOwnProperty("compilationId")){
- doc.from="cpt";
- if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
- doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
- }
- }else {
- doc.from="std";
- }
- $.bootstrapLoading.start();
- CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback,function () {
- $.bootstrapLoading.end();
- });
- };
- return new ration_glj(project);
- }
- };
|