ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. return result;
  77. }
  78. // 提交数据后返回数据处理
  79. ration_glj.prototype.doAfterUpdate = function(err, data){
  80. if(!err){
  81. if(data.updateTpye=='ut_update'){
  82. this.refreshAfterUpdate(data);
  83. }else if(data.updateTpye=='ut_delete'){
  84. this.refreshAfterDelete(data);
  85. } else {
  86. this.refreshAfterSave(data);
  87. }
  88. projectObj.project.projectGLJ.loadData();
  89. }
  90. };
  91. ration_glj.prototype.refreshAfterSave=function(data){
  92. let neRecodes=[];
  93. if(data){
  94. neRecodes=data.newRecords;
  95. gljOprObj.sheetData=data.showDatas;
  96. }
  97. if(projectObj.project.ration_glj.datas&&Array.isArray(projectObj.project.ration_glj.datas)){
  98. if(data){
  99. projectObj.project.ration_glj.datas = projectObj.project.ration_glj.datas.concat(neRecodes);
  100. }
  101. }else {
  102. projectObj.project.ration_glj.datas = neRecodes;
  103. }
  104. gljOprObj.showRationGLJSheetData();
  105. };
  106. ration_glj.prototype.refreshAfterUpdate=function(data){
  107. var me = this;
  108. if(data.quantityRefresh){
  109. data.glj_result.forEach(function (item) {
  110. me.refreshEachItme(item.doc,item.query);
  111. })
  112. }else {
  113. me.refreshEachItme(data.doc,data.query);
  114. }
  115. gljOprObj.showRationGLJSheetData();
  116. };
  117. ration_glj.prototype.refreshEachItme = function (doc,query) {
  118. var glj_list = projectObj.project.ration_glj.datas;
  119. var glj_index= _.findIndex(glj_list,(glj)=>{
  120. return glj.ID==query.ID;
  121. })
  122. var sheet_index= _.findIndex(gljOprObj.sheetData,(sd)=>{
  123. return sd.ID==query.ID;
  124. })
  125. _.forEach(doc, function(n, key) {
  126. glj_list[glj_index][key] = n;
  127. gljOprObj.sheetData[sheet_index][key]=n;
  128. });
  129. return glj_list[glj_index].rationID;
  130. };
  131. ration_glj.prototype.refreshAfterDelete=function(data){
  132. var glj_list = projectObj.project.ration_glj.datas;
  133. _.remove(glj_list,data.query);
  134. _.remove(gljOprObj.sheetData,data.query);
  135. gljOprObj.showRationGLJSheetData();
  136. projectObj.project.projectGLJ.loadData();
  137. var selected = projectObj.project.mainTree.selected;
  138. selected.data.adjustState=data.adjustState;
  139. projectObj.mainController.refreshTreeNode([selected]);
  140. };
  141. // CSL,2017.05.09
  142. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  143. this.project.beginUpdate('modifyQuantity');
  144. data.quantity = newQuantity;
  145. data.customQuantity = newQuantity;
  146. data.updateType = 'ut_update';
  147. this.project.push(this.getSourceType, data);
  148. this.project.endUpdate();
  149. };
  150. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  151. this.project.beginUpdate('modifyPrice');
  152. data.price = newPrice;
  153. data.updateType = 'ut_update';
  154. this.project.push(this.getSourceType, data);
  155. this.project.endUpdate();
  156. };
  157. ration_glj.prototype.deleteGLJ = function (data) {
  158. this.project.beginUpdate('deleteGLJ');
  159. data.customQuantity = 0;
  160. data.quantity = 0;
  161. data.rationItemQuantity = 0;
  162. data.updateType = 'ut_update';
  163. this.project.push(this.getSourceType, data);
  164. this.project.endUpdate();
  165. };
  166. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  167. var souceTypeList=[];
  168. var criteriaDataList = [];
  169. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  170. let criteria= {};
  171. criteria.ration_glj_list = [];
  172. for(let i=0;i<data.rationGljList.length;i++){
  173. let temdata = data.rationGljList[i];
  174. let newGLJ = {};
  175. newGLJ.projectID = newRation.projectID;
  176. newGLJ.GLJID = temdata.gljId;
  177. newGLJ.rationID = newRation.ID;
  178. newGLJ.rationItemQuantity= temdata.consumeAmt;
  179. newGLJ.quantity=temdata.consumeAmt;
  180. newGLJ.glj_repository_id=data.rationRepId;
  181. criteria.ration_glj_list.push(newGLJ);
  182. }
  183. criteria.updateType = 'ut_create';
  184. souceTypeList.push(this.getSourceType());
  185. criteriaDataList.push(criteria);
  186. }
  187. var ration_coe = projectObj.project.ration_coe;
  188. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  189. souceTypeList.push(ration_coe.getSourceType());
  190. criteriaDataList.push(rationCoeData);
  191. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  192. };
  193. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  194. var updateData = [];
  195. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  196. return updateData;
  197. };
  198. ration_glj.prototype.getDeleteDataByBills=function(datas){
  199. var updateData = [];
  200. datas.forEach(function (deleteData) {
  201. if(deleteData.type=='delete'){
  202. var billData = deleteData.data;
  203. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  204. }
  205. })
  206. return updateData;
  207. };
  208. ration_glj.prototype.deleteByRation = function(ration){
  209. var glj_list = projectObj.project.ration_glj.datas;
  210. var newList =_.filter(glj_list,(glj)=>{
  211. return glj.rationID!=ration.ID;
  212. });
  213. if(newList!=undefined){
  214. projectObj.project.ration_glj.datas = newList;
  215. }
  216. };
  217. ration_glj.prototype.deleteByBills=function(deleteData){
  218. var rationList = projectObj.project.Ration.datas;
  219. var deleteRationList = [];
  220. for(var i=0;i<deleteData.length;i++){
  221. if(deleteData[i].type=='delete'){
  222. var billID = deleteData[i].data.ID;
  223. var raList =_.filter(rationList,(ration)=>{
  224. return ration.billsItemID==billID;
  225. });
  226. deleteRationList = deleteRationList.concat(raList);
  227. }
  228. }
  229. for(var i=0;i<deleteRationList.length;i++){
  230. this.deleteByRation(deleteRationList[i]);
  231. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  232. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  233. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  234. }
  235. }
  236. ration_glj.prototype.updataOrdelete=function(row){
  237. var updateData = null;
  238. if(row.rationItemQuantity==0){
  239. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID},{rationID:row.rationID});
  240. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  241. }else {
  242. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  243. }
  244. };
  245. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  246. var updateData = [];
  247. var newobj = {
  248. 'updateType': type,
  249. 'query': query,
  250. }
  251. if(doc){
  252. newobj['doc']=doc;
  253. }
  254. if(callfunction){
  255. newobj['updateFunction']=callfunction;
  256. }
  257. updateData.push(newobj);
  258. return updateData;
  259. };
  260. ration_glj.prototype.customQuantityUpdate = function(recode,newVal,text){
  261. newVal = _.round(newVal,3);
  262. var query = {
  263. 'ID':recode.ID,
  264. 'projectID': recode.projectID,
  265. 'rationID':recode.rationID
  266. };
  267. var doc;
  268. if(text===null){
  269. doc ={
  270. customQuantity:null
  271. };
  272. }else {
  273. doc ={
  274. customQuantity:newVal,
  275. quantity:newVal
  276. };
  277. }
  278. var updateData = this.getUpdateData('ut_update',query,doc,'customQuantityUpdate');
  279. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  280. };
  281. ration_glj.prototype.marketPriceAdjustUpdate = function (recode,newVal,text) {
  282. newVal = _.round(newVal,2);
  283. if(text===null){
  284. newVal=null;
  285. }
  286. var query = {
  287. 'ID':recode.ID,
  288. 'projectID': recode.projectID,
  289. 'rationID':recode.rationID
  290. };
  291. var doc ={
  292. base_price:Number(recode.basePrice),
  293. market_price:newVal,
  294. code:recode.code,
  295. name:recode.name,
  296. project_id:recode.projectID,
  297. repositoryId:recode.repositoryId
  298. };
  299. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  300. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  301. };
  302. ration_glj.prototype.getGLJData = function(cb){
  303. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  304. cb(data);
  305. })
  306. };
  307. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  308. var gljList=[];
  309. var allGLJ=gljOprObj.AllRecode;
  310. GLJSelection.sort();
  311. _.forEach(GLJSelection,function (g) {
  312. var glj=_.find(allGLJ,{'code':g});
  313. var ration_glj ={
  314. projectID:ration.projectID,
  315. GLJID:glj.ID,
  316. rationID:ration.ID,
  317. rationItemQuantity:0,
  318. quantity:0,
  319. name:glj.name,
  320. code:glj.code,
  321. unit:glj.unit,
  322. specs:glj.specs,
  323. basePrice:glj.basePrice,
  324. shortName:glj.shortName,
  325. type:glj.gljType,
  326. createType:'add',
  327. repositoryId:glj.repositoryId
  328. }
  329. if(glj.hasOwnProperty("compilationId")){
  330. ration_glj.from="cpt";
  331. }
  332. gljList.push(ration_glj);
  333. });
  334. CommonAjax.post("/rationGlj/addGLJ",gljList,callback);
  335. };
  336. ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
  337. var allGLJ=gljOprObj.AllRecode;
  338. var glj=_.find(allGLJ,{'code':selectCode});
  339. if(selectCode==oldData.code){
  340. return callback(null);
  341. }
  342. if(oldData.createType!='replace'){
  343. oldData.rcode=oldData.code;
  344. oldData.createType='replace';
  345. }
  346. oldData.GLJID=glj.ID;
  347. oldData.rationItemQuantity=0;
  348. oldData.name=glj.name;
  349. oldData.code=glj.code;
  350. oldData.unit=glj.unit;
  351. oldData.specs=glj.specs;
  352. oldData.basePrice=glj.basePrice;
  353. oldData.repositoryId=glj.repositoryId;
  354. if(glj.hasOwnProperty("compilationId")){
  355. oldData.from="cpt";
  356. }else {
  357. oldData.from="std";
  358. }
  359. CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback);
  360. };
  361. ration_glj.prototype.mReplaceGLJ=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. var query={
  368. projectID:oldData.projectID,
  369. code:oldData.code,
  370. name:oldData.name
  371. }
  372. var doc={
  373. GLJID:glj.ID,
  374. createType:'replace',
  375. rationItemQuantity:0,
  376. name:glj.name,
  377. code:glj.code,
  378. unit:glj.unit,
  379. specs:glj.specs,
  380. type:glj.gljType,
  381. basePrice:glj.basePrice,
  382. repositoryId:glj.repositoryId,
  383. projectID:oldData.projectID
  384. }
  385. if(oldData.createType=='replace'){
  386. doc.rcode=oldData.rcode;
  387. }else {
  388. doc.rcode=oldData.code;
  389. }
  390. if(glj.hasOwnProperty("compilationId")){
  391. doc.from="cpt";
  392. }else {
  393. doc.from="std";
  394. }
  395. CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback);
  396. };
  397. return new ration_glj(project);
  398. }
  399. };