#include <diction.h>
#include <modaut.h>

#include "mglib.h"
#include "mg3frm.h"
#include "mg3msk.h"
#include "../cg/cglib01.h"

void TForm_stampemg::add_giaclev(TString &levname, int from, int to)
{
  to=(to==0 ? (_tolivgiac ? _tolivgiac:livelli_giacenza().last_level()) : to);
  int lv=(from==0 ? 1: from );
  for (; lv <to ; lv++) 
  {
    const TString& piece = livelli_giacenza().unpack_grpcode(relation()->curr(LF_MAG).get("LIVELLO"),lv);
    if (piece.not_empty()) 
      levname << '/' << piece ;
  } 
}


void TForm_stampemg::gruppogiac(TForm_item &cf, TToken_string &s)
{
  TString valore,levname;
  int livello=atoi(s.get());

  if (_fromlivgiac && livello == _fromlivgiac) 
  {
    levname << livelli_giacenza().name(livello) << ' ' ;
  }
  valore=livelli_giacenza().group_descr_packed(relation()->curr(LF_MAG).get("LIVELLO"),livello);
  if (valore.empty())
    valore = UNKNOWN_NAME;
  cf.set(levname << valore);
}


void TForm_stampemg::gruppoart(TForm_item &cf, TToken_string &s)
{
  int lv=atoi(s.get());
  TToken_string valore;

  valore=livelli_articolo().group_descr_packed(relation()->curr(LF_ANAMAG).get("CODART"),lv);
  if (valore.empty())
  {
    valore.separator(',');
    valore << "_USER,CODGRUPPOART," << lv;
    validate(cf,valore);
    valore.cut(0);
    valore << '"' << cf.get() << '"';
  }
  cf.set(valore);
}

void TForm_stampemg::codgruppogiac(TForm_item &cf, TToken_string &s)
{
  TString80 valore, levname;
  int livello=atoi(s.get());
  if (_fromlivgiac && livello == _fromlivgiac) 
  {
    levname=relation()->curr(LF_MAG).get("CODART");
    add_giaclev(levname,1,_fromlivgiac);
    levname << ' ';
  } else {
    levname << livelli_giacenza().name(livello) << ' ' ;
  }
  valore=livelli_giacenza().unpack_grpcode(relation()->curr(LF_MAG).get("LIVELLO"),livello);
  if (valore.empty())
    valore = UNKNOWN_NAME;
  cf.set(levname << valore);
}

// Flag: 0 = ORDF-ORDC, 1 = ORDF, 2 = ORDC, 3 = ORDF+ORDC 
real TForm_stampemg::ordinato(int flag) const
{  
  real ordc, ordf;

  TArticolo_giacenza ag(relation()->curr(LF_MAG));
  const TString4 annoes = ag.get(MAG_ANNOES);
  const TString8 codmag = ag.get(MAG_CODMAG);
  const TString16 livello = ag.get(MAG_LIVELLO);

  TRecord_array& rmag = ag.mag(annoes);
  for (int i = ag.find_mag(annoes, codmag, livello); i > 0; 
    i = ag.find_mag(annoes, codmag, livello, i)) 
  {                                    
    const TRectype & rec = rmag.row(i);
    ordf += rec.get_real(MAG_ORDF);
    ordc += rec.get_real(MAG_ORDC);
  }                        
  if (!riporta_ordinato())      
  {                         
    TEsercizi_contabili ese;
    const int prev = ese.pred(atoi(annoes));     // Controllo anche l'anno precedente
    if (prev > 0)                        
    {
      TString16 oldannoes;  oldannoes.format("%04d", prev);
      const TRecord_array& oldrmag = ag.mag(oldannoes);
      for (int i = ag.find_mag(oldannoes, codmag, livello); i > 0; 
        i = ag.find_mag(oldannoes, codmag, livello, i)) 
      {                                    
        const TRectype& rec = oldrmag.row(i);
        ordf += rec.get_real(MAG_ORDF);
        ordc += rec.get_real(MAG_ORDC);
      }
    }
  }

  real val; 
  switch (flag & 0x3)
  { 
  case  1: val = ordf; break;
  case  2: val = ordc; break;
  case  3: val = ordf + ordc; break;
  default: val = ordf - ordc; break;
  };
  return val;
}

bool TForm_stampemg::validate(TForm_item &cf, TToken_string &s)
{ 
  const TFixed_string code = s.get(0); // prende il primo parametro, il codice del messaggio

  if (code=="_USER") 
  {
    const TFixed_string subcode = s.get();
    TString valore,levname;

    if (subcode=="GRUPPOGIAC") {
      gruppogiac(cf,s);
    } else if (subcode=="GRUPPOART")  {
      gruppoart(cf,s);
    } else if (subcode=="CODGRUPPOGIAC") {
      codgruppogiac(cf,s);
    } else if (subcode=="CODGRUPPOART") {
      int lv=atoi(s.get());
      do {
        valore=relation()->curr(LF_ANAMAG).get("CODART");
        valore.rpad(25).cut(livelli_articolo().packed_length(lv));
      } while (--lv>0 && valore==""); 
      cf.set(valore);
    } else if (subcode=="CATMER") {
      valore=relation()->curr(_ordering == mg_ragg_fisc ? -600 : (_ordering == mg_scat_merc ? -401 : -400)).get("S0");
      cf.set(valore);
    } else if (subcode=="CODCATMER") {
      valore=relation()->curr(LF_ANAMAG).get(_ordering == mg_ragg_fisc ? "RAGGFIS" : "GRMERC");
      if (_ordering != mg_ragg_fisc) 
      {
        valore.rpad(5);
        valore.cut(_ordering == mg_scat_merc ? 5 : 3);
      }
      cf.set(valore);
    } else if (subcode=="LIVELLIGIAC") {
      int lv=_tolivgiac;
      valore="";
      do {
        const TString & piece = livelli_giacenza().unpack_grpcode(relation()->curr(LF_MAG).get("LIVELLO"),lv+1);
        if (piece.not_empty()) {
          if (valore.not_empty())
            valore << '/';
          valore << piece ;
        }
      } while (lv++<livelli_giacenza().last_level());  
      if (valore!="")
        levname=livelli_giacenza().name(_tolivgiac+1);
      else
        levname=TR("Giacenza");
      cf.set(levname << ' ' << valore);
    } else if (subcode=="VALIDATE_UM") {
      TForm_string &um_field=(TForm_string &)cf.form().find_field('B',odd_page,atoi(s.get()) );
      if (*um_field.get()<=' ') // blank : not a valid UM!
        cf.set("0");
    } else if (subcode=="SAVE_UM") {
      CHECK(FALSE,"SAVE_UM no more available");
    } else if (subcode=="RESET_UM") {
      TForm_string &dest=(TForm_string &)cf.form().find_field('B',odd_page,atoi(s.get()) );
      dest.set("");
    } else if (subcode=="COPY_UM") {
      /*if (last_um==NULL) 
        last_um = new TString16(cf.get());
      if (*last_um != cf.get())
      {
        // impossibile sommare le quantit� degli articoli per UM diverse
        last_um->cut(0);
        dest.set("");
        cf.form().find_field('B',odd_page,FF_FLAGTOTQTA).set("0");
      }*/
      TForm_string &dest=(TForm_string &)cf.form().find_field('B',odd_page,atoi(s.get()) );
      if (*dest.get() && strcmp(dest.get(),cf.get()) != 0)
      {
        // impossibile sommare le quantit� degli articoli per UM diverse
        dest.set("  ");
      } else {
        // prima sommare le quantit� degli articoli per UM diverse
        dest.set( cf.get() );
      }
    } else if (subcode=="ORDINATO")
    {
      const int flag = s.get_int();
      const real o = ordinato(flag);
      cf.set(o.string());
    }
    return TRUE;
  } else
    return TForm::validate(cf, s);
}

void TForm_stampemg::set_ordering(const TTipo_ordinamento_mg t)
{
  _ordering = t;
  TForm_subsection &s=((TForm_subsection &)find_field('B',odd_page,"GRUPPI_CATMER"));
  const char* cond = "";
  switch (_ordering)
  {
  case mg_normale:   cond = "CODART"; break;
  case mg_ragg_fisc: cond = "RAGGFIS"; break;
  case mg_cat_merc:  cond = "GRMERC[1,3]"; break;
  default: break;
  }
  s.setcondition(cond,_strexpr);
}

bool TForm_stampemg::setdett_perart(bool totaliart,int fromlivart,int livart,bool totaligiac,int fromlivgiac,int livgiac,bool showmag, bool showdep)
{
  const TCodart_livelli& la = livelli_articolo();
  const int last_artlev = la.last_level() ? la.last_level() : 1;
  const int last_giaclev = livelli_giacenza().last_level();

  if (totaliart)  // voglio i totali di articolo
  {
    if (livart==0 && fromlivgiac==0)
      livart=last_artlev; // 
  }
  else
  {
    if (fromlivgiac==0)
      fromlivart=last_artlev;
  }
  if (totaligiac) // voglio i totali di giacenza
  {
    if (livgiac==0 && livart==0)
      livgiac=last_giaclev;
  } 
  else
  {
    if (fromlivart>livart)
      livart=fromlivart;
  }
  find_field('B',odd_page,"H_MAGAZZINO").enable(showmag && showdep);
  find_field('B',odd_page,"TOT_MAGAZZINO").show(showmag);
  find_field('B',odd_page,"H_DEPOSITO").show(FALSE);
  find_field('B',odd_page,"TOT_DEPOSITO").show(showdep);
  return setdettaglio(TRUE,fromlivart,livart,fromlivgiac,livgiac,showmag||showdep);
}

bool TForm_stampemg::setdett_permag(bool totaliart,int fromlivart,int livart,bool totaligiac,int fromlivgiac,int livgiac,bool showmag, bool showdep,bool showdett)
{
  const int last_artlev=livelli_articolo().last_level() ? livelli_articolo().last_level() : 1;
  const int last_giaclev=livelli_giacenza().last_level();

  if (totaliart)  // voglio i totali di articolo
  {
    if (livart==0 && fromlivgiac==0)
      livart=last_artlev; // 
  }
  else
  {
    if (fromlivgiac==0)
      fromlivart=last_artlev;
  }
  if (totaligiac) // voglio i totali di giacenza
  {
    if (livgiac==0 && livart==0)
      livgiac=last_giaclev; // 
  } 
  else
  {
    if (fromlivart>livart)
      livart=fromlivart;
  }

  find_field('B',odd_page,"H_MAGAZZINO").show(showmag && (showdep || showdett) );
  find_field('B',odd_page,"TOT_MAGAZZINO").show(showmag);

  find_field('B',odd_page,FF_DIVDEPOSITI).enable(showdep);
  find_field('B',odd_page,"H_DEPOSITO").show(showdep && showdett);
  find_field('B',odd_page,"TOT_DEPOSITO").show(showdep);
  return  setdettaglio(showdett,fromlivart,livart,fromlivgiac,livgiac, FALSE);
}


bool TForm_stampemg::setdettaglio(bool show, int fromlivart,int livart,int fromlivgiac,int livgiac,bool dettgiac)
{
  const TCodart_livelli& la = livelli_articolo();
  const int last_artlev=la.last_level() ? la.last_level() : 1;
  const int last_giaclev=livelli_giacenza().last_level();

  const int tolivart =min(( livart ? livart  :last_artlev) ,last_artlev-1);
  const int tolivgiac=min((livgiac ? livgiac :last_giaclev),last_giaclev );

  _fromlivart=fromlivart;
  _fromlivgiac=fromlivgiac;
  _tolivart=livart;
  _tolivgiac=livgiac;
  // ***********
  // abilita le sezioni dei livelli di codice 
  TForm_subsection &s1=(TForm_subsection &)find_field('B',odd_page,"H_ARTICOLO");
  TForm_subsection &s2=(TForm_subsection &)find_field('B',odd_page,"TOT_ARTICOLO");
  if (fromlivgiac==0 // non raggruppo a partire dai livelli di giacenza
    && (livart==0 || livart==last_artlev)) {
    s1.show(show && (dettgiac || livgiac!=0) );
    s2.show(show);
  } else {
    s1.hide();
    s2.hide();
  }
  int i;
  for (i=1; i<=FORM_MAXARTLEV; i++)
  {
    TString16 sname("TOT_GART");
    sname << i;
    TForm_subsection &s1=(TForm_subsection &)find_field('B',odd_page,sname);
    s1.show(show && i>=fromlivart && i<=tolivart && fromlivgiac==0);
    sname="H_GCODART";
    sname << i;
    TForm_subsection &s2=(TForm_subsection &)find_field('B',odd_page,sname);
    s2.show(show && i>=fromlivart && 
      (i<tolivart || (i==tolivart && (livart==0 || livart>tolivart || livgiac || dettgiac))) && fromlivgiac==0);
  }
  // abilita le sezioni del codice livelli giacenza
  for (i=1; i<=FORM_MAXGIACLEV; i++)
  {
    TString16 sname("TOT_GLIVGIAC");
    sname << i;
    TForm_subsection &s1=(TForm_subsection &)find_field('B',odd_page,sname);
    s1.show(show && i>=fromlivgiac && (i<=tolivgiac) && (livart==0));
    sname="H_GLIVGIAC";
    sname << i;
    TForm_subsection &s2=(TForm_subsection &)find_field('B',odd_page,sname);
    const bool on = show && i>=fromlivgiac && (i<tolivgiac ||(i==tolivgiac && dettgiac)) && livart==0;
    s2.show(on);
  }

  // ***********
  fromlivart = fromlivart ? fromlivart  :1;
  fromlivgiac=fromlivgiac ? fromlivgiac :1;
  // codice livello giacenza
  if (livart==0)
  {
    for (i=fromlivgiac;i<=tolivgiac ; i++) 
    {
      TString80 sname;
      sname << "GRUPPI_LIVGIAC" << i;
      TForm_subsection &s=(TForm_subsection &)find_field('B',odd_page,sname);
    
      TString cond(s.condition());  
      cond << "+LIVELLO[" << livelli_giacenza().packed_length(i-1)+1 << "," << livelli_giacenza().packed_length(i) <<']';
          
      s.setcondition(cond,_strexpr);
    }
  }
  // sezioni livello codice 
  // for (i=fromlivart;i<=tolivart ; i++) {
  for (i=1;i<=tolivart ; i++) 
  {
    TString sname("GRUPPI_CODART");
    sname << i;
    TForm_subsection &s=(TForm_subsection &)find_field('B',odd_page,sname);
    TString cond(s.condition());
    cond << '[' << (i==1 ? 1:livelli_articolo().packed_length(i-1))<<',' << livelli_articolo().packed_length(i) <<']';
    s.setcondition(cond,_strexpr);
  }

  return TRUE;
}


TForm_stampemg::TForm_stampemg(const char *name,const char *code) 
  : TForm(name,code)
{
  _fromlivgiac=_tolivgiac=0;
  _fromlivart=_tolivart=0;
  _ordering = mg_normale;
  
}
TForm_stampemg::~TForm_stampemg()
{
}

// maschera delle stampe magazzino

void TStampemg_mask::enable_livellicodice() 
{
  int pos = id2pos(F_RAGGCODICE);
  if (pos >= 0) 
  {
    TMask_field &f_ragg=field(F_RAGGCODICE);
    if (!livelli_articolo().enabled()) 
    {
      // nasconde i campi di livello articolo
      f_ragg.reset(); f_ragg.check(); f_ragg.hide();       
      field(F_FROMLIVELLOART).hide();
      field(F_TOLIVELLOART).hide();
    } else {
      f_ragg.set("");
      f_ragg.show();
      f_ragg.check();
      field(F_FROMLIVELLOART).show();
      field(F_TOLIVELLOART).show();
    }
    
  } 
  int pos_g = id2pos(F_RAGGLIVGIAC);
  if (pos_g >= 0) 
  {
    TMask_field &f_ragg=field(F_RAGGLIVGIAC);
    if (!livelli_giacenza().enabled()) 
    {
      // nasconde i campi di livello giacenza
      f_ragg.reset(); f_ragg.check();f_ragg.hide();
      field(F_FROMLIVELLOGIAC).hide();
      field(F_TOLIVELLOGIAC).hide();
    } else {
      f_ragg.set("");
      f_ragg.show();
      f_ragg.check();
      field(F_FROMLIVELLOGIAC).show();
      field(F_TOLIVELLOGIAC).show();
    }
    
  }
}

void TStampemg_mask::set_livellicodice()
{
  if (!get_bool(F_RAGGCODICE) && get_int(F_FROMLIVELLOGIAC)==0)
  {
    set(F_FROMLIVELLOART,livelli_articolo().last_level());
    //set(F_TOLIVELLOART,);
  }
  if (!get_bool(F_RAGGLIVGIAC))
  {
    if (get_int(F_TOLIVELLOART)==0)
      set(F_TOLIVELLOART,livelli_articolo().last_level()); // 
  } else {
    if (get_int(F_TOLIVELLOGIAC)==0)
      set(F_TOLIVELLOGIAC,livelli_giacenza().last_level()); // 
  }
}