//Stampa causali

#include <mask.h>
#include <printapp.h>
#include <relation.h>
#include <tabutil.h>
#include <utility.h>

#include <nditte.h>
#include <comuni.h>
#include <anagr.h>
#include <causali.h>
#include <rcausali.h>

#include "conto.h"
#include "cg1.h"
#include "cg1700.h"

enum descr {causale, conto};

class CG1700_application : public TPrintapp
{
  TRelation         * _rel;
  TLocalisamfile    * _clifo, * _pcon, * _nditte, * _anag, * _com;
  TCursor           * _cur;
  TMask             * _msk;
  TParagraph_string * _descr_causale, * _descr_conto;
  TDate _data;
  TString _cofi, _cap, _paiva, _ragsoc, _comunefis, _provfis, _viafis;

protected:
  virtual void preprocess_header();
  virtual bool preprocess_print(int,int);
  virtual bool preprocess_page(int,int);
  virtual bool set_print(int);

public:
  virtual bool user_create() ;
  virtual bool user_destroy();
  void get_dati_ditta();
  void setta_righe_descr(TParagraph_string*,enum descr);
  int  stampa_intestazione_ditta();
  
  CG1700_application() {}
};

TRectype& leggi_com (const char * cod, TLocalisamfile *comuni)
{
  comuni->zero();
  comuni->put(COM_COM, cod);
  comuni->read();
  if (comuni->bad())
    comuni->zero();

  return comuni->curr();
}

void CG1700_application::get_dati_ditta()
{
  TLocalisamfile nditte(LF_NDITTE); 
  TLocalisamfile anag(LF_ANAG); 
  TString        codanagr;
  TString        tipoa;

  nditte.zero();
  nditte.put(NDT_CODDITTA, get_firm());
  nditte.read();

  if (nditte.bad()) nditte.zero();

  codanagr = nditte.get(NDT_CODANAGR);
  tipoa    = nditte.get(NDT_TIPOA);
  _ragsoc  = nditte.get(NDT_RAGSOC);

  anag.setkey(1);
  anag.zero();
  anag.put (ANA_TIPOA, tipoa);
  anag.put (ANA_CODANAGR, codanagr);
  anag.read();
  if (anag.bad()) anag.zero();
  
  _cofi      = anag.get(ANA_COFI);
  _paiva     = anag.get(ANA_PAIV);
  _comunefis = anag.get(ANA_COMRF);

  if (_comunefis.empty()) 
    _comunefis   = anag.get(ANA_COMRES);
  
  TRectype dep = leggi_com (_comunefis, _com);

  _comunefis   = dep.get(COM_DENCOM);
  _provfis     = dep.get(COM_PROVCOM);
  _cap         = dep.get(COM_CAPCOM);
  if (_comunefis.empty()) 
  {
    _viafis = anag.get(ANA_INDRF);
    _viafis.rtrim();
    _viafis << " " << anag.get (ANA_CIVRF); 
  }
  else
  {
    _viafis = anag.get(ANA_INDRES);
    _viafis.rtrim();
    _viafis << " " << anag.get (ANA_CIVRES); 
  }
}

int CG1700_application::stampa_intestazione_ditta()
{
  int r = 1;
  TString16 codice_ditta;
  TString riga(132);

  get_dati_ditta();
  codice_ditta << get_firm(); 

  set_header (r, "Ditta %s %s %s %s %s %s", (const char*)codice_ditta,
              (const char*)_ragsoc, (const char*)_viafis,
              (const char*)_cap, (const char*)_comunefis,
              (const char*)_provfis);
  r++;
  printer().setdate(_data);
  riga = "Data @<  Pag. @#";
  riga.right_just(127);
  riga.overwrite (format ("Partita iva %s Codice fiscale %s", (const char*)_paiva, (const char*)_cofi));
  set_header (r, "%s", (const char*) riga);
  r++;

  return r; 
}

void CG1700_application::preprocess_header()
{
  int r;
  TString riga (132);
  
  reset_header();
  r = stampa_intestazione_ditta();

  set_header(r, "ELENCO CAUSALI");

  r++;
  riga.fill('-');
  set_header(r++, (const char*) riga);
  //riga = "";
  //set_header(r, (const char*) riga);
  set_header(r++, "@5gDescrizione@30gTipo Codice@55gDescrizione@80gDes Reg. All. Mod      Gest Num. Data Cau. Intr. Fat");
  set_header(r++, "Cod. Causale@30gDoc. Conto@51gD/A Conto@80gAgg IVA  IVA  770 Cesp Sald Doc. Doc. Rif. Op/v. Rit");
  set_header(r++, (const char*) riga);
}

bool CG1700_application::preprocess_print(int file, int counter)
{
  switch (file)
  {
  case LF_CAUSALI:
    reset_print();
    if (current_cursor()->items() == 0)
    {
      warning_box ("Non ci sono causali. La stampa e' annullata");
      return FALSE;
    }
    set_wait_message("Stampa in corso");
    break;
  case LF_RCAUSALI:
    break;
  default:
    break;
  }
  return TRUE;
}

bool CG1700_application::preprocess_page(int file,int count)
{
  TString cau_descr, rcau_descr;
  TString cau_reg (3);
  bool cau_alleg, cau_rfatt, cau_intra, cau_val;
  int rcau_gruppo, rcau_conto;
  long rcau_sottoconto;
  char rcau_tipocf;
  
  reset_print();
  switch (file)
  {
  case LF_CAUSALI:
    cau_descr = current_cursor()->file(LF_CAUSALI).get(CAU_DESCR);
    cau_reg   = current_cursor()->file(LF_CAUSALI).get(CAU_REG);
    cau_alleg = current_cursor()->file(LF_CAUSALI).get_bool(CAU_ALLEG);
    cau_rfatt = current_cursor()->file(LF_CAUSALI).get_bool(CAU_RITFATT);
    cau_intra = current_cursor()->file(LF_CAUSALI).get_bool(CAU_INTRACOM);
    cau_val   = current_cursor()->file(LF_CAUSALI).get_bool(CAU_MOVVAL);
    *_descr_causale = (const char*) cau_descr;
    setta_righe_descr(_descr_causale,causale);
    set_row(1, "@0g@3s",   FLD(LF_CAUSALI,CAU_CODCAUS));
    set_row(1, "@30g@2s",  FLD(LF_CAUSALI,CAU_TIPODOC));
    set_row(1, "@84g@3s",  FLD(LF_CAUSALI,CAU_REG));
    if (cau_reg.not_empty())
      //set_row(1, "@89g@f", FLD(LF_CAUSALI,CAU_ALLEG));
      if (cau_alleg) 
        set_row(1, "@89gNo"); 
      else set_row(1, "@89gSi"); 
    set_row(1, "@95g@1s",  FLD(LF_CAUSALI,CAU_M770));
    set_row(1, "@99g@1s",  FLD(LF_CAUSALI,CAU_COLLCESP));
    set_row(1, "@104g@1n", FLD(LF_CAUSALI,CAU_TIPOMOV));
    set_row(1, "@108g@f",  FLD(LF_CAUSALI,CAU_NUMDOC));
    set_row(1, "@113g@f",  FLD(LF_CAUSALI,CAU_DATADOC));
    set_row(1, "@118g@3s", FLD(LF_CAUSALI,CAU_CODCAUSIM));
    if (cau_intra)
      set_row(1, "@123g@f",  FLD(LF_CAUSALI,CAU_INTRACOM));
    if (cau_val)   
      set_row(1, "@126g@f",  FLD(LF_CAUSALI,CAU_MOVVAL));
    if (cau_rfatt)
      set_row(1, "@130g@f",  FLD(LF_CAUSALI,CAU_RITFATT));
    break;
  case LF_RCAUSALI:
  {
    //rcau_descr = current_cursor()->file(LF_RCAUSALI).get(RCA_DESC);
    rcau_gruppo     = current_cursor()->file(LF_RCAUSALI).get_int(RCA_GRUPPO);
    rcau_conto      = current_cursor()->file(LF_RCAUSALI).get_int(RCA_CONTO);
    rcau_sottoconto = current_cursor()->file(LF_RCAUSALI).get_long(RCA_SOTTOCONTO);
    rcau_tipocf     = current_cursor()->file(LF_RCAUSALI).get_char(RCA_TIPOCF);
    TConto tc (rcau_gruppo,rcau_conto,rcau_sottoconto,rcau_tipocf);
    //rcau_tipocf = tc.tipo();
    rcau_descr  = tc.descrizione();
    *_descr_conto = (const char*) rcau_descr;
    setta_righe_descr(_descr_conto,conto);
    set_row (1, "@35g@3n",  FLD(LF_RCAUSALI,RCA_GRUPPO));
    set_row (1, "@39g@3n",  FLD(LF_RCAUSALI,RCA_CONTO));
    set_row (1, "@43g@6n",  FLD(LF_RCAUSALI,RCA_SOTTOCONTO));
    set_row (1, "@51g@1s",  FLD(LF_RCAUSALI,RCA_SEZIONE));
    set_row (1, "@80g@3s",  FLD(LF_RCAUSALI,RCA_CODDESC));
    break;
  }
  default:
    break;
  }
  return TRUE;
}

void CG1700_application::setta_righe_descr(TParagraph_string* str, enum descr desc)
{
  const char* r;
  int i = 1;
  TString frm;

  switch (desc)
  {
  case causale:
    frm = "@5g%s";
    break;
  case conto:
    frm = "@55g%s";
    break;
  default:
    break;
  }
  while ((r = str->get()) != NULL)
  {
    set_row (i, (const char*)frm, r);
    i++;
  }
}

bool CG1700_application::set_print(int)
{
  const KEY tasto = _msk->run();
  if (tasto == K_ENTER)
  { 
    _data = _msk->get(F_DATASTAMPA);
    return TRUE;
  }
  return FALSE;
}


bool CG1700_application::user_create()
{
  _msk = new TMask("cg1700a");
  _rel = new TRelation (LF_CAUSALI);
  _rel->add(LF_RCAUSALI,"CODCAUS==CODCAUS",1);
  
  _cur = new TCursor (_rel,"", 1);
  add_cursor(_cur);

  _com    = new TLocalisamfile(LF_COMUNI); 
  _pcon   = new TLocalisamfile(LF_PCON);
  _clifo  = new TLocalisamfile(LF_CLIFO);
  _nditte = new TLocalisamfile(LF_NDITTE);
  _anag   = new TLocalisamfile(LF_ANAG);

  _descr_conto   = new TParagraph_string ("",23);
  _descr_causale = new TParagraph_string ("",23);
  add_file(LF_CAUSALI);
  add_file(LF_RCAUSALI,LF_CAUSALI);
  return TRUE;
}

bool CG1700_application::user_destroy() 
{ 
  delete _msk;
  delete _cur;
  delete _rel;
  delete _clifo;
  delete _com;
  delete _pcon;
  delete _nditte;
  delete _anag;
  delete _descr_conto;
  delete _descr_causale;
  return TRUE;
}

int cg1700(int argc, char* argv[])
{
  CG1700_application a;
  a.run(argc, argv, "Stampa causali");
  return 0;
}


