Files correlati : Ricompilazione Demo : [ ] Commento : Riportata la versione AGA patch 218 sul main trunk git-svn-id: svn://10.65.10.50/trunk@10087 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			963 lines
		
	
	
		
			25 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			963 lines
		
	
	
		
			25 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include <defmask.h>
 | 
						|
#include <recarray.h>
 | 
						|
#include <relapp.h>
 | 
						|
#include <sheet.h>
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
#include "ce0.h"
 | 
						|
#include "ce2101.h"
 | 
						|
#include "celib.h"
 | 
						|
 | 
						|
#include "ce0500a.h"
 | 
						|
#include "../cg/cglib01.h"
 | 
						|
 | 
						|
 | 
						|
#include "cespi.h"
 | 
						|
#include "salce.h"
 | 
						|
#include "ammce.h"
 | 
						|
 | 
						|
class TQuery_mask : public TAutomask
 | 
						|
{      
 | 
						|
  int _staat;
 | 
						|
 
 | 
						|
protected:
 | 
						|
  virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
 | 
						|
  void on_search_event(TOperable_field& o);
 | 
						|
  virtual void on_firm_change();
 | 
						|
 | 
						|
  int calcola_stato_attivita();
 | 
						|
  bool cespite_ok() const;
 | 
						|
  void set_cespi_filter();
 | 
						|
  
 | 
						|
public:
 | 
						|
   int stato_attivita() const { return _staat; }
 | 
						|
 | 
						|
  TQuery_mask();
 | 
						|
};
 | 
						|
 | 
						|
void TQuery_mask::on_search_event(TOperable_field& o)
 | 
						|
{
 | 
						|
  TToken_string order, fields, header;
 | 
						|
  if (o.dlg() >= F_SEARCH3)
 | 
						|
  {
 | 
						|
    order.add(CESPI_STABILIM); header.add("Stabilimento");
 | 
						|
    order.add(CESPI_REPARTO); header.add("Reparto@10");
 | 
						|
  }
 | 
						|
  if (o.dlg() >= F_SEARCH2) 
 | 
						|
  {
 | 
						|
    order.add(CESPI_CODIMP); header.add("Impianto@10");
 | 
						|
  }
 | 
						|
  if (o.dlg() >= F_SEARCH1)
 | 
						|
  {
 | 
						|
    order.add(CESPI_CODCAT); header.add("Cat");
 | 
						|
    order.add(CESPI_DTCOMP); header.add("Data Acq.@10");
 | 
						|
  }
 | 
						|
 | 
						|
  order.add(CESPI_IDCESPITE); header.add("Codice@10");
 | 
						|
  
 | 
						|
  fields = order;
 | 
						|
  fields.add(CESPI_DESC); header.add("Descrizione@50");
 | 
						|
  
 | 
						|
  TRelation rel(LF_CESPI);
 | 
						|
  
 | 
						|
  TString filter;                             
 | 
						|
  if (get(F_SELECT)[0] == 'I') // Filtro per impianto
 | 
						|
  {
 | 
						|
    const TString& imp = get(F_IMPIANTO);
 | 
						|
    if (!imp.empty())
 | 
						|
      filter << CESPI_CODIMP << "=\"" << imp << '"';
 | 
						|
  }
 | 
						|
  else // Filtro per attività
 | 
						|
  {
 | 
						|
    filter << "STR(" << CESPI_CODCGRA << "=" << get(F_GRUPPO) << ')';
 | 
						|
    filter << "&&(" << CESPI_CODSPA << "=\"" << get(F_SPECIE) << "\")";
 | 
						|
  }  
 | 
						|
  
 | 
						|
  TSorted_cursor cur(&rel, order, filter);
 | 
						|
  TCursor_sheet sht(&cur, fields, "Cespiti", header, 0, 1);
 | 
						|
  if (sht.run() == K_ENTER)
 | 
						|
  { 
 | 
						|
    TToken_string& row = sht.row();
 | 
						|
    const int cod_pos = row.items()-2;
 | 
						|
    set(F_IDCESPITE, row.get(cod_pos), TRUE);
 | 
						|
    stop_run(K_AUTO_ENTER);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
int TQuery_mask::calcola_stato_attivita()
 | 
						|
{
 | 
						|
  const int ese = get_int(F_ESERCIZIO);
 | 
						|
  const int gru = get_int(F_GRUPPO);
 | 
						|
  const char* spe = get(F_SPECIE);
 | 
						|
 | 
						|
  TString16 str;  
 | 
						|
  str.format("%04d%02d%-4s", ese, gru, spe);
 | 
						|
 | 
						|
  const TRectype& curr_ccb = cache().get("CCB", str);
 | 
						|
  if (curr_ccb.get_bool("B1")) // Bollato stampato
 | 
						|
    _staat = 3;
 | 
						|
  else
 | 
						|
  {  
 | 
						|
    TEsercizi_contabili esc;
 | 
						|
    str.format("%04d%02d%-4s", esc.pred(ese), gru, spe);
 | 
						|
    const TRectype& prev_ccb = cache().get("CCB", str);
 | 
						|
    _staat = prev_ccb.get_bool("B1") ? 2 : 1;
 | 
						|
  }
 | 
						|
  
 | 
						|
  TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
  dc.set_attivita(ese, gru, spe);
 | 
						|
  
 | 
						|
  return _staat;
 | 
						|
}
 | 
						|
 | 
						|
bool TQuery_mask::cespite_ok() const
 | 
						|
{
 | 
						|
  TLocalisamfile cespi(LF_CESPI);
 | 
						|
  cespi.put(CESPI_IDCESPITE, get(F_IDCESPITE));
 | 
						|
  bool ok = cespi.read() == NOERR;
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
void TQuery_mask::set_cespi_filter()
 | 
						|
{ 
 | 
						|
  TString filter;
 | 
						|
  if (get(F_SELECT)[0] == 'I') // Filtro per impianto
 | 
						|
  {
 | 
						|
    const TString& imp = get(F_IMPIANTO);
 | 
						|
    if (!imp.empty())
 | 
						|
      filter << CESPI_CODIMP << "=\"" << imp << '"';
 | 
						|
  }
 | 
						|
  else // Filtro per attività
 | 
						|
  {
 | 
						|
    filter << "STR(" << CESPI_CODCGRA << "=" << get(F_GRUPPO) << ')';
 | 
						|
    filter << "&&(" << CESPI_CODSPA << "=\"" << get(F_SPECIE) << "\")";
 | 
						|
  }  
 | 
						|
  efield(F_IDCESPITE).browse()->set_filter(filter);
 | 
						|
  efield(F_DESC).browse()->set_filter(filter);
 | 
						|
}
 | 
						|
 | 
						|
bool TQuery_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
 | 
						|
{
 | 
						|
  switch (o.dlg())
 | 
						|
  {         
 | 
						|
  case F_ESERCIZIO:
 | 
						|
  case F_GRUPPO:
 | 
						|
  case F_SPECIE:
 | 
						|
    if (e == fe_init || e == fe_modify)
 | 
						|
    {                                    
 | 
						|
      const bool can_create = calcola_stato_attivita() != 3; // Bollato non stampato
 | 
						|
      enable(DLG_NEWREC, can_create && !field(F_SPECIE).empty() && !field(F_CATEGORIA).empty());
 | 
						|
      enable(DLG_DELREC, can_create);
 | 
						|
      set_cespi_filter();
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_SELECT:
 | 
						|
  case F_IMPIANTO:  
 | 
						|
    if (e == fe_modify)
 | 
						|
      set_cespi_filter();
 | 
						|
    break;
 | 
						|
  case F_SEARCH1:
 | 
						|
  case F_SEARCH2:
 | 
						|
  case F_SEARCH3:
 | 
						|
    if (e == fe_button)
 | 
						|
      on_search_event(o);
 | 
						|
    break;  
 | 
						|
  case F_CATEGORIA:
 | 
						|
  case F_DESC_CAT:
 | 
						|
    {
 | 
						|
      TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
      bool ok = dc.on_category_event(o, e, jolly);
 | 
						|
      if (ok)
 | 
						|
      { 
 | 
						|
        if (e == fe_modify && !o.empty() && stato_attivita() == 2)
 | 
						|
        {
 | 
						|
          const TRectype& cac = dc.categoria(0, NULL, get_int(F_CATEGORIA));
 | 
						|
          const int fine_validita = cac.get_date("D1").year();
 | 
						|
          if (fine_validita > 0 && fine_validita < get_int(F_ESERCIZIO))
 | 
						|
            return error_box("Categoria scaduta nell'esercizio %d", fine_validita);
 | 
						|
        }
 | 
						|
        if (e == fe_init || e == fe_modify || e == fe_button)
 | 
						|
        {
 | 
						|
          const bool bollati = stato_attivita() == 3;
 | 
						|
          enable(DLG_NEWREC, !bollati && !o.empty() && !field(F_SPECIE).empty());
 | 
						|
        }
 | 
						|
        if (e == fe_close && o.empty() && !cespite_ok())
 | 
						|
          return error_box("È necessario specificare la categoria del nuovo cespite");
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_IDCESPITE:
 | 
						|
    if (e == fe_close)  
 | 
						|
    { 
 | 
						|
      if (stato_attivita() == 3 && !cespite_ok())
 | 
						|
        return error_box("E' stato stampato il bollato dell'anno:\nnon sono permessi inserimenti");
 | 
						|
    }
 | 
						|
    if (e == fe_modify && !o.empty() && cespite_ok())
 | 
						|
    {
 | 
						|
      TLocalisamfile salce(LF_SALCE);
 | 
						|
      salce.put(SALCE_IDCESPITE, get(F_IDCESPITE));
 | 
						|
      salce.put(SALCE_CODES, get(F_ESERCIZIO));
 | 
						|
      salce.put(SALCE_TPSALDO, 1);
 | 
						|
      if (salce.read() != NOERR)
 | 
						|
        return yesno_box("Non esistono saldi per l'anno selezionato:\nSi desidera continuare ugualmente?");
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  default:
 | 
						|
    break;
 | 
						|
  }  
 | 
						|
  return TRUE;
 | 
						|
}
 | 
						|
 | 
						|
void TQuery_mask::on_firm_change()
 | 
						|
{
 | 
						|
  TAutomask::on_firm_change();
 | 
						|
  ditta_cespiti().init_mask(*this);
 | 
						|
}
 | 
						|
 | 
						|
TQuery_mask::TQuery_mask() : TAutomask("ce0500a") 
 | 
						|
{
 | 
						|
  first_focus(F_IDCESPITE);
 | 
						|
}
 | 
						|
 | 
						|
class TEdit_mask : public TAutomask
 | 
						|
{ 
 | 
						|
  TTipo_cespite _tipo;
 | 
						|
  int _staat;
 | 
						|
 | 
						|
protected:
 | 
						|
  virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
 | 
						|
  virtual bool on_key(KEY k);
 | 
						|
  
 | 
						|
  bool one_compiled(const short* f) const;
 | 
						|
  TCurrency sum_fields(const short* f) const;
 | 
						|
  bool test_ammissibilita_dati();
 | 
						|
  bool test_ammissibilita_saldi();
 | 
						|
  bool test_ammissibilita_fondi();
 | 
						|
 | 
						|
public:
 | 
						|
  void set_stato_attivita(int sa) { _staat = sa; }
 | 
						|
  int stato_attivita() const { return _staat; }
 | 
						|
 | 
						|
  void set_tipo_cespite(TTipo_cespite tc) { _tipo = tc; }
 | 
						|
  TTipo_cespite tipo_cespite() const { return _tipo; }
 | 
						|
 | 
						|
  void protect_page(int page, TToken_string& enabling);
 | 
						|
 | 
						|
  TEdit_mask() : TAutomask("ce0500b") { }
 | 
						|
};
 | 
						|
 | 
						|
bool TEdit_mask::one_compiled(const short* f) const
 | 
						|
{
 | 
						|
  for (int i = 0; f[i]; i++)
 | 
						|
  {
 | 
						|
    if (!field(f[i]).empty())
 | 
						|
      break;
 | 
						|
  }
 | 
						|
  return f[i] > 0;
 | 
						|
}
 | 
						|
 | 
						|
TCurrency TEdit_mask::sum_fields(const short* f) const
 | 
						|
{
 | 
						|
  TCurrency sum, val;
 | 
						|
  for (int i = 0; f[i] != 0; i++)
 | 
						|
  {
 | 
						|
    get_currency(abs(f[i]), val);
 | 
						|
    if (f[i] > 0)
 | 
						|
      sum += val;
 | 
						|
    else
 | 
						|
      sum -= val;  
 | 
						|
  }
 | 
						|
  return sum;
 | 
						|
}
 | 
						|
 | 
						|
bool TEdit_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
 | 
						|
{ 
 | 
						|
  bool ok = TRUE;
 | 
						|
  switch (o.dlg())
 | 
						|
  { 
 | 
						|
  case F_CATEGORIA:
 | 
						|
    if (e == fe_init)
 | 
						|
    {
 | 
						|
      TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
      dc.on_category_event(o, e, jolly);
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_DTCOMP:
 | 
						|
    if (e == fe_modify || e == fe_close)
 | 
						|
    {
 | 
						|
      const TDate dtcomp = o.get();
 | 
						|
      TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
      TDate iniz, fine; 
 | 
						|
      const int ese = dc.esercizio_corrente(iniz, fine);
 | 
						|
      
 | 
						|
      if (stato_attivita() == 2)
 | 
						|
      {
 | 
						|
        if (dtcomp < iniz)
 | 
						|
          return error_box("La data appartiene ad un esercizio già stampato su bollato");
 | 
						|
        const TRectype& cac = dc.categoria(0, NULL, get_int(F_CATEGORIA));
 | 
						|
        iniz = cac.get("D0");
 | 
						|
        if (iniz.ok() && dtcomp < iniz)
 | 
						|
          return error_box("La data è precedente all'inizio della validità della categoria");
 | 
						|
        fine = cac.get("D1");
 | 
						|
        if (fine.ok() && dtcomp > fine)
 | 
						|
          return error_box("La data è succesiva alla fine della validità della categoria");
 | 
						|
      }
 | 
						|
      if (dc.esercizio_costituzione() && dtcomp < iniz)
 | 
						|
        return error_box("Non è possibile specificare una data antecedente all'esercizio di costituzione");
 | 
						|
      
 | 
						|
      if (tipo_cespite() != tc_materiale && dtcomp.year() >= dc.anno_tuir())  
 | 
						|
      {
 | 
						|
        if (field(F_DTFUNZ).empty())
 | 
						|
          set(F_DTFUNZ, o.get());
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_DTFUNZ:
 | 
						|
    if (e == fe_modify || e == fe_close)
 | 
						|
    {
 | 
						|
      const TDate dtfunz = o.get();      
 | 
						|
      if (dtfunz.ok())
 | 
						|
      {
 | 
						|
        const TDate dtcomp = field(F_DTCOMP).get();
 | 
						|
        if (dtfunz < dtcomp)
 | 
						|
          return error_box("La data di entrata in funzione deve essere successiva a quella di aquisizione");
 | 
						|
        if (!dtcomp.ok())                                                                    
 | 
						|
          return error_box("La data di entrata in funzione non puo' essere inserita senza specificare quella di aquisizione");      
 | 
						|
        if (stato_attivita() == 2)  
 | 
						|
        {
 | 
						|
          const TDate iniz = get(F_INIZIO_ES); 
 | 
						|
          if (dtfunz < iniz)
 | 
						|
            return error_box("La data di entrata in funzione deve essere successiva a quella di inizio esercizio");
 | 
						|
        }
 | 
						|
 | 
						|
        TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
        if (_tipo != tc_materiale && dtfunz != dtcomp)  
 | 
						|
        {
 | 
						|
          if (dtcomp.year() >= dc.anno_tuir())
 | 
						|
            return error_box("In base alla normativa del TUIR, le date di acquisizione e di entrata in funzione devono coincidere");
 | 
						|
        }
 | 
						|
        const TRectype& cac = dc.categoria(0, NULL, get_int(F_CATEGORIA));
 | 
						|
        const TDate iniz = cac.get("D0");
 | 
						|
        if (iniz.ok() && dtfunz < iniz)
 | 
						|
          return error_box("La data è precedente all'inizio validità della categoria");
 | 
						|
        const TDate fine = cac.get("D1");
 | 
						|
        if (fine.ok() && dtfunz > fine)
 | 
						|
          return error_box("La data è successiva alla fine validità della categoria");
 | 
						|
        if (dtfunz.year() >= dc.anno_tuir())
 | 
						|
        {
 | 
						|
          set(F_TUIR, "X");
 | 
						|
          set(F_AMMPROP, "X");
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
          reset(F_TUIR);
 | 
						|
          reset(F_AMMPROP);
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_QUADRATURA:
 | 
						|
    if (e == fe_button)
 | 
						|
    {
 | 
						|
      set(F_NORMALE2, get(F_NORMALE));
 | 
						|
      set(F_NORMALE3, get(F_NORMALE));
 | 
						|
      set(F_ACCELERATO2, get(F_ACCELERATO));
 | 
						|
      set(F_ACCELERATO3, get(F_ACCELERATO));
 | 
						|
      set(F_ANTICIPATO2, get(F_ANTICIPATO));
 | 
						|
      set(F_ANTICIPATO3, get(F_ANTICIPATO));
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_VNONAMM:
 | 
						|
    if (e == fe_modify || e == fe_close)
 | 
						|
    {
 | 
						|
      if (o.empty() && get_bool(F_LEASING))
 | 
						|
        return error_box("Indicare il valore del riscatto per beni in leasing");
 | 
						|
      test_ammissibilita_saldi();
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_PLUSREIN:
 | 
						|
    if ((e == fe_modify || e == fe_close) && !o.empty())
 | 
						|
    {
 | 
						|
      TCurrency costo; get_currency(F_COSTO, costo);
 | 
						|
      TCurrency noamm; get_currency(F_VNONAMM, noamm);
 | 
						|
      TCurrency plusr; get_currency(F_PLUSREIN, plusr);
 | 
						|
      const TCurrency minim = costo - noamm;
 | 
						|
      if (plusr > minim)
 | 
						|
        return error_box("La plusvalenza reinvestita non puo' superare %s", minim.string(TRUE));
 | 
						|
    }
 | 
						|
    break;  
 | 
						|
  case F_ELEMENTI:
 | 
						|
    if ((e == fe_modify || e == fe_close) &&  o.empty())
 | 
						|
    {
 | 
						|
      const TDate dtcomp = field(F_DTCOMP).get();
 | 
						|
      const TDate dtiniz = field(F_INIZIO_ES).get();
 | 
						|
      if (dtcomp.ok() && dtcomp < dtiniz)
 | 
						|
        return error_box("E' necessario specificare il numero di elementi per cespiti acquisiti negli esercizi precedenti");
 | 
						|
      const short fields[] = { F_COSTO, F_VNONAMM, F_RIV75, F_RIV83, F_RIV90, F_RIV91, F_RIVGF, F_RIVGC, 0 };
 | 
						|
      if (one_compiled(fields))
 | 
						|
        return error_box("E' necessario inserire il nunero degli elementi");
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_RIV90:  
 | 
						|
  case F_RIV91:  
 | 
						|
    if ((e == fe_init || e == fe_modify) && o.active())
 | 
						|
    {
 | 
						|
      TEdit_field& anni = efield(o.dlg()+1);
 | 
						|
      if (o.empty())
 | 
						|
      {
 | 
						|
        anni.reset();
 | 
						|
        anni.disable();
 | 
						|
      }
 | 
						|
      else
 | 
						|
        anni.enable();
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_ANNIAMM:
 | 
						|
    if (e == fe_init || e == fe_modify)
 | 
						|
      enable_page(2, !o.empty());
 | 
						|
    break;
 | 
						|
  case F_NORMALE:
 | 
						|
    if (e == fe_close)
 | 
						|
    { 
 | 
						|
      const short fv[] = { F_COSTO, -F_VNONAMM, F_RIV75, F_RIV83, F_RIV90, F_RIV91, F_RIVGF, 0 };
 | 
						|
      const short fa[] = { F_NORMALE, F_ACCELERATO, F_ANTICIPATO, F_QPERSE, F_FPRIVATO, F_QPERSEPRIV, 0 };
 | 
						|
      const TCurrency val_amm = sum_fields(fv);
 | 
						|
      const TCurrency fon_amm = sum_fields(fa);
 | 
						|
      if (fon_amm > val_amm)
 | 
						|
      {
 | 
						|
        TString msg;
 | 
						|
        msg << "Il fondo ammortamento fiscale (" << fon_amm.string(TRUE) << ')';
 | 
						|
        msg << "non puo' superare il valore da ammortizzare (" << val_amm.string(TRUE) << ')';
 | 
						|
        return error_box(msg);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;  
 | 
						|
  case F_FPRIVATO:
 | 
						|
    if (e == fe_close)
 | 
						|
    {
 | 
						|
      if (get_int(F_USOPROM) > 1 && !field(F_ANNIAMM).empty())
 | 
						|
      {
 | 
						|
        if (field(F_FPRIVATO).empty() && field(F_QPERSEPRIV).empty())
 | 
						|
          return error_box("E' necessario inserire un fondo di ammortamento privato o delle quote perse private");
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case F_NORMALE2:
 | 
						|
    if (e == fe_close)
 | 
						|
    { 
 | 
						|
      const short fv[] = { F_COSTO, F_RIV75, F_RIV83, F_RIV90, F_RIV91, F_RIVGC, 0 };
 | 
						|
      const short fa[] = { F_NORMALE2, F_ACCELERATO2, F_ANTICIPATO2, 0 };
 | 
						|
      const TCurrency val_amm = sum_fields(fv);
 | 
						|
      const TCurrency fon_amm = sum_fields(fa);
 | 
						|
      if (fon_amm > val_amm)
 | 
						|
      {
 | 
						|
        TString msg;
 | 
						|
        msg << "Il fondo ammortamento civilistico (" << fon_amm.string(TRUE) << ')';
 | 
						|
        msg << "non puo' superare il valore da ammortizzare (" << val_amm.string(TRUE) << ')';
 | 
						|
        return error_box(msg);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;  
 | 
						|
  default: 
 | 
						|
    break;
 | 
						|
  }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
// Disabilita tutti i campi della pagina "page" (1 based) tranne quelli nella lista "enabling"
 | 
						|
void TEdit_mask::protect_page(int page, TToken_string& enabling)
 | 
						|
{               
 | 
						|
  // Determina l'id del primo campo della pagina
 | 
						|
  short cid = 0;
 | 
						|
  switch (page)
 | 
						|
  {
 | 
						|
  case  2: cid = F_ELEMENTI; break;
 | 
						|
  case  3: cid = F_NORMALE; break;
 | 
						|
  default: cid = F_IDCESPITE; break;
 | 
						|
  } 
 | 
						|
  // Determina l'handle della pagina selezionata
 | 
						|
  WINDOW parent = field(cid).parent();
 | 
						|
 | 
						|
  for (int f = fields()-1; f >= 0; f--)
 | 
						|
  {
 | 
						|
    TMask_field& c = fld(f);
 | 
						|
    if (c.parent() == parent) // Il campo appartiene alla pagina voluta?
 | 
						|
    {
 | 
						|
      const short id = c.dlg();
 | 
						|
      if (id > 100 && id < 1000 && c.is_operable() && c.enabled_default())
 | 
						|
      {
 | 
						|
        const bool on = enabling.empty() || enabling.get_pos(c.dlg()) >= 0;
 | 
						|
        c.enable(on);
 | 
						|
        if (cid == id) // Ho raggiunto il capo pagina?
 | 
						|
          break;
 | 
						|
      }
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
bool TEdit_mask::test_ammissibilita_dati()
 | 
						|
{
 | 
						|
  const char* msg = insert_mode() ? "Inserimento" : "Modifica";
 | 
						|
  xvt_statbar_set(msg, TRUE);
 | 
						|
  return FALSE;
 | 
						|
}
 | 
						|
 | 
						|
bool TEdit_mask::test_ammissibilita_saldi()
 | 
						|
{
 | 
						|
  bool protez = FALSE;
 | 
						|
  const char* msg = "";
 | 
						|
  switch (_staat)
 | 
						|
  {
 | 
						|
  case 1:
 | 
						|
  case 2:
 | 
						|
    {
 | 
						|
      const TDate iniz = get(F_INIZIO_ES);
 | 
						|
      const TDate dtacq = get(F_DTCOMP);
 | 
						|
      const TDate dtfunz = get(F_DTFUNZ);
 | 
						|
      if (!dtacq.ok() || !dtfunz.ok())
 | 
						|
        protez = TRUE;
 | 
						|
      else
 | 
						|
        protez = dtacq >= iniz || dtfunz >= iniz;
 | 
						|
      if (protez)
 | 
						|
        msg = "Inserire gli importi nel movimento d'acquisto";
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case 3: 
 | 
						|
    protez = TRUE; 
 | 
						|
    msg = "Bollato stampato: non sono ammesse modifiche";
 | 
						|
    break;
 | 
						|
  default: 
 | 
						|
    break;
 | 
						|
  }
 | 
						|
 | 
						|
  TToken_string enabling;
 | 
						|
  if (protez)   
 | 
						|
    enabling = "883";  // Disabilita tutto!
 | 
						|
  protect_page(2, enabling);
 | 
						|
  
 | 
						|
  if (*msg)
 | 
						|
    beep(1);
 | 
						|
  else
 | 
						|
    msg = insert_mode() ? "Inserimento" : "Modifica";
 | 
						|
  xvt_statbar_set(msg, TRUE);
 | 
						|
                                 
 | 
						|
  return protez;
 | 
						|
}
 | 
						|
 | 
						|
bool TEdit_mask::test_ammissibilita_fondi()
 | 
						|
{
 | 
						|
  bool protez = FALSE;
 | 
						|
  const char* msg = "";
 | 
						|
  switch (_staat)
 | 
						|
  {
 | 
						|
  case 1:
 | 
						|
  case 2:
 | 
						|
    {
 | 
						|
      const TDate dtacq = get(F_DTCOMP);
 | 
						|
      const TDate dtfunz = get(F_DTFUNZ);
 | 
						|
      if (!dtacq.ok() || !dtfunz.ok())
 | 
						|
        protez = TRUE;
 | 
						|
      else
 | 
						|
      {  
 | 
						|
        const TDate iniz = get(F_INIZIO_ES);
 | 
						|
        protez = dtacq >= iniz;
 | 
						|
      }    
 | 
						|
      if (protez)
 | 
						|
        msg = "Inserire gli importi nel movimento d'acquisto";
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case 3: 
 | 
						|
    protez = TRUE; 
 | 
						|
    msg = "Bollato stampato: non sono ammesse modifiche";
 | 
						|
    break;
 | 
						|
  default: protez = FALSE; break;
 | 
						|
  }
 | 
						|
 | 
						|
  TToken_string enabling;
 | 
						|
  if (protez)   
 | 
						|
    enabling = "883";  // Disabilita tutto!
 | 
						|
  protect_page(3, enabling);
 | 
						|
 | 
						|
  if (!protez)
 | 
						|
  {
 | 
						|
    const TDate dtfunz = get(F_DTFUNZ);
 | 
						|
    const TDate iniz = get(F_INIZIO_ES);
 | 
						|
    bool prot9 = FALSE;
 | 
						|
    if (!dtfunz.ok() || dtfunz >= iniz)
 | 
						|
    {
 | 
						|
      msg = "Cespite non ancora entrato in funzione a inizio esercizio";
 | 
						|
      prot9 = TRUE;
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      const TRectype& cac = ditta_cespiti().categoria(0, NULL, get_int(F_CATEGORIA));
 | 
						|
      if (cac.get_bool("B0"))
 | 
						|
      {
 | 
						|
        msg = "Categoria non ammortizzabile";
 | 
						|
        prot9 = TRUE;
 | 
						|
      }
 | 
						|
    }
 | 
						|
    enable(-9, !prot9);
 | 
						|
  }
 | 
						|
  if (*msg)
 | 
						|
    beep(1);
 | 
						|
  else
 | 
						|
    msg = insert_mode() ? "Inserimento" : "Modifica";
 | 
						|
  xvt_statbar_set(msg, TRUE);
 | 
						|
 | 
						|
  return protez;
 | 
						|
}
 | 
						|
 | 
						|
bool TEdit_mask::on_key(KEY k)
 | 
						|
{   
 | 
						|
  // Try to predict next page!
 | 
						|
  const int old_page = curr_page()+1;
 | 
						|
  int new_page = old_page;
 | 
						|
  switch (k)
 | 
						|
  {
 | 
						|
  case K_CTRL+K_F1: new_page = 1; break;
 | 
						|
  case K_CTRL+K_F2: new_page = 2; break;
 | 
						|
  case K_CTRL+K_F3: new_page = 3; break;
 | 
						|
  case K_PREV     : new_page--; break;
 | 
						|
  case K_NEXT     : new_page++; break;
 | 
						|
  default: break;
 | 
						|
  }
 | 
						|
  
 | 
						|
  // If page will change ...
 | 
						|
  if (old_page != new_page) switch(new_page)
 | 
						|
  {
 | 
						|
  case 1: test_ammissibilita_dati(); break;
 | 
						|
  case 2: test_ammissibilita_saldi(); break;
 | 
						|
  case 3: test_ammissibilita_fondi(); break;
 | 
						|
  default: break;
 | 
						|
  }
 | 
						|
 | 
						|
  return TAutomask::on_key(k);
 | 
						|
}
 | 
						|
 | 
						|
class TAnacespi : public TRelation_application
 | 
						|
{
 | 
						|
  TRelation* _cespiti;
 | 
						|
  int _rel_year;
 | 
						|
  
 | 
						|
  TQuery_mask* _qmask;
 | 
						|
  TEdit_mask* _emask;
 | 
						|
  
 | 
						|
private:  
 | 
						|
  void init_mask(TMask& m);
 | 
						|
  void kill_cespite(const TString& idcespite, int lfile, int key = 1);
 | 
						|
 | 
						|
protected:
 | 
						|
  // @cmember Inizializzazione dei dati dell'utente
 | 
						|
  virtual bool user_create();
 | 
						|
  // @cmember Distruzione dei dati dell'utente
 | 
						|
  virtual bool user_destroy();
 | 
						|
  virtual bool changing_mask(int mode);
 | 
						|
  // @cmember Richiede la maschera da usare
 | 
						|
  virtual TMask* get_mask(int mode);
 | 
						|
  // @cmember Ritorna la relazione da modificare
 | 
						|
  virtual TRelation* get_relation() const;
 | 
						|
 | 
						|
  virtual const char* get_next_key();
 | 
						|
  virtual bool protected_record(TRectype& rec);
 | 
						|
  virtual void init_query_mode(TMask& m);
 | 
						|
  virtual void init_insert_mode(TMask& m);
 | 
						|
  virtual void init_modify_mode(TMask& m);
 | 
						|
  
 | 
						|
  virtual int read(TMask& m);
 | 
						|
  virtual int write(const TMask& m);
 | 
						|
  virtual bool remove();
 | 
						|
};
 | 
						|
 | 
						|
bool TAnacespi::changing_mask(int)
 | 
						|
{
 | 
						|
  return TRUE;
 | 
						|
}
 | 
						|
 | 
						|
TMask* TAnacespi::get_mask(int mode)
 | 
						|
{
 | 
						|
  return mode == MODE_QUERY ? (TMask*)_qmask : (TMask*)_emask;
 | 
						|
}
 | 
						|
 | 
						|
// get_relation più complessa della storia di campo
 | 
						|
TRelation* TAnacespi::get_relation() const
 | 
						|
{                    
 | 
						|
  const int year = _qmask->get_int(F_ESERCIZIO);
 | 
						|
  TRelation*& csp = ((TAnacespi*)this)->_cespiti;
 | 
						|
  if (year != _rel_year && csp != NULL)
 | 
						|
  {
 | 
						|
    delete csp;
 | 
						|
    csp = NULL;
 | 
						|
  }
 | 
						|
  if (csp == NULL)
 | 
						|
  { 
 | 
						|
    // Crea nuova relazione su cespi
 | 
						|
    csp = new TRelation(LF_CESPI);
 | 
						|
    ((TAnacespi*)this)->_rel_year = year; // Memorizza anno utilizzato
 | 
						|
    
 | 
						|
    // Collega salce
 | 
						|
    TString80 expr1;
 | 
						|
    expr1 << SALCE_IDCESPITE << "==" << CESPI_IDCESPITE;
 | 
						|
    expr1 << '|' << SALCE_CODES << "==" << year;
 | 
						|
    expr1 << '|' << SALCE_TPSALDO << "==1";
 | 
						|
    csp->add(LF_SALCE, expr1);    
 | 
						|
    
 | 
						|
    // Collega i tre tipi di ammce
 | 
						|
    for (int a = 1; a <= 3; a++) 
 | 
						|
    {
 | 
						|
      TString80 expr2 = expr1;
 | 
						|
      expr2 << '|' << AMMCE_TPAMM << "==" << a;
 | 
						|
      csp->add(LF_AMMCE, expr2, 1, 0, a-1); 
 | 
						|
    }
 | 
						|
    
 | 
						|
    // Attiva il salvataggio anche di salce e ammce
 | 
						|
    csp->write_enable(); 
 | 
						|
  }
 | 
						|
  
 | 
						|
  return _cespiti;
 | 
						|
}
 | 
						|
 | 
						|
void TAnacespi::init_query_mode(TMask& m)
 | 
						|
{
 | 
						|
  ditta_cespiti().init_mask(m);
 | 
						|
  set_search_field(F_IDCESPITE);
 | 
						|
}
 | 
						|
 | 
						|
bool TAnacespi::protected_record(TRectype& rec)
 | 
						|
{
 | 
						|
  const TDate dtalien = rec.get(CESPI_DTALIEN);
 | 
						|
  return dtalien.ok();
 | 
						|
}
 | 
						|
 | 
						|
void TAnacespi::init_mask(TMask& m)
 | 
						|
{
 | 
						|
  TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
  dc.init_mask(m);
 | 
						|
  const bool can_edit = !dc.bollato_stampato() && m.field(F_DTALIEN).empty();
 | 
						|
  m.enable(DLG_SAVEREC, can_edit);
 | 
						|
  m.enable(DLG_DELREC, can_edit && m.edit_mode());
 | 
						|
  m.disable(DLG_NEWREC);
 | 
						|
  
 | 
						|
  _emask->set_stato_attivita(_qmask->stato_attivita());
 | 
						|
  
 | 
						|
  const TRectype& cac = dc.categoria(0, NULL, _qmask->get_int(F_CATEGORIA));
 | 
						|
  const int tc = cac.get_int("I0");
 | 
						|
  _emask->set_tipo_cespite(tc==0 ? tc_materiale : (tc==1 ? tc_immateriale : tc_pluriennale));
 | 
						|
  
 | 
						|
  const bool mat_only = _emask->tipo_cespite() == tc_materiale;
 | 
						|
  m.show(F_LEASING, mat_only);
 | 
						|
  m.show(F_ANNIRIC, mat_only);
 | 
						|
  m.show(F_USATO, mat_only);
 | 
						|
}
 | 
						|
 | 
						|
void TAnacespi::init_insert_mode(TMask& m)
 | 
						|
{                
 | 
						|
  init_mask(m);
 | 
						|
  m.set(F_TUIR, "X");
 | 
						|
  if (_emask->tipo_cespite() == tc_materiale)
 | 
						|
    m.set(F_SPEMAN, 2);
 | 
						|
  else
 | 
						|
  {
 | 
						|
    m.set(F_SPEMAN, 1);
 | 
						|
    m.disable(F_SPEMAN);
 | 
						|
  }
 | 
						|
  m.set(F_ESCLPR, _emask->tipo_cespite() != tc_pluriennale ? "X" : "");
 | 
						|
  // Extra: non richiesti da analisi, ma obbligatori
 | 
						|
  m.set(F_VEICOLO, 1); 
 | 
						|
  m.set(F_USOPROM, 1);
 | 
						|
}
 | 
						|
 | 
						|
void TAnacespi::init_modify_mode(TMask& m)
 | 
						|
{                
 | 
						|
  init_mask(m);
 | 
						|
  
 | 
						|
  TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
  TDate iniz, fine;
 | 
						|
  const int ese = dc.esercizio_corrente(iniz, fine);
 | 
						|
  const TDate dtacq = m.get(F_DTCOMP);
 | 
						|
  const TDate dtfunz = m.get(F_DTFUNZ);
 | 
						|
  const int staat = _qmask->stato_attivita();
 | 
						|
  
 | 
						|
  TToken_string enabling;
 | 
						|
  switch (staat)
 | 
						|
  {
 | 
						|
  case 2:
 | 
						|
    if (dtacq < iniz)
 | 
						|
    {
 | 
						|
      if (!dtfunz.ok() || dtfunz >= iniz)
 | 
						|
      {
 | 
						|
        enabling.add(F_DTFUNZ);
 | 
						|
        enabling.add(F_USOPROM);
 | 
						|
        enabling.add(F_VEIDIP);
 | 
						|
        enabling.add(F_VEICOLO);
 | 
						|
        if (_emask->tipo_cespite() == tc_materiale)
 | 
						|
          enabling.add(F_SPEMAN);
 | 
						|
      }
 | 
						|
      else
 | 
						|
      {
 | 
						|
        enabling.add(F_USOPROM);
 | 
						|
        enabling.add(F_VEIDIP);
 | 
						|
        enabling.add(F_VEICOLO);
 | 
						|
        if (_emask->tipo_cespite() == tc_materiale)
 | 
						|
          enabling.add(F_SPEMAN);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case 3:
 | 
						|
    if (!dtfunz.ok() || dtfunz >= iniz)
 | 
						|
    {
 | 
						|
      enabling.add(F_DTFUNZ);
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  default: 
 | 
						|
    break;
 | 
						|
  }
 | 
						|
  
 | 
						|
  _emask->protect_page(1, enabling);
 | 
						|
 | 
						|
  enabling.cut(0);
 | 
						|
  if (staat == 2 || staat == 3)
 | 
						|
    _emask->protect_page(2, enabling);
 | 
						|
 | 
						|
  if (!m.field(F_DTALIEN).empty())
 | 
						|
    xvt_statbar_set("Cespite alienato: non è possibile apportare modifiche", TRUE);
 | 
						|
}
 | 
						|
 | 
						|
const char* TAnacespi::get_next_key()
 | 
						|
{
 | 
						|
  real num = 1;
 | 
						|
  TLocalisamfile cespi(LF_CESPI);
 | 
						|
  if (cespi.last() == NOERR)
 | 
						|
    num = cespi.get_real(CESPI_IDCESPITE) + 1;
 | 
						|
  return format("%d|%s", F_IDCESPITE, num.string());
 | 
						|
}
 | 
						|
 | 
						|
int TAnacespi::read(TMask& m)
 | 
						|
{
 | 
						|
  int err = TRelation_application::read(m);
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
int TAnacespi::write(const TMask& m)
 | 
						|
{    
 | 
						|
  TRectype& curr = get_relation()->curr();
 | 
						|
  const TDitta_cespiti& dc = ditta_cespiti();
 | 
						|
  const TRectype& cac = dc.categoria(0, NULL, m.get_int(F_CATEGORIA));
 | 
						|
  switch (_emask->tipo_cespite())
 | 
						|
  {
 | 
						|
  case tc_immateriale:
 | 
						|
    if (cac.get_int("I1") == 2) // Percentuali
 | 
						|
    {
 | 
						|
      curr.put(CESPI_PIMM, cac.get("R13")); 
 | 
						|
      curr.put(CESPI_VINCOLO, 1);
 | 
						|
    }
 | 
						|
    else  // Anni
 | 
						|
    {
 | 
						|
      curr.zero(CESPI_PIMM); 
 | 
						|
      curr.put(CESPI_ANNIRIC, cac.get("I3"));
 | 
						|
      curr.put(CESPI_VINCOLO, 2);
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  case tc_pluriennale:
 | 
						|
    curr.put(CESPI_VINCOLO, cac.get("I2"));
 | 
						|
    curr.put(CESPI_PMINP, cac.get("R14"));
 | 
						|
    curr.put(CESPI_PMAXP, cac.get("R15"));
 | 
						|
    curr.put(CESPI_ANNIRIC, cac.get("I3"));
 | 
						|
    break;
 | 
						|
  default: 
 | 
						|
    {
 | 
						|
      TDate dt = m.get(F_DTFUNZ);
 | 
						|
      if (!dt.ok())
 | 
						|
        dt = m.get_date(F_INIZIO_ES);         
 | 
						|
      // % beni mat D.M. 29/10/74 oppure D.M. 31/12/88
 | 
						|
      const TString& pmat = cac.get(dt.year() < 1989 ? "R12" : "R11");
 | 
						|
      curr.put(CESPI_PMAT, pmat);
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  }
 | 
						|
 | 
						|
  const int cat = m.get_int(F_CATEGORIA);
 | 
						|
  if (cat > 40)
 | 
						|
  {
 | 
						|
    curr.zero(CESPI_CODCGR);
 | 
						|
    curr.zero(CESPI_CODSP);
 | 
						|
  }
 | 
						|
  else
 | 
						|
  {
 | 
						|
    curr.put(CESPI_CODCGR, m.get(F_GRUPPO));
 | 
						|
    curr.put(CESPI_CODSP, m.get(F_SPECIE));
 | 
						|
  }
 | 
						|
 | 
						|
  int err = TRelation_application::write(m);
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
void TAnacespi::kill_cespite(const TString& idcespite, int lfile, int key)
 | 
						|
{
 | 
						|
  TRelation rel(lfile);
 | 
						|
  TRectype& filter = rel.curr(); 
 | 
						|
  filter.put("IDCESPITE", idcespite);
 | 
						|
  TCursor cur(&rel, "", key, &filter, &filter);
 | 
						|
  const long items = cur.items();
 | 
						|
  cur.freeze();
 | 
						|
  const TRectype& curr = rel.curr();
 | 
						|
  for (cur = 0L; cur.pos() < items; ++cur)
 | 
						|
  {
 | 
						|
    int err = curr.remove(rel.lfile());
 | 
						|
    if (err != NOERR)
 | 
						|
      error_box("Errore %d di cancellazione sul file %d", err, lfile);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
bool TAnacespi::remove()
 | 
						|
{
 | 
						|
 | 
						|
  const int staat = _qmask->stato_attivita();
 | 
						|
  bool yes = FALSE;
 | 
						|
  if (staat == 3)
 | 
						|
    yes = yesno_box("ATTENZIONE: il cespite è già stato stampato su bollato.\n"
 | 
						|
                    "Si desidera confermare l'elimininazione?");
 | 
						|
  else                  
 | 
						|
    yes = yesno_box("ATTENZIONE: verranno eliminati anche i valori relativi agli esercizi precedenti.\n"
 | 
						|
                    "Si desidera confermare l'elimininazione?");
 | 
						|
    
 | 
						|
  if (yes)  
 | 
						|
  {
 | 
						|
    const TString16 idcespite = get_relation()->curr().get(CESPI_IDCESPITE);
 | 
						|
    kill_cespite(idcespite, LF_SALCE);
 | 
						|
    kill_cespite(idcespite, LF_MOVCE, 2);
 | 
						|
    kill_cespite(idcespite, LF_MOVAM);
 | 
						|
    kill_cespite(idcespite, LF_AMMMV);
 | 
						|
    kill_cespite(idcespite, LF_AMMCE);
 | 
						|
    TRelation_application::remove();
 | 
						|
  }
 | 
						|
  return yes;
 | 
						|
}
 | 
						|
 | 
						|
bool TAnacespi::user_create()
 | 
						|
{             
 | 
						|
  open_files(LF_TABCOM, LF_TAB, LF_CESPI, LF_SALCE, LF_AMMCE, LF_MOVCE, LF_MOVAM, LF_AMMMV, 0);
 | 
						|
  _cespiti = NULL;
 | 
						|
  _rel_year = 0;
 | 
						|
 | 
						|
  _qmask = new TQuery_mask;
 | 
						|
  _emask = new TEdit_mask;
 | 
						|
  return TRUE;
 | 
						|
}
 | 
						|
 | 
						|
bool TAnacespi::user_destroy()
 | 
						|
{             
 | 
						|
  delete _cespiti;
 | 
						|
  delete _emask;
 | 
						|
  delete _qmask;
 | 
						|
  return TRUE;
 | 
						|
}
 | 
						|
 | 
						|
int ce0500(int argc, char* argv[])
 | 
						|
{
 | 
						|
  TAnacespi a;
 | 
						|
  a.run(argc, argv, "Anagrafica cespiti");
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 |