#include <applicat.h>
#include <mask.h>
#include <msksheet.h>
#include <relapp.h>
#include <relation.h>
#include <tabutil.h>
#include <urldefid.h>
#include <config.h>      
#include <utility.h>   
#include <mailbox.h>
#include <execp.h>

#include "771295.h"
#include <nditte.h>

class TDettaglio_H_app : public TRelation_application
{
  TRelation       * _rel;
  TMask           * _msk;
  
  int     _mode;              // Modo maschera corrente
  long    _codditta;
  int     _anno;
  TString _ente;
   
protected:
  virtual bool user_create();
  virtual bool user_destroy();
  
  virtual TRelation*   get_relation() const { return _rel; }
  virtual TMask*       get_mask(int mode);
  virtual bool         changing_mask(int mode) {return FALSE; }
  
  //  void init_mask(TMask&);
  //virtual void init_query_mode(TMask&);
  //virtual void init_modify_mode(TMask&);
  //virtual void init_insert_mode(TMask&);
  //virtual void init_query_insert_mode(TMask&);

public:
  bool almeno_un_dettaglio(TMask&);
  static bool generazione(TMask_field& f , KEY k);
  static bool dettaglio  (TMask_field& f , KEY k);
  static bool descr_ente (TMask_field& f , KEY k);
  
  TDettaglio_H_app() {}
};
                                                  
bool TDettaglio_H_app::descr_ente (TMask_field& f , KEY k)
{
  if (k == K_TAB || k == K_SPACE)
  { 
    TTable ent ("%ENT");   
    TString16 dep;
    TString   descr;
    
    int ente = atoi(f.get());
    dep.format("%d", ente);
    
    ent.zero();
    ent.put("CODTAB", dep);  
    if (ent.read() == NOERR)
      descr = ent.get("S0");
    else
      descr = "";   
      
    f.mask().set(F_DESCRENTE, descr);  
  }
  
  return TRUE;
}

TMask* TDettaglio_H_app::get_mask(int mode)
{
  return _msk;
}

bool TDettaglio_H_app::user_create()
{
  _rel = new TRelation (LF_DETH);

  _msk = new TMask("771295a");       
  
  _msk->set_handler(F_ENTE, descr_ente);
  
  set_search_field(F_ANNO);

  return TRUE;
}

bool TDettaglio_H_app::user_destroy()
{
  delete _msk;
  delete _rel;

  return TRUE;
}

int m71295(int argc, char* argv[])
{
  TDettaglio_H_app a;
  a.run(argc, argv, "Dettaglio Quadro H");
  return 0;
}


