// 772300.cpp - Lista movimenti percipienti
//
// 16. 1.96  Stampa solo le schede che hanno almeno un pagamento con data
//           compresa negli estremi richiesti
//
#include <mask.h>
#include <printapp.h>
#include <relation.h>
#include <tabutil.h>
#include <utility.h>
#include <sort.h>
#include <assoc.h>     
#include <config.h>     
#include <lffiles.h>
#include "scperc.h"
#include "rpag.h"
#include "rver.h"
#include <comuni.h>
#include <nditte.h>
#include <anagr.h>
#include <anafis.h>
#include "perc.h"
#include <mailbox.h>
#include <execp.h>

#include "772.h"
#include "772300.h"
#include "77lib.h"

HIDDEN TString256   __tmp;
HIDDEN bool filter_func (const TRelation *);

class TStampa_mov : public TPrintapp
{  
  char        _liv;       // Se stampa a livello di studio
  int         _anno_dic;  // Anno dichiarazione nei paramentri utente o studio
  int       _row;   // Riga corrente di stampa  
  TString16     _section;
  TConfig*      _cnf;
  TVersamento_stampa  _stvers;
  TRelation*        _rel;
  TCursor*          _cur;   
  TLocalisamfile*   _nditte;
  TLocalisamfile*   _anag;
  TLocalisamfile*   _rpag;
  TLocalisamfile*   _rver;
  TArray            _pagver;
  TPercipiente    _curr_perc, _prec_perc;                                     
  TString       _ragsocER,_ragsocPE,_datadoc,_numdoc,_datastampa;         
  int           _anno, _codcaus, _codtrib, _ind, _nprog;
  long          _codditta,_codanagER,_codanagPE,_cod_da,_cod_a;
  char          _tipoa,_tipoa_da,_tipoa_a;
  real          _tot_imponpag,_tot_ritenpag,_tot_impvers;
  real        _g_tot_imp, _g_tot_ver, _g_tot_rit;
  TDate         _datada,_dataa;
protected:
  friend bool   filter_func (const TRelation *);
  virtual void      on_config_change();
  virtual bool      user_create() ;
  virtual bool      user_destroy();
  virtual bool      set_print(int m);
  virtual bool      preprocess_page (int,int);
  virtual bool      preprocess_print(int,int);
  virtual print_action  postprocess_page (int,int);
  bool            init_print();
  void          set_total_perc();
  void          set_global_total();
  void          add(TVersamento& vers);
  bool          find(TVersamento& vers);
public:
  void dati_erogante();
//  void intesta_fissa();  
  void stampa_dati_erog_perc();
  void ragione_sociale();
  int  ricerca_causale();
  void setta_righe_documento();
  void setta_righe_pagamenti(const TString&,const real&,const TString&,const real&,int);
  void setta_righe_versamenti(char,char,const TString&,const TString&,const TString&,const real&,int); 
  static bool codditta_hnd(TMask_field& f, KEY k);
// D = stampa ditta, S = stampa a livello di studio 
  TStampa_mov(char livello='D') : _liv(toupper(livello)) {}
  virtual ~TStampa_mov() {}
};

HIDDEN TStampa_mov& app() { return (TStampa_mov&) main_app(); }

bool filter_func (const TRelation * rel)
{ 
  const int anno_corr = app()._anno;
  TRectype  sch_curr(rel->lfile().curr());
  TSchedaP  scheda(sch_curr);
  return esiste_pag_in_anno(scheda, anno_corr);
}                                        
                  
bool TStampa_mov::codditta_hnd(TMask_field& f, KEY k)
{         
  if (k == K_TAB && !(f.mask().is_running()) )
  {
    TString16 codditta; codditta << app()._codditta;
    if (codditta != "0")        
    {
      f.set(codditta);
      f.check();
    }
  }   
  return TRUE;                                           
}

void TStampa_mov::on_config_change()
{ 
  TConfig conf(_liv == 'S' ? CONFIG_USER : CONFIG_STUDIO);
  _anno_dic = (int)conf.get_long(ANNO_SEL, _section); 
}

HIDDEN int compare_rows(const TObject** o1, const TObject** o2)
{
  TToken_string* r1 = (TToken_string*)*o1;
  TToken_string* r2 = (TToken_string*)*o2;

  long np1 = atol(r1->get(0));
  long nv1 = atol(r1->get(1));
  long np2 = atol(r2->get(0));
  long nv2 = atol(r2->get(1));
  
  if (nv1 == nv2 && np1 == np2)
    return 0;
    
  if (nv1 > nv2)
    return 1;
    
  if (nv1 < nv2)
    return -1;
    
  if (nv1 == nv2)
    if (np1 > np2)
      return 1;
    else
      return -1;
      
  return 0;
}
                                         
int TStampa_mov::ricerca_causale()
{                           
  TTable ca7 ("%CA7");   
  TString16 dep;
  int cod = 0;
  
  ca7.zero();
  dep.format("%02d", _codcaus);
  ca7.put("CODTAB", dep);      
  if (ca7.read() == NOERR)
    cod    = ca7.get_int("I0"); 
  
  return cod;
}
                                         
bool TStampa_mov::preprocess_print(int file, int counter)
{
  if (file == LF_SCPERC)
  {
    _tot_imponpag  = ZERO;
    _tot_ritenpag  = ZERO;
    _tot_impvers   = ZERO;
  }                   
  return TRUE;
}

void TStampa_mov::ragione_sociale()
{
  _ragsocPE = "";
  _anag->setkey(1);
  _anag->zero();
  _anag->put(ANA_TIPOA,    _tipoa);
  _anag->put(ANA_CODANAGR, _codanagPE);
  if (_anag->read() == NOERR)
  {  
    _ragsocPE = _anag->get(ANA_RAGSOC);
    _ragsocPE.trim();    
    if (_tipoa == 'F')
    {
      TString80 nome = _ragsocPE.mid(30);
      _ragsocPE.cut(30);
      _ragsocPE.trim(); nome.trim();
      _ragsocPE << ' ' << nome;
    }
  }                   
  set_row (_row, "%c %5ld %-50s", _tipoa, _codanagPE, (const char*) _ragsocPE);
}
                         
void TStampa_mov::setta_righe_documento()
{ 
  set_row (_row, "%-4d",       _nprog);    /* Prima era 2 */
  if (_codcaus != 0)
    set_row (_row, "@5g%02d", _codcaus);
  if (_codtrib != 0)
    set_row (_row, "@8g%4d",  _codtrib);

  set_row (_row, "@13g%10s",  (const char*) _datadoc);  
  set_row (_row, "@24g%-7s",  (const char*) _numdoc);
}
      
void TStampa_mov::setta_righe_pagamenti(const TString& datapag,const real& imponpag,
                                        const TString& percent,const real& ritenpag,int i)
{
  set_row (i, "@32g%10s",   (const char*) datapag);
  set_row (i, "@43g%15.0r", &imponpag);
  set_row (i, "@59g%s", (const char*) percent);  
  set_row (i, "@65g%12r", &ritenpag);
} 
                        
void TStampa_mov::setta_righe_versamenti(char luovers,char tipovers,const TString& dataver,
                                          const TString& serie,const TString& numero,const real& impvers,int j)
{
  if (impvers != ZERO)
  {
  set_row (j, "@79g%c",      luovers);    
  set_row (j, "@82g%c",      tipovers);
  set_row (j, "@84g%10s",    (const char*) dataver);
  set_row (j, "@97g%s",      (const char*) serie);
  set_row (j, "@103g%s",     (const char*) numero);
  set_row (j, "@116g%15.0r", &impvers);
  }
}

void TStampa_mov::set_total_perc()
{
  set_row (_row, "@20gTOTALE PERCIPIENTE@43g%15.0r", &_tot_imponpag);
  set_row (_row, "@62g%15.0r",  &_tot_ritenpag);
  set_row (_row, "@116g%15.0r", &_tot_impvers);
  set_row (_row+1, "");

  _g_tot_imp += _tot_imponpag;
  _g_tot_rit += _tot_ritenpag;
  _g_tot_ver += _tot_impvers;
        
  _tot_imponpag = _tot_ritenpag = _tot_impvers  = ZERO;
}

void TStampa_mov::set_global_total()
{
  set_row (_row, "@20gTOTALE GENERALE   @43g%15.0r", &_g_tot_imp);
  set_row (_row, "@62g%15.0r",  &_g_tot_rit);
  set_row (_row, "@116g%15.0r", &_g_tot_ver);
  set_row (_row+1, "");
}

void TStampa_mov::add(TVersamento& vers)
{
  _stvers.add(vers);
}

bool TStampa_mov::find(TVersamento& vers)
{
  return _stvers.find(vers);
}

bool TStampa_mov::preprocess_page(int file, int counter)
{      
  TString datapag,dataver,serie,numero;        
  TDate   datadoc,datav;
  real    imponpag,ritenpag,impvers,dep,dep2;  
  char    luovers,tipovers;
  real    percent;
  int     i,j, numvers;
  
  i = j = _row = 1;
        
  if (counter)
    return TRUE;
    
  if (file == LF_SCPERC)
  { 
    reset_print();
    TRectype sch_curr(current_cursor()->curr(LF_SCPERC));      
    _tipoa     = current_cursor()->curr(LF_SCPERC).get     (SPR_TIPOA)[0];
    _codanagPE = current_cursor()->curr(LF_SCPERC).get_long(SPR_CODANAGR);
    _nprog     = current_cursor()->curr(LF_SCPERC).get_int(SPR_NPROG); 
    _codcaus   = current_cursor()->curr(LF_SCPERC).get_int (SPR_CODCAUS);  
    _codtrib   = ricerca_causale();
    datadoc    = current_cursor()->curr(LF_SCPERC).get_date(SPR_DATADOC);
    _datadoc   = datadoc.string();
    _numdoc    = current_cursor()->curr(LF_SCPERC).get     (SPR_NUMDOC);

    const int anno_doc = datadoc.year();
    TSchedaP scheda(sch_curr);
    bool esiste_pag_anno_in_periodo = esiste_pag_in_periodo(scheda, _datada, _dataa);
    bool stampa_scheda = (anno_doc == _anno_dic) || esiste_pag_anno_in_periodo;
    if (!stampa_scheda)
      return TRUE;
      
    _curr_perc.set(_tipoa, _codanagPE);
    if (_curr_perc != _prec_perc)
    {
      if (_prec_perc.not_empty()) 
      {
        set_total_perc();
        _row = 3;
      }
      _prec_perc = _curr_perc;
      ragione_sociale();
      _row++;
    }
    
    setta_righe_documento();

    i = j = _row;              
    TVersamento_stampa ve;
    int nriga = 0;
    TRectype recpag(_rpag->curr());
    recpag.zero();
    recpag.put(SPR_CODDITTA,   _codditta);
    recpag.put(SPR_TIPOA,      _tipoa);
    recpag.put(SPR_CODANAGR,   _codanagPE);
    recpag.put(SPR_NPROG,      _nprog);
    _rpag->setkey(1);
    _rpag->zero();
    _rpag->curr() = recpag;

    for (_rpag->read(_isgteq); _rpag->good(); _rpag->next())
    {
      if (_rpag->curr() > recpag) break;
      TDate datap(_rpag->get_date(PAG_DATAPAG));
      if (datap < _datada || datap > _dataa) continue;
        
      datapag     = datap.string();
      imponpag    = _rpag->get_real(PAG_IMPONIBILE);
      percent     = _rpag->get_real(PAG_PERC);
      TString16 st_perc(percent.string("###,@@"));      
      ritenpag    = _rpag->get_real(PAG_RITENUTA); 
      numvers     = _rpag->get_int(PAG_NUMVERS);   
      nriga       = _rpag->get_int(PAG_NRIGA);
      _tot_imponpag += imponpag;
      _tot_ritenpag += ritenpag;

      setta_righe_pagamenti(datapag,imponpag,st_perc,ritenpag,i++);      

      if (numvers < 0) continue;  

// Stampa estremi versamento
      real vers_stampa = ZERO;
      TLocalisamfile rver (LF_RVER);
      rver.zero();
      rver.put(VER_CODDITTA, _codditta);
      rver.put(VER_TIPOA,    _tipoa);
      rver.put(VER_CODANAGR, _codanagPE);
      rver.put(VER_NPROG,    _nprog);
      rver.put(VER_NRIGA,    numvers);

      if (rver.read() == NOERR) 
      {
        luovers  = rver.get(VER_LUOVERS)[0];
        tipovers = rver.get(VER_TIPOVERS)[0];
        datav    = rver.get_date(VER_DATAVERS);
        dataver  = datav.string();
        serie    = rver.get(VER_SERIE);      
        numero   = rver.get(VER_NUMERO);
        impvers  = rver.get_real(VER_IMPVERS); 
        ve.set(_codditta,_tipoa,_codanagPE,_nprog, numvers, impvers);
                           
        const bool gia_visto = find(ve);
          
        if (gia_visto)
          impvers = ve.importo_versato_residuo();
        else
        {
          _tot_impvers += impvers;          
          add(ve);
        }

// NB - in vers_stampa() viene fatta anche la add
        vers_stampa = ve.vers_stampa(_rpag, numvers, impvers, ritenpag);                    
//        if (vers_stampa != ZERO)
        setta_righe_versamenti(luovers,tipovers,dataver,serie,numero,vers_stampa,j++);
      }  
    } 
  }  
  return TRUE;
}

print_action TStampa_mov::postprocess_page(int file, int counter)
{                                                                      
  if (counter)
  {
    //printer().formfeed();
    reset_print();     // per evitare che stampi di nuovo il totale prec.  
    return NEXT_PAGE;
  }

  if (file == LF_SCPERC)
  {                                                  
    if (_cur->pos() >= _cur->items()-1)  // Se son sull'ultimo
    {
      reset_print();
      _row = 1;
      set_total_perc();
      _row++;
      set_global_total();
      return REPEAT_PAGE;
    }
  }
  return NEXT_PAGE;
}

bool TStampa_mov::set_print(int)
{
  long  codditta_prec;
  int   i = 0;

  if (_liv == 'S')
  {
    codditta_prec = get_firm_770();
    while ((_codditta = _cnf->get_long(DITTE_SEL, _section, i++)) != 0L) 
    {
      set_firm_770(_codditta);
   
// init_print ritorna TRUE solo se K_ENTER      
      if (init_print())
        print();
      else break;
    }  
    set_firm_770(codditta_prec);
// Non richiamare di nuovo print()!    
    return FALSE;
  }
  else
    return init_print();
}

bool TStampa_mov::init_print()
{
  TMask msk ("772300a");
  KEY tasto;
  TLocalisamfile* fl;

  msk.set_handler (F_CODDITTA, codditta_hnd);

  printer().footerlen(3);
  
  _g_tot_imp = _g_tot_rit = _g_tot_ver = ZERO;
        
  tasto = msk.run();

  if (tasto == K_ENTER)
  {
    _tipoa_da = msk.get     (F_TIPODA)[0];
    _cod_da   = msk.get_long(F_CODDA);
    _tipoa_a  = msk.get     (F_TIPOA)[0];
    _cod_a    = msk.get_long(F_CODA);   
      
    TDate datada (msk.get(F_DATADA));
    TDate dataa  (msk.get(F_DATAA)); 
    
    TString data1 = datada.string();
    TString data2 = dataa.string();
    
    _datada = data1 == "" ? botime : datada;
    _dataa  = data2 == "" ? eotime : dataa;
     
    TDate data (msk.get(F_DATASTAMPA));
    _datastampa = data.string();
    
    reset_files();         //resetta l'albero di stampa
    add_file(LF_SCPERC);
 
    fl = &(current_cursor()->file(LF_SCPERC));
    TRectype da (fl->curr());
    TRectype a  (fl->curr());  

    da.zero();
    a.zero();
     
    da.put(SPR_CODDITTA, _codditta);        
    da.put(SPR_TIPOA,    _tipoa_da);
    da.put(SPR_CODANAGR, _cod_da);

    a.put(SPR_CODDITTA,  _codditta);        
    a.put(SPR_TIPOA,     _tipoa_a);
    a.put(SPR_CODANAGR,  _cod_a);

    current_cursor()->setregion(da, a);
                        
// viene ordinato per numero pagamento e numero versamento 
//    _pagver.sort(compare_rows); 
    dati_erogante();
    stampa_dati_erog_perc();    
    //intesta_fissa();  
    return TRUE;
  }
  else
    return FALSE;
}

void TStampa_mov::dati_erogante()
{ 
  char    tipoa;
  bool  ditta_ok = FALSE;
         
  _ragsocER  = "";
  _codanagER = 0L;
                 
  _nditte->setkey(1);
  _nditte->curr().zero();
  _nditte->curr().put(NDT_CODDITTA, _codditta);
  ditta_ok = _nditte->read() == NOERR;
  if (ditta_ok)
  {
    tipoa      = _nditte->get_char(NDT_TIPOA);
    _codanagER = _nditte->get_long(NDT_CODANAGR);
  }

  if (ditta_ok)
  { 
    _anag->setkey(1);
    _anag->zero();
    _anag->put(ANA_TIPOA,    tipoa);
    _anag->put(ANA_CODANAGR, _codanagER);
    if (_anag->read() == NOERR)
    {  
      _ragsocER = _anag->get(ANA_RAGSOC);
      if (tipoa == 'F')
      {
        TString80 nome = _ragsocER.mid(30);
        _ragsocER.cut(30);
        _ragsocER.trim(); nome.trim();
        _ragsocER << ' ' << nome;
      }
    }
  }
}

void TStampa_mov::stampa_dati_erog_perc()
{ 
  TString sep;
  TString app(132);
  reset_header();

// 7.12.95 Numero di pagina qui!
  set_header (1, "ELENCO MOVIMENTI PER PERCIPIENTE @121gPag. @#"); 
  set_header (3, "SOGGETTO EROGANTE"); 
  set_header (3, "@19g%5d", _codditta);  // 20.11.95 codditta e NON codanag
  set_header (3, "@25g%s",  (const char*) _ragsocER);
  set_header (3, "@80gData@85g%10s", (const char*) _datastampa);
  sep << "@#";
  sep.right_just(125); 
  set_header(3,(const char*) sep);
  app.fill('_');
  set_header (5, (const char*) app);
  set_header (6, "Num.@5gCod.@13gDocumento@32gData@78gEstremi versamento@97gSerie@103gNumero");
  set_header (7, "Reg.@5gCaus.@13gData@24gNumero@48gImponibili@62g%%@69gRitenute@78gLV@81gTV@84gData@97gABI@103gCAB@116gImporto versato");
  set_header (8, (const char*) app);
}

/*
// Crea l'intestazione per la stampa a 132 e a 198 caratteri
void TStampa_mov::intesta_fissa()
{ 
  set_background();
  set_background("W4l{1 5 198 5} W4l{1 8 198 8}");
}
*/          

bool TStampa_mov::user_create()
{
  // Parametri configurazione
  _section = name(); 
  _section.cut(2); 
  _cnf = new TConfig(CONFIG_USER, _section);
  _rel    = new TRelation (LF_SCPERC);
  _cur    = new TCursor(_rel,"",2); 
//  _cur->set_filterfunction (filter_func);
  add_cursor(_cur);
  
  _nditte = new TLocalisamfile (LF_NDITTE);
  _anag   = new TLocalisamfile (LF_ANAG);
  _rpag   = new TLocalisamfile (LF_RPAG);
  _rver   = new TLocalisamfile (LF_RVER);
  _codditta = get_firm_770();

  return TRUE;
}

bool TStampa_mov::user_destroy()
{
  delete _rel;
  delete _cur;   
  delete _nditte;
  delete _anag;
  delete _rpag;
  delete _rver;
  delete _cnf;
  return TRUE;
}

int m72300(int argc, char* argv[])
{
  TStampa_mov a(*argv[2]);
  a.run(argc, argv, "Stampa movimenti per percipienti");
  return 0;
}
