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

#ifndef __APPLICAT_H
#include <applicat.h>
#endif

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

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

#ifndef __TABUTIL_H
#include <tabutil.h>
#endif

#ifndef __UTILITY_H
#include <utility.h>
#endif

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

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

TDocumento_mask::TDocumento_mask(const char* name, TDocumento * doc, TRelation * rel,
                                 int num, int max)
               : TVariable_mask(name, num, max), _progs_page(-1), _last_prog(-1),
                 _doc(doc), _rel(rel)
{                                                              
  const int pos = id2pos(BASE_PIEDE + 1);
  
  if (pos >= 0)
  {
    _progs_page = find_parent_page(fld(pos));
    _last_prog = 0;
    while (id2pos(BASE_PIEDE + _last_prog + 1) >= 0)
      _last_prog++;
  }
}

void TDocumento_mask::next_page(int p)
{     
  TMask::next_page(p);
  if (curr_page() == _progs_page)
  {                    
    begin_wait();                         
    TDocumento & curr_doc = doc();
    TDocumento & rel_doc = (TDocumento &) _rel->curr();
    
    rel_doc = curr_doc;
    autosave(*_rel);
    for (int i = _last_prog; i > 0; i--)
    {
      const short id = BASE_PIEDE + i;
      const TString16 name(field(id).field()->name());
      
      set(id , rel_doc.get(name)); 
    }
    end_wait();
  }
}

bool TDocumento_mask::on_key(KEY key)
{ 
  if (key == K_SHIFT + K_F12)
  {
    field(F_STATO).enable(!field(F_STATO).enabled());
    return TRUE;
  }
  return TVariable_mask::on_key(key); 
}

void TDocumento_mask::occ2mask(TOccasionale & o)
{  
  reset(F_COFI);
  reset(F_PAIVA);
  set(F_OCFPI, o.get(OCC_CFPI));   
  const TString & ragsoc = o.get(OCC_RAGSOC);
  set(F_RAGSOC, ragsoc);
  set(F_RAGSOCSP, ragsoc);
  const TString & indir = o.get(OCC_INDIR);
  set(F_INDCF, indir);
  set(F_INDSP, indir);
  const TString & civ = o.get(OCC_CIV);
  set(F_CIVCF, civ);
  set(F_CIVSP, civ);
  const TString & cap = o.get(OCC_CAP);
  set(F_CAPCF, cap);
  set(F_CAPSP, cap);
  const TString & com = o.get(OCC_COM);
  set(F_COMCF, com);    
  set( F_COMSP, com);
  const TString & stato = o.get(OCC_STATO);
  set(F_STATOCF, stato);  
  set( F_STATOSP, stato);
  if (id2pos(F_CODINDSP) >= 0)
    reset(F_CODINDSP);
  // Sugli occasionali non c'� la localit�
  const TString & localita = o.get(OCC_LOCALITA);
  set(F_LOCALITACF, localita);
  set(F_LOCALITASP, localita);
  check_field(F_COMSP);
  check_field(F_STATOSP);
}

void TDocumento_mask::cli2mask( TCli_for & c, TOccasionale & o, TConfig & ditta)
{  
  const bool onload = !is_running();
  const TString16 codval(get(F_CODVAL));
  const TString16 datacambio(get(F_DATACAMBIO1));
  
  // Setta i campi che appartengono al file LF_CLIFO  
  const bool occas = c.occasionale();

  show(F_OCCASEDIT, occas);
  if (!occas)
    reset(F_OCFPI);
  show(F_OCFPI, occas);
  // In forse per l'Occasionale, sicuri per il Normale
  show(F_COFI, !occas);
  show(F_STATOPAIVA, !occas);
  show(F_PAIVA, !occas);    
  set(F_RAGSOC, c.get(CLI_RAGSOC));
  enable(F_RAGSOC, !occas);
  
  const TRectype & ven_rec = c.vendite();

  if(!onload)    
  {                       
    short pos = id2pos(F_CODVAL);                
    const TString & codval = c.get(CLI_CODVAL);
    
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(codval);
    pos = id2pos(F_CODVAL1);              
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(codval);
    pos = id2pos(F_CODLIN);              
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(c.get(CLI_CODLIN));
    set(F_CODPAG, c.get(CLI_CODPAG));
    set(F_CODABIA, c.get(CLI_CODABI));
    set(F_CODCABA, c.get(CLI_CODCAB));
    // Setta i campi che appartengono al file LF_CFVEN
    set(F_CODABIP, ven_rec.get(CFV_CODABIPR));
    set(F_CODCABP, ven_rec.get(CFV_CODCABPR));
    set(F_RAGGR, ven_rec.get(CFV_RAGGDOC));
    set(F_CODINDSP, ven_rec.get(CFV_CODINDSP));
    set(F_CODAG, ven_rec.get(CFV_CODAG));
    set(F_CODSPMEZZO, ven_rec.get(CFV_CODSPMEZZO));
    set(F_CODPORTO, ven_rec.get(CFV_CODPORTO));
    set(F_CODNOTESP1, ven_rec.get(CFV_CODNOTESP1));
    set(F_CODNOTESP2, ven_rec.get(CFV_CODNOTESP2));
    set(F_CODNOTE, ven_rec.get(CFV_CODNOTE));
    set(F_CODVETT1, ven_rec.get(CFV_CODVETT1));
    set(F_CODVETT2, ven_rec.get(CFV_CODVETT2));
    set(F_CODVETT3, ven_rec.get(CFV_CODVETT3));
    set(F_SPESEINC, ven_rec.get(CFV_PERCSPINC));  
    set(F_ADDBOLLI, ven_rec.get(CFV_ADDBOLLI));
    set(F_CATVEN, ven_rec.get(CFV_CATVEN));
    pos = id2pos(F_CODLIST);              
    const TString & codlist = ven_rec.get(CFV_CODLIST);
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(codlist);
    pos = id2pos(F_CODLIST1);              
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(codlist);
    pos = id2pos(F_CODAG);              
    if (pos >= 0 && fld(pos).active())
      fld(pos).set(ven_rec.get(CFV_CODAG));
    set(F_CODZON, ven_rec.get(CFV_CODZONA));
    set(F_RAGGR, ven_rec.get(CFV_RAGGDOC));
    sconto_testa2mask(c, ditta);
    spese2mask(c);
  }
  enable( F_CODINDSP , !occas);      
  if (occas) 
    occ2mask(o);
  else           
  {        
    set(F_COFI, c.get(CLI_COFI));
    set(F_INDCF, c.get(CLI_INDCF));
    set(F_CIVCF, c.get(CLI_CIVCF));
    set(F_STATOPAIVA, c.get(CLI_STATOPAIV));
    set(F_PAIVA, c.get(CLI_PAIV));
    set(F_LOCALITACF, c.get(CLI_LOCCF));
    set(F_CAPCF, c.get(CLI_CAPCF));
    set(F_COMCF, c.get(CLI_COMCF));
    set(F_STATOCF, c.get(CLI_STATOCF));
    check_field( F_CODINDSP );
  }
  const TString16 newcodval(get(F_CODVAL));
  const TString16 newdatacambio(get(F_DATACAMBIO1));

  short pos = id2pos(F_CAMBIO);                           
  if ((pos >= 0) && (newcodval.empty() || newdatacambio.empty()))
    fld(pos).reset();  
  pos = id2pos(F_CODVAL);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  pos = id2pos(F_CODVAL1);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  pos = id2pos(F_DATACAMBIO1);                           
  if (pos >= 0 && fld(pos).active() &&
     (codval != newcodval || datacambio != newdatacambio))
    fld(pos).check();
  check_field(F_COMCF);
  check_field(F_STATOCF);
  check_field( F_CODPAG );
  pos = id2pos(F_CODLIN);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  check_field( F_CODCABA );
  pos = id2pos(F_CODLIST);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  pos = id2pos(F_CODLIST1);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  check_field( F_CODCABP );
  pos = id2pos(F_CODAG);                           
  if (pos >= 0 && fld(pos).active())
    fld(pos).check();
  check_field( F_CODSPMEZZO );
  check_field( F_CODPORTO );
  check_field( F_CODNOTESP1 );
  check_field( F_CODNOTESP2 );
  check_field( F_CODNOTE );
  check_field( F_CODVETT1 );
  check_field( F_CODVETT2 );
  check_field( F_CODVETT3 );    
  check_field( F_CATVEN );    
  check_field( F_CODZON );
  const bool gescontr      = ditta.get_bool("GES", "ve", 2);          
  const bool contr_enabled = ven_rec.get_bool(CFV_GESTCONTR);
  
  enable(F_CODCONT1, contr_enabled && gescontr);
  enable(F_CODCONT, contr_enabled && gescontr);
}

void TDocumento_mask::sconto_testa2mask( TCli_for & c, TConfig & ditta )
{
  const char tipogestione = ditta.get("GESSCO", "ve")[ 0 ];
  switch( tipogestione )
  {
    case 'N':  // Sconti non gestiti: pussa via!
      break;
    case 'P':  // Percentuale su anagrafica cliente
      set( F_SCONTOPERC, c.vendite().get(CFV_SCONTO));
      break;
    case 'T':  // Gestione tabella sconti
      {
        const TString16 codsconto(c.vendite().get(CFV_CODSCC));
        
        if (codsconto.not_empty())
        {
          TTable sconti("%SCC");
          sconti.setkey(1);
          sconti.zero();
          sconti.put("CODTAB", codsconto);
          if (sconti.read( ) == NOERR)
            set(F_SCONTOPERC, sconti.get("S1"));
        }
      }
      break;
    case 'A':  // Gestione archivio sconti
      {
        TString16 cod;               
        const TRectype & ven_rec = c.vendite();
        TLocalisamfile sconti(LF_SCONTI );
                                     
        sconti.setkey(1);
        sconti.zero();
        sconti.put("TIPO", "I");
        if(ditta.get_bool("SCOKEY", "ve", 1))
          sconti.put("CODCAT", ven_rec.get(CFV_CATVEN));
        if(ditta.get_bool("SCOKEY", "ve", 2))
          cod.format("%-2s", (const char *)ven_rec.get(CFV_CODSCC));
        else
          cod = "  ";
        if( ditta.get_bool("SCOKEY", "ve", 3))
          cod << format("%-2s", (const char *)ven_rec.get(CFV_CODZONA));
        else
          cod << "  ";
        if( ditta.get_bool("SCOKEY", "ve", 4))
          cod << c.get(CLI_CODPAG);
        sconti.put("CODART", cod);
        if(sconti.read() == NOERR)
          set(F_SCONTOPERC, sconti.get( "SCONTO"));
      }
      break;    
    default:
      break;  
  }  
  return;
}

void TDocumento_mask::spese2mask(TCli_for & c)
{
  TSheet_field & sh = (TSheet_field &) field(F_SHEET);
  
  TString16 name("CODSP0");
  TString_array spese;        
  TRectype & ven_rec = c.vendite();
  for (int i = 1; i <= 4; i++)
  {                     
    name.rtrim(1); name << i;
    const TString16 s(ven_rec.get(name));
    
    if (s.not_empty()) 
      spese.add(s);
  }               
//  autosave(*_rel);   
  doc().put("SPESEUPD", FALSE);
  doc().update_spese_aut(spese, FALSE, &sh);
  sh.force_update();
}