ration_glj.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var ration_glj = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. var ration_glj = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. var sourceType = ModuleNames.ration_glj;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration_glj, this);
  18. };
  19. // 从后台获取数据
  20. /*glj.prototype.pullData = function (){
  21. this.project.pullData(
  22. '/glj/getData',
  23. {projectID: this.project.ID},
  24. function(result){
  25. if (result.error ===0){
  26. this.loadDatas(result.data);
  27. }
  28. else {
  29. // to do: 错误处理需要细化
  30. alert(result.message);
  31. }
  32. },
  33. function (){}//to do: 错误处理需要细化
  34. )
  35. };*/
  36. // prototype用于定义public方法
  37. ration_glj.prototype.loadData = function (datas) {
  38. this.datas = datas;
  39. };
  40. ration_glj.prototype.getGljArrByRation = function (rationID) {
  41. let result = this.datas.filter(function (data) {
  42. return data.rationID === rationID;
  43. })
  44. result = gljOprObj.combineWithProjectGlj(result);
  45. return result;
  46. };
  47. ration_glj.prototype.getGatherGljArrByRations = function (rations) {
  48. let result = [];
  49. let clone = function (obj) {
  50. if (obj === null) return null;
  51. var o = Object.prototype.toString.apply(obj) === "[object Array]" ? [] : {};
  52. for (var i in obj) {
  53. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? clone(obj[i]) : obj[i]);
  54. }
  55. return o;
  56. }
  57. let findGlj = function (sourceGlj, gljArr) {
  58. for (let glj of gljArr) {
  59. if (glj.projectGLJID === sourceGlj.projectGLJID) {
  60. return glj;
  61. }
  62. }
  63. return null;
  64. }
  65. for (let ration of rations) {
  66. let rationGljs = this.getGljArrByRation(ration.ID);
  67. for (let glj of rationGljs) {
  68. let sameGlj = findGlj(glj, result);
  69. if (!sameGlj) {
  70. sameGlj = clone(glj);
  71. sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
  72. result.push(sameGlj);
  73. } else {
  74. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  75. }
  76. }
  77. };
  78. result = gljOprObj.combineWithProjectGlj(result);
  79. return result;
  80. }
  81. // 提交数据后返回数据处理
  82. ration_glj.prototype.doAfterUpdate = function(err, data){
  83. if(!err){
  84. if(data.updateTpye=='ut_update'){
  85. this.refreshAfterUpdate(data);
  86. }else if(data.updateTpye=='ut_delete'){
  87. this.refreshAfterDelete(data);
  88. } else {
  89. this.refreshAfterSave(data);
  90. }
  91. projectObj.project.projectGLJ.loadData();
  92. }
  93. };
  94. ration_glj.prototype.refreshAfterSave=function(data){
  95. let neRecodes=[];
  96. if(data){
  97. // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
  98. neRecodes = data.showDatas;
  99. gljOprObj.sheetData=neRecodes;
  100. }
  101. if(projectObj.project.ration_glj.datas&&Array.isArray(projectObj.project.ration_glj.datas)){
  102. if(data){
  103. projectObj.project.ration_glj.datas = projectObj.project.ration_glj.datas.concat(neRecodes);
  104. }
  105. }else {
  106. projectObj.project.ration_glj.datas = neRecodes;
  107. }
  108. gljOprObj.showRationGLJSheetData(true);
  109. //add to mainTree;
  110. let node = project.mainTree.selected;
  111. project.calcProgram.calculate(node);
  112. project.calcProgram.saveNode(node);
  113. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  114. calcProgramObj.showData(node, false);
  115. };
  116. };
  117. ration_glj.prototype.refreshAfterUpdate=function(data){
  118. var me = this;
  119. if(data.quantityRefresh){
  120. data.glj_result.forEach(function (item) {
  121. me.refreshEachItme(item.doc,item.query);
  122. })
  123. }else {
  124. me.refreshEachItme(data.doc,data.query);
  125. }
  126. gljOprObj.showRationGLJSheetData(true);
  127. };
  128. ration_glj.prototype.refreshEachItme = function (doc,query) {
  129. var glj_list = projectObj.project.ration_glj.datas;
  130. var glj_index= _.findIndex(glj_list,(glj)=>{
  131. return glj.ID==query.ID;
  132. })
  133. var sheet_index= _.findIndex(gljOprObj.sheetData,(sd)=>{
  134. return sd.ID==query.ID;
  135. })
  136. _.forEach(doc, function(n, key) {
  137. glj_list[glj_index][key] = n;
  138. });
  139. return glj_list[glj_index].rationID;
  140. };
  141. ration_glj.prototype.refreshAfterDelete=function(data){
  142. var glj_list = projectObj.project.ration_glj.datas;
  143. _.remove(glj_list,data.query);
  144. _.remove(gljOprObj.sheetData,data.query);
  145. gljOprObj.showRationGLJSheetData();
  146. projectObj.project.projectGLJ.loadData();
  147. var selected = projectObj.project.mainTree.selected;
  148. selected.data.adjustState=data.adjustState;
  149. projectObj.mainController.refreshTreeNode([selected]);
  150. };
  151. // CSL,2017.05.09
  152. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  153. this.project.beginUpdate('modifyQuantity');
  154. data.quantity = newQuantity;
  155. data.customQuantity = newQuantity;
  156. data.updateType = 'ut_update';
  157. this.project.push(this.getSourceType, data);
  158. this.project.endUpdate();
  159. };
  160. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  161. this.project.beginUpdate('modifyPrice');
  162. data.price = newPrice;
  163. data.updateType = 'ut_update';
  164. this.project.push(this.getSourceType, data);
  165. this.project.endUpdate();
  166. };
  167. ration_glj.prototype.deleteGLJ = function (data) {
  168. this.project.beginUpdate('deleteGLJ');
  169. data.customQuantity = 0;
  170. data.quantity = 0;
  171. data.rationItemQuantity = 0;
  172. data.updateType = 'ut_update';
  173. this.project.push(this.getSourceType, data);
  174. this.project.endUpdate();
  175. };
  176. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  177. var souceTypeList=[];
  178. var criteriaDataList = [];
  179. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  180. let criteria= {};
  181. criteria.ration_glj_list = [];
  182. for(let i=0;i<data.rationGljList.length;i++){
  183. let temdata = data.rationGljList[i];
  184. let newGLJ = {};
  185. newGLJ.projectID = parseInt(newRation.projectID);
  186. newGLJ.GLJID = temdata.gljId;
  187. newGLJ.rationID = newRation.ID;
  188. newGLJ.billsItemID=newRation.billsItemID,
  189. newGLJ.rationItemQuantity= temdata.consumeAmt;
  190. newGLJ.quantity=temdata.consumeAmt;
  191. newGLJ.glj_repository_id=data.rationRepId;
  192. criteria.ration_glj_list.push(newGLJ);
  193. }
  194. criteria.updateType = 'ut_create';
  195. souceTypeList.push(this.getSourceType());
  196. criteriaDataList.push(criteria);
  197. }
  198. var ration_coe = projectObj.project.ration_coe;
  199. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  200. souceTypeList.push(ration_coe.getSourceType());
  201. criteriaDataList.push(rationCoeData);
  202. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  203. };
  204. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  205. var updateData = [];
  206. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  207. return updateData;
  208. };
  209. ration_glj.prototype.getDeleteDataByBills=function(datas){
  210. var updateData = [];
  211. datas.forEach(function (deleteData) {
  212. if(deleteData.type=='delete'){
  213. var billData = deleteData.data;
  214. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  215. }
  216. })
  217. return updateData;
  218. };
  219. ration_glj.prototype.deleteByRation = function(ration){
  220. var glj_list = projectObj.project.ration_glj.datas;
  221. var newList =_.filter(glj_list,(glj)=>{
  222. return glj.rationID!=ration.ID;
  223. });
  224. if(newList!=undefined){
  225. projectObj.project.ration_glj.datas = newList;
  226. }
  227. };
  228. ration_glj.prototype.deleteByBills=function(deleteData){
  229. var rationList = projectObj.project.Ration.datas;
  230. var deleteRationList = [];
  231. for(var i=0;i<deleteData.length;i++){
  232. if(deleteData[i].type=='delete'){
  233. var billID = deleteData[i].data.ID;
  234. var raList =_.filter(rationList,(ration)=>{
  235. return ration.billsItemID==billID;
  236. });
  237. deleteRationList = deleteRationList.concat(raList);
  238. }
  239. }
  240. for(var i=0;i<deleteRationList.length;i++){
  241. this.deleteByRation(deleteRationList[i]);
  242. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  243. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  244. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  245. }
  246. }
  247. ration_glj.prototype.updataOrdelete=function(row){
  248. var updateData = null;
  249. if(row.rationItemQuantity==0){
  250. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID},{rationID:row.rationID});
  251. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  252. }else {
  253. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  254. }
  255. };
  256. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  257. var updateData = [];
  258. var newobj = {
  259. 'updateType': type,
  260. 'query': query,
  261. }
  262. if(doc){
  263. newobj['doc']=doc;
  264. }
  265. if(callfunction){
  266. newobj['updateFunction']=callfunction;
  267. }
  268. updateData.push(newobj);
  269. return updateData;
  270. };
  271. ration_glj.prototype.updateRationGLJByEdit=function (recode,updateField,newval,node) {
  272. var me=this;
  273. $.bootstrapLoading.start();
  274. var callback=function (data) {
  275. let initShow = false;//是否需要表格初始化显示
  276. if(updateField=='customQuantity'){
  277. console.log(data);
  278. me.refreshAfterQuantityUpdate(data,node);
  279. }else {
  280. var doc = data.doc;
  281. for(var key in doc){
  282. recode[key] = doc[key];
  283. }
  284. console.log(data);
  285. if(data.hasOwnProperty('adjustState')){//更新定额调整状态
  286. me.updateRationAdjustState(data.adjustState,recode.rationID,node);
  287. }
  288. if(recode.subList&&recode.subList.length>0){
  289. initShow = true;
  290. }
  291. }
  292. if(initShow==false){//不需要初始化,只需耍新当前显示就可以了
  293. gljOprObj.showRationGLJSheetData();
  294. }
  295. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  296. if(initShow==true){
  297. gljOprObj.refreshView();
  298. }
  299. $.bootstrapLoading.end();
  300. });
  301. }
  302. var query = {
  303. 'ID':recode.ID,
  304. 'projectID': recode.projectID,
  305. 'rationID':recode.rationID
  306. };
  307. var priceInfo={
  308. base_price: recode.basePrice,
  309. market_price: recode.marketPrice
  310. }
  311. var doc = {};
  312. doc[updateField]=newval;
  313. if(updateField=="type"){
  314. doc.shortName = gljTypeMap[newval];
  315. }
  316. CommonAjax.post("/rationGlj/updateRationGLJByEdit",{query:query,doc:doc,priceInfo:priceInfo},callback,function (err) {
  317. $.bootstrapLoading.end();
  318. });
  319. }
  320. ration_glj.prototype.refreshAfterQuantityUpdate=function (data,node) {
  321. var me=this;
  322. data.glj_result.forEach(function (item) {
  323. me.refreshEachItme(item.doc,item.query);
  324. })
  325. me.updateRationAdjustState(data.adjustState,data.rationID,node);
  326. };
  327. ration_glj.prototype.updateRationAdjustState=function(adjustState,rationID,rnode){
  328. var nodes=[];
  329. var node = _.find(projectObj.project.mainTree.items,function (n) {
  330. return n.sourceType==ModuleNames.ration&&n.data.ID==rationID;
  331. })
  332. if(node){
  333. node.data.adjustState=adjustState;
  334. nodes.push(node)
  335. }
  336. if(rnode){
  337. nodes.push(rnode);
  338. }
  339. projectObj.mainController.refreshTreeNode(nodes);
  340. };
  341. ration_glj.prototype.getGLJData = function(cb){
  342. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  343. cb(data);
  344. })
  345. };
  346. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  347. var gljList=[];
  348. var allGLJ=gljOprObj.AllRecode;
  349. GLJSelection.sort();
  350. _.forEach(GLJSelection,function (g) {
  351. var glj=_.find(allGLJ,{'code':g});
  352. var ration_glj ={
  353. projectID:ration.projectID,
  354. GLJID:glj.ID,
  355. rationID:ration.ID,
  356. billsItemID:ration.billsItemID,
  357. rationItemQuantity:0,
  358. quantity:0,
  359. name:glj.name,
  360. code:glj.code,
  361. original_code:glj.code,
  362. unit:glj.unit,
  363. specs:glj.specs,
  364. basePrice:glj.basePrice,
  365. shortName:glj.shortName,
  366. type:glj.gljType,
  367. adjCoe:glj.adjCoe,
  368. createType:'add',
  369. repositoryId:glj.repositoryId
  370. }
  371. if(glj.hasOwnProperty("compilationId")){
  372. ration_glj.from="cpt";
  373. if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  374. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  375. }
  376. }
  377. gljList.push(ration_glj);
  378. });
  379. $.bootstrapLoading.start();
  380. CommonAjax.post("/rationGlj/addGLJ",gljList,callback,function () {
  381. $.bootstrapLoading.end();
  382. });
  383. };
  384. ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
  385. var allGLJ=gljOprObj.AllRecode;
  386. var glj=_.find(allGLJ,{'code':selectCode});
  387. if(selectCode==oldData.code){
  388. return callback(null);
  389. }
  390. if(oldData.createType!='replace'){
  391. oldData.rcode=oldData.code;
  392. oldData.createType='replace';
  393. }
  394. oldData.GLJID=glj.ID;
  395. oldData.rationItemQuantity=0;
  396. oldData.name=glj.name;
  397. oldData.code=glj.code;
  398. oldData.original_code=glj.code;
  399. oldData.unit=glj.unit;
  400. oldData.specs=glj.specs;
  401. oldData.basePrice=glj.basePrice;
  402. oldData.repositoryId=glj.repositoryId;
  403. if(glj.hasOwnProperty("compilationId")){
  404. oldData.from="cpt";
  405. if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  406. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  407. }
  408. }else {
  409. oldData.from="std";
  410. }
  411. $.bootstrapLoading.start();
  412. CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback,function () {
  413. $.bootstrapLoading.end();
  414. });
  415. };
  416. ration_glj.prototype.mReplaceGLJ=function (selectCode,oldData,callback) {
  417. var allGLJ=gljOprObj.AllRecode;
  418. var glj=_.find(allGLJ,{'code':selectCode});
  419. if(selectCode==oldData.code){
  420. return callback(null);
  421. }
  422. var query={
  423. projectID:oldData.projectID,
  424. code:oldData.code,
  425. name:oldData.name,
  426. unit:oldData.unit,
  427. type:oldData.type
  428. }
  429. if(oldData.specs&&oldData.specs!=''){
  430. query.specs=oldData.specs;
  431. }
  432. var doc={
  433. GLJID:glj.ID,
  434. createType:'replace',
  435. rationItemQuantity:0,
  436. name:glj.name,
  437. code:glj.code,
  438. original_code:glj.code,
  439. unit:glj.unit,
  440. specs:glj.specs,
  441. type:glj.gljType,
  442. basePrice:glj.basePrice,
  443. repositoryId:glj.repositoryId,
  444. projectID:oldData.projectID
  445. }
  446. if(oldData.createType=='replace'){
  447. doc.rcode=oldData.rcode;
  448. }else {
  449. doc.rcode=oldData.code;
  450. }
  451. if(glj.hasOwnProperty("compilationId")){
  452. doc.from="cpt";
  453. if(glj.code.indexOf('-')!=-1){//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  454. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  455. }
  456. }else {
  457. doc.from="std";
  458. }
  459. $.bootstrapLoading.start();
  460. CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback,function () {
  461. $.bootstrapLoading.end();
  462. });
  463. };
  464. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  465. var gljList = _.filter(this.datas,function (n) {
  466. return n.rationID == rationID&&(n.type==gljType.MAIN_MATERIAL||n.type==gljType.EQUIPMENT)
  467. });
  468. return gljOprObj.combineWithProjectGlj(gljList);
  469. };
  470. ration_glj.prototype.transferToNodeData = function (data) {
  471. data.subType = data.type;
  472. data.marketUnitFee = data.marketPrice;
  473. };
  474. ration_glj.prototype.updateFromMainSpread=function (value,node,fieldName) {
  475. console.log(fieldName);
  476. console.log(value);
  477. if(node.data[fieldName]===value){
  478. return;
  479. }
  480. if(fieldName=="marketUnitFee"){
  481. }else {
  482. if(value!==undefined&&value!==null){
  483. if(fieldName=="subType"){
  484. node.data.subType = value;
  485. fieldName = "type";//转换成更新工料机类型
  486. }
  487. this.updateRationGLJByEdit(node.data,fieldName,value,node);
  488. return;
  489. }
  490. }
  491. // node.data.subType = value;
  492. projectObj.mainController.refreshTreeNode([node]);
  493. };
  494. return new ration_glj(project);
  495. }
  496. };