ration_glj.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.replaceGLJ = function (data, newGLJID) {
  164. this.project.beginUpdate('replaceGLJ');
  165. data.GLJID = newGLJID;
  166. data.updateType = 'ut_update';
  167. this.project.push(this.getSourceType, data);
  168. this.project.endUpdate();
  169. };
  170. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  171. var souceTypeList=[];
  172. var criteriaDataList = [];
  173. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  174. let criteria= {};
  175. criteria.ration_glj_list = [];
  176. for(let i=0;i<data.rationGljList.length;i++){
  177. let temdata = data.rationGljList[i];
  178. let newGLJ = {};
  179. newGLJ.projectID = newRation.projectID;
  180. newGLJ.GLJID = temdata.gljId;
  181. newGLJ.rationID = newRation.ID;
  182. newGLJ.rationItemQuantity= temdata.consumeAmt;
  183. newGLJ.quantity=temdata.consumeAmt;
  184. newGLJ.glj_repository_id=data.rationRepId;
  185. criteria.ration_glj_list.push(newGLJ);
  186. }
  187. criteria.updateType = 'ut_create';
  188. souceTypeList.push(this.getSourceType());
  189. criteriaDataList.push(criteria);
  190. }
  191. var ration_coe = projectObj.project.ration_coe;
  192. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  193. souceTypeList.push(ration_coe.getSourceType());
  194. criteriaDataList.push(rationCoeData);
  195. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  196. };
  197. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  198. var updateData = [];
  199. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  200. return updateData;
  201. };
  202. ration_glj.prototype.getDeleteDataByBills=function(datas){
  203. var updateData = [];
  204. datas.forEach(function (deleteData) {
  205. if(deleteData.type=='delete'){
  206. var billData = deleteData.data;
  207. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  208. }
  209. })
  210. return updateData;
  211. };
  212. ration_glj.prototype.deleteByRation = function(ration){
  213. var glj_list = projectObj.project.ration_glj.datas;
  214. var newList =_.filter(glj_list,(glj)=>{
  215. return glj.rationID!=ration.ID;
  216. });
  217. if(newList!=undefined){
  218. projectObj.project.ration_glj.datas = newList;
  219. }
  220. };
  221. ration_glj.prototype.deleteByBills=function(deleteData){
  222. var rationList = projectObj.project.Ration.datas;
  223. var deleteRationList = [];
  224. for(var i=0;i<deleteData.length;i++){
  225. if(deleteData[i].type=='delete'){
  226. var billID = deleteData[i].data.ID;
  227. var raList =_.filter(rationList,(ration)=>{
  228. return ration.billsItemID==billID;
  229. });
  230. deleteRationList = deleteRationList.concat(raList);
  231. }
  232. }
  233. for(var i=0;i<deleteRationList.length;i++){
  234. this.deleteByRation(deleteRationList[i]);
  235. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  236. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  237. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  238. }
  239. }
  240. ration_glj.prototype.updataOrdelete=function(row){
  241. var updateData = null;
  242. if(row.rationItemQuantity==0){
  243. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID});
  244. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  245. }else {
  246. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  247. }
  248. };
  249. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  250. var updateData = [];
  251. var newobj = {
  252. 'updateType': type,
  253. 'query': query,
  254. }
  255. if(doc){
  256. newobj['doc']=doc;
  257. }
  258. if(callfunction){
  259. newobj['updateFunction']=callfunction;
  260. }
  261. updateData.push(newobj);
  262. return updateData;
  263. };
  264. ration_glj.prototype.customQuantityUpdate = function(recode,newVal,text){
  265. newVal = _.round(newVal,3);
  266. var query = {
  267. 'ID':recode.ID,
  268. 'projectID': recode.projectID,
  269. 'rationID':recode.rationID
  270. };
  271. var doc;
  272. if(text===null){
  273. doc ={
  274. customQuantity:null
  275. };
  276. }else {
  277. doc ={
  278. customQuantity:newVal,
  279. quantity:newVal
  280. };
  281. }
  282. var updateData = this.getUpdateData('ut_update',query,doc,'customQuantityUpdate');
  283. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  284. };
  285. ration_glj.prototype.marketPriceAdjustUpdate = function (recode,newVal,text) {
  286. newVal = _.round(newVal,2);
  287. if(text===null){
  288. newVal=null;
  289. }
  290. var query = {
  291. 'ID':recode.ID,
  292. 'projectID': recode.projectID,
  293. 'rationID':recode.rationID
  294. };
  295. var doc ={
  296. base_price:Number(recode.basePrice),
  297. market_price:newVal,
  298. code:recode.code,
  299. name:recode.name,
  300. project_id:recode.projectID,
  301. repositoryId:recode.repositoryId
  302. };
  303. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  304. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  305. };
  306. ration_glj.prototype.getGLJData = function(cb){
  307. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  308. cb(data);
  309. })
  310. };
  311. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  312. var gljList=[];
  313. var allGLJ=gljOprObj.stdGLJ.concat(gljOprObj.complementaryGLJs);
  314. GLJSelection.sort();
  315. _.forEach(GLJSelection,function (g) {
  316. var glj=_.find(allGLJ,{'code':g});
  317. var ration_glj ={
  318. projectID:ration.projectID,
  319. GLJID:glj.ID,
  320. rationID:ration.ID,
  321. rationItemQuantity:0,
  322. quantity:0,
  323. name:glj.name,
  324. code:glj.code,
  325. unit:glj.unit,
  326. specs:glj.specs,
  327. basePrice:glj.basePrice,
  328. shortName:glj.shortName,
  329. type:glj.gljType,
  330. createType:'add',
  331. repositoryId:glj.repositoryId
  332. }
  333. if(glj.hasOwnProperty("compilationId")){
  334. ration_glj.from="cpt";
  335. }
  336. gljList.push(ration_glj);
  337. });
  338. CommonAjax.post("/rationGlj/addGLJ",gljList,callback);
  339. };
  340. return new ration_glj(project);
  341. }
  342. };