/*
 * JSMin
 * Javascript Compressor
 * http://www.crockford.com/
 * http://www.smallsharptools.com/Projects/Packer/
*/

// V:\1.1.007\js\lib\ext-namirial\Ext.ux.Namirial.Dichiarazione.js

Ext.ns('Ext.ux.Namirial');Ext.ux.Namirial.Dichiarazione=Ext.extend(Ext.util.Observable,{constructor:function(c){this.addEvents('load','loadexception','loadtable','locked','notlocked','checklockexception','lock','lockedexception','unlock','unlockedexception');this.nuova=false;this.lastType=DICHIARANTE;this.lastSezione=CONTRIBUENTE;this.congiunta=false;this.cfisc=c.cfisc;this.sesso='U';this.congiunta_sesso='U';this.gearsConn=c.gearsConn;this.scriptUrl=c.scriptUrl;this.dataUrl=c.dataUrl;this.structureStore=c.structureStore;this.recDic=c.recDic;Ext.apply(this,c);Ext.ux.Namirial.Dichiarazione.superclass.constructor.call(this);this.on('loadexception',function(e,server){log.fatal("Ext.ux.Namirial.Dichiarazione loadexception -> ",e)
Toast("Errore","Si "+UNI_E+" verificato un errore nell'apertura della dichiarazione");if(server){this.unlock();rimuoviDichiarazioneLocale()
if(this.progress){this.progress.updateProgress(1,"Errore","Si "+UNI_E+"verificato un'errore");}
hideLoading();}})},load:function(user){showLoading();this.checkLock(this.lock.createDelegate(this,[user,hideLoading.createCallback(0,this.loadGear,this)]));},loadLocal:function(){var nuove=this.sysConn.queryBy(getQuery(SEL_NUOVA_ANAG,[this.cfisc]));if(nuove.totalRows>0){this.nuova=true;}
var rec=this.gearsConn.queryBy(getQuery(SEL_LOCAL_ANAG,[this.cfisc]));this.recDic=rec.rows[0];this.sesso=rec.rows[0].SS;this.tipo=parseInt(rec.rows[0].ST);if(!Ext.isEmpty(rec.rows[0].CCFISC)&&!this.congiunta){this.congiunta=true;this.congiunta_cfisc=rec.rows[0].CCFISC;var localQuery;switch(this.tipo){case 2:localQuery=getQuery(SEL_LOCAL_ANAG,[this.congiunta_cfisc]);break;case 7:case 8:localQuery=getQuery(SEL_LOCAL_TUTORI,[this.congiunta_cfisc]);break;}
var recCongiunta=this.gearsConn.queryBy(localQuery);this.congiunta_nome=recCongiunta.rows[0].NOME;this.congiunta_cognome=recCongiunta.rows[0].COGNOME;this.congiunta_sesso=recCongiunta.rows[0].SS;}
this.fireEvent('load',false)},loadGear:function(){var cfisc=(this.congiunta)?this.congiunta_cfisc:this.cfisc;log.info('Loading '+cfisc);var increment=1/TABELLE_DICHIARAZIONE.length;var messaggio="Caricamento "+((this.congiunta)?"congiunta":"dichiarante")+"...";var progress=Ext.Msg.progress("Caricamento",messaggio,"0%")
var gearsFiller=new Ext.ux.Namirial.data.GearsFiller({gearsConn:this.gearsConn,cfisc:cfisc,tables:TABELLE_DICHIARAZIONE,url:this.scriptUrl});gearsFiller.on('loadexception',function(e){this.fireEvent('loadexception',e,true);},this)
gearsFiller.on('loadtable',function(filler,table){var index=TABELLE_DICHIARAZIONE.indexOf(table);var val=(index+1)*increment;var perc=(val*100).toFixed(0);progress.updateProgress(val,perc+"%",messaggio);},this)
gearsFiller.on('load',function(){try{var rec=this.gearsConn.queryBy(getQuery(SEL_LOCAL_ANAG,[cfisc]));eval('this.'+((this.congiunta)?'congiunta_':'')+'nome = rec.rows[0].NOME');eval('this.'+((this.congiunta)?'congiunta_':'')+'cognome = rec.rows[0].COGNOME');eval('this.'+((this.congiunta)?'congiunta_':'')+'sesso = rec.rows[0].SS');if(!this.congiunta){this.recDic=rec.rows[0];var recSostituto=this.gearsConn.queryBy('SELECT SOSTIMP, SUBCODICE FROM ANAGRAFI WHERE CFISC = \''+cfisc+'\'');var gearsFillerSostituti=new Ext.ux.Namirial.data.GearsFiller({gearsConn:this.gearsConn,url:this.scriptUrl});gearsFillerSostituti.on('loadsingle',function(){if(!Ext.isEmpty(rec.rows[0].CCFISC)){this.tipo=parseInt(rec.rows[0].ST);this.congiunta=true;this.congiunta_cfisc=rec.rows[0].CCFISC;switch(this.tipo){case 2:this.loadGear();break;case 7:case 8:this.loadTutore();break;default:log.timeEnd('gearsfiller');this.confirmLock(session.userLogged,function(){this.fireEvent('load',false,this.congiunta);})
break;}}
else{log.timeEnd('Loading of '+cfisc);this.confirmLock(session.userLogged,function(){this.fireEvent('load',false,this.congiunta);})}},this,{single:true})
var sost=conn.query(getQuery(SEL_SOSTITUTO,[recSostituto.rows[0].SOSTIMP,recSostituto.rows[0].SUBCODICE]));if(sost.totalRows>0){conn.exec(getQuery(DEL_SOSTITUTI,[recSostituto.rows[0].SOSTIMP,((Ext.isEmpty(recSostituto.rows[0].SUBCODICE))?('IS NULL'):("= '"+recSostituto.rows[0].SUBCODICE+"'"))]));gearsFillerSostituti.fireEvent('loadsingle');}
gearsFillerSostituti.loadSingle({table:'SOSIMPW',fields:structStore.getFields('SOSIMPW'),filter:'[S4] = \''+recSostituto.rows[0].SOSTIMP+'\' AND ISNULL([SUBCODICE], \'\') = \''+(recSostituto.rows[0].SUBCODICE||'')+'\''})}else{log.timeEnd('Loading of '+cfisc);this.confirmLock(session.userLogged,function(){this.fireEvent('load',false,this.congiunta);})}}catch(e){this.fireEvent('loadexception',e,true)}},this,{single:true})
log.time('Loading of '+cfisc);gearsFiller.load();},loadTutore:function(){var gearsFillerTutori=new Ext.ux.Namirial.data.GearsFiller({gearsConn:this.gearsConn,url:this.scriptUrl});gearsFillerTutori.on('loadsingle',function(){var recTutore=this.gearsConn.queryBy('SELECT * FROM TUTORI WHERE CFISC = \''+this.congiunta_cfisc+'\'');this.congiunta_nome=recTutore.rows[0].NOME;this.congiunta_cognome=recTutore.rows[0].COGNOME;this.congiunta_sesso=recTutore.rows[0].SS;log.timeEnd('gearsfiller');this.confirmLock(session.userLogged,function(){this.fireEvent('load',false,this.congiunta);})},this)
gearsFillerTutori.loadSingle({table:'TUTORI',fields:structStore.getFields('TUTORI'),filter:'[CFISC] = \''+this.congiunta_cfisc+'\''})},checkLock:function(callback){log.info('Check lock '+this.cfisc);callback=callback||false;Ext.Ajax.request({url:this.dataUrl,params:{s:getQuery(sp_ANAGRAFI_PERMISSIONS,[session.userLogged,this.cfisc,'',(session.powerUser)?session.caf:'']),db:session.userDb},scriptTag:true,success:function(response){this._responseCheckLock(response,callback);},failure:function(){this.fireEvent('checklockexception');},scope:this});},lock:function(utente,callback,coniuge){callback=callback||false;coniuge=coniuge||false;var cfisc=this.cfisc
if(coniuge){cfisc=this.congiunta_cfisc}
log.info('LOCK DICHIARAZIONE | '+cfisc);Ext.Ajax.request({method:'POST',url:WRITEDATAURL,params:{s:getQuery(INS_LOCK,[cfisc,utente,session.serialID,session.computer,session.browser,'']),db:session.userDb},scriptTag:true,success:function(response){var responseObj=Ext.util.JSON.decode(response.responseText);if(responseObj.Success=='True'){log.info('Lock '+this.cfisc+' success');if(callback)
callback.call(this,callback);}else{log.error('Errore nel lock: '+unescape(responseObj.ErrorMessage));this.fireEvent('lockedexception');}},failure:function(response){log.warn('Lock exception '+this.cfisc);this.fireEvent('lockedexception');},scope:this});},confirmLock:function(utente,callback,coniuge){callback=callback||false;coniuge=coniuge||false;var cfisc=this.cfisc
if(coniuge){cfisc=this.congiunta_cfisc}
log.info('CONFERMA LOCK DICHIARAZIONE | '+cfisc);Ext.Ajax.request({method:'POST',url:WRITEDATAURL,params:{s:getQuery(CONFIRM_LOCK,[getDataFormat(),cfisc,utente]),db:session.userDb},scriptTag:true,success:function(response){var responseObj=Ext.util.JSON.decode(response.responseText);if(responseObj.Success=='True'){log.info('Lock '+this.cfisc+' confermato');this.fireEvent('lock');if(callback)
callback.call(this,callback);}else{log.error('Errore nella conferma del lock: '+unescape(responseObj.ErrorMessage));this.fireEvent('lockedexception');}},failure:function(response){log.warn('Lock exception '+this.cfisc);this.fireEvent('lockedexception');},scope:this});},unlock:function(callback,coniuge){callback=callback||false;coniuge=coniuge||false;var cfisc=this.cfisc
if(coniuge){cfisc=this.congiunta_cfisc}
log.info('UNLOCK DICHIARAZIONE | '+cfisc)
Ext.Ajax.request({url:WRITEDATAURL,params:{s:getQuery(DEL_LOCK,[cfisc]),db:session.userDb},scriptTag:true,success:function(response){var responseObj=Ext.util.JSON.decode(response.responseText);if(responseObj.Success=='True'){log.info('Unlock '+this.cfisc+' success');this.fireEvent('unlock');if(callback)
callback.call(this,callback);}else{log.error('Errore nell\'unlock: '+unescape(responseObj.ErrorMessage));this.fireEvent('lockedexception');}},failure:function(){this.fireEvent('unlockedexception');},scope:this});},_responseCheckLock:function(response,callback){callback=callback||false;var locked=false;var lockresponse=Ext.util.JSON.decode(response.responseText);if(lockresponse.totalRows==0){this.fireEvent('locked');this.fireEvent('loadexception','Errore checklock',true);return false;}
var rec=lockresponse.rows[0];var lock=unescape(rec.LOCKED);if(!session.powerUser&&rec.CAF){locked=true;}else{if(session.powerUser&&(rec.CAF!=session.caf)){locked=true;}else{if(lock!=''){if(lock!=session.userLogged){locked=true;}else{if(rec.SERIALID!=session.serialID){locked=true;}}}}}
if(locked){this.fireEvent('locked');this.fireEvent('load',true);}else{this.fireEvent('notlocked');if(callback)callback();}},getCurrentCfisc:function(){return(this.lastType==DICHIARANTE)?this.cfisc:this.congiunta_cfisc;},getTipo:function(){if(this.lastType==DICHIARANTE){return'Dichiarante'}else{return this.getTipoCongiunta();}},getTipoCongiunta:function(){switch(dichiarazione.tipo){case 2:return'Coniuge'
break;case 7:return'Tutore'
break;case 8:return'Rappresentate'
break;default:return''
break;}}})

