#include 
#include 
#include 
#include 
#include 
#include "donaz.h"
#include "contsan.h"
#include "soggetti.h"
#include "sezioni.h"
#include "at5.h"
#include "at5700a.h"
class TFogliDonazione : public TPrintapp
{
  TRelation*					_rel;
  TMask*      				_msk;
  int									_contatore, _totfinestampa;
  TDate								_dataold, _dataini, _datafin;
  TString16						_tipodon, _luogodon, _luogoold, _sezold;
  TString16						_sezini, _sotini, _sezfin, _sotfin;
  TString16						_tipostampa, _ordinamento;
  
protected:
  virtual bool user_create();
  virtual bool user_destroy();
  virtual bool set_print(int m);
  virtual void set_page(int file, int cnt);
  virtual bool preprocess_page(int file, int counter);
  virtual print_action postprocess_print(int file, int counter);
public:
  void crea_intestazione();
  void header_stampa(const TDate data, const TString16 luogo, const TString16 sezione, const TString16 sottog, const TString16 gruppoazie);
  void footer_stampa();
  void fine_stampa();
  TMask& app_mask() { return *_msk; }
  TFogliDonazione() : _cognome_nome("",35) {}
};
HIDDEN inline TFogliDonazione& app() { return (TFogliDonazione&) main_app(); }
void TFogliDonazione::set_page(int file, int cnt)
{
	set_row(1,"@0g#D", &_contatore);
	set_row(2,"@0g@pn", FLD(LF_SOGGETTI,SOG_CODICE,"########"));
	set_row(1,"@9g@pn", FLD(LF_DONAZ,DON_ETICHETTA,"#########"));
	set_row(1,"@19g@S", FLD(LF_SOGGETTI,SOG_CATDON));
//	set_row(2,"@16g@8,rs", FLD(LF_SOGGETTI,SOG_TESSAVIS));
	set_row(1,"@22g@S", FLD(LF_SOGGETTI,SOG_COGNOME));
	set_row(2,"@22g@S", FLD(LF_SOGGETTI,SOG_NOME));
	set_row(1,"@48g@S", FLD(LF_SOGGETTI, SOG_GRUPPOAB0));
	set_row(1,"@52g@S", FLD(LF_SOGGETTI, SOG_RHANTID));
	set_row(2,"@48g@ld", FLD(LF_SOGGETTI,SOG_DATANASC));
	set_row(1,"@59g@S", FLD(LF_SOGGETTI,SOG_DOM_INDIR));
	set_row(2,"@64g@S", FLD(LF_SOGGETTI,SOG_CODSOT));
	set_row(1,"@64g@ld", FLD(LF_DONAZ,DON_DATADON));
	set_row(2,"@64g@S", FLD(LF_DONAZ,DON_TIPODON));
	set_row(1,"@79g#t", &_controllo);
	set_row(2,"@79g#t", &_prima);
}
bool TFogliDonazione::preprocess_page(int file, int counter)
{
	bool prima = current_cursor()->curr().get_bool(DON_PRIMADON);
	if (prima)
		_prima = "X";
	else
		_prima = " ";		
	// salto pagina se cambio punto di rottura
	switch (_tipostampa[0])
	{
		case 'D':
		{
			const TDate datanew = current_cursor()->curr().get(DON_DATADON);		
			if (datanew != _dataold )
			{
				if (_dataold.ok())
					footer_stampa();
				_dataold = datanew;				
				header_stampa(datanew,"","","","");
				_contatore = 0;
			} 
		}
		break;
		case 'L':
		{
			const TString16 luogonew = current_cursor()->curr().get(DON_LUOGODON);		
			if (luogonew != _luogoold )
			{
				if (_luogoold != "****")
					footer_stampa();
				_luogoold = luogonew;				
				header_stampa(NULLDATE,luogonew,"","","");
				_contatore = 0;
			} 
		}
		break;			
		case 'S':
		{ 
			//TString16 sezione = current_cursor()->curr(LF_SOGGETTI).get(SOG_CODSEZ);		
			//TString16 sottog = current_cursor()->curr(LF_SOGGETTI).get(SOG_CODSOT);		
			TString16 sezione = current_cursor()->curr().get(DON_CODSEZ);		
			TString16 sottog = current_cursor()->curr().get(DON_CODSOT);		
			TString16 seznew = "";
			seznew << sezione;
			seznew << "/";
			seznew << sottog;
			if (seznew != _sezold )
			{
				if (_sezold != "****")
					footer_stampa();
				_sezold = seznew;				
				header_stampa(NULLDATE,"",sezione,sottog,"");
				_contatore = 0;
			} 
		}
		break;			
		case 'G':
		{ 
			TString16 grupponew = current_cursor()->curr(LF_SOGGETTI).get(SOG_GRUPPOAZIE);		
			if (grupponew != _gruppoold )
			{
				if (_gruppoold != "****")
					footer_stampa();
				_gruppoold = grupponew;				
				header_stampa(NULLDATE,"","","",grupponew);
				_contatore = 0;
			} 
		}
		break;			
	}		
	// salto pagina se non ci sono abbastanza righe per il record
	if ((_stampa80) && (printer().rows_left() < 2))
		printer().formfeed();
	_contatore++;
	_totfinestampa++;
  return TRUE;
}
print_action TFogliDonazione::postprocess_print(int file, int counter)
{ 
	if (_contatore > 0)
		footer_stampa();
	if (_totfinestampa > 0 && _contatore != _totfinestampa)
		fine_stampa();				
	return NEXT_PAGE;
}
void TFogliDonazione::footer_stampa()
{
	// stampa totale soggetti a fine pagina
	if (_contatore > 0)
	{
		reset_footer();
		TString sep(80);
		sep.fill('-');
		set_footer(1, (const char *) sep);
		if (_dataini == _datafin)
			set_footer(2,"TOTALE DONAZIONI DEL %s   %d", _dataini.string(), _contatore);
		else                              
		{
			TString d1 = _dataini.string();
			TString d2 = _datafin.string();
			set_footer(2,"TOTALE DONAZIONI DAL %s AL %s   %d", (const char*)d1, (const char*)d2, _contatore);
		}			
		printer().formfeed(); 
		reset_footer();
	}		
}
void TFogliDonazione::fine_stampa()
{
	// stampa totale soggetti a fine stampa
	
	reset_footer();
 	printer().footerlen(20);
	TString sep(80);
	sep.fill('-');
	set_footer(1, (const char *) sep);
	set_footer(2,"DONAZIONI TOTALI STAMPATE %d", _totfinestampa);
	printer().formfeed();
	reset_footer();
}
void TFogliDonazione::header_stampa(const TDate data, const TString16 luogo, const TString16 sezione, const TString16 sottog, const TString16 gruppoazie)
{
	TString intestazione(132);
	intestazione = "STAMPA FOGLI DELLE DONAZIONI";
	switch (_tipostampa[0])
	{
		case 'D':
		{
			intestazione << "PER DATA ";
			intestazione << data;
		}			
		break;
		case 'L':
		{
			intestazione << "PER PUNTO DI PRELIEVO ";
			intestazione << luogo;
			intestazione << " ";
			intestazione << cache().get("LDN", luogo).get("S0");
		}			
		break;
		case 'S':
		{
			intestazione << "PER SEZIONE ";
			intestazione << sezione;
			if (sottog.not_empty())
			{
				intestazione << "/";
				intestazione << sottog;
			}
			TLocalisamfile sez(LF_SEZIONI);
			sez.setkey(1);
			TRectype& recsez = sez.curr();
			recsez.zero();
			recsez.put(SEZ_CODSEZ, sezione);
			recsez.put(SEZ_CODSOT, sottog);
			if (sez.read() == NOERR)
			{
				intestazione << " ";
				intestazione << recsez.get(SEZ_DENSEZ);
				TString80 densot = recsez.get(SEZ_DENSOT);
				if (densot.not_empty())
				{
					intestazione << "/";
					intestazione << densot;
				}
			}			
		}			
		break;
		case 'G':
		{
			intestazione << "PER GRUPPO AZIENDALE ";
			if (gruppoazie.not_empty())
			{
				intestazione << " ";
				intestazione << cache().get("GAZ", gruppoazie).get("S0");
			}		
		}			
		break;
	}		
	if (_stampa80)
		intestazione.center_just(80);
	else		
		intestazione.center_just(132);	
	set_header(1,"@0g%s", (const char*) intestazione);
 	intestazione = "Pag. @#";
 	if (_stampa80)
 		set_header(1, "@73g%s", (const char*) intestazione);  				 	
 	else
 		set_header(1, "@110g%s", (const char*) intestazione);  
	return;
}
bool TFogliDonazione::set_print(int m)
{ 
  KEY tasto;
 	tasto = _msk->run();
 	if (tasto != K_QUIT)
 	{ 
		_sezini = 	_msk->get(F_SEZINI);
 		_sotini = 	_msk->get(F_SOTINI);
		_sezfin = 	_msk->get(F_SEZFIN);
 		_sotfin = 	_msk->get(F_SOTFIN);
		_dataini = 	_msk->get_date(F_DATADA);	
		_datafin = 	_msk->get_date(F_DATAA);	
		_tipodon = 	_msk->get(F_TIPODON);
 		_luogodon =	_msk->get(F_LUOGODON);
		_tipostampa = 
		TString80 chiave = "";
		switch (_tipostampa[0])
		{
			case 'D':
				chiave = "92->DATADON|";
			break;
			case 'L':
				chiave = "92->LUOGODON|";
			break;
			case 'S':
				chiave = "92->CODSEZ|92->CODSOT|";
			break;
			case 'G':
				chiave = "90->GRUPPOAZIE|";
			break;
		}
		switch (_ordinamento[0])
		{
			case 'C':
				chiave << "92->DATADON|UPPER(90->COGNOME)|UPPER(90->NOME)";
			break;
			case 'I':
				chiave << "92->DATADON|92->PROGINS";
			break;
			case 'E':
				chiave << "92->DATADON|92->ETICHETTA";
			break;
			case 'X':
				chiave << "UPPER(90->COGNOME)|UPPER(90->NOME)|92->DATADON";
			break;
			case 'Y':
				chiave << "92->PROGINS|92->DATADON";
			break;
			case 'Z':
				chiave << "92->ETICHETTA|92->DATADON";
			break;
		}
		TString256 filtro = "";			
		TString80 filtrotmp ="";
		// filtro per tipo donazione			
  	if (_tipodon.not_empty())
  		filtro.format("(TIPODON == \"%s\")",(const char*)_tipodon);
  	// filtro per luogo donazione
  	if (_luogodon.not_empty())
  	{
  		filtrotmp.format("(LUOGODON == \"%s\")",(const char*)_luogodon);
  		if (filtro.not_empty())	
				filtro << " && ";
			filtro << filtrotmp;
		}				
		// filtro per prime donazioni
  	if (_primedon)
  	{
 			filtrotmp.format("(PRIMADON == \"X\")");
  		if (filtro.not_empty())	
				filtro << " && ";
			filtro << filtrotmp;
		}				              
		// filtro per gruppo aziendale
  	if (_pergruppo)
  	{ 	
  		if (_gruppoazie.not_empty())
	  		filtrotmp.format("(90->GRUPPOAZIE == \"%s\")",(const char*)_gruppoazie);
			else	  		
	  		filtrotmp.format("(90->GRUPPOAZIE != \"\")");
  		if (filtro.not_empty())	
				filtro << " && ";
			filtro << filtrotmp;
		}		
		TString16 inizio = "";
		inizio << _sezini;
		inizio << _sotini;
		TString16 fine = "";
		fine << _sezfin;
		fine << _sotfin;
		if (inizio.not_empty())
		{
  		if (filtro.empty())
  			filtro = format("(92->CODSEZ+92->CODSOT >= \"%s\")",(const char*)inizio);
			else
			{
				filtro << " && ";
  			filtro << format("(92->CODSEZ+92->CODSOT >= \"%s\")",(const char*)inizio);
			}				
			if (fine.not_empty())
			{
	  		if (filtro.empty())
	  			filtro = format("(92->CODSEZ+92->CODSOT <= \"%s\")",(const char*)fine);
				else
				{
					filtro  << " && ";
	  			filtro << format("(92->CODSEZ+92->CODSOT <= \"%s\")",(const char*)fine);
				}				
			}
		}
		
		// filtro per data
	  TRectype da(LF_DONAZ);
  	TRectype a (LF_DONAZ);
		if (_riepfinale)
		{
			TDate inizio (_dataini);
			TDate fine   (_datafin);
			inizio.set_day(1);
			inizio.set_month(1);
			fine.set_end_month();
			da.put(DON_DATADON, inizio);
			a.put(DON_DATADON, fine);
			
			_giorni.destroy();
			_mesi.destroy();
			_giornifinale.destroy();
			for (int i=0;i<31;i++)
			{
     		_giorni.add(new real(ZERO),i);
     		_giornifinale.add(new real(ZERO),i);
			}	     		
			for (int j=0;j<12;j++)
     		_mesi.add(new real(ZERO),j);
		}
		else
		{			
	  	if (_dataini.ok())
				da.put(DON_DATADON, _dataini);
	  	if (_datafin.ok())
				a.put(DON_DATADON, _datafin);
		}	
		current_cursor()->setregion(da, a);
		current_cursor()->setfilter(filtro, TRUE);
		((TSorted_cursor*)current_cursor())->change_order(chiave);
 		_contatore = 0;
 		_totfinestampa = 0;
		_dataold = NULLDATE;
		_luogoold = "****";
		_sezold = "****";
		_gruppoold = "****";
	  reset_files(); 
  	add_file(LF_DONAZ);
		reset_print();
		printer().footerlen(0);		
		crea_intestazione();
		return TRUE;
  }	            
 	else
  	return FALSE;
}
void TFogliDonazione::crea_intestazione()
{
  reset_header();
  TString sep(132);
  sep = "STAMPA FOGLI DELLE DONAZIONI - ";
	if (_dataini.ok())
	{
		sep << "dal ";
		sep << _dataini.string();
	}
	if (_datafin.ok())
	{
		sep << " al ";
		sep << _datafin.string();
	}
	if (_luogodon.not_empty())
	{		
		sep << "effettuate a ";
		sep << _luogodon;
	}		
	sep.center_just(132);			
	set_header(2,"@0g%s", (const char*) sep);
	
	set_header(3,"@0gProg.@9gEt.sacca@30gCat.@34gTessera@34gCognome e nome@70gNato il@81gSe/So@87gData don.@98gTipo@104gLuogo@110gGr.@114gRh@118gFen.Rh@125gKellDu");
	
	set_header(4,"@0gCodice@7gEt.sacca@17gCodice@26gTessera@34gCognome e nome@70gNato il@81gSe/So@87gData don.@98gTipo@104gLuogo@110gGr.@114gRh@118gFen.Rh@125gKellDu");
}
bool TFogliDonazione::user_create()
{
  _rel = new TRelation(LF_DONAZ);
  _rel->add(LF_SOGGETTI, "CODICE==CODICE");
  _msk = new TMask("at5700a");
  add_cursor(new TSorted_cursor(_rel,"","",2));
  return TRUE;
}
bool TFogliDonazione::user_destroy()
{
  delete _msk;
  delete _rel;
  return TRUE;
}
int at5700(int argc, char* argv[])
{
  TFogliDonazione a;
  a.run(argc, argv, "Stampa fogli di donazioni");
  return 0;
}