#include <execp.h>
#include <files.h>
#include <mailbox.h>
#include <prefix.h>
#include <relapp.h>
#include <tabutil.h>
#include <utility.h>

#include "ba4.h"
#include "ba4300.h"

#define FLD_UL1_CODDITTA 133
#define FLD_UL1_CODDITTA_NDITTE_RAGSOC 100
#define FLD_AT1_CODDITTA 102
#define FLD_AT1_CODDITTAH 198
#define FLD_AT1_PROGD_NDITTE_RAGSOC 105
#define FLD_SC1_CODDITTA 143
#define FLD_SC1_CODANAGRSOH 147
#define FLD_SC1_CODDITTA_NDITTE_RAGSOC 144
#define FLD_UL1_CODULC 125

const char* CODDITTA = "CODDITTA";
const char* CODATTPREV = "CODATTPREV";
const char* TIPOA = "TIPOA";
const char* CODANAGR = "CODANAGR";

const char* CODATT = "CODATT";

const char* COMRF = "COMRF";
const char* INDRF = "INDRF";
const char* CIVRF = "CIVRF";
const char* CAPRF = "CAPRF";
const char* COMRES = "COMRES";
const char* INDRES = "INDRES";
const char* CIVRES = "CIVRES";
const char* CAPRES = "CAPRES";

const char* CODULC = "CODULC";
const char* COMULC = "COMULC";
const char* INDULC = "INDULC";
const char* CIVULC = "CIVULC";
const char* CAPULC = "CAPULC";
const char* COMCCIAA = "COMCCIAA";
const char* COMTRIB = "COMTRIB";

#define TABATT "%ais"
#define CODTAB "CODTAB"
#define S0               "S0"
#define DESCR    "DESCR"

class TDitte_application : public TRelation_application
{
  TMask* _msk;
  TRelation *_rel;
  TLocalisamfile* _anag;
  TLocalisamfile* _unloc;
  TLocalisamfile* _attiv;
  TLocalisamfile* _tabcom;
  TString16  _oldattprev;

protected:
  bool user_create() ;
  bool user_destroy() ;
  virtual TMask* get_mask(int) { return _msk;}
  virtual bool changing_mask(int) { return FALSE;}
  virtual void init_query_mode(TMask& m) ;
  virtual void init_insert_mode(TMask& m) ;
  void enable_reg(TMask& m) ;
  virtual void init_modify_mode(TMask& m) ;
  virtual bool protected_record(TRectype& rec) { return prefix().exist(rec.get_long("CODDITTA")); }
  void set_att_prev(const TMask& m);

public:
  TDitte_application();
  virtual TRelation* get_relation() const { return _rel;}
  virtual int rewrite(const TMask& m);
  virtual int write(const TMask& m);
  virtual ~TDitte_application() {}
};

bool ba4300_handler(TMask& m, KEY k)

{    
  TDitte_application&  app = (TDitte_application&)main_app();

  if (k == (K_SHIFT+K_F12) && m.mode() == MODE_MOD)
  {
    m.enable(FLD_GD1_TIPOA);
    m.enable(FLD_GD1_CODANAGR);
    m.enable(FLD_GD1_CODANAGR_ANAGR_RAGSOC);
    return TRUE;
  }

  if (k != K_F5 && k != K_F6 && k != K_F7 && k != K_F8)
    return TRUE;
  if (m.mode() == MODE_MOD || m.mode() == MODE_INS)
  {
    if (m.mode() == MODE_INS)
    {
      if (!m.check_fields()) return FALSE;
      if (app.write(m) != NOERR) return TRUE;
      m.stop_run(K_ESC);
    }
    TString appname;
    int fld = 0, fld1 = 0, fldbrowse = 0;

    if (k == K_F5)
    {
      appname = "ba4 -3";
      fld = FLD_UL1_CODDITTA;
      fld1 = FLD_UL1_CODDITTA_NDITTE_RAGSOC;
      fldbrowse = FLD_UL1_CODULC;
    }
    if (k == K_F6) 
    {
      appname = "ba4 -4";
      fld = FLD_AT1_CODDITTA;
      fld1 = FLD_AT1_PROGD_NDITTE_RAGSOC;
      fldbrowse = FLD_AT1_CODDITTAH;
    }
    if (k == K_F7)
    {
      appname = "ba4 -5";
      fld = FLD_SC1_CODDITTA;
      fld1 = FLD_SC1_CODDITTA_NDITTE_RAGSOC;
      fldbrowse = FLD_SC1_CODANAGRSOH;
    }
    if (k == K_F8) { appname = "ba3 -0 reg"; fld = 0;fldbrowse = 0;}
    if (appname.not_empty())
    {
      TString body(16);
      //      TLocalisamfile& d = app.get_relation()->lfile();

      if (fldbrowse)
        body << fldbrowse << "|";
      if (fld)
        body << fld << "=" << m.get(FLD_GD1_CODDITTA) ;
      if (fld1)
        body << "|" << fld1 << "=" ;
      if (fldbrowse || fld)
      {
        TMessage  msg(cmd2name(appname), MSG_FS, (const char*) body);

        TMailbox mb;
        mb.send(msg);
      }
      //      d.setkey(1);
      //      d.zero();
      //      d.put("CODDITTA",  m.get(FLD_GD1_CODDITTA));
      //      d.read(_isequal, _unlock) ;
      TExternal_app a(appname);
      const long oldditta = main_app().get_firm();
      if (k == K_F8) main_app().set_firm(m.get_long(FLD_GD1_CODDITTA));
      a.run();
      if (k == K_F8) main_app().set_firm(oldditta);
      //      d.read(_iscurr, _lock) ;
      m.set_focus();
      return FALSE;
    }
  }
  return TRUE;
}


TDitte_application::TDitte_application()
{}


bool TDitte_application::user_create() // initvar e arrmask
{
  _msk = new TMask("ba4300a") ;
  _msk->set_handler(ba4300_handler);
  _rel = new TRelation(LF_NDITTE);
  _anag = new TLocalisamfile(LF_ANAG);
  _unloc = new TLocalisamfile(LF_UNLOC);
  _attiv = new TLocalisamfile(LF_ATTIV);
  _tabcom = new TLocalisamfile(LF_TABCOM);
  set_search_field(FLD_GD1_CODDITTA);
  if (argc() > 2)
  {
    get_mask(MODE_QUERY);
    TString16 s("1|");

    s << argv(2);
    set_link(*_msk, s);
  }
  return TRUE;
}


bool TDitte_application::user_destroy() // releasev e arrmask
{
  delete  _msk;
  delete _rel;
  delete _anag;
  delete _unloc;
  delete _attiv;
  delete _tabcom;
  return TRUE;
}

void TDitte_application::set_att_prev(const TMask& m)
{
  const TString16 attprev(m.get(FLD_GD1_CODATTPREV));

  if (_oldattprev == attprev)
    return;

  TRectype& ditte=_rel->curr();
  TString codditta(ditte.get(CODDITTA)) ;

  if (_oldattprev.not_empty())
  {
    _attiv->zero() ;
    _attiv->put(CODDITTA, codditta) ;
    _attiv->put(CODATT, _oldattprev);
    if (_attiv->read(_isequal, _lock) == NOERR)
    {
      _attiv->put("ATTPREV", FALSE);
      if (_attiv->rewrite() != NOERR)
        error_box("Non posso aggiornare l'attivita' prevalente precedente : errore n. %d", _attiv->status());
    }
  }
  _attiv->zero() ;
  _attiv->put(CODDITTA, codditta) ;
  _attiv->put(CODATT, attprev) ;
  
  const bool itwas = _attiv->read(_isequal, _lock) == NOERR;
  if (!itwas)
  {
    _attiv->zero() ;
    _attiv->put(CODDITTA, codditta) ;
    _attiv->put(CODATT, attprev) ;

    TTable tabatt = TTable(TABATT);

    tabatt.zero();
    tabatt.put(CODTAB, ditte.get(CODATTPREV)) ;
    if (tabatt.read() == NOERR)
      _attiv->put(DESCR, tabatt.get(S0)) ;
  }
  _attiv->put("ATTPREV", TRUE) ;
  if (itwas)
  {
    if (_attiv->rewrite() != NOERR)
      error_box("Non posso aggiornare l'attivita' prevalente : errore n. %d", _attiv->status());
  }
  else
  {
    if (_attiv->write() != NOERR)
      error_box("Non posso creare l'attivita' prevalente : errore n. %d", _attiv->status());
  }
}

int TDitte_application::write(const TMask& m)
{
  const int err = TRelation_application::write(m);
  if (err == NOERR)
  {
    TRectype& ditte=_rel->curr();
    
    TString codditta(ditte.get(CODDITTA)) ;
    
    set_att_prev(m);
    
    _anag->zero() ;
    _anag->put(TIPOA, ditte.get(TIPOA));
    _anag->put(CODANAGR, ditte.get(CODANAGR));
    _anag->read();
    if (_anag->good())
    {
      _unloc->zero() ;
      _unloc->put(CODDITTA, (const char*) codditta) ;
      _unloc->put(CODULC, (int) 1) ;
      TString s(_anag->get(COMRF));
      if (s.empty())
      {
        _unloc->put(COMULC, _anag->get(COMRES));
        _unloc->put(INDULC, _anag->get(INDRES));
        _unloc->put(CIVULC, _anag->get(CIVRES));
        _unloc->put(CAPULC, _anag->get(CAPRES));
        _unloc->put(COMCCIAA, _anag->get(COMRES));
        _unloc->put(COMTRIB, _anag->get(COMRES));
      }
      else
      {
        _unloc->put(COMULC, (const char*) s);
        _unloc->put(INDULC, _anag->get(INDRF));
        _unloc->put(CIVULC, _anag->get(CIVRF));
        _unloc->put(CAPULC, _anag->get(CAPRF));
        _unloc->put(COMCCIAA, (const char*) s);
        _unloc->put(COMTRIB, (const char*) s);
      }
      _unloc->write();
    }
  }
  return err;
}

int TDitte_application::rewrite(const TMask& m)
{
  const int err = TRelation_application::rewrite(m);
  if (err == NOERR)
    set_att_prev(m);
  return err;
}

void TDitte_application::init_query_mode(TMask& m)
{
  if (filtered())
  {
    m.show(FLD_GD1_CODDITTAH);
    m.show(FLD_GD1_RAGSOCH);
    m.hide(FLD_GD1_CODDITTA);
    m.hide(FLD_GD1_RAGSOC);
  }
  else
  {
    m.show(FLD_GD1_CODDITTA);
    m.show(FLD_GD1_RAGSOC);
    m.hide(FLD_GD1_CODDITTAH);
    m.hide(FLD_GD1_RAGSOCH);
  }
  init_insert_mode(m);
}

void TDitte_application::init_insert_mode(TMask& m)
{
  if (!filtered())
  {
    m.enable(FLD_GD1_TIPOA);
    m.enable(FLD_GD1_CODANAGR);
    m.enable(FLD_GD1_CODANAGR_ANAGR_RAGSOC);
    m.show(FLD_GD1_CODDITTA);
    m.show(FLD_GD1_RAGSOC);
    m.hide(FLD_GD1_CODDITTAH);
    m.hide(FLD_GD1_RAGSOCH);
  }
  else
  {
    m.show(FLD_GD1_CODDITTAH);
    m.show(FLD_GD1_RAGSOCH);
    m.hide(FLD_GD1_CODDITTA);
    m.hide(FLD_GD1_RAGSOC);
  }
  m.disable(DLG_ATT);
  m.disable(DLG_ULC);
  m.disable(DLG_SOC);
  m.disable(DLG_REG);
  _oldattprev = "";
}

void TDitte_application::enable_reg(TMask& m)

{
  const long  codditta = m.get_long(FLD_GD1_CODDITTA);

  m.enable(DLG_REG, codditta > 0 && prefix().exist(codditta));
}

void TDitte_application::init_modify_mode(TMask& m)
{
  m.disable(FLD_GD1_TIPOA);
  m.disable(FLD_GD1_CODANAGR);
  m.disable(FLD_GD1_CODANAGR_ANAGR_RAGSOC);
  if (filtered())
  {
    m.show(FLD_GD1_CODDITTAH);
    m.show(FLD_GD1_RAGSOCH);
    m.hide(FLD_GD1_RAGSOC);
    m.hide(FLD_GD1_CODDITTA);
  }
  else
  {
    m.show(FLD_GD1_CODDITTA);
    m.show(FLD_GD1_RAGSOC);
    m.hide(FLD_GD1_CODDITTAH);
    m.hide(FLD_GD1_RAGSOCH);
  }
  m.enable(DLG_ATT);
  m.enable(DLG_ULC);
  m.enable(DLG_SOC);
  enable_reg(m);
  _oldattprev = m.get(FLD_GD1_CODATTPREV);
}

int ba4300(int argc, char* argv[])
{
  TDitte_application a ;
  a.run(argc, argv, "Anagrafica Ditte");
  return 0;
}

