#ifndef __CG2103_H                
#define __CG2103_H

#ifndef __ISAM_H
#include <isam.h>
#endif                                           

#ifndef __CONTO_H
#include "conto.h"
#endif

enum TipoIVA {
  iva_errata   = -1,
  nessuna_iva  = 0,
  iva_vendite  = 1,
  iva_acquisti = 2,
  iva_generica = 9
  };

const char* iva2name(TipoIVA i);                                                 


class TRegistro : public TObject
{           
  bool read_att();

protected:
  enum TipoRegistro { libro_giornale = 5 };
  TRectype _rec, _att;

public:
  bool read(const char* code, int year);
  TRegistro& operator =(const TRegistro& r);

  virtual bool ok() const { return !_rec.empty(); }

  int tipo() const;
  TipoIVA iva() const;
  bool corrispettivi() const;
  const TString& name() const { return _rec.get("CODTAB").mid(4); }
  int year() const;
  TDate last_reg() const { return _rec.get_date("D2"); } 
  TDate last_print() const { return _rec.get_date("D3"); }
  long protocol() const { return _rec.get_long("I5"); }
  const TString& attivita() const { return _rec.get("S8"); }

  bool agenzia_viaggi();
  const TString& tipo_attivita();

  bool update(long uprotiva, const TDate& lastreg);

  TRegistro(const char* code = "", int year = 0);  
  virtual ~TRegistro() {}
};


class TLibro_giornale : public TRegistro
{
public:
  bool read(int year);
  TLibro_giornale(int year = 0);
  virtual ~TLibro_giornale() {}
};


class TCausale : public TArray
{ 
  TipoIVA _iva;
  TRectype _rec;
  TRegistro _reg;

protected:
  const TRectype& row(int num) const;

public:
  TConto& bill(int num, TConto& c) const;
  
  bool data_doc() const;
  bool num_doc() const;
  bool apertura() const;
  bool sezionale() const;
  bool valuta() const;
  bool intra() const;
  bool corrval() const;
  bool puramentecontabile() const;
  const char* causale_inc_imm() const;
  const char* tipo_doc() const;
  const char* cod_reg() const;
  int tipo_mov() const;

  TRegistro& reg() { return _reg; }
  TipoIVA iva() const;
  
  virtual bool ok() const { return !_rec.empty(); }
  bool similar(const TCausale& c) const;
  
  bool read(const char* cod, int year);

  TCausale(const char* code = "", int year = 0);
  virtual ~TCausale() {}
};                                               

class TCodiceIVA : public TRectype
{
public:
  TCodiceIVA(const char* codice = NULL);                        
  bool read(const char* codice);                    
  bool ok() const { return !empty(); }
  const TString& codice() const { return get("CODTAB"); }
  real percentuale() const { return get_real("R0"); }
  const TString& tipo() const { return  get("S1"); }
};

#endif
