#include <applicat.h>
#include <defmask.h>
#include <modaut.h>
#include <progind.h>
#include <sheet.h>
#include <smartcard.h>
#include <tabutil.h>

#include "velib.h"
#include "vepriv.h"

#ifndef __VERIG_H
#include "verig.h"
#endif

#ifndef __VEUML1_H
#include "veuml1.h"
#endif

#ifndef __VEINI_H
#include "veini.h"
#endif

#ifndef __SCONTI_H
#include "sconti.h"
#endif

#include "../cg/cg2103.h"

#ifndef __MGLIB_H 
#include "../mg/mglib.h"
#endif

#ifndef __DBLIB_H
#include "../db/dblib.h"
#endif

#include "../mg/anamag.h"
#include "../mg/codcorr.h"
#include "../mg/deslin.h"
#include "../mg/umart.h"
#include "../ca/calib01.h"
#include "velib.h"

#define MAX_VIS_RATE 5

bool numdocrif_hndl( TMask_field& field, KEY key )
{
	if (key == K_ENTER)
	{
		TDocumento_mask& m = (TDocumento_mask&)field.mask();
		TString16 campo = DOC_NUMDOCRIF;
		if (field.field() != NULL)
			campo = field.field()->name();

		TRectype filtrec(LF_DOC);
		filtrec.put(DOC_TIPOCF, m.get(F_TIPOCF));
		filtrec.put(DOC_CODCF, m.get(F_CODCF));
		filtrec.put(DOC_PROVV, m.get(F_PROVV));
		filtrec.put(DOC_ANNO, m.get(F_ANNO));

		TRelation rel(LF_DOC);
		TString sortkey = "TIPOCF|CODCF|PROVV|ANNO|CODNUM|"; sortkey << campo;
		TSorted_cursor cur(&rel, sortkey, "", 2, &filtrec, &filtrec);
		TString80 f; f.format("CODNUM==\"%s\"", (const char*)m.get(F_CODNUM));

		cur.setfilter(f);

		filtrec.put(DOC_CODNUM, m.get(F_CODNUM));
		filtrec.put(campo, field.get());
		cur.curr() = filtrec;
		cur.read(_isequal);

		if (cur.file().status() == NOERR)
		{
			const long numdoc = m.get_long(F_NDOC);
			if (numdoc != cur.curr().get_long(DOC_NDOC))
				return yesno_box("Il numero di riferimento %s � gi� stato utilizzato\nsi desidera registrare ugualmente?",
													(const char *)field.get());
		}
	}
	return true;

}

bool totdoc_hndl( TMask_field& field, KEY key )

{
	if (key == K_ENTER)
	{
		TDocumento_mask & m = (TDocumento_mask &) field.mask();
		const real totdoc = m.doc().totale_doc();
		const real totdoc_check(field.get()); 
		
		
		if (m.doc().physical_rows() > 0 && totdoc != totdoc_check)
		{
			const TString16 tchk(totdoc_check.string());
			
			return yesno_box("Il totale documento digitato (%s) non corrisponde\nal totale documento (%s) calcolato,\n devo registrare ugualmente",
												(const char *) tchk, totdoc.string());
		}
	}
	return true;

}

bool smart_hndl( TMask_field& field, KEY key )

{
	if (key == K_SPACE)
	{
		TDocumento_mask & m = (TDocumento_mask &) field.mask();
		TSmart_card * s = m.smartcard();

		if (s != NULL)
		{
			smartcard_error err = s->connect_card();
			if (err == no_smarterror)
				err = s->check_key(m);
			if (err == no_smarterror)
			{
				s->card2mask(m);
				if (s->with_card(m))
					m.enable(DLG_SAVEREC);
				m.disable(F_CODCF);
				m.disable(F_RAGSOC);
			}
			else
			{
				s->display_error(err);
				if (err == new_card)
					while (m.get(F_CODCF).empty() && m.field(F_CODCF).on_key(K_F9) == false);
			}
		}
	}
	else
		if (key == K_ENTER)
		{
			TDocumento_mask & m = (TDocumento_mask &) field.mask();
			TSmart_card * s = m.smartcard();

			if (s != NULL && s->card_connected())
			{
				s->mask2card(m);
				smartcard_error err = s->write();
				if (err != no_smarterror)
					s->display_error(err);
				if (m.insert_mode() && m.doc().codice_numerazione().save_and_new())
					s->disconnect_card();
			}
		}

	return true;

}

bool ora_hndl( TMask_field& field, KEY key )

{
	if (field.to_check(key,true))
	{
		TString16 ora;
		
		if (field.automagic() && field.get().empty())
		{
			time_t lt; time(&lt);
			struct tm* t = localtime(&lt);
 
			ora.format("%02d%02d", t->tm_hour, t->tm_min);
			field.set((ora));
		}
		else
		{
			ora = field.get();
			if (ora.not_empty())
			{

				if (!isdigit(ora[0]) ||
						!isdigit(ora[1]) ||
						!isdigit(ora[2]) ||
						!isdigit(ora[3]) ||
						 atoi(ora.left(2)) > 23 ||
						 atoi(ora.mid(2)) > 59)
					return error_box("Ora errata");
			}
			else
				 if (field.required())
					 return error_box("Ora obbligatoria");
		}
	}

	return true;
}

bool dummy_hndl(TMask_field& field, KEY key)
{
	warning_box( "Al campo %d � arrivato un KEY %d", field.dlg( ), key );
	return true;
}

// Handler per il calcolo delle date di pagamento
bool condpag_hndl( TMask_field& field, KEY key )
{
	TDocumento_mask& m = (TDocumento_mask &) field.mask( );
	
	if ( field.to_check(key) || (key == K_TAB && !m.is_running()))
	{
		const TString& condpag = m.get(F_CODPAG);
		if (condpag.not_empty())
		{
			TDocumento& doc = m.doc();
			// Aggiorna dati necessari per determinare il pagamento
			doc.put(DOC_CODPAG, condpag);
			doc.put(DOC_DATADOC, m.get(F_DATADOC));
			doc.put(DOC_DATAINSC, m.get(F_DATAINSC));
			doc.put(DOC_TIPOCF, m.get(F_TIPOCF));
			doc.put(DOC_CODCF, m.get(F_CODCF));
			TPagamento& pag = doc.pagamento();		 
			pag.set_total(CENTO, real(10), real(10));
			pag.set_rate_auto();
			
			const int numrate = pag.n_rate( );
			for(int i = 0; i < MAX_VIS_RATE; i++)
			{
				if (i < numrate)
				{
					m.show(F_DATASCAD1+i);
					m.set(F_DATASCAD1+i, pag.data_rata(i));
				}
				else	
					m.hide(F_DATASCAD1+i);
			}
		}
	}
	return true;
}

bool note_hndl( TMask_field& f, KEY key )
{
	TDocumento_mask& m = (TDocumento_mask &) f.mask();

	if (key == K_TAB && (f.focusdirty() || !m.is_running()))
	{ 
		const TRectype& note = cache().get("%NOT", f.get());
		if (!note.empty())
		{
			if (m.is_running() || m.field(F_NOTECLI).empty()) // Preserva descrizione presente in caricamento
			{
				// gestione del campo con descrizione estesa
				TString stringone;
				for (int i = 0; i < 6; i++)
				{
					const char fieldname[3] = { 'S', i+'0', '\0' };
					stringone << note.get(fieldname);
				}
				stringone.replace(char(0xB6), '\n');
				m.set(F_NOTECLI, stringone);
			}

			if (m.doc().modificabile() && m.field(DLG_SAVEREC).enabled())
			{
				const bool reg_disabled = note.get_bool("B0");	
				if (reg_disabled)
				{
					message_box(FR("Registrazione disabilitata : %s"), (const char*)note.get("S0"));
					m.disable(DLG_SAVEREC);
				}
			}
		}

	}
	
	return true;
}

// Handler per il calcolo delle date di pagamento
bool data_hndl( TMask_field& field, KEY key )
{
	TDocumento_mask& m = (TDocumento_mask &) field.mask();
	if (field.to_check(key))
	{								
		if (m.id2pos(F_DATAINSC) >= 0)
		{
			TEdit_field & e = m.efield(F_DATAINSC);
			e.set_dirty();
			e.on_hit();
		}

		if (m.id2pos(F_DATACAMBIO1) >= 0 && !m.get(F_CODVAL).empty())
			m.set(F_DATACAMBIO1, field.get(), true);
	}
	if (key == K_ENTER || field.to_check(key))
	{
		const TDate datadoc(m.get(F_DATADOC));
		if (!datadoc.ok())
			return field.error_box("La data documento deve essere comunque indicata.");
			
		const TCodice_numerazione codnum(m.get(F_CODNUM));
		if (codnum.test_eser())
		{

			TEsercizi_contabili esc;
			if (esc.date2esc(datadoc) <= 0)
				return field.error_box("La data documento non appartiene ad un esercizio valido.");
			if (main_app().has_module(CGAUT))
			{
				const TTipo_documento& td = m.doc().tipo();
				TString16 codcaus = td.causale();
				if (codcaus.empty())
				{
					const TRectype& clifo = m.doc().clifor().vendite();
					codcaus = clifo.get("CODCAUS");
				}
				if (codcaus.not_empty())
				{
					const int year = datadoc.year();
					TCausale caus;
					if (!caus.read(codcaus, year))
						return error_box(TR("Causale assente!")); // L'errore viene segnalato nella read
				}
			}
		}

		if (codnum.dont_test_datadoc())
			return true;	// Non devo fare altri test

		TLocalisamfile doc(LF_DOC);

		doc.curr() = m.doc().head(); 
		bool same_key = false;

		doc.read(_isgteq);
		if (doc.eof() || doc.prev() == NOERR)
		{											 
			TDate dataprev = doc.get_date(DOC_DATADOC);

			same_key = doc.curr().same_key(m.doc().head(), 1, 1);
			if (same_key && datadoc < dataprev)
				return field.error_box("Data documento inferiore alla data del documento precedente");
		} 
	}
	return true;

}
	
// handler delle righe

void row_set_handler( TMask& m, const int field, const int index )
{
	switch ( index )
	{
		case 1:
			m.set_handler( field, pricerange_handler);
			break;
		case 2:
			m.set_handler( field, dummy_hndl );
			break;
        case 3:
			if (field == FR_CODART)
				m.set_handler( field, search_price_handler );
      break;
		default:
			yesnofatal_box( false, "Funzione di handler sulla riga non definita( %d ).", index );
	}

}

HIDDEN TString16 curr_um;
HIDDEN real curr_fc(1.0);

bool iva_handler( TMask_field& f, KEY key )
{
	TDocumento_mask & mask = (TDocumento_mask &) f.mask().get_sheet()->mask();

	if (key == 0 || (key == K_ENTER && f.get().empty()))
	{
		const TString16 codiva = mask.condv().clifo().vendite().get(CFV_ASSFIS);
		if (codiva.not_empty())
			f.set(codiva);
		f.check();
	}

  if (key == 0 || (key == K_ENTER && f.get().empty()))
  {
    const TString16 codiva = mask.condv().clifo().vendite().get(CFV_ASSFIS);
    if (codiva.not_empty())
      f.set(codiva);
    f.check();
  }

  if (key == K_ENTER && /*f.focusdirty() &&*/ f.get().empty())
  {
    TMask & row_mask = f.mask();
    const int r = f.mask().get_sheet()->selected() + 1;
    TRiga_documento& riga = mask.doc()[r];
    
    const int pos_ai = row_mask.id2pos(FR_ADDIVA);
    bool  addiva = false;
    
    if (pos_ai >= 0)
      addiva = row_mask.fld(pos_ai).get() == "X"; // Controlla le righe Omaggio solo se e' settato l'addebito IVA
    
    const bool check = riga.is_merce() || riga.is_spese() || riga.is_prestazione() || 
                       (riga.is_omaggio() && addiva);
    
    if (check)
    {
      const int pos_p = row_mask.id2pos(FR_PREZZO);
      const int pos_q = row_mask.id2pos(FR_QTA);
      const bool pe = pos_p >= 0 && row_mask.fld(pos_p).enabled();
      const bool qe = pos_q >= 0 && row_mask.fld(pos_q).enabled();
      const bool pf = pe && row_mask.fld(pos_p).get().not_empty();
      const bool qf = qe && row_mask.fld(pos_q).get().not_empty();
      const bool required = pf && !(qe && !qf);
      if (required)
        return f.error_box("Il codice IVA e' obbligatorio.");
    }
  }
  return true;
} 

bool dcons_handler( TMask_field& f, KEY key )
{
	if (key == K_F8)
	{
		TMask& row_mask = f.mask();
		TSheet_field& s = *row_mask.get_sheet();
		TDocumento_mask & mask = (TDocumento_mask &) s.mask();
		int r = f.mask().get_sheet()->selected() + 1;

		if (r > 1)
		{
			TDocumento & doc = mask.doc();
			const TRiga_documento& prev = doc[r - 1];
			TDate first_date = prev.get_date(RDOC_DATACONS);
			TDate second_date = row_mask.get_date(FR_DATACONS);

			if (!second_date.ok())
				second_date = mask.get_date(F_DATACONS);
			if (second_date.ok())
			{
				if (!first_date.ok())
					first_date = mask.get_date(F_DATACONS);
				if (first_date.ok())
				{
					if (second_date.is_end_month() && first_date.is_end_month())
					{
						int year = second_date.year();
						int month = second_date.month();
						const int months = ( year * 12 + month) - (first_date.year() * 12 + first_date.month());
						if (months > 0)
						{
							for (r++ ; r <= doc.physical_rows(); r++)
							{
								TRiga_documento & riga = doc[r];
								TToken_string & tr = s.row(r - 1);

								month += months;
								if (month > 12)
								{
									year++;
									month -= 12;
								}
								TDate d(1, month, year);

								d.set_end_month();
								riga.put(RDOC_DATACONS, d);
								tr.add(riga.get(RDOC_DATACONS), s.cid2index(FR_DATACONS));
							}
							s.force_update();
						}

					}
					else
						if (second_date.day() == first_date.day())
						{
							const int months = ( second_date.year() * 12 + second_date.month()) - (first_date.year() * 12 + first_date.month());
							if (months > 0)
							{
								for (r++ ; r <= doc.physical_rows(); r++)
								{
									TRiga_documento & riga = doc[r];
									TToken_string & tr = s.row(r - 1);

									second_date.addmonth(months);
									riga.put(RDOC_DATACONS, second_date);
									tr.add(riga.get(RDOC_DATACONS), s.cid2index(FR_DATACONS));
								}
								s.force_update();
							}
						}
						else
						{
							const int days = second_date - first_date;

							if (days > 0)
							{
								for (r++ ; r <= doc.physical_rows(); r++)
								{
									TRiga_documento & riga = doc[r];
									TToken_string & tr = s.row(r - 1);

									second_date += days;
									riga.put(RDOC_DATACONS, second_date);
									tr.add(riga.get(RDOC_DATACONS), s.cid2index(FR_DATACONS));
								}
								s.force_update();
							}
						}
				}
			}
		}
	}
	return true;
}

bool tipo_riga_handler(TMask_field& f, KEY key)
{
	if (key == K_SPACE && f.focusdirty())
	{
		TMask & row_mask = f.mask();		
		TVariable_sheet_field * sf = (TVariable_sheet_field *) row_mask.get_sheet();

		if (sf != NULL)
		{
			TDocumento_mask & docmask = (TDocumento_mask&)sf->mask();
			if (docmask.is_running())
			{
				const int curr_row = sf->selected();
				TToken_string & row = sf->row(curr_row);
				const int col = sf->cid2index(FR_TIPORIGA);
				const TString16 old_tipo_riga = row.get(col);
				const TString16 tipo_riga = f.get();

				if (!row_mask.is_running())
				{
					if (old_tipo_riga != tipo_riga)
					{
						row.add(tipo_riga, col);
						docmask.doc()[curr_row + 1].set_tipo(tipo_riga);
						TMask * rm = docmask.riga_mask(curr_row);
						sf->post_insert(curr_row);
						sf->check_row(curr_row);
						sf->force_update(curr_row);
					}
				}
				else
					if (old_tipo_riga != tipo_riga)
					{
						f.set(old_tipo_riga);
						return error_box("Impossibile cambiare il tipo nella maschera di riga");
					}
			}
		}
	}
	return true;

}

bool codmag_handler( TMask_field& f, KEY key )
{
//  if (f.to_check(key, true))
  if (key == K_TAB && f.focusdirty())
  {        
    TMask& row_mask = f.mask();    
    TSheet_field& sf = *row_mask.get_sheet();
    TDocumento_mask& docmask = (TDocumento_mask&)sf.mask();
    
    if (sf.column_enabled(sf.cid2index(FR_CODDEP)))
    {  
      const int pos = row_mask.id2pos(FR_CODDEP);
      const TString & val = f.get();
                                                                                  
      if (pos >= 0 && val.not_empty())                                                               
      {
        const TRectype& mag = cache().get("MAG", val);
        const bool active = mag.get_bool("B0");
        row_mask.fld(pos).enable(active);                              
        if (!active)
          row_mask.fld(pos).reset();                              
      }
    }


    if (f.get().empty() && docmask.doc().tipo().mov_mag())
      return f.error_box("Indicare il magazzino.");
    
    docmask.update_giacenza();
  }
  return true;
}             


bool codmag_coll_handler( TMask_field& f, KEY key )
{
  if (key == K_TAB && f.focusdirty())
  {        
    TMask& row_mask = f.mask();    
    TSheet_field& sf = *row_mask.get_sheet();
    
    if (sf.column_enabled( ((TSheet_field &)f).cid2index(FR_CODDEPC)))
    {  
      const int pos = row_mask.id2pos(FR_CODDEPC);
      const TString & val = f.get();
                                                                                  
      if (pos >= 0 && val.not_empty())                                                               
      {
        const TRectype& mag = cache().get("MAG", val);
        const bool active = mag.get_bool("B0");
        row_mask.fld(pos).enable(active);                              
        if (!active)
          row_mask.fld(pos).reset();                              
      }
    }
  }
  return true;
}  


void upd_colli_peso_tara(TMask& m, const TRectype& a)
{
	// const real qta = m.get_real(FR_QTA) * curr_fc;	// curr_fc puo' non essere inizializzata e vale -1
	// Usando il metodo apposito di TArticolo non si sbaglia micca mai. 
	// Sarebbe bene sparare (come dice Luca) a curr_fc ovunque copaia
	TArticolo articolo(a);
	const real qta = articolo.convert_to_um(m.get_real(FR_QTA), NULL, m.get(FR_UMQTA));

	const real ppcollo = a.get_real(ANAMAG_PPCOLLO);
	
	real ncolli = ppcollo.is_zero() ? UNO : qta / ppcollo;
	ncolli.ceil(0);
	
	int pos = m.id2pos(FR_NCOLLI);
	if (pos >= 0)	
		if (ppcollo != ZERO)
			m.fld(pos).set(ncolli.string());
		else	
			m.fld(pos).reset();

	pos = m.id2pos(FR_TARA);
	if (pos >= 0)
	{
		const real tara = ncolli * a.get_real(ANAMAG_TARA);
		m.fld(pos).set(tara.string());
	}
	pos = m.id2pos(FR_PNETTO);
	if (pos >= 0)
	{
		const real peso = qta * a.get_real(ANAMAG_PESO);
		m.fld(pos).set(peso.string());
	}
}



bool pricerange_handler(TMask_field& f, KEY key )
{             
  if (key == K_TAB && f.focusdirty())
  {            
    TMask& row_mask = f.mask();   
    TSheet_field& sh = *row_mask.get_sheet();
    TDocumento_mask& mask = (TDocumento_mask &)sh.mask(); 
    
    if (mask.doc().tipo().controllo_prezzi())
    {
      const int current_doc_row = sh.selected() + 1;      
      TRiga_documento & riga = mask.doc()[current_doc_row];
      const real old_price = riga.get_real(RDOC_PREZZO);
      const real new_price = row_mask.get_real(FR_PREZZO);
      
      if (!old_price.is_zero())
      {
        const TTipo_riga_documento& tipo_riga = riga.tipo();
        const int incr = tipo_riga.incr_perc_prezzo();
        const int decr = tipo_riga.decr_perc_prezzo();
    
        if (incr == 0 && decr == 0)
        {
          if (old_price != new_price && !yesno_box("Il prezzo � variato, confermare"))
            return false;
        }
        else
        {
          real inf = old_price * (UNO - (decr/CENTO));
          if (inf > new_price && !yesno_box("Il prezzo � diminuito di pi� del %d%%, confermare", decr))
            return false;
          real sup = old_price * (UNO + (incr/CENTO));
          if (sup < new_price && !yesno_box("Il prezzo � aumentato di pi� del %d%%, confermare", incr))
            return false;
        }
      }                 
      riga.put(RDOC_PREZZO, new_price);
    }
  }
  
	return true;
}


static bool __in_handler = false;

int sort_sheet(const TObject** a, const TObject** b)
{
	TToken_string * t1 = (TToken_string *) *a;
	TToken_string * t2 = (TToken_string *) *b;
	const char * a1 = t1->get(1);
	const char * a2 = t2->get(1);
	int cmp = strcmp(a1, a2);

	if (cmp == 0)
	{
		TDate d1(t1->get(2));
		TDate d2(t2->get(2));

		cmp = d2 - d1;
		if (cmp == 0)
			return t2->get_int(8) - t1->get_int(8);
	}
	return cmp;
}

void search_price(TMask_field& f, KEY key )
{
	TMask& row_mask = f.mask();   
  TSheet_field& sh = *row_mask.get_sheet();
  TDocumento_mask& mask = (TDocumento_mask &)sh.mask();
	TArray_sheet sheet(0, 3, -1, 16, "Ricerca Prezzi", 
										 "N.ro doc.|Codice articolo@20|Data@10|Prezzo@18P|Quantit�|Sconto@10|Numerazione@18|Tipo Documento@18", 0, 1);
	TString_array & el = sheet.rows_array();
	TString form;
	TRelation * rel = new TRelation(LF_DOC);
	TRectype from(rel->curr());
	TRectype to(rel->curr());
	TDate datadoc = mask.get_date(F_DATADOC);
	const long codcf = mask.get_long(F_CODCF);
	const TString16 numdoc(mask.get(F_NDOC));
	const int last_anno = mask.get_int(F_ANNO);
	const int current_doc_row = sh.selected() + 1;
	const int first_anno = last_anno - ((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_years();
	TToken_string nums(((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_nums());
	const TString& tipocf = mask.get(F_TIPOCF);

	from.put(DOC_TIPOCF, tipocf);
	to.put(DOC_TIPOCF, tipocf);
	from.put(DOC_CODCF, codcf);
	to.put(DOC_CODCF, codcf);
	from.put(DOC_PROVV, "D");
	to.put(DOC_PROVV, "D");
	from.put(DOC_ANNO, first_anno);
	to.put(DOC_ANNO, last_anno);
	to.put(DOC_DATADOC, datadoc);


	if (nums.empty())
		nums =  mask.get(F_CODNUM);

	TString8 first_num(nums.get(0));
	TString8 last_num(first_num);;
	
	form << "((CODNUM==\"" << first_num << "\")";
	for (const char * s = nums.get(); s && *s; s = nums.get()) 
	{
		if (first_num > s)
			first_num = s;
		if (last_num < s)
			last_num = s;
		form << "||(CODNUM==\"" << s << "\")";
	}
	form << ")";

	TCursor cur(rel, form, 2, &from, &to);
	const int items = cur.items();
	cur.freeze();
	TRecord_array r(LF_RIGHEDOC, RDOC_NRIGA);
	TRectype rec(LF_RIGHEDOC);

	for(cur = 0L; cur.pos() < items; ++cur)
	{
		const TString16 data(cur.curr().get(DOC_DATADOC));
		const TString80	num(cache().get("%NUM", cur.curr().get(DOC_CODNUM), "S0"));
		const TString80	tipo(cache().get("%TIP", cur.curr().get(DOC_TIPODOC), "S0"));

		rec.zero();
		rec.put(RDOC_PROVV, cur.curr().get(DOC_PROVV));
		rec.put(RDOC_ANNO, cur.curr().get(DOC_ANNO));
		rec.put(RDOC_CODNUM, cur.curr().get(DOC_CODNUM));
		rec.put(RDOC_NDOC, cur.curr().get(DOC_NDOC));
		r.read(rec);

		int last_row = r.last_row();

		for (int i = r.first_row(); i <= last_row; i = r.succ_row(i))
		{
			const TRectype & rdoc = r.row(i);
			const TString & codart = rdoc.get(RDOC_CODART);

			if (codart.not_empty())
			{
				TToken_string row;

				row.add(rdoc.get(RDOC_NDOC));
				row.add(codart);
				row.add(data);
				row.add(rdoc.get(RDOC_PREZZO));
				row.add(rdoc.get(RDOC_QTA));
				row.add(rdoc.get(RDOC_SCONTO));
				row.add(num);
				row.add(tipo);
				row.add(rdoc.get(RDOC_NRIGA));
				el.add(row);
			}
		}
	}
	el.TArray::sort(sort_sheet);

	const int max_rows = ((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).max_rows_art();

	if (max_rows > 0)
	{
		const int items = el.items();
		TString80 last_cod;
		long last_doc = 0L;
		real last_price(ZERO);
		int count = 0;
		for (int i = 0 ; i < items; i++)
		{
			const TString & codart = el.row(i).get(1);
			long ndoc = el.row(i).get_long(0);
			real price(el.row(i).get(3));

			if (codart != last_cod)
			{
				last_cod = codart;
				last_doc = ndoc;
				last_price = price;
				count = 1;
			}
			else
			{
				if (ndoc == last_doc && price == last_price)
				{
					el.destroy(i);
				}
				else
				{
					last_doc = ndoc;
					last_price = price;
					count++;
					if (count > max_rows)
						el.destroy(i);
				}
			}
		}
		el.pack();
	}
	
	if (sheet.run() == K_ENTER)
	{              
		__in_handler = true;
		const TString & xx = sheet.row(-1).get(3);
		row_mask.set(FR_PREZZO, xx);
		const TString & codart_sel = sheet.row(-1).get(1);
		row_mask.set(FR_CODART, codart_sel, 3);
		row_mask.field(FR_CODART).set_dirty(false);
		const TString & prezzo = sheet.row(-1).get(3);
		row_mask.set(FR_PREZZO, prezzo);
		const TString & sconto = sheet.row(-1).get(5);
		row_mask.set(FR_SCONTO, sconto);
		__in_handler = false;
	}
}

void search_price_article(TMask_field& f, KEY key )
{
	TMask& row_mask = f.mask();   
  TSheet_field& sh = *row_mask.get_sheet();
  TDocumento_mask& mask = (TDocumento_mask &)sh.mask();
	const int current_doc_row = sh.selected() + 1;      
	const TString16 numdoc(mask.get(F_NDOC));
	const TString80 codart(row_mask.get(FR_CODART));
	TDate datadoc = mask.get_date(F_DATADOC);
	const char tipocf = mask.get(F_TIPOCF)[0];
	const TString16 codcf = mask.get(F_CODCF);

	TToken_string nums(((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_nums());

	if (nums.empty())
		nums =  mask.get(F_CODNUM);

	TString8 first_num(nums.get(0));
	TString8 last_num(first_num);;
	TString form;
	
	form << "(33->TIPOCF==\"" << tipocf << "\")&&(33->CODCF==\"" << (const char *)codcf << "\")&&(ANSI(33->DATADOC)<=\"" << datadoc.string(ANSI) << "\")" << "&&((CODNUM==\"" << first_num << "\")";
	for (const char * s = nums.get(); s && *s; s = nums.get()) 
	{
		if (first_num > s)
			first_num = s;
		if (last_num < s)
			last_num = s;
		form << "||(CODNUM==\"" << s << "\")";
	}
	form << ")";

	TRelation rel(LF_RIGHEDOC);
	TRectype from(rel.curr());
	TRectype to(rel.curr());
	rel.add(LF_DOC, "PROVV==PROVV|ANNO=ANNO|CODNUM==CODNUM|NDOC==NDOC");

	const int last_anno = mask.get_int(F_ANNO);
	const int first_anno = last_anno - ((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_years();

	from.put(RDOC_CODART, codart);
	from.put(RDOC_CODNUM, first_num);
	from.put(RDOC_ANNO, first_anno);
	from.put(RDOC_PROVV, "D");
	to.put(RDOC_CODART, codart);
	to.put(RDOC_CODNUM, last_num);
	to.put(RDOC_ANNO, last_anno);
	to.put(RDOC_PROVV, "D");
	TSorted_cursor cur(&rel, "33->DATADOC-|CODNUM|NDOC-", form, 5, &from, &to);
	cur.items();
	cur.freeze();
	cur.relation()->add("%NUM", "CODTAB==CODNUM", 1, 0, 201);
	cur.relation()->add("%TIP", "CODTAB==TIPODOC", 1, LF_DOC, 202);

	TCursor_sheet sheet(&cur, "NDOC|CODART|33->DATADOC|PREZZO|QTA|SCONTO|-201->S0|-202->S0",
											"Ricerca Prezzi",
											"N.ro doc.|Codice articolo@20|Data@10|Prezzo@18P|Quantit�|Sconto@10|Numerazione@18|Tipo Documento@18",
											0, 4);

	sheet.add_string (101, 0, "Codice Articolo ", 2, 1, 20, "D");
	sheet.add_string (102, 0, "Descrizione     ", 2, 3, 50, "D");
	sheet.set(101, codart);
	sheet.set(102, cache().get(LF_ANAMAG, codart, "DESCR"));
	if (sheet.run() == K_ENTER)
	{              
		__in_handler = true;
		row_mask.set(FR_CODART, codart, 3);
		row_mask.field(FR_CODART).set_dirty(false);
		__in_handler = false;
		const TString & prezzo = sheet.row(-1).get(3);
		row_mask.set(FR_PREZZO, prezzo);
		const TString & sconto = sheet.row(-1).get(5);
		row_mask.set(FR_SCONTO, sconto);
	}
}

bool search_price_handler(TMask_field& f, KEY key )
{
	TMask& row_mask = f.mask();   
	TSheet_field& sh = *row_mask.get_sheet();

	if (key == K_F8 && !sh.sheet_mask().is_running())
	{
		const TString & codart = row_mask.get(FR_CODART);
		if (codart.empty())
			search_price(f, key);
		else
			search_price_article(f,key);
		return true;
	}
	else
		if ((key == K_TAB && f.focusdirty()))
		{
		  TDocumento_mask& mask = (TDocumento_mask &)sh.mask();

			const TString80 codart(row_mask.get(FR_CODART));

			if (!__in_handler && codart.not_empty() && row_mask.get(FR_PREZZO).empty())
			{
				TDate datadoc = mask.get_date(F_DATADOC);
				const char tipocf = mask.get(F_TIPOCF)[0];
				const TString16 codcf = mask.get(F_CODCF);
				const int current_doc_row = sh.selected() + 1;      
			
				TToken_string nums(((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_nums());

				if (nums.empty())
					nums =  mask.get(F_CODNUM);

				TString8 first_num(nums.get(0));
				TString8 last_num(first_num);;
				
				TString form;
				form << "(33->TIPOCF==\"" << tipocf << "\")&&(33->CODCF==\"" << (const char *)codcf << "\")&&(ANSI(33->DATADOC)<=\"" << datadoc.string(ANSI) << "\")" << "&&((CODNUM==\"" << first_num << "\")";
				for (const char * s = nums.get(); s && *s; s = nums.get()) 
				{
					if (first_num > s)
						first_num = s;
					if (last_num < s)
						last_num = s;
					form << "||(CODNUM==\"" << s << "\")";
				}
				form << ")";
  
				TRelation rel(LF_RIGHEDOC);
				TRectype from(rel.curr());
				TRectype to(rel.curr());
				rel.add(LF_DOC, "PROVV==PROVV|ANNO=ANNO|CODNUM==CODNUM|NDOC==NDOC");

				const int last_anno = mask.get_int(F_ANNO);
				const int first_anno = last_anno - ((TTipo_riga_documento&)mask.doc()[current_doc_row].tipo()).search_years();

				from.put(RDOC_CODART, codart);
				from.put(RDOC_CODNUM, first_num);
				from.put(RDOC_ANNO, first_anno);
				from.put(RDOC_PROVV, "D");
				to.put(RDOC_CODART, codart);
				to.put(RDOC_CODNUM, last_num);
				to.put(RDOC_ANNO, last_anno);
				to.put(RDOC_PROVV, "D");
				
				TSorted_cursor cur(&rel, "33->DATADOC-|CODNUM|NDOC-", "", 5, &from, &to);
				cur.setfilter(form, true);
				if (cur.items() > 0L)
				{
					cur = 0L;
					__in_handler = true;
					row_mask.set(FR_CODART, codart, 3);
					row_mask.field(FR_CODART).set_dirty(false);
					__in_handler = false;
					const TString & prezzo = cur.curr().get(RDOC_PREZZO);
					row_mask.set(FR_PREZZO, prezzo);
					const TString & sconto = cur.curr().get(RDOC_SCONTO);
					row_mask.set(FR_SCONTO, sconto);
				}
			}
		}
	return codart_handler( f, key);
}


bool codart_handler(TMask_field& f, KEY key )
{
	TMask& row_mask = f.mask();
	TSheet_field& sh = *row_mask.get_sheet();
	TDocumento_mask& mask = (TDocumento_mask &)sh.mask();
	const int current_doc_row = sh.selected() + 1;

	if (f.to_check(key, true))
	{ 
		if (!f.empty())
			row_mask.enable(FR_LIV1);
		else
		{
			row_mask.reset(FR_LIV1);
			row_mask.disable(FR_LIV1);
		}		
		row_mask.field(FR_LIV1).on_hit();
	}
	
	if (key == K_TAB && (f.focusdirty() || row_mask.get(FR_CHECKED).empty()))
	{
		TCond_vendita & condv = mask.condv();


		condv.set_testa(&mask);
		condv.set_riga(&row_mask);


		TString80 codart = f.get();	 
		TRectype anamag = cache().get(LF_ANAMAG, codart);	// anamag puo' cambiare
		bool found = !anamag.empty();


		if (found)
			row_mask.set(FR_CODARTMAG, codart, true);
		else
		{
			TLocalisamfile codalt(LF_CODCORR);

			codalt.setkey(2);
			codalt.put(CODCORR_CODARTALT, codart);
			if (codalt.read() == NOERR)
			{
				codart = codalt.get(CODCORR_CODART);
				anamag = cache().get(LF_ANAMAG, codart);
				found = !anamag.empty();
				if (found)
					row_mask.set(FR_CODARTMAG, codart, true);
			}
		}
		row_mask.set(FR_CHECKED, "X");
		if (!found)
			row_mask.set(FR_CODARTMAG, "", true);
		else
		{
			const TString8 lingua = mask.get(F_CODLIN);
			const TCodice_articolo codart(row_mask.get(FR_CODARTMAG));
			TString desc(anamag.get("DESCR"));

			if (mask.doc()[current_doc_row].is_omaggio())	
			{
				static TString* dicitura_omaggio = NULL;
				if (dicitura_omaggio == NULL)
				{
					TConfig c(CONFIG_STUDIO, "ve");
					dicitura_omaggio = new TString(c.get("DESOMAGGI"));
					if (dicitura_omaggio->blank())
						*dicitura_omaggio = TR("(OMAGGIO)");
				}
				desc << ' ' << *dicitura_omaggio;
			}
			if (lingua.not_empty())
			{
				TLocalisamfile deslin(LF_DESLIN);


				deslin.setkey(2);
				deslin.put(DESLIN_CODART, codart);
				deslin.put(DESLIN_CODLIN, lingua);
				if (deslin.read() == NOERR)
				{
					static int __lingua_only = 3;
				
					if (__lingua_only > 2)
					{
						TConfig c(CONFIG_DITTA, "ve");
						__lingua_only = c.get_bool("LINGUA_ONLY");
					}
					if (__lingua_only)
						desc = deslin.get(DESLIN_DESCR);
					else
						desc << '\n' << deslin.get(DESLIN_DESCR);
				}
			}

			const TString& descest = anamag.get("DESCRAGG");
			if (descest.not_empty())
				desc << "\n" << descest;

			row_mask.set(FR_DESCR, desc);

			TString80 key; key << codart << "|1";
			const TRectype& umart = cache().get(LF_UMART, key);
			if (!umart.empty())
			{
				curr_um = umart.get(UMART_UM);
				curr_fc = umart.get_real(UMART_FC);
			}
			else
			{
				curr_um.cut(0);
				curr_fc = 1.0;
			}
			row_mask.set(FR_UMQTA, curr_um);
			upd_colli_peso_tara(row_mask, anamag);
		}

		condv.ricerca();
		mask.doc()[current_doc_row].put(RDOC_PREZZO, row_mask.get(FR_PREZZO));

		const int pos = row_mask.id2pos(FR_CODIVA);

		if (pos >= 0)
			iva_handler(row_mask.fld(pos), 0);

		if (found)
		{
			TString16 caus(row_mask.get(FR_CAUS));
				
			if (caus.empty())
				caus = mask.get(F_CAUSMAG);

			if (caus.not_empty())
			{
				const TCausale_magazzino c(caus);

				if (!c.movimenta_sospesi())
				{
					const TRectype & rec = cache().get(LF_ANAMAG, row_mask.get(FR_CODARTMAG));

					if (rec.get_bool("SOSPESO")) 
						return error_box(TR("Articolo %s sospeso, quindi non movimentabile"), (const char *)codart);
				}
			}
		}
	}
	else
		if (key == K_F8 && !sh.sheet_mask().is_running())
		{
			static int explode_db = 3;
			static bool valcomp = false;
			static bool matbase = true;		
			static TExplosion_grouping raggart = RAGGR_EXP_NONE;
			static bool elrorig = false;
			static int livello = 1;
			static int ordin = 0;
			
			if (explode_db > 2)
			{
				TConfig d(CONFIG_DITTA, "ve");
																	 
				explode_db = d.get_bool("EXPLODEDB", "ve");
				valcomp = d.get_bool("VALCOMP", "ve");
				matbase = d.get_bool("TIPOESPL", "ve");
				raggart = (TExplosion_grouping) d.get_int("RAGGART", "ve");
				livello = d.get_int("LIVESPL", "ve");
				elrorig = d.get_bool("ELRORIG", "ve");
				ordin = d.get_int("ORDDB", "ve");
			}
			if (explode_db && !sh.sheet_mask().is_running())
			{
				TDocumento & doc = mask.doc();
				TRiga_documento & curr_row = doc[current_doc_row];	

				sh.update_row(current_doc_row - 1);
				curr_row.autosave(sh);

				TDistinta_tree db;
				TArray components;

				db.set_root(curr_row);

				const int items = db.explode(components, matbase, raggart, livello, "A", ordin);
				if (items > 0)
				{ 
					TProgind pi(items, "Esplosione in corso...",false, true);
					int row = current_doc_row;
					const TString16 tiporiga(curr_row.tipo().codice());

					TString_array& str_arr = sh.rows_array();

					for (int i = components.first(); i < items; i = components.succ(i))
					{
						pi.addstatus(1L);
						TRiga_esplosione & r = (TRiga_esplosione &) components[i];
						TRiga_documento & new_row = doc.insert_row(row + 1, tiporiga);
						//sh.insert(row, false);								 
						str_arr.insert(new TToken_string, row);
						
						TDocumento::copy_data(new_row, curr_row);
						new_row.put(RDOC_CODART, r.articolo());
						new_row.put(RDOC_CODARTMAG, r.articolo());
						new_row.put(RDOC_LIVELLO, r.giacenza());
						new_row.zero(RDOC_DESCR);
						new_row.zero(RDOC_DESCLUNGA);
						new_row.zero(RDOC_DESCEST);
						new_row.put(RDOC_CHECKED, "");
						new_row.put(RDOC_UMQTA, r.um());
						new_row.put(RDOC_QTA, r.val() /* * qta_fin */);
						new_row.autoload(sh);
						sh.check_row(row);
						new_row.autosave(sh);				// Da sheet a rdoc
						if (!valcomp)
						{
							new_row.zero(RDOC_PREZZO);
							// new_row.autoload(sh);			// Da rdoc a shee
							sh.row(row).add("0", FR_PREZZO-FIRST_FIELD);
							row_mask.reset(FR_PREZZO);
						}
						row++;
					}
					if (elrorig)
					{
						doc.destroy_row(current_doc_row, true);
						sh.destroy(current_doc_row - 1, false);
						row--;
					}
					else
						if (valcomp)
						{
							curr_row.zero(RDOC_PREZZO);
							curr_row.autoload(sh);
						}
					sh.force_update();
					sh.select(row-1);
				}
			}
        mask.update_giacenza();
		}
	return true;
}

bool codartmag_handler( TMask_field& f, KEY key )
{
	TMask & m = f.mask(); 
	TSheet_field& s = *m.get_sheet();
	TDocumento_mask& mask= (TDocumento_mask&)s.mask();
 
	bool to_check = key == K_TAB && f.focusdirty();
	if (!to_check)
		to_check = !mask.is_running();

	if (to_check)
	{
		const TString& codart = f.get();
		const bool artmag = codart.not_empty();

		m.show(FR_UMQTA, artmag);
		m.show(FR_UMQTA2, !artmag);

		mask.update_giacenza();
	}
	return true;

}

bool liv_handler( TMask_field& f, KEY key )
{
	bool ok = true;
	TDocumento_mask & mask=(TDocumento_mask &) f.mask().get_sheet()->mask();

	if (key == K_TAB && f.focusdirty() && !f.get().empty()) 
	{
		const int levnum=f.dlg()-FR_LIV1+1;
		// Se e' abilitato l'autoinserimento del livello di giacenza...
		if (mask.livelli().autoinsert(levnum))
			mask.livelli().autoinsert(levnum, f);
	}

	if (key == K_ENTER && !f.get().empty()) 
	{
		static int checkgiac = 3;
		if (checkgiac > 2)
		{
			TConfig c(CONFIG_DITTA);
			checkgiac = c.get_bool("LIVPERART", "ve");
		}
		if (mask.is_running() && checkgiac)
		{
			TLocalisamfile fl(LF_MAG);
			fl.setkey(2);
			TRectype & r = fl.curr();
			TMask& row_mask = f.mask();
			TEsercizi_contabili esc;
			const int annoes = esc.date2esc(mask.get_date(F_DATADOC));

			r.put(MAG_ANNOES, annoes);
			TString16 codmag(row_mask.get(FR_CODMAG));

			codmag.left_just(3);
			codmag << row_mask.get(FR_CODDEP);
			r.put(MAG_CODMAG, codmag);
			const TString80 codart(row_mask.get(FR_CODART));

			r.put(MAG_CODART, codart);
			TString liv;

			for (int l = 0 ; l <= f.dlg() - FR_LIV1; l++)
				mask.doc().livelli().pack_grpcode(liv,row_mask.get(FR_LIV1+l),l+1);

			r.put(MAG_LIVELLO, liv);

			TRectype new_rec(r);
			if (fl.read() != NOERR)
			{
				ok = yesno_box("Il codice di giacenza %s non e' legato all' articolo. Devo legarlo", (const char *)f.get());
				if (ok) 
				{
					fl.setkey(1);
					r = new_rec;
					r.put("NRIGA", 999);
					int nriga = 1;
					if (fl.read(_isgteq) == NOERR)
						fl.prev();
					if (codart == fl.get("CODART"))
						nriga = fl.get_int("NRIGA")+1;
					new_rec.put("NRIGA", nriga);
					const int err = new_rec.write(fl);
					if (err != NOERR)
						ok = error_box("Non sono riuscito a legare il codice di giacenza. Errore %d", err);
				}
			}
		}
	} 
		
	if (f.to_check(key, true))
	{
		if (f.dlg() < FR_LIV4)
		{
			TMask& row_mask = f.mask();	 
			TMask_field & next = row_mask.field(f.dlg() + 1);
			if (f.get().not_empty())
				next.enable();
			else
			{
				next.reset();
				next.disable();
			}
			next.on_hit(); 
		}
		TDocumento_mask& mask=(TDocumento_mask&)f.mask().get_sheet()->mask();
		mask.update_giacenza();
	}
	return ok;
}


void set_curr_um(const TString & um)
{
	curr_um = um;
	curr_fc = -1.0;
}

bool umart_handler( TMask_field& f, KEY key )
{
  TMask& row_mask = f.mask( );               
  TSheet_field& sh = *row_mask.get_sheet();
  TDocumento_mask & mask = (TDocumento_mask &)sh.mask();
  // Se qualcuno cerca di modificare la maschera
  if ( key == K_TAB && f.focusdirty() && mask.is_running())
  { 
    const int current_doc_row = sh.selected() + 1;      

    TCond_vendita & condv = mask.condv();
    condv.set_riga(&row_mask);

    const TString& um = f.get();
    real fc(1.0);
    
    if (um.not_empty() && curr_um.not_empty() && um != curr_um)
    {           
      TLocalisamfile umart(LF_UMART);
      umart.setkey(2);                               
      umart.put(UMART_CODART, row_mask.get(FR_CODARTMAG));
      umart.put(UMART_UM, um);
      if (umart.read() == NOERR)
      {
        real qta(row_mask.get_real(FR_QTA));
        fc = umart.get_real(UMART_FC);
        if (curr_fc < ZERO)
        {
          umart.put(UMART_CODART, row_mask.get(FR_CODARTMAG));
          umart.put(UMART_UM, curr_um);
          if (umart.read() == NOERR)
            curr_fc = umart.get_real(UMART_FC);
          else
            curr_fc = 1.0;
        }
        qta *= curr_fc;          
        qta /= fc;
        qta.round(5);
        row_mask.set(FR_QTA, qta);
        qta = row_mask.get_real(FR_QTAEVASA);
        qta *= curr_fc;          
        qta /= fc;
        qta.round(5);
        row_mask.set(FR_QTAEVASA, qta);
      }
    }
    curr_um = um;
    curr_fc = fc;
    condv.ricerca(true);
    mask.doc()[current_doc_row].put(RDOC_PREZZO, row_mask.get(FR_PREZZO));  

    const int pos = row_mask.id2pos(FR_CODIVA);

    if (pos >= 0)
      iva_handler(row_mask.fld(pos), 0);
  }   
  return true;
}

bool um_handler( TMask_field& f, KEY key )
{
	// Se qualcuno cerca di modificare la maschera
	if ( key == K_TAB && f.focusdirty())
	{ 
		TMask& row_mask = f.mask( ); 
		TTable & ums	= (TTable &) ((TEdit_field &) f).browse()->cursor()->file();

		const TString16 um(f.get());
		real fc(1.0);

		if (um.not_empty() && curr_um.not_empty() && um != curr_um)
		{
			ums.put("CODTAB", curr_um);
			if (ums.read() == NOERR)
			{
				real qta(row_mask.get_real(FR_QTA));
				const TString16 umsrif0(ums.get("S7"));
				const real fc0 = ums.get_real("R10");
				if (um == umsrif0)
					fc = fc0;
				else
				{
					ums.put("CODTAB", um);
					if (ums.read() == NOERR)
					{
						const real fc1 = ums.get_real("R10");
						const TString16 umsrif1(ums.get("S7"));

						if (fc1 > ZERO)
						{
							if (curr_um == umsrif1)
								 fc = 1 / fc1;
							else
								 if (umsrif0 == ums.get("S7"))
									 fc = fc0 / ums.get_real("R10");
						}
					}
				}
				qta *= fc;
				qta.round(5);
				row_mask.set(FR_QTA, qta);
				qta = row_mask.get_real(FR_QTAEVASA);
				qta *= fc;
				qta.round(5);
				row_mask.set(FR_QTAEVASA, qta);
			}
		}
		curr_um = um;
		curr_fc = fc;
	}
	return true;

}

bool descr_handler( TMask_field& f, KEY key )
{
	if (key == K_TAB && f.focusdirty())
	{
		const TString s(f.get());
		if (s.find('\n') < 0)
		{
			TLocalisamfile & anamag = (TLocalisamfile &) ((TEdit_field &) f).browse()->cursor()->file();

			if (f.get() == anamag.get(ANAMAG_DESCR))
				f.mask().set(FR_CODART, anamag.get(ANAMAG_CODART), 3);
		}
	}
	return true;

}

bool qtaart_handler( TMask_field& f, KEY key )
{
	// Se qualcuno cerca di modificare la maschera
	if ( key == K_TAB && f.focusdirty())
	{ 
		TMask& row_mask = f.mask();
		TSheet_field& sh = *row_mask.get_sheet();
		TDocumento_mask& mask = (TDocumento_mask&) sh.mask();
		const int current_doc_row = sh.selected() + 1;

		TCond_vendita & condv = mask.condv();
		condv.set_riga(&row_mask);
		condv.ricerca(false, true);
		mask.doc()[current_doc_row].put(RDOC_PREZZO, row_mask.get(FR_PREZZO));
		const TString& codart = row_mask.get(FR_CODARTMAG);

		if (codart.not_empty())
		{
			const TRectype& anamag = cache().get(LF_ANAMAG, codart);
			if (!anamag.empty())
				upd_colli_peso_tara(row_mask, anamag);
		}

	}
	else
		if (key == K_ENTER && f.dirty())
		{
			TDocumento_mask& mask=(TDocumento_mask&)f.mask().get_sheet()->mask();
			TDocumento & doc = mask.doc();

			if ((doc.tipo().check_giac() || doc.tipo().check_disp()) &&
					main_app().has_module(MGAUT, CHK_DONGLE) && doc.tipo().mov_mag())
			{
				TSheet_field& sf = mask.sfield(F_SHEET);
				TToken_string& row = sf.row(sf.selected());			 
				const TString codart = row.get(sf.cid2index(FR_CODARTMAG));

				if (codart.not_empty())
				{
					TString16 codmag = row.get(sf.cid2index(FR_CODMAG));
					const TString16 coddep = row.get(sf.cid2index(FR_CODDEP));
					if (!coddep.blank())
						codmag << coddep;

					TString16 livello;
					for (int i = 0; i < 4; i++)
					{
						const char* liv = row.get(sf.cid2index(FR_LIV1+i));
						if (*liv > ' ')
							livello << liv;
						else
							break;
					}

					real qta(row.get(sf.cid2index(FR_QTA)));
					const TString16 caus(mask.get(F_CAUSMAG));
					TCausale_magazzino c(caus);	
					const long nmovmag = doc.get_long(DOC_MOVMAG); 


					if (nmovmag != 0)
					{
						TLocalisamfile rmovmag(LF_RMOVMAG); rmovmag.setkey(2);
						rmovmag.put(RMOVMAG_CODART, codart);
						rmovmag.put(RMOVMAG_LIVGIAC, livello);
						rmovmag.put(RMOVMAG_CODMAG, codmag);
						rmovmag.put(RMOVMAG_NUMREG, nmovmag);
						if (rmovmag.read() == NOERR)
							qta -= rmovmag.get_real(RMOVMAG_QUANT);
					}
					if (doc.tipo().check_giac())
					{
						real giac(mask.get(F_CURGIAC));
						if (giac < -(c.sgn(s_giac) * qta))
							warning_box("Attenzione giacenza negativa");
					}
					else
						if (doc.tipo().check_disp())		 
						{
							real disp(mask.get(F_CURDISP));
							if (disp < -(c.sgn(s_giac) * qta))
								warning_box("Attenzione disponibilita' negativa");
						}
				}
			}
		}
		return qta_handler(f, key);
	}


bool qta_handler( TMask_field& f, KEY key )
{
	// Se qualcuno cerca di modificare la maschera
	if ( key == K_TAB && f.focusdirty())
	{ 
		TMask& row_mask = f.mask( );
		const real qta_evasa = row_mask.get_real(FR_QTAEVASA);
		const real qta(f.get());

		if (qta_evasa > 0 && qta_evasa >= qta)
		{
			row_mask.set(FR_RIGAEVASA, "X");
			row_mask.disable(FR_RIGAEVASA);
		}
		else
			row_mask.enable(FR_RIGAEVASA);
	} 

	return true;

}

bool qta_evasa_handler( TMask_field& f, KEY key )
{
	TMask& row_mask = f.mask( );
	if (f.to_check(key) || (key == K_TAB && !row_mask.is_running()))
	{
		const real qta_evasa(f.get());
		const real qta = row_mask.get_real(FR_QTA);

		bool enable_evasa = true;
		if (qta_evasa > 0 && qta_evasa >= qta)
		{
			row_mask.set(FR_RIGAEVASA, "X");
			enable_evasa = false;
		}
		row_mask.enable(FR_RIGAEVASA, enable_evasa);
		if (!row_mask.is_running())
		{
			TSheet_field* sheet = row_mask.get_sheet();
			const int riga = sheet->selected();
			sheet->enable_cell(riga, sheet->cid2index(FR_RIGAEVASA), enable_evasa);
		}
	} 
	return true;

}

bool causmag_handler( TMask_field& f, KEY key )
{
  if (f.to_check(key))
  {
    TMask& row_mask = f.mask( );               
    TDocumento_mask& mask = (TDocumento_mask &) row_mask.get_sheet()->mask();
    TString4 causmag(f.get());
    if (causmag.blank())
      causmag = mask.get(F_CAUSMAG);
    if (causmag.full())
    {
      TCausale_magazzino c(causmag);
      if (c.has_default_mag() && row_mask.get(FR_CODMAG).empty())
      {
        row_mask.set(FR_CODMAG, c.default_mag(), true);
        row_mask.set(FR_CODDEP, "", true);
      }
      if (c.has_default_dep() && row_mask.get(FR_CODDEP).empty())
        row_mask.set(FR_CODDEP, c.default_dep(), true);  
        
      if (c.caus_collegata().full())
      {
        c = cache().get("%CAU", c.caus_collegata());
        if (c.has_default_mag() && row_mask.get(FR_CODMAGC).empty())
        {
          row_mask.set(FR_CODMAGC, c.default_mag(), true);
          row_mask.set(FR_CODDEPC, "", true);
        }
        if (c.has_default_dep() && row_mask.get(FR_CODDEPC).empty())
          row_mask.set(FR_CODDEPC, c.default_dep(), true); 
          
        static int copy_defmagc = -883;
        if (copy_defmagc < 0)
        {     
          TFilename fname; mask.doc().tipo().profile_name(fname); 
          TConfig c_tipo_documento(fname,"MAIN");
          copy_defmagc = c_tipo_documento.get_bool("DEFMAGXCOLL");
        }
        if (copy_defmagc!=0)
        {
          if (row_mask.get(FR_CODMAGC).empty())       
            row_mask.set(FR_CODMAGC, mask.stdmag(), true);
          if (row_mask.get(FR_CODDEPC).empty())
            row_mask.set(FR_CODDEPC, mask.stddep(), true);
        }
      }
      if (!c.movimenta_sospesi())
      { 
        const TRectype & rec = cache().get(LF_ANAMAG, row_mask.get(FR_CODARTMAG));
            
        if (rec.get_bool("SOSPESO")) 
          return error_box("Articolo %s sospeso, quindi non movimentabile", (const char *)rec.get(ANAMAG_CODART));
      }
    }
  } 
  return true;

}
	 
bool sppr_handler( TMask_field& f, KEY key )
{ 
	TMask& row_mask = f.mask();
	
	if (key == K_TAB && (f.focusdirty() || row_mask.get(FR_DESCR).empty()))
	{
		const TRectype& curr = ((TEdit_field&)f).browse()->cursor()->curr();
/*
    const TString& cod = curr.get("COD");
    char tiposp = RIGA_SPESEDOC;
		if (cod == "PRS")
			tiposp = RIGA_PRESTAZIONI; else
		if (cod == "RSS")
		  tiposp = RIGA_RISORSE; else
		if (cod == "ATR")
		  tiposp = RIGA_ATTREZZATURE;
  	const TSpesa_prest sp(row_mask.get(FR_CODART), tiposp);
*/
  	const TSpesa_prest sp(curr);
	
    const int pos = row_mask.id2pos(FR_PREZZO);
		if (pos >= 0 && !sp.empty())
		{
      const char tipo = sp.tipo();
			const bool qta_val_fl = tipo == 'Q';
			const bool perc_fl = tipo == 'P';
			int pos = row_mask.id2pos(FR_UMQTASP);

			if (pos >= 0)
				row_mask.fld(pos).enable(!perc_fl);
			pos = row_mask.id2pos(FR_PREZZO);
			if (pos >= 0)
				row_mask.fld(pos).enable(!perc_fl);
			pos = row_mask.id2pos(FR_SCONTO);
			if (pos >= 0)
				row_mask.fld(pos).enable(!perc_fl);
			pos = row_mask.id2pos(FR_QTA);
			if (pos >= 0)
			{
				row_mask.fld(pos).show(!perc_fl);
				row_mask.fld(pos).enable(qta_val_fl);
			}
			pos = row_mask.id2pos(FR_PERCSP);
			if (pos >= 0)
			{
				row_mask.fld(pos).show(perc_fl);
				row_mask.fld(pos).enable(perc_fl);
			}
			if (!perc_fl)
			{
        const TMask& mask = row_mask.get_sheet()->mask();
				const real cambio = mask.get_real(F_CAMBIO);
				real prezzo = sp.prezzo();
				const TString& doc_valuta = mask.get(F_CODVAL);
				const bool controeuro = mask.get_bool(F_CONTROEURO);

				sppr_calc(sp, doc_valuta, cambio, prezzo, controeuro ?	_exchange_contro : _exchange_base);
				row_mask.set(FR_PREZZO, prezzo);					
			}
		}

    const int posiva = row_mask.id2pos(FR_CODIVA);
		if (posiva >= 0)
			iva_handler(row_mask.fld(posiva), 0);

		if (row_mask.id2pos(FR_CDC1) >= 0)
		{
			const TString80 cdc(sp.cdc());
			const TString80 cms(sp.cms());
			const TString80 fase(sp.fase());

			for (short i = FR_CDC1; i <= FR_CDC12; i++)
			{
				const pos = row_mask.id2pos(i);
				if (pos >=0)
				{
					TEdit_field& e = (TEdit_field&)row_mask.fld(pos);
					const TFieldref& fr = *e.field();

					if (fr.name() == RDOC_CODCOSTO)
						e.set(cdc.sub(fr.from(), fr.to()));
					else
						if (fr.name() == RDOC_CODCMS)
							e.set(cms.sub(fr.from(), fr.to()));
						else
							if (fr.name() == RDOC_FASCMS)
								e.set(fase.sub(fr.from(), fr.to()));
				}
			}					
		}
	} 
	return true;

}

bool TDocumento_mask::numdocrif_search_handler(TMask_field& f, KEY key)
{
	if (key == K_F9)
	{
		TMask& m = f.mask();

		TRectype filtrec(LF_DOC);

		filtrec.put(DOC_TIPOCF, m.get(F_TIPOCF));
		filtrec.put(DOC_CODCF, m.get(F_CODCF));

		const long codcf = atol(m.get(F_CODCF));

		if (codcf != 0)
			filtrec.put(DOC_TIPOCF, m.get(F_TIPOCF));

		TRelation rel(LF_DOC);
		rel.add(LF_CLIFO, "TIPOCF==TIPOCF|CODCF==CODCF");
		TSorted_cursor cur(&rel, "TIPOCF|CODCF|PROVV|ANNO|CODNUM|NUMDOCRIF", "", 2, &filtrec, &filtrec);
		TString80 f; f.format("(CODNUM==\"%s\") && (PROVV==\"%s\") && (ANNO==\"%s\")",
													(const char *)m.get(F_CODNUM), (const char *)m.get(F_PROVV), (const char *)m.get(F_ANNO));

		cur.setfilter(f);
		TCursor_sheet sheet(&cur, "CODCF|20->RAGSOC|ANNO|CODNUM|NUMDOCRIF|DATADOCRIF|DOC1|DOC2|DOC3|NDOC|DATADOC|CODVAL|G1:TOTDOC",
												"Documento di riferimento",
												"Codice|Ragione Sociale@50|Anno|Numeraz|Docum.Rif.|Data@10|Docum.Rif. 1|Docum.Rif. 2|Docum.Rif. 3|Documento|Data\nDocumento@10|Valuta|Totale\nDocumento@18V",
												0, 1);
		if (sheet.run() == K_ENTER)
		{
			const TString16 ndoc = sheet.row(-1).get(9);
			m.set(F_NDOC, ndoc);
			m.stop_run(K_AUTO_ENTER);
		}
	}
	return true;

}

bool TDocumento_mask::ragsoc_search_handler(TMask_field& f, KEY key)
{
	if (key == K_F9)
	{
		TMask& m = f.mask();

		TRectype filtrec(LF_DOC);

		filtrec.put(DOC_CODNUM, m.get(F_CODNUM));
		filtrec.put(DOC_PROVV, m.get(F_PROVV));
		filtrec.put(DOC_ANNO, m.get(F_ANNO));

		TRelation rel(LF_DOC);
		rel.add(LF_CLIFO, "TIPOCF==TIPOCF|CODCF==CODCF");
		TSorted_cursor cur(&rel, "TIPOCF|UPPER(20->RAGSOC)|PROVV|ANNO|CODNUM|PROVV|NDOC", "", 1, &filtrec, &filtrec);
		TString80 form; form.format("CODNUM==\"%s\"", (const char *)m.get(F_CODNUM));

		cur.setfilter(form, true);

		TCursor_sheet sheet(&cur, "20->RAGSOC|CODCF|20->PAIV|ANNO|CODNUM|NDOC|DATADOC|CODVAL|G1:TOTDOC",
												"Ragione Sociale",
												"Ragione Sociale@50|Codice|Partita IVA|Anno|Numeraz|Documento|Data\nDocumento@10|Valuta|Totale\nDocumento@18V",
												0, 1);

		TString ragsoc(f.get());

		rel.zero();
		rel.curr().put(DOC_TIPOCF, "C");
		ragsoc.upper();
		rel.curr(LF_CLIFO).put(CLI_RAGSOC, ragsoc);
		
		sheet.cursor()->read();
		if (sheet.run() == K_ENTER)
		{
			const TString16 ndoc = sheet.row(-1).get(5);
			m.set(F_NDOC, ndoc);
			m.stop_run(K_AUTO_ENTER);
		}
	}
	return true;
}

bool TDocumento_mask::datadocrif_handler(TMask_field& f, KEY key)
{
	if (key == K_ENTER && f.empty())
	{
		const TDocumento_mask& m = (const TDocumento_mask&)f.mask();
		if (m.get_bool(F_RAGGREFF))
		{
			const TDocumento& doc = m.doc();
			if (doc.is_nota_credito())
				return f.error_box("E' necessario specificare data e numero documento di riferimento\n"
													 "quando si desidera generare effetti raggruppati");
		}
	}
	return true;
}

bool link_handler( TMask_field& f, KEY key )
{
	if (key == K_SPACE)
	{
		const TSheet_field & sf = *f.mask().get_sheet();
		const TDocumento_mask & m = (const TDocumento_mask & )sf.mask();
		const TDocumento & d = m.doc();
		const int nrow = sf.selected() + 1;
		const TRiga_documento & r = d[nrow];
		r.edit(LF_DOC, "DAPROVV|DAANNO|DACODNUM|DANDOC");
	}
	return true;

}

bool ca_mag_handler(TMask_field& f, bool cdc = true)
{
	TMask& row_mask = f.mask();
	const TSheet_field & sf = *f.mask().get_sheet();
	const TDocumento_mask & mask = (TDocumento_mask & )sf.mask();
	short i, first_fld, last_fld;
	const int lffile = cdc ? LF_CDC : LF_COMMESSE;
	const TMultilevel_code_info & info = ca_multilevel_code_info(lffile);
	TString80 cod;

	last_fld = f.dlg();
	first_fld = last_fld - info.levels() + 1;
	for (i = first_fld; i <= last_fld; i++)
		cod << row_mask.get(i);
	if (cod.blank())
	{
		cod.cut(0);
		last_fld = cdc ? mask.last_cdc_dlg() : mask.last_cms_dlg();
		first_fld = last_fld - info.levels() + 1;

		for (i = first_fld; i <= last_fld; i++)
			cod << mask.get(i);
	}

	const TRectype & rec = cache().get(lffile, cod);
	const TString16 codmag = rec.get("CODMAG");

	if (codmag.not_empty())
	{
		row_mask.set(FR_CODMAG, codmag.left(3), true);
		row_mask.set(FR_CODDEP, codmag.mid(3), true);
	}
	return true;
}

bool cms_mag_handler(TMask_field& f, KEY key)
{
	if ((key == K_TAB && f.focusdirty()) || key == K_ENTER)
		ca_mag_handler(f, false);
	return true;

}

bool cdc_mag_handler(TMask_field& f, KEY key)
{
	if ((key == K_TAB && f.focusdirty()) || key == K_ENTER)
		ca_mag_handler(f);
	return true;

}