// quadn_t.cpp
#include <relapp.h>
#include <urldefid.h>
#include <tabutil.h>
#include <config.h>
#include "774200.h"
#include "77lib.h"   
#include "quadron.h"
#include "77qn.h"
#include "77qq.h"
#include "77qp.h"

#define CODDITTA77	181
#define	ANNODIC77	182

class TQuadriNT_application : public TRelation_application
{
  static bool nprog_handler     (TMask_field& m, KEY k);
  static bool anno_handler      (TMask_field& m, KEY k);
  static bool mese_handler      (TMask_field& f, KEY k);
  static bool codreg_handler    (TMask_field& f, KEY k);
  static bool codtrib_handler   (TMask_field& f, KEY k);
  static bool tipo              (TMask_field& f , KEY k);
  static bool luogo             (TMask_field& f , KEY k);
  const char*   _maskname;
  int           _num;
  int			_pos_quadro;  // posizione nell'array dei quadri. Usato in set_comp
  TString16     _quadro;
  TRiporti      _rip;
  TRelation*    _rel;
  TMask*        _msk;
  bool          _registra;
  int			_anno_dic;
  bool			_MaskConAnnoDic, _MaskConCodditta;
  bool			MaskConAnnoDic() const;
  bool			MaskConCodditta() const { return _MaskConCodditta; }
  void 			LeggiMask();
  bool 			EsisteUnRec();
  bool			QuadroNoR() const { return _num == LF_QUAN || _num == LF_QUAR; }
  bool			QuadroPoS() const { return _num == LF_QUAP || _num == LF_QUAS; }
  bool			QuadroQoT() const { return _num == LF_QUAQ || _num == LF_QUAT; }
  bool 			CheckImporti(const TMask& m); 
  long		  Get_newprog();
  long			_codditta; 
  
protected:
  virtual bool user_create();
  virtual bool user_destroy();                  
  virtual int  rewrite(const TMask& m);
  virtual int  write  (const TMask& m);  
  virtual bool remove();                     
  virtual void on_config_change();
  virtual TMask* get_mask(int) { return _msk; }
  virtual bool changing_mask(int) { return FALSE;}
  virtual TRelation* get_relation() const { return _rel; }  
  virtual void init_mask(TMask&);
  virtual void init_query_mode(TMask&);
  virtual void init_insert_mode(TMask&);
  virtual void init_query_insert_mode(TMask&);
public:                                                                     
  TQuadriNT_application(const char* name, const int num, const char* quadro);
};

TQuadriNT_application::TQuadriNT_application(const char* name, const int num, const char* quadro)
: _maskname(name), _num(num),
  _MaskConCodditta(FALSE), _MaskConAnnoDic(FALSE),
  _msk(NULL), _rel(NULL), _quadro(quadro) 
{
  switch (_num)
  {
    case LF_QUAN:
      _pos_quadro = N;
      break;
    case LF_QUAR:
      _pos_quadro = R;
      break;
    case LF_QUAP:
      _pos_quadro = P;
      break;
    case LF_QUAQ:
      _pos_quadro = Q;
      break;
    case LF_QUAS:
      _pos_quadro = S;
      break;
    case LF_QUAT:
      _pos_quadro = T;
      break;
  }
}

TQuadriNT_application& app() { return (TQuadriNT_application&)main_app(); }

void TQuadriNT_application::LeggiMask()
{
  _MaskConAnnoDic  = _msk->id2pos(ANNODIC77) > 0;
  _MaskConCodditta = _msk->id2pos(CODDITTA77) > 0;
}                  

bool TQuadriNT_application::MaskConAnnoDic() const
{
  return  _MaskConAnnoDic;
}

void TQuadriNT_application::on_config_change()
{
  TConfig conf(CONFIG_STUDIO);
  _anno_dic = (int)conf.get_long(ANNO_SEL, NULL, -1, TDate(TODAY).year());
}
                                    
bool TQuadriNT_application::user_create()
{           
  _msk = new TMask(_maskname);
  _rel = new TRelation(_num);
  _codditta = get_firm_770();  
  _msk->set_handler(QNF_NPROG,  nprog_handler);       
  _msk->field(CODDITTA77).set(_codditta);  
  
  if (QuadroNoR())
  {
    _msk->set_handler(QNF_ANNORIF, anno_handler);    
    _msk->set_handler(QNF_MESERIF, mese_handler);    
  }
    
  if (QuadroQoT())
  {
    _msk->set_handler(QNF_ANNORIF, anno_handler);    
    _msk->set_handler(QNF_MESERIF, mese_handler);    
    _msk->set_handler(QQF_CODREG, codreg_handler);    
  }
    
  if (QuadroPoS())
    _msk->set_handler(QPF_CODTRIB, codtrib_handler);
    
  _registra = FALSE;
  set_search_field(QNF_NPROG);

  return TRUE;
}

bool TQuadriNT_application::EsisteUnRec()
{
  TLocalisamfile qn(_num);
  qn.zero();
  qn.put(QN_CODDITTA, (long)app()._codditta);
  TRectype dep(qn.curr());
  qn.read(_isgteq);
  if (qn.curr() > dep || qn.eof()) 
    return FALSE;
  else
    return TRUE;
}

bool TQuadriNT_application::user_destroy()
{  
  if (_quadro != "")
    if (_registra)
      _rip.set_compilato(_codditta, _pos_quadro, EsisteUnRec());
  delete _msk;
  delete _rel;
  return TRUE;
}                         

// Controlla che ci siano o l'imposta o i compensi
// NB che siano uguali QNF_IMPOSTA anche nei corrispondenti campi dei
// quadri Q, R e T (107 e 106)
bool TQuadriNT_application::CheckImporti(const TMask& m)
{
  TString impost(m.get(QNF_IMPOSTA));  
  TString compen(m.get(QNF_COMPENSI));  
  return impost.not_empty() || compen.not_empty();
}

int TQuadriNT_application::rewrite(const TMask& m)
{
  m.autosave(*_rel);
  const int err = _rel->rewrite();      
  _registra = err == NOERR;
  return err;
}

int TQuadriNT_application::write(const TMask& m)
{
  m.autosave(*_rel);
  const int err = _rel->write();
  _registra = err == NOERR;
  return err;
}

bool TQuadriNT_application::remove()
{
  _registra = TRUE;
  return TRelation_application::remove();
}

void TQuadriNT_application::init_mask(TMask& m)
{
  m.set(ANNODIC77, _anno_dic); 
}

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

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

void TQuadriNT_application::init_query_insert_mode(TMask& m)
{
  init_mask(m);
  TMask_field& f = m.field(QNF_NPROG); 
  f.set(Get_newprog());    
  m.send_key(K_TAB,QNF_NPROG);
}


///////////////////////////////////////////////////////////
// 2 modi :
//  RUN,773,-1,771230m,89,"Prospetto del Quadro E1","E1"    [771230i.uml]
// oppure
//  773 -1 menu_st "Stampe dichiarazioni" [prassi.mnu]
// QUADRO N:
// 773,-2,77qn,72,"Quadro N","N"
///////////////////////////////////////////////////////////
int quadriN_T(int argc, char* argv[])
{
  TFilename mask;
  int num = 0;
  if (argc > 1) 
    mask = argv[2];
  num    = atoi(argv[3]);
  const char* title  = argv[4];             
  const char* quadro = argv[5];
  TQuadriNT_application a(mask, num, quadro);
  a.run(argc, argv, title ? title : "Test Relation Application");
  return 0;
}

bool TQuadriNT_application::anno_handler(TMask_field& f, KEY k)
{
  TMask& m = f.mask();
  if (k == K_ENTER && (modifica(m) || inserimento(m)) )
  {
    TString dep(f.get());
    if (dep.empty())
      return f.warning_box("Manca l'anno");
    else
      if (!app().CheckImporti(m))
        return f.warning_box("Manca almeno un importo");
  }
  return TRUE;
}

bool TQuadriNT_application::mese_handler(TMask_field& f, KEY k)
{
  TMask& m = f.mask();
  if (k == K_ENTER && (modifica(m) || inserimento(m)) )
  {
    TString dep(f.get());
    if (dep.empty())
      return f.warning_box("Manca il mese");
  }
  return TRUE;
}

// Per i Quadri Q e T
bool TQuadriNT_application::codreg_handler(TMask_field& f, KEY k)
{
  TMask& m = f.mask();
  if (k == K_ENTER && (modifica(m) || inserimento(m)) )
  {
    TString dep(f.get());
    if (dep.empty())
      return f.warning_box("Manca il codice regione");
  }
  return TRUE;
}

// Per i Quadri P e S. Codice tributo REQUIRED 
bool TQuadriNT_application::codtrib_handler(TMask_field& f, KEY k)
{
  TMask& m = f.mask();
  if (k == K_ENTER && (modifica(m) || inserimento(m)) )
  {
    TString dep(f.get());
    if (dep.empty())
      return f.warning_box("Manca il codice tributo");
  }
  return TRUE;
}

bool TQuadriNT_application::nprog_handler(TMask_field& f, KEY k)
{
  if (k == K_TAB)    
		f.set_dirty();
  return TRUE;                   
}   

// ritorna nuovo progressivo da utilizzare 
long TQuadriNT_application::Get_newprog()
{                      
	long nprog = 0L;              
	TLocalisamfile qnr(app()._num);
	qnr.zero();
	qnr.put(QN_CODDITTA, (long)app()._codditta);
	TRectype dep(qnr.curr());
	for (qnr.read(_isgteq); !qnr.eof(); qnr.next())
	{
		if (qnr.curr() > dep) break;
		nprog = qnr.get_long(QN_NPROG);
	}
	return ++nprog; 
}
 
bool TQuadriNT_application::tipo(TMask_field& f , KEY k)
{
/*
  if (k == K_SPACE)
  {
    char tipo  = f.mask().get(F_QLTIPOVERS)[0];
    char luogo = f.mask().get(F_QLLUOVERS)[0];
    
    if (tipo == 'D' && luogo == 'T')
      f.mask().hide(F_QLSERIE);
    else
      f.mask().show(F_QLSERIE);
  }
*/
  return TRUE;
}

bool TQuadriNT_application::luogo(TMask_field& f , KEY k)
{
/*  
  if (k == K_SPACE)
  {
    char tipo  = f.mask().get(F_QLTIPOVERS)[0];
    char luogo = f.mask().get(F_QLLUOVERS)[0];
    
    if (tipo == 'D' && luogo == 'T')
      f.mask().hide(F_QLSERIE);
    else
      f.mask().show(F_QLSERIE);  
  }           
  */
  return TRUE;
}

