ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. };
  138. // CSL,2017.05.09
  139. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  140. this.project.beginUpdate('modifyQuantity');
  141. data.quantity = newQuantity;
  142. data.customQuantity = newQuantity;
  143. data.updateType = 'ut_update';
  144. this.project.push(this.getSourceType, data);
  145. this.project.endUpdate();
  146. };
  147. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  148. this.project.beginUpdate('modifyPrice');
  149. data.price = newPrice;
  150. data.updateType = 'ut_update';
  151. this.project.push(this.getSourceType, data);
  152. this.project.endUpdate();
  153. };
  154. ration_glj.prototype.deleteGLJ = function (data) {
  155. this.project.beginUpdate('deleteGLJ');
  156. data.customQuantity = 0;
  157. data.quantity = 0;
  158. data.rationItemQuantity = 0;
  159. data.updateType = 'ut_update';
  160. this.project.push(this.getSourceType, data);
  161. this.project.endUpdate();
  162. };
  163. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  164. var souceTypeList=[];
  165. var criteriaDataList = [];
  166. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  167. let criteria= {};
  168. criteria.ration_glj_list = [];
  169. for(let i=0;i<data.rationGljList.length;i++){
  170. let temdata = data.rationGljList[i];
  171. let newGLJ = {};
  172. newGLJ.projectID = newRation.projectID;
  173. newGLJ.GLJID = temdata.gljId;
  174. newGLJ.rationID = newRation.ID;
  175. newGLJ.rationItemQuantity= temdata.consumeAmt;
  176. newGLJ.quantity=temdata.consumeAmt;
  177. newGLJ.glj_repository_id=data.rationRepId;
  178. criteria.ration_glj_list.push(newGLJ);
  179. }
  180. criteria.updateType = 'ut_create';
  181. souceTypeList.push(this.getSourceType());
  182. criteriaDataList.push(criteria);
  183. }
  184. var ration_coe = projectObj.project.ration_coe;
  185. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  186. souceTypeList.push(ration_coe.getSourceType());
  187. criteriaDataList.push(rationCoeData);
  188. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  189. };
  190. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  191. var updateData = [];
  192. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  193. return updateData;
  194. };
  195. ration_glj.prototype.getDeleteDataByBills=function(datas){
  196. var updateData = [];
  197. datas.forEach(function (deleteData) {
  198. if(deleteData.type=='delete'){
  199. var billData = deleteData.data;
  200. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  201. }
  202. })
  203. return updateData;
  204. };
  205. ration_glj.prototype.deleteByRation = function(ration){
  206. var glj_list = projectObj.project.ration_glj.datas;
  207. var newList =_.filter(glj_list,(glj)=>{
  208. return glj.rationID!=ration.ID;
  209. });
  210. if(newList!=undefined){
  211. projectObj.project.ration_glj.datas = newList;
  212. }
  213. };
  214. ration_glj.prototype.deleteByBills=function(deleteData){
  215. var rationList = projectObj.project.Ration.datas;
  216. var deleteRationList = [];
  217. for(var i=0;i<deleteData.length;i++){
  218. if(deleteData[i].type=='delete'){
  219. var billID = deleteData[i].data.ID;
  220. var raList =_.filter(rationList,(ration)=>{
  221. return ration.billsItemID==billID;
  222. });
  223. deleteRationList = deleteRationList.concat(raList);
  224. }
  225. }
  226. for(var i=0;i<deleteRationList.length;i++){
  227. this.deleteByRation(deleteRationList[i]);
  228. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  229. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  230. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  231. }
  232. }
  233. ration_glj.prototype.updataOrdelete=function(row){
  234. var updateData = null;
  235. if(row.rationItemQuantity==0){
  236. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID});
  237. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  238. }else {
  239. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  240. }
  241. };
  242. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  243. var updateData = [];
  244. var newobj = {
  245. 'updateType': type,
  246. 'query': query,
  247. }
  248. if(doc){
  249. newobj['doc']=doc;
  250. }
  251. if(callfunction){
  252. newobj['updateFunction']=callfunction;
  253. }
  254. updateData.push(newobj);
  255. return updateData;
  256. };
  257. ration_glj.prototype.customQuantityUpdate = function(recode,newVal,text){
  258. newVal = _.round(newVal,3);
  259. var query = {
  260. 'ID':recode.ID,
  261. 'projectID': recode.projectID,
  262. 'rationID':recode.rationID
  263. };
  264. var doc;
  265. if(text===null){
  266. doc ={
  267. customQuantity:null
  268. };
  269. }else {
  270. doc ={
  271. customQuantity:newVal,
  272. quantity:newVal
  273. };
  274. }
  275. var updateData = this.getUpdateData('ut_update',query,doc,'customQuantityUpdate');
  276. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  277. };
  278. ration_glj.prototype.marketPriceAdjustUpdate = function (recode,newVal,text) {
  279. newVal = _.round(newVal,2);
  280. if(text===null){
  281. newVal=null;
  282. }
  283. var query = {
  284. 'ID':recode.ID,
  285. 'projectID': recode.projectID,
  286. 'rationID':recode.rationID
  287. };
  288. var doc ={
  289. base_price:Number(recode.basePrice),
  290. market_price:newVal,
  291. code:recode.code,
  292. name:recode.name,
  293. project_id:recode.projectID,
  294. repositoryId:recode.repositoryId
  295. };
  296. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  297. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  298. };
  299. ration_glj.prototype.getGLJData = function(cb){
  300. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  301. cb(data);
  302. })
  303. };
  304. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  305. var gljList=[];
  306. var allGLJ=gljOprObj.AllRecode;
  307. GLJSelection.sort();
  308. _.forEach(GLJSelection,function (g) {
  309. var glj=_.find(allGLJ,{'code':g});
  310. var ration_glj ={
  311. projectID:ration.projectID,
  312. GLJID:glj.ID,
  313. rationID:ration.ID,
  314. rationItemQuantity:0,
  315. quantity:0,
  316. name:glj.name,
  317. code:glj.code,
  318. unit:glj.unit,
  319. specs:glj.specs,
  320. basePrice:glj.basePrice,
  321. shortName:glj.shortName,
  322. type:glj.gljType,
  323. createType:'add',
  324. repositoryId:glj.repositoryId
  325. }
  326. if(glj.hasOwnProperty("compilationId")){
  327. ration_glj.from="cpt";
  328. }
  329. gljList.push(ration_glj);
  330. });
  331. CommonAjax.post("/rationGlj/addGLJ",gljList,callback);
  332. };
  333. ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
  334. var allGLJ=gljOprObj.AllRecode;
  335. var glj=_.find(allGLJ,{'code':selectCode});
  336. if(selectCode==oldData.code){
  337. return callback(null);
  338. }
  339. oldData.createType='replace';
  340. oldData.GLJID=glj.ID;
  341. oldData.rationItemQuantity=0;
  342. oldData.name=glj.name;
  343. oldData.rcode=oldData.code;
  344. oldData.code=glj.code;
  345. oldData.unit=glj.unit;
  346. oldData.specs=glj.specs;
  347. oldData.basePrice=glj.basePrice;
  348. oldData.repositoryId=glj.repositoryId;
  349. if(glj.hasOwnProperty("compilationId")){
  350. oldData.from="cpt";
  351. }else {
  352. oldData.from="std";
  353. }
  354. CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback);
  355. };
  356. ration_glj.prototype.mReplaceGLJ=function (selectCode,oldData,callback) {
  357. var allGLJ=gljOprObj.AllRecode;
  358. var glj=_.find(allGLJ,{'code':selectCode});
  359. if(selectCode==oldData.code){
  360. return callback(null);
  361. }
  362. var query={
  363. projectID:oldData.projectID,
  364. code:oldData.code,
  365. name:oldData.name
  366. }
  367. var doc={
  368. GLJID:glj.ID,
  369. createType:'replace',
  370. rationItemQuantity:0,
  371. name:glj.name,
  372. rcode:oldData.code,
  373. code:glj.code,
  374. unit:glj.unit,
  375. specs:glj.specs,
  376. type:glj.gljType,
  377. basePrice:glj.basePrice,
  378. repositoryId:glj.repositoryId,
  379. projectID:oldData.projectID
  380. }
  381. if(glj.hasOwnProperty("compilationId")){
  382. doc.from="cpt";
  383. }else {
  384. doc.from="std";
  385. }
  386. CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback);
  387. };
  388. return new ration_glj(project);
  389. }
  390. };