ration_coe.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var ration_coe = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. var ration_coe = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. var sourceType = ModuleNames.ration_coe;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration_coe, this);
  18. };
  19. // prototype用于定义public方法
  20. ration_coe.prototype.loadData = function (datas) {
  21. this.datas = datas;
  22. };
  23. // 提交数据后返回数据处理
  24. ration_coe.prototype.doAfterUpdate = function(err, data){
  25. if(!err){
  26. if(data.updateTpye=='ut_update'){
  27. this.refreshAfterUpdate(data);
  28. }else if(data.updateTpye=='ut_delete'){
  29. this.refreshAfterDelete(data);
  30. } else {
  31. this.refreshAfterSave(data);
  32. }
  33. }
  34. };
  35. ration_coe.prototype.refreshAfterSave=function(data){
  36. projectObj.project.ration_coe.addDatasToList(data);
  37. zmhs_obj.showDatas();
  38. };
  39. ration_coe.prototype.addDatasToList = function (datas) {
  40. let me = projectObj.project.ration_coe;
  41. if(datas&&datas.length>0){
  42. if (me.datas && Array.isArray(me.datas)) {
  43. me.datas = me.datas.concat(datas);
  44. } else {
  45. me.datas = datas;
  46. }
  47. }
  48. };
  49. ration_coe.prototype.getCoeByRationID = function (rationID) {
  50. let coeList = _.filter(this.datas, {'rationID': rationID});
  51. return coeList;
  52. };
  53. ration_coe.prototype.refreshAfterUpdate=function(data){
  54. var coe_list = projectObj.project.ration_coe.datas;
  55. var coe_index= _.findIndex(coe_list,function(coe){
  56. return coe.ID==data.query.ID&&coe.projectID==data.query.projectID;
  57. })
  58. _.forEach(data.doc, function(n, key) {
  59. coe_list[coe_index][key] = n;
  60. });
  61. $('#coeSpread').is(':visible'),
  62. zmhs_obj.showDatas();
  63. };
  64. ration_coe.prototype.refreshAfterDelete=function(data){
  65. var glj_list = projectObj.project.ration_coe.datas;
  66. _.remove(glj_list,data.query);
  67. _.remove(gljOprObj.sheetData,data.query);
  68. zmhs_obj.showDatas();
  69. };
  70. ration_coe.prototype.getRationCoedata=function(newRation,data){
  71. var criteria= {};
  72. criteria.ration_coe_list = [];
  73. var dataLength = 0;
  74. if(data.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
  75. dataLength = data.rationCoeList.length
  76. }
  77. for(var i=0;i<=dataLength;i++){;
  78. var newCoe = {
  79. libID :data.rationRepId,
  80. rationID:newRation.ID,
  81. projectID : newRation.projectID
  82. }
  83. if(i==dataLength){
  84. newCoe.coeID=-1;
  85. newCoe.name = '自定义系数';
  86. newCoe.content='人工×1,材料×1,机械×1,设备×1';
  87. newCoe.isAdjust=0;
  88. newCoe.coes = this.getCustomerCoeData();
  89. }else {
  90. newCoe.coeID= data.rationCoeList[i].ID;
  91. }
  92. criteria.ration_coe_list.push(newCoe);
  93. }
  94. criteria.updateType = 'ut_create';
  95. console.log('enter');
  96. console.log(criteria);
  97. return criteria;
  98. };
  99. ration_coe.prototype.getCustomerCoeData = function () {
  100. var coeList = [];
  101. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
  102. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
  103. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'材料'});
  104. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'机械'});
  105. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'设备'});
  106. return coeList;
  107. };
  108. ration_coe.prototype.getUpdateData=function(type,query,doc,callfunction){
  109. var updateData = [];
  110. var newobj = {
  111. 'updateType': type,
  112. 'query': query,
  113. }
  114. if(doc){
  115. newobj['doc']=doc;
  116. }
  117. if(callfunction){
  118. newobj['updateFunction']=callfunction;
  119. }
  120. updateData.push(newobj);
  121. return updateData;
  122. };
  123. ration_coe.prototype.adjustCoeClick = function(record,newval,ext){
  124. let me=this,codesList = [],add=[],replace=[];
  125. let doc = ext?ext:{};
  126. doc['isAdjust'] = newval;
  127. var updateData = {
  128. 'ID':record.ID,
  129. 'projectID': record.projectID,
  130. 'rationID':record.rationID,
  131. 'doc':doc,
  132. add:[],
  133. delete:[],
  134. replace:[]
  135. };
  136. let gljList = project.ration_glj.getGLJListByRationID(record.rationID);
  137. if(gljUtil.isDef(record.option_codes)&&record.option_codes!=""){ //说明编辑的是下拉选择编号的类型
  138. //勾选而不是下拉的情况下,默认选择下拉框中的第一个
  139. if(newval == 1 && (!gljUtil.isDef(doc.select_code)||doc.select_code == '')){
  140. if(record.option_list&&record.option_list.length > 0) doc.select_code = record.option_list[0].value;
  141. }
  142. this.prepareDataForOptionType(record,newval,gljList,updateData,replace,codesList,ext);
  143. }
  144. for(let coe of record.coes){//做单个、替换等检查
  145. if(coe.coeType == "单个工料机"){ //单个工料机的情况
  146. let glj = _.find(gljList,{"code":coe.gljCode});
  147. if(newval == 1){ //单个工料机,选中时,如果能找到则不管,如果没找到,则需自动插入
  148. if(glj) continue;
  149. add.push(coe.gljCode);
  150. codesList.push(coe.gljCode);
  151. }else {//取消勾选时,要删除添加的工料机记录
  152. if(glj && glj.createType == "add") updateData.delete.push(glj.ID);
  153. }
  154. }
  155. //替换工料机的情况
  156. if(coe.coeType == "替换人材机"){
  157. let glj = _.find(gljList,{"rcode":coe.gljCode,'code':coe.replaceCode});//两个条件都满足,则表示已找到对应的记录
  158. if(newval == 1){
  159. if(glj) continue;
  160. let r_glj = _.find(gljList,function (g) {
  161. return (g.code == coe.gljCode && (g.createType===undefined || g.createType == 'normal')) || g.rcode == coe.gljCode //这两个满足一个说明要替换
  162. });
  163. if(r_glj){
  164. replace.push({code:coe.replaceCode,oldData:r_glj});
  165. codesList.push(coe.replaceCode);
  166. }
  167. }
  168. if(newval == 0 && glj){//取消勾选并找到的情况,要恢复成原来的
  169. replace.push({code:coe.gljCode,oldData:glj});
  170. codesList.push(coe.gljCode);
  171. }
  172. }
  173. }
  174. project.ration_glj.getGLJDataByCodes(codesList,function (gljList) {//统一从后端按编号查找标准工料机信息
  175. let gljMap = _.indexBy(gljList, 'code');
  176. for(let a of add){
  177. if(gljMap[a]){
  178. let tem = project.ration_glj.getAddDataByStd(gljMap[a],record.rationID,project.mainTree.selected.data.billsItemID,record.projectID);
  179. updateData.add.push(tem);
  180. }
  181. }
  182. for(let r of replace){
  183. if(gljMap[r.code]){
  184. let r_tem = project.ration_glj.getReplaceDataByStd(r.oldData,gljMap[r.code]);
  185. updateData.replace.push(r_tem);
  186. }
  187. }
  188. $.bootstrapLoading.start();
  189. CommonAjax.post("/ration/updateCoeAdjust",updateData,function (result) {
  190. $.bootstrapLoading.end();
  191. me.refreshAfterUpdate(result.coe);
  192. zmhs_obj.refreshAfterUpdate(result,true)
  193. })
  194. });
  195. };
  196. ration_coe.prototype.prepareDataForOptionType = function (record,newval,gljList,updateData,replace,codesList,ext) {
  197. let select_code = record.select_code;
  198. if(ext && gljUtil.isDef(ext.select_code)&&ext.select_code!=""){//是下拉框选择了人材机编号进来的
  199. select_code = ext.select_code;
  200. }
  201. if(gljUtil.isDef(select_code) && select_code!=""){//有选中编号的情况下,才会做这些操作
  202. let glj = _.find(gljList,{"rcode":record.original_code,'code':select_code});//两个条件都满足,则表示已找到对应的记录
  203. if(newval == 1){
  204. if(!glj){//在没找到的情况下,要替换
  205. let r_glj = _.find(gljList,function (g) {
  206. return (g.code == record.original_code && (g.createType===undefined || g.createType == 'normal')) || g.rcode == record.original_code //这两个满足一个说明要替换
  207. });
  208. if(r_glj){
  209. replace.push({code:select_code,oldData:r_glj});
  210. codesList.push(select_code);
  211. }
  212. }
  213. }else {
  214. updateData.doc['select_code'] = ""; //取消勾选,选中值要恢复为空
  215. if(glj){//如果找到工料机,还需要恢复成原来的
  216. replace.push({code:record.original_code,oldData:glj});
  217. codesList.push(record.original_code);
  218. }
  219. }
  220. }
  221. };
  222. ration_coe.prototype.updateCustomerCoe = function (data) {
  223. var updateData = this.getUpdateData('ut_update',data.query,data.doc,'updateCustomerCoe');
  224. project.pushNow('updateCustomerCoe',[this.getSourceType()],updateData);
  225. };
  226. ration_coe.prototype.deleteByRation = function(ration){
  227. var coe_list = projectObj.project.ration_coe.datas;
  228. _.remove(coe_list,{'rationID':ration.ID});
  229. };
  230. return new ration_coe(project);
  231. }
  232. };