// 77g1.cpp - Quadro G1
#include <relapp.h>
#include <config.h>
#include <msksheet.h>
#include <sort.h>
#include <tabutil.h>
#include <execp.h>
#include <progind.h>
#include <defmask.h>

#include "quadrog1.h"
#include "77lib.h"
#include "774200.h"
#include "77g1a.h"   
#include "771232.h"

HIDDEN const int	RIGHE_PI = 11;

TQuadroG1::TQuadroG1()
{
  _registra   = FALSE;
  _quadro = "G1";  
  _title  = "Quadro G1";
}

inline TQuadroG1& app() { return (TQuadroG1&) main_app(); }

void TQuadroG1::init_insert_mode (TMask& m)
{
  fill_arr(m);
}

void TQuadroG1::init_modify_mode (TMask& m)
{

}

int TQuadroG1::read(TMask& m)
{
  int err = TRelation_application::read(m);
  if (err == NOERR)
    fill_arr(m);
  return err;
}

void TQuadroG1::init_query_mode (TMask& m)
{
  TString16 codditta; codditta << _codditta;
  if (codditta != "0")        
  {
    m.field(F_CODDITTA).set(codditta);
    m.field(F_CODDITTA).check();
  }
  m.send_key(K_AUTO_ENTER,0);
//  init_mask(m);
}

//void TQuadroG1::init_mask (TMask& m){}

void TQuadroG1::update_totals(TSheet_field& f)
{
  int i = 0;
      real tcol2, tcol3, tcol5, tcol6, tcol7;
      tcol2 = tcol3 = tcol5 = tcol6 = tcol7 = ZERO;
      TSheet_field&	pI = f;
// Calcolo il totale
      for (i = 0;  i < RIGHE_PI; i++)
      {
        TToken_string& r = pI.row(i);
        real c2(r.get(1));
        real c3(r.get(2));
        real c5(r.get(4));
        real c6(r.get(5));
        real c7(r.get(6));                        
        tcol2  += c2;
        tcol3  += c3;
        tcol5  += c5;
        tcol6  += c6;                
        tcol7  += c7;
      }
// Setto le celle di totale
      TToken_string& row = pI.row(i);
      row.add(tcol2.string(), 1);
      row.add(tcol3.string(), 2);
      row.add(tcol5.string(), 4);
      row.add(tcol6.string(), 5);
      row.add(tcol7.string(), 6);                  
      pI.force_update(i);
}

bool TQuadroG1::user_create() 
{
  _msk = new TMask("77g1a");
  _rel = new TRelation(LF_QUAG1);
  _codditta = get_firm_770();
  _msk->disable(DLG_CANCEL);   // disabilito Annulla nella toolbar
  _msk->disable(DLG_NEWREC);   // disabilito Nuovo   nella toolbar
  _msk->disable(DLG_DELREC);   // disabilito Elimina nella toolbar
  _msk->disable(DLG_FINDREC);  // disabilito Ricerca nella toolbar
  _msk->set_handler(mainmsk_handler);
  TSheet_field& p1 = (TSheet_field&) _msk->field(F_PI);  
  p1.set_notify(p1_notify);                          
  return TRUE;
}

bool TQuadroG1::user_destroy() 
{
  delete _rel; delete _msk;
  if (_registra)
    _rip.set(_quadro);  
  return TRUE;
}                           

void TQuadroG1::arr2rel(const TMask& m)
{
  TToken_string p3a;
  TToken_string p3b;
  TToken_string p3c;    
  TToken_string p3d;    
  TToken_string p3e;        
  TToken_string p3f;        
  TRectype& r = _rel->curr();        
  int dlg; dlg = 0;
  
// Prospetto 3 - Prospetto delle ritenute
  TSheet_field&	pI = (TSheet_field&)m.field(F_PI);
  
  for (int i = 0;  i < RIGHE_PI; i++)
  {
    TToken_string& r = pI.row(i);
    TString ut94 (r.get(1));  // Salto data delibera che e' fisso
    TString imp  (r.get());   
    TString ali  (r.get());
    TString racc (r.get());
    TString rimp (r.get());
    TString utnsogg (r.get());
    p3a.add(ut94);
    p3b.add(imp);
    p3c.add(ali);
    p3d.add(racc);
    p3e.add(rimp);
    p3f.add(utnsogg);        
  }
  r.put(QG1_P12, p3a);
  r.put(QG1_P13, p3b);
  r.put(QG1_P14, p3c);
  r.put(QG1_P15, p3d);
  r.put(QG1_P16, p3e);
  r.put(QG1_P17, p3f); 
// Salvo i totali che servono poi per i riporti (774200.cpp)
  TToken_string& r_tot = pI.row(i);
  real tot_col2(r_tot.get(1));
  real tot_col3(r_tot.get(2));  
  real tot_col5(r_tot.get(4));  
  real tot_col6(r_tot.get(5));    
  real tot_col7(r_tot.get(6));    
  r.put(QG1_P12TOT, tot_col2);
  r.put(QG1_P13TOT, tot_col3);
  r.put(QG1_P15TOT, tot_col5);
  r.put(QG1_P16TOT, tot_col6);      
  r.put(QG1_P17TOT, tot_col7);      
}

void TQuadroG1::fill_arr(TMask& m)
{                                      
  TRectype& r = _rel->curr();        
  TToken_string p3a(r.get(QG1_P12));
  TToken_string p3b(r.get(QG1_P13));
  TToken_string p3c(r.get(QG1_P14));    
  TToken_string p3d(r.get(QG1_P15));    
  TToken_string p3e(r.get(QG1_P16));        
  TToken_string p3f(r.get(QG1_P17));        
  int dlg, j; dlg = j = 0;

  const char* date_delib[20] = 
  {
    "18-12-1977/31-12-1995",
    "18-12-1977/31-12-1995",
    "18-12-1977/31-12-1995",
    "11-10-1976/17-12-1977",    
    "11-10-1976/17-12-1977",    
    "11-10-1976/17-12-1977",    
    "1-1-1974/10-10-1976",                
    "1-1-1974/10-10-1976",                
    "24-2-1964/31-12-1973",
    "24-2-1964/31-12-1973",
    "1-1-1963/23-2-1964"
  };
  
  const char* aliq[15] =
  {
    "10",
    "12.5",
    "30",
    "10",
    "30",
    "50",
    "10",
    "30",
    "5",
    "30",
    "15"
  };
  
  TSheet_field&	pI = (TSheet_field&)m.field(F_PI);
  const int COL_ALIQ = 3;
  int       ind_aliq = 0;
  for (int i = 1; i <= RIGHE_PI; i++)
  { 
    TRectype& r = _rel->curr();        
    TToken_string& row = pI.row(i-1);
    row.add(date_delib[i-1]);
    row.add(p3a.get(i-1));
    row.add(p3b.get(i-1));
    row.add(p3c.get(i-1));
    row.add(p3d.get(i-1));
    row.add(p3e.get(i-1));
    row.add(p3f.get(i-1));           
    row.add(aliq[ind_aliq++], 3);
    pI.disable_cell(i-1, COL_ALIQ);
  }
  
  if (m.mode() == MODE_MOD)
    update_totals(pI);
  else
  {
// Aggiungo la riga per i totali     
    TToken_string& row = pI.row(i-1);
    row.add("Totali", 0);
    pI.disable_cell(i-1, -1);
    pI.force_update();
  }
}

int TQuadroG1::write(const TMask& m)
{
  arr2rel(m);
  int err = TRelation_application::write(m);
  _registra = err == NOERR;    
  return err;
}

int TQuadroG1::rewrite(const TMask& m)
{
  arr2rel(m);
  int err = TRelation_application::rewrite(m);
  _registra = err == NOERR;    
  return err;
}

bool TQuadroG1::remove()
{  
  _registra = TRelation_application::remove();
  return _registra;
}

bool TQuadroG1::p1_notify(TSheet_field& s, int r, KEY k)
{
  switch(k)
  {                               
    case K_ENTER:
      update_totals(s);    
      break;
    case K_SPACE: 
      break;
// Rifiuta di entrare sull'ultima riga
    case K_TAB: 
      return r < RIGHE_PI;
      break;
    case K_INS:                   
      if (s.mask().is_running())
        return FALSE;  // rifiuta inserimento di nuove righe
      break;
    default:
      break;
  }
  return TRUE;
}
 
bool TQuadroG1::mainmsk_handler(TMask& m, KEY k)
{ 
  if (k == K_ESC)
  {
  	TOperable_field& f = m.focus_field();
  	m.set_focus_field(f.dlg()); 
  }	
  return TRUE;
} 
