ration_coe.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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,设备×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. return criteria;
  96. };
  97. ration_coe.prototype.getCustomerCoeData = function () {
  98. var coeList = [];
  99. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
  100. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
  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. return coeList;
  106. };
  107. ration_coe.prototype.getUpdateData=function(type,query,doc,callfunction){
  108. var updateData = [];
  109. var newobj = {
  110. 'updateType': type,
  111. 'query': query,
  112. }
  113. if(doc){
  114. newobj['doc']=doc;
  115. }
  116. if(callfunction){
  117. newobj['updateFunction']=callfunction;
  118. }
  119. updateData.push(newobj);
  120. return updateData;
  121. };
  122. ration_coe.prototype.adjustCoeClick = function(record,newval,ext){
  123. let me=this,codesList = [],add=[],replace=[];
  124. let doc = ext?ext:{};
  125. doc['isAdjust'] = newval;
  126. var updateData = {
  127. 'ID':record.ID,
  128. 'projectID': record.projectID,
  129. 'rationID':record.rationID,
  130. 'doc':doc,
  131. add:[],
  132. delete:[],
  133. replace:[]
  134. };
  135. let gljList = project.ration_glj.getGLJListByRationID(record.rationID);
  136. if(gljUtil.isDef(record.option_codes)&&record.option_codes!=""){ //说明编辑的是下拉选择编号的类型
  137. //勾选而不是下拉的情况下,默认选择下拉框中的第一个
  138. if(newval == 1 && (!gljUtil.isDef(doc.select_code)||doc.select_code == '')){
  139. if(record.option_list&&record.option_list.length > 0) doc.select_code = record.option_list[0].value;
  140. }
  141. this.prepareDataForOptionType(record,newval,gljList,updateData,replace,codesList,doc);
  142. }
  143. for(let coe of record.coes){//做单个、替换等检查
  144. if(coe.coeType == "单个工料机"){ //单个工料机的情况
  145. let glj = _.find(gljList,{"code":coe.gljCode});
  146. if(newval == 1){ //单个工料机,选中时,如果能找到则不管,如果没找到,则需自动插入
  147. if(glj) continue;
  148. add.push(coe.gljCode);
  149. codesList.push(coe.gljCode);
  150. }else {//取消勾选时,要删除添加的工料机记录
  151. if(glj && glj.createType == "add") updateData.delete.push(glj.ID);
  152. }
  153. }
  154. //替换工料机的情况
  155. if(coe.coeType == "替换人材机"){
  156. let glj = _.find(gljList,{"rcode":coe.gljCode,'code':coe.replaceCode});//两个条件都满足,则表示已找到对应的记录
  157. if(newval == 1){
  158. if(glj) continue;
  159. let r_glj = _.find(gljList,function (g) {
  160. return (g.code == coe.gljCode && (g.createType===undefined || g.createType == 'normal')) || g.rcode == coe.gljCode //这两个满足一个说明要替换
  161. });
  162. if(r_glj){
  163. replace.push({code:coe.replaceCode,oldData:r_glj});
  164. codesList.push(coe.replaceCode);
  165. }
  166. }
  167. if(newval == 0 && glj){//取消勾选并找到的情况,要恢复成原来的
  168. replace.push({code:coe.gljCode,oldData:glj});
  169. codesList.push(coe.gljCode);
  170. }
  171. }
  172. }
  173. project.ration_glj.getGLJDataByCodes(codesList,function (gljList) {//统一从后端按编号查找标准工料机信息
  174. let gljMap = _.indexBy(gljList, 'code');
  175. for(let a of add){
  176. if(gljMap[a]){
  177. let tem = project.ration_glj.getAddDataByStd(gljMap[a],record.rationID,project.mainTree.selected.data.billsItemID,record.projectID);
  178. updateData.add.push(tem);
  179. }
  180. }
  181. for(let r of replace){
  182. if(gljMap[r.code]){
  183. let r_tem = project.ration_glj.getReplaceDataByStd(r.oldData,gljMap[r.code]);
  184. updateData.replace.push(r_tem);
  185. }
  186. }
  187. $.bootstrapLoading.start();
  188. CommonAjax.post("/ration/updateCoeAdjust",updateData,function (result) {
  189. $.bootstrapLoading.end();
  190. me.refreshAfterUpdate(result.coe);
  191. zmhs_obj.refreshAfterUpdate(result);
  192. })
  193. });
  194. };
  195. ration_coe.prototype.prepareDataForOptionType = function (record,newval,gljList,updateData,replace,codesList,ext) {
  196. let select_code = record.select_code;
  197. if(ext && gljUtil.isDef(ext.select_code)&&ext.select_code!=""){//是下拉框选择了人材机编号进来的
  198. select_code = ext.select_code;
  199. }
  200. if(gljUtil.isDef(select_code) && select_code!=""){//有选中编号的情况下,才会做这些操作
  201. let glj = _.find(gljList,{"rcode":record.original_code,'code':select_code});//两个条件都满足,则表示已找到对应的记录
  202. if(newval == 1){
  203. if(!glj){//在没找到的情况下,要替换
  204. let r_glj = _.find(gljList,function (g) {
  205. return (g.code == record.original_code && (g.createType===undefined || g.createType == 'normal')) || g.rcode == record.original_code //这两个满足一个说明要替换
  206. });
  207. if(r_glj){
  208. replace.push({code:select_code,oldData:r_glj});
  209. codesList.push(select_code);
  210. }
  211. }
  212. }else {
  213. updateData.doc['select_code'] = ""; //取消勾选,选中值要恢复为空
  214. if(glj){//如果找到工料机,还需要恢复成原来的
  215. replace.push({code:record.original_code,oldData:glj});
  216. codesList.push(record.original_code);
  217. }
  218. }
  219. }
  220. };
  221. ration_coe.prototype.updateCustomerCoe = function (data) {
  222. var updateData = this.getUpdateData('ut_update',data.query,data.doc,'updateCustomerCoe');
  223. project.pushNow('updateCustomerCoe',[this.getSourceType()],updateData);
  224. };
  225. ration_coe.prototype.deleteByRation = function(ration){
  226. var coe_list = projectObj.project.ration_coe.datas;
  227. _.remove(coe_list,{'rationID':ration.ID});
  228. };
  229. return new ration_coe(project);
  230. }
  231. };