#ifndef __CG2102_H
#define __CG2102_H

#ifndef __MSKSHEET_H
#include <msksheet.h>
#endif

#ifndef __RELAPP_H
#include <relapp.h>
#endif

#ifndef __CGLIB_H
#include "cglib.h"
#endif

#ifndef __CG2101_H
#include "cg2101.h"
#endif

#ifndef __CG2103_H
#include "cg2103.h"
#endif

class TPrimanota_application : public TRelation_application
{
  TMovimentoPN* _rel;
  TLocalisamfile* _tabcom;
  TLocalisamfile* _tab;
  TLocalisamfile* _caus;
  TLocalisamfile* _rcaus;
  TLocalisamfile* _clifo;
  TLocalisamfile* _pcon;
  TLocalisamfile* _attiv;
  TLocalisamfile* _saldo;
  TLocalisamfile* _comuni;
  TLocalisamfile* _occas;
  TLocalisamfile* _nditte;
  
  TCausale _causale;          // Causale corrente
  TLibro_giornale _giornale;  // Libro giornale corrente
  TipoIVA _iva;               // Tipo di IVA corrente
  bool _ges_val, _ges_sal;    // Gestione valuta e saldaconto
  bool _savenew;              // Registra e nuovo
  bool _iva_showed;           // Lo sheet IVA e' gia' stato visualizzato una volta?
  
  TSaldo_agg _saldi;          // Saldi da aggiornare

  TMask* _msk[4];             // Maschere di query, cg, iva
  long _lastreg;              // Numero ultima registrazione
  int _mode;                  // Modo maschera corrente

  void* _app_data;

  static bool suspended_handler(TMask_field& f, KEY k);
  static bool num_handler(TMask_field& f, KEY key);
  static bool caus_query_handler(TMask_field& f, KEY key);
  static bool caus_modify_handler(TMask_field& f, KEY key);
  static bool datareg_handler(TMask_field& f, KEY key);
  static bool datacomp_handler(TMask_field& f, KEY key);
  static bool data74ter_handler(TMask_field& f, KEY key);
  static bool doc_handler(TMask_field& f, KEY key);
  static bool protiva_handler(TMask_field& f, KEY key);
  static bool clifo_handler(TMask_field& f, KEY key);
  static bool totale_handler(TMask_field& f, KEY key);
  static bool ritfis_handler(TMask_field& f, KEY key);
  static bool ritsoc_handler(TMask_field& f, KEY key);
  static bool main_codiva_handler(TMask_field& f, KEY key);
  static bool cambio_handler(TMask_field& f, KEY key);
  static bool visval_handler(TMask_field& f, KEY key);
  static bool corrlire_handler(TMask_field& f, KEY key);
  static bool corrvaluta_handler(TMask_field& f, KEY key);
  static bool occas_code_handler(TMask_field& f, KEY key);
  static bool occas_handler(TMask_field& f, KEY key);
  static bool solaiva_handler(TMask_field& f, KEY key);
  static bool descr_handler(TMask_field& f, KEY k);
  
  static bool sheet_clifo_handler(TMask_field& f, KEY k);
  static bool iva_sottoconto_handler(TMask_field& f, KEY k);
  static bool imponibile_handler(TMask_field& f, KEY key);
  static bool detrazione_handler(TMask_field& f, KEY key);
  static bool codiva_handler(TMask_field& f, KEY key);
  static bool imposta_handler(TMask_field& f, KEY key);
  static bool dareavere_handler(TMask_field& f, KEY k);
  static bool cg_conto_handler(TMask_field& f, KEY key);

  static bool iva_notify(int r, KEY key);
  static bool iva_handler(TMask_field& f, KEY key);
  static bool cg_notify(int r, KEY key);
  static bool cg_handler(TMask_field& f, KEY key);
  static bool pag_notify(int r, KEY key);

protected:
  virtual bool user_create();
  virtual bool user_destroy();
  virtual void print();
  virtual bool change_config(const char* var, const char* old, const char* val);
  virtual bool changing_mask(int mode);
  virtual TMask* get_mask(int mode);
  virtual TRelation* get_relation() const { return _rel; }
  virtual void init_query_mode(TMask& m);
  virtual void init_insert_mode(TMask& m);
  virtual void init_modify_mode(TMask& m);
  virtual bool save_and_new() const;
  virtual const char* get_next_key();
  virtual int read(TMask& m);
  virtual int write(const TMask& m);
  virtual int rewrite(const TMask& m);
  virtual bool remove();
  
  void genera_incasso(const char* causimm);
  void aggiorna_scadenzario(const TMask& m);
  
  TMask* load_mask(int n);
  TMask& occas_mask() { return *load_mask(3); }
  
  bool get_conto(const TRectype& r, TBill& c) const;
  void put_conto(TRectype& r, const TBill& c) const;

  void mask2rel(const TMask& m);

  TipoIVA iva() const { return _iva; }
  char clifo() const { return iva() == iva_vendite ? 'C' : 'F'; }
  bool gestione_valuta() const { return _ges_val; }
  bool gestione_saldaconto() const { return _ges_sal; }

  void fill_sheet(TMask& m) const;
  
  void read_firm_params();
  void init_mask(TMask& m);

  TSheet_field& cgs() const;
  TSheet_field& ivas() const;

  static int bill2pos(const TBill& conto, char tipo);
  static TipoIVA reg2IVA(const char* registro, int anno);
  static TipoIVA cau2IVA(const char* causale, int anno);
  static int type2pos(char tipo);
  static const real& cod2IVA(const TMask& m);
  static real scorpora(real& imponibile, const real& percentuale);
  static bool detraibile(TToken_string& row);

  bool IVA2bill(const TCodiceIVA& iva, TBill& bill);
  int bill2contr(const TBill& c, char sezione) const;
  int bill_used(const TBill& conto) const;
  int det_used(char detraib) const;
  TBill& ivas_bill(TBill& tc);
  
  bool read_caus(const char* cod, int year);                                                  
  TCausale& causale() { return _causale; }
  TLibro_giornale& giornale() { return _giornale; }

  bool test_swap(bool ritsoc);
  TImporto real2imp(const real& r, char tipo);
  real totale_documento();
  
  void set_cgs_imp(int n, const TImporto& importo);
  TImporto get_cgs_imp(int n);
  TImporto add_cgs_imp(int n, const TImporto& importo);
  TImporto sub_cgs_imp(int n, const real& imp);
  
  void cgs_pack();
  bool ci_sono_importi() const;
  real calcola_saldo() const;
  real calcola_imp() const;

  int set_cgs_row(int n, const TImporto& importo, TBill& conto, const char* desc, char tipo);
  void reset_cgs_row(int n);
  void disable_cgs_cells(int n, char tipo);
  void add_cgs_tot(TMask& m);
  void add_cgs_rit(bool fisc);        
  void generazione_righe_cg(int r);
  
  void* get_app_data() { return _app_data; }
  void  set_app_data(void* v) { _app_data = v; }

public:
  TPrimanota_application();
};

#endif
