// 771231.cpp - Quadri F
//
// Lista modifiche
// 12. 1.96 Controlla codcaus anche dopo che e' stato immesso (nf)
//
#include <relapp.h>
#include <config.h>
#include <msksheet.h>
#include <sort.h>
#include <tabutil.h>
#include <progind.h>
#include <defmask.h>
#include "77lib.h"
#include "scperc.h"
#include "rpag.h"
#include "perc.h"
#include "prospe1.h"
#include "774200.h"
#include "771231a.h"	// F, F-1
#include "771232.h"		// quadro G
#include "quadrif.h"
#include "ca7.h"

#define	S_CODC	101
#define	S_DESC	102
#define	S_ALIQ	103
#define	S_SOGG	104
#define S_ROPE	105
// Solo nel prospetto A
#define S_TACC	106    
#define S_SALD	107
#define S_VECC	108

const int RIGHE_PA = 6;

HIDDEN TString80	__dep;

void ss_pack(TSheet_field& ss);
void do_output(TString& quadro, TMask& m, const bool bOutputAliq);
bool check_caus(const char* codcau);

class TQuadroF : public TRelation_application
{
  private:
    TRelation*       	_rel;
    TMask*           	_msk;
    int              	_anno_dic;  // anno dichiarazione
    long				_codditta;
    TLocalisamfile*		_qf, *_rf;
    TTable*				_ca7; 
    TRecord_array*		_prosp_a, *_prosp_b, *_prosp_c, *_prosp_d;
    TRiporti			_rip;
    bool  				_registra;
    char				_tipo_prosp;
    TString				_sAliqPrec;	// in set_notify per det.se cambiata aliq.
    static real			calcola_rope(TMask_field& f, const real& sogg, const bool force);
    static bool			codc_handler(TMask_field& f, KEY k);
    static bool			aliq_handler(TMask_field& f, KEY k);
    static bool			sogg_handler(TMask_field& f, KEY k);
    static bool			sald_handler(TMask_field& f, KEY k);
    static bool			vecc_handler(TMask_field& f, KEY k);
    static bool			tacc_handler(TMask_field& f, KEY k);            
    static bool			eprov_handler(TMask_field& f, KEY k);        
    static bool			ealiq_handler(TMask_field& f, KEY k);        
    static bool			fsogg_handler(TMask_field& f, KEY k);        
    static bool			faliq_handler(TMask_field& f, KEY k);        
    static bool			a_notify(TSheet_field& s, int r, KEY k);
    static bool			b_notify(TSheet_field& s, int r, KEY k);
    static bool			c_notify(TSheet_field& s, int r, KEY k);
    static bool			d_notify(TSheet_field& s, int r, KEY k);        
    int					salva_prosp_a(const TMask& m, bool re);
    int					salva_prosp_b(const TMask& m, bool re);
    int					salva_prosp_c(const TMask& m, bool re);  
    int					salva_prosp_d(const TMask& m, bool re);
//    const char* 		get_desc_cau(const char* codcau);
  protected:
    virtual bool	user_create();
    virtual bool	user_destroy();
    virtual int		read(TMask& m);
    virtual int		rewrite(const TMask& m);
    virtual int		write  (const TMask& m);  
    virtual bool	remove();
    virtual TRelation*	get_relation() const { return _rel; }
    virtual TMask*	get_mask(int mode) { return _msk; }
    virtual bool	changing_mask(int mode) { return FALSE; }
    virtual void	init_query_mode (TMask&);
    virtual void	init_modify_mode (TMask&);
    void			init_mask (TMask&);
    void			fill_ss(TMask& m);
    void			riempi_prospetto(TMask& m, const char prosp);
    int				re_write(const TMask& m, bool re);

  public:                      
    TString16			_quadro, _aliq_caus;
    TString80			_title,  _desc_caus;
    TSheet_field& 		sfProspA() const;
    TSheet_field& 		sfProspB() const;
    TSheet_field& 		sfProspC() const;        
    TSheet_field& 		sfProspD() const;            
    const char* taitol() { return (const char*)_title; }
    
    TQuadroF(const char quadro='0');
    virtual ~TQuadroF() {};
};                    

TSheet_field& TQuadroF::sfProspA() const
{
  TSheet_field& s = (TSheet_field&)curr_mask().field(F_A);
  return s;
}

TSheet_field& TQuadroF::sfProspB() const
{
  TSheet_field& s = (TSheet_field&)curr_mask().field(F_B);
  return s;
}

TSheet_field& TQuadroF::sfProspC() const
{                               
  TSheet_field& s = (TSheet_field&)curr_mask().field(F_C);
  return s;
}

TSheet_field& TQuadroF::sfProspD() const
{                               
  TSheet_field& s = (TSheet_field&)curr_mask().field(F_D);
  return s;
}

TQuadroF::TQuadroF(const char quadro)
{
  _registra   = FALSE;
  _tipo_prosp = 'A';
  switch (quadro)
  {
    case '0':
      _quadro = "F";  
      _title  = "Quadro F";
      break;
    case '1':
      _quadro = "F1";  
      _title  = "Quadro F-1";
      _tipo_prosp = 'B';
      break;
    case '2':
      _quadro = "F2";  
      _title  = "Quadro F-2";
      _tipo_prosp = 'B';      
      break;
    default:
      break;
  }
}

inline TQuadroF& app() { return (TQuadroF&) main_app(); }

void TQuadroF::init_modify_mode (TMask& m)
{
  init_mask(m);
  m.disable(DLG_DELREC);
}

void TQuadroF::init_query_mode (TMask& m)
{
  TString16 codditta; codditta << _codditta;
  if (codditta != "0")        
  {
    m.field(F_CODDITTA).set(codditta);
    m.field(F_CODDITTA).check();
  }
  m.send_key(K_AUTO_ENTER,0);
  init_mask(m);
}

void TQuadroF::init_mask (TMask& m)
{
  fill_ss(m);
}

bool TQuadroF::user_create() 
{
  if (_quadro == "F")
  {
    _msk = new TMask("771231a");
    _msk->disable(DLG_CANCEL);  // disabilito Annulla nella toolbar
    _msk->set_handler(E_PROVENTI, eprov_handler);
    _msk->set_handler(E_ALI,      ealiq_handler);
    _msk->set_handler(F_SOGG,     fsogg_handler);
    _msk->set_handler(F_ALI,      faliq_handler);
    TSheet_field& p_a = (TSheet_field&) _msk->field(F_A);  
    p_a.set_notify(a_notify);                          
    p_a.sheet_mask().set_handler(S_CODC, codc_handler);  
    p_a.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_a.sheet_mask().set_handler(S_SOGG, sogg_handler);
    p_a.sheet_mask().set_handler(S_SALD, sald_handler);
    p_a.sheet_mask().set_handler(S_VECC, vecc_handler);
    p_a.sheet_mask().set_handler(S_TACC, tacc_handler);
    TSheet_field& p_b = (TSheet_field&) _msk->field(F_B);  
    p_b.set_notify(b_notify);                            
    p_b.sheet_mask().set_handler(S_CODC, codc_handler);    
    p_b.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_b.sheet_mask().set_handler(S_SOGG, sogg_handler);
    TSheet_field& p_c = (TSheet_field&) _msk->field(F_C);  
    p_c.set_notify(c_notify);
    p_c.sheet_mask().set_handler(S_CODC, codc_handler);    
    p_c.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_c.sheet_mask().set_handler(S_SOGG, sogg_handler);
    TSheet_field& p_d = (TSheet_field&) _msk->field(F_D);  
    p_d.set_notify(d_notify);
    p_d.sheet_mask().set_handler(S_CODC, codc_handler);    
    p_d.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_d.sheet_mask().set_handler(S_SOGG, sogg_handler);
  
    _rel = new TRelation(LF_QUAF);
    _rf  = new TLocalisamfile(LF_RIGHEF);

    _codditta = get_firm_770();
    TRectype dep(_rf->curr());
    dep.zero();                
    dep.put("CODDITTA", _codditta);
    dep.put("TIPOPRO", "A");
    _prosp_a = new TRecord_array(dep, "NPROG");
    dep.put("TIPOPRO", "B");
    _prosp_b = new TRecord_array(dep, "NPROG");
    dep.put("TIPOPRO", "C");
    _prosp_c = new TRecord_array(dep, "NPROG");
    dep.put("TIPOPRO", "D");
    _prosp_d = new TRecord_array(dep, "NPROG");
  }

  if (_quadro == "F1")
  {
    _msk = new TMask("771232a");
    _msk->disable(DLG_CANCEL);  // disabilito Annulla nella toolbar
    TSheet_field& p_b = (TSheet_field&) _msk->field(F_B);
    p_b.sheet_mask().set_handler(S_CODC, codc_handler);        
    p_b.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_b.sheet_mask().set_handler(S_SOGG, sogg_handler);

    _rel = new TRelation(LF_QUAF1);
    _rf  = new TLocalisamfile(LF_RIGHEF1);

    _codditta = get_firm_770();
    TRectype dep(_rf->curr());
    dep.zero();                
    dep.put("CODDITTA", _codditta);
    dep.put("TIPOPRO", "B");
    _prosp_b = new TRecord_array(dep, "NPROG");
  }

  if (_quadro == "F2")
  {
    _msk = new TMask("771233a");
    _msk->disable(DLG_CANCEL);  // disabilito Annulla nella toolbar
    TSheet_field& p_b = (TSheet_field&) _msk->field(F_B);  
    p_b.sheet_mask().set_handler(S_CODC, codc_handler);  
    p_b.sheet_mask().set_handler(S_ALIQ, aliq_handler);
    p_b.sheet_mask().set_handler(S_SOGG, sogg_handler);

    _rel = new TRelation(LF_QUAF2);
    _rf  = new TLocalisamfile(LF_RIGHEF2);

    _codditta = get_firm_770();
    TRectype dep(_rf->curr());
    dep.zero();                
    dep.put("CODDITTA", _codditta);
    dep.put("TIPOPRO", "B");
    _prosp_b = new TRecord_array(dep, "NPROG");
  }
    
  return TRUE;                               
}

bool TQuadroF::user_destroy()
{ 
  delete _msk;
  delete _rel;
  delete _rf;
  delete _prosp_b;
  
  if (_quadro == "F")
  {
    delete _prosp_a; 
    delete _prosp_c; 
    delete _prosp_d;
  }

  if (_registra) _rip.set(_quadro);
  return TRUE;
}

HIDDEN const int PROSP_RIGHE = 10;

/***********************************
const char* TQuadroF::get_desc_cau(const char* codcau)
{
  TTable ca7("%ca7");
  __dep = "";
  ca7.zero();
  ca7.put("CODTAB", codcau);
  const int rc = ca7.read();
  if (rc == NOERR)
    __dep = ca7.get("S0");
  return __dep;
}
**************************/

void TQuadroF::riempi_prospetto(TMask& m, const char prosp)
{
  switch (prosp)
  {
    case 'A': 
    {
      TSheet_field&	ss = (TSheet_field&)m.field(F_A);
      const int last_a = _prosp_a->last_row();
      for (int i = 1; i <= last_a; i++)
      {         
        TRectype& r = _prosp_a->row(i, TRUE);
        TToken_string& row = ss.row(i-1);
        TString16 codcau(r.get(QUF_CODCAUS));
        TString   descr_cau(get_desc_cau(codcau));
        TString16 aliq  (r.get(QUF_ALIQUOTA));   
        TString   sogg  (r.get(QUF_SOGRIT));
        TString   rope  (r.get(QUF_ROPE));
        row.add(codcau);
        row.add(descr_cau);          
        row.add(aliq);
        row.add(sogg);
        row.add(rope);
        // Solo se (prosp == 'A')
        TString acc(r.get(QUF_ACCONTI));
        TString ecc(r.get(QUF_VERSECC));
        TString sal(r.get(QUF_VERSALDO));
        row.add(acc);
        row.add(sal);
        row.add(ecc);
      }
      for (i = last_a; i < PROSP_RIGHE; i++)
        ss.row(i);
      ss.force_update();
      break;   
    }
    case 'B':  {
      TSheet_field& ss = (TSheet_field&)m.field(F_B);
      const int last_b = _prosp_b->last_row();
      for (int i = 1; i <= last_b; i++)
      {         
        TRectype& r = _prosp_b->row(i, TRUE);
        TToken_string& row = ss.row(i-1);                 
        TString dep(50);
//        if (_quadro != "F2")
        dep = r.get(QUF_CODCAUS);                 
        TString   descr_cau(get_desc_cau(dep));        
//        else dep = r.get("DENFONDO");
        TString16 aliq (r.get(QUF_ALIQUOTA));
        TString   sogg (r.get(QUF_SOGRIT));
        TString   rope (r.get(QUF_ROPE));
        row.add(dep);
        row.add(descr_cau);
        row.add(aliq);
        row.add(sogg);
        row.add(rope);
      }
      for (i = last_b+1; i < PROSP_RIGHE; i++)
        ss.row(i);
      if (_quadro != "F") 
        ss.force_update();
      break;    }
    case 'C':  {
      TSheet_field& ss = (TSheet_field&)m.field(F_C);
      const int last_c = _prosp_c->last_row();
      for (int i = 1; i <= last_c; i++)
      {         
        TRectype& r = _prosp_c->row(i, TRUE);
        TToken_string& row = ss.row(i-1);
        TString16 codcau(r.get(QUF_CODCAUS));
        TString   descr_cau(get_desc_cau(codcau));        
        TString16 aliq  (r.get(QUF_ALIQUOTA));   
        TString   sogg  (r.get(QUF_SOGRIT));
        TString   rope  (r.get(QUF_ROPE));
        row.add(codcau);
        row.add(descr_cau);
        row.add(aliq);
        row.add(sogg);
        row.add(rope);
      }
      for (i = last_c+1; i < PROSP_RIGHE; i++)
        ss.row(i);
      break;  }
    case 'D': {
      TSheet_field& ss = (TSheet_field&)m.field(F_D);
      const int last_d = _prosp_d->last_row();
      for (int i = 1; i <= last_d; i++)
      {         
        TRectype& r = _prosp_d->row(i, TRUE);
        TToken_string& row = ss.row(i-1);
        TString16 codcau(r.get(QUF_CODCAUS));
        TString   descr_cau(get_desc_cau(codcau));        
        TString16 aliq  (r.get(QUF_ALIQUOTA));   
        TString   sogg  (r.get(QUF_SOGRIT));
        TString   rope  (r.get(QUF_ROPE));
        row.add(codcau);
        row.add(descr_cau);
        row.add(aliq);
        row.add(sogg);
        row.add(rope);
      }
      for (i = last_d+1; i < PROSP_RIGHE; i++)
        ss.row(i);
      break; }
    default:
      break;
  }
}

void TQuadroF::fill_ss(TMask& m)
{
  riempi_prospetto(m, 'B');
  if (_quadro == "F")
  {
    riempi_prospetto(m, 'A');
    riempi_prospetto(m, 'C');
    riempi_prospetto(m, 'D');  
  }
}

int TQuadroF::read(TMask& m)
{
  int err = TRelation_application::read(m);

  if (err == NOERR)
  {  
    TRectype r = _rf->curr();
    r.zero();
    r.put("CODDITTA", m.get(F_CODDITTA)); 
    r.put("TIPOPRO", "B");
    _prosp_b->read(r);
    if (_quadro == "F")
    {
      r.put("TIPOPRO", "A");
      _prosp_a->read(r);
      r.put("TIPOPRO", "C");
      _prosp_c->read(r);
      r.put("TIPOPRO", "D");
      _prosp_d->read(r);
    }
  }  
  fill_ss(m);
  return err;
}            

int TQuadroF::write(const TMask& m)
{
  int err = TRelation_application::write(m);
  if (err == NOERR)
    err = re_write(m, FALSE);   
  _registra = err == NOERR;    
  return err;
}

int TQuadroF::rewrite(const TMask& m)
{
  int err =  re_write(m, TRUE);
  int err1 = TRelation_application::rewrite(m);
//  _registra = err == NOERR;    
// 12.6.96 se compilo solo prosp.A del F-1 non fa i riporti
  _registra = err == NOERR || err1 == NOERR;    
  return err == NOERR ? err1 : err;
}

void ss_pack(TSheet_field& ss)
{
  TString_array& rows = ss.rows_array();

  const int max = rows.items();
  for (int i = 0; i < max; i++)
  {
    TToken_string& r = (TToken_string&)rows[i];
    TString16 codcaus(r.get(0));
    codcaus.trim();
    if (codcaus.empty()) 
      rows.destroy(i, FALSE);
  }
  rows.pack();
}        

int TQuadroF::salva_prosp_a(const TMask& m, bool re)
{
  _prosp_a->destroy_rows();
  if (m.insert_mode())
    _prosp_a->renum_key("CODDITTA", m.get(F_CODDITTA));

  TSheet_field& p_a = (TSheet_field&)m.field(F_A);
  TString_array& rows = p_a.rows_array();
  ss_pack(p_a);
  
  for (int i = 0;  i < p_a.items(); i++)
  {
    TToken_string& riga = p_a.row(i);
    TString16 codcaus(riga.get(0));
    if (codcaus.empty()) continue;
    TString16 aliq   (riga.get(2));   // salta descr. caus.
//    const float aliq = atof(riga.get());
    TString   sogg   (riga.get());
    TString   rope   (riga.get());
    TString   totacc (riga.get());
    TString   vers_a_saldo (riga.get());
    TString   somm_versata (riga.get());
    const int row = i + 1 ;    
    TRectype& r = _prosp_a->row(row, TRUE);
    r.zero();
    r.put (QUF_CODDITTA, (long)_codditta);
    r.put (QUF_TIPOPRO, "A");
    r.put (QUF_NPROG, i+1);
    r.put (QUF_CODCAUS, codcaus);
    r.put (QUF_ALIQUOTA , (const char*)aliq);
    r.put (QUF_SOGRIT  , sogg);
    r.put (QUF_ROPE,     rope);         
    r.put (QUF_ACCONTI,  totacc);
    r.put (QUF_VERSALDO, vers_a_saldo);
    r.put (QUF_VERSECC,  somm_versata);        
  }
  return _prosp_a->write(re);                   
}          

int TQuadroF::salva_prosp_b(const TMask& m, bool re)
{
  _prosp_b->destroy_rows();                      
  TSheet_field& p_b = (TSheet_field&)m.field(F_B);
  TString_array& rowsb = p_b.rows_array();
  ss_pack(p_b);

  if (m.insert_mode())
    _prosp_b->renum_key("CODDITTA", m.get(F_CODDITTA));

  for (int i = 0;  i < p_b.items(); i++)
  {
    TToken_string &riga = p_b.row(i);
    TString dep(40);
    dep = riga.get(0); dep.trim();
//    if (_quadro != "F2" && dep.empty()) continue;    
    if (dep.empty()) continue;    
    TString   aliq   (riga.get(2));   
    TString   sogg   (riga.get());
    TString   rope   (riga.get());

    const int row = i + 1 ;
    TRectype& r = _prosp_b->row(row, TRUE);
    r.zero();
    r.put (QUF_CODDITTA, (long)_codditta);
    r.put (QUF_TIPOPRO, "B");
    r.put (QUF_NPROG, i+1);
    r.put (QUF_ALIQUOTA, (const char*)aliq);
//    r.put (_quadro == "F2" ? "DENFONDO" : "CODCAUS" , dep);
    r.put (QUF_CODCAUS , dep);
    r.put (QUF_SOGRIT  , sogg);
    r.put (QUF_ROPE,     rope);
  }
  return _prosp_b->write(re);  
}        

int TQuadroF::salva_prosp_c(const TMask& m, bool re)
{
  _prosp_c->destroy_rows();                      
  TSheet_field& p_c = (TSheet_field&)m.field(F_C);
  TString_array& rowsc = p_c.rows_array();
  ss_pack(p_c);

  if (m.insert_mode())
    _prosp_c->renum_key("CODDITTA", m.get(F_CODDITTA));

  for (int i = 0;  i < p_c.items(); i++)
  {
    TToken_string &riga = p_c.row(i);
    TString16 codcaus(riga.get(0));
    if (codcaus.empty()) continue;    
    TString16 aliq   (riga.get(2));   
    TString   sogg   (riga.get());
    TString   rope   (riga.get());

    const int row = i + 1 ;
    TRectype& r = _prosp_c->row(row, TRUE);
    r.zero();
    r.put (QUF_CODDITTA, (long)_codditta);
    r.put (QUF_TIPOPRO, "C");
    r.put (QUF_NPROG, i+1);
    r.put (QUF_CODCAUS, codcaus);
    r.put (QUF_ALIQUOTA , (const char*)aliq);
    r.put (QUF_SOGRIT  , sogg);
    r.put (QUF_ROPE,     rope);
  }
  return _prosp_c->write(re);  
}                                    

int TQuadroF::salva_prosp_d(const TMask& m, bool re)
{
  _prosp_d->destroy_rows();                      
  TSheet_field& p_d = (TSheet_field&)m.field(F_D);
  TString_array& rowsd = p_d.rows_array();
  ss_pack(p_d);

  if (m.insert_mode())
    _prosp_d->renum_key("CODDITTA", m.get(F_CODDITTA));

  for (int i = 0;  i < p_d.items(); i++)
  {
    TToken_string &riga = p_d.row(i);
    TString16 codcaus(riga.get(0));
    if (codcaus.empty()) continue;    
    TString16 aliq   (riga.get(2));   
    TString   sogg   (riga.get());
    TString   rope   (riga.get());

    const int row = i + 1 ;
    TRectype& r = _prosp_d->row(row, TRUE);
    r.zero();
    r.put (QUF_CODDITTA, (long)_codditta);
    r.put (QUF_TIPOPRO, "D");
    r.put (QUF_NPROG, i+1);
    r.put (QUF_CODCAUS, codcaus);
    r.put (QUF_ALIQUOTA , (const char*)aliq);
    r.put (QUF_SOGRIT  , sogg);
    r.put (QUF_ROPE,     rope);
  }
  return _prosp_d->write(re);  
}

int TQuadroF::re_write(const TMask& m, bool re)
{
  int err_a, err_b, err_c, err_d;

  err_b = salva_prosp_b(m, re);  
  if (_quadro == "F")
  {
    err_a = salva_prosp_a(m, re);
    err_c = salva_prosp_c(m, re);  
    err_d = salva_prosp_d(m, re);  
    return err_a | err_b | err_c | err_d;
  } 
  else 
    return err_b;
}

bool TQuadroF::remove()
{  
  bool ok, ok_a, ok_b, ok_c, ok_d;
  ok = TRelation_application::remove();
  ok_b = _prosp_b->remove();
  
  if (_quadro == "F")
  {
    ok_a = _prosp_a->remove();
    ok_c = _prosp_c->remove();
    ok_d = _prosp_d->remove();  
    return ok && ok_a && ok_b && ok_c && ok_d;
  }                 
  else
    return ok && ok_b;
}

bool TQuadroF::a_notify(TSheet_field& s, int r, KEY k)
{
  switch(k)
  {                               
    case K_SPACE:      
    {                     
      TSheet_field& sfProspA = app().sfProspA();       
      TToken_string& row = sfProspA.row(r);
      app()._sAliqPrec = row.get(2);
      app()._sAliqPrec.trim();
    }
    case K_TAB: 
      app()._tipo_prosp = 'A';
      break;
    case K_INS:                   
      break;
    default:
      break;
  }
  return TRUE;
}

bool TQuadroF::b_notify(TSheet_field& s, int r, KEY k)
{
  switch(k)
  {                               
    case K_SPACE: 
    {                     
      TSheet_field& sfProspB = app().sfProspB();       
      TToken_string& row = sfProspB.row(r);
      app()._sAliqPrec = row.get(2);
      app()._sAliqPrec.trim();
    }
    case K_TAB:     
      app()._tipo_prosp = 'B';
      break;
    case K_INS:                   
      break;
    default:
      break;
  }
  return TRUE;
}

bool TQuadroF::c_notify(TSheet_field& s, int r, KEY k)
{
  switch(k)
  {                         
    case K_SPACE: 
    {                     
      TSheet_field& sfProspC = app().sfProspC();       
      TToken_string& row = sfProspC.row(r);
      app()._sAliqPrec = row.get(2);
      app()._sAliqPrec.trim();      
    }
    case K_TAB:     
      app()._tipo_prosp = 'C';
      break;
    case K_INS:                   
      break;
    default:
      break;
  }
  return TRUE;
}

bool TQuadroF::d_notify(TSheet_field& s, int r, KEY k)
{
  switch(k)
  {                               
    case K_SPACE: 
    {                     
      TSheet_field& sfProspD = app().sfProspD();       
      TToken_string& row = sfProspD.row(r);
      app()._sAliqPrec = row.get(2);
      app()._sAliqPrec.trim();      
    }
    case K_TAB: 
      app()._tipo_prosp = 'D';
      break;
    case K_INS:                   
      break;
    default:
      break;
  }
  return TRUE;
}

bool TQuadroF::eprov_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {                           
    app()._tipo_prosp = 'E';    
    const real sogg(f.mask().get(E_PROVENTI));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}
        
bool TQuadroF::ealiq_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {                         
    app()._tipo_prosp = 'E';  
    const real sogg(f.mask().get(E_PROVENTI));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

bool TQuadroF::fsogg_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {                         
    app()._tipo_prosp = 'F';  
    const real sogg(f.mask().get(F_SOGG));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

bool TQuadroF::faliq_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {                               
    app()._tipo_prosp = 'F';
    const real sogg(f.mask().get(F_SOGG));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

// Se scrivo S_SALD devo pulire S_VECC e viceversa
bool TQuadroF::sald_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {
    TString16 dep(f.get());
    if (dep.not_empty())
      f.mask().reset(S_VECC);
  }
  return TRUE;
}

bool TQuadroF::vecc_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB && f.focusdirty())
  {
    TString16 dep(f.get());
    if (dep.not_empty())
      f.mask().reset(S_SALD);
  }
  return TRUE;
}

bool TQuadroF::tacc_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)
  {
    const real sogg(f.mask().get(S_SOGG));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

bool TQuadroF::aliq_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)
  {
    const real sogg(f.mask().get(S_SOGG));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

bool TQuadroF::sogg_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)
  {
    const real sogg(f.mask().get(S_SOGG));
    calcola_rope(f, sogg, f.focusdirty());
  }
  return TRUE;
}

void do_output(TString& quadro, TMask& m, const bool bOutputAliq)
{
  m.set(S_DESC, app()._desc_caus);                              
  if (bOutputAliq)
    m.set(S_ALIQ, app()._aliq_caus);
}

bool check_caus(const char* codcau)
{
  TTable ca7("%ca7");
  bool ok = FALSE;
  TString& quadro = app()._quadro;
  
  ca7.zero();
  ca7.put(CA7_CODTAB, codcau);
  ok = ca7.read();
  if (ok != NOERR)
    __dep = "";
  else
    __dep = ca7.get(CA7_CODQUA);
  
  if (__dep != quadro)
    return FALSE;
  else
  {
    app()._aliq_caus = ca7.get(CA7_ALIQ);
    app()._desc_caus = ca7.get(CA7_DESCR);
    return TRUE;
  }
}

bool TQuadroF::codc_handler(TMask_field& f, KEY k)
{  
// Controlla codice quadro
  if (k == K_TAB)
  {
    TString16 codcau(f.get());
    if (codcau.empty())
      return TRUE;
    TMask& m = f.mask();
    TString& quadro = app()._quadro;
    const bool ok = check_caus(codcau);          
    TString sAliqCorr(m.get(S_ALIQ));
    sAliqCorr.trim();
// Se sono diverse vuol dire che e' stata cambiata l'aliq.e non va scritta
    bool bAliqCorrVuota = sAliqCorr.empty();
    bool bOutputAliq = app()._sAliqPrec == sAliqCorr || bAliqCorrVuota;
    if (ok)  
      do_output(quadro, f.mask(), bOutputAliq);
    else
      return warning_box("Causale con codice quadro diverso da %s", (const char*)quadro);
  }
  return TRUE;
}

real TQuadroF::calcola_rope(TMask_field& f, const real& sogg, const bool force)
{
  real	pa_col4 = ZERO;
  int 	id_rope, id_ali;
  TMask& m = f.mask();
  
  if (app()._tipo_prosp == 'E')
  {
    id_rope = E_ROP;
    id_ali  = E_ALI;
  }
  else
    if (app()._tipo_prosp == 'F')
    {
      id_rope = F_ROP;
      id_ali  = F_ALI;
    }
    else  // prospetti A,B,C,D
    {
      id_rope = S_ROPE;
      id_ali  = S_ALIQ;
    }

  real       ali   = m.get_real(id_ali);
  const real pa_col4_prec(m.get(id_rope));  // rit.operata
  if (pa_col4_prec != ZERO && !force) 
    return pa_col4_prec;
      
  pa_col4 = (sogg * ali) / 100.00;           
  pa_col4.round(0);
  
  TString ropest(20);  
  if (pa_col4 != ZERO)  // Evita di scrivere "0" nel campo (v. reqif_func nella msk)
  {
    ropest = pa_col4.string();
    m.set(id_rope, ropest);
  } 
  else
    m.reset(id_rope);
  
// Nel prospetto A oltre alla ritenuta operata setto anche S_SALD
  if (app()._tipo_prosp == 'A')
  {
    const real pa_col5(m.get(S_TACC));  
    real pa_col6 = pa_col4 - pa_col5;
    if (pa_col6 > ZERO)
    {
      TString col6_st(pa_col6.string()); 
      m.set(S_SALD, col6_st);        
      m.reset(S_VECC);
    }
    else
    {                  
// Sempre positiva, la somma versata in ecc.    
      pa_col6 *= -1.00;
      TString col7_st(pa_col6.string()); 
      m.set(S_VECC, col7_st);        
      m.reset(S_SALD);
    }
  }
  return pa_col4;
}


int quadroF(int argc, char* argv[])
{
  const char quadro = *argv[2];  // 0 = F, 1 = F-1, 2 = F-2, 3 = G
                                 // 4 = distinta G, 5 = G-1
  if (quadro <= '2')
  {  
    TQuadroF a(quadro);
    a.run(argc, argv, a.taitol());
  }

  if (quadro == '3')
  {
    TQuadroG a(quadro);
    a.run(argc, argv, a.taitol());
  }

  if (quadro == '4')
  {
    TDistintaQuadroG a;
    a.run(argc, argv, a.taitol());
  }

  if (quadro == '5')
  {
    TQuadroG1 a;
    a.run(argc, argv, a.taitol());
  }

  return 0;
}
