///////////////////////////////////////////////////////////
// Quadro 770/A
///////////////////////////////////////////////////////////

#include "77stqab.h" 

TDipendente::TDipendente(const TCursor& cur)
{                   
  _pos = ((TCursor&)cur).pos();
  
  const TRectype& rec = cur.curr();
  const TRectype& dip = cur.curr(LF_DIPEND);

  _codice_ditta      = rec.get_long("CODDITTA");
  _ragsoc            = dip.get("COGNOME"); _ragsoc << ' ' << dip.get("NOME");
  _codice_deceduto   = dip.get_long("CODDIPDEC");
  _codice_dipendente = rec.get_long("CODDIP");
  
  if (rec.num() == LF_QUAA)
  {
    _fine_rapporto        = rec.get("DATAFR");
    _lavoro_precedente    = rec.get("CFDLPREC").not_empty();
    _causa                = toupper(rec.get_char("CAUSA"));
    _rettifica_conguaglio = toupper(rec.get_char("RETCON"));
    _numero_progressivo   = rec.get_int("NPROG");
  }  
  else
  {
    _fine_rapporto        = rec.get("DATAFIN");
    _lavoro_precedente    = FALSE;
    _causa                = ' ';
    _rettifica_conguaglio = ' ';
    _numero_progressivo   = rec.get_int("NPROG");
  } 
  
  if (_rettifica_conguaglio == 'B' || !_fine_rapporto.ok())
    _fine_rapporto = eotime;
}

int TDipendente::compare(const TSortable& s) const
{
  const TDipendente& rec = (const TDipendente&)s;
  
  if (_codice_ditta != rec._codice_ditta)
    return _codice_ditta > rec._codice_ditta ? +1 : -1;

  const int diff = _ragsoc.compare(rec._ragsoc, -1, TRUE);
  if (diff != 0)
    return diff;
    
  if (_codice_dipendente != rec._codice_dipendente)  
    return _codice_dipendente > rec._codice_dipendente ? +1 : -1;
    
  if (_fine_rapporto != rec._fine_rapporto)                      
    return _fine_rapporto > rec._fine_rapporto ? +1 : -1;
    
  if (_lavoro_precedente != rec._lavoro_precedente)  
    return _lavoro_precedente ? +1 : -1;
    
  if (_causa != rec._causa)  
    return _causa > rec._causa ? +1 : -1;
    
  if (_rettifica_conguaglio != rec._rettifica_conguaglio)  
    return _rettifica_conguaglio > rec._rettifica_conguaglio ? +1 : -1;
                                                                       
  return _numero_progressivo - rec._numero_progressivo;                                                                     
}

int TDipendenti::find(long cod) const
{
  int i;
  for (i = _data.last(); i >= 0; i--)
    if (dip(i).codice() == cod)
      break;
  return i;    
}

int TDipendenti::fill(TCursor& cur)
{
  for (cur = 0; cur.ok(); ++cur)
  {
    TDipendente* n = new TDipendente(cur);
    if (n->pos() >= 0)
      _data.add(n);
    else
      delete n;  
  } 
  const int i = _data.items();
  if (i > 0)
  {
    _data.sort();
    for (int d = i-1; d >= 0; d--)
    {
      const long coddec = dip(d).codice_deceduto();
      if (coddec > 0)                     // E' un erede!
      {
        int posdec = find(coddec);
        if (posdec >= 0 && posdec != d-1) // Il deceduto esiste
        {
          TDipendente* dipen = (TDipendente*)_data.remove(d, TRUE);
          if (d < posdec)                 // Se l'erede precede il defunto ...
            posdec--;                     // ... il defunto avanza di un posto!
          dipen->reset_deceduto();        // Non e' piu' un erede
          _data.insert(dipen, posdec+1);  // Attaccalo dietro al deceduto
        }  
      }
    }
  }  
  return i;
}

void TQuadroA::preprocess_corpo(const TRectype& rec)
{
  if (rec.num() == LF_QUAA)
  {
    // Stampa solo le date di fine rapporto del 96   
    TPrint_section& body = section('B', odd_page);
    TForm_item& datafr = body.find_field(15);
    const TDate dfr = rec.get("DATAFR");
    datafr.enable(dfr.year() == 1996);
  }
}

void TQuadroA::stampa_testata(TPrinter& pr)
{
  TPrint_section& hh = section('H', first_page);
  hh.reset();

  TForm_item& nf=hh.find_field(_NUM_FOGLIO_QA);
  _num_foglio++;
  TString fstr(10); fstr << _num_foglio;
  nf.set(fstr);      
  
  hh.update();       
  const int hhr = hh.height();
  for (int i = 0; i < hhr; i++)
  {
    pr.print(hh.row(i));
    _RigaCorr++;
  }
  if (usa_progind())
    progind()->addstatus(1);
}

void TQuadroA::stampa_corpo(TPrinter& pr)
{
  TPrint_section& body = section('B', odd_page);
  const int body_righe = body.height();  
  body.reset();
  body.update();       

  for (int i = 0; i < body_righe; i++)
  {
    pr.print(body.row(i));
    _RigaCorr++;
  }

  if (usa_progind())
    progind()->addstatus(1);
}


int TQuadroA::calcola_firma()
{
  TPrint_section& totali = section('F', last_page);
  TForm_item& signature = totali.find_field(1);
  const int y = signature.y()-1;
  
  TString cognome = signature.get();
  if (cognome.len() > 30 && cognome[29] == ' ')
  {
    TString nome = cognome.mid(30, -1);
    cognome.cut(30); cognome.trim();
    cognome << ' ' << nome;
    signature.set(cognome);
  }   
  totali.row(y).reset();
  totali.row(y).put(cognome, signature.x()-1);
  
  return y;
}

void TQuadroA::stampa_totali(TPrinter& pr) 
{  
  TPrint_section& totali = section('F', last_page);
  const int totali_righe = totali.height();  
  totali.update();

  calcola_firma();
  
  for (int i = 0; i < totali_righe; i++)
  {
    pr.print(totali.row(i));
    _RigaCorr++;
  }                         
  
  if (usa_progind())
  progind()->addstatus(1);
}

void TQuadroA::next_page(TPrinter& pr)
{                       
  fill_page(pr, -1);   // formfeed "adattato"  
  inc_curr_page();
}

int TQuadroA::elementi_pagina(PaginaQuadro p) const
{ 
  const int n[4] = { 3, 4, 4, 4 };
  return n[p - PRIMA];
}

int TQuadroA::dic_form_len() const
{
  if (curr_page()==PRIMA && _PaginaPosizionamento)
    return QA_FORMLEN-HEADER_PRIMA_NOPOS_QA;
  else
    return QA_FORMLEN;
}

int TQuadroA::prima_riga(PaginaQuadro p) const
{
  int line = 0; 
  
  if (p==PRIMA)
  {
    if (_PaginaPosizionamento)
      line = 1;
    else
      line = 1+HEADER_PRIMA_NOPOS_QA;
  }
  
  else if (p==SECONDA) line = 3; 
    else if (p==TERZA) line = 3;  
      else if (p==QUARTA) line = 4;
 
  return line;    
}

bool TQuadroA::init(long codditta)
{
  return InitPrint(codditta);
}

bool TQuadroA::print(const long codditta, const long NumFis, const long NumNoFis)
{  
  TCursor& cur = *cursor();
  TPrinter& pr = printer();  
  _num_foglio=0;
    
  if (!init(codditta))
    return FALSE;
  
  int elementi = 0;    // Numero di elementi stampati in questa pagina
  bool stampato_ultimo = FALSE;
  bool stampati_totali = FALSE;
  long persone = 0;
  int pos = 0;
  TDipendenti indice;
  if (quadro() == "A" || quadro() == "B")
  {                 
    persone = indice.fill(cur);
    if (persone > 0)
    {                     
      if (usa_progind()) 
        progind()->addstatus(cur.items()-persone);  // Segna come elaborati tutti gli ignorati
      
      cur = indice[0].pos();
    }  
    else
      return FALSE;  
  }         
  else
  {   
    persone = cur.items();
    cur = 0;
  }  
  
  // se il dispositivo scelto non � una winprinter
  // non � mai eseguito il posizionamento
  if (pr.printtype() != winprinter)
    _PaginaPosizionamento = FALSE;  
  
  if (_modulaser) 
  {
    TPrintrow r;
    put_modulaser(r, VEGASTARTDOC);
    put_modulaser(r, VEGASTARTDITTA);
    put_modulaser(r, VEGASTARTPAGE, PRIMA);
    pr.print(r);
    _RigaCorr++;
  }
  
  while (!stampati_totali)
  {   
    bool stampero_ultimo = (stampato_ultimo == FALSE);
    if (stampero_ultimo)
    {
      if (indice.items() > 0)                          // Usa indice alternativo
        stampero_ultimo = (pos == persone-1);
      else                                             // Usa indice del cursore
        stampero_ultimo = (cur.pos() == persone-1);
    }                        

    if (!stampato_ultimo) 
      preprocess_corpo(cur.curr());
    
    pr.formlen(dic_form_len());
    
    switch(_PaginaCorrente)
    {                     
    // stampa prima pagina
    case PRIMA:  
      set_curr_page(PRIMA);          

      if (_modulaser) 
        ClearFlagModulaser();
      
      if (elementi == 0) 
      {
        jump_to_line(pr, prima_riga(PRIMA));
        stampa_testata(pr);                 
      }  
      
      if (!stampato_ultimo)
      {
        stampa_corpo(pr);       
        stampato_ultimo = stampero_ultimo;
      }
      
      elementi++;
      if (elementi >= elementi_pagina(PRIMA))
      {
        next_page(pr);
        _PaginaPosizionamento=FALSE;
        elementi = 0;
      }  
      break;             
    
    // stampa seconda o terza pagina
    case SECONDA:
    case TERZA:              
      if (!stampato_ultimo)
      {
        if (elementi == 0)
          jump_to_line(pr, prima_riga(_PaginaCorrente));      
        stampa_corpo(pr);       
        stampato_ultimo = stampero_ultimo;
      }
      elementi++;
      if (elementi >= elementi_pagina(_PaginaCorrente))
      {
        next_page(pr);
        elementi = 0;
      }  
      break;
    
    // stampa quarta pagina
    case QUARTA:
      if (!stampato_ultimo)
      {
        if (elementi == 0)
          jump_to_line(pr, prima_riga(QUARTA));      
        stampa_corpo(pr);       
        stampato_ultimo = stampero_ultimo;
      }
      
      elementi++;
 
      if (elementi >= elementi_pagina(QUARTA))
      {
        // salto ad inizio sezione
        jump_to_line(pr, 60);
        stampa_totali(pr);
        if (stampato_ultimo)  
          stampati_totali = TRUE;
        _EndPrintDitta=stampato_ultimo;
        next_page(pr);
        elementi = 0;
      }
      break;
    default:
      CHECK(0, "Invalid Quadro A page");
      break;
    }
    
    if (!stampato_ultimo)
    {
      if (indice.items() > 0)
        cur = indice[++pos].pos();
      else
        ++cur;
    }
  }
  
  close_print();

  return TRUE;
}

bool TStampaQuadroA::user_create()
{
  _form = new TQuadroA("77QA", quadro());

  TString sortkey(80);
  sortkey.format("CODDITTA|%d->COGNOME|%d->NOME|CODDIP", LF_DIPEND, LF_DIPEND);
  _cur = new TSorted_cursor(_form->TForm::relation(), sortkey);
  
  return TRUE;
}

bool TStampaQuadroA::user_destroy()
{         
  if (_cur)
    delete _cur;       
  
  if (_form)
    delete _form;

  return TRUE;
}          

/////////////////////////////////////////////////////////////////////////////////
// Quadro A1
/////////////////////////////////////////////////////////////////////////////////

class TQuadroA1 : public TQuadroD
{
protected:  
  virtual int elementi_pagina(PaginaQuadro p) const;
  virtual long filtra(const long codditta);
  virtual bool preprocess_body(const TCursor& cur);

public:
  TQuadroA1(const char* form, const char* quadro) : TQuadroD(form, quadro) { }
  virtual ~TQuadroA1() { } 
};      

int TQuadroA1::elementi_pagina(PaginaQuadro p) const
{
  const int n[4] = { 4, 6, 6, 5 };
  return n[p-PRIMA];
}

long TQuadroA1::filtra(const long codditta)
{
  TString filtr(32); 
  filtr.format("(CODDITTA=%ld)&&(ANNORIF=1996)", codditta);

  cursor()->setfilter(filtr, TRUE);   
  const long items = cursor()->items();

  return items;
}      

bool TQuadroA1::preprocess_body(const TCursor& cur)
{
  const TRectype& rec = cur.curr();
  if (rec.num() == LF_QUAA1)
  {
    // Stampa solo le date di ricezione del 96
    const TDate dr = rec.get("DATARIC26");
    TForm_item& dataric = find_field('B', odd_page, 26);  
    dataric.enable(dr.year() == 1996);
  }
  return TRUE;
}  

bool TStampaQuadroA1::user_create()
{
  _form = new TQuadroA1("77QA1", quadro());
                                                                
  TString sortkey(80);
  sortkey.format("CODDITTA|%d->COGNOME|%d->NOME|CODDIP|MESERIF|NPROG", LF_DIPEND, LF_DIPEND);
  _cur = new TSorted_cursor(_form->TForm::relation(), sortkey);
  
  return TRUE;
} 


/////////////////////////////////////////////////////////////////////////////////
// Quadro A2
/////////////////////////////////////////////////////////////////////////////////

class TQuadroA2 : public TQuadroD
{
protected:  
  virtual int elementi_pagina(PaginaQuadro p) const;
  virtual bool preprocess_body(const TCursor& cur);

public:
  TQuadroA2(const char* form, const char* quadro) : TQuadroD(form, quadro) { }
  virtual ~TQuadroA2() { } 
};      

int TQuadroA2::elementi_pagina(PaginaQuadro p) const
{
  const int n[4] = { 4, 6, 6, 5 };
  return n[p-PRIMA];
}

bool TQuadroA2::preprocess_body(const TCursor& cur)
{                     
  static bool flag = FALSE;
  const TRectype& rec = cur.curr();
  if (rec.num() == LF_QUAA2)
  {
    char ret_con = toupper(rec.get_char("RETCON"));
    
    if (flag) 
    {
      ret_con = 'X';
      flag = FALSE;
    }
    else
      flag = ret_con == 'A';
    
    // Nasconde i dati tranne il codice fiscale (al secondo giro)
    TPrint_section& body = section('B', odd_page);
    int i;
    for (i = 2; i <= 25; i++)
    {
      TForm_item& f = body.find_field(i);
      f.enable(ret_con != 'X');
    }
    for (i = 101; i <= 109; i++)
    {
      TForm_item& f = body.find_field(i);
      f.enable(ret_con == 'X');
    }
    return !flag;
  }
  return TRUE;
}

bool TStampaQuadroA2::user_create()
{
  _form = new TQuadroA2("77QA2", quadro());
                                                                
  TString sortkey(80);
  sortkey.format("CODDITTA|%d->COGNOME|%d->NOME|CODDIP|RETCON|NPROG", LF_DIPEND, LF_DIPEND);
  _cur = new TSorted_cursor(_form->TForm::relation(), sortkey);
  
  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////
// Quadro A3
/////////////////////////////////////////////////////////////////////////////////

class TQuadroA3 : public TQuadroD
{
protected:  
  virtual int elementi_pagina(PaginaQuadro p) const;

public:
  TQuadroA3(const char* form, const char* quadro) : TQuadroD(form, quadro) { }
  virtual ~TQuadroA3() { } 
};      

int TQuadroA3::elementi_pagina(PaginaQuadro p) const
{
  const int n[4] = { 8, 10, 10, 9 };
  return n[p-PRIMA];
}

bool TStampaQuadroA3::user_create()
{
  _form = new TQuadroA3("77QA3", quadro());
                                                                
  TString sortkey(80);
  sortkey.format("CODDITTA|NPROG|%d->COGNOME|%d->NOME|CODDIP", LF_DIPEND, LF_DIPEND);
  _cur = new TSorted_cursor(_form->TForm::relation(), sortkey);
  
  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////
// Quadro Abis
/////////////////////////////////////////////////////////////////////////////////

class TQuadroAbis : public TQuadroA2
{
protected:  
  virtual int  elementi_pagina(PaginaQuadro p) const;
  virtual bool preprocess_body(const TCursor& cur);

public:
  TQuadroAbis(const char* form, const char* quadro) : TQuadroA2(form, quadro) { }
  virtual ~TQuadroAbis() { } 
};

int TQuadroAbis::elementi_pagina(PaginaQuadro p) const
{
  const int n[4] = { 4, 5, 5, 4 };
  return n[p-PRIMA];
}

bool TQuadroAbis::preprocess_body(const TCursor& cur)
{                     
  static bool flag = FALSE;
  const TRectype& rec = cur.curr();
  if (rec.num() == LF_QUAAB)
  {
    char ret_con = toupper(rec.get_char("RETCON"));
    
    if (flag) 
    {
      ret_con = 'X';
      flag = FALSE;
    }
    else
      flag = ret_con == 'A';
    
    // Nasconde i dati tranne il codice fiscale (al secondo giro)
    TPrint_section& body = section('B', odd_page);
    int i;
    for (i = 2; i <= 37; i++)
    {
      TForm_item& f = body.find_field(i);
      f.enable(ret_con != 'X');
    }
    for (i = 101; i <= 112; i++)
    {
      TForm_item& f = body.find_field(i);
      f.enable(ret_con == 'X');
    }
    
    return !flag;
  }
  return TRUE;
}

bool TStampaQuadroAbis::user_create()
{
  _form = new TQuadroAbis("77QAB", quadro());
                                                                
  TString sortkey(80);
  sortkey.format("CODDITTA|%d->COGNOME|%d->NOME|CODDIP|RETCON|NPROG", LF_DIPEND, LF_DIPEND);
  _cur = new TSorted_cursor(_form->TForm::relation(), sortkey);
  
  return TRUE;
}