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

#include "at0.h"

// nomi campi maschera
#include "at0200a.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 TGiornalieroDC : 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;
	// array dei soggetti inseriti per controllare doppio inserimento
	TAssoc_array*		_asoggetti;
	bool						_modified, _ricerca;
	long						_progins;	                         
	TDate						_datadon;
	TString16				_tipodon, _luogodon;
	// 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, _insdaticompl;
	int							_cc;
	
	
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; }
	
	TAssoc_array& get_array_sogg() { return *_asoggetti; }
	int write(TSheet_field& s);
	int read(TSheet_field& s);
	bool check_sog_sheet(const char* codsog);	
	static bool nome_handler(TMask_field& f, KEY k);
	static bool codice_handler(TMask_field& f, KEY k);
	static bool codct_handler(TMask_field& f, KEY k);
	static bool tipodon_handler(TMask_field& f, KEY k);
	static bool soggetti_notify(TSheet_field& s, int r, KEY k);
	void add_rows_soggetti(TSheet_field& s, int count = 20, int start = 0);
	static bool ins_controlli_handler(TMask_field& f, KEY k);

public:
	TGiornalieroDC() {}
	
};

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

bool TGiornalieroDC::ins_controlli_handler(TMask_field& f, KEY  k)
{
	if (k == K_SPACE)
	{
		TMask& m = f.mask();
		TSheet_field& s = (TSheet_field&)m.field(F_SOGGETTI);
		const bool ins_controlli = m.get_bool(F_INS_CONTROLLI);
		s.enable_column(5,ins_controlli);
	}		
	return TRUE;	
}

void TGiornalieroDC::add_rows_soggetti(TSheet_field& s, int count, int start)
{
	if (start == 0)
		s.destroy();
	for (int r=start; r<=start+count-1; r++)
		s.row(r);
}
	
bool TGiornalieroDC::create()
{
	TApplication::create();
	_modified = FALSE;
	_ricerca = FALSE;
	_msk = new TMask("at0200a");
	_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);
	_asoggetti = new TAssoc_array();
	_msk->set_handler(F_INS_CONTROLLI,ins_controlli_handler);
	TSheet_field& ss = (TSheet_field&)_msk->field(F_SOGGETTI);
	ss.set_notify(soggetti_notify);
	ss.sheet_mask().set_handler(F_S_NOME,nome_handler);
	ss.sheet_mask().set_handler(F_S_TESSAVIS,nome_handler);
	ss.sheet_mask().set_handler(F_S_CODICE,codice_handler);
	ss.sheet_mask().set_handler(F_S_CODCT,codct_handler);
	ss.sheet_mask().set_handler(F_S_TIPODON,tipodon_handler);
  TConfig config(CONFIG_STUDIO);
  _autoid = config.get_bool("AutoId");
  _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");
  _insdaticompl = config.get_bool("InsDatiCompl");
  _donaz->setkey(3);
	_donaz->last();
	_progins = _donaz->get_long(DON_PROGINS);
	_donaz->setkey(1);
  dispatch_e_menu(BAR_ITEM(1));
	return TRUE;
}	

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

bool TGiornalieroDC::menu(MENU_TAG m)
{ 
	TMask& msk = get_mask();
	const TDate oggi(TODAY);
	KEY tasto;
	do
	{
		TSheet_field& s = (TSheet_field&)msk.field(F_SOGGETTI);
		if (s.items() == 0)
			add_rows_soggetti(s);
		_asoggetti->destroy();
		_ricerca = FALSE;
		if (!_insdaticompl)
		{
		  msk.hide(-2);
			TSheet_field& s = (TSheet_field&)msk.field(F_SOGGETTI);
			s.enable_column(15, _insdaticompl);
			s.enable_column(16, _insdaticompl);
			s.enable_column(17, _insdaticompl);
			s.enable_column(18, _insdaticompl);
			s.enable_column(19, _insdaticompl);
		}	
  	tasto = msk.run();
  	_datadon = msk.get(F_DATADON);
  	_tipodon = msk.get(F_TIPODON);
  	_cc = msk.get_int(F_CC);
  	_luogodon = msk.get(F_LUOGODON);
  	switch (tasto)
  	{
  		case K_F9:		// ricerca
  		{
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroDC::write(s);
				_modified = FALSE;
				if (_datadon.ok())
				{
					_ricerca = TRUE;
					TGiornalieroDC::read(s);
				}					
				else
					error_box("Inserire almeno la data donazione per effettuare la ricerca");					
			}	
  		break;
  	
  		case K_ESC:		// annulla
  		{
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroDC::write(s);
				_modified = FALSE;  					
				msk.reset();
				msk.field(DLG_SAVEREC).enable();
				msk.field(DLG_FINDREC).enable();
				msk.field(DLG_NEWREC).enable(); 
				msk.enable(-1);
			}	
  		break;
  		case K_SAVE:	// registra
  		{
  			TGiornalieroDC::write(s);
				_modified = FALSE;  					
  			//msk.reset(); //verificare se va tolta
  		}  			
  		break;
  		case K_INS:		// nuovo
  		{
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroDC::write(s);
				_modified = FALSE;
				msk.reset();
			}	
  		break;
  		case K_QUIT:	// fine
  		{                   
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroDC::write(s);
				msk.reset();
				_modified = FALSE;  					
  		}
  		break;
  	}	
  }	
	while (tasto != K_QUIT);
  return FALSE;
}  

int TGiornalieroDC::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;
	TRectype r(donaz.curr());
	int items = 0;
  for(donaz.read(); !donaz.eof(); donaz.next())
  {   
    if (donaz.curr() != r) break;
    if (_tipodon.empty() && _luogodon.not_empty())
    {
    	luogodon = donaz.get(DON_LUOGODON);
    	if (luogodon == _luogodon)
    		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));			
			}		
			row.add(donaz.get(DON_CC));		
			row.add(donaz.get(DON_PA));		
			row.add(donaz.get(DON_HB));		
			row.add(donaz.get(DON_SGPT));		
			row.add(donaz.get(DON_PROTIDEMIA));		
			items++;
		}			
	}
	_msk->field(DLG_SAVEREC).disable();
	_msk->field(DLG_FINDREC).disable();
	_msk->field(DLG_NEWREC).disable();
	return items;
}

int TGiornalieroDC::write(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);
					}						
				}						
				_progins++;			
				TRectype* rec = new TRectype(LF_DONAZ);
				rec->put(DON_CODICE, row.get(0));
				rec->put(DON_DATADON, datadonazione);
				TString16 tipodon = row.get(4);
				if (tipodon.blank())
					tipodon = _tipodon;
				rec->put(DON_TIPODON, tipodon);				
				rec->put(DON_LUOGODON, _luogodon);
				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));
				rec->put(DON_PROGINS,_progins);
                                       
				int cc = atoi(row.get(14));
				if (cc == 0)
					cc = _cc;                                       
				rec->put(DON_CC, cc);
				rec->put(DON_PA, row.get(15));
				rec->put(DON_HB, row.get(16));
				rec->put(DON_SGPT, row.get(17));
				rec->put(DON_PROTIDEMIA, row.get(18));

				bool insert = FALSE;
				bool exist = 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 && !insert && !exist)
					{
						const TRectype& riga = _sdonazioni->row(r);
						const TDate d = riga.get(DON_DATADON);
						if (datadonazione==d)	// esiste gia' una donazione in questa data
						{
							exist=TRUE;
							r=0;
						}	 
						else
							if (datadonazione > d)
							{                            
								rec->put(DON_PROGDON,r+1);
								_sdonazioni->insert_row(rec);
								insert=TRUE;
							}
						r--;						
					}
					if (!exist && !insert)
					{
						rec->put(DON_PROGDON,1);
						_sdonazioni->insert_row(rec);
					}	
				}	
				else
			  	if (err == _iseof || err == _isemptyfile)
			  	{
						rec->put(DON_PROGDON,1);
						_sdonazioni->insert_row(rec);		  	
					}
				if (!exist)					
				{	
					err = _sdonazioni->write(TRUE);
					if (err == NOERR)
					{
					  // 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
						
						const char modstatosi = modstato_tcs(statosi);
						const char modstatoaf = modstato_tcs(statoaf);
						id_si = ((modstatosi == 'I') && (intsi != 0));		// il soggetto � idoneo SI
						id_af = ((modstatoaf == 'I') && (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);
							err = _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
						{ 
							const char modstatosi = modstato_tcs(statosi);
							const char modstatoaf = modstato_tcs(statoaf);
							id_si = ((modstatosi == 'I') && (intsi != 0));		// il soggetto � idoneo SI
							id_af = ((modstatoaf == 'I') && (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 ???
	    			TString16 catdon = sog.get(SOG_CATDON);
	    			const TRectype& ctd = cache().get("CTD", catdon);
    				if (ctd.get_bool("B0")) // categoria di dimissione
						{       
							const TString16 catcoll = ctd.get("S6");
							if (catcoll.not_empty())
							{
								sog.put(SOG_CATDON, catcoll);		    				
								sog.put(SOG_DATADIM,NULLDATE);								
								catdon = catcoll;							
							}	
						}							
						if ((catdon == _catini1 || _catini1.empty()) && (totdon+1>=_numdon1) && _catfin1.not_empty())
							sog.put(SOG_CATDON, _catfin1);
						const bool tstampata = sog.get_bool(SOG_T_STAMPATA);
						if ((catdon == _catini2 || _catini2.empty()) && (totdon+1>=_numdon2) && _catfin2.not_empty() && (!_sttess2 || tstampata))
							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 in scrittura archivio soggetti, riga %d", err, rigasog);
							break;
						}
					}					
					else
						error_box("Errore %d in scrittura archivio donazioni, riga %d", err, rigasog);
				}	
			}				
		}														
	}
  delete pi;
	return NOERR;	
}

bool TGiornalieroDC::check_sog_sheet(const char* codsog)
{ 
	TAssoc_array& array_sogg = app().get_array_sogg();
	if (array_sogg.is_key(codsog))
		return FALSE;
	else
		return TRUE;
}		

bool TGiornalieroDC::soggetti_notify(TSheet_field& s, int r, KEY k)
{              
	bool ok = TRUE;
	switch (k)
	{ 
		case K_INS:
			// richiesta inserimento riga
			if (app()._ricerca)
		 		ok = s.error_box("Fase di ricerca: impossibile inserire donazioni");
		break;	
		case K_DEL:
		// richiesta cancellazione riga
		break;
		case K_CTRL+K_DEL:
		// avvenuta cancellazione riga
		break;
		case K_SPACE:
		// inizio modifica riga
		break;
		case K_TAB:
		// entrata riga			
		{ 
		  static bool entering = TRUE;
		  if (entering)
		  {
		    entering = FALSE;
			  if ((r == s.items()-1) && (r == s.first_empty()))
				  app().add_rows_soggetti(s,10,r+1);
			  TToken_string& row = s.row(r);
			  if (row.empty_items())
			  	s.select(r,1,FALSE);			
			  entering = TRUE;	
			}  	
		}
	  break;
		case K_ENTER:
		// uscita da riga modificata
		case K_CTRL+K_TAB:
		// uscita riga
		{ 
			TToken_string& row = s.row(r);
			if (!row.empty_items())
				app()._modified = TRUE;
			const long codsog = row.get_long(0);
			const long etichetta = row.get_long(6);
			if (codsog != 0)
			{
				for (int i=s.items()-1; i>=0; i--)
				{
					if (i != r)
					{
						TToken_string& row = s.row(i); 
						if (codsog == row.get_long(0))
							return s.sheet_mask().field(F_S_CODICE).error_box("Soggetto gia' inserito");
						else
						{                            
							// controllo sull'etichetta
							if (etichetta != 0)							
							{
								if (etichetta == row.get_long(6))
									return s.sheet_mask().field(F_S_ETICHETTA).error_box("Etichetta sacca gia' inserita");
							}
						}							
					}					
				}				
				if (etichetta != 0)
				{
					TLocalisamfile donaz(LF_DONAZ);
					donaz.setkey(5);
					donaz.zero();
					donaz.put(DON_ETICHETTA, etichetta);
					int anno = s.mask().get_date(F_DATADON).year();
				  for(donaz.read(); !donaz.eof(); donaz.next())
				  {                              
				  	const long etic = donaz.get_long(DON_ETICHETTA);
				  	const TDate datadon = donaz.get_date(DON_DATADON);
				  	if (etichetta!=etic) break;
				  	if ((etichetta==etic) && (anno==datadon.year())) return s.sheet_mask().field(F_S_ETICHETTA).error_box("Etichetta sacca gia' inserita");
					}								  	
				}					
			}	
			else
			{
				const char* cognome = row.get(1);
				if ((cognome != NULL) && (cognome != "\0"))
					s.sheet_mask().field(F_S_NOME).set_focusdirty();
			}
		}
	  break;  	
	}  
  return ok;  
}

bool TGiornalieroDC::nome_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if (f.to_check(k))
  {
  	TMask& m = f.mask();
  	long codsog = m.get_long(F_S_CODICE);
  	if (codsog == 0)
      f.on_key(K_F9);
  }	
  return ok;
}

bool TGiornalieroDC::codice_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if ((k==K_TAB) && (f.to_check(k)))
  {                          
  	TString256 messaggio = "";
  	TMask& m = f.mask();
  	long codsog = m.get_long(F_S_CODICE);
  	if (codsog != 0)
  	{
			TLocalisamfile& sog = app().get_relation()->lfile();
  		sog.setkey(1);	
			sog.zero();
			sog.put(SOG_CODICE, codsog);
			int err = sog.read();
			if (err == NOERR)
			{
  			m.set(F_S_COGNOME, sog.get(SOG_COGNOME));
  			m.set(F_S_NOME, sog.get(SOG_NOME));
  			m.set(F_S_DATANASC, sog.get(SOG_DATANASC));
  			m.set(F_S_TESSAVIS, sog.get(SOG_TESSAVIS));
  			m.set(F_S_CODSEZ, sog.get(SOG_CODSEZ));
  			m.set(F_S_CODSOT, sog.get(SOG_CODSOT));
  			m.set(F_S_CATDON, sog.get(SOG_CATDON));
			}  			
			else
				ok = FALSE; // codice non esistente  			
		}  			
  }	
  return ok;
}

bool TGiornalieroDC::codct_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if ((k==K_TAB) && (f.to_check(k)))
  {                          
  	TString256 messaggio = "";
  	TMask& m = f.mask();
  	TString16 codsog = m.get(F_S_CODCT);
  	if (codsog[0] == '0')
  		codsog = "";
  	if (codsog.not_empty() )
  	{
			TLocalisamfile& sog = app().get_relation()->lfile();
  		sog.setkey(7);	
			sog.zero();
			sog.put(SOG_CODCT, codsog);
			int err = sog.read();
			if (err == NOERR)
			{
  			m.set(F_S_CODICE, sog.get(SOG_CODICE));
  			m.set(F_S_COGNOME, sog.get(SOG_COGNOME));
  			m.set(F_S_NOME, sog.get(SOG_NOME));
  			m.set(F_S_DATANASC, sog.get(SOG_DATANASC));
  			m.set(F_S_TESSAVIS, sog.get(SOG_TESSAVIS));
  			m.set(F_S_CODSEZ, sog.get(SOG_CODSEZ));
  			m.set(F_S_CODSOT, sog.get(SOG_CODSOT));
  			m.set(F_S_CATDON, sog.get(SOG_CATDON));
			}  			
			else
				ok = FALSE; // codice non esistente  			
		}  			
  }	
  return ok;
}

bool TGiornalieroDC::tipodon_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if (f.to_check(k))
  {                          
  	TString256 messaggio = "";
  	TMask& m = f.mask();
  	long codsog = m.get_long(F_S_CODICE);
  	if (codsog != 0)
  	{
			TLocalisamfile& sog = app().get_relation()->lfile();
  		sog.setkey(1);	
			sog.zero();
			sog.put(SOG_CODICE, codsog);
			int err = sog.read();
			if (err == NOERR)
			{
  			if (!(app()._ricerca))
  			{
	  			const int totdon = sog.get_int(SOG_TOTDON);
	  			if (totdon == 0)
	  				m.set(F_S_PRIMADON, 'X');
					if (!app()._nomessage)
					{
						TDate datadon = m.get_date(F_S_DATADON);
						if (!datadon.ok())
							datadon = app().get_mask().get_date(F_DATADON);
						TDate dataultdon = sog.get(SOG_DATAULTDON);
						if (datadon > dataultdon)
						{
							const TRectype& ctd = cache().get("CTD", sog.get(SOG_CATDON));
	  					if (ctd.get_bool("B0"))
	  					{
								const TString16 catcoll = ctd.get("S6");
								if (catcoll.empty())
									messaggio << "Soggetto dimesso senza categoria collegata\n";							
								else
									messaggio << "Soggetto dimesso, cambio categoria automatico\n";
							}
							const TString16 stato = sog.get(SOG_STATO);
			  			const char modstato = modstato_tcs(stato);
							if (modstato == 'I' || modstato == 'F')	// il soggetto � idoneo
							{          
								TDate datapros(NULLDATE);
								TString16 tipodon = m.get(F_S_TIPODON);
								if (tipodon.empty())
									tipodon = app().get_mask().get(F_TIPODON);
								if (is_donaz(tipodon,IDON_SI))
									datapros = sog.get_date(SOG_DATAPROSSI);
								else
									datapros = sog.get_date(SOG_DATAPROSAF);											
								if (datapros.empty())
										messaggio << "Soggetto non idoneo al tipo donazione\n";
								else
									if (datapros > datadon)
										messaggio << "Il soggetto ha donato prima della scadenza del " << datapros.string() << "\n";
							}
							else
							{
								messaggio << "Stato soggetto " << stato << " non compatibile con la donazione\n";	
								if (cache().get("TCS", stato).get("S7").empty())
									messaggio << "Verificare la situazione del soggetto";
								else
									messaggio << "Cambio stato automatico";
							}						
						}							
						else
							if (datadon == dataultdon)
								messaggio << "Donazione gia' inserita per il soggetto";
							else
								messaggio << "Attenzione: donazione precedente all'ultima donazione del soggetto";
					}						
				}						
			}  			
			else
				ok = FALSE; // codice non esistente  			
	  	if (messaggio.not_empty() && !app()._ricerca && !app()._nomessage)
  			warning_box(messaggio);
		}  			
  }	
  return ok;
}
		
int at0200(int argc, char* argv[])
{
	TGiornalieroDC a;
	a.run(argc, argv, "Giornaliero donazioni/controlli");
	return 0;
}