//**************************************************
//* Aggiornamento Flag Operazioni Intracomunitarie *
//**************************************************
#include <mov.h>  
#include <rmoviva.h>
#include <caus.h>
#include "cg2103.h"  
#include "cg1301.h"  

bool TAgg_opintra::create()
{
  TApplication::create();

  _causali = new TLocalisamfile(LF_ATTIV);
  
  dispatch_e_menu (BAR_ITEM(1));
  return TRUE;
}

bool TAgg_opintra::destroy()
{ 
  delete _causali;
  return TApplication::destroy();
}                                

bool TAgg_opintra::menu(MENU_TAG m)
{
  TMask msk("cg1300a");
  
  while (msk.run() == K_ENTER) 
  {
    look_mov();
    message_box("Aggiornamento completato");
    //return TRUE;
  }
  return FALSE;
}

void TAgg_opintra::look_mov()
{
  TLocalisamfile caus(LF_CAUSALI);
  
  TRelation rel (LF_MOV);
  rel.add(LF_RMOVIVA, "NUMREG=NUMREG",1);
  TCursor cursor(&rel,"",1);
  
  TLocalisamfile& mov = rel.lfile();
  TLocalisamfile& rmoviva = rel.lfile(LF_RMOVIVA);
  
  long ditta = get_firm();
  cursor = 0L;
  const long nitems = cursor.items();
  
  TProgind p(nitems ? nitems : 1, "Aggiornamento in corso..." , TRUE, TRUE, 70);

  for (; cursor.pos() < cursor.items(); ++cursor)
  {
    p.addstatus(1);
    const TString16 reg(mov.get(MOV_REG));
    if (reg.empty()) continue;
    
    const TDate& datareg = mov.get_date(MOV_DATAREG);
    //    if (datareg < "01-01-93") continue;
    if (datareg.year() < 1993) continue;   // Meglio cosi'
    
    const TString16 codcaus(mov.get(MOV_CODCAUS)); 
    if (codcaus.not_empty())
    {
      caus.setkey(1);
      caus.put(CAU_CODCAUS, codcaus);
      if (caus.read() == NOERR)
      { 
        bool intra = caus.get_bool(CAU_INTRACOM);
        bool ok = rel.is_first_match(LF_RMOVIVA);
        while (ok)
        {
          rmoviva.put(RMI_INTRA, intra); 
          rmoviva.rewrite();
          ok = rel.next_match(LF_RMOVIVA); 
        }  
      }
    }   
  } 
}

