ration_glj.js 19 KB

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