Ext.QuickTips.init(); Ext.define('erp.controller.pm.outsource.MakeMaterialScrap', { extend: 'Ext.app.Controller', requires: ['erp.util.RenderUtil', 'erp.util.GridUtil', 'erp.util.BaseUtil'], views:[ 'pm.outsource.MakeMaterialScrap', 'core.grid.Panel5', 'common.editorColumn.GridPanel', 'core.grid.YnColumn', 'core.button.CreateDetail', 'core.button.PrintDetail', 'core.trigger.DbfindTrigger' ], init:function(){ var me = this; me.GridUtil = Ext.create('erp.util.GridUtil'); me.BaseUtil = Ext.create('erp.util.BaseUtil'); this.control({ 'button[id=retreat]': { click: function(btn){ var grid = Ext.getCmp('editorColumnGridPanel'); var e = me.checkQty(Ext.getCmp('grid'), grid); if(e.length > 0) { showError(e);return; } e = me.check(grid.selModel.getSelection()); if(e.length > 0) { showError(e);return; } warnMsg("确定要生成报废单吗?", function(btn){ if(btn == 'yes'){ me.turnOut(grid); } }); } }, 'button[name=query]': { click: function(btn){ me.onQuery(); } }, 'erpEditorColumnGridPanel' : { afterlayout: function(grid) { grid.store.on('load', function(store){ me.getProductWh(grid); }); } }, 'checkbox[id=whcode]' : { afterrender : function(f) { me.getSetting(f); } } }); }, turnOut: function(grid) { var me = this, material = this.getEffectData(grid.selModel.getSelection()); if(material.length > 0){ grid.setLoading(true);//loading... Ext.Ajax.request({ url : basePath + 'pm/make/turnScrap.action', params: { data: Ext.encode(material), caller: caller, type: 'OS' }, method : 'post', callback : function(options,success,response){ grid.setLoading(false); var localJson = new Ext.decode(response.responseText); if(localJson.exceptionInfo){ showError(localJson.exceptionInfo); } if(localJson.log){ showMessage('提示', localJson.log); } if(localJson.success){ turnSuccess(function(){ grid.multiselected = new Array(); }); } me.onQuery(); } }); } }, /** * 筛选 */ onQuery: function(){ var grid = Ext.getCmp('grid'); //计算thisqty this.calOnlineQty(grid); //Query var condition = null; Ext.each(grid.store.data.items, function(item){ if(item.data['ma_code'] != null && item.data['ma_code'] != ''){ if(condition == null){ condition = "(mm_code='" + item.data['ma_code'] + "'"; } else { condition += " OR mm_code='" + item.data['ma_code'] + "'"; } } }); if(condition != null){ grid.busy = true; grid.multiselected = new Array(); var dg = Ext.getCmp('editorColumnGridPanel'); dg.busy = true; condition += ") AND (nvl(mm_materialstatus,' ')=' ') AND (mm_thisqty > 0)"; dg.getGridColumnsAndStore(condition + ' order by mm_maid,mm_detno'); this.showReplace(condition, dg); setTimeout(function(){ dg.busy = false; grid.busy = false; }, 1000); } }, /** * 更新工单用料在线结存数量 **/ calOnlineQty: function(grid){ var items = grid.store.data.items, idx = new Array(); Ext.each(items, function(item){ if(item.data['ma_code'] != null && item.data['ma_code'] != ''){ idx.push(item.data['ma_id']); } }); if(idx.length > 0) { Ext.Ajax.request({ url : basePath + 'pm/make/calOnlineQty.action', async: false, params: { ids: Ext.Array.concate(idx, ',') }, callback: function(opt, s, r){ var res = Ext.decode(r.responseText); if(res.exceptionInfo) { showError(res.exceptionInfo); } } }); } }, /** * 替代料 */ showReplace: function(condition, grid){ Ext.Ajax.request({ url : basePath + 'common/getFieldsDatas.action', params: { caller: 'MakeMaterialReplace left join MakeMaterial on mm_id=mp_mmid left join Product on mp_prodcode=pr_code' + ' left join WareHouse on wh_id=mp_warehouseid', fields: 'mp_mmid,mp_detno,mp_thisqty,mp_canuseqty,mp_repqty,mp_remark,mp_prodcode,pr_detail,pr_spec,pr_unit,wh_code', condition: condition + ' and (mp_thisqty > 0)' }, async: false, method : 'post', callback : function(options,success,response){ var localJson = new Ext.decode(response.responseText); if(localJson.exceptionInfo){ showError(localJson.exceptionInfo);return; } if(localJson.success){ var data = Ext.decode(localJson.data); if(data && data.length > 0) { var idx, store = grid.store, record; grid.lockrender = true; Ext.each(data, function(d){ idx = store.find('mm_id', d.MP_MMID); record = store.getAt(idx); if(idx != null && idx >= 0) { store.insert(idx + 1, { mm_prodcode: d.MP_PRODCODE, mm_oneuseqty: record.data.mm_oneuseqty, mm_code: record.data.mm_code, pr_detail: d.PR_DETAIL, pr_spec: d.PR_SPEC, pr_unit: d.PR_UNIT, mm_canuserepqty: d.MP_CANUSEQTY, mm_thisqty: d.MP_THISQTY, mm_totaluseqty: d.MP_REPQTY, mm_ifrep: 1, mm_remark: d.MP_REMARK, mm_whcode: d.WH_CODE, mm_detno: d.MP_DETNO, mm_id: d.MP_MMID, isrep: true }); } }); grid.lockrender = false; } grid.store.fireEvent('load', grid.store); } } }); }, /** * 转领料前,校验发料套数与领料数 */ checkQty: function(a, b){ var c = this.getMixedGroups(b.selModel.getSelection(), ['mm_code', 'mm_id']), code,count,q = 0,m = 0,err = ''; a.store.each(function(d){ code = d.get('ma_code'); if(!Ext.isEmpty(code)) { q = d.get('ma_thisqty'); Ext.Array.each(c, function(i) { if(i.keys.mm_code == code) { count = 0; m = 0; Ext.Array.each(i.groups, function(j){ if(m == 0) m = j.get('mm_oneuseqty'); count += j.get('mm_thisqty'); }); if(q * m < count) { err += '\n报废数超出报废套数,工单号[' + code + ']'; } } }); } }); return err; }, getMixedGroups: function(items, fields) { var data = new Object(),k,o; Ext.Array.each(items, function(d){ k = ''; o = new Object(); Ext.each(fields, function(f){ k += f + ':' + d.get(f) + ','; o[f] = d.get(f); }); if(k.length > 0) { if(!data[k]) { data[k] = {keys: o, groups: [d]}; } else { data[k].groups.push(d); } } }); return Ext.Object.getValues(data); }, check: function(items) { var e = ''; Ext.Array.each(items, function(item){ if(Ext.isEmpty(item.get('mm_whcode'))) { e += '\n工单[' + item.get('mm_code') + '],行号[' + item.get('mm_detno') + ']仓库为空'; } if(Ext.isEmpty(item.get('mm_thisqty'))) { e += '\n工单[' + item.get('mm_code') + '],行号[' + item.get('mm_detno') + ']领料数为空'; } }); return e; }, getEffectData: function(items) { var d = new Array(); Ext.Array.each(items, function(item){ d.push({ mm_detno: item.get('mm_detno'), mm_code: item.get('mm_code'), mm_id: item.get('isrep') == null ? item.get('mm_id') : -item.get('mm_id'), mm_thisqty: item.get('mm_thisqty'), mm_whcode: item.get('mm_whcode') }); }); return d; }, getProductWh: function(grid) { var codes = []; grid.store.each(function(d){ codes.push("'" + d.get('mm_prodcode') + "'"); }); Ext.Ajax.request({ url: basePath + 'scm/product/getProductwh.action', params: { codes: codes.join(',') }, callback: function (opt, s, r) { if(s) { var rs = Ext.decode(r.responseText); if(rs.data) { grid.productwh = rs.data; } } } }); }, getSetting : function(f) { Ext.Ajax.request({ url : basePath + 'common/getFieldData.action', async: false, params: { caller: 'Setting', field: 'se_value', condition: 'se_what=\'GroupWarehouse.OS\'' }, method : 'post', callback : function(opt, s, res){ var r = new Ext.decode(res.responseText); if(r.exceptionInfo){ showError(r.exceptionInfo);return; } if(r.success && r.data){ if (r.data == 'false') { f.setValue(false); } } } }); } });