ration_glj.js 13 KB

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