#ifndef __M70102_H
#define __M70102_H

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

#ifndef __CONFIG_H
#include <config.h>
#endif                   

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

#ifndef __77LIB_H
#include "77lib.h"   
#endif

#ifndef __770100a_H
#include "770100a.h"
#endif

// Id della maschera di edit dello sheet pagamenti
#define MPAG_DATAPAG       	101
#define MPAG_COMPENSO      	102
#define MPAG_SPESA         	103
#define MPAG_IMPONIBILE    	104
#define MPAG_PERCRA        	105 
#define MPAG_RITPAG    			106
#define	MPAG_INPSPERC				107
#define	MPAG_INPSCOMP				108
#define MPAG_NETTO      		109

// Id della maschera di edit dello sheet versamenti
#define F_VERS_1015     		101
#define F_DATAVERS     			102
#define F_LUOVERS      		  103
#define F_TIPOVERS      		104
#define F_ABI  							105   // abi e cab
#define F_CAB  							72
#define	SM_PROGRBANCA				107
#define F_RITVERS      			108                       

// Posizioni nella riga
const int	SS_POS_NUMVERS   = 8;
const int	SS_POS_RITPAG  	 = 5;  
const int 	SS_POS_NRIGA   = 5;
const int 	SS_POS_NVER    = 6; 
const int	SS_POS_RITVERS   = 7;

// Posizioni nell'array-sheet dei pagamenti
const int 	AS_POS_NRIGA   = 5;            
const int 	AS_POS_NVER    = 6; 
const int	AS_POS_RITPAG  	 = 4;  

#define COLONNE_SIGNIFICATIVE_SPREADSHEET_VERSAMENTI  7
#define COLONNE_SIGNIFICATIVE_SPREADSHEET_PAGAMENTI   7


class TSchedaPercipienti : public TRelation_application
{
  private:
    bool					_from_modify;
    bool					_azzerato_pag;
    TLink770				_coll;
    TSchedaPE*      		_rel;
    TMask*          		_msk[2];
    long            		_codanagr;
    TString16          		_tipoa;
    TString80				_ragsoc;
    int             		_nproga;
    int             		_mode;
    TLocalisamfile*  		_per, *_scper, *_rver, *_rpag;
    long            		_rigavers;
    long            		_lastvers, _lastpag, _last_scheda;
    long            		_numreg;    // NUMREG inviato dalla 1. nota    
    int             		_anno_dic;  // anno dichiarazione

// Collegamento versamento->pagamenti
    TBit_array      _selected;
    TNikArray_sheet*  _pags;

    real            _tot_compenso, _tot_spese, _tot_netto;
    real            _tot_vers, _tot_rit;
    
  private:               
    void	get_ragsoc(const char* tipoa, const long codanagr);
    static void work_tipoluogo(TMask_field& f);
    static bool check_pagamenti     (TMask_field& f, KEY key);
    static real calcola_imponibile  (TMask_field& f,const real& compenso,const bool force=FALSE);
    static bool calcola_ritenuta    (TMask_field& f,const real& compenso,const bool forza=FALSE,const real perc=ZERO);
    static real calcola_netto       (TMask_field& f);
    real calcola_imponibile(const real& compenso);
    real calcola_ritenuta (const real& imponibile, TString& sPercRa,
          real& rPercCassaPrev, real &rPercContrInps, real& rPercAssContrInps,
          real& rQuotaCommitContrInps, bool& bContrInps); 
    static long get_numvers  (TMask_field& f);
    static void setta_ritvers(TMask_field& f, const real& ritenuta);
    
    static bool nprog_handler     (TMask_field& f, KEY key);
    static bool codanagr_handler  (TMask_field& f, KEY key);
    static bool codditta_handler  (TMask_field& f, KEY key);
    
    static bool check_compensi    (TMask_field& f, KEY k);
    static bool check_spese       (TMask_field& f, KEY k);
        
// Handler del versamento
    static bool vers_notify       (TSheet_field& s, int r, KEY key);
    static bool pags_notify       (TSheet_field& s, int r, KEY key);
    static bool vers1015_handler  (TMask_field& f, KEY key);

    static bool compilato_vers(TMask_field& f);

//    static bool pag_handler   (TMask_field& f, KEY key);    

    static bool compenso_handler   (TMask_field& f, KEY key);
    static bool imponibile_handler (TMask_field& f, KEY key);
    static bool percra_handler   (TMask_field& f, KEY key);
    static bool spesa_handler    (TMask_field& f, KEY key);      
    static bool netto_handler    (TMask_field& f, KEY k);
    static bool tipo_hndl        (TMask_field& f, KEY key);      
    static bool luo_hndl         (TMask_field& f, KEY key);      
    static bool abicab_hndl      (TMask_field& f, KEY key);          
    static bool check_handler    (TMask_field& f, KEY key);
    static bool check_ritenuta   (TMask_field& f, KEY k);
    static bool check_causale    (TMask_field& f, KEY k);

    static bool forced_handler (TMask& m, KEY key);
            
    static bool ver_notify (int r, KEY key);        
    static bool pag_select (TMask_field& f, KEY key);
    static bool ver_azzera (TMask_field& f, KEY key);
    static bool pag_azzera (TMask_field& f, KEY key);
    
    void        enable_vers_cells  (int r, bool enable=TRUE);
    void        disable_vers_cells (int r) { enable_vers_cells(r,FALSE); }
        
    bool        vers_attached(const long numvers);

    void pag_pack();
    void ver_pack();
 
    static bool setta_nprog    (TMask& m, const bool variazione=FALSE, 
                                          const char* codanagr=NULL);
                            
  protected:
    virtual bool				user_create();
    virtual bool  			user_destroy();
    virtual TRelation*	get_relation() const { return _rel; }
    virtual TMask*      get_mask(int mode);
    virtual bool        changing_mask(int mode);  
    virtual void        init_query_mode (TMask&);
    virtual void        init_query_insert_mode (TMask&);    
    virtual void        init_insert_mode(TMask&);
    virtual void        init_modify_mode(TMask&);
    virtual int         rewrite(const TMask& m);
    virtual int         write(const TMask& m);
    virtual int         read(TMask& m);              
    virtual void        on_config_change();    
            	
    void		init_mask(TMask&);  
    TMask*	load_mask(int n);
    void    fill_sheet(TMask&);
    bool    scrivi_dati_per(TRectype& rec);
    void    mask2rel(const TMask& m);
    void    rec2row(TToken_string& r, TRectype& rec, int row);
    void    update_lasts(TMask& m);
    
    void 		attach_pag_vers (int pag, long numvers);
    void 		detach_pag_vers (int pag);   
		void 		new_row(TSheet_field& ver_pag, int r, bool vers);        
    bool    my_empty_items(TToken_string& r, bool versamenti);
    int     anno_dic() const { return _anno_dic; }

  public:
  	bool				_flag_cg;
    void        load_pag_sheet(const long numvers);
    TTipo_coll	tipo_coll() const { return _coll._tipo_coll; }
    void 				compila_testata(TMask& m);
    bool 				coll_variazione() const; 
    bool 				coll_datanum() const { return _coll._datadoc.not_empty() && _coll._numdoc.not_empty(); }
    bool				esiste_perc(TMask& m, const char* coda=NULL);

    TSheet_field& pags() const;
    TSheet_field& vers() const;

    TSchedaPercipienti ();
    ~TSchedaPercipienti() {};
};

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

#endif
