Ext.QuickTips.init(); Ext.define('erp.controller.fa.ars.PreRec', { extend: 'Ext.app.Controller', FormUtil: Ext.create('erp.util.FormUtil'), GridUtil: Ext.create('erp.util.GridUtil'), BaseUtil: Ext.create('erp.util.BaseUtil'), views:[ 'core.form.Panel','fa.ars.PreRec','core.grid.Panel2','core.toolbar.Toolbar', 'core.form.SeparNumber', 'core.button.Save','core.button.Add','core.button.Close','core.button.Delete','core.button.Submit','core.button.ResAudit', 'core.button.Audit','core.button.ResSubmit','core.button.Post','core.button.ResPost','core.button.Update','core.button.DeleteDetail', 'core.trigger.DbfindTrigger','core.trigger.TextAreaTrigger','core.form.YnField','core.button.ResAccounted','core.button.AssMain','core.button.Accounted','core.button.StrikeBalance', 'core.trigger.CateTreeDbfindTrigger','core.trigger.HrOrgTreeDbfindTrigger','core.button.Print','core.button.AssDetail','core.button.GetSumAmount' ], init:function(){ var me = this; me.gridLastSelected = null; this.control({ 'erpGridPanel2': { itemclick:function(selModel, record){ me.onGridItemClick(selModel, record); var btn = Ext.getCmp('assdetail'); var ass = record.data['ca_asstype']; if(!Ext.isEmpty(ass)){ btn.setDisabled(false); } else { btn.setDisabled(true); } } }, 'erpGetSumAmountButton':{ click: function(btn){ var grid = Ext.getCmp("grid"),items = grid.store.data.items; var detailamount1 = 0; Ext.each(items,function(item,index){ if(!me.GridUtil.isBlank(grid,item.data)) { detailamount1 = detailamount1 + Number(item.data['prd_nowbalance']); } }); Ext.getCmp("pr_amount").setValue(detailamount1); Ext.getCmp("pr_cmamount").setValue(detailamount1); } }, 'erpAssMainButton':{ afterrender:function(btn){ if(Ext.getCmp('ca_asstype').getValue()==null||Ext.getCmp('ca_asstype').getValue()==""){ btn.setDisabled(true); }else{ btn.setDisabled(false); } } }, 'erpSaveButton': { click: function(btn){ var form = me.getForm(btn); if(Ext.getCmp(form.codeField).value == null || Ext.getCmp(form.codeField).value == ''){ /*Ext.getCmp(form.codeField).setValue(me.BaseUtil.getRandomNumber());*///自动添加编号 me.BaseUtil.getRandomNumber(); } me.getJsamount(); //保存之前的一些前台的逻辑判定 this.beforeSaveAccountRegister(); } }, 'erpDeleteDetailButton': { afterrender: function(btn){ //辅助核算 btn.ownerCt.add({ xtype:'erpAssDetailButton', disabled:true }); } }, 'erpDeleteButton' : { click: function(btn){ me.FormUtil.onDelete(Ext.getCmp('pr_id').value); } }, 'erpPostButton' : { afterrender: function(btn){ var status = Ext.getCmp(me.getForm(btn).statusCode); if(status && status.value == 'UNPOST'){ btn.show(); }else{ btn.hide(); } }, click: function(btn){ var amount = Number(Ext.getCmp('pr_amount').getValue()); var cmamount =Number(Ext.getCmp('pr_cmamount').getValue()); var cmcurrency = Ext.getCmp('pr_cmcurrency').getValue(); var cmrate = Number(Ext.getCmp('pr_cmrate').getValue()); var currency = Ext.getCmp('pr_currency').getValue(); if(amount != 0 && cmamount != 0){ if(Ext.Number.toFixed(cmrate, 8)!= Ext.Number.toFixed(cmamount/amount, 8)){ showError('冲账汇率不正确,不能过账!'); return; } } if(currency == cmcurrency){ if(cmrate != 1){ showError('币别相同,冲账汇率不等于1,不能过账!'); return; } } if(currency != cmcurrency){ if(cmrate == 1){ showError('币别不相同,冲账汇率等于1,不能过账!'); return; } } me.FormUtil.onPost(Ext.getCmp('pr_id').value); } }, 'erpResPostButton' : { afterrender: function(btn){ var status = Ext.getCmp(me.getForm(btn).statusCode); if(status && status.value == 'POSTED'){ btn.show(); }else{ btn.hide(); } }, click: function(btn){ me.FormUtil.onResPost(Ext.getCmp('pr_id').value); } }, 'erpUpdateButton': { click: function(btn){ me.getJsamount(); me.beforeUpdate(); } }, 'erpAddButton': { click: function(){ var form = Ext.getCmp('form'); var title = '新增'; if(form){ if(form.title){ title = title+form.title; } } me.FormUtil.onAdd('addPreRec', title, 'jsps/fa/ars/preRec.jsp?whoami='+caller); } }, 'erpCloseButton': { click: function(btn){ me.FormUtil.beforeClose(me); } }, 'erpSubmitButton': { afterrender: function(btn){ var auditStatus = Ext.getCmp(me.getForm(btn).auditStatusCode); if(auditStatus && auditStatus.value != 'ENTERING'){ btn.hide(); } }, click: function(btn){ var amount = Number(Ext.getCmp('pr_amount').getValue()); var cmamount =Number(Ext.getCmp('pr_cmamount').getValue()); var cmcurrency = Ext.getCmp('pr_cmcurrency').getValue(); var cmrate = Number(Ext.getCmp('pr_cmrate').getValue()); var currency = Ext.getCmp('pr_currency').getValue(); if(amount != 0 && cmamount != 0){ if(Ext.Number.toFixed(cmrate, 8)!= Ext.Number.toFixed(cmamount/amount, 8)){ showError('冲账汇率不正确,不能提交!'); return; } } if(currency == cmcurrency){ if(cmrate != 1){ showError('币别相同,冲账汇率不等于1,不能提交!'); return; } } if(currency != cmcurrency){ if(cmrate == 1){ showError('币别不相同,冲账汇率等于1,不能提交!'); return; } } me.onSubmit(Ext.getCmp('pr_id').value); } }, 'erpResSubmitButton': { afterrender: function(btn){ var auditStatus = Ext.getCmp(me.getForm(btn).auditStatusCode); if(auditStatus && auditStatus.value != 'COMMITED'){ btn.hide(); } }, click: function(btn){ me.FormUtil.onResSubmit(Ext.getCmp('pr_id').value); } }, 'erpAuditButton': { afterrender: function(btn){ var auditStatus = Ext.getCmp(me.getForm(btn).auditStatusCode); if(auditStatus && auditStatus.value != 'COMMITED'){ btn.hide(); } }, click: function(btn){ me.FormUtil.onAudit(Ext.getCmp('pr_id').value); } }, 'erpResAuditButton': { afterrender: function(btn){ var auditStatus = Ext.getCmp(me.getForm(btn).auditStatusCode); if(auditStatus && auditStatus.value != 'AUDITED'){ btn.hide(); } }, click: function(btn){ me.FormUtil.onResAudit(Ext.getCmp('pr_id').value); } }, 'erpPrintButton': { click: function(btn){ me.FormUtil.onPrint(Ext.getCmp('pr_id').value); } }, 'textfield[name=pr_amount]':{ change: me.changeCmamount }, 'textfield[name=pr_cmrate]':{ change: me.changeCmamount }, 'cateTreeDbfindTrigger[name=pr_accountcode]':{ aftertrigger:function(trigger, data){ Ext.getCmp('pr_accountname').setValue(data[0].raw.data.ca_name); var asstype = data[0].raw.data.ca_asstype; var btn = Ext.getCmp("assmainbutton"); if(Ext.isEmpty(asstype)){ btn.setDisabled(true); }else{ btn.setDisabled(false); } } }, 'field[name=prd_ordercode]': { aftertrigger: function(){ var grid = Ext.getCmp('grid'); var record = grid.selModel.lastSelected; var data = record.data['prd_orderamount']-record.data['sa_prepayamount']; record.set('prd_nowbalance', data); }, afterrender: function(t){ t.gridKey="pr_custcode"; t.mappinggirdKey="sa_custcode"; t.gridErrorMessage="请先选择客户"; } }, 'textfield[name=pr_amount]':{ change: function(){ me.changeCmrateOrCmamount(); } }, 'textfield[name=pr_cmrate]':{ change: function(){ me.changeCmrateOrCmamount(); } }, 'textfield[name=pr_cmamount]':{ change: function(){ me.changeCmrateOrCmamount(); } }/*, 'dbfindtrigger[name=pr_custcode]':{ //pr_beginlast beforetrigger:function(t){ if(caller =='PreRec!Ars!DEPR'){ var dateField = Ext.getCmp('pr_date'); var dateStr = dateField.rawValue; // 2013-03-19 var s=dateStr.split('-'); var config =''; //得到的月份其次 格式为 201303 Ext.each(s,function(i,index){ config=config+i+''; }); if(config.length>6){ config=config.substring(0,6); } // t.findConfig = " cu_code in (select distinct cm_custcode from custmonth where cm_yearmonth<='"+config+"' and cm_beginlast >0)"; t.findConfig = " ca_prepayamount>0 and cu_auditstatuscode='AUDITED' group by cu_id,cu_code,cu_name,cu_sellerid,cu_sellername "; } } }*/ }); }, changeCmrateOrCmamount: function () { var me = this; if(Ext.getCmp('pr_cmrate').readOnly){ me.changeCmrate(); }else{ me.changeCmamount(); } }, changeCmrate:function () { if(Ext.getCmp('pr_cmamount')&&Ext.getCmp('pr_amount')&&Ext.getCmp('pr_cmrate')){ var cmamount = Ext.getCmp('pr_cmamount').getValue();; var amount = Ext.getCmp('pr_amount').getValue(); var rate = Number(cmamount/amount); rate = Ext.Number.toFixed(rate,8); Ext.getCmp('pr_cmrate').setValue(rate); } }, //计算冲账金额 = 预收金额*冲账汇率 changeCmamount:function(){ if(Ext.getCmp('pr_amount')&&Ext.getCmp('pr_cmrate')){ var pramount = Ext.getCmp('pr_amount').getValue(); var prcmrate = Ext.getCmp('pr_cmrate').getValue(); if(!Ext.isNumber(pramount)){ pramount = Ext.Number.from(pramount,0); } if(!Ext.isNumber(prcmrate)){ prcmrate = Ext.Number.from(prcmrate,0); } var cmamount = pramount*prcmrate; Ext.getCmp('pr_cmamount').setValue(Ext.util.Format.number(cmamount,'0.00')); } }, onGridItemClick: function(selModel, record){//grid行选择 this.gridLastSelected = record; var grid = Ext.getCmp('grid'); if(record.data[grid.necessaryField] == null || record.data[grid.necessaryField] == ''){ this.gridLastSelected.findable = true;//空数据可以在输入完code,并移开光标后,自动调出该条数据 } else { this.gridLastSelected.findable = false; } this.GridUtil.onGridItemClick(selModel, record); }, getForm: function(btn){ return btn.ownerCt.ownerCt; }, beforeSaveAccountRegister: function(){ var me = this; var grid = Ext.getCmp('grid'); var items = grid.store.data.items; var bool = true; var warnStr=''; Ext.each(items,function(item,index){ if(item.dirty && item.data[grid.necessaryField] != null && item.data[grid.necessaryField] != ''){ var orderamount =item.data['prd_orderamount']; //订单金额 var havebalance = item.data['sa_prepayamount']; //已预收金额prd_havebalance var nowbalance = item.data['prd_nowbalance']; //本次预收额 //订单金额>=已预收金额+本次预收额 //此种情况不能进行保存 if(havebalance+nowbalance>orderamount){ var i = index+1; if(warnStr ==''||warnStr.length<=0){ warnStr = warnStr +i; }else{ warnStr = warnStr +','+i; } bool = false; } } }); if(bool){ me.beforeSave(this); }else{ if(warnStr != ''||warnStr.length>0){ showError('明细表第'+warnStr+'行 已预收金额与本次预收额的合计超过订单金额,不能保存');return; } } }, getJsamount: function(){ var grid = Ext.getCmp('grid'); var items = grid.store.data.items; var jsamount = 0; var detailcount = 0; var cateamount = 0; Ext.each(items,function(item,index){ if(item.data['prd_catecode']!=null&&item.data['prd_catecode']!=""){ cateamount = cateamount + Number(item.data['prd_nowbalance']); } }); Ext.getCmp('pr_jsamount').setValue(Ext.Number.from(Ext.getCmp('pr_cmamount').getValue())-cateamount); }, beforeSave:function(){ var me = this; var form = Ext.getCmp('form'); if(! me.FormUtil.checkForm()){ return; } if(Ext.getCmp(form.keyField).value == null || Ext.getCmp(form.keyField).value == ''){ me.FormUtil.getSeqId(form); } var detail = Ext.getCmp('grid'); var param2 = new Array(); var param3 = new Array(); if(Ext.getCmp('assdetail')){ Ext.each(Ext.Object.getKeys(Ext.getCmp('assdetail').cacheStoreGrid), function(key){ Ext.each(Ext.getCmp('assdetail').cacheStoreGrid[key], function(d){ d['dass_condid'] = key; param2.push(d); }); }); } if(Ext.getCmp('assmainbutton')){ Ext.each(Ext.Object.getKeys(Ext.getCmp('assmainbutton').cacheStoreForm), function(key){ Ext.each(Ext.getCmp('assmainbutton').cacheStoreForm[key], function(d){ d['ass_conid'] = key; param3.push(d); }); }); } Ext.each(detail.store.data.items, function(item){ if(item.data.prd_id == null || item.data.prd_id == 0){ item.data.prd_id = -item.index; } }); var param1 = me.GridUtil.getGridStore(detail); if(detail.necessaryField.length > 0 && (param1.length == 0)){ showError($I18N.common.grid.emptyDetail); return; } me.onSave(form, param1, param2,param3); }, onSave: function(form, param1, param2,param3) { var me = this; param1 = param1 == null ? [] : "[" + param1.toString().replace(/\\/g,"%") + "]"; param2 = param2 == null ? [] : Ext.encode(param2).replace(/\\/g,"%"); param3 = param3 == null ? [] : Ext.encode(param3).replace(/\\/g,"%"); if(form.getForm().isValid()){ Ext.each(form.items.items, function(item){ if(item.xtype == 'numberfield'){ if(item.value == null || item.value == ''){ item.setValue(0); } } }); var r = form.getValues(); form.getForm().getFields().each(function(){ if(this.logic == 'ignore') { delete r[this.name]; } }); me.FormUtil.save(r, param1, param2,param3); }else{ me.FormUtil.checkForm(); } }, beforeUpdate: function(){ var me = this; var form = Ext.getCmp('form'); if(! me.FormUtil.checkForm()){ return; } var detail = Ext.getCmp('grid'); Ext.each(detail.store.data.items, function(item){ if(item.data.prd_id == null || item.data.prd_id == 0){ item.data.prd_id = -item.index; } }); var param1 = me.GridUtil.getGridStore(detail); var param2 = new Array(); var param3 = new Array(); if(Ext.getCmp('assdetail')){ Ext.each(Ext.Object.getKeys(Ext.getCmp('assdetail').cacheStoreGrid), function(key){ Ext.each(Ext.getCmp('assdetail').cacheStoreGrid[key], function(d){ d['dass_condid'] = key; param2.push(d); }); }); } if(Ext.getCmp('assmainbutton')){ Ext.each(Ext.Object.getKeys(Ext.getCmp('assmainbutton').cacheStoreForm), function(key){ Ext.each(Ext.getCmp('assmainbutton').cacheStoreForm[key], function(d){ d['ass_conid'] = key; param3.push(d); }); }); } if(me.FormUtil.checkFormDirty(form) == '' && detail.necessaryField.length > 0 && (param1.length == 0) && param2.length == 0&& param3.length == 0){ showError($I18N.common.grid.emptyDetail); return; } else { param1 = param1 == null ? [] : "[" + param1.toString().replace(/\\/g,"%") + "]"; param2 = param2 == null ? [] : Ext.encode(param2).replace(/\\/g,"%"); param3 = param3 == null ? [] : Ext.encode(param3).replace(/\\/g,"%"); if(form.getForm().isValid()){ Ext.each(form.items.items, function(item){ if(item.xtype == 'numberfield'){ if(item.value == null || item.value == ''){ item.setValue(0); } } }); var r = form.getValues(); form.getForm().getFields().each(function(){ if(this.logic == 'ignore') { delete r[this.name]; } }); me.FormUtil.update(r, param1, param2,param3); }else{ me.FormUtil.checkForm(); } } }, //提交时的相关操作开始 checkFormDirty: function(){ var form = Ext.getCmp('form'); var s = ''; form.getForm().getFields().each(function (item,index,length){ var value = item.value == null ? "" : item.value; item.originalValue = item.originalValue == null ? "" : item.originalValue; if(item.originalValue.toString() != value.toString()){//isDirty、wasDirty、dirty一直都是true,没办法判断,所以直接用item.originalValue,原理是一样的 var label = item.fieldLabel || item.ownerCt.fieldLabel || item.boxLabel || item.ownerCt.title;//针对fieldContainer、radio、fieldset等 if(label){ s = s + ' ' + label.replace(/ /g,''); } } }); return (s == '') ? s : ('表单字段('+s+')已修改'); }, onSubmit: function(id){ var me = this; var form = Ext.getCmp('form'); if(form && form.getForm().isValid()){ var s = me.checkFormDirty(form); var grids = Ext.ComponentQuery.query('gridpanel'); if(grids.length > 0){//check所有grid是否已修改 var param = grids[0].GridUtil.getAllGridStore(grids[0]); // if(grids[0].necessaryField.length > 0 && (param == null || param == '')){ // showError("明细表还未添加数据,无法提交!"); // return; // } Ext.each(grids, function(grid, index){ if(grid.GridUtil){ var msg = grid.GridUtil.checkGridDirty(grid); if(msg.length > 0){ s = s + '
' + grid.GridUtil.checkGridDirty(grid); } } }); } if(s == '' || s == '
'){ me.submit(id); } else { Ext.MessageBox.show({ title:'保存修改?', msg: '该单据已被修改:
' + s + '
提交前要先保存吗?', buttons: Ext.Msg.YESNOCANCEL, icon: Ext.Msg.WARNING, fn: function(btn){ if(btn == 'yes'){ me.beforeUpdate(form); } else if(btn == 'no'){ } else { return; } } }); } } else { me.checkForm(); } }, contains: function(string,substr,isIgnoreCase){ if(isIgnoreCase){ string=string.toLowerCase(); substr=substr.toLowerCase(); } var startChar=substr.substring(0,1); var strLen=substr.length; for(var j=0;j 0){ tab = win[win.length-1]; } } return tab; }, submit: function(id){ var me = this; var form = Ext.getCmp('form'); if(!me.contains(form.submitUrl, '?caller=', true)){ form.submitUrl = form.submitUrl + "?caller=" + caller; } me.getActiveTab().setLoading(true);//loading... Ext.Ajax.request({ url : basePath + form.submitUrl, params: { id: id }, method : 'post', callback : function(options,success,response){ me.getActiveTab().setLoading(false); var localJson = new Ext.decode(response.responseText); if(localJson.success){ //submit成功后刷新页面进入可编辑的页面 submitSuccess(function(){ window.location.href = window.location.href; }); } else { if(localJson.exceptionInfo){ var str = localJson.exceptionInfo; if(str.trim().substr(0, 12) == 'AFTERSUCCESS'){//特殊情况:操作成功,但是出现警告,允许刷新页面 str = str.replace('AFTERSUCCESS', ''); submitSuccess(function(){ var url = window.location.href; var formCondition = form.keyField + "IS" + id; var gridCondition = ''; var grid = Ext.getCmp('grid'); if(grid && grid.mainField){ gridCondition = grid.mainField + "IS" + id; } if(me.contains(url, 'formCondition', true)){ url = url.replace('formCondition', 1); url = url.replace('gridCondition', 1); } if(me.contains(url, '?', true)){ url = url + '&formCondition=' + formCondition + '&gridCondition=' + gridCondition; } else { url = url + '?formCondition=' + formCondition + '&gridCondition=' + gridCondition; } window.location.href = url; }); } showMessage("提示", str);return; } } } }); } //提交时的相关操作结束 // beforeUpdate: function(){ // var grid = Ext.getCmp('grid'); // Ext.Array.each(grid.store.data.items, function(item){ // item.set('prd_prid',Ext.getCmp('pr_id').value); // }); // //采购价格不能为0 // var grid = Ext.getCmp('grid'); // var items = grid.store.data.items; // var bool = true; // var warnStr=''; // Ext.each(items,function(item,index){ // if(item.dirty && item.data[grid.necessaryField] != null && item.data[grid.necessaryField] != ''){ // var orderamount =item.data['prd_orderamount']; //订单金额 // var havebalance = item.data['sa_prepayamount']; //已预收金额 // var nowbalance = item.data['prd_nowbalance']; //本次预收额 // //订单金额>=已预收金额+本次预收额 // // //此种情况不能进行保存 // if(havebalance+nowbalance>orderamount){ // var i = index+1; // if(warnStr ==''||warnStr.length<=0){ // warnStr = warnStr +i; // }else{ // warnStr = warnStr +','+i; // } // bool = false; // } // } // // }); // // if(bool){ // this.FormUtil.onUpdate(this); // }else{ // if(warnStr != ''||warnStr.length>0){ // showError('明细表第'+warnStr+'行 已预收金额与本次预收额的合计超过订单金额,不能保存');return; // } // } // // } });