#include <relapp.h>

#include "cg0.h"
#include "cg0300.h"

class CG0300_application : public TRelation_application
{
  TMask* _msk;
  TRelation *_rel;
  TLocalisamfile* _ditte;
  TLocalisamfile* _attiv;
  TLocalisamfile* _clifo;

  bool user_create();
  bool user_destroy();
  virtual TMask* get_mask(int mode) {return _msk;}
  virtual bool changing_mask(int mode) {return FALSE;}
  virtual TRelation* get_relation() const {return _rel;}
  virtual void init_query_mode(TMask& m);
  virtual void init_query_insert_mode(TMask& m);

public:
  CG0300_application() {}
};

void CG0300_application::init_query_mode(TMask& m)

{ 
  disable_menu_item(M_FILE_NEW);
  m.send_key(K_SHIFT + K_CTRL + 's', -1);
  m.send_key(K_SHIFT + K_CTRL + 'h', -2);
}

void CG0300_application::init_query_insert_mode(TMask& m)

{
  disable_menu_item(M_FILE_NEW);
  m.send_key(K_SHIFT + K_CTRL + 'h', -1);
  m.send_key(K_SHIFT + K_CTRL + 's', -2);
}


HIDDEN bool key_handler (TMask_field& f, KEY k)

{
  if (k == K_TAB)
  {
    TMask& m = f.mask();

    if (m.get(F_ANNO).not_empty() && m.get(F_CODATT).not_empty() &&
        m.get(F_CODCF).not_empty())
      m.stop_run(K_AUTO_ENTER);
  }
  return TRUE;
}

bool CG0300_application::user_create() 
{
  _msk = new TMask("cg0300a") ;
  _msk->set_handler (F_IMMESSO, key_handler);
  _rel = new TRelation(LF_ALLEG);
  _ditte = new TLocalisamfile(LF_NDITTE);
  _attiv = new TLocalisamfile(LF_ATTIV);
  _clifo = new TLocalisamfile(LF_CLIFO);
  const long ditta = get_firm();
  _msk->set(F_CODDITTA, ditta);
  _ditte->zero();
  _ditte->put(N_CODDITTA, ditta);
  _ditte->read();
  if (_ditte->good())
    _msk->set(F_CODATT,_ditte->get(N_CODATTPREV));
  return TRUE;
  
}

bool CG0300_application::user_destroy()
{
  delete  _msk;
  delete  _rel;
  delete  _ditte;
  delete  _attiv;
  delete  _clifo;
  return TRUE;
}

int cg0300(int argc, char* argv[])
{
  CG0300_application a;
  a.run(argc, argv, "Immissione progressivi allegati");
  return 0;
}
