/*
 * JSMin
 * Javascript Compressor
 * http://www.crockford.com/
 * http://www.smallsharptools.com/Projects/Packer/
*/

// V:\1.1.007\js\form\QE\Ext.ux.Namirial.form.DettagliQE.js

Ext.ns('Ext.ux','Ext.ux.Namirial','Ext.ux.Namirial.form');Ext.ux.Namirial.form.DettagliQE=Ext.extend(Ext.util.Observable,{constructor:function(c){try{this.multirigo=(!Ext.isEmpty(c.recordStore));this.recordStore=c.recordStore;if(this.multirigo){this.rollbackData=this.recordStore.queryBy(function(){return true;});}
this.localArray=new Array();this.totalField=c.totalField;this.cfisc=c.cfisc||dichiarazione.getCurrentCfisc();if(this.totalField)
this.cod=this.totalField.dataIndex;if(c.optionalPanel){this.optionalPanel=c.optionalPanel;}else{this.optionalPanel=new Ext.Panel({border:false});c.gridHeight=c.gridHeight||250;}
this.idRecord=c.idRecord||'';this.idLabel=c.idLabel||false;this.numIdField=c.numIdField;this.filtro="RIGO = '"+this.cod+"' UNION select COD as CODICE,DETTAGLIO as RIGO,[DESC] as DESCRIZIONE,DA_CUD as BLOCCO,ID as NO_IMPORTO,CFISC as OBBLIGATORIO from "+QEDETT_TAB+" WHERE RIGO = '"+this.cod+"' AND CODICE = '0'";this.dirty=false;this.tb_nuovo=new Ext.Toolbar.Button({text:'Nuovo',icon:IMG_NUOVO,disabled:(this.cod=='E17'||this.cod=='E25'),iconCls:'x-btn-text-icon',tooltip:{title:'Nuovo',text:'Aggiunge una nuova voce'},handler:this.nuovoDettaglio,scope:this});this.tb_nuovo.on('click',function(e){this.sm.clearSelections();this.tb_salva.enable();this.tb_modifica.disable();this.tb_cancella.disable();this.tb_annulla.enable();},this);this.tb_salva=new Ext.Toolbar.Button({text:'Salva',icon:IMG_SALVA,disabled:true,iconCls:'x-btn-text-icon',tooltip:{title:'Salva',text:'Salva le modifiche apportare alla voce'},handler:this.salvaDettaglio,scope:this});this.tb_modifica=new Ext.Toolbar.Button({text:'Modifica',iconCls:'x-btn-text-icon',disabled:true,tooltip:{title:'Modifica',text:'Modifica della voce selezionata'},icon:IMG_EDIT,handler:this.modificaDettaglio,scope:this});this.tb_modifica.on('click',function(){this.tb_salva.enable();this.tb_annulla.enable();},this);this.tb_cancella=new Ext.Toolbar.Button({text:'Cancella',iconCls:'x-btn-text-icon',disabled:true,tooltip:{title:'Cancella',text:'Cancella la voce selezionata'},icon:IMG_CANCEL,handler:this.cancellaDettaglio,scope:this});this.tb_annulla=new Ext.Toolbar.Button({text:'Annulla',icon:IMG_UNDO,scope:this,iconCls:'x-btn-text-icon',disabled:true,tooltip:{title:'Annulla',text:'Annulla modifiche'},handler:this.annulla_record});this.basicForm=new Ext.form.BasicForm(null,{trackResetOnLoad:true});this.numero=new Ext.ux.Namirial.form.NumberField({name:'NDOC',width:30,disabled:true,allowDecimals:false,allowNegative:false,labelSeparator:'',fieldLabel:'N.',value:1});this.basicForm.add(this.numero);this.codice=new Ext.ux.Namirial.form.NumberField({name:'COD',width:30,disabled:true,allowDecimals:false,allowNegative:false,allowZero:(this.cod!='E25'&&this.cod!='E17'),nanText:'',labelSeparator:'',fieldLabel:'Cod.'});this.basicForm.add(this.codice);this.codice.on('change',function(field,newval){if(newval==0){this.descrizione.setValue('');this.tot.enable();this.tot.setRawValue('');this.percentuale.enable();}
else{this.descStore.clearFilter();var recPosition=this.descStore.find('CODICE',newval);var rec=this.descStore.getAt(recPosition);if(rec){this.descrizione.setValue(rec.get('DESCRIZIONE'))}
else{this.codice.setValue(0);this.descrizione.setValue('');}}},this)
this.descStore=new Ext.ux.Namirial.data.GearsStore({gearsConn:conn,dbname:GDATABASE,selectFrom:QE_DOC_TAB,tableId:'DESCRIZIONE',fields:structStore.getFields(QE_DOC_TAB),selectFilter:this.filtro,autoLoad:true,autoCommit:false});this.descStore.on('load',function(dstore,rec){if(this.cod=='E17'||this.cod=='E25'){this.store.each(function(record){var idx=dstore.find('CODICE',record.get('COD'));dstore.removeAt(idx);},this)}
this.obbligatori=dstore.query('OBBLIGATORIO','1');},this);this.descrizione=new Ext.form.ComboBox({name:'DESC',width:190,listWidth:290,tpl:new Ext.XTemplate('<table border="0" width="100%" cellspacing="0">','<tpl for=".">','<tpl if="CODICE &gt; 0">','<tr class="x-combo-list-item wrap">','<td style="font-size: 11px; padding-left: 3px; padding-right: 3px; padding-bottom: 3px">','{CODICE}:','</td>','<td style="font-size: 11px; padding-right: 3px; padding-bottom: 3px">','<span qtip="{DESCRIZIONE}">{[(values.DESCRIZIONE.length > 50)?(values.DESCRIZIONE.slice(0,50)+"..."):values.DESCRIZIONE]}</span>','</td>','</tr>','</tpl>','<tpl if="CODICE &lt; 1">','<tr class="x-combo-list-item wrap">','<td style="font-size: 11px; padding-left: 3px; padding-right: 3px; padding-bottom: 3px"></td><td style="font-size: 11px; padding-right: 3px; padding-bottom: 3px">','<span qtip="{DESCRIZIONE}">{[(values.DESCRIZIONE.length > 50)?(values.DESCRIZIONE.slice(0,50)+"..."):values.DESCRIZIONE]}</span>','</td style="font-size: 11px">','</tr>','</tpl>','</tpl>','</table>'),disabled:true,labelSeparator:'',fieldLabel:'Tipologia documento esibito',store:this.descStore,displayField:'DESCRIZIONE',showValue:true,readOnly:(this.cod=='E17'||this.cod=='E25'),valueField:'DESCRIZIONE',mode:'local',triggerAction:'all',allowBlank:false});this.basicForm.add(this.descrizione);this.descrizione.on('select',function(combo,rec,index){this.codice.setValue(rec.get('CODICE'));if(rec.get('NO_IMPORTO')=='1'){this.tot.setRawValue('');this.tot.disable();this.percentuale.setRawValue('');this.percentuale.disable();this.quota.setRawValue('');}
else{this.tot.enable()
this.percentuale.enable();this.percentuale.setValue(100);}},this);this.descrizione.on('blur',function(combo){var value=this.descrizione.getRawValue();this.descStore.clearFilter();var recPosition=this.descStore.findBy(function(test){return(test.get('DESCRIZIONE')==value)});if(recPosition==-1){this.codice.setValue(0);this.descrizione.setValue(value);}},this);var tip={title:"",text:"Inserire l'importo totale della spesa"}
this.tot=new Ext.ux.Namirial.form.NumberField({name:'IMPORTO_TOT',width:90,allowNegative:false,allowZero:true,decimalPrecision:2,minValue:0.01,disabled:true,labelSeparator:'',fieldLabel:'Spesa totale',asString:true});this.basicForm.add(this.tot);this.tot.on('change',function(field,newval){var totale=field.getNumericValue();if(this.cod=='E17'){if(totale>E17_MAX)totale=E17_MAX;}
if(this.cod=='E25'){if(totale>E25_RIGO_MAX)totale=E25_RIGO_MAX;}
var importo=totale*(this.percentuale.getNumericValue()/100);if(importo<0.01||this.cod=='E14'||this.cod=='E18'||this.cod=='C33'||this.cod=='C36'){this.quota.setRawValue('');}
else{this.quota.setValue(importo);};},this)
this.percentuale=new Ext.ux.Namirial.form.NumberField({name:'PERCENTUALE',width:50,allowNegative:false,allowZero:true,decimalPrecision:2,maxValue:100,minValue:0.01,disabled:true,labelSeparator:'',fieldLabel:'%',allowBlank:false,value:100,asString:true});this.basicForm.add(this.percentuale);if(this.cod=='E16'||this.cod=='E17'){this.percentuale.on('enable',function(){this.disable();})}
if(this.cod=='E7'){linkFnAppoggio=function(){var calcE7=new Ext.ux.Namirial.form.CalcE7({targetField:this.tot})
calcE7.showWin()}.createDelegate(this)
this.tot.setPersistentTooltip({title:"Rideterminazione interessi (F7)",bodyCfg:{tag:'center'},html:'<a href="javascript:linkFnAppoggio()">Calcola</a>',align:'b-t',hideDelay:500})
this.tot.enableKeyEvents=true;this.tot.on('keyup',function(field,e){if(e.getKey()==Ext.EventObject.F7){e.stopEvent();linkFnAppoggio();}},this)}
if(this.cod=='E10'){linkFnAppoggio=function(){var calcE10=new Ext.ux.Namirial.form.CalcE10({targetField:this.tot})
calcE10.showWin()}.createDelegate(this)
this.tot.setPersistentTooltip({title:"Rideterminazione interessi (F7)",bodyCfg:{tag:'center'},html:'<a href="javascript:linkFnAppoggio()">Calcola</a>',align:'b-t',hideDelay:500})
this.tot.enableKeyEvents=true;this.tot.on('keyup',function(field,e){if(e.getKey()==Ext.EventObject.F7){e.stopEvent();linkFnAppoggio();}},this)}
if(this.cod=='E24'){linkFnAppoggio=function(){var calcE24=new Ext.ux.Namirial.form.CalcE24({targetField:this.tot})
calcE24.showWin()}.createDelegate(this)
this.tot.setPersistentTooltip({title:"Calcolo dei contributi<br>per addetti ai servizi domestici (F7)",bodyCfg:{tag:'center'},html:'<a href="javascript:linkFnAppoggio()">Calcola</a>',align:'b-t',hideDelay:500})
this.tot.enableKeyEvents=true;this.tot.on('keyup',function(field,e){if(e.getKey()==Ext.EventObject.F7){e.stopEvent();linkFnAppoggio();}},this)}
this.percentuale.on('change',function(field,newval){var totale=this.tot.getNumericValue();if(this.cod=='E17'){if(totale>E17_MAX)totale=E17_MAX;}
if(this.cod=='E25'){if(totale>E25_RIGO_MAX)totale=E25_RIGO_MAX;}
if(totale>0){var importo=totale*(this.percentuale.getNumericValue()/100);if(importo<0.01||this.cod=='E14'||this.cod=='E18'||this.cod=='C33'||this.cod=='C36'){this.quota.setRawValue('');}
else{this.quota.setValue(importo);};}},this)
this.quota=new Ext.ux.Namirial.form.NumberField({name:'IMPORTO',width:90,allowNegative:false,decimalPrecision:2,allowZero:true,disabled:true,labelSeparator:'',readOnly:true,fieldLabel:'Quota spett.',asString:true,minValue:0.01});this.basicForm.add(this.quota);var gridColumns;if(this.idLabel){gridColumns=header_QE_Dettagli.concat({header:this.idLabel,dataIndex:'DATA',sortable:false})
this.data=new Ext.ux.Namirial.form.TextField({name:'DATA',disabled:true,labelSeparator:'',fieldLabel:this.idLabel,allowBlank:false,autoCreate:MAX_LENGHT(10)});}else{gridColumns=header_QE_Dettagli.concat({header:'Data',dataIndex:'DATA',sortable:false,renderer:renderDate2})
this.data=new Ext.ux.Namirial.form.DateField({name:'DATA',disabled:true,labelSeparator:'',fieldLabel:'Data',format:'d/m/Y',returnFormat:'dmY'});}
this.basicForm.add(this.data);this.cfiscField=new Ext.ux.Namirial.form.TextField({name:'CFISC',hidden:true,value:this.cfisc});this.basicForm.add(this.cfiscField);this.keyIdField=new Ext.ux.Namirial.form.TextField({name:'keyId',hidden:true});this.basicForm.add(this.keyIdField);this.idField=new Ext.ux.Namirial.form.TextField({name:'ID',hidden:true,value:this.idRecord});this.basicForm.add(this.idField);this.dettaglio=new Ext.ux.Namirial.form.TextField({name:'DETTAGLIO',hidden:true});this.basicForm.add(this.dettaglio);this.cud=new Ext.form.NumberField({name:'DA_CUD',hidden:true,value:0});this.basicForm.add(this.cud);this.totale_quote=new Ext.ux.Namirial.form.NumberField({name:'TOT_QUOTE',width:90,allowNegative:false,decimalPrecision:2,allowZero:true,readOnly:true,fieldLabel:'Totale quote'});this.importo_detrazione=new Ext.ux.Namirial.form.NumberField({name:'IMPORTO_DETRAZIONE',width:90,allowNegative:false,allowDecimals:false,allowZero:true,readOnly:true,fieldLabel:'Importo di detrazione'});this.formPanel=new Ext.form.FormPanel({labelAlign:'top',width:650,height:60,region:'north',layout:'column',bodyStyle:SFONDO_TRASPARENTE+PADDING_5,border:false,defaults:{layout:'form',bodyStyle:SFONDO_TRASPARENTE,border:false},items:[{width:40,items:this.numero},{width:40,items:this.codice},{width:200,items:this.descrizione},{width:100,items:this.tot},{width:60,items:this.percentuale},{width:100,items:this.quota},{width:130,items:this.data}]})
this.store=this.recordStore||new Ext.ux.Namirial.data.GearsStore({gearsConn:conn,dbname:GDATABASE,selectFrom:QEDETT_TAB,tableId:'keyId',fields:structStore.getFields(QEDETT_TAB),selectFilter:"CFISC = '"+this.cfisc+"' AND DETTAGLIO = '"+this.cod+"'"+(this.idRecord?(" AND ID ='"+this.idRecord+"'"):""),autoLoad:true,autoCommit:false});this.store.on({'load':{fn:function(o,rec){if(this.cod=='E17'||this.cod=='E25'){this.store.each(function(record){var idx=this.descStore.find('CODICE',record.get('COD'));this.descStore.removeAt(idx);},this)}
var record=conn.query(getQuery(SEL_QE,[this.cfisc]));var rec=new Ext.data.Record(record.rows[0]);if(this.cod=='E7'){contr=parseFloat(rec.get('E7_CONTRIBUTI'));}
if(this.cod=='E8'){contr=parseFloat(rec.get('E8_CONTRIBUTI'));}
if(this.cod=='E9'){contr=parseFloat(rec.get('E9_CONTRIBUTI'));perc=parseFloat(rec.get('E9_PERC'));}
if(this.cod=='E10'){contr=parseFloat(rec.get('E10_CONTRIBUTI'));perc=parseFloat(rec.get('E10_PERC'));}
if(this.cod=='E11'){contr=parseFloat(rec.get('E9_CONTRIBUTI'));}
if(this.cod=='E17'){perc=parseFloat(rec.get('E17_PERC'));}
if(this.cod=='E27_1'){contr=parseFloat(rec.get('E27_1_CONTRIBUTI'));}
if(!contr){contr=0;}
if(!perc){perc=0;}
this.setTotali();if(this.descStore.getCount()>0){this.tb_nuovo.enable();}},scope:this,single:true}})
function rowSelAction(sm,row,rec){this.btnOk.enable();this.tb_annulla.disable();this.basicForm.loadRecord(rec);for(i=0;i<this.descStore.getCount();i++){if(this.descStore.getAt(i).get('CODICE')==this.codice.getValue()&&this.descStore.getAt(i).get('NO_IMPORTO')=='1'){this.tot.setRawValue('');this.percentuale.setRawValue('');this.quota.setRawValue('');}};this.numero.disable();this.codice.disable();this.descrizione.disable();this.tot.disable();this.percentuale.disable();this.quota.disable();this.data.disable();this.tb_cancella.enable();this.tb_modifica.enable();this.tb_salva.disable();}
this.sm=new Ext.grid.RowSelectionModel({singleSelect:true,listeners:{rowselect:{fn:rowSelAction,scope:this}}})
this.grid=new Ext.grid.GridPanel({autoHeight:(c.gridHeight)?false:true,height:c.gridHeight||'auto',border:false,stripeRows:true,store:this.store,sm:this.sm,columns:gridColumns,listeners:{rowdblclick:{scope:this,fn:function(){this.tb_salva.enable();if(this.cod=='E25'||this.cod=='E17'){this.numero.enable();this.codice.disable();this.descrizione.disable();this.tot.enable();this.percentuale.enable();this.quota.disable();this.data.enable();}
else{for(i=0;i<this.descStore.getCount();i++){if(this.descStore.getAt(i).get('CODICE')==this.codice.getValue()&&this.descStore.getAt(i).get('NO_IMPORTO')=='1'){this.numero.enable();this.codice.enable();this.descrizione.enable();this.tot.disable();this.percentuale.disable();this.quota.disable();this.data.enable();}
else{this.numero.enable();this.codice.enable();this.descrizione.enable();this.tot.enable();this.percentuale.enable();this.quota.disable();this.data.enable();}};}},scope:this}}})
this.btnOk=new Ext.Button({text:'Ok',minWidth:70,handler:this.controlloDocumenti,cls:'x-btn-text-icon',icon:IMG_OK,scope:this})
this.btnCancel=new Ext.Button({text:'Annulla',cls:'x-btn-text-icon',icon:IMG_CANCEL,handler:function(){this.winDettagliQE.close();},scope:this})
this.totali=new Ext.Panel({width:650,height:34,region:'south',layout:'column',bodyStyle:SFONDO_TRASPARENTE+PADDING_5,border:false,defaults:{labelAlign:'right',layout:'form',bodyStyle:SFONDO_TRASPARENTE,border:false},items:[{labelWidth:70,width:200,items:this.totale_quote},{labelWidth:120,columnWidth:1,items:this.importo_detrazione},{width:80,items:this.btnOk},{width:80,items:this.btnCancel}]})
this.winDettagliQE=new Ext.Window({title:'Dettaglio '+dettagli_QE[this.cod],constrainHeader:true,resizable:false,modal:true,width:710,height:400,layout:'border',tbar:[this.tb_nuovo,this.tb_salva,this.tb_modifica,this.tb_cancella,this.tb_annulla],items:[this.formPanel,{layout:'form',region:'center',border:false,bodyStyle:SFONDO_TRASPARENTE+'border-top: 1px solid '+BORDER_COLOR+';border-bottom: 1px solid '+BORDER_COLOR,items:[this.grid,this.optionalPanel]},this.totali]});this.winDettagliQE.on('show',function(){this.setTotali();},this)
this.winDettagliQE.on('beforeclose',function(){if(this.dirty){Ext.Msg.confirm('Attenzione','Chiudendo la finestra si perderanno i dati, vuoi continuare?',function(btn){if(btn=='yes'){this.winDettagliQE.suspendEvents();if(this.multirigo){this.recordStore.removeAll();this.rollbackData.each(function(record){this.recordStore.add(record);},this)}
this.winDettagliQE.close();this.winDettagliQE.resumeEvents();}},this)
return false;}},this)
this.winDettagliQE.on('close',function(){},this)
this.storeObbligatori=new Ext.data.SimpleStore({fields:structStore.getFields(QEDETT_TAB)});return this;}catch(e){getToastMsg(MSG_TEXT,'Dettagli QE');log.error("Ext.ux.Namirial.form.DettagliQE#1 -> ",e)}},nuovoDettaglio:function(){this.numero.enable();this.codice.enable();this.descrizione.enable();this.tot.enable();this.percentuale.enable();this.quota.disable();this.data.enable();this.codice.setRawValue('');this.descrizione.setValue('');this.tot.setRawValue('');this.percentuale.setValue(100);this.quota.setRawValue('');this.data.setRawValue('');this.cfiscField.setValue(this.cfisc);this.cud.setValue(0);this.idField.setValue(this.idRecord)
this.dettaglio.setValue(this.cod);this.btnOk.disable();},salvaDettaglio:function(){try{var isvalid=this.basicForm.isValid();var isdirty=this.basicForm.isDirty();if(isdirty&&isvalid){var record=this.sm.getSelected();var percent=this.percentuale.getNumericValue();var importo=this.tot.getNumericValue()*(percent/100);var quota=0;var max;var id;var nuovoDettaglio;if(record){nuovoDettaglio=false;}else{nuovoDettaglio=true;}
function controlloParticolare(){for(var i=0;i<this.store.getCount();i++){if(!this.sm.isSelected(i)||nuovoDettaglio){if(this.store.getAt(i).get('DATA')==id){quota=quota+parseInt(this.store.getAt(i).get('IMPORTO'),10);}}};if((importo+quota)>max){var val=max-quota;if(val<0){this.quota.setValue(0);}
else{this.quota.setValue(val);}}
else{this.quota.setValue(importo);};}
switch(this.cod){case'E14':max=E14_MAX*(percent/100);id=this.data.getValue();controlloParticolare.call(this);break;case'E18':max=E18_MAX*(percent/100);id=this.data.getValue();controlloParticolare.call(this);break;case'E17':max=E17_MAX;if(importo>max){this.quota.setValue(max);}
else{this.quota.setValue(importo);};break;case'E25':max=E25_RIGO_MAX;id=this.codice.getValue();controlloParticolare.call(this);break;case'C33':max=C33_MAX*(percent/100);id=this.data.getValue();controlloParticolare.call(this);break;case'C36':max=C36_MAX*(percent/100);id=this.data.getValue();controlloParticolare.call(this);break;}
var values;if(nuovoDettaglio){values=getValuesFromForm(this.basicForm,true,['keyId']);var record=new this.store.recordType(values);this.store.add(record);this.localArray.push(generateQuery(LOCAL,QEDETT_TAB,values,INSERT))
if(this.cod=='E25'||this.cod=='E17'){var idx=this.descStore.find('CODICE',record.get('COD'));this.descStore.removeAt(idx);if(this.descStore.getCount()==0){this.tb_nuovo.disable();}}}else{values=getValuesFromForm(this.basicForm,true);this.basicForm.updateRecord(record);record.commit();this.localArray.push(generateQuery(LOCAL,QEDETT_TAB,values,UPDATE,"keyId = "+values.keyId))}
this.dirty=true;this.setTotali();this.tb_salva.disable();this.tb_annulla.disable();this.btnOk.enable();this.numero.setValue(1);this.codice.setRawValue('');this.descrizione.setRawValue('');this.tot.setRawValue('');this.percentuale.setValue(100);this.quota.setRawValue('');this.data.setRawValue('');this.numero.disable();this.codice.disable();this.descrizione.disable();this.tot.disable();this.percentuale.disable();this.quota.disable();this.data.disable();this.tb_cancella.enable();this.tb_modifica.enable();this.tb_salva.disable();this.sm.clearSelections();}else{if(!isvalid){Toast('Valori non validi','Sono stati inseriti dei valori non validi. Correggere gli errori e continuare.');}else if(!isdirty){this.tb_salva.disable();this.tb_annulla.disable();this.btnOk.enable();this.setModifica(false);}}}catch(e){log.error("Ext.ux.Namirial.form.DettagliQE#2 -> ",e)}},modificaDettaglio:function(){this.btnOk.disable();if(this.cod=='E25'||this.cod=='E17'){this.numero.enable();this.codice.disable();this.descrizione.disable();this.tot.enable();this.percentuale.enable();this.quota.disable();this.data.enable();}
else{for(i=0;i<this.descStore.getCount();i++){if(this.descStore.getAt(i).get('CODICE')==this.codice.getValue()&&this.descStore.getAt(i).get('NO_IMPORTO')=='1'){this.numero.enable();this.codice.enable();this.descrizione.enable();this.tot.disable();this.percentuale.disable();this.quota.disable();this.data.enable();}
else{this.numero.enable();this.codice.enable();this.descrizione.enable();this.tot.enable();this.percentuale.enable();this.quota.disable();this.data.enable();}};}},cancellaDettaglio:function(){var rec=this.sm.getSelected();if(rec){this.store.remove(rec);this.localArray.push("DELETE FROM "+QEDETT_TAB+" WHERE keyId = "+rec.get('keyId'));if(this.cod=='E17'||this.cod=='E25'){var add=conn.query(getQuery(SEL_QE_DOC,[this.cod,rec.get('COD')]));var addRec=new this.descStore.recordType(add.rows[0]);this.descStore.add(addRec);this.descStore.sort('CODICE','ASC');if(this.descStore.getCount()>0){this.tb_nuovo.enable();}}
this.numero.setValue(1);this.codice.setRawValue('');this.codice.disable();this.descrizione.setRawValue('');this.descrizione.disable();this.tot.setRawValue('');this.tot.disable();this.percentuale.setValue(100);this.percentuale.disable();this.quota.setRawValue('');this.quota.disable();this.data.setRawValue('');this.data.disable();this.tb_modifica.disable();this.tb_cancella.disable();}
this.setTotali()
this.dirty=true;},annulla_record:function(sm,row,rec){var rec=this.sm.getSelected();this.btnOk.enable();this.tb_annulla.disable();if(rec){this.basicForm.loadRecord(rec);for(i=0;i<this.descStore.getCount();i++){if(this.descStore.getAt(i).get('CODICE')==this.codice.getValue()&&this.descStore.getAt(i).get('NO_IMPORTO')=='1'){this.tot.setRawValue('');this.percentuale.setRawValue('');this.quota.setRawValue('');}};this.tb_cancella.enable();this.tb_modifica.enable();}
this.numero.disable();this.codice.disable();this.descrizione.disable();this.tot.disable();this.percentuale.disable();this.quota.disable();this.data.disable();this.tb_salva.disable();},salvaTutto:function(btn){try{if(btn){if(btn.getText()=='Si'){var records=this.smObbligatori.getSelections();Ext.each(records,function(rec){this.store.add(rec);this.localArray.push(generateQuery(LOCAL,QEDETT_TAB,rec.data,INSERT));},this)}
this.documentiObbligatori.close();}
var tot=this.importo_detrazione.getValue();switch(this.cod){case'E8':var max;if(e7_val>0){if(e7_val<E8_MAX)
max=E8_MAX-e7_val;else
max=0;}
else{max=E8_MAX;}
e8_val_tot=tot;if(tot>max)
tot=max;e8_val=tot;break;case'E14':if(tot==0){this.totalField.setReadOnly(false);this.numIdField.setReadOnly(false);}
else{this.totalField.setReadOnly(true);this.numIdField.setReadOnly(true);}
this.numIdField.setValue(num_field);break;case'C36':this.numIdField.setValue(num_field);var errorText="figlio";case'E18':if(!errorText){var errorText="studente";}
var vista=this.grid.getView();vista.refresh();var groups=this.store.collect('DATA');var errati=[];Ext.each(groups,function(gruppo){var checkPercentuale=-1;var collection=this.store.queryBy(function(rec){return(rec.get('DATA')==gruppo)},this);collection.each(function(record){if(checkPercentuale==-1){checkPercentuale=convertiStrNum(record.get('PERCENTUALE'));}else{if(convertiStrNum(record.get('PERCENTUALE'))!=checkPercentuale){errati.push(gruppo);return false;}}},this)},this)
if(errati.length){this.store.each(function(record){if(errati.indexOf(record.get('DATA'))>-1){vista.setRowError(record,"Non si possono indicare percentuali diverse per uno stesso "+errorText)}},this)
return;}
break;}
this.totalField.setValue(tot);this.totalField.fireEvent('change',this.totalField);if(this.multirigo){}else{for(var i=0,len=this.localArray.length;i<len;i++){conn.exec(this.localArray[i]);}}
this.dirty=false;this.winDettagliQE.close();}catch(e){log.error("Ext.ux.Namirial.form.DettagliQE#3 -> ",e)
getToastMsg(MSG_ERROR_SAVE);}},controlloDocumenti:function(){switch(this.cod){case'E7':case'E8':case'E9':case'E10':case'E11':case'E17':case'E27_1':if(BF_QE.isValid()){var sql;var whereCl="CFISC="+"'"+this.cfisc+"'";sql=generateQueryFromForm(QE_TAB,BF_QE,'UPDATE',whereCl,null,'basicform');this.localArray.push(sql);}
else{getToastMsg(MSG_INVALID_FIELD);return false;}
break;}
var open=false;if(this.store.getCount()>0){var ok=false;for(var i=0;i<this.store.getCount();i++){if(this.store.getAt(i).get('IMPORTO')!=''||this.cod=='E25'||this.cod=='E17'){ok=true}}}
else{ok=true;}
if(ok){if(this.store.getCount()>0&&this.obbligatori.getCount()>0){this.obbligatori.each(function(rec){var codice=rec.get('CODICE');if(this.store.find('COD',codice)==-1){open=true;var there=false;for(var i=0;i<this.storeObbligatori.getCount();i++){if(this.storeObbligatori.getAt(i).get('COD')==codice){there=true;}};if(!there){var newRecord=new this.store.recordType({CFISC:this.cfisc,DETTAGLIO:this.cod,COD:codice,DESC:rec.get('DESCRIZIONE'),NDOC:1,DA_CUD:0,ID:this.idRecord})
this.storeObbligatori.add(newRecord);}}else{}},this)}
if(open){this.smObbligatori=header_QE_Obbligatori[0];this.gridObbligatori=new Ext.grid.GridPanel({height:150,border:false,store:this.storeObbligatori,autoExpandColumn:'desc_column',columns:header_QE_Obbligatori,sm:this.smObbligatori})
this.documentiObbligatori=new Ext.Window({title:'Controllo documenti',constrainHeader:true,resizable:false,modal:true,width:400,height:300,buttonAlign:'right',buttons:[{text:'Si',handler:this.salvaTutto,scope:this},{text:'No',handler:this.salvaTutto,scope:this}],items:[{bodyStyle:SFONDO_TRASPARENTE+PADDING_10,border:false,items:{xtype:'label',text:'Si ricorda che, in merito alle spese in oggetto, occorre acquisire anche la seguente documentazione:'}},this.gridObbligatori]})
this.documentiObbligatori.on('show',function(){this.smObbligatori.selectAll.defer(50,this.smObbligatori);},this)
this.documentiObbligatori.show();}
else
this.salvaTutto.call(this);}
else{Ext.MessageBox.alert('Attenzione','Inserire almeno un importo diverso da zero.')}},setTotali:function(){var tot_quote=this.store.stringSum('IMPORTO');var tot_detrazione=this.store.stringSum('IMPORTO_TOT');var max;var importo;switch(this.cod){case'E1':case'E1_2':case'E3':case'E13':case'E16':case'E22':case'E23':case'E23_2':case'E26':case'E27_2':case'E27_3':case'E27_4':case'E27_5':case'E28':case'C24':case'C25':case'C26':case'C27':case'C28':case'C30':case'C31':case'C34':case'C35':case'E33':case'E37_3':case'E37_4':case'E38_1':case'E38_2':case'E38_3':case'E38_4':case'E44_1':case'E44_2':this.totale_quote.setValue(tot_quote.toFixed(2));this.importo_detrazione.setValue(tot_quote.toFixed(0));break;case'E2':max=E2_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E4_2':max=max_e4_2;tot_quote_e4_2=tot_quote;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E4':case'E4_3':max=E4_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E7':max=E7_MAX;if((tot_quote.toFixed(2)-contr)>0){importo=tot_quote.toFixed(2)-contr;}
else{importo=0}
this.totale_quote.setValue(importo);if(importo>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(importo.toFixed(0));}
break;case'E8':max=E8_MAX;if((tot_quote.toFixed(2)-contr)>0){importo=tot_quote.toFixed(2)-contr;}
else{importo=0}
this.totale_quote.setValue(importo);if(importo>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(importo.toFixed(0));}
break;case'E9':case'E10':max=(E10_MAX*perc)/100;if((tot_quote.toFixed(2)-contr)>0){importo=tot_quote.toFixed(2)-contr;}
else{importo=0}
this.totale_quote.setValue(importo);if(importo>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(importo);}
break;case'E11':if((tot_quote.toFixed(2)-contr)>0){importo=tot_quote.toFixed(2)-contr;}
else{importo=0}
this.totale_quote.setValue(importo);this.importo_detrazione.setValue(importo);break;case'E12':max=E12_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E14':this.totale_quote.setValue(tot_quote.toFixed(2));this.importo_detrazione.setValue(tot_quote.toFixed(0));var id=new Array();for(var i=0;i<this.store.getCount();i++){id[i]=this.store.getAt(i).get('DATA');};for(var i=0;i<id.length;i++){for(var j=i+1;j<id.length;j++){if(id[i]==id[j]){id.splice(j,1);}};};num_field=id.length;break;case'E15':max=E15_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E17':this.totale_quote.setValue(tot_quote.toFixed(2));this.importo_detrazione.setValue((tot_quote.toFixed(0))*perc/100);break;case'E18':max=E18_MAX;this.totale_quote.setValue(tot_detrazione.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E25':max=E25_MAX;this.totale_quote.setValue(tot_detrazione.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'E27_1':max=E27_1_MAX;if((max-contr)>0){importo=tot_quote.toFixed(2);}
else{importo=0}
if(importo>max){this.totale_quote.setValue((max-contr).toFixed(2));this.importo_detrazione.setValue((max-contr).toFixed(0));}
else{this.totale_quote.setValue(parseInt(importo).toFixed(2));this.importo_detrazione.setValue(parseInt(importo).toFixed(0));}
break;case'C19':max=C19_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C20':case'C23':max=C23_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C21':max=C21_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C22':max=C22_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C29':max=C29_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C32':max=C32_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C33':max=C33_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;case'C36':this.totale_quote.setValue(tot_quote.toFixed(2));this.importo_detrazione.setValue(tot_quote.toFixed(0));var id=new Array();for(var i=0;i<this.store.getCount();i++){id[i]=this.store.getAt(i).get('DATA');};for(var i=0;i<id.length;i++){for(var j=i+1;j<id.length;j++){if(id[i]==id[j]){id.splice(j,1);}};};num_field=id.length;break;case'E24':max=E24_MAX;this.totale_quote.setValue(tot_quote.toFixed(2));if(tot_quote.toFixed(0)>max){this.importo_detrazione.setValue(max.toFixed(0));}
else{this.importo_detrazione.setValue(tot_quote.toFixed(0));}
break;}},apri:function(){this.winDettagliQE.show();},setModifica:function(state){this.grid.getView().refresh();var fields=this.basicForm.items.items;this.btnOk.setDisabled(state);Ext.each(fields,function(field){if(field.name!='IMPORTO'&&field.name!='TOT_QUOTE'&&field.name!='IMPORTO_DETRAZIONI')
field.setDisabled(!state);},this)}})

