429 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			429 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
// pd6142100.cpp Programma personalizzato per Istituto Suore: Liquidazione riepilogativa IVA
 | 
						||
// Che cosa fa:
 | 
						||
// prende un serie di codici ditta in input, piu' un anno ed un mese (da 1 a 13)
 | 
						||
// e copia tutti i movimenti (mov, rmoviva) in una unica ditta ricevente.
 | 
						||
// (il mese 13 copia tutti i movimenti dell'anno).
 | 
						||
 | 
						||
#include <applicat.h>
 | 
						||
#include <progind.h>
 | 
						||
#include <relation.h>
 | 
						||
#include <sheet.h>
 | 
						||
#include <tabutil.h>
 | 
						||
#include <urldefid.h>
 | 
						||
 | 
						||
#include <nditte.h>
 | 
						||
#include <mov.h>
 | 
						||
#include <rmoviva.h>
 | 
						||
 | 
						||
#include "pd6142100a.h"
 | 
						||
 | 
						||
// La parte di selezione delle ditte e' tratta dal programma di liquidazione IVA (cg4300.cpp)
 | 
						||
class TCopia_movimenti : public TSkeleton_application
 | 
						||
{
 | 
						||
  TArray_sheet   *_ditte;          // Array sheet delle ditte selezionabili
 | 
						||
  TLocalisamfile *_nditte;         // File delle ditte
 | 
						||
  TTable         *_lia;            // Tabella LIA per controllare i parametri liquidazione
 | 
						||
  long           _rcv_firm,        // Ditta ricevente
 | 
						||
                 _firm;            // Ditta iniziale, da ripristinare alla fine di tutto
 | 
						||
  int            _anno;            // Anno da copiare
 | 
						||
  int            _mese;            // Mese da copiare (13 == tutti)
 | 
						||
 | 
						||
  virtual bool check_autorization() const {return false;}
 | 
						||
  virtual const char * extra_modules() const {return "cg";}
 | 
						||
 | 
						||
protected:  
 | 
						||
  virtual void main_loop();
 | 
						||
  static bool fr_ditt_handler(TMask_field& f, KEY key);
 | 
						||
  static bool to_ditt_handler(TMask_field& f, KEY key);
 | 
						||
  static bool select_button  (TMask_field& f, KEY key);
 | 
						||
  static bool reset_button   (TMask_field& f, KEY key);
 | 
						||
  static bool firm_handler   (TMask_field& f, KEY key);
 | 
						||
  static TCopia_movimenti& app() { return (TCopia_movimenti&)main_app(); }
 | 
						||
  
 | 
						||
public:
 | 
						||
  virtual bool create();
 | 
						||
  virtual bool destroy();
 | 
						||
  TArray_sheet* get_ditte_sheet() { return _ditte; }
 | 
						||
  bool          look_lia(long codditta, int anno);
 | 
						||
  long          select_firm_range(long from, long to);
 | 
						||
  void          build_ditte_sheet();
 | 
						||
  void          reset_choices(TMask&);
 | 
						||
  void          set_choice_limits(TMask&);
 | 
						||
  void          copia_mov();
 | 
						||
 | 
						||
  TCopia_movimenti() {};
 | 
						||
  virtual ~TCopia_movimenti() {};
 | 
						||
};
 | 
						||
 | 
						||
bool TCopia_movimenti::look_lia(long ditta, int year)
 | 
						||
{
 | 
						||
  if (year == 0) year = _anno;
 | 
						||
  if (ditta == 0l) ditta = get_firm();
 | 
						||
  TString16 y; y.format("%05ld%04d", ditta, year);
 | 
						||
  
 | 
						||
  _lia->zero();
 | 
						||
  _lia->put("CODTAB", y);
 | 
						||
  _lia->read();
 | 
						||
  return _lia->good();
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::firm_handler(TMask_field& f, KEY k)
 | 
						||
{
 | 
						||
  if (k == K_ENTER && f.to_check(k))
 | 
						||
  {
 | 
						||
    const long codditta = atol(f.get());
 | 
						||
    const int  anno     = f.mask().get_int(F_ANNO);
 | 
						||
    if (!prefix().exist(codditta))
 | 
						||
      return f.error_box("La ditta specificata non <20> abilitata in contabilit<69>.");
 | 
						||
    if (!app().look_lia(codditta,anno))
 | 
						||
      return f.error_box("Non esistono i parametri di liquidazione per la ditta specificata.");
 | 
						||
  }
 | 
						||
  if (k == K_TAB && f.to_check(k))
 | 
						||
  {
 | 
						||
    const long codditta = atol(f.get());
 | 
						||
    TArray_sheet* s = app()._ditte;
 | 
						||
    const long items = s->items();
 | 
						||
    for (long j=0;j<items;j++)
 | 
						||
    {
 | 
						||
      const long ditta = s->row(j).get_long(1);
 | 
						||
      if (ditta == codditta)
 | 
						||
      {
 | 
						||
        s->uncheck(j);
 | 
						||
        s->disable_row(j);
 | 
						||
      }
 | 
						||
      else 
 | 
						||
        if (ditta == app()._rcv_firm)
 | 
						||
          s->enable_row(j);
 | 
						||
    }
 | 
						||
    app()._rcv_firm = codditta;
 | 
						||
    app().set_choice_limits(f.mask());
 | 
						||
  }
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::to_ditt_handler(TMask_field& f, KEY key)
 | 
						||
{
 | 
						||
  TMask& m = f.mask();
 | 
						||
  if (key == K_F9)
 | 
						||
  {
 | 
						||
    TArray_sheet* sh = app().get_ditte_sheet();
 | 
						||
    
 | 
						||
    sh->disable_check();
 | 
						||
    sh->disable(DLG_USER);
 | 
						||
    if (sh->run() == K_ENTER)
 | 
						||
    {
 | 
						||
      app().select_firm_range(m.get_long(F_DFR),sh->row(sh->selected()).get_long(1));
 | 
						||
      app().set_choice_limits(m);
 | 
						||
    }
 | 
						||
    sh->enable(DLG_USER);
 | 
						||
  }
 | 
						||
  if (key == K_TAB && f.focusdirty())
 | 
						||
  {
 | 
						||
    const long l = app().select_firm_range(m.get_long(F_DFR), m.get_long(F_DTO)); 
 | 
						||
    app().set_choice_limits(m);
 | 
						||
    m.set(F_SELECTED, l);
 | 
						||
  }
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::fr_ditt_handler(TMask_field& f, KEY key)
 | 
						||
{                          
 | 
						||
  TMask& m = f.mask();
 | 
						||
  if (key == K_F9)
 | 
						||
  {
 | 
						||
    TMask& m = f.mask();
 | 
						||
    TArray_sheet* sh = app().get_ditte_sheet();
 | 
						||
 | 
						||
    sh->disable_check();    
 | 
						||
    sh->disable(DLG_USER);
 | 
						||
    if (sh->run() == K_ENTER)
 | 
						||
    {
 | 
						||
      app().select_firm_range(sh->row(sh->selected()).get_long(1), m.get_long(F_DTO));
 | 
						||
      app().set_choice_limits(m);
 | 
						||
    }
 | 
						||
    sh->enable(DLG_USER);
 | 
						||
 }
 | 
						||
  else if (key == K_TAB && f.focusdirty())
 | 
						||
  {
 | 
						||
    const long l = app().select_firm_range(m.get_long(F_DFR), m.get_long(F_DTO));
 | 
						||
    app().set_choice_limits(m);
 | 
						||
    m.set(F_SELECTED, l);
 | 
						||
  }
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
void TCopia_movimenti::set_choice_limits(TMask& m)
 | 
						||
{     
 | 
						||
  long first = -1l, last = -1l;
 | 
						||
  const long items = _ditte->items();
 | 
						||
  for (long i = 0; i < items; i++)
 | 
						||
  {
 | 
						||
    if (_ditte->checked(i))
 | 
						||
    {
 | 
						||
      const long dit = _ditte->row(i).get_long(1);
 | 
						||
      if (first == -1l) first = dit;
 | 
						||
      if (last < dit)   last  = dit;
 | 
						||
    }
 | 
						||
  }
 | 
						||
  if (first != -1) m.set(F_DFR, first);                        
 | 
						||
  if (last  != -1) m.set(F_DTO, last);                        
 | 
						||
  m.set(F_SELECTED, _ditte->checked());
 | 
						||
}
 | 
						||
 | 
						||
void TCopia_movimenti::reset_choices(TMask& m)
 | 
						||
{
 | 
						||
  m.reset(F_SELECTED);
 | 
						||
  m.reset(F_DFR);
 | 
						||
  m.reset(F_DTO);
 | 
						||
  _ditte->check(-1, FALSE);
 | 
						||
}
 | 
						||
 | 
						||
long TCopia_movimenti::select_firm_range(long from, long to)
 | 
						||
{
 | 
						||
  if (to == 0l) to = 99999L;                              
 | 
						||
  const long items = _ditte->items();
 | 
						||
  for (long i = 0; i < items; i++)
 | 
						||
  {
 | 
						||
    if (_ditte->row_disabled(i))
 | 
						||
      continue;
 | 
						||
 | 
						||
    const long cod = _ditte->row(i).get_long(1);
 | 
						||
    if (cod >= from && cod <= to)
 | 
						||
      _ditte->check(i);
 | 
						||
    else 
 | 
						||
      _ditte->uncheck(i);
 | 
						||
  } 
 | 
						||
  return _ditte->checked();
 | 
						||
}
 | 
						||
 | 
						||
void TCopia_movimenti::build_ditte_sheet()
 | 
						||
{                 
 | 
						||
  // ricostruisce l'array_sheet delle ditte slezionabili
 | 
						||
  _ditte->destroy();
 | 
						||
  TLocalisamfile& dt = *_nditte;
 | 
						||
  
 | 
						||
  for (dt.first(); !dt.eof(); dt.next())
 | 
						||
  {     
 | 
						||
    TToken_string* d = new TToken_string(64);
 | 
						||
    d->add(" ");
 | 
						||
    d->add(dt.get(NDT_CODDITTA));
 | 
						||
    d->add(dt.get(NDT_RAGSOC));    
 | 
						||
    const long pos = _ditte->add(d);  
 | 
						||
    if (prefix().exist(dt.get_long(NDT_CODDITTA))) 
 | 
						||
      _ditte->enable_row(pos);
 | 
						||
    else
 | 
						||
      _ditte->disable_row(pos);
 | 
						||
  }
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::select_button(TMask_field& f, KEY key)
 | 
						||
{
 | 
						||
  if (key == K_SPACE)
 | 
						||
  {
 | 
						||
    app()._ditte->enable_check();
 | 
						||
    // seleziona e aggiungi alle gia' selezionate 
 | 
						||
    if (app()._ditte->run() == K_ENTER)
 | 
						||
      app().set_choice_limits(f.mask());
 | 
						||
  }
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::reset_button(TMask_field& f, KEY key)
 | 
						||
{
 | 
						||
  if (key == K_SPACE)
 | 
						||
    app().reset_choices(f.mask());
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
void TCopia_movimenti::main_loop()
 | 
						||
{
 | 
						||
  TMask msk("pd6142100a");
 | 
						||
  msk.set_handler(F_DFR,  fr_ditt_handler);
 | 
						||
  msk.set_handler(F_DTO,  to_ditt_handler);
 | 
						||
  msk.set_handler(F_RCVFIRM, firm_handler);
 | 
						||
  msk.set_handler(FBUT_SELECT,select_button);
 | 
						||
  msk.set_handler(FBUT_RESET,reset_button);
 | 
						||
  KEY k;
 | 
						||
  do
 | 
						||
  {
 | 
						||
    k = msk.run();
 | 
						||
    if (k == K_ENTER)
 | 
						||
    {
 | 
						||
      if (msk.get_int(F_SELECTED) == 0)
 | 
						||
      {
 | 
						||
        error_box("Selezionare le ditte invianti.");
 | 
						||
        continue;
 | 
						||
      }
 | 
						||
      _anno = msk.get_int(F_ANNO);
 | 
						||
      _mese = msk.get_int(F_MESE);
 | 
						||
      _rcv_firm = msk.get_long(F_RCVFIRM);
 | 
						||
      if (yesno_box("E' stata selezionata la ditta %ld come ditta ricevente. Continuare?",_rcv_firm))
 | 
						||
      {
 | 
						||
        copia_mov();
 | 
						||
        message_box("Copia movimenti completata.");
 | 
						||
      }
 | 
						||
    }
 | 
						||
  } while(k == K_ENTER);
 | 
						||
}
 | 
						||
 | 
						||
void TCopia_movimenti::copia_mov()
 | 
						||
{
 | 
						||
  TFilename t1,t2;
 | 
						||
  t1.tempdir();
 | 
						||
  t1.insert("%");
 | 
						||
  t2 = t1;
 | 
						||
  t1 << "/tmov";
 | 
						||
  t2 << "/trmoviva";
 | 
						||
  
 | 
						||
  long numreg = 0L;  // numero di registrazione progressivo
 | 
						||
  long count  = 0L;  // numero di items (mov+rmoviva) totali
 | 
						||
  TIsamtempfile  tmov(LF_MOV,t1,TRUE);
 | 
						||
  TIsamtempfile  trmoviva(LF_RMOVIVA,t2,TRUE);
 | 
						||
  tmov.set_autodel();
 | 
						||
  trmoviva.set_autodel();
 | 
						||
  int err=NOERR;
 | 
						||
  {
 | 
						||
    set_firm(_ditte->row(0).get_long(1)); // Per evitare fastidiosi fatal in caso non sia settata nemmeno una ditta
 | 
						||
    
 | 
						||
    TLocalisamfile m(LF_MOV); // necessario per allocare i records
 | 
						||
    TRectype from(LF_MOV),to(LF_MOV);
 | 
						||
    TDate inizio(1,_mese == 13 ? 1 : _mese,_anno);
 | 
						||
    TDate fine(inizio);
 | 
						||
  
 | 
						||
    if (_mese == 13) fine.set_month(12);
 | 
						||
    fine.set_end_month();
 | 
						||
    from.put(MOV_DATAREG,inizio);
 | 
						||
    to.put(MOV_DATAREG,fine);
 | 
						||
    TRelation rel(LF_MOV);
 | 
						||
    rel.add(LF_RMOVIVA,"NUMREG=NUMREG");
 | 
						||
    TCursor cur(&rel,"",2,&from,&to);
 | 
						||
  
 | 
						||
    const long items = _ditte->items();
 | 
						||
  
 | 
						||
    TProgind p(items,"Copia movimenti ditte...", FALSE,TRUE);
 | 
						||
    TString msg;
 | 
						||
    
 | 
						||
    for (long i=0; i<items && err == NOERR; i++) // ciclo sulle ditte selezionate
 | 
						||
      if (_ditte->checked(i))
 | 
						||
      {
 | 
						||
        const long codditta = _ditte->row(i).get_long(1);
 | 
						||
        set_firm(codditta);
 | 
						||
        msg.format("Copia movimenti ditta %ld in corso...",codditta);
 | 
						||
        p.setstatus(i);  
 | 
						||
        p.set_text(msg);        
 | 
						||
        const TRecnotype recs = cur.items();
 | 
						||
        for (TRecnotype j=0; j<recs && err==NOERR;j++)
 | 
						||
        {
 | 
						||
          cur = j;
 | 
						||
          if (cur.is_first_match(LF_RMOVIVA))
 | 
						||
          {
 | 
						||
            tmov.curr() = cur.curr();
 | 
						||
            const long orig_nreg = tmov.get_long(MOV_NUMREG);
 | 
						||
            tmov.put(MOV_NUMREG,numreg+1);
 | 
						||
            count++;
 | 
						||
            if (tmov.write() != NOERR)
 | 
						||
            {
 | 
						||
              err=tmov.status();
 | 
						||
              error_box("Errore %d in scrittura file temporaneo movimenti. Ditta %ld, movimento %ld.",
 | 
						||
                        err,codditta,orig_nreg);
 | 
						||
              break;
 | 
						||
            }
 | 
						||
            do
 | 
						||
            {
 | 
						||
              trmoviva.curr() = cur.curr(LF_RMOVIVA);
 | 
						||
              const int numrig = trmoviva.get_int(RMI_NUMRIG);
 | 
						||
              trmoviva.put(RMI_NUMREG,numreg+1);
 | 
						||
              if (trmoviva.write() != NOERR)
 | 
						||
              {
 | 
						||
                err=trmoviva.status();
 | 
						||
                error_box("Errore %d in scrittura file temporaneo righe iva. Ditta %ld, movimento %ld, riga %d.",
 | 
						||
                           err,codditta,orig_nreg, numrig);
 | 
						||
                break;
 | 
						||
              }
 | 
						||
              count++;
 | 
						||
            } while (cur.next_match(LF_RMOVIVA));
 | 
						||
            numreg++;
 | 
						||
          }
 | 
						||
        }
 | 
						||
      }
 | 
						||
  }
 | 
						||
  //  una volta finita la copia, se non ci sono errori e ci sono record da trasferire
 | 
						||
  // copia i records nella ditta ricevente
 | 
						||
  if (err == NOERR && numreg > 0)
 | 
						||
  {
 | 
						||
    set_firm(_rcv_firm); // setta la ditta ricevente
 | 
						||
    // Zappa i files
 | 
						||
    {
 | 
						||
      TDir d;
 | 
						||
      d.get(LF_MOV);
 | 
						||
      d.set_eod(0);
 | 
						||
      d.put(LF_MOV);
 | 
						||
      d.get(LF_RMOVIVA);
 | 
						||
      d.set_eod(0);
 | 
						||
      d.put(LF_RMOVIVA);
 | 
						||
      TSystemisamfile m(LF_MOV),r(LF_RMOVIVA); 
 | 
						||
      err = m.pack();
 | 
						||
      if (err == NOERR)
 | 
						||
        err = r.pack();
 | 
						||
    }
 | 
						||
    if (err == NOERR)
 | 
						||
    { 
 | 
						||
      TIsamfile mov(LF_MOV);
 | 
						||
      TIsamfile rmoviva(LF_RMOVIVA);
 | 
						||
      
 | 
						||
      err = mov.open(_excllock);
 | 
						||
      if (err == NOERR)
 | 
						||
        err = rmoviva.open(_excllock);
 | 
						||
      if (err == NOERR) // Scrive sui files della ditta ricevente
 | 
						||
      {
 | 
						||
        long j=0L;
 | 
						||
        TProgind p(count,"Trasferimento movimenti...",FALSE,TRUE);
 | 
						||
        for (tmov.first(); tmov.good(); tmov.next())
 | 
						||
        {
 | 
						||
          p.setstatus(j++);
 | 
						||
          mov.write(tmov.curr());
 | 
						||
        }
 | 
						||
        for (trmoviva.first(); trmoviva.good(); trmoviva.next())
 | 
						||
        {
 | 
						||
          p.setstatus(j++);
 | 
						||
          rmoviva.write(trmoviva.curr());
 | 
						||
        }
 | 
						||
      }
 | 
						||
      else
 | 
						||
        error_box("Errore %d tentando di aprire in modo esclusivo i files della ditta ricevente",err);
 | 
						||
    }
 | 
						||
    else
 | 
						||
      error_box("Errore %d azzerando i files della ditta ricevente",err);
 | 
						||
  }
 | 
						||
  
 | 
						||
  // Il distruttore di TIsamtempfile rimuove i files temporanei automaticamente
 | 
						||
  // dato che e' stato settato l'apposito flag
 | 
						||
  set_firm(_firm); // ripristina la ditta
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::create()
 | 
						||
{
 | 
						||
  _ditte  = new TArray_sheet(-1, -1, -4, -4, "Selezione Ditte", "@1|Cod.@5R|Ragione Sociale@50");
 | 
						||
  _nditte = new TLocalisamfile(LF_NDITTE);
 | 
						||
  _lia = new TTable("%LIA");
 | 
						||
  _firm = TApplication::get_firm();
 | 
						||
  build_ditte_sheet();
 | 
						||
 
 | 
						||
  return TSkeleton_application::create();
 | 
						||
}
 | 
						||
 | 
						||
bool TCopia_movimenti::destroy()
 | 
						||
{
 | 
						||
  delete _ditte;
 | 
						||
  delete _nditte;
 | 
						||
  delete _lia;
 | 
						||
  return TRUE;
 | 
						||
}
 | 
						||
 | 
						||
int pd6142100 (int argc, char** argv)
 | 
						||
{
 | 
						||
  TCopia_movimenti a;
 | 
						||
  a.run(argc,argv,"Liquidazione riepilogativa IVA");
 | 
						||
  return 0;
 | 
						||
}
 |