ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. var selected = projectObj.project.mainTree.selected;
  138. selected.data.adjustState=data.adjustState;
  139. projectObj.mainController.refreshTreeNode([selected]);
  140. };
  141. // CSL,2017.05.09
  142. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  143. this.project.beginUpdate('modifyQuantity');
  144. data.quantity = newQuantity;
  145. data.customQuantity = newQuantity;
  146. data.updateType = 'ut_update';
  147. this.project.push(this.getSourceType, data);
  148. this.project.endUpdate();
  149. };
  150. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  151. this.project.beginUpdate('modifyPrice');
  152. data.price = newPrice;
  153. data.updateType = 'ut_update';
  154. this.project.push(this.getSourceType, data);
  155. this.project.endUpdate();
  156. };
  157. ration_glj.prototype.deleteGLJ = function (data) {
  158. this.project.beginUpdate('deleteGLJ');
  159. data.customQuantity = 0;
  160. data.quantity = 0;
  161. data.rationItemQuantity = 0;
  162. data.updateType = 'ut_update';
  163. this.project.push(this.getSourceType, data);
  164. this.project.endUpdate();
  165. };
  166. ration_glj.prototype.addRationGLJ = function (newRation,data) {
  167. var souceTypeList=[];
  168. var criteriaDataList = [];
  169. if(data.hasOwnProperty('rationGljList')&&data.rationGljList.length>0){
  170. let criteria= {};
  171. criteria.ration_glj_list = [];
  172. for(let i=0;i<data.rationGljList.length;i++){
  173. let temdata = data.rationGljList[i];
  174. let newGLJ = {};
  175. newGLJ.projectID = newRation.projectID;
  176. newGLJ.GLJID = temdata.gljId;
  177. newGLJ.rationID = newRation.ID;
  178. newGLJ.billsItemID=newRation.billsItemID,
  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},{rationID:row.rationID});
  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. repositoryId:recode.repositoryId
  299. };
  300. var updateData = this.getUpdateData('ut_update',query,doc,'marketPriceAdjustUpdate');
  301. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData);
  302. };
  303. ration_glj.prototype.getGLJData = function(cb){
  304. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  305. cb(data);
  306. })
  307. };
  308. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  309. var gljList=[];
  310. var allGLJ=gljOprObj.AllRecode;
  311. GLJSelection.sort();
  312. _.forEach(GLJSelection,function (g) {
  313. var glj=_.find(allGLJ,{'code':g});
  314. var ration_glj ={
  315. projectID:ration.projectID,
  316. GLJID:glj.ID,
  317. rationID:ration.ID,
  318. billsItemID:ration.billsItemID,
  319. rationItemQuantity:0,
  320. quantity:0,
  321. name:glj.name,
  322. code:glj.code,
  323. unit:glj.unit,
  324. specs:glj.specs,
  325. basePrice:glj.basePrice,
  326. shortName:glj.shortName,
  327. type:glj.gljType,
  328. createType:'add',
  329. repositoryId:glj.repositoryId
  330. }
  331. if(glj.hasOwnProperty("compilationId")){
  332. ration_glj.from="cpt";
  333. }
  334. gljList.push(ration_glj);
  335. });
  336. CommonAjax.post("/rationGlj/addGLJ",gljList,callback);
  337. };
  338. ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
  339. var allGLJ=gljOprObj.AllRecode;
  340. var glj=_.find(allGLJ,{'code':selectCode});
  341. if(selectCode==oldData.code){
  342. return callback(null);
  343. }
  344. if(oldData.createType!='replace'){
  345. oldData.rcode=oldData.code;
  346. oldData.createType='replace';
  347. }
  348. oldData.GLJID=glj.ID;
  349. oldData.rationItemQuantity=0;
  350. oldData.name=glj.name;
  351. oldData.code=glj.code;
  352. oldData.unit=glj.unit;
  353. oldData.specs=glj.specs;
  354. oldData.basePrice=glj.basePrice;
  355. oldData.repositoryId=glj.repositoryId;
  356. if(glj.hasOwnProperty("compilationId")){
  357. oldData.from="cpt";
  358. }else {
  359. oldData.from="std";
  360. }
  361. CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback);
  362. };
  363. ration_glj.prototype.mReplaceGLJ=function (selectCode,oldData,callback) {
  364. var allGLJ=gljOprObj.AllRecode;
  365. var glj=_.find(allGLJ,{'code':selectCode});
  366. if(selectCode==oldData.code){
  367. return callback(null);
  368. }
  369. var query={
  370. projectID:oldData.projectID,
  371. code:oldData.code,
  372. name:oldData.name
  373. }
  374. var doc={
  375. GLJID:glj.ID,
  376. createType:'replace',
  377. rationItemQuantity:0,
  378. name:glj.name,
  379. code:glj.code,
  380. unit:glj.unit,
  381. specs:glj.specs,
  382. type:glj.gljType,
  383. basePrice:glj.basePrice,
  384. repositoryId:glj.repositoryId,
  385. projectID:oldData.projectID
  386. }
  387. if(oldData.createType=='replace'){
  388. doc.rcode=oldData.rcode;
  389. }else {
  390. doc.rcode=oldData.code;
  391. }
  392. if(glj.hasOwnProperty("compilationId")){
  393. doc.from="cpt";
  394. }else {
  395. doc.from="std";
  396. }
  397. CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback);
  398. };
  399. return new ration_glj(project);
  400. }
  401. };