#include <execp.h>
#include <mask.h>
#include <mailbox.h>
#include <relapp.h>
#include <tabutil.h>
#include <utility.h>
#include <validate.h>

#include "ba4.h"
#include "ba4200.h"

#define FLD_GD1_TIPOA     126
#define FLD_GD1_CODANAGR  127
#define FLD_GD1_CODDITTAH 627


class TPersone_app : public TRelation_application
{
  char _tip_ana ;
  bool _reset_uff_conc;
  TString16     _cod_ana ;
  TMask*        _msk[2] ;
  TRelation*    _rel[2];
  TLocalisamfile* _comuni;
  TLocalisamfile* _tabcom;

protected:
  virtual bool user_create() ;
  virtual bool user_destroy() ;
  virtual int read(TMask& m);
  virtual int rewrite(const TMask& m);
  virtual void  init_query_mode(TMask& m);
  virtual void  init_insert_mode(TMask& m);
  virtual void  init_modify_mode(TMask& m);
  virtual bool changing_mask(int mode) {return _msk[tip_msk()] == NULL;}
  virtual const char* get_next_key();
  virtual TMask* get_mask(int mode) ;
  virtual void write_enable(bool on = TRUE);
  
  static bool ba4200_handler(TMask& m, KEY k);
  
public:
  virtual int write(const TMask& m);
  virtual TRelation* get_relation() const { return _rel[tip_msk()];}
  void manual_save(const TMask& m);
  int tip_msk() const { return _tip_ana == TFIS ? MSKF : MSKG;}
  bool ana_fis() const { return _tip_ana == TFIS;}
  bool ana_giu() const { return _tip_ana == TGIU;}
  void set_tip_ana(char tip) { _tip_ana = toupper(tip);}
  void set_cod_ana(const char* cod) { _cod_ana = cod;}
  const char* cod_ana() { return _cod_ana;}
  char tip_ana() { return _tip_ana;}
  virtual bool force_cursor_usage() const { return TRUE;}
  void set_reset_uff_conc(bool reset) { _reset_uff_conc = reset; }

  TPersone_app(char ta) : _reset_uff_conc(FALSE) { set_tip_ana(ta); }
  virtual ~TPersone_app() {}
};

bool TPersone_app::ba4200_handler(TMask& m, KEY k)
{
  TPersone_app& app = (TPersone_app&)main_app();

  if (k == K_F10)
  {
    if (app.ana_fis()) app.set_tip_ana(TGIU);
    else app.set_tip_ana(TFIS);
    m.stop_run(K_ESC);
    return FALSE;
  }
  if (k != K_F5 || m.mode() != MODE_MOD) return TRUE;
  TToken_string body(16);

  body.add(format("%d", FLD_GD1_CODDITTAH));
  if ( app.tip_ana() == TFIS )
  {
    body.add(format("%d=F", FLD_GD1_TIPOA));
    body.add(format("%d=%s", FLD_GD1_CODANAGR, (const char*) app.cod_ana()));
  }
  else
  {
    body.add(format("%d=G", FLD_GD1_TIPOA));
    body.add(format("%d=%s", FLD_GD1_CODANAGR, (const char*) app.cod_ana()));
  }
  TString16 cmd("ba4 -2");
  TMessage msg(cmd2name(cmd), MSG_FS, (const char*) body);

  msg.send();
  //  TLocalisamfile& d = app.get_relation()->lfile();

  //  d.setkey(1);
  //  d.zero();
  //  d.put(N_TIPOA, app.ana_fis() ? "F" : "G");
  //  d.put(N_CODANAGR,  m.get(FLD_CODANAGR));
  //  d.read(_isequal, _unlock) ;
  TExternal_app a(cmd);
  a.run();
  //  d.read(_iscurr, _lock) ;
  m.set_focus();
  return FALSE;
}

void  TPersone_app::init_query_mode(TMask& m)

{
  m.disable(DLG_DITTE);
  m.enable(DLG_CHGTIP);
}

const char* TPersone_app::get_next_key()

{
  TLocalisamfile&       anag = get_relation()->lfile() ;
  long cod_anagr = 1;

  if (!anag.empty() )
  {
    anag.zero() ;
    anag.setkey(1) ;
    if (ana_fis())
    {
      anag.put(N_TIPOA, TGIU);
      anag.read(_isgteq) ;
      if (anag.good()) anag.prev();
      const char t_ret = anag.get_char(N_TIPOA);
      if ( t_ret == TFIS ) 
        cod_anagr = anag.get_long(N_CODANAGR)+1;
    }
    else
    {
      anag.last( ) ;
      if (anag.good())
      {
        const char t_ret = anag.get_char(N_TIPOA);
        if ( t_ret == TGIU ) 
          cod_anagr = anag.get_long(N_CODANAGR)+1;
      }
    }
  }
  _cod_ana.format("%ld", cod_anagr);
  return format("%d|%ld", FLD_CODANAGR, cod_anagr);
}


void  TPersone_app::init_insert_mode(TMask& m)
{
  m.disable(DLG_DITTE);
  m.disable(DLG_CHGTIP);
}


void  TPersone_app::init_modify_mode(TMask& m)
{
  m.enable(DLG_DITTE);
  m.disable(DLG_CHGTIP);
}

HIDDEN bool stato_hnd(TMask_field& f, KEY key)
{
  if (f.get().empty())
    f.mask().set(FLD_AF1_STATONASC_TABSTE_S0, "Italiana");
  return TRUE;
}

HIDDEN bool corr_hnd(TMask_field& f, bool giuridica)
{
  TMask & m = f.mask();

  if (f.get().empty())
  {
    const TString80 s(m.get(giuridica ? FLD_AG2_COMRES : FLD_AF1_COMRES));

    if (s.not_empty())
    {
      m.set(giuridica ? FLD_AG2_STATOCORR : FLD_AF2_STATOCORR, m.get(F_STATORES));
      f.set(s);
      m.set(giuridica ? FLD_AG2_CAPCORR : FLD_AF2_CAPCORR, 
            m.get(giuridica ? FLD_AG2_CAPRES : FLD_ANAG_CAPRES));
      f.check();
    }     
  }
  return TRUE;
}

HIDDEN bool clear_cap(TMask_field& f, KEY key)
{                     
  if (f.mask().query_mode()) 
    return TRUE;

  TMask& msk = f.mask() ;
  const short   dlg = f.dlg();

  if (dlg != FLD_AG2_COMRES && dlg != FLD_AF1_COMRES && !f.to_check(key, TRUE)) 
    return TRUE;

  const bool giuridica = ((TPersone_app&)main_app()).ana_giu();
  if (dlg == FLD_AG2_COMCORR || dlg == FLD_AF2_COMCORR)
    corr_hnd(f, giuridica);

  const TString16 val(f.get());

  if ( giuridica )
  {
    if (val.empty())
    {
      if (dlg == FLD_AG1_COMRF)
        msk.set(FLD_AG1_CAPRF, "");
      else
        if (dlg == FLD_AG2_COMRES)
          msk.set(FLD_AG2_CAPRES, "");
        else
          if (dlg == FLD_AG2_COMCORR)
            msk.set(FLD_AG2_CAPCORR, "");
    }
  }
  else
  {
    if (val.empty())
    {
      if (dlg == FLD_AF2_COMRF)
        msk.set(FLD_AF2_CAPRF, "");
      else
        if (dlg == FLD_AF1_COMRES)
          msk.set(FLD_ANAG_CAPRES, "");
        else
          if (dlg == FLD_AF2_COMCORR)
            msk.set(FLD_AF2_CAPCORR, "");
    }
  }  
  return TRUE; 
} 


// Handler del comunde di residenza e residenza fiscale                                            
bool gest_uff_imp(TMask_field& f, KEY key)
{
  if (f.mask().query_mode()) 
    return TRUE;

  TMask& msk = f.mask() ;
  const short   dlg = f.dlg();
  const bool giuridica = ((TPersone_app&)main_app()).ana_giu();

  if (giuridica && dlg == FLD_AG2_COMRES)
    clear_cap(f,key);

  if (!giuridica && dlg == FLD_AF1_COMRES)
    clear_cap(f,key);

  if (!f.to_check(key, TRUE)) 
    return TRUE;

  TString       cod_com;

  if ( giuridica )
  {
    cod_com = msk.get(FLD_AG1_COMRF);
    if (dlg == FLD_AG2_COMRES)
      if (cod_com.not_empty()) return TRUE;
  }
  else
  {
    cod_com = msk.get(FLD_AF2_COMRF);
    if (dlg == FLD_AF1_COMRES)
      if (cod_com.not_empty()) return TRUE;
  }
  clear_cap(f,key);
  if (cod_com.empty())
    cod_com = msk.get(giuridica ? FLD_AG2_COMRES : FLD_AF1_COMRES) ;
  
  if (cod_com.empty())
    return TRUE;                                   

  TLocalisamfile comuni(LF_COMUNI);
  
  comuni.setkey(1) ;
  comuni.zero() ;
  comuni.put(N_COM, cod_com );
  if (comuni.read() == NOERR)
  {
    msk.set(F_IMP_DENCOM, comuni.get("DENCOM"));
    msk.set(F_IMP_PROVCOM, comuni.get("PROVCOM"));

    TString s(comuni.get(N_IMPDIR1)) ; 
    if (s.empty()) s = comuni.get(N_IMPDIR2) ; 
    if (s.empty()) s = comuni.get(N_IMPDIR3) ; 
    if (msk.get(F_UFFIIDD).empty() || msk.is_running()) 
    {
      msk.set(F_UFFIIDD, s) ; msk.field(F_UFFIIDD).on_key(K_TAB);
    }
    s = comuni.get(N_IVA1); 
    if (s.empty()) s = comuni.get(N_IVA2) ; 
    if (s.empty()) s = comuni.get(N_IVA3) ; 
    if (msk.get(F_UFFIVA).empty() || msk.is_running()) 
    {
      msk.set(F_UFFIVA, s) ; msk.field(F_UFFIVA).on_key(K_TAB);
    }
    msk.set(F_UFFREG, comuni.get("UFFREG")); msk.field(F_UFFREG).on_key(K_TAB);
    if (msk.get_bool(F_TITCF))
    {
      if (msk.get(F_UFFCONC).empty() || msk.is_running()) 
        msk.set(F_UFFCONC, comuni.get("UFFCONC")); 
      msk.set(F_CONFIS, msk.get(giuridica ? FLD_AG1_COFI : FLD_AF1_COFI)); 
    }
    msk.set(F_UFFCONC_P, comuni.get("UFFCONC")); msk.field(F_UFFCONC_P).on_key(K_TAB);
    msk.set(F_UFFSERV, comuni.get("UFFCSERV")); msk.field(F_UFFSERV).on_key(K_TAB);

    msk.set(F_CODCONTR_EP, comuni.get("UFFESPROV").mid(1, 2));
    msk.set(F_CODCONTR_EC, comuni.get("UFFESCOM"));
  }     
  
  return TRUE;
}

// Handler del titolare conto fiscale                                           
bool tit_cf(TMask_field& f, KEY key)
{ 
  if (key == K_SPACE && f.mask().get_bool(F_TITCF))
  {
    TMask& msk = f.mask() ;   
    TPersone_app& app = (TPersone_app&)main_app();
    const bool giuridica = app.ana_giu();
    TString16  cod_com(msk.get(giuridica ? FLD_AG1_COMRF : FLD_AF2_COMRF));
    
    if (cod_com.empty())
      cod_com = msk.get(giuridica ? FLD_AG2_COMRES : FLD_AF1_COMRES) ;
    
    if (cod_com.not_empty())
    { 
      TLocalisamfile comuni(LF_COMUNI);

      comuni.setkey(1) ;
      comuni.zero() ;
      comuni.put(N_COM, cod_com );
      if (comuni.read() == NOERR)
      {
        if (msk.get(F_UFFCONC).empty() || msk.is_running()) 
          msk.set(F_UFFCONC, comuni.get("UFFCONC")); 
      }
    } 
    msk.set(F_CONFIS, msk.get(giuridica ? FLD_AG1_COFI : FLD_AF1_COFI)); 
  }
  return TRUE;          
}

// Handler dell'ufficio concessione                                           
bool uff_conc(TMask_field& f, KEY key)
{     
  if (f.mask().query_mode()) 
    return TRUE;
  
  if (key == K_ENTER)
  {
    TMask& msk = f.mask() ;   
    TPersone_app& app = (TPersone_app&)main_app();
    const bool giuridica = app.ana_giu();
    TString16  cod_com(msk.get(giuridica ? FLD_AG1_COMRF : FLD_AF2_COMRF));
    
    if (cod_com.empty())
      cod_com = msk.get(giuridica ? FLD_AG2_COMRES : FLD_AF1_COMRES) ;
    
    if (cod_com.not_empty())
    { 
      TLocalisamfile comuni(LF_COMUNI);

      comuni.setkey(1) ;
      comuni.zero() ;
      comuni.put(N_COM, cod_com );
      app.set_reset_uff_conc(comuni.read() == NOERR && msk.get(F_UFFCONC) == comuni.get("UFFCONC"));
    } 
  } 
  return TRUE;
}        

bool no_dup_fis(TMask_field& f, KEY key)
{
  if (f.mask().query_mode() || !f.to_check(key)) return TRUE;
  TPersone_app& app = (TPersone_app&)main_app();
  TLocalisamfile&       anag = app.get_relation()->lfile(LF_ANAG) ;
  
  const TString16 cofi(f.get());                     
  if ( cofi.not_empty() )
  { 
    //    TRelation * r = app.get_relation();
    
    //    r->save_status();              
    anag.zero() ;
    anag.put(N_TIPOA, app.tip_ana());
    anag.put(N_COFI, cofi ) ;
    anag.setkey(3) ;
    anag.read() ;
    anag.setkey(1) ;
    const char t = anag.get_char(N_TIPOA), t1 = app.tip_ana();
    const TString16     s = anag.get(N_CODANAGR), s1 = app.cod_ana();

    if (anag.good() && (t1 != t || s1 != s))
      f.message_box("Codice fiscale gia' utilizzato per il codice %c/%s",
                    t, (const char*)s);
    //    r->restore_status();              
  }
  return TRUE ;
}

bool gest_iva(TMask_field& f, KEY key , short   fd_sta ,short fd_iva )
{
  if (f.mask().query_mode() || !f.to_check(key)) return TRUE;
  
  TPersone_app& app = (TPersone_app&)main_app();
  TLocalisamfile&       anag = app.get_relation()->lfile(LF_ANAG) ;
  TMask& msk = f.mask() ;

  if ( f.get().not_empty() && !anag.empty() )
  {
    anag.zero() ;        
    anag.put(N_TIPOA, app.tip_ana());
    anag.put(N_STATOPAIV, msk.get(fd_sta) ) ;
    anag.put(N_PAIV, msk.get(fd_iva) ) ;
    anag.setkey(4) ;
    anag.read() ;
    anag.setkey(1) ;
    const char t  = anag.get_char(N_TIPOA), t1 = app.tip_ana();
    const TFixed_string s(anag.get(N_CODANAGR)),        s1(app.cod_ana());
    if (anag.good() && (t1 != t || s1 != s))
      f.message_box("Partita iva gia' definita per il codice : %c/%s",
                    t, (const char*) s);
  }
  return TRUE ;
}

bool no_dup_iva_f(TMask_field& f, KEY key)

{
  if (f.mask().query_mode() || !f.to_check(key)) return TRUE;
  return ( gest_iva(f , key , FLD_AF1_STATOPAIV ,FLD_AF1_PAIV ) ) ;
}

bool no_dup_iva_g(TMask_field& f, KEY key)
{ 
  if (key == K_ENTER && !f.mask().query_mode() && f.get().empty())
  {                                    
    bool ok = FALSE;   
    if (!f.mask().get_bool(CHK_AG1_ENTENCOMM))
    {
      const TString cofi(f.mask().get(FLD_AG1_COFI));
      if (pi_check("IT", cofi)) 
      {
        f.set(cofi);
        ok = TRUE;
      }  
    }
    if (!ok) return error_box("Partita IVA obbligatoria!");
  }

  return gest_iva(f , key , FLD_AG1_STATOPAIV ,FLD_AG1_PAIV ) ;
}

TMask* TPersone_app::get_mask(int mode)
{ 
  if (!changing_mask(mode)) return _msk[tip_msk()];
  if (ana_fis())
  {
    if (_msk[MSKG] != NULL) delete _msk[MSKG];
    _msk[MSKG] = NULL;
    _msk[MSKF] = new TMask("ba4200b") ;
    _msk[MSKF]->set_handler(F_STATO, stato_hnd);
    _msk[MSKF]->set_handler(FLD_AF1_PAIV, no_dup_iva_f);
    _msk[MSKF]->set_handler(FLD_AF1_COFI, no_dup_fis);
    _msk[MSKF]->set_handler(FLD_AF1_COMRES, gest_uff_imp);
    _msk[MSKF]->set_handler(FLD_AF2_COMRF, gest_uff_imp);
    _msk[MSKF]->set_handler(FLD_AF2_COMCORR, clear_cap);
    _msk[MSKF]->set_handler(F_UFFCONC, uff_conc);
    _msk[MSKF]->set_handler(F_TITCF, tit_cf);
    _msk[MSKF]->set_handler(ba4200_handler);
  }
  else
  {
    if (_msk[MSKF] != NULL) delete _msk[MSKF];
    _msk[MSKF] = NULL;
    _msk[MSKG] = new TMask("ba4200c") ;
    _msk[MSKG]->set_handler(FLD_AG2_COMRES, gest_uff_imp);
    _msk[MSKG]->set_handler(FLD_AG1_COMRF, gest_uff_imp);
    _msk[MSKG]->set_handler(FLD_AG2_COMCORR, clear_cap);
    //   _msk[MSKG]->set_handler(FLD_AG1_COFI, no_dup_iva_g);
    _msk[MSKG]->set_handler(FLD_AG1_PAIV, no_dup_iva_g);
    _msk[MSKG]->set_handler(F_UFFCONC, uff_conc);
    _msk[MSKG]->set_handler(F_TITCF, tit_cf);
    _msk[MSKG]->set_handler(ba4200_handler);
  }
  return _msk[tip_msk()];
}

bool TPersone_app::user_create() 

{
  _msk[MSKF] = NULL;
  _rel[MSKF] = new TRelation(LF_ANAG);
  _rel[MSKF]->add(LF_ANAGFIS, "CODANAGR=CODANAGR", 1, 0, 0);

  _msk[MSKG] = NULL;
  _rel[MSKG] = new TRelation(LF_ANAG);
  _rel[MSKG]->add(LF_ANAGGIU, "CODANAGR=CODANAGR", 1, 0, 0);
  
  _comuni = new TLocalisamfile(LF_COMUNI) ;
  _tabcom = new TLocalisamfile(LF_TABCOM) ;

  if (argc() > 3)
  {
    get_mask(MODE_QUERY);
    TString16 s("1|");

    s << argv(3);
    set_link(*_msk[tip_msk()], s);
  }
  return TRUE;
}

void TPersone_app::write_enable(bool on)

{
  _rel[MSKF]->write_enable(0, on);
  _rel[MSKG]->write_enable(0, on);
}

bool TPersone_app::user_destroy() 
{
  for ( int j = 0 ; j < 2 ; j++ )
  {
    if (_msk[j]) delete _msk[j] ;
    if (_rel[j]) delete _rel[j] ;
  }
  delete _comuni;
  delete _tabcom;

  return TRUE;
}

void TPersone_app::manual_save(const TMask& m)

{
  TRelation *r = get_relation();
  TLocalisamfile& anag = r->lfile(LF_ANAG) ;

  if (ana_fis())
  {
    TLocalisamfile&     anafis = r->lfile(LF_ANAGFIS) ;

    anafis.put(N_CODANAGR, _cod_ana);
  }
  else
  {
    TLocalisamfile&     anagiu = r->lfile(LF_ANAGGIU) ;
    anagiu.put(N_CODANAGR, _cod_ana);
  }

  const TDate   oggi(TODAY) ;
  anag.put(N_DATAAGG, oggi.string());
}

int TPersone_app::read(TMask& m) 

{
  const TRelation *r = get_relation();
  m.autoload(r);
  _cod_ana = r->lfile().get(N_CODANAGR);
  return NOERR;
}

int TPersone_app::write(const TMask& m) 

{
  TRelation *r = get_relation();
  r->zero();
  m.autosave(r);
  set_cod_ana(r->lfile().get(N_CODANAGR));
  if (_reset_uff_conc)
  {
    _reset_uff_conc = FALSE;
    r->lfile().zero("UFFCONC");
  }
  manual_save(m);
  return r->write();
}

int TPersone_app::rewrite(const TMask& m) 

{
  TRelation *r = get_relation();

  m.autosave(r);
  if (_reset_uff_conc)
  {
    _reset_uff_conc = FALSE;
    r->lfile().zero("UFFCONC");
  }
  manual_save(m);     
  /* if (ana_fis())
     {
     if (m.field(FLD_AF2_INDRF).dirty() || m.field(FLD_AF2_CIVRF).dirty() ||
     m.field(FLD_AF2_CAPRF).dirty() || m.field(FLD_AF2_COMRF).dirty())
     message_box("Attenzione e' stata variata la residenza fiscale. Controllare le unita' locali");
     }
     else
     {
     if (m.field(FLD_AG1_INDRF).dirty() || m.field(FLD_AG1_CIVRF).dirty() ||
     m.field(FLD_AG1_CAPRF).dirty() || m.field(FLD_AG1_COMRF).dirty())
     message_box("Attenzione e' stata variata la residenza fiscale. Controllare le unita' locali");
     } */   
  return r->rewrite();
}

int ba4200(int argc, char* argv[])
{                         
  char ta = 'F';
  
  TApplication::check_parameters(argc, argv);
  if (argc > 2 && toupper(*argv[2]) == 'G') ta = 'G';

  TPersone_app a(ta);

  TString80 titolo("Persone ");
  titolo << (ta == 'F' ? "Fisiche" : "Giuridiche");
  
  a.run(argc, argv, titolo);
  return 0;
}
