#include 
#include 
#include 
#include 
#include 
#include 
#include "at1.h"
#include "lf.h"				// se non lo metto non riesco a usare gli LF
// nomi campi maschera
#include "at1200a.h"
          
// nomi dei campi
#include "soggetti.h"
#include "donaz.h"
#include "benem.h"
#include "sezioni.h"
class TAttriben_app : public TPrintapp
{
	TMask*						_msk;
	TRelation*   			_rel;
	
	TLocalisamfile*		_donaz;
	TLocalisamfile* 	_benem;
	TRecord_array*		_sdonazioni;
	TRecord_array* 		_sbenemerenze;
	int 							_cur;	
	TDate							_data_attribuzione;	// data attribuzione benemerenza
	TDate							_data_stampa;
	TString16					_tipoben;						// tipo benemerenza da assegnare
	int								_numdon;						// numero donazioni necessarie
	TString16					_codsez, _codsot;
  TParagraph_string _cognome_nome;
	static bool filter_func_attriben(const TRelation* rel);
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 TMask& get_mask() { return *_msk; }
	
public:
  void filtra_sezioni();
  void crea_intestazione();
  void header_sezione(const TString16 codsez, const TString16 codsot);
	TAttriben_app() : _data_attribuzione(TODAY), _data_stampa(TODAY), _tipoben(""), _cognome_nome("",25) {}
};
HIDDEN inline TAttriben_app& app() { return (TAttriben_app&) main_app(); }
void TAttriben_app::filtra_sezioni()
{
 	const TString16 sezini = _msk->get(F_SEZINI);
 	const TString16 sotini = _msk->get(F_SOTINI);
 	const TString16 sezfin = _msk->get(F_SEZFIN);
 	const TString16 sotfin = _msk->get(F_SOTFIN);
  TRectype da(LF_SOGGETTI);
  TRectype a(LF_SOGGETTI);   
  if (sezini.not_empty())
		da.put(SOG_CODSEZ, sezini);
  if (sotini.not_empty())
		da.put(SOG_CODSOT, sotini);
  if (sezfin.not_empty())
		a.put(SOG_CODSEZ, sezfin);
  if (sotfin.not_empty())
		a.put(SOG_CODSOT, sotfin);
	current_cursor()->setregion(da, a);
}	
bool TAttriben_app::preprocess_page(int file, int counter)
{
	TString80 nome = current_cursor()->curr().get(SOG_COGNOME);
	nome << " ";
	nome << current_cursor()->curr().get(SOG_NOME);
	_cognome_nome = nome;
	TRectype& recsez = current_cursor()->curr(LF_SEZIONI);
	TLocalisamfile& sogg = current_cursor()->file(LF_SOGGETTI);
	const int totdon = sogg.get_int(SOG_TOTDON);
	// salto pagina se cambio sezione
	const TString16 codsez = current_cursor()->curr().get(SOG_CODSEZ);		
	const TString16 codsot = current_cursor()->curr().get(SOG_CODSOT);		
	if ((_codsez!=codsez)||(_codsot!=codsot))
	{
		if (_codsez != "**")
			printer().formfeed();
		_codsez = codsez;
		_codsot = codsot;
		header_sezione(codsez, codsot);
	}
  return TRUE;
}
void TAttriben_app::set_page(int file, int cnt)
{ 
	set_row(1,"@0g@pn", FLD(LF_SOGGETTI,SOG_CODICE,"######"));
	set_row(1,"@7g@S", FLD(LF_SOGGETTI,SOG_CATDON));
	set_row(1,"@10g@S", FLD(LF_SOGGETTI,SOG_COGNOME));
	set_row(1,"@36g@S", FLD(LF_SOGGETTI,SOG_NOME));   
	
	set_row(1,"@0g@pn", FLD(LF_SOGGETTI,SOG_CODICE,"######"));
	set_row(1,"@7g@S", FLD(LF_SOGGETTI,SOG_CATDON));
	set_row(1,"@10g#a", &_cognome_nome);
	set_row(1,"@36g@ld", FLD(LF_SOGGETTI,SOG_DATANASC));
	set_row(1,"@47g@pn", FLD(LF_SOGGETTI,SOG_TOTDON,"###"));
}
bool TAttriben_app::filter_func_attriben(const TRelation* rel)
{   
	bool filtrato;
	// filtro chi non ha già ricevuto la benemerenza richiesta	
	TLocalisamfile ben(LF_BENEM);
	ben.setkey(3);	
	TRectype& recben = ben.curr();
	recben.zero();
	recben.put(BEN_CODICE, rel->lfile().curr().get_long(SOG_CODICE));
	recben.put(BEN_TIPOBEN, app()._tipoben);
	filtrato = (ben.read() != NOERR);	// se ha gia' la ben. non va filtrato
	// filtro chi ha il numero donazioni giuste
	if (filtrato)
  	filtrato = rel->lfile().curr().get_int(SOG_TOTDON) >= app()._numdon;
	return filtrato;
}
void TAttriben_app::header_sezione(const TString16 codsez, const TString16 codsot)
{
	const TString80 densez = current_cursor()->curr(LF_SEZIONI).get(SEZ_DENSEZ);
	const TString80 densot = current_cursor()->curr(LF_SEZIONI).get(SEZ_DENSOT);
	TString intestazione(132);
	intestazione = "Sezione: ";
	intestazione << codsez;
	intestazione << "/";
	intestazione << codsot;
	intestazione << " ";
	intestazione << densez;
	if ((densot.ok())&& (densot.not_empty()))
	{
		intestazione << "/";
		intestazione << densot;
	}		
	intestazione.center_just();
	set_header(1,"@0g%s", (const char*) intestazione);
	return;
}
bool TAttriben_app::set_print(int)
{
  KEY tasto;
  tasto = _msk->run();
	if (tasto == K_ENTER)
	{  	 
		_codsez = "**";
		_codsot = "**";
		_data_attribuzione = _msk->get(F_DATA);
		_tipoben = _msk->get(F_TIPOBEN);
		TTable bnz("BNZ");
		bnz.put("CODTAB",(const char*) _tipoben);
		if (bnz.read() == NOERR)
			_numdon = bnz.get_int("I0");
		else
			return FALSE;
    reset_files(); 
    add_file(LF_SOGGETTI);
		filtra_sezioni();
		current_cursor()->setfilter("TOTDON!=0");
		current_cursor()->set_filterfunction(filter_func_attriben,TRUE);
		reset_print();		
    crea_intestazione();
    return TRUE;
  }
  else
    return FALSE;
}
void TAttriben_app::crea_intestazione()
{
	reset_header();
	TString sep(132);
  sep = "ATTRIBUZIONE BENEMERENZA ";
  sep << _tipoben;
  const TString80 descr = _msk->get(F_D_TIPOBEN);
  sep << " ";
  sep << descr;
  sep << " ALLA DATA ";
	TString16 data_stampa = _data_attribuzione.string();
	sep << data_stampa;
	sep.center_just();
 	set_header(2, "@0g%s", (const char*) sep);
 	data_stampa = _data_stampa.string();
  set_header(2,"@0g%10s", (const char*) data_stampa);
  sep = "";
  sep << "Pag. @#";
  set_header(2, "@120g%s", (const char*) sep);  
  	
  sep = "";
  sep.fill('-');
  set_header(3, (const char *) sep);
 	set_header(4,"@0gCod.@7gC.@10gCognome e nome@36gNato il@47Num.don.");
 	set_header(5,"@0g------@7g--@10g-------------------------@36g----------@47g--------");
}
bool TAttriben_app::user_create()
{
	_msk = new TMask("at1200a");
	_rel = new TRelation(LF_SOGGETTI);
	_rel->add(LF_SEZIONI,"CODSEZ==CODSEZ|CODSOT==CODSOT");
  _cur = add_cursor(new TCursor(_rel, "", 3));	//cursore ordinamento per sezione+sottogruppo+codice
	
	_donaz = new TLocalisamfile(LF_DONAZ);
	_benem = new TLocalisamfile(LF_BENEM);
	_sdonazioni = new TRecord_array(LF_DONAZ,DON_PROGDON);
	_sbenemerenze = new TRecord_array(LF_BENEM, BEN_PROGBEN);
	return TRUE;
}	
bool TAttriben_app::user_destroy()	
{
	delete _rel;
	delete _msk;
	delete _donaz;
	delete _benem;
	delete _sdonazioni;
	delete _sbenemerenze;
	return TRUE;
}
int at1200(int argc, char* argv[])
{
	TAttriben_app a;
	a.run(argc, argv, "Attribuzione benemerenze");
	return 0;
}