ration_glj.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. result = gljOprObj.combineWithProjectGlj(result);
  77. return result;
  78. }
  79. // 提交数据后返回数据处理
  80. ration_glj.prototype.doAfterUpdate = function(err, data){
  81. if(!err){
  82. if(data.updateTpye=='ut_update'){
  83. this.refreshAfterUpdate(data);
  84. }else if(data.updateTpye=='ut_delete'){
  85. this.refreshAfterDelete(data);
  86. } else {
  87. this.refreshAfterSave(data);
  88. }
  89. projectObj.project.projectGLJ.loadData();
  90. }
  91. };
  92. ration_glj.prototype.refreshAfterSave=function(data){
  93. let neRecodes=[];
  94. if(data){
  95. neRecodes=data.newRecords;
  96. gljOprObj.sheetData=data.showDatas;
  97. }
  98. if(projectObj.project.ration_glj.datas&&Array.isArray(projectObj.project.ration_glj.datas)){
  99. if(data){
  100. projectObj.project.ration_glj.datas = projectObj.project.ration_glj.datas.concat(neRecodes);
  101. }
  102. }else {
  103. projectObj.project.ration_glj.datas = neRecodes;
  104. }
  105. gljOprObj.showRationGLJSheetData(true);
  106. };
  107. ration_glj.prototype.refreshAfterUpdate=function(data){
  108. var me = this;
  109. if(data.quantityRefresh){
  110. data.glj_result.forEach(function (item) {
  111. me.refreshEachItme(item.doc,item.query);
  112. })
  113. }else {
  114. me.refreshEachItme(data.doc,data.query);
  115. }
  116. gljOprObj.showRationGLJSheetData(true);
  117. };
  118. ration_glj.prototype.refreshEachItme = function (doc,query) {
  119. var glj_list = projectObj.project.ration_glj.datas;
  120. var glj_index= _.findIndex(glj_list,(glj)=>{
  121. return glj.ID==query.ID;
  122. })
  123. var sheet_index= _.findIndex(gljOprObj.sheetData,(sd)=>{
  124. return sd.ID==query.ID;
  125. })
  126. _.forEach(doc, function(n, key) {
  127. glj_list[glj_index][key] = n;
  128. gljOprObj.sheetData[sheet_index][key]=n;
  129. });
  130. return glj_list[glj_index].rationID;
  131. };
  132. ration_glj.prototype.refreshAfterDelete=function(data){
  133. var glj_list = projectObj.project.ration_glj.datas;
  134. _.remove(glj_list,data.query);
  135. _.remove(gljOprObj.sheetData,data.query);
  136. gljOprObj.showRationGLJSheetData();
  137. projectObj.project.projectGLJ.loadData();
  138. var selected = projectObj.project.mainTree.selected;
  139. selected.data.adjustState=data.adjustState;
  140. projectObj.mainController.refreshTreeNode([selected]);
  141. };
  142. // CSL,2017.05.09
  143. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  144. this.project.beginUpdate('modifyQuantity');
  145. data.quantity = newQuantity;
  146. data.customQuantity = newQuantity;
  147. data.updateType = 'ut_update';
  148. this.project.push(this.getSourceType, data);
  149. this.project.endUpdate();
  150. };
  151. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  152. this.project.beginUpdate('modifyPrice');
  153. data.price = newPrice;
  154. data.updateType = 'ut_update';
  155. this.project.push(this.getSourceType, data);
  156. this.project.endUpdate();
  157. };
  158. ration_glj.prototype.deleteGLJ = function (data) {
  159. this.project.beginUpdate('deleteGLJ');
  160. data.customQuantity = 0;
  161. data.quantity = 0;
  162. data.rationItemQuantity = 0;
  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.billsItemID=newRation.billsItemID,
  180. newGLJ.rationItemQuantity= temdata.consumeAmt;
  181. newGLJ.quantity=temdata.consumeAmt;
  182. newGLJ.glj_repository_id=data.rationRepId;
  183. criteria.ration_glj_list.push(newGLJ);
  184. }
  185. criteria.updateType = 'ut_create';
  186. souceTypeList.push(this.getSourceType());
  187. criteriaDataList.push(criteria);
  188. }
  189. var ration_coe = projectObj.project.ration_coe;
  190. var rationCoeData = ration_coe.getRationCoedata(newRation,data);
  191. souceTypeList.push(ration_coe.getSourceType());
  192. criteriaDataList.push(rationCoeData);
  193. project.pushNow('addRationGLJAndRationCoe',souceTypeList,criteriaDataList);
  194. };
  195. ration_glj.prototype.getDeleteDataByRation=function(rationData){
  196. var updateData = [];
  197. updateData.push({'deleteType':'RATION','updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}});
  198. return updateData;
  199. };
  200. ration_glj.prototype.getDeleteDataByBills=function(datas){
  201. var updateData = [];
  202. datas.forEach(function (deleteData) {
  203. if(deleteData.type=='delete'){
  204. var billData = deleteData.data;
  205. updateData.push({'deleteType':'BILL','updateType': 'ut_delete', 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}});
  206. }
  207. })
  208. return updateData;
  209. };
  210. ration_glj.prototype.deleteByRation = function(ration){
  211. var glj_list = projectObj.project.ration_glj.datas;
  212. var newList =_.filter(glj_list,(glj)=>{
  213. return glj.rationID!=ration.ID;
  214. });
  215. if(newList!=undefined){
  216. projectObj.project.ration_glj.datas = newList;
  217. }
  218. };
  219. ration_glj.prototype.deleteByBills=function(deleteData){
  220. var rationList = projectObj.project.Ration.datas;
  221. var deleteRationList = [];
  222. for(var i=0;i<deleteData.length;i++){
  223. if(deleteData[i].type=='delete'){
  224. var billID = deleteData[i].data.ID;
  225. var raList =_.filter(rationList,(ration)=>{
  226. return ration.billsItemID==billID;
  227. });
  228. deleteRationList = deleteRationList.concat(raList);
  229. }
  230. }
  231. for(var i=0;i<deleteRationList.length;i++){
  232. this.deleteByRation(deleteRationList[i]);
  233. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  234. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  235. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  236. }
  237. }
  238. ration_glj.prototype.updataOrdelete=function(row){
  239. var updateData = null;
  240. if(row.rationItemQuantity==0){
  241. updateData=this.getUpdateData('ut_delete',{'ID': row.ID, 'projectID': row.projectID},{rationID:row.rationID});
  242. project.pushNow('updateRationGLJ',[this.getSourceType()],updateData)
  243. }else {
  244. this.customQuantityUpdate(row,0,0);//('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  245. }
  246. };
  247. ration_glj.prototype.getUpdateData=function(type,query,doc,callfunction){
  248. var updateData = [];
  249. var newobj = {
  250. 'updateType': type,
  251. 'query': query,
  252. }
  253. if(doc){
  254. newobj['doc']=doc;
  255. }
  256. if(callfunction){
  257. newobj['updateFunction']=callfunction;
  258. }
  259. updateData.push(newobj);
  260. return updateData;
  261. };
  262. ration_glj.prototype.updateRationGLJByEdit=function (recode,updateField,newval) {
  263. var me=this;
  264. $.bootstrapLoading.start();
  265. var callback=function (data) {
  266. if(updateField=='customQuantity'){
  267. me.refreshAfterQuantityUpdate(data);
  268. }else {
  269. var doc = data.doc;
  270. for(var key in doc){
  271. recode[key] = doc[key];
  272. }
  273. if(data.hasOwnProperty('adjustState')){//更新定额调整状态
  274. me.updateRationAdjustState(data.adjustState);
  275. }
  276. }
  277. gljOprObj.showRationGLJSheetData();
  278. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  279. $.bootstrapLoading.end();
  280. });
  281. }
  282. var query = {
  283. 'ID':recode.ID,
  284. 'projectID': recode.projectID,
  285. 'rationID':recode.rationID
  286. };
  287. var priceInfo={
  288. base_price: recode.basePrice,
  289. market_price: recode.marketPrice
  290. }
  291. var doc = {};
  292. doc[updateField]=newval;
  293. CommonAjax.post("/rationGlj/updateRationGLJByEdit",{query:query,doc:doc,priceInfo:priceInfo},callback,function (err) {
  294. $.bootstrapLoading.end();
  295. });
  296. }
  297. ration_glj.prototype.refreshAfterQuantityUpdate=function (data) {
  298. var me=this;
  299. data.glj_result.forEach(function (item) {
  300. me.refreshEachItme(item.doc,item.query);
  301. })
  302. me.updateRationAdjustState(data.adjustState);
  303. };
  304. ration_glj.prototype.updateRationAdjustState=function(adjustState){
  305. var selected = projectObj.project.mainTree.selected;
  306. selected.data.adjustState=adjustState;
  307. projectObj.mainController.refreshTreeNode([selected]);
  308. };
  309. ration_glj.prototype.getGLJData = function(cb){
  310. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  311. cb(data);
  312. })
  313. };
  314. ration_glj.prototype.addGLJByLib=function (GLJSelection,ration,callback) {
  315. var gljList=[];
  316. var allGLJ=gljOprObj.AllRecode;
  317. GLJSelection.sort();
  318. _.forEach(GLJSelection,function (g) {
  319. var glj=_.find(allGLJ,{'code':g});
  320. var ration_glj ={
  321. projectID:ration.projectID,
  322. GLJID:glj.ID,
  323. rationID:ration.ID,
  324. billsItemID:ration.billsItemID,
  325. rationItemQuantity:0,
  326. quantity:0,
  327. name:glj.name,
  328. code:glj.code,
  329. unit:glj.unit,
  330. specs:glj.specs,
  331. basePrice:glj.basePrice,
  332. shortName:glj.shortName,
  333. type:glj.gljType,
  334. createType:'add',
  335. repositoryId:glj.repositoryId
  336. }
  337. if(glj.hasOwnProperty("compilationId")){
  338. ration_glj.from="cpt";
  339. }
  340. gljList.push(ration_glj);
  341. });
  342. CommonAjax.post("/rationGlj/addGLJ",gljList,callback);
  343. };
  344. ration_glj.prototype.replaceGLJ=function (selectCode,oldData,callback) {
  345. var allGLJ=gljOprObj.AllRecode;
  346. var glj=_.find(allGLJ,{'code':selectCode});
  347. if(selectCode==oldData.code){
  348. return callback(null);
  349. }
  350. if(oldData.createType!='replace'){
  351. oldData.rcode=oldData.code;
  352. oldData.createType='replace';
  353. }
  354. oldData.GLJID=glj.ID;
  355. oldData.rationItemQuantity=0;
  356. oldData.name=glj.name;
  357. oldData.code=glj.code;
  358. oldData.unit=glj.unit;
  359. oldData.specs=glj.specs;
  360. oldData.basePrice=glj.basePrice;
  361. oldData.repositoryId=glj.repositoryId;
  362. if(glj.hasOwnProperty("compilationId")){
  363. oldData.from="cpt";
  364. }else {
  365. oldData.from="std";
  366. }
  367. CommonAjax.post("/rationGlj/replaceGLJ",oldData,callback);
  368. };
  369. ration_glj.prototype.mReplaceGLJ=function (selectCode,oldData,callback) {
  370. var allGLJ=gljOprObj.AllRecode;
  371. var glj=_.find(allGLJ,{'code':selectCode});
  372. if(selectCode==oldData.code){
  373. return callback(null);
  374. }
  375. var query={
  376. projectID:oldData.projectID,
  377. code:oldData.code,
  378. name:oldData.name
  379. }
  380. var doc={
  381. GLJID:glj.ID,
  382. createType:'replace',
  383. rationItemQuantity:0,
  384. name:glj.name,
  385. code:glj.code,
  386. unit:glj.unit,
  387. specs:glj.specs,
  388. type:glj.gljType,
  389. basePrice:glj.basePrice,
  390. repositoryId:glj.repositoryId,
  391. projectID:oldData.projectID
  392. }
  393. if(oldData.createType=='replace'){
  394. doc.rcode=oldData.rcode;
  395. }else {
  396. doc.rcode=oldData.code;
  397. }
  398. if(glj.hasOwnProperty("compilationId")){
  399. doc.from="cpt";
  400. }else {
  401. doc.from="std";
  402. }
  403. CommonAjax.post("/rationGlj/mReplaceGLJ",{query:query,doc:doc},callback);
  404. };
  405. return new ration_glj(project);
  406. }
  407. };