// Handler di MarcoclaD sui campi delle maschere

#ifndef __CHECKS_H
#include <checks.h>
#endif

#ifndef __MASK_H
#include <mask.h>
#endif

#ifndef __MASKFLD_H
#include <maskfld.h>
#endif

#ifndef __KEYS_H
#include <keys.h>
#endif

#ifndef __STRINGS_H
#include <strings.h>
#endif

#ifndef __VEUML_H
#include "veuml.h"
#endif

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

#ifndef __VE0100_H
#include "ve0100.h"
#endif

#ifndef __VE0100C_H
#include "ve0100c.h"
#endif

#ifndef __PAGAMENT_H
#include "..\cg\pagament.h"
#endif

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

bool ora_hndl( TMask_field& field, KEY key )

{
  if (field.to_check(key))
  {
    TFixed_string ora( field.get( ), 6 );
    
    ora.trim( );
    if (ora.not_empty() || field.required() )
    {
      if ( isdigit( ora[ 0 ] ) )
      {
        if ( ora[ 2 ] != ':')
        {
          if ( ora.len( ) > 4 )
            ora.overwrite( ":", 2 );
          else
            ora.insert( ":", 2 );            
        }
      }
      const bool ok = ((isdigit(ora[0]))&&(isdigit(ora[1]))&&(isdigit(ora[3]))&&(isdigit(ora[4]))) &&
                      ((atoi(&(ora[0]))<24)&&(atoi(&(ora[3]))<60));
      if (ok )
        field.set((ora));          
      else  
        return error_box("Ora errata o formato non valido");
    }
  }
  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 )
{                
  if ( field.to_check(key))
  {
    TMask& m = field.mask( );
    TString16 condpag( m.get( F_CODPAG ) );
    TString16 data( m.get( F_DATAINSC ) );
    if ( condpag.not_empty() && data.not_empty())
    {
      TPagamento pag( condpag, data );
      pag.set_total( 100, 10, 10 );
      pag.set_rate_auto( );      
      const int numrate = pag.n_rate( );
      for( int i = 0; i < numrate; i ++ )
        m.set( F_DATASCAD1 + i, pag.data_rata(i).string());
      for( ; i < 5; i ++ )
        m.hide( F_DATASCAD1 + i );
    }
  }
  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, dummy_hndl );
      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 codart_handler( TMask_field& f, KEY key )
{
  // Se qualcuno cerca di modificare la maschera
  if ( key == K_TAB && f.focusdirty())
  {            
    TMask& row_mask = f.mask();               
//    TMask & mask = app().edit_mask();
    TMask & mask = row_mask.get_sheet()->mask();
    TCond_vendita & condv = app().condv();
    
    condv.set_testa(&mask);
    condv.set_riga(&row_mask);

    TLocalisamfile & anamag = ((TEdit_field &) f).browse()->cursor()->file();
    TLocalisamfile & umart  = ((TEdit_field &) row_mask.field(FR_UMQTA)).browse()->cursor()->file();
    
    condv.set_anamag(anamag);
    condv.set_umart(umart);
    TString80 codart(f.get());   
    anamag.setkey(1);
    anamag.put("CODART", codart);
    if (anamag.read() != NOERR)
    {
      TLocalisamfile codalt(LF_CODCORR);
      
      codalt.setkey(2);
      codalt.put("CODARTALT", codart);
      if (codalt.read() == NOERR)
      {                              
        codart = codalt.get("CODART");
        anamag.zero();
        anamag.put("CODART", codart);
        if (anamag.read() == NOERR)
          f.set(codart);
      }
    }   

    if (anamag.good())
    {
      const TString16 lingua = mask.get(F_CODLIN);                              
      const TString codart(row_mask.get(FR_CODART));
      TString desc(anamag.get("DESCR"));
      
      if (lingua.not_empty())
      {
        TLocalisamfile deslin(LF_DESLIN);
        
        deslin.setkey(2);
        deslin.put("CODART", codart);
        deslin.put("CODLIN", lingua);
        if (deslin.read() == NOERR)
          desc = deslin.get("DESCR");
      }
      row_mask.set(FR_DESCR, desc);
                                     
      umart.setkey(1);              
      umart.zero(); 
      umart.put("CODART", codart);
      if (umart.read(_isgteq) == NOERR && codart == umart.get("CODART"))
      {
        curr_um = umart.get("UM");       
        curr_fc = umart.get_real("FC");
      }
      else
      {
        curr_um.cut(0);                  
        curr_fc = 1.0;
      }
      row_mask.set(FR_UMQTA, curr_um);
    }

    condv.ricerca();
  }
  return TRUE;
}

bool umart_handler( TMask_field& f, KEY key )
{
  // Se qualcuno cerca di modificare la maschera
  if ( key == K_TAB && f.focusdirty())
  { 
    TMask& row_mask = f.mask( );               
//    TMask & mask = app().edit_mask();
    TMask & mask = row_mask.get_sheet()->mask();
    TLocalisamfile & anamag = ((TEdit_field &) row_mask.field(FR_CODART)).browse()->cursor()->file();
    TLocalisamfile & umart  = ((TEdit_field &) f).browse()->cursor()->file();
    TCond_vendita & condv = app().condv();
    
    condv.set_testa(&mask);
    condv.set_riga(&row_mask);
    condv.set_anamag(anamag);
    condv.set_umart(umart);
    condv.ricerca(FALSE);
  
    const TString16 um(f.get());
    real fc(1.0);
    
    if (um.not_empty() && curr_um.not_empty() && um != curr_um)
    {           
      umart.setkey(2);                               
      umart.put("CODART", row_mask.get(FR_CODART));
      umart.put("UM", um);
      if (umart.read() == NOERR)
      {
        real qta(row_mask.get_real(FR_QTA));
        qta *= curr_fc;          
        fc = umart.get_real("FC");
        qta /= fc;
        row_mask.set(FR_QTA, 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 = ((TEdit_field &) f).browse()->cursor()->file();
      
      anamag.zero();
      anamag.setkey(2);
      anamag.put("DESCR", ((TZoom_field &) f).get_first_line());
      if (anamag.read() == NOERR)
      {                         
        f.mask().set(FR_CODART, anamag.get("CODART"));
        f.mask().field(FR_CODART).set_dirty();
        f.mask().check_field(FR_CODART);
      }
    }
  }       
  return TRUE;
}

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( );               
//    TMask & mask = app().edit_mask();
    TMask & mask = row_mask.get_sheet()->mask();
    TLocalisamfile & anamag = ((TEdit_field &) row_mask.field(FR_CODART)).browse()->cursor()->file();
    TLocalisamfile & umart  = ((TEdit_field &) row_mask.field(FR_UMQTA)).browse()->cursor()->file();
    TCond_vendita & condv = app().condv();
    
    condv.set_testa(&mask);
    condv.set_riga(&row_mask);
    condv.set_anamag(anamag);
    condv.set_umart(umart);
    condv.ricerca(FALSE);
  } 
  return TRUE;
}

bool cod_handler( TMask_field& f, KEY key )
{
  if ( key == K_TAB && f.focusdirty())
  {                  
    TMask& row_mask = f.mask( );               
    TMask & mask = row_mask.get_sheet()->mask();
    real cambio = mask.get(F_CAMBIO);
                               
    if (cambio != 0.0)
    {                           
      real prezzo = row_mask.get(FR_PREZZO);
      
      prezzo /= mask.get_real(F_CAMBIO);
      row_mask.set(FR_PREZZO, prezzo);
    }
  } 
  return TRUE;
}