ration_glj.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 findGlj = function (sourceGlj, gljArr) {
  48. gljArr.forEach(function (glj) {
  49. if (glj.projectGLJID === sourceGlj.projectGLJID) {
  50. return glj
  51. }
  52. });
  53. return null;
  54. }
  55. for (let ration of rations) {
  56. let rationGljs = this.getGljArrByRation(ration.ID);
  57. for (let glj of rationGljs) {
  58. let sameGlj = findGlj(glj, result);
  59. if (!sameGlj) {
  60. result.push(glj);
  61. } else {
  62. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  63. }
  64. }
  65. }
  66. return result;
  67. }
  68. // 提交数据后返回数据处理
  69. ration_glj.prototype.doAfterUpdate = function(err, data){
  70. if(!err){
  71. if(data.updateTpye=='ut_update'){
  72. this.refreshAfterUpdate(data);
  73. }else if(data.updateTpye=='ut_delete'){
  74. this.refreshAfterDelete(data);
  75. } else {
  76. this.refreshAfterSave(data);
  77. }
  78. }
  79. };
  80. ration_glj.prototype.refreshAfterSave=function(data){
  81. if(projectObj.project.ration_glj.datas&&Array.isArray(projectObj.project.ration_glj.datas)){
  82. projectObj.project.ration_glj.datas = projectObj.project.ration_glj.datas.concat(data);
  83. }else {
  84. projectObj.project.ration_glj.datas = data;
  85. }
  86. sheetCommonObj.showData(gljOprObj.sheet,gljOprObj.setting,data);
  87. gljOprObj.sheetData=data;
  88. // SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
  89. };
  90. ration_glj.prototype.refreshAfterUpdate=function(data){
  91. var me = this;
  92. if(data.quantityRefresh){
  93. data.glj_result.forEach(function (item) {
  94. me.refreshEachItme(item.doc,item.query);
  95. })
  96. }else {
  97. me.refreshEachItme(data.doc,data.query);
  98. }
  99. var selected = projectObj.project.mainTree.selected
  100. if(selected==null){
  101. return;
  102. }
  103. if(selected.sourceType==ModuleNames.ration){
  104. var showList = _.filter(projectObj.project.ration_glj.datas,{'rationID':selected.data.ID});
  105. gljOprObj.sheetData=showList;
  106. sheetCommonObj.showData(gljOprObj.sheet,gljOprObj.setting,showList);
  107. }
  108. };
  109. ration_glj.prototype.refreshEachItme = function (doc,query) {
  110. var glj_list = projectObj.project.ration_glj.datas;
  111. var glj_index= _.findIndex(glj_list,(glj)=>{
  112. return glj.ID==query.ID&&glj.projectID==query.projectID;
  113. })
  114. _.forEach(doc, function(n, key) {
  115. glj_list[glj_index][key] = n;
  116. });
  117. return glj_list[glj_index].rationID;
  118. };
  119. ration_glj.prototype.refreshAfterDelete=function(data){
  120. var glj_list = projectObj.project.ration_glj.datas;
  121. _.remove(glj_list,data.query);
  122. _.remove(gljOprObj.sheetData,data.query);
  123. sheetCommonObj.showData(gljOprObj.sheet,gljOprObj.setting,gljOprObj.sheetData);
  124. };
  125. // CSL,2017.05.09
  126. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  127. this.project.beginUpdate('modifyQuantity');
  128. data.quantity = newQuantity;
  129. data.customQuantity = newQuantity;
  130. data.updateType = 'ut_update';
  131. this.project.push(this.getSourceType, data);
  132. this.project.endUpdate();
  133. };
  134. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  135. this.project.beginUpdate('modifyPrice');
  136. data.price = newPrice;
  137. data.updateType = 'ut_update';
  138. this.project.push(this.getSourceType, data);
  139. this.project.endUpdate();
  140. };
  141. ration_glj.prototype.deleteGLJ = function (data) {
  142. this.project.beginUpdate('deleteGLJ');
  143. data.customQuantity = 0;
  144. data.quantity = 0;
  145. data.rationItemQuantity = 0;
  146. data.updateType = 'ut_update';
  147. this.project.push(this.getSourceType, data);
  148. this.project.endUpdate();
  149. };
  150. ration_glj.prototype.replaceGLJ = function (data, newGLJID) {
  151. this.project.beginUpdate('replaceGLJ');
  152. data.GLJID = newGLJID;
  153. data.updateType = 'ut_update';
  154. this.project.push(this.getSourceType, data);
  155. this.project.endUpdate();
  156. };
  157. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  158. var souceTypeList=[];
  159. var criteriaDataList = [];
  160. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  161. let criteria= {};
  162. criteria.ration_glj_list = [];
  163. for(let i=0;i<data.rationGljList.length;i++){
  164. let temdata = data.rationGljList[i];
  165. let newGLJ = {};
  166. newGLJ.projectID = newRation.projectID;
  167. newGLJ.GLJID = temdata.gljId;
  168. newGLJ.rationID = newRation.ID;
  169. newGLJ.rationItemQuantity= temdata.consumeAmt;
  170. newGLJ.quantity=temdata.consumeAmt;
  171. newGLJ.glj_repository_id=data.rationRepId;
  172. criteria.ration_glj_list.push(newGLJ);
  173. }
  174. criteria.updateType = 'ut_create';
  175. souceTypeList.push(this.getSourceType());
  176. criteriaDataList.push(criteria);
  177. }
  178. var ration_coe = projectObj.project.ration_coe;
  179. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  180. souceTypeList.push(ration_coe.getSourceType());
  181. criteriaDataList.push(rationCoeData);
  182. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  183. };
  184. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  185. var updateData = [];
  186. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  187. return updateData;
  188. };
  189. ration_glj.prototype.getDeleteDataByBills=function(datas){
  190. var updateData = [];
  191. datas.forEach(function (deleteData) {
  192. if(deleteData.type=='delete'){
  193. var billData = deleteData.data;
  194. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  195. }
  196. })
  197. return updateData;
  198. };
  199. ration_glj.prototype.deleteByRation = function(ration){
  200. var glj_list = projectObj.project.ration_glj.datas;
  201. var newList =_.filter(glj_list,(glj)=>{
  202. return glj.rationID!=ration.ID;
  203. });
  204. if(newList!=undefined){
  205. projectObj.project.ration_glj.datas = newList;
  206. }
  207. };
  208. ration_glj.prototype.deleteByBills=function(deleteData){
  209. var rationList = projectObj.project.Ration.datas;
  210. var deleteRationList = [];
  211. for(var i=0;i<deleteData.length;i++){
  212. if(deleteData[i].type=='delete'){
  213. var billID = deleteData[i].data.ID;
  214. var raList =_.filter(rationList,(ration)=>{
  215. return ration.billsItemID==billID;
  216. });
  217. deleteRationList = deleteRationList.concat(raList);
  218. }
  219. }
  220. for(var i=0;i<deleteRationList.length;i++){
  221. this.deleteByRation(deleteRationList[i]);
  222. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  223. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  224. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  225. }
  226. }
  227. ration_glj.prototype.updataOrdelete=function(row){
  228. var updateData = null;
  229. if(row.rationItemQuantity==0){
  230. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID});
  231. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  232. }else {
  233. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  234. }
  235. };
  236. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  237. var updateData = [];
  238. var newobj = {
  239. 'updateType': type,
  240. 'query': query,
  241. }
  242. if(doc){
  243. newobj['doc']=doc;
  244. }
  245. if(callfunction){
  246. newobj['updateFunction']=callfunction;
  247. }
  248. updateData.push(newobj);
  249. return updateData;
  250. };
  251. ration_glj.prototype.customQuantityUpdate = function(recode,newVal,text){
  252. newVal = _.round(newVal,3);
  253. var query = {
  254. 'ID':recode.ID,
  255. 'projectID': recode.projectID,
  256. 'rationID':recode.rationID
  257. };
  258. var doc;
  259. if(text===null){
  260. doc ={
  261. customQuantity:null
  262. };
  263. }else {
  264. doc ={
  265. customQuantity:newVal,
  266. quantity:newVal
  267. };
  268. }
  269. var updateData = this.getUpdateData('ut_update',query,doc,'customQuantityUpdate');
  270. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  271. };
  272. ration_glj.prototype.marketPriceAdjustUpdate = function (recode,newVal,text) {
  273. newVal = _.round(newVal,2);
  274. if(text===null){
  275. newVal=null;
  276. }
  277. var query = {
  278. 'ID':recode.ID,
  279. 'projectID': recode.projectID,
  280. 'rationID':recode.rationID
  281. };
  282. var doc ={
  283. base_price:Number(recode.basePrice),
  284. market_price:newVal,
  285. code:recode.code,
  286. name:recode.name,
  287. project_id:recode.projectID
  288. };
  289. /* code: '01010101',
  290. market_price: '40',
  291. name: '水泥',
  292. project_id: projectId*/
  293. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  294. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  295. };
  296. return new ration_glj(project);
  297. }
  298. };