// 771230.cpp - Quadri C, D, D bis, D1, E, E1, E2
#include <relapp.h>
#include <msksheet.h>
#include "caus77.h"
#include "77lib.h"
#include "scperc.h"
#include "defmask.h"
#include "771230.h" 
#include "774200.h"

class TQuadroC_E2 : public TRelation_application
{
private:                                             
  TString16   _quadro;  // Codice del quadro in gestione        
  int         _file;
  bool        _registra;  // Se TRUE fa i riporti
  TRelation*  _rel;
  TMask*      _msk;
  long        _codditta;    
  
private:               
  bool QuadroC()    const { return _quadro == "C"; }  
  bool QuadroD()    const { return _quadro == "D"; }  
  bool QuadroDbis() const { return _quadro == "DB"; }  
  bool QuadroD1()   const { return _quadro == "D1"; }  
  bool QuadroE()    const { return _quadro == "E"; }  
  bool QuadroE1()   const { return _quadro == "E1"; }  
  bool QuadroE2()   const { return _quadro == "E2"; }  
                    
protected:
  virtual bool user_create();
  virtual bool user_destroy();         
  virtual int  read(TMask& m);
  virtual int  rewrite(const TMask& m);
  virtual int  write  (const TMask& m);  
  virtual bool remove();
  virtual TRelation* get_relation() const { return _rel; }
  virtual bool changing_mask(int mode) { return FALSE; }
  virtual TMask* get_mask(int mode) { return _msk; }
  virtual void init_query_mode (TMask&);
  virtual void init_insert_mode (TMask&);
  virtual void init_modify_mode (TMask&);
  
protected: 
  void fill_sheet(const TMask&); 
  void pack_sheet(const TMask&);
  
  static bool ricalcola_imposte_c(TMask_field& f, KEY key);
  static bool ricalcola_imposte_d(TMask_field& f, KEY key);
  static bool ricalcola_imposte_dbis(TMask_field& f, KEY key);
  static bool ricalcola_imposte_e(TMask_field& f, KEY key);
  static bool ricalcola_imposte_e1(TMask_field& f, KEY key);
  static bool prospetto_e1_notify(TSheet_field& s, int r, KEY k);
  void reset_prospetto_e1(); 
  void read_prospetto_e1(); 
  int write_prospetto_e1();
    
  static bool ricalcola_imposte_e2(TMask_field& f, KEY key);
  static bool ritenute_handler_e2(TMask_field& f, KEY key);

public:                    
  TQuadroC_E2(const char* quadro);
  virtual ~TQuadroC_E2() {};
};                    

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

TQuadroC_E2::TQuadroC_E2(const char* quadro)
           : _msk(NULL), _rel(NULL), _quadro(quadro), _file(0)
{  
}

int TQuadroC_E2::rewrite(const TMask& m)
{
  pack_sheet(m);
  int err = TRelation_application::rewrite(m);
  if (err == NOERR && QuadroE1())
    err = write_prospetto_e1();
  if (err == NOERR)  
    _registra = TRUE;
  return err;
}

int TQuadroC_E2::read(TMask& m)
{
  int err = TRelation_application::read(m);
  if (err == NOERR && QuadroE1())
    read_prospetto_e1();
  return err;
}

int TQuadroC_E2::write(const TMask& m)
{               
  pack_sheet(m);
  int err = TRelation_application::write(m);
  if (err == NOERR && QuadroE1())
    err = write_prospetto_e1();
  if (err == NOERR)  
  _registra = TRUE;
  return err;
}

bool TQuadroC_E2::remove()
{        
  bool ok = TRelation_application::remove();
  if (ok && QuadroE1())
  {
    reset_prospetto_e1();
    write_prospetto_e1();
    _registra = TRUE;
  }
  return ok;
}

bool TQuadroC_E2::user_create()
{                      
  _codditta = get_firm_770();
  _registra = FALSE;
  
  TFilename name("771230"); 
  name << _quadro;
  _msk = new TMask(name);
  set_search_field(F_CODANAGR);
  
  TSheet_field& s = (TSheet_field&)_msk->field(F_RIGHE);
  TMask& m = s.sheet_mask();
  if (QuadroC())
  {
    _file = LF_QUAC;
    m.set_handler(102, ricalcola_imposte_c);
    m.set_handler(105, ricalcola_imposte_c);
    m.set_handler(107, ricalcola_imposte_c);
    m.set_handler(108, ricalcola_imposte_c);
  } else
  if (QuadroD())
  {
    _file = LF_QUAD;
    m.set_handler(102, ricalcola_imposte_d);
    m.set_handler(104, ricalcola_imposte_d);
    m.set_handler(105, ricalcola_imposte_d);
    m.set_handler(106, ricalcola_imposte_d);
    m.set_handler(107, ricalcola_imposte_d);
  } else
  if (QuadroD1())
  {
    _file = LF_QUAD1;
    m.set_handler(102, ricalcola_imposte_d);
    m.set_handler(104, ricalcola_imposte_d);
    m.set_handler(105, ricalcola_imposte_d);
    m.set_handler(106, ricalcola_imposte_d);
    m.set_handler(107, ricalcola_imposte_d);
  } else
  if (QuadroDbis())
  {
    _file = LF_QUADBIS;
    m.set_handler(102, ricalcola_imposte_dbis);
    m.set_handler(104, ricalcola_imposte_dbis);
    m.set_handler(105, ricalcola_imposte_dbis);
    m.set_handler(106, ricalcola_imposte_dbis);
    m.set_handler(107, ricalcola_imposte_dbis);
  } else
  if (QuadroE())
  {                
    _file = LF_QUAE;
    m.set_handler(104, ricalcola_imposte_e);
    m.set_handler(105, ricalcola_imposte_e);
  } else
  if (QuadroE1())
  {
    _file = LF_QUAE1;
    m.set_handler(105, ricalcola_imposte_e1);
    m.set_handler(106, ricalcola_imposte_e1);
    TSheet_field& s = (TSheet_field&)_msk->field(F_PROSPETTO);
    s.set_notify(prospetto_e1_notify);
  } else
  if (QuadroE2())
  {
    _file = LF_QUAE2;
    m.set_handler(103, ricalcola_imposte_e2);
    m.set_handler(104, ricalcola_imposte_e2);
    m.set_handler(105, ritenute_handler_e2);
  }

  _rel = new TRelation(_file);
  
  return TRUE;
}

bool TQuadroC_E2::user_destroy()
{ 
  if (_registra)
  {
    TRiporti rip;
    rip.set(_quadro);
  }

  delete _rel; 
  delete _msk;
  
  return TRUE;
}

void TQuadroC_E2::init_query_mode(TMask& m)
{
  m.set(F_CODDITTA, _codditta);
  TSheet_field& sf = (TSheet_field&)m.field(F_RIGHE);
  sf.destroy();
    
  if (QuadroE1())
    reset_prospetto_e1();
}

void TQuadroC_E2::init_insert_mode(TMask& m)
{
  fill_sheet(m);
}

void TQuadroC_E2::init_modify_mode(TMask& m)
{
  fill_sheet(m);
}

void TQuadroC_E2::fill_sheet(const TMask& m)
{
  TSheet_field& sf = (TSheet_field&)m.field(F_RIGHE);
  for (int r = sf.items(); r < 16; r++)
    sf.row(r);   
}

void TQuadroC_E2::pack_sheet(const TMask& m)
{
  TSheet_field& sf = (TSheet_field&)m.field(F_RIGHE);
  for (int r = sf.items(); r >= 0; r--)
  {
    TToken_string& row = sf.row(r);
    if (row.empty_items())
      sf.destroy(r);
  }  
}

bool TQuadroC_E2::ricalcola_imposte_c(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    if (m.get_bool(115) == FALSE)  // Calcola solo se non generata dalle schede
    {                             
      if (f.dlg() == 105 || f.dlg() == 107)
      {
        real amm_lordo  = m.get(105);
        real somme_non  = m.get(107);
        real imponibile = amm_lordo - somme_non;
        m.set(108, imponibile);
      }
      
      real aliquota   = m.get(102);
      real imponibile = m.get(108);
      real imposta    = aliquota * imponibile / 100.0;
      m.set(109, imposta, TRUE);
    }
  }
  return TRUE;
}

bool TQuadroC_E2::ricalcola_imposte_d(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    if (m.get_bool(109) == FALSE)  // Calcola solo se non generata dalle schede
    {                               
      if (f.dlg() >= 104 && f.dlg() <= 106 )
      {      
        real imponibile = m.get(104);    // Ammontare lordo
        imponibile -= m.get_real(105);   // Somme non soggette
        imponibile -= m.get_real(106);   //   "    "     "     reg. conv.
        m.set(107, imponibile);
      }
      
      real aliquota   = m.get(102);
      real imponibile = m.get(107);
      real imposta    = imponibile * aliquota / 100.0;
      m.set(108, imposta);
    }
  }
  return TRUE;
}

bool TQuadroC_E2::ricalcola_imposte_dbis(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    if (m.get_bool(110) == FALSE)  // Calcola solo se non generata dalle schede
    {                               
      if (f.dlg() >= 104 && f.dlg() <= 106 )
      {      
        real imponibile = m.get(104);    // Ammontare lordo
        imponibile -= m.get_real(105);   // Somme non soggette
        imponibile -= m.get_real(106);   //   "    "     "     reg. conv.
        m.set(107, imponibile);
      }
      
      real aliquota   = m.get(102);
      real imponibile = m.get(107);
      real imposta    = imponibile * aliquota / 100.0;
      m.set(108, imposta);
    }
  }
  return TRUE;
}

bool TQuadroC_E2::ricalcola_imposte_e(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    real aliquota   = m.get(105);
    real imponibile = m.get(104);
    real imposta    = imponibile * aliquota / 100.0;
    m.set(106, imposta, TRUE);
  }
  return TRUE;
}

bool TQuadroC_E2::ricalcola_imposte_e1(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    real imponibile = m.get(105);
    real aliquota   = m.get(106);
    real imposta    = imponibile * aliquota / 100.0;
    m.set(107, imposta, TRUE);
  }
  return TRUE;
}


bool TQuadroC_E2::ricalcola_imposte_e2(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty())
  {
    TMask& m = f.mask();
    real imponibile = m.get(103);
    real aliquota   = m.get(104);
    real imposta    = imponibile * aliquota / 100.0;
    m.set(105, imposta);
  }
  return TRUE;
}

bool TQuadroC_E2::ritenute_handler_e2(TMask_field& f, KEY key)
{
  if (key == K_TAB && f.focusdirty() || key == K_ENTER)
  {
    TMask& m = f.mask();
    real imponibile = m.get(103);
    real aliquota   = m.get(104);
    real imposta    = imponibile * aliquota / 100.0;
    real imp        = f.get();
    if (imposta != imp)
      f.warning_box("Importo ritenuta non corretto");
  }
  return TRUE;
}


bool TQuadroC_E2::prospetto_e1_notify(TSheet_field& s, int r, KEY k)
{
  bool ok = TRUE;
  if (k == K_INS || k == K_DEL)
    ok = FALSE;
  return ok;
}

void TQuadroC_E2::reset_prospetto_e1()
{
  TSheet_field& s = (TSheet_field&)_msk->field(F_PROSPETTO);
  s.row(0) = "C";
  s.row(1) = "D";
}

void TQuadroC_E2::read_prospetto_e1()
{
  TSheet_field& s = (TSheet_field&)_msk->field(F_PROSPETTO);
  TLocalisamfile prosp(LF_PROSPE1);
  TRectype& curr = prosp.curr();
  curr.put("CODDITTA", _codditta);
  for (int r = 0; r < 2; r++)
  {
    curr.put("CODCAUS", char('C'+r));
    if (prosp.read() == NOERR)
    {
      TToken_string& row = s.row(r);
      row.cut(0);
      row << char('C'+r);
      row.add(curr.get("COMPENSO"));
      row.add(curr.get("IMPONIBILE"));
      row.add(curr.get("RITENUTA"));
    }
  }
}

int TQuadroC_E2::write_prospetto_e1()
{  
  int error = NOERR;
  TSheet_field& s = (TSheet_field&)_msk->field(F_PROSPETTO);
  TLocalisamfile prosp(LF_PROSPE1);
  TRectype& curr = prosp.curr();
  for (int r = 0; r < 2; r++)
  { 
    TToken_string& row = s.row(r);
    real compenso   = row.get(1);
    real imponibile = row.get();
    real ritenuta   = row.get();
    bool empty = compenso.is_zero() && imponibile.is_zero() && ritenuta.is_zero();

    curr.put("CODDITTA", _codditta);
    curr.put("CODCAUS", char('C'+r));
    if (!empty)
    {           
      curr.put("COMPENSO", compenso);
      curr.put("IMPONIBILE", imponibile);
      curr.put("RITENUTA", ritenuta);
      int err = prosp.rewrite();
      if (err != NOERR)
        err = prosp.write();
      if (err != NOERR)
        error = err;
    }  
    else
      prosp.remove();
  }    
  
  return error;
}


int quadriC_E2(int argc, char* argv[])
{
  TString16 taitol;
  taitol << "Quadro ";
  if (strnicmp(argv[2], "DB", 2) == 0)    
    taitol << "D bis";
  else
    taitol << argv[2];
  TQuadroC_E2 a(argv[2]); 
  a.run(argc, argv, taitol);
  return TRUE;
}