#include <applicat.h>
#include <msksheet.h>
#include <progind.h>
#include <relation.h>
#include <urldefid.h>

#include "at0.h"

// nomi campi maschera
#include "at0400a.h"
#include "atlib.h"
          
// nomi dei campi
#include "soggetti.h"
#include "donaz.h"
#include "contsan.h"
#include "idoneita.h"
#include "rconvoc.h"

#define ALIAS_CTD		700

class TCorreggiDC : public TApplication
{
	TMask*					_msk;
	TRelation*   		_rel;
	
  TLocalisamfile* _soggetti;
	TLocalisamfile* _donaz;
	TLocalisamfile* _contsan;
	TLocalisamfile* _idoneita;
	TLocalisamfile*	_rconvoc;
	TRecord_array*	_sdonazioni; 
	TRecord_array*	_scontrolli;
	TRecord_array*	_sidoneita;
	TDate						_datadon, _ndatadon;
	TString16				_tipodon, _luogodon, _ntipodon, _nluogodon;
	// parametri di sezione
  int							_intsi_f1, _intsi_f2, _intsi_m, _intaf_m, _etadonne;
	int							_numdon1, _numdon2;
	TString16				_catini1, _catfin1, _catini2, _catfin2, _motivoid;
	bool						_sttess2, _dataisc, _nomessage, _autoid;
	
protected:
	virtual bool create();
	virtual bool destroy();
  virtual bool menu(MENU_TAG m);
	virtual TMask& get_mask() { return *_msk; }
	virtual TRelation* get_relation() const { return _rel; }
	int read(TSheet_field& s);
	bool modifica(TSheet_field& s);
	bool elimina(TSheet_field& s);
public:
	TCorreggiDC() {}
	
};

HIDDEN inline TCorreggiDC& app() { return (TCorreggiDC&) main_app(); }

bool TCorreggiDC::create()
{
	TApplication::create();
	_msk = new TMask("at0400a");
	_rel = new TRelation(LF_SOGGETTI);
  _rel->add("CTD", "CODTAB==CATDON",1,0,ALIAS_CTD);
	_donaz = new TLocalisamfile(LF_DONAZ);
	_contsan = new TLocalisamfile(LF_CONTSAN);
	_idoneita = new TLocalisamfile(LF_IDONEITA);
	_rconvoc = new TLocalisamfile(LF_RCONVOC);
	_sdonazioni = new TRecord_array(LF_DONAZ,DON_PROGDON);
	_scontrolli = new TRecord_array(LF_CONTSAN,CON_PROGCON);
	_sidoneita = new TRecord_array(LF_IDONEITA,IDO_PROGIDO);
  TConfig config(CONFIG_STUDIO);
  _numdon1 = config.get_int("NumDon1");
  _numdon2 = config.get_int("NumDon2");
  _catini1 = config.get("CatIni1");
  _catfin1 = config.get("CatFin1");
  _catini2 = config.get("CatIni2");
  _catfin2 = config.get("CatFin2");
  _sttess2 = config.get_bool("StTess2");
  _dataisc = config.get_bool("DataIsc");
  _nomessage = config.get_bool("NoMessage");
  _etadonne = config.get_int("EtaDonne");
  _intsi_f1 = config.get_int("IntSI_F1");
  _intsi_f2 = config.get_int("IntSI_F2");
  _intsi_m = config.get_int("IntSI_M");
  _intaf_m = config.get_int("IntAF_M");
  dispatch_e_menu(BAR_ITEM_ID(1));
	return TRUE;
}	

bool TCorreggiDC::destroy()	
{                          
	delete _sidoneita;
	delete _scontrolli;
	delete _sdonazioni;
	delete _rconvoc;
	delete _idoneita;
	delete _contsan;
	delete _donaz;	
	delete _rel;
	delete _msk;
	return TApplication::destroy();
}

bool TCorreggiDC::menu(MENU_TAG m)
{ 
	TMask& msk = get_mask();
	msk.field(F_MODIFICA).disable();
	msk.field(F_ELIMINA).disable();
	KEY tasto = K_ENTER;
	TSheet_field& s = msk.sfield(F_SOGGETTI);
 	while (tasto != K_QUIT)
 	{
	 	tasto = msk.run();
	 	if (tasto == F_VISUALIZZA)
	 	{
	  	_datadon = msk.get_date(F_DATADON);
	  	_tipodon = msk.get(F_TIPODON);
	  	_luogodon = msk.get(F_LUOGODON);
			int totale = TCorreggiDC::read(s);
			if (totale > 0)
			{
				msk.set(F_TOTALE, totale);
				msk.disable(F_VISUALIZZA);
				msk.enable(F_MODIFICA);
				msk.enable(F_ELIMINA);
			}	
			else
				error_box("ATTENZIONE! Non ci sono donazioni inserite con le caratteristiche richieste");
		}		
		if (tasto == F_MODIFICA)
		{
	  	_ndatadon = msk.get_date(F_NDATADON);
	  	_ntipodon = msk.get(F_NTIPODON);
	  	_nluogodon = msk.get(F_NLUOGODON);
			if (_ndatadon.ok() || !_ntipodon.blank() || !_nluogodon.blank())
			{
 				if (yesno_box("Confermare la modifica dei dati della donazione?"))
					TCorreggiDC::modifica(s);
			}	
			else
				error_box("Inserire almeno un dato da modificare");					
		}
		if (tasto == F_ELIMINA)
		{
			if (yesno_box("Confermare l'eliminazione delle donazioni inserite?"))
				TCorreggiDC::elimina(s);
		}
		if (tasto == F_MODIFICA || tasto == F_ELIMINA)
		{
			msk.reset();
			msk.field(F_MODIFICA).disable();
			msk.field(F_ELIMINA).disable();
			msk.field(F_MODIFICA).hide();
			msk.field(F_ELIMINA).hide();
			msk.enable(F_VISUALIZZA);
		}
  }	
 	return FALSE;
}  

bool TCorreggiDC::elimina(TSheet_field& s)
{                    
	TMask& m = s.mask();
  int items = s.items();
  TProgind *pi;
  pi = new TProgind(items,"Eliminazione donazioni", FALSE, TRUE, 10);
  pi->setstatus(1);
	TLocalisamfile& sog = get_relation()->lfile();
	TLocalisamfile rconv(LF_RCONVOC);
	for (int rigasog=1; rigasog<=s.items(); rigasog++)
	{	
    pi->addstatus(1);
		TToken_string& row = s.row(rigasog-1);
		const long codsog = row.get_long(0);
		if (codsog != 0)
		{
			sog.setkey(1);
			sog.zero();
			sog.put(SOG_CODICE, codsog);
			int err = sog.read();
			if (err == NOERR)
			{ 
				bool deleted = FALSE;
				TRectype* key = new TRectype(LF_DONAZ);
				key->put(DON_CODICE, row.get(0));
				err = _sdonazioni->read(key);
				if (err == NOERR)
				{                           
					int r=_sdonazioni->rows();
					while (r>=1 && !deleted)
					{
						const TRectype& riga = _sdonazioni->row(r);
						const TDate d = riga.get(DON_DATADON);
						if (_datadon==d)
						{
							_sdonazioni->destroy_row(r,TRUE);
							deleted=TRUE;
							r=0;
						}	 
						r--;						
					}
				}	
				if (deleted)					
				{	
					err = _sdonazioni->write(TRUE);
					calcola_donazioni_lib(sog.curr(),_sdonazioni);
				  // aggiorno data e utente ultimo aggiornamento
				  const TDate oggi(TODAY);
				  sog.put(SOG_DATAULTAGG,oggi);
				  sog.put(SOG_UTENULTAGG,user());
				  if (err == NOERR)
						sog.rewrite();
					else
					{
						error_box("Errore %d durante la scrittura della riga %d", err, rigasog);
						break;
					}
				}					
			}				
		}														
	}
  delete pi;
	return TRUE;
}	

bool TCorreggiDC::modifica(TSheet_field& s)
{                    
	s.destroy();           
	TLocalisamfile donaz(LF_DONAZ);
	TLocalisamfile soggetti(LF_SOGGETTI);
	soggetti.setkey(1);
	donaz.setkey(4);
	donaz.zero();
	donaz.put(DON_DATADON, _datadon);
	if (_tipodon.not_empty())
	{
		donaz.put(DON_TIPODON, _tipodon);
		if (_luogodon.not_empty())			
			donaz.put(DON_LUOGODON, _luogodon);
	}			
	TString16 luogodon, tipodon;
	TDate datadon;
	bool leggi = TRUE;
	const TRectype r(donaz.curr());
	donaz.read(_isgteq)  ;
  while (!donaz.eof() && (donaz.curr() == r))
  {     
   	luogodon = donaz.get(DON_LUOGODON);
   	tipodon = donaz.get(DON_TIPODON);
   	datadon = donaz.get(DON_DATADON);
   	if (_luogodon != luogodon || _tipodon != tipodon || _datadon != datadon) break;
    if (_tipodon.empty())
    {
    	if (luogodon == _luogodon)
    		leggi = TRUE;
			else
				leggi = FALSE;    		
    }	
    else
    {
    	if (_luogodon.empty())
    	{
    		if (luogodon.empty())
					leggi = TRUE;    
			  else
			  	leggi = FALSE;		
			}
			else
				leggi = TRUE;	  	
		}		
		if (leggi)
		{			      
			if (!_nluogodon.blank())
			{
				donaz.put(DON_LUOGODON, _nluogodon);
			  donaz.rewrite();
			} 
		}
		donaz.zero();
		donaz.put(DON_DATADON, _datadon);
		if (_tipodon.not_empty())
		{
			donaz.put(DON_TIPODON, _tipodon);
			if (_luogodon.not_empty())			
				donaz.put(DON_LUOGODON, _luogodon);
		}			
		donaz.read(_isgteq);
	}		
	return TRUE;
}	

int TCorreggiDC::read(TSheet_field& s)
{                    
	s.destroy();
	TLocalisamfile donaz(LF_DONAZ);
	TLocalisamfile soggetti(LF_SOGGETTI);
	soggetti.setkey(1);
	donaz.setkey(4);
	donaz.zero();
	donaz.put(DON_DATADON, _datadon);
	if (_tipodon.not_empty())
	{
		donaz.put(DON_TIPODON, _tipodon);
		if (_luogodon.not_empty())			
			donaz.put(DON_LUOGODON, _luogodon);
	}			
	TString16 luogodon;
	bool leggi = TRUE;
	const TRectype r(donaz.curr());
	int items = 0;
  for(donaz.read(_isgteq); !donaz.eof(); donaz.next())
  {   
    if (donaz.curr() != r) break;
    if (_tipodon.empty())
    {
    	luogodon = donaz.get(DON_LUOGODON);
    	if (luogodon == _luogodon)
    		leggi = TRUE;
			else
				leggi = FALSE;    		
    }	
    else
    	if (_luogodon.empty())
    	{
    		luogodon = donaz.get(DON_LUOGODON);
    		if (luogodon.empty())
    			leggi = TRUE;
    		else
    			leggi = FALSE;
    	}
    	else	
				leggi = TRUE;    
		if (leggi)
		{			
			TToken_string& row = s.row(items);
			row.add(donaz.get(DON_CODICE));
  		soggetti.zero();
  		soggetti.put(SOG_CODICE,donaz.get(DON_CODICE));
  		int err = soggetti.read();
  		if (err == NOERR)
			{
				row.add(soggetti.get(SOG_COGNOME));
				row.add(soggetti.get(SOG_NOME));			
				row.add(soggetti.get(SOG_TESSAVIS));			
			}				
			else
			{
				row.add("");
				row.add("");
				row.add("");
			}
			row.add(donaz.get(DON_TIPODON));			
			row.add("");
			row.add(donaz.get(DON_ETICHETTA));
			row.add(donaz.get(DON_PRIMADON));		
			row.add("");		// data donazione se diversa
			if (err == NOERR)
			{
				row.add(soggetti.get(SOG_DATANASC));
				row.add(soggetti.get(SOG_CODSEZ));			
				row.add(soggetti.get(SOG_CODSOT));			
				row.add(soggetti.get(SOG_CATDON));			
				row.add(soggetti.get(SOG_INTSI));			
			}		
			items++;
		}			
	}
	return items;
}

/*
int TCorreggiDC::modifica(TSheet_field& s)
{ 
	TMask& m = s.mask();
	m.disable(-1);
  int items = s.items();
  TProgind *pi;
  pi = new TProgind(items,"Registrazione donazioni e controlli", FALSE, TRUE, 10);
  pi->setstatus(1);
	TLocalisamfile& sog = get_relation()->lfile();
	TLocalisamfile rconv(LF_RCONVOC);
	for (int rigasog=1; rigasog<=s.items(); rigasog++)
	{	
    pi->addstatus(1);
		TToken_string& row = s.row(rigasog-1);
		const long codsog = row.get_long(0);
		if (codsog != 0)
		{
			sog.setkey(1);
			sog.zero();
			sog.put(SOG_CODICE, codsog);
			int err = sog.read();
			if (err == NOERR)
			{ 
				TDate datadonazione = row.get(8);
				if (!datadonazione.ok())
					datadonazione = _datadon;
				TString16 tipocon = row.get(5);
				if (tipocon.blank())
					tipocon = m.get(F_TIPOCON);
				if (tipocon.not_empty())
				{
					TRectype* recc = new TRectype(LF_CONTSAN);
					recc->put(CON_CODICE, row.get(0));
					recc->put(CON_DATACON, datadonazione);
					recc->put(CON_TIPOCON, tipocon);					
					if (modstato_tcs(tipocon) == 'I')
					{
						recc->put(CON_IDON1,sog.get(SOG_IDON1)); 
						recc->put(CON_IDON2,sog.get(SOG_IDON2)); 
						recc->put(CON_IDON3,sog.get(SOG_IDON3)); 
						recc->put(CON_IDON4,sog.get(SOG_IDON4)); 
						recc->put(CON_INTSI,sog.get(SOG_INTSI)); 
						recc->put(CON_INTAF,sog.get(SOG_INTAF)); 
					}
					bool insert = FALSE;
					bool exist =  FALSE;
					TRectype* keyc = new TRectype(LF_CONTSAN);
					keyc->put(CON_CODICE, row.get(0));
					int err = _scontrolli->read(keyc);
					if (err == NOERR)
					{
						int r=_scontrolli->rows();
						while (r>=1 && !insert && !exist)
						{
							const TRectype& riga = _scontrolli->row(r);
							const TDate d = riga.get(CON_DATACON);
							if (datadonazione==d)	// esiste gi� un controllo in questa data
							{
								exist=TRUE;
								r=0;
							}	 
							else
								if (datadonazione > d)
								{                            
									recc->put(CON_PROGCON,r+1);
									_scontrolli->insert_row(recc);
									insert=TRUE;
								}
							r--;						
						}
						if (!exist && !insert)
						{
							recc->put(CON_PROGCON,1);
							_scontrolli->insert_row(recc);
						}
					}	
					else
			  		if (err == _iseof || err == _isemptyfile)
			  		{
							recc->put(CON_PROGCON,1);
							_scontrolli->insert_row(recc);		  	
						}
					if (!exist)					
					{
						_scontrolli->write(TRUE);
						TRectype* keyc = new TRectype(LF_IDONEITA);
						keyc->put(IDO_CODICE, row.get(0));
						int err = _sidoneita->read(keyc);
						con_reord(sog.curr(),_scontrolli,_sidoneita);
					}						
				}						
				TRectype* rec = new TRectype(LF_DONAZ);
				rec->put(DON_CODICE, row.get(0));
				
				rec->put(DON_ETICHETTA, row.get(6));
				rec->put(DON_PRIMADON, row.get(7));				
				
				rec->put(DON_CODSEZ,sog.get(SOG_CODSEZ));
				rec->put(DON_CODSOT,sog.get(SOG_CODSOT));

				bool modified = FALSE;
				bool found = FALSE;
			  TString16 tipodon = row.get(4);
				if (tipodon.blank())
					tipodon = _tipodon;
				TRectype* key = new TRectype(LF_DONAZ);
				key->put(DON_CODICE, row.get(0));
				err = _sdonazioni->read(key);
				if (err == NOERR)
				{
					int r = _sdonazioni->rows();
					while (r>0 && !found)
					{
						const TRectype& riga = _sdonazioni->row(r);
						const TDate d = riga.get(DON_DATADON);
						if ((datadonazione==d) && (riga.get(DON_TIPODON)== tipodon) && (riga.get(DON_LUOGODON)==_luogodon))	// ho trovato la donazione da modificare
						{
							found=TRUE;
						}	 
						r--;						
					}
				}	
				if (found)
				{
					r++;
					TRectype& riga = _sdonazioni->row(r);
					if (riga.get(DON_DATADON).blank())
						datadonazione = _ndatadon;
					if (riga.get(DON_TIPODON).blank())
						tipodon=_ntipodon;
					if (riga.get(DON_LUOGODON).blank())
						luogodon=_nluogodon;
					riga.put(DON_DATADON, datadonazione);
					riga.put(DON_LUOGODON, luogodon);
					riga.put(DON_TIPODON, tipodon);
					err= _sdonazioni.rewrite(); 
				  // controllo se � idoneo
          bool ctrlsi = FALSE; //devo controllare se � idoneo SI
          bool ctrlaf = FALSE; //devo controllare se � idoneo AF
					bool id_si = FALSE;		// il soggetto � idoneo per si?
					bool id_af = FALSE;		// il soggetto � idoneo per af?
					char modstato = ' ';     
					TString16 stato = sog.get(SOG_STATO);	// stato attuale
					TString16 statosi = sog.get(SOG_STATOSI);	// stato attuale SI
					TString16 statoaf = sog.get(SOG_STATOAF);	// stato attuale AF
					TString16 id1 = sog.get(SOG_IDON1);		// idon. 1
					TString16 id2 = sog.get(SOG_IDON2);		// idon. 2
					TString16 id3 = sog.get(SOG_IDON3);		// idon. 3
					TString16 id4 = sog.get(SOG_IDON4);	  // idon. 4
					int intsi = sog.get_int(SOG_INTSI);		// intervallo per SI
					int intaf = sog.get_int(SOG_INTAF);		// intervallo per AF
					//id_si = ((is_idon(id1,id2,id3,id4,IDON_SI) || (statosi == "ID")) && intsi != 0);		// il soggetto � idoneo SI
					//id_af = ((is_idon(id1,id2,id3,id4,IDON_AF) || (statoaf == "ID")) && intaf != 0);		// il soggetto � idoneo AF
					id_si = ((statosi == "ID") && (intsi != 0));		// il soggetto � idoneo SI
					id_af = ((statoaf == "ID") && (intaf != 0));		// il soggetto � idoneo AF
			 
					int totdon = sog.get_int(SOG_TOTDON);
					TDate dataultdon = sog.get_date(SOG_DATAULTDON);
					sog.put(SOG_TOTDON,totdon+1);
					if (datadonazione > dataultdon)
					{            
						sog.put(SOG_DATAULTDON,datadonazione);
						sog.put(SOG_TIPOULTDON,tipodon);
					} 
 					if (is_donaz(tipodon,IDON_SI))
 					{
 						ctrlsi = TRUE;
 						totdon = sog.get_int(SOG_TOTDONSI);
 						sog.put(SOG_TOTDONSI,totdon+1);
 						dataultdon = sog.get_date(SOG_DATAULTSI);
 						if (datadonazione>dataultdon)
 							sog.put(SOG_DATAULTSI,datadonazione);
					}
 					if (is_donaz(tipodon,IDON_AF))
 					{
 						ctrlaf = TRUE;
 						totdon = sog.get_int(SOG_TOTDONAF);
 						sog.put(SOG_TOTDONAF,totdon+1);
 						dataultdon = sog.get_date(SOG_DATAULTAF);
 						if (datadonazione>dataultdon)
 							sog.put(SOG_DATAULTAF,datadonazione);
					}
					dataultdon = sog.get_date(SOG_DATAULTDON);	// data ultima donazione
					const TString16 tipoultdon(sog.get(SOG_TIPOULTDON));	// tipo ultima donazione
					TDate datasi(NULLDATE);		// data prossima si calcolata
					TDate dataaf(NULLDATE);		// data prossima af calcolata
					TDate dataultsi(NULLDATE);	// data ultima donazione si
					
				  modstato = modstato_tcs(stato);
				  // il soggetto � idoneo per il tipo di donazione ?
				  if ((ctrlsi) && (!id_si) && (_autoid) && (modstato!='B'))
				  {
						TRectype* key = new TRectype(LF_IDONEITA);
						long codice = sog.get_long(SOG_CODICE);
						key->put(IDO_CODICE, codice);
						int err = _sidoneita->read(key);
						int progido = _sidoneita->rows()+1;
						TRectype& recido = _idoneita->curr();
						recido.zero();
						recido.put(IDO_CODICE, codice);
						recido.put(IDO_PROGIDO, progido);
						recido.put(IDO_DATAIDO, datadonazione);
						recido.put(IDO_IDO_SOS, "ID");
						recido.put(IDO_TIPOIDO, "SI");
						TString80 resp = user();
						resp << " - HA DONATO OGGI";
						recido.put(IDO_RESPONSAB, resp);
						recido.put(IDO_MOTIVO, _motivoid);
						recido.put(IDO_INTERVALLO, sog.get(SOG_INTSI));				
						if (sog.get_int(SOG_INTSI) == 0)
						{           
							const TString16 sesso = sog.get(SOG_SESSO);
							if (sesso == "2")
							{
								TDate datanasc = sog.get_date(SOG_DATANASC);
								int eta = datadonazione.year() - datanasc.year();						
								if(eta <= _etadonne)
									recido.put(IDO_INTERVALLO, _intsi_f1);
								else
									recido.put(IDO_INTERVALLO, _intsi_f2);
							}	
							else
								recido.put(IDO_INTERVALLO, _intsi_m);
						}					
						_sidoneita->add_row(recido);
						_sidoneita->rewrite();
						TRectype* keyc = new TRectype(LF_CONTSAN);
						keyc->put(CON_CODICE, codice);
						err = _scontrolli->read(keyc);
						con_reord(sog.curr(),_scontrolli,_sidoneita);
						sog.rewrite();
						stato = sog.get(SOG_STATO);	// stato attuale
					  modstato = modstato_tcs(stato);						
					}						
				  if ((ctrlaf) && (!id_af) && (_autoid) && (modstato!='B'))
				  {
						TRectype* key = new TRectype(LF_IDONEITA);
						long codice = sog.get_long(SOG_CODICE);
						key->put(IDO_CODICE, codice);
						int err = _sidoneita->read(key);
						int progido = _sidoneita->rows()+1;
						TRectype& recido = _idoneita->curr();
						recido.zero();
						recido.put(IDO_CODICE, codice);
						recido.put(IDO_PROGIDO, progido);
						recido.put(IDO_DATAIDO, datadonazione);
						recido.put(IDO_IDO_SOS, "ID");
						recido.put(IDO_TIPOIDO, "AF");
						TString80 resp = user();
						resp << " - HA DONATO OGGI";
						recido.put(IDO_RESPONSAB, resp);
						recido.put(IDO_MOTIVO, _motivoid);
						recido.put(IDO_INTERVALLO, sog.get(SOG_INTAF));				
						if (sog.get_int(SOG_INTAF) == 0)
							recido.put(IDO_INTERVALLO, _intaf_m);
						_sidoneita->add_row(recido);
						_sidoneita->rewrite();
						TRectype* keyc = new TRectype(LF_CONTSAN);
						keyc->put(CON_CODICE, codice);
						err = _scontrolli->read(keyc);
						con_reord(sog.curr(),_scontrolli,_sidoneita);
						sog.rewrite();
						stato = sog.get(SOG_STATO);	// stato attuale
					  modstato = modstato_tcs(stato);
					}
					sog.get(SOG_STATO);	// stato attuale
					statosi = sog.get(SOG_STATOSI);	// stato attuale SI
					statoaf = sog.get(SOG_STATOAF);	// stato attuale AF
					id1 = sog.get(SOG_IDON1);		// idon. 1
					id2 = sog.get(SOG_IDON2);		// idon. 2
					id3 = sog.get(SOG_IDON3);		// idon. 3
					id4 = sog.get(SOG_IDON4);	  // idon. 4
					intsi = sog.get_int(SOG_INTSI);		// intervallo per SI
					intaf = sog.get_int(SOG_INTAF);		// intervallo per AF					
				  modstato = modstato_tcs(stato);
					if (modstato == 'I' || modstato == 'F')	// il soggetto � idoneo
					{ 
						//id_si = (is_idon(id1,id2,id3,id4,IDON_SI) && intsi != 0);		// il soggetto � idoneo SI
						//id_af = (is_idon(id1,id2,id3,id4,IDON_AF) && intaf != 0);		// il soggetto � idoneo AF
						id_si = ((statosi == "ID") && (intsi != 0));		// il soggetto � idoneo SI
						id_af = ((statoaf == "ID") && (intaf != 0));		// il soggetto � idoneo AF
						
						if (dataultdon.ok())		// se ha fatto almeno una donazione
						{
							if (is_donaz(tipoultdon,IDON_SI))	// se l'ultima donazione � una SI
							{
								if (id_si) 
								{
									datasi=dataultdon;
									datasi+=intsi;
								}	
				  			if (id_af) 
				  			{
				  				dataaf=dataultdon;
				  				dataaf+=intaf;
				  			}	
				      }
				      if (is_donaz(tipoultdon,IDON_AF))	// se l'ultima donazione � una AF
				      {
				      	dataultsi = sog.get_date(SOG_DATAULTSI);
								if (id_si)
								{
									if (intaf != 0)
									{
										datasi=dataultdon; 
										datasi+=intaf;
									}	
									else
									{
										datasi=dataultdon;
									  datasi+=intsi;    
									}  
									if (dataultsi.ok())
										dataultsi+=intsi;
									if (dataultsi > datasi)
										datasi = dataultsi;
								}
								if (id_af)
								{
									dataaf=dataultdon;
									dataaf+=intaf;
								}					
				      }	
				 		}
				 		else
				 		{
				 			if (id_si)
				 				datasi=sog.get_date(SOG_DATAULTID);
				 			if (id_af)
				 				dataaf=sog.get_date(SOG_DATAULTID); 				
				 		}
					}	
					sog.put(SOG_DATAPROSSI,datasi);
					sog.put(SOG_DATAPROSAF,dataaf);

					// se la data prossima SI � superiore all'ultima convocazione
					// va cancellata, anche dall'archivio convocazioni
					TDate dataprossi = sog.get_date(SOG_DATAPROSSI);
					TDate dataconv = sog.get_date(SOG_DATACONV);
					if (dataprossi >= dataconv)
					{
						rconv.setkey(2);
						rconv.zero();
						rconv.put(RCV_DATACONV, dataconv);
						rconv.put(RCV_CODICE, codsog);
						int err = rconv.read();
						if (err == NOERR)
						{
							rconv.put(RCV_ANNULLATO,TRUE);
							rconv.rewrite();
						}							
						sog.put(SOG_DATACONV, NULLDATE);
						sog.put(SOG_DATAULTSOL, NULLDATE);
						const int zeroconv = 0;
						sog.put(SOG_NUMCONV, zeroconv);
					}
				  // controllo la sua categoria
				  // se dimesso diventa della categoria collegata
				  // se non c'e' la categoria collegata ???
    			TTable ctd("CTD"); 
    			TString16 catdon = sog.get(SOG_CATDON);
    			ctd.put("CODTAB",catdon);
    			if (ctd.read() == NOERR)
    			{
    				bool dimissione = ctd.get_bool("B0");
						if (dimissione)          
						{
							sog.put(SOG_CATDON,ctd.get("S6"));		    				
							if (ctd.get("S6").not_empty())
								catdon = ctd.get("S6");							
							sog.put(SOG_DATADIM,NULLDATE);								
						}							
						if ((catdon == _catini1 || _catini1.empty()) && (totdon+1>=_numdon1) && _catfin1.not_empty())
							sog.put(SOG_CATDON, _catfin1);
						if ((catdon == _catini2 || _catini2.empty()) && (totdon+1>=_numdon2) && _catfin2.not_empty() && (!_sttess2 || dimissione))
							sog.put(SOG_CATDON, _catfin2);
				  }
				  TDate dataisc = sog.get_date(SOG_DATAISC);
				  if (dataisc.ok() && _dataisc)
				  {            
						if (sog.get_date(SOG_DATAPRISI).ok())
							sog.put(SOG_DATAISC,sog.get(SOG_DATAPRISI));
						else
				    {
							const TRectype& riga = _sdonazioni->row(1);
							sog.put(SOG_DATAISC,riga.get(DON_DATADON));
						}							
					}				  	
				  // aggiorno data e utente ultimo aggiornamento
				  const TDate oggi(TODAY);
				  sog.put(SOG_DATAULTAGG,oggi);
				  sog.put(SOG_UTENULTAGG,user());
				  if (err == NOERR)
						sog.rewrite();
					else
					{
						error_box("Errore %d durante la scrittura della riga %d", err, rigasog);
						break;
					}
				}					
			}				
		}														
	}
  delete pi;
	return NOERR;	
} 
*/

int at0400(int argc, char* argv[])
{
	TCorreggiDC a;
	a.run(argc, argv, "Correzione errato giornaliero donazioni");
	return 0;
}