ration_glj.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. var billData = deleteData.data;
  193. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  194. })
  195. return updateData;
  196. };
  197. ration_glj.prototype.deleteByRation = function(ration){
  198. var glj_list = projectObj.project.ration_glj.datas;
  199. var newList =_.filter(glj_list,(glj)=>{
  200. return glj.rationID!=ration.ID;
  201. });
  202. if(newList!=undefined){
  203. projectObj.project.ration_glj.datas = newList;
  204. }
  205. };
  206. ration_glj.prototype.deleteByBills=function(deleteData){
  207. var rationList = projectObj.project.Ration.datas;
  208. var deleteRationList = [];
  209. for(var i=0;i<deleteData.length;i++){
  210. var billID = deleteData[i].data.ID;
  211. var raList =_.filter(rationList,(ration)=>{
  212. return ration.billsItemID==billID;
  213. });
  214. deleteRationList = deleteRationList.concat(raList);
  215. }
  216. for(var i=0;i<deleteRationList.length;i++){
  217. this.deleteByRation(deleteRationList[i]);
  218. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  219. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  220. }
  221. }
  222. ration_glj.prototype.updataOrdelete=function(row){
  223. var updateData = null;
  224. if(row.rationItemQuantity==0){
  225. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID});
  226. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  227. }else {
  228. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  229. }
  230. };
  231. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  232. var updateData = [];
  233. var newobj = {
  234. 'updateType': type,
  235. 'query': query,
  236. }
  237. if(doc){
  238. newobj['doc']=doc;
  239. }
  240. if(callfunction){
  241. newobj['updateFunction']=callfunction;
  242. }
  243. updateData.push(newobj);
  244. return updateData;
  245. };
  246. ration_glj.prototype.customQuantityUpdate = function(recode,newVal,text){
  247. newVal = _.round(newVal,3);
  248. var query = {
  249. 'ID':recode.ID,
  250. 'projectID': recode.projectID,
  251. 'rationID':recode.rationID
  252. };
  253. var doc;
  254. if(text===null){
  255. doc ={
  256. customQuantity:null
  257. };
  258. }else {
  259. doc ={
  260. customQuantity:newVal,
  261. quantity:newVal
  262. };
  263. }
  264. var updateData = this.getUpdateData('ut_update',query,doc,'customQuantityUpdate');
  265. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  266. };
  267. ration_glj.prototype.marketPriceAdjustUpdate = function (recode,newVal,text) {
  268. newVal = _.round(newVal,2);
  269. if(text===null){
  270. newVal=null;
  271. }
  272. var query = {
  273. 'ID':recode.ID,
  274. 'projectID': recode.projectID,
  275. 'rationID':recode.rationID
  276. };
  277. var doc ={
  278. base_price:Number(recode.basePrice),
  279. market_price:newVal,
  280. code:recode.code,
  281. name:recode.name,
  282. project_id:recode.projectID
  283. };
  284. /* code: '01010101',
  285. market_price: '40',
  286. name: '水泥',
  287. project_id: projectId*/
  288. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  289. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  290. };
  291. return new ration_glj(project);
  292. }
  293. };