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

#include "at0.h"

// nomi campi maschera
#include "at0300a.h"
          
// nomi dei campi
#include "soggetti.h"
#include "donaz.h"
#include "contsan.h"
#include "idoneita.h"

#include "atlib.h"

#define ALIAS_CTD		700

class TGiornalieroC : public TApplication
{
	TMask*					_msk;
	TRelation*   		_rel;
	
  TLocalisamfile* _soggetti;
	TLocalisamfile* _donaz;
	TLocalisamfile* _contsan;
	TLocalisamfile* _idoneita;
	TRecord_array*	_sdonazioni; 
	TRecord_array*	_scontrolli;
	TRecord_array*	_sidoneita;
	TAssoc_array*		_asoggetti;	// array dei soggetti inseriti per controllare doppio ins.
	bool						_modified;
	
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);

	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 tipocon_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);

public:
	TGiornalieroC() {}
	
};

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

void TGiornalieroC::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 TGiornalieroC::create()
{
	TApplication::create();
	_modified = FALSE;
	_msk = new TMask("at0300a");
	_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);
	_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();
	
	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_CODICE,codice_handler);
	ss.sheet_mask().set_handler(F_S_CODCT,codct_handler);
	ss.sheet_mask().set_handler(F_S_TIPOCON,tipocon_handler);
  
  dispatch_e_menu(BAR_ITEM_ID(1));
  
	return TRUE;
}	

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

bool TGiornalieroC::menu(MENU_TAG m)
{ 
	TMask& msk = get_mask();
	const TDate oggi(TODAY);
	KEY tasto;
	do
	{
		TSheet_field& s = (TSheet_field&)msk.field(F_SOGGETTI);
		//s.reset();
		if (s.items() == 0)
			add_rows_soggetti(s);
		_asoggetti->destroy();
		msk.set(F_DATACON,oggi);
  	tasto = msk.run();
  	switch (tasto)
  	{
  		case K_ESC:		// annulla
  		{
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroC::write(s);
				_modified = FALSE;  					
				msk.reset();
			}	
  		break;
  		case K_SAVE:	// registra
  		{
  			TGiornalieroC::write(s);
				_modified = FALSE;  					
  			msk.reset();
  		}  			
  		break;
  		case K_INS:		// nuovo
  		{  
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroC::write(s);
				_modified = FALSE;
				msk.reset();
			}	
  		break;
  		case K_QUIT:	// fine
  		{                   
  			if (_modified)
  				if (yesno_box("Registrare le modifiche?"))
  					TGiornalieroC::write(s);
				msk.reset();
				_modified = FALSE;  					
  		}
  		break;
  	}	
  }	
	while (tasto != K_QUIT);
  return FALSE;
}  

int TGiornalieroC::write(TSheet_field& s)
{ 
	
	const TMask& m = s.mask();
	const TDate datagen = m.get(F_DATACON);
	const TString16 tipogen = m.get(F_TIPOCON);
	const TDate prdatagen = m.get(F_PROSSDATA);
	const TString16 prtipogen = m.get(F_PROSSTIPO);
	const TString16 motivogen = m.get(F_MOTIVO);
	const TString80 respgen = m.get(F_RESPONSAB);
	
  int items = s.items();
  TProgind *pi;
  pi = new TProgind(items,"Registrazione controlli", FALSE, TRUE, 10);
  pi->setstatus(1);
	
	for (int r=1; r<=s.items(); r++)
	{	
		
    pi->addstatus(1);
    
		TToken_string& row = s.row(r-1);
		const long codsog = row.get_long(0);
		if (codsog != 0)
		{
			TLocalisamfile& sog = get_relation()->lfile();
			sog.setkey(1);
			sog.zero();
			sog.put(SOG_CODICE, codsog);
			int err = sog.read();
			if (err == NOERR)
			{ 
				TString16 tipocon = row.get(4);
				TString16 idon1 = row.get(5);
				TString16 idon2 = row.get(6);
				TString16 idon3 = row.get(7);
				TString16 idon4 = row.get(8);
				int intsi = row.get_int(9);
				int intaf = row.get_int(10);
				TString16 prosstipo = row.get(11);
				TDate prossdata = row.get(12);				
				TString16 motivo = row.get(13);
				TString80 resp = row.get(14);
				if (tipocon.blank())
					tipocon = tipogen;
				if (prosstipo.blank())
					prosstipo = prtipogen;
				if (!prossdata.ok())					
					prossdata = prdatagen;
				if (motivo.blank())
					motivo = motivogen;
				if (resp.blank())
					resp = respgen;
				if (!tipocon.blank())					
				{
					TRectype* recc = new TRectype(LF_CONTSAN);
					recc->put(CON_CODICE, row.get(0));
					recc->put(CON_DATACON, datagen);
					recc->put(CON_TIPOCON, tipocon);
					recc->put(CON_IDON1, idon1);
					recc->put(CON_IDON2, idon2);
					recc->put(CON_IDON3, idon3);
					recc->put(CON_IDON4, idon4);
					recc->put(CON_INTSI, intsi);					
					recc->put(CON_INTAF, intaf);					
					recc->put(CON_PROSSTIPO, prosstipo);
					recc->put(CON_PROSSDATA, prossdata);
					recc->put(CON_MOTIVO, motivo);
					recc->put(CON_RESPONSAB, resp);
					if (modstato_tcs(tipocon) == 'I')          
					{                         
						if (idon1.empty() && idon2.empty() && idon3.empty() && idon4.empty() && intsi == 0 && intaf == 0)
						{
							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 (datagen==d)	// esiste gia' un controllo in questa data
							{
								exist=TRUE;
								r=0;
							}	 
							else
								if (datagen > 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* keyi = new TRectype(LF_IDONEITA);
					keyi->put(IDO_CODICE, row.get(0));
					err = _sidoneita->read(keyi);
					con_reord(sog.curr(),_scontrolli,_sidoneita);
					const TDate oggi(TODAY);
				  sog.put(SOG_DATAULTAGG,oggi);
				  sog.put(SOG_UTENULTAGG,user());
				  sog.rewrite();
					const TDate dataprossi = sog.get_date(SOG_DATAPROSSI);
					const TDate dataconv = sog.get_date(SOG_DATACONV);
					const char stato = modstato_tcs(sog.get(SOG_STATO));
					const TString16 statosi = sog.get(SOG_STATOSI);
					// cancellazione data convocazione se sospeso
					// o se � venuto a donare (la dataprossi diventa > della dataconvoc.
					if ((stato == 'S') || (statosi == SOSPENSIONE) || (dataprossi > dataconv))
					{
						const TDate datanulla(NULLDATE);
						sog.put(SOG_DATACONV, datanulla);
						const int numzero = 0;
						sog.put(SOG_NUMCONV,numzero);
						sog.rewrite();
					}	
				}						
			}				
		}														
	}

  delete pi;
	
	return NOERR;	
}

bool TGiornalieroC::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 TGiornalieroC::soggetti_notify(TSheet_field& s, int r, KEY k)
{              
	bool ok = TRUE;
	switch (k)
	{ 
		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);
			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");
					}					
				}				
  			TLocalisamfile contsan(LF_CONTSAN);
  			contsan.setkey(2);
	 			contsan.zero();         
 				TDate datacon = s.mask().get_date(F_DATACON);
 				contsan.put(CON_DATACON,datacon);
				contsan.put(CON_CODICE,codsog);
				if (contsan.read() == NOERR)
					return s.sheet_mask().field(F_S_CODICE).error_box("Il soggetto ha gia' un controllo in data %s", datacon.string());
				else
				{
					TLocalisamfile soggetti(LF_SOGGETTI);
					soggetti.setkey(1);
					soggetti.zero();
					soggetti.put(SOG_CODICE,codsog);					
					if (soggetti.read() == NOERR)
					{
						TString16 statosog = soggetti.get(SOG_STATO);
						const char modstatosog = modstato_tcs(statosog);
						if (modstatosog == 'S')
						{
							contsan.zero();
		 					contsan.put(CON_DATACON,soggetti.get(SOG_DATASTATO));
							contsan.put(CON_CODICE,codsog);
							if (contsan.read() == NOERR)
							{ 
								TString16 motivo = contsan.get(CON_MOTIVO);
								const TRectype& recmot = cache().get("MTC", motivo);
								if (recmot.get_bool("B0"))
									warning_box("Attenzione! Il soggetto e' sospeso, motivo: %s", (const char *) recmot.get("S0"));
							}	
						}	
					}
					TString16 tipocon = row.get(4);
					if (tipocon.blank())
	  				tipocon = s.mask().get(F_TIPOCON);
					const char modstato = modstato_tcs(tipocon);
	  			if ( modstato == 'I' || modstato == 'M')
	  			{
						TString16 idon1 = row.get(5);
						TString16 idon2 = row.get(6);
						TString16 idon3 = row.get(7);
						TString16 idon4 = row.get(8);
						int intsi = row.get_int(9);
						int intaf = row.get_int(10);
						if ((idon1.blank() && idon2.blank() && idon3.blank() && idon4.blank()) || (intsi==0 && intaf==0))
							return s.sheet_mask().field(F_S_CODICE).error_box("Il soggetto non ha idoneita' precedenti; inserire idoneita' e intervalli");
					}	
				}	
			}				
			else
			{
				const char* cognome = row.get(2);
				if ((cognome != NULL) && (cognome != "\0"))
					s.sheet_mask().field(F_S_NOME).set_focusdirty();
					//s.set_focus_cell(r,2);
			}
		}
	  break;  	
	}  
  return ok;  
}

bool TGiornalieroC::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 TGiornalieroC::codice_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if ((k==K_TAB) && (f.to_check(k)))
  {
  	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));
  			m.set(F_S_CODCT, sog.get(SOG_CODCT));
  			m.set(F_S_TOTDON, sog.get(SOG_TOTDON));
				TString16 tipogen = app()._msk->get(F_TIPOCON);
				if (tipogen == "ID")
				{
	  			m.set(F_S_IDON1, sog.get(SOG_IDON1));
	  			m.set(F_S_IDON2, sog.get(SOG_IDON2));
	  			m.set(F_S_IDON3, sog.get(SOG_IDON3));
	  			m.set(F_S_IDON4, sog.get(SOG_IDON4));
	  			m.set(F_S_INTSI, sog.get(SOG_INTSI));
	  			m.set(F_S_INTAF, sog.get(SOG_INTAF));
				}
			}  			
			else
				ok = FALSE; // codice non esistente  			
  	}
  }	
  return ok;
} 

bool TGiornalieroC::codct_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;
  if ((k==K_TAB) && (f.to_check(k)))
  {
  	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));
  			m.set(F_S_TOTDON, sog.get(SOG_TOTDON));
				TString16 tipogen = app()._msk->get(F_TIPOCON);
				if (tipogen == "ID")
				{
	  			m.set(F_S_IDON1, sog.get(SOG_IDON1));
	  			m.set(F_S_IDON2, sog.get(SOG_IDON2));
	  			m.set(F_S_IDON3, sog.get(SOG_IDON3));
	  			m.set(F_S_IDON4, sog.get(SOG_IDON4));
	  			m.set(F_S_INTSI, sog.get(SOG_INTSI));
	  			m.set(F_S_INTAF, sog.get(SOG_INTAF));
				}
			}  			
			else
				ok = FALSE; // codice non esistente  			
  	}
  }	
  return ok;
} 
                                            
bool TGiornalieroC::tipocon_handler(TMask_field& f, KEY k)
{
	bool ok = TRUE;	
  if ((k==K_TAB) && (f.to_check(k)))
	{
  	TMask& m = f.mask();
		TString16 tipo = m.get(F_S_TIPOCON);
		if (tipo.empty())
			tipo = app()._msk->get(F_TIPOCON);
		const char modstato = modstato_tcs(tipo);
	  if ( modstato == 'I' || modstato == 'M')
	  {                   
			TString16 idon1 = m.get(F_S_IDON1);
			TString16 idon2 = m.get(F_S_IDON2);
			TString16 idon3 = m.get(F_S_IDON3);
			TString16 idon4 = m.get(F_S_IDON4);
	  
			// propongo i dati precedenti per idoneita'
			if (idon1.empty() && idon2.empty() && idon3.empty() && idon4.empty())
			{
				TLocalisamfile& sog = app().get_relation()->lfile();
  			sog.setkey(1);	
				sog.zero();
				sog.put(SOG_CODICE, m.get_long(F_S_CODICE));
				int err = sog.read();
				if (err == NOERR)
				{
					m.set(F_S_IDON1,sog.get(SOG_IDON1));
					m.set(F_S_IDON2,sog.get(SOG_IDON2));
					m.set(F_S_IDON3,sog.get(SOG_IDON3));
					m.set(F_S_IDON4,sog.get(SOG_IDON4));
					m.set(F_S_INTSI,sog.get(SOG_INTSI));
					m.set(F_S_INTAF,sog.get(SOG_INTAF));
				}
	  	}
		}			
	}	
	return TRUE;
} 

		
int at0300(int argc, char* argv[])
{
	TGiornalieroC a;
	a.run(argc, argv, "Giornaliero controlli");
	return 0;
}