#include <applicat.h>
#include <automask.h>
#include <execp.h>
#include <form.h>
#include <printer.h>
#include <recarray.h>
#include <tabutil.h>
#include <utility.h>

#include "ce3.h"
#include "ammce.h"
#include "ammmv.h"
#include "cespi.h"
#include "movam.h"
#include "movce.h"
#include "salce.h"
#include "ce2101.h"
#include "ce3100a.h"
#include "ce3100.h"

#include "celib.h"
#include "..\cg\cglib01.h"
                       
class TForm_registroce : public TForm_cespiti
{          
  TRelation*  _newrelation;
  TCursor*    _newcursor;
  
public:
  virtual bool validate(TForm_item& fld, TToken_string& val);
  TForm_registroce(const char *name);
  virtual ~TForm_registroce();
  void set_testata() { set_header(1, TRUE); set_background(1, TRUE); }
  void set_piede() { set_footer(0,FALSE); set_footer(0, TRUE);}
  void set_pagina() { set_footer(0, TRUE);}
  TPrint_section& get_testata_cespite() { return section('H', odd_page); }
  TPrint_section& get_testata_categoria() { return section('H', even_page); }
  TPrint_section& get_piede_categoria() { return section('F', even_page); }
  TPrint_section& get_piede_registro() { return section('F', odd_page); }
  TPrint_section& get_saldiiniziali() { return section('H', last_page); }
  TPrint_section& get_movimenti() { return section('B', first_page); }
  TPrint_section& get_valorifinali() { return section('B', even_page); }
  TPrint_section& get_totali() { return section('B', odd_page); }
  virtual TCursor* cursor() const {return _newcursor; }
  virtual TRelation* relation() const {return _newrelation; }
};

TForm_registroce::TForm_registroce(const char *name):
  TForm_cespiti(name)
{
  _newrelation = new TRelation(LF_CESPI);
  _newrelation->add("%CAC","CODTAB[1,2]==CODCGRA|CODTAB[3,6]==CODSPA|CODTAB[7,8]==CODCAT",1,0,101);
  _newrelation->add(LF_SALCE,"IDCESPITE==IDCESPITE",1,0,102);
  _newrelation->add("ESC","CODTAB==CODES",1,LF_SALCE,103);
  _newrelation->add(LF_AMMCE,"IDCESPITE==IDCESPITE|CODES==CODES|TPSALDO==TPSALDO",1,LF_SALCE,104);
  _newrelation->add(LF_MOVCE,"IDCESPITE==IDCESPITE",2,0,105);
  _newrelation->add(LF_MOVAM,"IDCESPITE==IDCESPITE|IDMOV==IDMOV",1,LF_MOVCE,106);
  _newrelation->add(LF_AMMMV,"IDCESPITE==IDCESPITE|IDMOV==IDMOV",1,LF_MOVCE,107);
  _newrelation->add("%TMC","CODTAB==CODMOV",1,LF_MOVCE,108);
  _newcursor = new TCursor(_newrelation,"",2);
}

TForm_registroce::~TForm_registroce()
{
  delete _newrelation;
  delete _newcursor;
}

bool TForm_registroce::validate(TForm_item& fld, TToken_string& val) 
{
  return TForm_cespiti::validate(fld, val);
}
                       
                       
/////////////////////////////////////////////////////
// Applicazione
/////////////////////////////////////////////////////

class TRegistro_cespiti : public TSkeleton_application
{         
  TMask*            _mask;
  TForm_registroce* _form;
  TDitta_cespiti*   _dittace;
  int               _tipoamm;
  bool              _bollato;
  TEsercizi_contabili _esc;
  TString _idspese5, _idspese25;

protected:
  virtual bool create();
  virtual bool destroy();
  virtual void main_loop();
  static bool attivita_handler(TMask_field& f, KEY k);

public:           
  void header_registro();
  void footer_registro();
  void footer_categoria();
  void header_categoria(const int categoria);
  void print_cespite(); 
  void header_cespite();       
  void print_saldiiniziali();
  void print_movimenti();
  void print_valorifinali();
  void aggiorna_bollato();
  void crea_cespite_man();
  void distruggi_cespite_man();
  void costruisci_cespite(int cat, const real& spese);
  void stampa_movimento(TRectype& removce, TRectype& removam, TRectype& recammmv);
  void stampa_sezione(TPrint_section& section);
  TRegistro_cespiti() {}
  virtual ~TRegistro_cespiti() {};
};

bool TRegistro_cespiti::create()
{                       
  open_files(LF_CESPI,0);
  _mask = new TMask("ce3100a");
  _mask->set_handler(F_ESERCIZIO,attivita_handler);
  _mask->set_handler(F_SPECIE,attivita_handler);
  _dittace = new TDitta_cespiti();
  _dittace->init_mask(*_mask);
  _form = new TForm_registroce("ce3100a");
  return TSkeleton_application::create();
}

bool TRegistro_cespiti::destroy()
{
  delete _form;
  delete _dittace;
  delete _mask;
  return TSkeleton_application::destroy();
}

bool TRegistro_cespiti::attivita_handler(TMask_field& f, KEY k)
{             
  TMask& m = f.mask();
  if (f.to_check(k) || (k == K_TAB && !m.is_running()))
  {                                     
    const int esercizio = m.get_int(F_ESERCIZIO);
    const int gruppo = m.get_int(F_GRUPPO);
    const TString16 specie = m.get(F_SPECIE);
    TString80 key; key.format("%4d%02d%s",esercizio, gruppo, (const char*) specie);
    const TRectype& ccb = cache().get("CCB", key);  
    const TDate dataultcalc = ccb.get_date("D0");
    m.set(F_DATAULTCALC, dataultcalc);

    bool calc_on = TRUE, reg_on = TRUE;
    if (dataultcalc.empty())
    {
      calc_on = FALSE;
      m.set(F_CALCOLO,"X"); 
    }
    if (ccb.get_bool("B2"))
    {              
      calc_on = reg_on = FALSE;
      m.reset(F_CALCOLO); 
      xvt_statbar_set("L'esercizio � stato chiuso");
    }  
    else
      if (ccb.get_bool("B1"))
      {              
        calc_on = reg_on = FALSE;
        m.set(F_CALCOLO,"X"); 
        xvt_statbar_set("Il bollato dell'esercizio � stato stampato");
      }
      else
        xvt_statbar_set("");
      
    m.enable(F_TIPOREGISTRO, reg_on);
    m.enable(F_CALCOLO, calc_on);
    if (!reg_on)
      m.set(F_TIPOREGISTRO,"1");

    if (m.get_int(F_TIPOREGISTRO) == 2 && ccb.get_bool("B2"))
    { 
      if (k == K_ENTER)
        return f.error_box("Attenzione! Esercizio chiuso. \nNon � possibile stampare il registro bollato.");
    }
  }
  return TRUE;
}

void TRegistro_cespiti::main_loop()
{ 
  while (_mask->run() == K_ENTER) 
  { 
    // Esecuzione calcolo globale
    if (_mask->get_bool(F_CALCOLO)) 
    {                                               
      const int es = _mask->get_int(F_ESERCIZIO);
      const int gr = _mask->get_int(F_GRUPPO);
      const TString& sp = _mask->get(F_SPECIE);
      ditta_cespiti().set_attivita(es, gr, sp); // Fissa attivit� per calcolo
      TExternal_app cal("ce2 -0 A");            
      cal.run();                                // Esegue programma di calcolo in batch
    } 

    // flag bollato per aggiornamenti
    _bollato = (_mask->get_int(F_TIPOREGISTRO)==2);
    if (_bollato)        
      crea_cespite_man();     //crea il cespite della manutenzione per cespiti materiali nell'esercizio selezionato

    TRectype darec(LF_CESPI),arec(LF_CESPI);
    TString filtro;
    // filtro sulla attivit� e categorie
    const int esercizio = _mask->get_int(F_ESERCIZIO);
    const TString4 catini = _mask->get(F_CATINI);
    const TString4 catfin = _mask->get(F_CATFIN);
    darec.put(CESPI_CODCGRA, _mask->get(F_GRUPPO));
    darec.put(CESPI_CODSPA, _mask->get(F_SPECIE));
    darec.put(CESPI_CODCAT, catini);
    arec.put(CESPI_CODCGRA, _mask->get(F_GRUPPO));
    arec.put(CESPI_CODSPA, _mask->get(F_SPECIE));
    arec.put(CESPI_CODCAT, catfin);
    // filtro sulle date di acquisizione e alienazione
    const TDate datainizio = _mask->get_date(F_DATAINIZIO);
    const TDate datafine = _mask->get_date(F_DATAFINE);
    filtro.format("(ANSI(%d->DTCOMP)<=\"",LF_CESPI) << datafine.string(ANSI) << "\")";
    filtro << format(" && ((ANSI(%d->DTALIEN)>=\"",LF_CESPI) << datainizio.string(ANSI);
    filtro << format("\") || (%d->DTALIEN==\"\"))",LF_CESPI);

    // filtro su ammortamenti fiscali o civilistici
    _tipoamm = _mask->get_int(F_AMMORTAMENTI);
    _form->find_field('H', last_page, FR_SI_RIVGF).enable(_tipoamm==1);
    _form->find_field('H', last_page, FR_SI_RIVGC).enable(_tipoamm==2);
    _form->find_field('B', first_page, FR_MV_RIVGF).enable(_tipoamm==1);
    _form->find_field('B', first_page, FR_MV_RIVGC).enable(_tipoamm==2);
    _form->find_field('B', even_page, FR_FE_RIVGF).enable(_tipoamm==1);
    _form->find_field('B', even_page, FR_FE_RIVGC).enable(_tipoamm==2);
    _form->find_field('H', last_page, FR_SI_RIVGF).hide();
    _form->find_field('H', last_page, FR_SI_RIVGC).hide();
    _form->find_field('B', first_page, FR_MV_RIVGF).hide();
    _form->find_field('B', first_page, FR_MV_RIVGC).hide();
    _form->find_field('B', even_page, FR_FE_RIVGF).hide();
    _form->find_field('B', even_page, FR_FE_RIVGC).hide();
    // scrive sull'intestazione del form il tipo di situazione da stampare
    if (_tipoamm == 1)
      _form->find_field('H', first_page, FR_SITUAZIONE).set("@bSituazione Fiscale@r");
    if (_tipoamm == 2)
      _form->find_field('H', first_page, FR_SITUAZIONE).set("@bSituazione Civilistica@r");
      
    TCursor& cur = *_form->cursor();
    cur.setregion(darec,arec);
    cur.setfilter(filtro,TRUE); 
    
    const long num = cur.items();
    cur.freeze();
    
    printer().open();
    header_registro();
    
    const TRectype& reccesp = cur.curr();
    int catatt = -1;  // Memorizza ultima categoria stampata (inizialmente nessuna)
    for (cur=0; cur.pos()<num; ++cur)
    {
      if (catatt != reccesp.get_int(CESPI_CODCAT))
      {
        if (catatt != -1)
          footer_categoria();
        catatt = reccesp.get_int(CESPI_CODCAT);
        header_categoria(catatt);
      }
      print_cespite();  
    }
    if (catatt != -1)
    {
      footer_categoria();
      _form->set_pagina();
      footer_registro();
      _form->set_pagina();
    } 
    cur.freeze(FALSE);
    printer().close(); 
    //trattazione del bollato: aggiorna i campi su files e tab solo dopo conferma, in caso contrario distrugge il cespite
    //della manutenzione creato precedentemente
    if (_bollato)      
    {     
      if (yesno_box("Bollato stampato in modo corretto ?"))
        aggiorna_bollato();
      else
        distruggi_cespite_man();  
    }
  } 
  return;
}

void TRegistro_cespiti::costruisci_cespite(int cat, const real& spese)
{               
  int es;
  TString4 gr, sp;
  ditta_cespiti().get_attivita(es, gr, sp);
  const TDate datafine = _mask->get(F_DATAFINE);

  TString& idcespite = (cat == 91) ? _idspese5 : _idspese25;

  { // Scrittura anagrafica cespite
    TLocalisamfile cespi(LF_CESPI);
    cespi.last();
    const real num = cespi.get_real(CESPI_IDCESPITE) + 1;
    idcespite = num.string("@@@@@@@@@@");

    cespi.zero();
    cespi.put(CESPI_IDCESPITE, idcespite);  //zero filled field!
    cespi.put(CESPI_CODCGRA, gr);
    cespi.put(CESPI_CODSPA, sp);
    cespi.put(CESPI_CODCAT, cat);
    cespi.put(CESPI_DTCOMP, datafine);
    cespi.put(CESPI_DTFUNZ, datafine);
    cespi.put(CESPI_AMMPROP, "X");
    cespi.put(CESPI_TUIR, "X");
    cespi.put(CESPI_USOPROM, 1);
    cespi.put(CESPI_FLGTPVEI, 1);
    cespi.put(CESPI_TPSPEMAN, 1);
    
    TString desc = "Importo spese di manutenzione sostenute - beni materiali ";
    desc << (cat == 91 ? 5 : 25) << '%'; 
    cespi.put(CESPI_DESC, desc);
    
    const TRectype& cac = ditta_cespiti().categoria(0, NULL, cat);
    cespi.put(CESPI_VINCOLO, cac.get("I2"));
    cespi.put(CESPI_ANNIRIC, cac.get("I3"));
    cespi.put(CESPI_PMINP,   cac.get("R14"));
    cespi.put(CESPI_PMAXP,   cac.get("R15"));
    
    cespi.write(); //abbiamo creato il cespite su CESPI!
  }
   
  { // Scrittura saldo iniziale 
    TLocalisamfile salce(LF_SALCE);
    salce.put(SALCE_IDCESPITE, idcespite);
    salce.put(SALCE_CODES, es);
    salce.put(SALCE_TPSALDO, 1);
    salce.put(SALCE_NUMELE, 1);
    salce.put(SALCE_CSTO, spese);
    salce.write();  //abbiamo creato il cespite su SALCE!
  }  
  
  // Calcola ammortamenti per cespite (creando anche salce 2 e i 6 ammce)
  TCespite ces(idcespite);
  for (int i = 1; i <= 3; i++)
    ces.calc_amm(i, datafine);
}

void TRegistro_cespiti::crea_cespite_man()
{ 
  _idspese5 = _idspese25 = "";
  const TRectype& ccb = ditta_cespiti().get_attivita();
  const real speseman_5 = ccb.get_real("R0") - ccb.get_real("R2");
  if (speseman_5 > ZERO)
    costruisci_cespite(91, speseman_5);
  
  const real speseman_25 = ccb.get_real("R1") - ccb.get_real("R3");
  if (speseman_25 > ZERO)
    costruisci_cespite(92, speseman_25);      
}

HIDDEN void kill_rec(TLocalisamfile& f)
{
  int err = f.read();
  if (err == NOERR)
  {
    err = f.remove();
    if (err == NOERR && f.read() == NOERR)
      err = _iskeyerr;
  }
  if (err != NOERR)
    error_box("Errore %d durante la cancellazione del cespite %s dal file: %d", 
              err, (const char*)f.get("IDCESPITE"), f.num());
}

void TRegistro_cespiti::distruggi_cespite_man()
{ 
  TLocalisamfile cespi(LF_CESPI);
  TLocalisamfile salce(LF_SALCE);
  TLocalisamfile ammce(LF_AMMCE);
  const int es = _mask->get_int(F_ESERCIZIO);
  
  for (int cat = 91; cat <= 92; cat++)
  {
    TString& idcespite = (cat == 91) ? _idspese5 : _idspese25;
    if (idcespite.not_empty())
    {  
      cespi.zero();
      cespi.put(CESPI_IDCESPITE, idcespite);
      kill_rec(cespi);
      
      for (int tpsaldo = 1; tpsaldo <= 2; tpsaldo++)
      {
        salce.zero();
        salce.put(SALCE_IDCESPITE, idcespite);
        salce.put(SALCE_CODES, es);
        salce.put(SALCE_TPSALDO, tpsaldo);     
        kill_rec(salce);
        for (int tpamm = 1; tpamm <= 3; tpamm++)
        { 
          ammce.zero();
          ammce.put(AMMCE_IDCESPITE, idcespite);
          ammce.put(AMMCE_CODES, es);
          ammce.put(AMMCE_TPSALDO, tpsaldo);
          ammce.put(AMMCE_TPAMM, tpamm);     
          kill_rec(ammce);
        }
      }      
    }
    idcespite.cut(0);
  }  
}

void TRegistro_cespiti::aggiorna_bollato()
{ 
  { //aggiorna la tabella CCB
    TTable ccb("CCB");
    ccb.curr() = ditta_cespiti().get_attivita();
    ccb.put("B1", "X"); //bollato stampato
    ccb.rewrite();
  } 
  
  const TDate oggi(TODAY);
  
  { //aggiorna il file SALCE
    TRelation rel (LF_SALCE); 
    TString expr = "CODES=";
    expr << _mask->get(F_ESERCIZIO);
    TCursor cursor(&rel, expr);     
    const long items = cursor.items();
    cursor.freeze();

    TRectype& curr = cursor.curr();
    for (cursor = 0; cursor.pos() < items; ++cursor)
    {    
      const TString16 idcespite = curr.get(SALCE_IDCESPITE);    //controlla che il cespite appartenga all'attivit� selezionata
      const TRectype& ces = cache().get(LF_CESPI, idcespite);
      if (ces.get_int(CESPI_CODCGRA) != _mask->get_int(F_GRUPPO))   
        continue;
      if (ces.get(CESPI_CODSPA) != _mask->get(F_SPECIE))   
        continue;        

      curr.put(SALCE_DTSTBOLL, oggi);
      rel.rewrite();
    }
  }
  
  { //aggiorna il file MOVCE
    TRelation rel (LF_MOVCE);
    TString expr;
    expr << "(ANSI(DTMOV)>=\""<<_mask->get_date(F_DATAINIZIO).string(ANSI)<<"\")&&";
    expr << "(ANSI(DTMOV)<=\""<<_mask->get_date(F_DATAFINE).string(ANSI)<<"\")";
    TCursor cursor(&rel, expr);     
    TRectype& curr = cursor.curr();
    const long items = cursor.items();
    cursor.freeze();
    for (cursor = 0; cursor.pos() < items; ++cursor)
    { 
      const TString16 idcespite = curr.get(MOVCE_IDCESPITE);    //controlla che il cespite appartenga all'attivit� selezionata
      const TRectype& ces = cache().get(LF_CESPI, idcespite);
      if (ces.get_int(CESPI_CODCGRA) != _mask->get_int(F_GRUPPO))   
        continue;
      if (ces.get(CESPI_CODSPA) != _mask->get(F_SPECIE))   
        continue;        
      curr.put(MOVCE_STAMPATO, "X");
      rel.rewrite();
    }            
  }
}
  
void TRegistro_cespiti::header_registro()
{  
  _form->find_field('H', first_page, FR_CODDITTA).set(_mask->get(F_CODDITTA));
  _form->find_field('H', first_page, FR_RAGSOC).set(_mask->get(F_RAGSOC));
  _form->find_field('H', first_page, FR_GRUPPO).set(_mask->get(F_GRUPPO));
  _form->find_field('H', first_page, FR_D_GRUPPO).set(_mask->get(F_D_GRUPPO));
  _form->find_field('H', first_page, FR_SPECIE).set(_mask->get(F_SPECIE));
  _form->find_field('H', first_page, FR_D_SPECIE).set(_mask->get(F_D_SPECIE));
  _form->find_field('H', first_page, FR_DATAINIZIO).set(_mask->get(F_DATAINIZIO));
  _form->find_field('H', first_page, FR_DATAFINE).set(_mask->get(F_DATAFINE));
  TString key; key.format("%04d",_mask->get_int(F_ESERCIZIO));
  
  const TRectype& cce = cache().get("CCE", key);  
  _form->find_field('H', first_page, FR_NUMGIOSOL).set(cce.get("I3"));  
  const int giorni = int(_mask->get_date(F_DATAFINE) - _mask->get_date(F_DATAINIZIO) + 1);
  key.format("%3d",giorni);
  _form->find_field('H', first_page, FR_NUMGIORNI).set(key);
  
  _form->find_field('H', first_page, FR_LIBROCESPITI); // .enable(_bollato);
  _form->set_testata();
  
  //se stampa bollato -> nascondi data stampa
  _form->find_field('F', last_page, FR_DATASTAMPA).show(_mask->get_int(F_TIPOREGISTRO) != 2); 
  _form->set_piede();
}


void TRegistro_cespiti::print_cespite()
{
  // stampa i dati del cespite
  const long pos = _form->cursor()->pos(); //memorizza posizione cursore
  
  header_cespite();
  print_saldiiniziali();
  print_movimenti();
  print_valorifinali();

  *_form->cursor() = pos;           //riposiziona il cursore
  return;
}

void TRegistro_cespiti::header_cespite()
{         
  // intestazione cespite
  TPrint_section& section = _form->get_testata_cespite();
  section.update();
  _form->set_pagina();
  for (word i = 0; i < section.height(); i++)
    printer().print(section.row(i));
  return;
}

void TRegistro_cespiti::print_saldiiniziali()
{ 
  // saldi iniziali cespite (da salce e ammce con tpsaldo=1(saldo iniziale) ed esercizio = esercizio selezionato)
  const int esercizio = _mask->get_int(F_ESERCIZIO);
  TRectype& recsalce = _form->cursor()->curr(LF_SALCE);
  bool continua = _form->cursor()->is_first_match(LF_SALCE);
  while (continua && (recsalce.get_int(SALCE_TPSALDO) != 1 || recsalce.get_int(SALCE_CODES) != esercizio))
    continua = _form->cursor()->next_match(LF_SALCE);
  if (!continua)
    recsalce.zero();   

  TRectype& recammce = _form->cursor()->curr(LF_AMMCE);
  continua = _form->cursor()->is_first_match(LF_AMMCE);
  while (continua && recammce.get_int(AMMCE_TPAMM)!= _tipoamm)  //l'esercizio � quello selezionato poich� � nella chiave
    continua = _form->cursor()->next_match(LF_AMMCE);
  if (!continua)
    _form->cursor()->curr(LF_AMMCE).zero();   

  TPrint_section& section = _form->get_saldiiniziali();
  section.update();
  _form->set_pagina();
  for (word i = 0; i < section.height(); i++)
    printer().print(section.row(i));
  return;
}


void TRegistro_cespiti::stampa_sezione(TPrint_section& section)
{
  section.update();
  _form->set_pagina();
  word y1 = section.find_field(FR_MV_QNOR).y();
  word y2 = section.find_field(FR_MV_QANT).y();
  for (word i = 0; i < section.height(); i++)
  {
    if (i>=y1-1 && i<= y2-1)
    {
      TPrintrow& row = section.row(i);
      TString rowstr = row.row();
      if (!rowstr.blank())        //se almeno 1 valore della riga � <> 0 allora la riga viene stampata, in caso contrario viene saltata
        printer().print(section.row(i));
    }
    else    
      printer().print(section.row(i));
  }
}


void TRegistro_cespiti::stampa_movimento(TRectype& recmovce, TRectype& recmovam, TRectype& recammmv)
{ 
  TPrint_section& section = _form->get_movimenti();
  _form->cursor()->curr(LF_MOVCE) = recmovce;      //
  _form->cursor()->curr(LF_MOVAM) = recmovam;      //copia il record del cursore che scannerizza movimenti e rettifiche nel
  _form->cursor()->curr(LF_AMMMV) = recammmv;      //cursore principale del form
  
  if (recmovce.get_char(MOVCE_SEGNO)=='-')
  {
    section.find_field(FR_MV_SEGNO).set("-1");
  } 
  else  
  {
    section.find_field(FR_MV_SEGNO).set("1");
  }
  
  section.update();
  _form->set_pagina();

  for (word i = 0; i < section.height(); i++)
  {
    TPrintrow& row = section.row(i);
    TString rowstr = row.row();
    if (!rowstr.blank())
      printer().print(row);
  }    
}

void TRegistro_cespiti::print_movimenti()
{ 
  TPrint_section& totali = _form->get_totali();
  // movimenti
  for (bool ok=_form->cursor()->is_first_match(LF_MOVCE); ok; ok=_form->cursor()->next_match(LF_MOVCE))
  { 
    TRectype& recmovce = _form->cursor()->curr(LF_MOVCE);
    const TString16 idret = recmovce.get(MOVCE_IDRET);
    if (idret.not_empty())
      continue;
    
    const TDate dtmov = recmovce.get(MOVCE_DTMOV);
    const int esmov =  _esc.date2esc(dtmov);
    if (_mask->get_int(F_ESERCIZIO) != esmov)
      continue;
      
    for(word t = 0; t < totali.fields(); t++)     //azzera i campi dei totali
      totali.field(t).set("");
    
    TRectype& recmovam = _form->cursor()->curr(LF_MOVAM);
    bool continua = _form->cursor()->is_first_match(LF_MOVAM);  
    while (recmovam.get_int(MOVAM_TPAMM) != _tipoamm && continua)
      continua = _form->cursor()->next_match(LF_MOVAM);
    if (!continua) recmovam.zero();  
    TRectype& recammmv = _form->cursor()->curr(LF_AMMMV);
    continua = _form->cursor()->is_first_match(LF_AMMMV);  
    while (recammmv.get_int(AMMMV_TPAMM) != _tipoamm && continua)
      continua = _form->cursor()->next_match(LF_AMMMV);
    if (!continua) recammmv.zero();
                                    
    stampa_movimento(recmovce,recmovam,recammmv);   //stampa i movimenti SENZA rettifiche
    
  // rettifiche (� necessario utilizzare un nuovo cursore, quindi si comincia con la relazione...)  
    TRelation rel_rettif(LF_MOVCE);
    TString expr; expr << "IDCESPITE==IDCESPITE|IDMOV==IDMOV|TPAMM==\"" << _tipoamm << '"';
    rel_rettif.add(LF_MOVAM, expr);
    rel_rettif.add(LF_AMMMV, expr);
    const TString16 id = recmovce.get(MOVCE_IDCESPITE);
    
  // e quindi si passa al cursore...; notare che il cursore delle rettifiche usa la chiave 2: IDCESPITE+IDMOV;
  // per cui parte per selezionare le possibili rettifiche del movimento da rettificare da lui... 
    TRectype fromret(LF_MOVCE);
    fromret.put(MOVCE_IDCESPITE, id);                     //IDCESPITE della chiave 2
    fromret.put(MOVCE_IDMOV, recmovce.get(MOVCE_IDMOV));  //IDMOV della chiave 2
  //...in poi
    TRectype toret(LF_MOVCE);
    toret.put(MOVCE_IDCESPITE, id);
    
    TString filtro;
    filtro << MOVCE_IDRET << "==\"" << recmovce.get(MOVCE_IDMOV) << '"'; //confronta numericamente IDRET e IDMOV
  //ed ecco il cursore!
    TCursor rettcur(&rel_rettif, filtro, 2, &fromret, &toret);
    const long items = rettcur.items();
    if (items > 0)
    {
      _form->cursor()->relation()->save_status();   //salva posizione di tutti i files del cursore 
                                                        //(serve per rettifiche "staccate" dai loro movimenti)
      rettcur.freeze();
      for (rettcur = 0l ; rettcur.pos()<items; ++rettcur)
        stampa_movimento(rettcur.curr(LF_MOVCE), rettcur.curr(LF_MOVAM), rettcur.curr(LF_AMMMV));
      
      _form->cursor()->relation()->restore_status();   //ripristina la posizione di tutti i files del cursore
    }
    
    stampa_sezione(totali);
  }   
  return;
}

void TRegistro_cespiti::print_valorifinali()
{
  // valori a fine esercizio cespite (da salce e ammce con tpsaldo=2)
  const int esercizio = _mask->get_int(F_ESERCIZIO);   
  
  TRectype& recsalce = _form->cursor()->curr(LF_SALCE);
  const TString16 idcespite = _form->cursor()->curr().get(CESPI_IDCESPITE);
  bool continua = recsalce.get(SALCE_IDCESPITE) == idcespite;
  while (continua && (recsalce.get_int(SALCE_TPSALDO) !=2 || recsalce.get_int(SALCE_CODES) != esercizio))
    continua = _form->cursor()->next_match(LF_SALCE);
  if (!continua)
    recsalce.zero();    
    
  TRectype& recammce = _form->cursor()->curr(LF_AMMCE);
  continua = recammce.get(AMMCE_IDCESPITE) == idcespite;   
  while (recammce.get_int(AMMCE_TPAMM) != _tipoamm  && continua)
    continua = _form->cursor()->next_match(LF_AMMCE);
  if (!continua)
    recammce.zero();    
    
  TPrint_section& section = _form->get_valorifinali();
  
  bool msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG01);
  section.find_field(FR_FE_MSG1).enable(msg);
  msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG02);
  TString80 key; key.format("%04d",_mask->get_int(F_ESERCIZIO));
  const TRectype& cce = cache().get("CCE", key);  
  long giorni = (_mask->get_date(F_DATAFINE) - _mask->get_date(F_DATAINIZIO) + 1);
  key.format("Amm.to ragguagliato all'esercizio di %3d giorni su %3d",giorni, cce.get("I3"));
  section.find_field(FR_FE_MSG2).set(key);  
  section.find_field(FR_FE_MSG2).enable(msg);
  msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG03);
  const int usoprom = _form->cursor()->curr().get_int(CESPI_USOPROM);
  if (usoprom==2 || usoprom==4)
    key="Ammortamento ridotto al 50% per bene in uso promiscuo";
  else if (usoprom==3 || usoprom==5)
          key="Ammortamento ridotto all'80% per bene in uso promiscuo";
  section.find_field(FR_FE_MSG3).set(key);
  section.find_field(FR_FE_MSG3).enable(msg);
  msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG04);
  section.find_field(FR_FE_MSG4).enable(msg);
  msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG05);
  section.find_field(FR_FE_MSG5).enable(msg);
  msg = _form->cursor()->curr(LF_AMMCE).get_bool(AMMCE_MSG06);
  section.find_field(FR_FE_MSG6).enable(msg);
  section.update();
  _form->set_pagina();
  word y1 = section.find_field(FR_FE_QNOR).y();
  word y2 = section.find_field(FR_FE_MSG6).y();
  for (word i = 0; i < section.height(); i++)
  {
    if (i+1>=y1 && i+1<= y2)
    {
      TPrintrow& row = section.row(i);
      TString256 rowstr = row.row();
      if (!rowstr.blank())
        printer().print(section.row(i));
    }
    else    
      printer().print(section.row(i));
  }   
  return;
}

void TRegistro_cespiti::header_categoria(const int categoria)
{
  // intestazione della categoria
  TPrint_section& section = _form->get_testata_categoria();
  section.update();
  _form->set_pagina();
  for (word i = 0; i < section.height(); i++)
    printer().print(section.row(i));
  return ;
}

void TRegistro_cespiti::footer_categoria()
{
  // stampa il footer della categoria
  TPrint_section& section = _form->get_piede_categoria();
  section.update();
  _form->set_pagina();
  for (word i = 0; i < section.height(); i++)
    printer().print(section.row(i));
  return ;
}

void TRegistro_cespiti::footer_registro()
{
  // stampa il footer del registro (totali generali)
  TPrint_section& section = _form->get_piede_registro();
  section.update();
  _form->set_pagina();
  for (word i = 0; i < section.height(); i++)
    printer().print(section.row(i));
  return ;
}

int ce3100(int argc, char* argv[])
{
  TRegistro_cespiti a;
  a.run(argc,argv,"Stampa registro cespiti");
  return 0;
}