// stampa allegato soci
#include "77stba.h"
#include "soci.h"

bool AlleSoci_filter_function(const TRelation* rel)
{ 
  TString qual(3);               
  qual = rel->lfile(LF_SOCI).get(SOCI_RICQUAL); 
  return (qual=="A" || qual=="B" || qual=="C");
}

long TAlleSoci::filtra(const long codditta)
{
  TString filtro(24); 
  _codditta=codditta;
  
  TLocalisamfile& fl = cursor()->file(LF_SOCI);
  TRectype da(fl.curr());
  TRectype a(fl.curr());   
  da.zero();
  a.zero();
  da.put(SOCI_CODDITTA, codditta);
  da.put(SOCI_ATTPREC, "A");
  da.put(SOCI_TIPOASOC, "F");
  a.put(SOCI_CODDITTA, codditta);
  a.put(SOCI_ATTPREC, "A");
  a.put(SOCI_TIPOASOC, "F");
  cursor()->setregion(da, a);

  cursor()->set_filterfunction(AlleSoci_filter_function);
  const long items = cursor()->items();
  return items;
}

void TAlleSoci::set_testata(TPrinter& pr)
{
  TPrint_section& header = section('H', first_page);
  header.update();
  const int header_righe = header.height();
  for (int i = 0; i < header_righe; i++)
  	pr.setheaderline(i, header.row(i));	
}

void TAlleSoci::stampa_corpo(TPrinter& pr)
{
  TPrint_section& body = section('B', odd_page);
	_nprog++;  
  TForm_item& item_nprog = body.find_field(1);
  TString snprog(5);
  snprog << _nprog;
  item_nprog.set(snprog);
  
  body.update();       
  const int body_righe = body.height();
  for (int i = 0; i < body_righe; i++)
  {
    pr.print(body.row(i));
    _RigaCorr++;
  }
  if (usa_progind())
    progind()->addstatus(1);
}
 
bool TAlleSoci::print(const long codditta, const long NumFis, const long NumNoFis)
{  
  TCursor* cur = cursor();
  TPrinter& pr = printer();
  
  const long numsoci = filtra(codditta);
  if (numsoci <= 11)
  	return FALSE;

  (*cur)=0L;  
	set_testata(pr);  	
  	
  _RigaCorr=0;
  _nprog=0;

  for ((*cur)=0L; cur->ok(); ++(*cur))
  	stampa_corpo(pr);       
	pr.formfeed();
  close_print();                                      
  return TRUE;
}

bool TStampaAlleSoci::user_create()
{
	_form = new TAlleSoci("77AS", quadro());
	_form->set_cursor(_form->TForm::cursor());
	_cur = _form->cursor();
	_rel = _form->relation();
	return TRUE;
}  

bool TStampaAlleSoci::user_destroy()
{
	delete _form;
	return TRUE;
}