#include "cg6901.h"
#include "cglib04.h"

#include <mailbox.h>
#include <printer.h>
#include <tabutil.h>
#include <urldefid.h>

#include <nditte.h>

#define MAX_SYSTEM   real("100000000000") // Gli importi debbono essere minore di questo

HIDDEN inline TLista_archivi& app() { return (TLista_archivi&)main_app();}

bool TLista_archivi::create()
{
  _ditte   = new TLocalisamfile (LF_NDITTE);
  _caus    = new TLocalisamfile (LF_CAUSALI);
  _rcaus   = new TLocalisamfile (LF_RCAUSALI);
  _clifo   = new TLocalisamfile (LF_CLIFO);
  _pcon    = new TLocalisamfile (LF_PCON);
  _mov     = new TLocalisamfile (LF_MOV);
  _rmov    = new TLocalisamfile (LF_RMOV);
  _rmoviva = new TLocalisamfile (LF_RMOVIVA);      
  _part    = new TLocalisamfile (LF_PARTITE);
  _scad    = new TLocalisamfile (LF_SCADENZE);
  _pagsca  = new TLocalisamfile (LF_PAGSCA);
  
  _errore_grave = FALSE;
  _risposta     = 0;

  TMailbox m;
  TMessage* msg = m.next_s("");
  TString subj;
     
  if (msg != NULL)
  {
    subj = msg->body();
    _datalimite = subj;
  }
  
  dispatch_e_menu (BAR_ITEM_ID(1));
    
  return TRUE;
}

bool TLista_archivi::destroy()
{            
  delete _ditte; 
  delete _caus;
  delete _rcaus;
  delete _clifo; 
  delete _pcon;    
  delete _mov;
  delete _rmov;
  delete _rmoviva;
  delete _part;
  delete _scad;
  delete _pagsca;
  
  return TApplication::destroy();  
}                                

bool TLista_archivi::tabella_ditta()
{
  TTable ind ("%IND");
  TString dep;
  
  dep.format("%05ld", _ditta);
  
  ind.zero();
  ind.put("CODTAB", dep);
  if (ind.read() == NOERR) 
  {
    _files = "";
    if (ind.get_bool("B0"))
      _files << "W";
    
    if (ind.get_bool("B1"))
      _files << "A";
    
    if (ind.get_bool("B2"))
      _files << "P";        
    
    if (ind.get_bool("B3"))
      _files << "Z";
    
    if (ind.get_bool("B4"))
      _files << "U";
    
    if (ind.get_bool("B5"))
      _files << "B";
    
    // Compila l'array contenente le causali di corrispondenza per l'invio extracontabile      
    dep = "";
    TString s5(ind.get("S5"));
    _tab_cau.destroy();
    _tab_cau.add(s5.mid(0,3)); 
    _tab_cau.add(s5.mid(3,3));
    _tab_cau.add(s5.mid(6,3));
    _tab_cau.add(dep);   // corrisponde al tipo dell'abbuono.
    _tab_cau.add(s5.mid(9,3));
    _tab_cau.add(s5.mid(12,3));
    _cpg_nc = ind.get("S7"); // condizione pagamento (rimessa diretta) per le note di credito
    _cpg_nc.trim();
    return TRUE;
  }  
  
  return FALSE;  
}
                   
bool TLista_archivi::set()
{
//****************************************
//****** Apro la finestra di stampa ******
//****************************************

  //printer().set_printtype(winprinter);
  
  if (!(printer().isopen ()))
    if (!printer().open ())
      return FALSE;

//****************************************

  _ditta = get_firm();
  
  _ditte->zero();
  _ditte->put(NDT_CODDITTA, _ditta);   
  if (_ditte->read() == NOERR) 
    _ragsoc = _ditte->get(NDT_RAGSOC);
  
  if (!tabella_ditta()) return FALSE;
  
  if (_files.find('W') != -1)
    controlla_causali('W');

  if (_files.find('A') != -1)
    controlla_clifo('A');

  if (_files.find('P') != -1)
    controlla_pcon('P');

  if (_files.find('Z') != -1)
    controlla_PNIVA('Z');

  if (_files.find('B') != -1)
    controlla_fatture('B');
    
  printer().close();
  
  if (_risposta == 1)                  
    message_box("Rilevati errori gravi durante il controllo archivi: \n trasferimento interrotto");
                    
  TString appname = "cg6 -8 S";
  TString body    = "";
  
  body << _risposta;
  
  TMessage msg (appname,"",(const char*)body);
  TMailbox mb;
  mb.send(msg);
  
  return FALSE;
}
     
bool TLista_archivi::controlla_valore(const TString& val,int lim, int len)
{
  if (real::is_natural(val))
  {
    if (atoi(val) > lim)
      return FALSE;
  }
  else
    if (val.len() > len)
      return FALSE;
  
  return TRUE;
}
     
void TLista_archivi::controlla_causali(char file)
{   
  TPrintrow row;
  TString   str;
  bool      errore  = FALSE;
  bool      salto   = FALSE;
  bool      intesta = TRUE;
  TString_array stampa(50); 
  int       num;
  
  _caus->setkey(1);
  _caus->zero();
  
  for (_caus->first(); !_caus->eof(); _caus->next())
  {
    const char* codcau = _caus->get(CAU_CODCAUS);
    TString codcaus = codcau;
    if (codcau && *codcau && !real::is_natural(codcau) )
    {
      errore = TRUE;
      stampa.add("  - Rilevato codice causale alfanumerico"); 
    }
    
    TString reg = _caus->get(CAU_REG);
    if (!controlla_valore(reg,9,1))
    {
      errore = TRUE;
      stampa.add("  - Rilevato registro IVA con lunghezza superiore a 1");
    }
    
    const char* codcausim = _caus->get(CAU_CODCAUSIM);
    if (codcausim && *codcausim && !real::is_natural(codcausim) )
    {
      errore = TRUE;
      stampa.add("  - Rilevato codice causale per incasso immediato alfanumerico");
    }

    num = 0;
    
    _rcaus->setkey(1);
    _rcaus->zero();
    _rcaus->put(RCA_CODCAUS, codcaus);
    
    TRectype rcau (_rcaus->curr());
    
    for (_rcaus->read(); !_rcaus->eof(); _rcaus->next())
    {                
      bool tit_riga = FALSE;
      
      if (_rcaus->curr() > rcau) break;
      
      num++;
     
      if (num > 15)
      {
        errore = TRUE;
        stampa.add("  - Rilevata causale con numero righe superiore a 15");
      }
 
      int     g   = _rcaus->get_int (RCA_GRUPPO);
      int     c   = _rcaus->get_int (RCA_CONTO);  
      int     nr  = _rcaus->get_int (RCA_NRIGA);
        
      if (g > 99)
      {   
        errore = TRUE;
        TString app;
        app.format("%9s%03d", (const char*)"@b  - Riga ", nr);
        app << " " << "@rRilevato un gruppo composto da 3 cifre";
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (c > 99)
      {   
        errore = TRUE;
        TString app;     
        if (tit_riga)
          app << "             Rilevato un conto  composto da 3 cifre";
        else
        {  
          app.format("%9s%03d", (const char*)"@b  - Riga ", nr);
          app << " " << "@rRilevato un conto  composto da 3 cifre";
        }
        stampa.add(app);
      }
    }
      
    if (errore)
    {                      
      if (intesta)
      {
        setta_intestazione(file);
        intesta = FALSE;
      }
        
      TString app;
      app << "@b- Codice causale  " << codcaus;
      row.put(app,0);
      printer().print(row);
      row.reset();
      
      long items = stampa.items();
    
      for (int i = 0; i < items; i++)
      {
        str = stampa.row(i);
        row.put(str,0);
        printer().print(row);
        row.reset();
      }

      row.put(" ",0);
      printer().print(row);
      row.reset();
      
      errore = FALSE;      
      salto  = TRUE;           
      _risposta = 1;
    }
    stampa.destroy();
  } 
  if (salto)
    printer().formfeed();
}

void TLista_archivi::controlla_clifo(char file)
{
  TPrintrow row;
  TString   str;
  bool      errore  = FALSE;
  bool      salto   = FALSE;
  bool      intesta = TRUE;
  TString_array stampa(10);
  
  _clifo->setkey(1);
  _clifo->zero();
  
  for (_clifo->first(); !_clifo->eof(); _clifo->next())
  { 
    str = "";
       
    char tipo   = _clifo->get_char(CLI_TIPOCF);
    long codice = _clifo->get_long(CLI_CODCF);
    
    TString paiv = _clifo->get(CLI_PAIV);
    if (paiv.len() > 11)   
    {
      errore = TRUE;
      stampa.add("  - Rilevata partita IVA con piu' di 11 caratteri");
    }
    
/*    TString ptel = _clifo->get(CLI_PTEL);
    TString tel  = _clifo->get(CLI_TEL);
    TString appoggio;
    appoggio << ptel << " " << tel;   
    if (appoggio.len() > 12)
    {
      errore = TRUE;
      stampa.add("  - Rilevato numero telefonico troppo lungo");
    }
*/    
    int gruppo = _clifo->get_int(CLI_GRUPPORIC);
    int conto  = _clifo->get_int(CLI_CONTORIC);
    
    if (gruppo > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato gruppo costo/ricavo composto da 3 cifre");
    }
    
    if (conto > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato conto costo/ricavo composto da 3 cifre");
    }
    
    TString codpag = _clifo->get(CLI_CODPAG);
    if (!controlla_valore(codpag,99,2))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice di pagamento composto da 3 o piu' cifre");
    }
    
    if (errore)
    {          
      TString app;
      
      if (intesta)
      {
        setta_intestazione(file);
        intesta = FALSE;
      }
        
      str << "@b- Codice  ";
      app.format("%c %06ld", tipo, codice);
      str << app;
        
      row.put(str,0);
      printer().print(row);
      row.reset();
      str = "";
      
      long items = stampa.items();
    
      for (int i = 0; i < items; i++)
      {
        str = stampa.row(i);
        row.put(str,0);
        printer().print(row);
        row.reset();
      }

      row.put(" ",0);
      printer().print(row);
      row.reset();
      
      errore = FALSE;
      salto  = TRUE;
      _risposta = 1;
    }
    stampa.destroy();
  } 
  if (salto)                   
    printer().formfeed();
}

void TLista_archivi::controlla_pcon(char file)
{
  TPrintrow row;
  TString   str, ivacomp;
  bool      errore  = FALSE;
  bool      salto   = FALSE;
  bool      intesta = TRUE;
  TString_array stampa(10);
  
  
  _pcon->setkey(1);
  _pcon->zero();
  
  for (_pcon->first(); !_pcon->eof(); _pcon->next())
  { 
    str = "";
       
    int  gruppo = _pcon->get_int (PCN_GRUPPO);
    int  conto  = _pcon->get_int (PCN_CONTO);
    long sottoc = _pcon->get_long(PCN_SOTTOCONTO);
    ivacomp     = _pcon->get(PCN_IVACOMP);
    
    if (gruppo > 99)   
    {
      errore = TRUE;
      stampa.add("  - Rilevato un gruppo composto da 3 cifre");
    }                                                    
    
    if (conto > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un conto  composto da 3 cifre");
    }
    
    if (!controlla_valore(ivacomp,99,2))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice IVA di compensazione non valido");
    }
    
    if (errore)
    {          
      TString app;
                           
      if (intesta)
      {
        setta_intestazione(file);
        intesta = FALSE;
      }
                           
      str << "@b- Conto  ";
      app.format("%3d %3d %6ld", gruppo, conto, sottoc);
      str << app;
        
      row.put(str,0);
      printer().print(row);
      row.reset();
      str = "";
      
      long items = stampa.items();
    
      for (int i = 0; i < items; i++)
      {
        str = stampa.row(i);
        row.put(str,0);
        printer().print(row);
        row.reset();
      }

      row.put(" ",0);
      printer().print(row);
      row.reset();
      
      errore = FALSE;
      salto  = TRUE;
      _risposta = 1;
    }              
    stampa.destroy();
  }           
  if (salto)
    printer().formfeed();
}
                                   
void TLista_archivi::controlla_PNIVA(char file)
{
  TPrintrow row;
  TString   str;
  bool      errore   = FALSE;
  bool      salto    = FALSE;
  bool      intesta  = TRUE; 
  bool      tit_riga = FALSE;
  TString_array stampa(50); 
  int       num;
  long      nreg_p = 0;
  real      importo;
  
  _mov->setkey(1);
  _mov->zero();
  
  for (_mov->first(); !_mov->eof(); _mov->next())
  {
    bool soliva = TRUE;
    
    long  nreg    = _mov->get_long(MOV_NUMREG);
    TDate datareg = _mov->get_date(MOV_DATAREG);
    bool  inviato = _mov->get_bool(MOV_INVIATO);
    bool  inviva  = _mov->get_bool(MOV_INVIVA);

    if (inviato && inviva) continue; //Se il movimento e' gia' stato inviato non lo posso piu' inviare
                                     //cosi' come le sue righe di primanota e iva
                                     
    if (datareg > _datalimite) continue;
    
    if (nreg > 999999)
    { 
      errore = TRUE;
      stampa.add("  - Rilevato un numero di registrazione composto da 7");
    }

    TString reg = _mov->get(MOV_REG);
    if (!controlla_valore(reg,9,1))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice registro IVA composto da 2 o piu' cifre");
    }
    
    const char* codcaus = _mov->get(MOV_CODCAUS);
    if (codcaus && *codcaus)
      if (!real::is_natural(codcaus))
      {
        errore = TRUE;
        stampa.add("  - Rilevato un codice causale alfanumerico");
      }
    
    TString codpag = _mov->get(MOV_CODPAG);
    if (!controlla_valore(codpag,99,2))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice di pagamento composto da 3 o piu' cifre");
    }    
    
    importo = _mov->get_real(MOV_TOTDOC);
    if (importo >= MAX_SYSTEM)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un totale documento eccedente le 11 cifre");
    }

    importo = _mov->get_real(MOV_CORRLIRE);
    if (importo >= MAX_SYSTEM)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un corrispettivo in lire eccedente le 11 cifre");
    }
    
    num = 0;

    bool prima = TRUE;
    
    _rmov->setkey(1);
    _rmov->zero();
    _rmov->put(RMV_NUMREG, nreg);
    
    TRectype rmov (_rmov->curr());
    
    for (_rmov->read(); !_rmov->eof(); _rmov->next())
    {
      
      if (_rmov->curr() > rmov) break;
      
      tit_riga = FALSE;      
        
      soliva = FALSE;
      
      num++;
     
      if (num > 99)
      {
        errore = TRUE;
        stampa.add("  - Rilevato movimento di Primanota con numero righe superiore a 99");
      }

      int     nr  = _rmov->get_int (RMV_NUMRIG);
      int     g   = _rmov->get_int (RMV_GRUPPO);
      int     c   = _rmov->get_int (RMV_CONTO);  
      int     gc  = _rmov->get_int (RMV_GRUPPOC);
      int     cc  = _rmov->get_int (RMV_CONTOC);

      if (nr > 99)
      { 
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        errore = TRUE;
        TString app;                  
        app.format("%5s%03d", (const char*)"@b  - ", nr);
        app << " " << "@rRilevato numero di riga con piu' di due cifre";
        stampa.add(app);
        tit_riga = TRUE;
      }
      
      if (g > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un gruppo di partita composto da 3 cifre";
        else
        { 
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un gruppo di partita composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (c > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un conto  di partita composto da 3 cifre";
        else
        {  
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un conto  di partita composto da 3 cifre";
        }
        stampa.add(app);
      }
      
      if (gc > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un gruppo di contropartita composto da 3 cifre";
        else
        {            
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un gruppo di contropartita composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (cc > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un conto  di contropartita composto da 3 cifre";
        else
        {                  
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un conto  di contropartita composto da 3 cifre";
        }
        stampa.add(app);
      }      

      importo = _rmov->get_real(RMV_IMPORTO);
      if (importo >= MAX_SYSTEM)
      {
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un importo eccedente le 11 cifre";
        else
        {                  
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un importo eccedente le 11 cifre";
        }
        stampa.add(app);
      }
    }

    if (soliva)
    {  
      TLocalisamfile rcaus (LF_RCAUSALI);
      int g = 0;
      int c = 0;
      bool tit_riga = FALSE;  
      bool prima    = TRUE;           
      int nr = 1;
      
      rcaus.setkey(1);
      rcaus.zero();
      rcaus.put(RCA_CODCAUS, codcaus);
      rcaus.read();
      TString codcau = rcaus.get(RCA_CODCAUS);
      if (codcau == codcaus)
      {
        g = rcaus.get_int (RCA_GRUPPO);
        c = rcaus.get_int (RCA_CONTO);
      }            

  
      if (g > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un gruppo di partita composto da 3 cifre";
        else
        {                                           
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un gruppo di partita composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (c > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe primanota"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un conto  di partita composto da 3 cifre";
        else
        {           
          app.format("5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un conto  di partita composto da 3 cifre";
        }
        stampa.add(app);
      }    
    }
          
    num = 0;
    
    if (_files.find('U') != -1)
    { 
    nreg_p = 0;
    _rmoviva->setkey(1);
    _rmoviva->zero();
    _rmoviva->put(RMI_NUMREG, nreg);
    
    TRectype rmoviva (_rmoviva->curr());
    
    prima = TRUE;
    
    for (_rmoviva->read(); !_rmoviva->eof(); _rmoviva->next())
    {                
      if (_rmoviva->curr() > rmoviva) break;

      num++;
     
      if (num > 9)
      {
        errore = TRUE;
        stampa.add("  - Rilevato movimento Iva con numero righe superiore a 9");
      }

      int     nr  = _rmoviva->get_int (RMI_NUMRIG);
      int     g   = _rmoviva->get_int (RMI_GRUPPO);
      int     c   = _rmoviva->get_int (RMI_CONTO);

      if (nr > 99)
      {
        errore = TRUE;
        TString app;   
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        app.format("%5s%03d", (const char*)"@b  - ", nr);
        app << " " << "@rRilevato numero di riga con piu' di due cifre";
        stampa.add(app);
        tit_riga = TRUE;
      }
      
      if (g > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un gruppo composto da 3 cifre";
        else
        {            
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un gruppo composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (c > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un conto  composto da 3 cifre";
        else
        {                                           
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un conto  composto da 3 cifre";
        }
        stampa.add(app);
      }
      const char* ci  = _rmoviva->get     (RMI_CODIVA);
      if ( ci && *ci && !real::is_natural(ci) )
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un codice IVA alfanumerico";
        else
        {           
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un codice IVA alfanumerico";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }                          
      
      if (!controlla_valore(ci,99,2))
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato un codice IVA composto da 3 o piu' cifre";
        else
        {                                           
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato un codice IVA composto da 3 o piu' cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }

      importo = _rmoviva->get_real(RMI_IMPONIBILE);
      if (importo >= MAX_SYSTEM)
      {
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevato imponibile eccedente le 11 cifre";
        else
        {                  
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato imponibile eccedente le 11 cifre";
        }
        stampa.add(app);
      }

      importo = _rmoviva->get_real(RMI_IMPOSTA);
      if (importo >= MAX_SYSTEM)
      {
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Righe iva"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r        Rilevata imposta eccedente le 11 cifre";
        else
        {                  
          app.format("%5s%03d", (const char*)"@b  - ", nr);
          app << " " << "@rRilevato imposta eccedente le 11 cifre";
        }
        stampa.add(app);
      }
    }
    }
    
    if (errore)
    {          
      TString app; 

      if (intesta)
      {
        setta_intestazione(file);
        intesta = FALSE;
      }
      
      app << "@b- Registrazione numero  " << nreg;
      row.put(app,0);
      printer().print(row);
      row.reset();
      
      long items = stampa.items();
    
      for (int i = 0; i < items; i++)
      {
        str = stampa.row(i);
        row.put(str,0);
        printer().print(row);
        row.reset();
      }

      row.put(" ",0);
      printer().print(row);
      row.reset();
      
      errore = FALSE;
      salto  = TRUE;
      _risposta = 1;
    }
    stampa.destroy();
  } 
  if (salto)
    printer().formfeed(); 
}                                

void TLista_archivi::ragsocCF(char tipo, long codcf, TString& ragsoc)
{
  TLocalisamfile clifo (LF_CLIFO);
  
  clifo.setkey(1); 
  clifo.zero();
  clifo.put(CLI_TIPOCF, tipo);
  clifo.put(CLI_CODCF,  codcf);
  if (clifo.read() == NOERR)
    ragsoc = clifo.get(CLI_RAGSOC);
  else
    ragsoc = "";
    
  if (ragsoc.not_empty())
  {
    TString cognome (ragsoc.mid(0,30));
    cognome.trim();
    TString nome (ragsoc.mid(30,20));
    nome.trim();
    ragsoc = "";
    ragsoc << cognome << " " << nome;
  }
}

void TLista_archivi::controlla_fatture(char file)
{
  TPrintrow row;
  TString   str,rec,filer;
  bool      errore   = FALSE;
  bool      salto    = FALSE;
  bool      intesta  = TRUE; 
  bool      tit_riga = FALSE;
  TString_array stampa(50);  
  real importo;
  char      tipo_p   = ' ';
  long      sottoc_p = 0;
  
  _part->setkey(1);
  _part->zero();
  
  for (_part->first(); !_part->eof(); _part->next())
  {  
    bool    inviata = _part->get_bool(PART_INVIATA);

    if (inviata) continue; // Se il movimento e' gia' stato inviato non lo posso piu' inviare
                           // cosi' come le sue rate e i suoi pagamenti.
                           
    TDate datareg (_part->get_date(PART_DATAREG));
                                     
    if (datareg > _datalimite) continue;

    char    tipo    = _part->get_char(PART_TIPOCF);
    int     gruppo  = _part->get_int (PART_GRUPPO);
    int     conto   = _part->get_int (PART_CONTO);
    long    sottoc  = _part->get_long(PART_SOTTOCONTO);
    int     anno    = _part->get_int (PART_ANNO);
    TString numpart = _part->get     (PART_NUMPART);
    TString codcaus = _part->get     (PART_CODCAUS);
    int     nriga   = _part->get_int (PART_NRIGA); 
    long    nreg    = _part->get_long(PART_NREG);
    int     numrig  = _part->get_int (PART_NUMRIG);
    int     tipomov = _part->get_int (PART_TIPOMOV);

    if (nriga == 9999) continue;
    
    if (nreg > 999999)
    { 
      errore = TRUE;
      stampa.add("  - Rilevato un numero di registrazione corrispondente a primanota composto da 7 cifre");
    }
    else
      if (nreg == 0 && codcaus.empty()) // Allora fa parte del saldaconto extracontabile: si controlli la tabella di corrispondenza relativa al tipo mov.
      {
        TString& s = (TString&) _tab_cau[tipomov-1];
        s.trim();
        if (s.empty())
        {
          errore = TRUE;
          stampa.add("  - Rilevato un movimento extracontabile senza causale. Compilare tabella di corrispondenza");
        }
      }
    if (tipomov == 2 && _cpg_nc.empty())
    {
      errore = TRUE;
      stampa.add("  - Rilevata una nota di credito senza codice pagamento. Compilare tabella di corrispondenza");
    }
    if (numrig > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un numero di riga corrispondente a primanota composto da 3 cifre");
    } 
    if (nriga > 999)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un numero di riga partita composto da 4 cifre");
    } 
    if (gruppo > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un gruppo composto da 3 cifre");
    }                                                          
    if (conto > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un conto composto da 3 cifre");
    }                                                         
    TString reg = _part->get(PART_REG);
    if (!controlla_valore(reg,9,1))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice registro IVA composto da 2 o piu' cifre");
    }
    if (codcaus.not_empty() && !real::is_natural(codcaus))
    {
      errore = TRUE;
      stampa.add("  - Rilevato un codice causale alfanumerico");
    }
    int gruppocl = _part->get_int(PART_GRUPPOCL);
    if (gruppocl > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un gruppo cliente composto da 3 cifre");
    }   
    int contocl = _part->get_int(PART_CONTOCL); 
    if (contocl > 99)
    {
      errore = TRUE;
      stampa.add("  - Rilevato un conto cliente composto da 3 cifre");
    }
    
    importo = _part->get_real(PART_IMPTOTDOC);
    if (importo >= MAX_SYSTEM)
    {
      errore = TRUE;
      stampa.add("  - Rilevato totale documento eccedente le 11 cifre");
    }

    importo = _part->get_real(PART_IMPORTO);
    if (importo >= MAX_SYSTEM)
    {
      errore = TRUE;
      stampa.add("  - Rilevato importo eccedente le 11 cifre");
    }
    
    bool prima = TRUE;
     
    _scad->setkey(1); 
    _scad->zero();
    _scad->put(SCAD_TIPOCF,       tipo);
    if (gruppo != 0)
      _scad->put(SCAD_GRUPPO,     gruppo);
    if (conto != 0)
      _scad->put(SCAD_CONTO,      conto);
    if (sottoc != 0)
      _scad->put(SCAD_SOTTOCONTO, sottoc);
    _scad->put(SCAD_ANNO,         anno);
    _scad->put(SCAD_NUMPART,      numpart);
    _scad->put(SCAD_NRIGA,        nriga);  
    
    TRectype scadenza (_scad->curr());
    
    for (_scad->read(_isgteq); !_scad->eof(); _scad->next())
    {                                              
      rec   = scadenza.get(SCAD_NUMPART);
      filer = _scad->get(SCAD_NUMPART);
          
      if (_scad->curr() != scadenza || filer != rec) break;

      tit_riga = FALSE;      
      
      int     gruppo   = _scad->get_int (SCAD_GRUPPO);
      int     conto    = _scad->get_int (SCAD_CONTO);  
      int     nriga    = _scad->get_int (SCAD_NRIGA);
      int     nrata    = _scad->get_int (SCAD_NRATA);

      if (gruppo > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un gruppo composto da 3 cifre";
        else
        { 
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un gruppo composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (conto > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un conto  composto da 3 cifre";
        else
        {  
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un conto  composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }

      if (nriga > 999)
      { 
        errore = TRUE;
        TString app;                  
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }   
        if (tit_riga)
          app << "@r              Rilevato un conto  composto da 3 cifre";
        else
        {        
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato numero di riga con piu' di tre cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
      
      if (nrata > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un numero di rata composto da 3 o piu' cifre";
        else
        {            
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un numero di rata composto da 3 o piu' cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      } 
      const char* codpag = _scad->get(SCAD_CODPAG);
      if (!controlla_valore(codpag,99,2))
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un codice pagamento composto da 3 o piu' cifre";
        else
        {            
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un codice pagamento composto da 3 o piu' cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
      const char* codag = _scad->get(SCAD_CODAG);
      if ( codag && *codag && !real::is_natural(codag) ) 
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un codice agente alfanumerico";
        else
        {            
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un codice agente alfanumerico";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }

      importo = _scad->get_real(SCAD_IMPORTO);
      if (importo >= MAX_SYSTEM) 
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un importo eccedente le 11 cifre";
        else
        {            
          app.format("%5s%04d", (const char*)"@b  - rata ", nrata);
          app << " " << "@rRilevato un importo eccedente le 11 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
    }    

    _pagsca->zero();
    _pagsca->put(PAGSCA_TIPOC,        tipo);  
    if (gruppo != 0)
      _pagsca->put(PAGSCA_GRUPPO,     gruppo);
    if (conto != 0)
      _pagsca->put(PAGSCA_CONTO,      conto);
    if (sottoc != 0)
      _pagsca->put(PAGSCA_SOTTOCONTO, sottoc);
    _pagsca->put(PAGSCA_ANNO,         anno);
    _pagsca->put(PAGSCA_NUMPART,      numpart);
    
    TRectype pagsca (_pagsca->curr());
    
    for (_pagsca->read(_isgteq); !_pagsca->eof(); _pagsca->next())
    {     
      tit_riga = FALSE;
                                              
      int nrigp = _pagsca->get_int(PAGSCA_NRIGP);         
                      
      rec   = pagsca.get(PAGSCA_NUMPART);
      filer = _pagsca->get(PAGSCA_NUMPART);
                      
      if (_pagsca->curr() != pagsca || filer != rec) break;
        
      if (nriga != nrigp) continue;

      int     gruppo   = _pagsca->get_int (PAGSCA_GRUPPO);
      int     conto    = _pagsca->get_int (PAGSCA_CONTO);
      int     nriga    = _pagsca->get_int (PAGSCA_NRIGA);
      int     nrata    = _pagsca->get_int (PAGSCA_NRATA);
      
      if (gruppo > 99)
      {   
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r                        Rilevato un gruppo composto da 3 cifre";
        else
        {            
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato un gruppo composto da 3 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
        
      if (conto > 99)
      {   
        errore = TRUE;
        TString app;     
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r                        Rilevato un conto  composto da 3 cifre";
        else
        {                                           
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato un conto  composto da 3 cifre";
        }
        stampa.add(app);
      }
      if (nriga != 9999 && nriga > 999)
      {
        errore = TRUE;
        TString app;   
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }   
        if (tit_riga)
          app << "@r                        Rilevato un conto  composto da 3 cifre";
        else
        {          
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato numero di riga con tre o piu' cifre";
        }  
        stampa.add(app);
        tit_riga = TRUE;
      }
      if (nrata != 9999 && nrata > 99)
      {
        errore = TRUE;
        TString app;   
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        } 
        if (tit_riga)
          app << "@r                        Rilevato un conto  composto da 3 cifre";
        else
        {        
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato numero di rata con piu' di due cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
      const char* codag = _scad->get(SCAD_CODAG);
      if ( codag && *codag && !real::is_natural(codag) ) 
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r                        Rilevato un codice agente alfanumerico";
        else
        {            
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato un codice agente alfanumerico";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
      importo = _pagsca->get_real(PAGSCA_IMPORTO);
      if (importo >= MAX_SYSTEM) 
      {
        errore = TRUE;
        TString app;    
        if (prima)
        {
          stampa.add(" ");             
          stampa.add("Pagamenti scadenze:"); 
          stampa.add(" ");
          prima = FALSE;
        }
        if (tit_riga)
          app << "@r              Rilevato un importo eccedente le 11 cifre";
        else
        {            
          app.format("%5srata %04d riga %04d", (const char*)"@b  - ", nrata, nrigp);
          app << " " << "@rRilevato un importo eccedente le 11 cifre";
        }
        stampa.add(app);
        tit_riga = TRUE;
      }
    }
    
    if (errore)
    {          
      TString app; 

      if (intesta)
      {
        setta_intestazione(file);
        intesta = FALSE;
      }
      if (tipo != tipo_p || sottoc != sottoc_p)
      {
        TString ragsoc;
        ragsocCF(tipo,sottoc,ragsoc);    
        if (tipo == 'C')
          app << "@b- Cliente : " << sottoc;
        else
          app << "@b- Fornitore : " << sottoc;
        app << " " << ragsoc;  
        row.put(app,0);
        printer().print(row);
        row.reset(); 
      }
      app = "";
      app.format("Partita %-7s riga %4d", (const char*) numpart, nriga);
      row.put(app,0);
      printer().print(row);
      row.reset();
      
      long items = stampa.items();
    
      for (int i = 0; i < items; i++)
      {
        str = stampa.row(i);
        row.put(str,0);
        printer().print(row);
        row.reset();
      }

      row.put(" ",0);
      printer().print(row);
      row.reset();
      
      errore    = FALSE;
      salto     = TRUE;
      _risposta = 1;  
      tipo_p    = tipo;
      sottoc_p  = sottoc;
    }
    stampa.destroy();
  } 
  if (salto)
    printer().formfeed(); 
}
                                   
void TLista_archivi::setta_intestazione(char sigla)
{ 
  TPrintrow row;
  TString sep(132);
  TString titolo;

  switch (sigla)
  {
    case 'W':
    titolo = "Tabella Causali";
    break;
    
    case 'A':
    titolo = "Anagrafica Clienti/Fornitori";
    break;
    
    case 'P':
    titolo = "Anagrafica Piano dei Conti";
    break;
    
    case 'Z':
    titolo = "Movimenti";
    break;
    
    case 'B':
    titolo = "Movimenti Saldaconto";
    break;
    
    default:
    break;
  }       
  
  printer().resetheader();
  
  sep << "Ditta  " << _ditta;
  sep << " " << _ragsoc;
  sep.left_just(132);
  
  row.put(sep,0);
  printer().setheaderline(1,row);
  row.reset();
  
  sep = "";
  sep << "Data @< Pag. @#";
  sep.right_just(127);
  sep.overwrite ("LISTA DI CONTROLLO ARCHIVI IN TRASFERIMENTO");
  row.put(sep,0);
  printer().setheaderline(2,row);
  row.reset();

  sep = "";
  sep.fill('-');
  row.put(sep,0);      
  printer().setheaderline(3,row);
  row.reset();
  
  row.put(titolo,0);   
  printer().setheaderline(4,row);
  row.reset();
  
  sep = "";
  sep.fill('-');
  row.put(sep,0);      
  printer().setheaderline(5,row);
  row.reset();
  
  row.put(" ",0);      
  printer().setheaderline(6,row);
  row.reset();
}
                   
bool TLista_archivi::menu(MENU_TAG m)
{
  if (m == BAR_ITEM_ID(1))
    return set(); 
  return FALSE;
}