#include <applicat.h>
#include <mask.h>
#include <msksheet.h>
#include <tabutil.h> 

// IVA a debito da riportare al mese successivo
#define IVA_DA_RIPORTARE      real(50000)

class TSheet_field;  
class TViswin;

class Visliq_app : public TApplication
{
  TString   _freqviva;  
  int       _year;                    
  long      _firm;

  TRelation*    _nditte;
  TArray_sheet* _ditte;
  TArray        _nomiditte;
  
  TTable*    _lim;  
  TTable*    _lam;  
  TTable*    _lia;  
  TTable*    _del;  

  TRectype*  _lim_r;
  TRectype*  _lam_r;
  TRectype*  _del_r;
  TRecfield* _del_ditta;
  TRecfield* _del_anno;
  TRecfield* _del_mese;
  TRecfield* _del_tipo;
  TRecfield* _lim_anno;
  TRecfield* _lim_mese;
  TRecfield* _lam_anno;
  TRecfield* _lam_mese;

  TSheet_field* _vers_sheet, *_liq_sheet;
  TArray        _vers_rows,   _liq_rows;
  TMask*        _mask;
  bool _from_one;

  const char* itoname(int m);
  
  bool is_trim(int x); 
  bool is_month_ok(int x, int month = -1);
  
protected:

  virtual bool menu(MENU_TAG);                              
  virtual bool create(); 
  virtual bool destroy(); 
  
  // handlers             
  static bool set_ditta(TMask_field&, KEY);
  static bool set_ragsoc(TMask_field&, KEY);
  static bool sel_mese_sh1 (TMask_field&, KEY);
  static bool sel_mese_sh2 (TMask_field&, KEY);
  static bool ch_year_handler(TMask_field& f, KEY key);

  static const char* link_handler(TMask&, int, const char*, bool doubleclick);

  static bool sheet_action(TSheet_field& s, int r, KEY k);
  static bool vers_action(TSheet_field& s, int r, KEY k);

public:                                                  

  static Visliq_app& app() { return (Visliq_app&)main_app(); }
  TArray_sheet* get_ditte_sheet() { return _ditte; }

  void build_ditte_sheet();
  void build_nomiditte();

  bool look_lia(long ditta = 0l, int year = 0);       
  bool look_lim(int m);
  bool look_del(int month, int type, bool create = FALSE);
  
  void read_general(TMask& m);
  void write_general(TMask& m);
  void write_liq();
  void write_del();

  
  void set_freqviva();                          
  void set_freqviva(const char* f) { _freqviva = f; }                          
  void set_mask_freq(TMask&);
  void set_year(const char* y) { _year = atoi(y); }                                                             
  void set_year(int y)         { _year = y; }   
  void reset_fields();

  void set_liq_rows (TSheet_field& s) { _liq_rows  = s.rows_array(); }
  void set_vers_rows(TSheet_field& s) { _vers_rows = s.rows_array(); }
  
  // 2 palle
  TMask* get_main_mask()      { return _mask; }
  bool select_butt(TMask& m);                   
  TTable*  del() { return _del; }
  
  // riaggiusta la liquidazione del mese dato
  void recalc_liq_data(TViswin* v, real& rimb, real& rett, real& vers, 
                       real& acc, TString& d1, TString& d2, TDate& date, TString&
                       abi, TString& cab, TString& con);   

  // riaggiusta le liquidazioni successive se serve                     
  void recalc_next_liq(int start_month, TSheet_field* sl, TSheet_field* sv);
  
  // workhorses
  bool vis_liq();
  bool vis_all();
  bool vis_one(int);
  
  Visliq_app() : TApplication(), _vers_sheet(NULL), _liq_sheet(NULL)  {}
  virtual ~Visliq_app() {}
};

