// stampa quadro G aggiuntivo
#include "77stba.h"
                            
// ATTENZIONE! cambiare se nel form 77qga.frm 
// cambia il numero identificativo del primo importo
const int PRIMO_IMPORTO = 24;
const int PRIMA_ALIQUOTA = 49;

// determina se codditta necessita dell'all.G
bool TQuadroGAgg::EsisteAllegG(const long codditta)
{
  TString filtr(24); 
  _codditta=codditta;
  TCursor* cur=cursor();
  filtr.format("CODDITTA==%ld", codditta);
  cur->setfilter(filtr, TRUE);   
  const long items = cur->items(); 
  if (!items) 
    return FALSE;

  (*cur)=0L;
  TRectype& rec = cur->curr();
	TToken_string p35a(rec.get("P35A"));    

  for (int i=0; i<20; i++)
  { 
    if (i == 6 || i == 10 || i == 13 || i == 16 || i == 19)
    {                              
    	TString aliquota = p35a.get(i);
    	if (!aliquota.blank())
    	  return TRUE;
    }
  }
  return FALSE;
}

bool TQuadroGAgg::print(const long codditta, const long NumFis, const long NumNoFis)
{  
  TCursor* cur = cursor();
  TPrinter& pr = printer();

  if (!EsisteAllegG(codditta))
    return FALSE;
    
 // MA SERVE???
//	(*cur)=0L;  
  
  TRectype& rec = cur->curr();
	
	TToken_string p33a(rec.get("P33A"));
	TToken_string p33b(rec.get("P33B"));
	TToken_string p35a(rec.get("P35A"));    
  TToken_string p35b(rec.get("P35B"));    
  TToken_string p36a(rec.get("P36A"));        
  TToken_string p36b(rec.get("P36B"));        

  bool bAggiuntivo = FALSE;
                
  TArray importi;
  TArray aliquote;

  real rip_p33a, rip_p33b, rip_p35b, rip_p36a, rip_p36b;
  real tot_p33a, tot_p33b, tot_p35b, tot_p36a, tot_p36b;

  rip_p33a = ZERO;
  rip_p33b = ZERO;
  rip_p35b = ZERO;
  rip_p36a = ZERO;
  rip_p36b = ZERO;

  tot_p33a = ZERO;
  tot_p33b = ZERO;
  tot_p35b = ZERO;
  tot_p36a = ZERO;
  tot_p36b = ZERO;

  for (int i=0; i<20; i++)
  { 
    if (i == 6 || i == 10 || i == 13 || i == 16 || i == 19)
    {                              
    	TString aliquota = p35a.get(i);
    	bAggiuntivo = bAggiuntivo || !aliquota.blank();
			
			real aliq = p35a.get(i);
   		aliquote.add(aliq);
    	
    	real imp = ZERO;
    	if (aliquota.blank())
    	{
    		importi.add(imp);
    		importi.add(imp);
    		importi.add(imp);
    		importi.add(imp);
    		importi.add(imp);
    	}
    	else
    	{
    		imp = p33a.get(i);
    		importi.add(imp);
    		tot_p33a += imp;
    		imp = p33b.get(i);
    		importi.add(imp);
    		tot_p33b += imp;
    		imp = p35b.get(i);
    		importi.add(imp);
    		tot_p35b += imp;
    		imp = p36a.get(i);
    		importi.add(imp);
    		tot_p36a += imp;
    		imp = p36b.get(i);
    		importi.add(imp);
    		tot_p36b += imp;
			}    		
		}
		else    	
		{
			rip_p33a += p33a.get(i);
			rip_p33b += p33b.get(i);
			rip_p35b += p35b.get(i);
			rip_p36a += p36a.get(i);
			rip_p36b += p36b.get(i);
		}
	}

  _RigaCorr=0;
	
	if (bAggiuntivo)
	{
	  TPrint_section& body = section('B', odd_page);

		// calcolo totali finali (tot+rip)
		tot_p33a += rip_p33a;
		tot_p33b += rip_p33b;
		tot_p35b += rip_p35b;
		tot_p36a += rip_p36a;
		tot_p36b += rip_p36b;

		TForm_item& item_tot_p33a = body.find_field(61);
		tot_p33a = tot_p33a / 1000.00;
		tot_p33a.trunc();
		TString stot_p33a (tot_p33a.string());
		item_tot_p33a.set(stot_p33a);

		TForm_item& item_tot_p33b = body.find_field(62);
		tot_p33b = tot_p33b / 1000.00;
		tot_p33b.trunc();
		TString stot_p33b (tot_p33b.string());
		item_tot_p33b.set(stot_p33b);

		TForm_item& item_tot_p35b = body.find_field(63);
		tot_p35b = tot_p35b / 1000.00;
		tot_p35b.trunc();
		TString stot_p35b (tot_p35b.string());
		item_tot_p35b.set(stot_p35b);

		TForm_item& item_tot_p36a = body.find_field(64);
		tot_p36a = tot_p36a / 1000.00;
		tot_p36a.trunc();
		TString stot_p36a (tot_p36a.string());
		item_tot_p36a.set(stot_p36a);

		TForm_item& item_tot_p36b = body.find_field(65);
		tot_p36b = tot_p36b / 1000.00;
		tot_p36b.trunc();
		TString stot_p36b (tot_p36b.string());
		item_tot_p36b.set(stot_p36b);
		
		// riporti
		TForm_item& item_rip_p33a = body.find_field(19);
		rip_p33a = rip_p33a / 1000.00;
		rip_p33a.trunc();
		TString srip_p33a (rip_p33a.string());
		item_rip_p33a.set(srip_p33a);

		TForm_item& item_rip_p33b = body.find_field(20);
		rip_p33b = rip_p33b / 1000.00;
		rip_p33b.trunc();
		TString srip_p33b (rip_p33b.string());
		item_rip_p33b.set(srip_p33b);

		TForm_item& item_rip_p35b = body.find_field(21);
		rip_p35b = rip_p35b / 1000.00;
		rip_p35b.trunc();
		TString srip_p35b (rip_p35b.string());
		item_rip_p35b.set(srip_p35b);

		TForm_item& item_rip_p36a = body.find_field(22);
		rip_p36a = rip_p36a / 1000.00;
		rip_p36a.trunc();
		TString srip_p36a (rip_p36a.string());
		item_rip_p36a.set(srip_p36a);
		
		TForm_item& item_rip_p36b = body.find_field(23);
		rip_p36b = rip_p36b / 1000.00;
		rip_p36b.trunc();
		TString srip_p36b (rip_p36b.string());
		item_rip_p36b.set(srip_p36b);
		 
				 
		for (int j = 0; j < importi.items(); j++)
		{
			TForm_item& item_imp = body.find_field(PRIMO_IMPORTO + j);
			real imp = (real&) importi[j];
			imp = imp / 1000.00;
			imp.trunc();
			TString simp (imp.string());
			item_imp.set(simp);
		}
		for (j = 0; j < aliquote.items(); j++)
		{
			TForm_item& item_aliq = body.find_field(PRIMA_ALIQUOTA + j);
			real aliq = (real&) aliquote[j];
			TString saliq (aliq.string());
			item_aliq.set(saliq);
		}
		
	  body.update();
	  const int body_righe = body.height();
	  for (int i = 0; i < body_righe; i++)
	  {
	  	pr.print(body.row(i));
    	_RigaCorr++;
		}    	
	}
  	
	pr.formfeed();
  close_print();                                      
  return TRUE;
}

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

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