// 773400.cpp - gestione tabelle 770
#include <applicat.h>
#include <tabapp.h>  
#include <strings.h>
#include <stdtypes.h>
#include <tabutil.h>
#include <utility.h>
#include "batbca7.h"

// Lista modifiche
// 
// 21.12.95  i codici quadro con 1015 sono C-E1 (= tolto E2) (nf)
//           la perc. r.a. non e' piu' obbligatoria (nf)
//
// 14.5.96	Modifiche per dichiarazione 96 (nf)
//

class Tab770_application : public Tab_application
{
  private:
    TMask*  _msk;   
    TString _tabname;
    
  protected:  
    bool	IsCausali() const { return _tabname == "%CA7"; }
    void	SetDefaultValueCausali(TMask& m);
    void	init_mask(TMask& m);
    static	bool TC_causD1_handler(TMask_field& f, KEY k);
  public:      
    bool user_create();                      
    virtual void  init_modify_mode(TMask& m);    
    virtual void  init_insert_mode(TMask& m);
    virtual void  init_query_mode(TMask& m);
    Tab770_application() {}
    virtual ~Tab770_application() {}
};

HIDDEN inline Tab770_application& app() {return (Tab770_application&) main_app();}

bool Tab770_application::TC_causD1_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)
  {
    TString16 value(f.get());
    TMask& m = f.mask();
    if (value == "F")
      m.reset(F_ASSRIT);     
    else
      if (value == "A" || value == "B" || value == "C" || value == "D"
          || value == "E")
          m.set(F_ASSRIT, "X");
  }
  return TRUE;
}

HIDDEN bool codqua_handler(TMask_field& f, KEY k)
{
  if (k == K_ENTER && f.mask().is_running())
  {
    TMask& m = f.mask();
    const int trib = atoi(m.get(F_CODTRIB));

    if (trib == 1015)               
    {  
      const int dlg_qua = F_QUADRCE2;
      TString16 quad(m.get(F_QUADRCE2)); 
      quad.trim();
      quad.upper();
      if (quad != "C" && quad != "D" && quad != "E" && quad != "E1" &&
          quad != "D1")
        return f.warning_box("Con il codice tributo 1015 si possono indicare solo i quadri C-D-D1-E-E1");
    }
  }
  return TRUE;
}

HIDDEN bool codtrib_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)
  { 
    TMask& m = f.mask();
    const int trib = atoi(f.get());
        
    if (trib == 1015) {
      m.hide(F_QUADRAT);
      m.show(F_QUADRCE2);
    }
    else {
      m.hide(F_QUADRCE2);
      m.show(F_QUADRAT);
    }      
  }
  return TRUE;
}

HIDDEN bool descr_handler(TMask_field& f, KEY k)
{
  if (k == K_ENTER)
  { 
    TMask& m = f.mask();
    const TString descr = f.get();
    
    if (descr == "")
    {
      f.error_box("La descrizione e' obbligatoria");
      return FALSE;
    }      
  }
  return TRUE;
}

void Tab770_application::SetDefaultValueCausali(TMask& m)
{    
  TString sAssImpInps(m.get(TCF_ASSIMPINPS));
  TString sPercInps(m.get(TCF_PERCINPS));
  TString sPercAss(m.get(F_PERASS));

  sAssImpInps.trim();
  sPercInps.trim();
  sPercAss.trim();
    
  if (sAssImpInps.empty())
    m.set(TCF_ASSIMPINPS, "95.00"); // % ass.imp.contr.INPS
//  m.set(TCF_COMMINPS,   "66.67");	// quota competenza committente
  if (sPercInps.empty())
    m.set(TCF_PERCINPS,   "10.00");	// % contr. INPS
  if (sPercAss.empty())
    m.set(F_PERASS,      "100.00");	// % ass. imponibile
}

void Tab770_application::init_mask(TMask& m)
{
  if (IsCausali())
    SetDefaultValueCausali(m);
}

void Tab770_application::init_query_mode(TMask& m)
{
  init_mask(m);
}

void Tab770_application::init_insert_mode(TMask& m)
{
  init_mask(m);
}

void Tab770_application::init_modify_mode(TMask& m)
{}

bool Tab770_application::user_create()
{ 
  Tab_application::user_create();
  _msk     = get_mask();
  _tabname = get_tabname();

  if (IsCausali()) 
  {
    _msk->set_handler(F_CODTRIB,  codtrib_handler);
    _msk->set_handler(F_QUADRCE2, codqua_handler);
    _msk->set_handler(F_QUADRAT,  codqua_handler);
    _msk->set_handler(TCF_CAUSD1, TC_causD1_handler);
  }
  return TRUE;
}

m73400(int argc, char* argv[])
{
  Tab770_application a;
  a.run(argc,argv, "Tabelle");
  return 0;
}


