Prima versione importazione Visual Gala git-svn-id: svn://10.65.10.50/branches/R_10_00@22780 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			734 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			734 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "tc8.h"
 | 
						|
#include "../cg/cg2101.h"
 | 
						|
 | 
						|
#include <applicat.h>
 | 
						|
#include <automask.h>
 | 
						|
#include <progind.h>
 | 
						|
#include <reputils.h>
 | 
						|
#include <textset.h>
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
#include <causali.h>
 | 
						|
#include <clifo.h>
 | 
						|
#include <occas.h>
 | 
						|
 | 
						|
///////////////////////////////////////
 | 
						|
// TVisualGala_set
 | 
						|
///////////////////////////////////////
 | 
						|
 | 
						|
enum TVisualGala_type { Datetime, Numerico, Char };
 | 
						|
 | 
						|
class TVisualGala_set : public TAS400_recordset
 | 
						|
{
 | 
						|
protected:
 | 
						|
  bool add_field(const char* name, TVisualGala_type t, int len, int integers = 0, int decimals = 0);
 | 
						|
 | 
						|
public:
 | 
						|
  bool get_bill(const char* name, TBill& bill) const;
 | 
						|
  TVisualGala_set(const char* asq) : TAS400_recordset(asq) {}
 | 
						|
};
 | 
						|
 | 
						|
bool TVisualGala_set::add_field(const char* name, TVisualGala_type t, int len, int integers, int decimals)
 | 
						|
{
 | 
						|
  bool ok = false;
 | 
						|
  switch (t)
 | 
						|
  {
 | 
						|
  case Datetime:
 | 
						|
    ok = TAS400_recordset::create_field(name, -1, len = 10, _datefld);
 | 
						|
    break;
 | 
						|
  case Numerico:
 | 
						|
    if (decimals > 0)
 | 
						|
    {
 | 
						|
      len = integers + decimals + 1;
 | 
						|
      ok = TAS400_recordset::create_field(name, -1, len, _realfld);
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      len = integers;
 | 
						|
      if (len == 1)
 | 
						|
        ok = TAS400_recordset::create_field(name, -1, len, _boolfld); 
 | 
						|
      else
 | 
						|
        ok = TAS400_recordset::create_field(name, -1, len, _intfld); 
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  default:
 | 
						|
    ok = TAS400_recordset::create_field(name, -1, len, _alfafld); 
 | 
						|
    break;
 | 
						|
  };
 | 
						|
  CHECKS(ok, "Can't create field ", name);
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_set::get_bill(const char* name, TBill& bill) const
 | 
						|
{
 | 
						|
  const TString& str = get(name).as_string();
 | 
						|
  const int  g = atoi(str.left(2));
 | 
						|
  const int  c = atoi(str.mid(2,2));
 | 
						|
  const long s = atoi(str.mid(4));
 | 
						|
  bill.set(g, c, s);
 | 
						|
  return s > 0;
 | 
						|
}
 | 
						|
 | 
						|
class TVisualGala_clifoset : public TVisualGala_set 
 | 
						|
{
 | 
						|
public:
 | 
						|
  TVisualGala_clifoset(const TFilename& n);
 | 
						|
};
 | 
						|
 | 
						|
TVisualGala_clifoset::TVisualGala_clifoset(const TFilename& n) : TVisualGala_set("AS400(680)")
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
class TVisualGala_movset : public TVisualGala_set 
 | 
						|
{
 | 
						|
public:
 | 
						|
  TVisualGala_movset(const TFilename& n);
 | 
						|
};
 | 
						|
 | 
						|
TVisualGala_movset::TVisualGala_movset(const TFilename& n) : TVisualGala_set("AS400(679)")
 | 
						|
{
 | 
						|
  add_field(MOV_DATAREG,   Datetime, 10);
 | 
						|
  add_field(CLI_CODCF,     Numerico,  0, 6);
 | 
						|
  add_field(CLI_PAIV,      Char,     12);
 | 
						|
  add_field(CLI_COFI,      Char,     20);
 | 
						|
  add_field(CLI_RAGSOC,    Char,     40);            //  49
 | 
						|
  add_field(CLI_INDCF,     Char,     40);            //  89
 | 
						|
  add_field(CLI_CAPCF,     Char,      8);            // 129
 | 
						|
  add_field(CLI_COMCF,     Char,      4);
 | 
						|
  add_field(CLI_STATOCF,   Char,      4);
 | 
						|
  add_field("PersFis",     Numerico,  0, 1);         // 145 
 | 
						|
  add_field(MOV_PROTIVA,   Numerico,  0, 7);
 | 
						|
  add_field("Sezionale",   Char,      2);
 | 
						|
  add_field("FlagDocBIS",  Numerico,  0, 1);
 | 
						|
  add_field(MOV_DATADOC,   Datetime, 10);            // 156 
 | 
						|
  add_field(MOV_CODCAUS,   Char,      4);
 | 
						|
  add_field("CauDescr",    Char,     40);
 | 
						|
  add_field(MOV_CODPAG,    Char,      6);            // 210
 | 
						|
  add_field(MOV_CODVAL,    Char,      4);
 | 
						|
  add_field(MOV_DATACAM,   Datetime, 10);            // 220 
 | 
						|
  add_field(MOV_CAMBIO,    Numerico,  0, 6, 6);
 | 
						|
  add_field(MOV_DATACOMP,  Datetime, 10);            // 243
 | 
						|
  add_field(RMV_NUMRIG,    Numerico,  0, 5);
 | 
						|
  //add_field("CodCauCont",  Char,      4);            // 258
 | 
						|
  add_field(RMV_SOTTOCONTO,Char,     10);
 | 
						|
  add_field(RMV_SEZIONE,   Numerico,  0, 2);
 | 
						|
  add_field(MOV_TOTDOC,    Numerico,  0, 11, 2);     // 270
 | 
						|
  add_field(MOV_TOTDOCVAL, Numerico,  0, 11, 2);
 | 
						|
  add_field(RMI_IMPONIBILE,Numerico,  0, 11, 2);
 | 
						|
  add_field(RMI_CODIVA,    Char,      4);
 | 
						|
  add_field(RMI_IMPOSTA,   Numerico,  0, 11, 2);
 | 
						|
  add_field("NonDetr",     Numerico,  0, 11, 2);
 | 
						|
  add_field("ImponibVal",  Numerico,  0, 11, 2);
 | 
						|
  add_field("ImpostaVal",  Numerico,  0, 11, 2);
 | 
						|
  add_field("NonDetrVal",  Numerico,  0, 11, 2);
 | 
						|
  add_field(MOV_NUMDOC,    Char,     10);
 | 
						|
  add_field(CLI_LOCCF,     Char,     40);
 | 
						|
  add_field("Provincia",   Char,      2);
 | 
						|
  add_field(MOV_DESCR,     Char,    198);
 | 
						|
  add_field(CLI_IBAN,      Char,     40);
 | 
						|
  //add_field("Franchigia0", Char,      2);
 | 
						|
 | 
						|
  load_file(n);
 | 
						|
}
 | 
						|
 | 
						|
///////////////////////////////////////
 | 
						|
// TVisualGala_mask
 | 
						|
///////////////////////////////////////
 | 
						|
 | 
						|
class TVisualGala_mask : public TAutomask
 | 
						|
{
 | 
						|
protected:
 | 
						|
  virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
 | 
						|
 | 
						|
public:
 | 
						|
  TVisualGala_mask() : TAutomask("tc8300a") {}
 | 
						|
};
 | 
						|
 | 
						|
bool TVisualGala_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
 | 
						|
{
 | 
						|
  switch (o.dlg())
 | 
						|
  {
 | 
						|
  case 102:
 | 
						|
  case 103:
 | 
						|
    if (e == fe_button)
 | 
						|
    {
 | 
						|
      TFilename n = get(101);
 | 
						|
      n.add("*.txt");
 | 
						|
      if (input_filename(n))
 | 
						|
      {
 | 
						|
        if (o.dlg() == 103)
 | 
						|
          set(101, n.path());
 | 
						|
        o.set(n.name());
 | 
						|
      }
 | 
						|
    } else
 | 
						|
    if (e == fe_close)
 | 
						|
    {
 | 
						|
      TFilename n = get(101);
 | 
						|
      n.add(o.get());
 | 
						|
      if (!n.exist())
 | 
						|
        return cantread_box(n);
 | 
						|
    }
 | 
						|
    break;
 | 
						|
  default: break;
 | 
						|
  }
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
///////////////////////////////////////
 | 
						|
// TVisualGala_app
 | 
						|
///////////////////////////////////////
 | 
						|
 | 
						|
class TVisualGala_app : public TSkeleton_application
 | 
						|
{
 | 
						|
  TArray _lf;
 | 
						|
  long _occas_code; // Codice del cliente occasionale
 | 
						|
 | 
						|
protected:
 | 
						|
  virtual bool create();
 | 
						|
  virtual void main_loop();
 | 
						|
  virtual bool destroy();
 | 
						|
 | 
						|
  TIsamfile& file(int logicnum) const; // Open a "Fast" isam file
 | 
						|
  void close_isamfiles();              // Close all isam files
 | 
						|
 | 
						|
  long find_clifo(const TString& cf_pi) const;
 | 
						|
  const TString& find_occas(const TString& cf_pi) const;
 | 
						|
  long create_new_clifo() const;
 | 
						|
  bool find_or_create_clifo(const TRecordset& mov, TLog_report& log) const;
 | 
						|
  long find_mov(const TRecordset& movset) const;
 | 
						|
  long find_or_create_mov(const TRecordset& movset, TLog_report& log) const;
 | 
						|
 | 
						|
  bool fill_mov_head(const TRecordset& movset, TRectype& mov, TLog_report& log) const;
 | 
						|
  bool fill_iva_row(const TVisualGala_movset& movset, TRectype& mov, TLog_report& log) const;
 | 
						|
  bool fill_cg_row(const TVisualGala_movset& movset, TRectype& mov, TLog_report& log) const;
 | 
						|
 | 
						|
public:
 | 
						|
  bool import_clifo(const TFilename& n, TLog_report& log) const;
 | 
						|
  bool import_mov(const TFilename& n, TLog_report& log) const;
 | 
						|
};
 | 
						|
 | 
						|
TIsamfile& TVisualGala_app::file(int logicnum) const
 | 
						|
{
 | 
						|
  CHECKD(logicnum >= LF_TABCOM && logicnum <= LF_RMOVIVA, "Invalid file number ", logicnum);
 | 
						|
  TFast_isamfile* f = (TFast_isamfile*)_lf.objptr(logicnum);
 | 
						|
  if (f == NULL)
 | 
						|
  {
 | 
						|
    f = new TFast_isamfile(logicnum);
 | 
						|
    ((TArray&)_lf).add(f, logicnum);
 | 
						|
  }
 | 
						|
  return *f;
 | 
						|
}
 | 
						|
 | 
						|
long TVisualGala_app::find_clifo(const TString& cf_pi) const
 | 
						|
{
 | 
						|
  if (cf_pi.blank() || cf_pi.blank())
 | 
						|
    return 0;
 | 
						|
 | 
						|
  TIsamfile& clifo = file(LF_CLIFO);
 | 
						|
 | 
						|
  TString cod = cf_pi;
 | 
						|
  cod.trim(); cod.upper();
 | 
						|
 | 
						|
  // Tento prima il codice fiscale o la partita IVA?
 | 
						|
  const char* const cofi_paiv = cod.len() > 12 ? "CP" : "PC";
 | 
						|
 | 
						|
  int err = 204;
 | 
						|
  for (int tentativo = 0; tentativo < 2; tentativo++)
 | 
						|
  {
 | 
						|
    clifo.zero();
 | 
						|
    clifo.put(CLI_TIPOCF, 'C');
 | 
						|
    if (cofi_paiv[tentativo] == 'C')
 | 
						|
    {
 | 
						|
      clifo.setkey(4);
 | 
						|
      clifo.put(CLI_COFI, cod);
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      clifo.setkey(5);
 | 
						|
      clifo.put(CLI_PAIV, cod);
 | 
						|
    }
 | 
						|
    err = clifo.read();
 | 
						|
    if (err == NOERR)
 | 
						|
      break;
 | 
						|
  }
 | 
						|
 | 
						|
  return err == NOERR ? clifo.get_long(CLI_CODCF) : 0;
 | 
						|
}
 | 
						|
 | 
						|
const TString& TVisualGala_app::find_occas(const TString& cf_pi) const
 | 
						|
{
 | 
						|
  if (cf_pi.full())
 | 
						|
  {
 | 
						|
    TString cod = cf_pi;
 | 
						|
    cod.trim(); cod.upper();
 | 
						|
 | 
						|
    TIsamfile& occas = file(LF_OCCAS);
 | 
						|
    occas.zero();
 | 
						|
    occas.put(OCC_CFPI, cf_pi);
 | 
						|
    if (occas.read() == NOERR)
 | 
						|
      return occas.get(OCC_CFPI);
 | 
						|
  }
 | 
						|
  return EMPTY_STRING;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::find_or_create_clifo(const TRecordset& mov, TLog_report& log) const
 | 
						|
{
 | 
						|
  TString paiv = mov.get(CLI_PAIV).as_string(); paiv.trim(); paiv.upper();
 | 
						|
  TString cofi = mov.get(CLI_COFI).as_string(); cofi.trim(); cofi.upper();
 | 
						|
  if (cofi.blank() && paiv.blank())
 | 
						|
  {
 | 
						|
    TString msg;
 | 
						|
    msg << TR("Cliente privo di Codice Fiscale e Partita IVA alla riga ") << mov.current_row();
 | 
						|
    log.log(2, msg);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
 | 
						|
  const long vg_codcf = mov.get(CLI_CODCF).as_int();  // Codice cliente VisualGala
 | 
						|
  long ce_codcf = 0;                                  // Codice cliente Campo Enterprise
 | 
						|
  
 | 
						|
  if (vg_codcf <= 0) // Cliente OCCASIONALE
 | 
						|
  {
 | 
						|
    // Posiziona correttamente clifo
 | 
						|
    TIsamfile& clifo = file(LF_CLIFO);
 | 
						|
    if (clifo.get_long(CLI_CODCF) != _occas_code)
 | 
						|
    {
 | 
						|
      clifo.setkey(1);
 | 
						|
      clifo.zero();
 | 
						|
      clifo.put(CLI_TIPOCF, 'C');
 | 
						|
      clifo.put(CLI_CODCF, _occas_code);
 | 
						|
      clifo.read();
 | 
						|
    }
 | 
						|
 | 
						|
    // Posiziona occas
 | 
						|
    const TString& found =  cofi.full() ? find_occas(cofi) : find_occas(paiv);
 | 
						|
    TIsamfile& occas = file(LF_OCCAS);
 | 
						|
    if (found.blank())
 | 
						|
    {
 | 
						|
      occas.zero();
 | 
						|
      occas.put(OCC_CFPI, cofi.full() ? cofi : paiv);
 | 
						|
      occas.put(OCC_RAGSOC, mov.get(CLI_RAGSOC).as_string());
 | 
						|
      const int err = occas.write();
 | 
						|
      if (err != NOERR)
 | 
						|
      {
 | 
						|
        TString msg;
 | 
						|
        msg << TR("Impossibile creare l'anagrafica del cliente occasionale alla riga ") << mov.current_row();
 | 
						|
        log.log(2, msg);
 | 
						|
        return false;
 | 
						|
      }
 | 
						|
      ce_codcf = _occas_code;
 | 
						|
    }
 | 
						|
 | 
						|
    occas.put(OCC_RAGSOC, mov.get(CLI_RAGSOC).as_string());
 | 
						|
 | 
						|
    TString80 indir = mov.get(CLI_INDCF).as_string();
 | 
						|
    const int comma = indir.rfind(',');
 | 
						|
    if (comma > 0)
 | 
						|
    {
 | 
						|
      TString80 civ = indir.mid(comma+1); civ.trim();
 | 
						|
      if (civ.len() <= occas.curr().length(OCC_CIV))
 | 
						|
      {
 | 
						|
        occas.put(OCC_CIV, civ.trim());
 | 
						|
        indir.cut(comma); indir.trim();
 | 
						|
      }
 | 
						|
    }
 | 
						|
    occas.put(OCC_INDIR,    indir);
 | 
						|
    occas.put(OCC_LOCALITA, mov.get(CLI_LOCCF).as_string());
 | 
						|
    occas.put(OCC_CAP,      mov.get(CLI_CAPCF).as_string());
 | 
						|
    occas.put(OCC_COM,      mov.get(CLI_COMCF).as_string());
 | 
						|
    occas.put(OCC_COFI,     cofi);
 | 
						|
    occas.put(OCC_PAIV,     paiv);
 | 
						|
    if (cofi.len() == 16)
 | 
						|
    {
 | 
						|
      const TFixed_string wm = "ABCDEHLMPRST";
 | 
						|
      const TString& codcom = cofi.mid(11, 4);
 | 
						|
      occas.put(OCC_COMNASC, codcom);
 | 
						|
      occas.put("SESSO", cofi[9] >= '4' ? 'F' : 'M');
 | 
						|
      int anno = atoi(cofi.mid(6, 2));
 | 
						|
      anno += (anno < 10) ? 2000 : 1900;
 | 
						|
      const int mese   = wm.find(cofi[8]) + 1;
 | 
						|
      const int giorno = atoi(cofi.mid(9, 2)) % 40;
 | 
						|
      occas.put(OCC_DNASC, TDate(giorno, mese, anno));
 | 
						|
    }
 | 
						|
 | 
						|
    const int err = occas.rewrite();
 | 
						|
    if (err != NOERR)
 | 
						|
    {
 | 
						|
      TString msg;
 | 
						|
      msg << TR("Impossibile aggiornare l'anagrafica del cliente occasionale ") << occas.get(OCC_CFPI);
 | 
						|
      log.log(1, msg);
 | 
						|
    }
 | 
						|
  }
 | 
						|
  else
 | 
						|
  {
 | 
						|
    ce_codcf =  cofi.full() && find_clifo(cofi);
 | 
						|
    if (ce_codcf <= 0)
 | 
						|
      ce_codcf = paiv.full() && find_clifo(paiv);
 | 
						|
    if (ce_codcf <= 0)
 | 
						|
    {
 | 
						|
      ce_codcf = create_new_clifo();
 | 
						|
      if (ce_codcf <= 0)
 | 
						|
      {
 | 
						|
        TString msg;
 | 
						|
        msg << TR("Impossibile creare l'anagrafica del cliente occasionale alla riga ") << mov.current_row();
 | 
						|
        log.log(2, msg);
 | 
						|
        return false;
 | 
						|
      }
 | 
						|
    }
 | 
						|
    TIsamfile& clifo = file(LF_CLIFO);
 | 
						|
    clifo.put(CLI_RAGSOC, mov.get(4).as_string());
 | 
						|
 | 
						|
    TString80 indir = mov.get(5).as_string();
 | 
						|
    TString80 civ;
 | 
						|
    const int comma = indir.rfind(',');
 | 
						|
    if (comma > 0)
 | 
						|
    {
 | 
						|
      civ = indir.mid(comma+1); civ.trim();
 | 
						|
      if (civ.len() <= clifo.curr().length(CLI_CIVCF))
 | 
						|
      {
 | 
						|
        indir.cut(comma); 
 | 
						|
        indir.trim();
 | 
						|
      }
 | 
						|
      else
 | 
						|
        civ.cut(0);
 | 
						|
    }
 | 
						|
    clifo.put(CLI_INDCF, indir);
 | 
						|
    clifo.put(CLI_CIVCF, civ);
 | 
						|
 | 
						|
    clifo.put(CLI_CAPCF,  mov.get(6).as_string());
 | 
						|
    clifo.put(CLI_COMCF,  mov.get(7).as_string());
 | 
						|
    clifo.put(CLI_COFI,   cofi);
 | 
						|
    clifo.put(CLI_PAIV,   paiv);
 | 
						|
    const int err = clifo.rewrite();
 | 
						|
    if (err != NOERR)
 | 
						|
    {
 | 
						|
      TString msg;
 | 
						|
      msg << TR("Impossibile aggiornare l'anagrafica del cliente ") << ce_codcf;
 | 
						|
      log.log(1, msg);
 | 
						|
    }
 | 
						|
  }
 | 
						|
  
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
bool TVisualGala_app::import_clifo(const TFilename& n, TLog_report& log) const
 | 
						|
{
 | 
						|
  TVisualGala_clifoset clifoset(n);
 | 
						|
  const TRecnotype r = clifoset.items();
 | 
						|
  TProgind pi(r, TR("Importazione movimenti"));
 | 
						|
  TRecnotype num = 0;
 | 
						|
  for (bool ok = clifoset.move_first(); ok; ok = clifoset.move_next())
 | 
						|
  {
 | 
						|
    if (!pi.addstatus(1))
 | 
						|
      break;
 | 
						|
 | 
						|
    num++;
 | 
						|
  }
 | 
						|
 | 
						|
  TString msg; 
 | 
						|
  msg.format("%ld anagrafiche importate.", num);
 | 
						|
  log.log(0, "");
 | 
						|
  log.log(0, msg);
 | 
						|
  log.log(0, "");
 | 
						|
 | 
						|
  return r > 0;
 | 
						|
}
 | 
						|
 | 
						|
long TVisualGala_app::find_mov(const TRecordset& movset) const
 | 
						|
{
 | 
						|
  const TString& codcau = movset.get(MOV_CODCAUS).as_string().left(3);
 | 
						|
  const TString4 codreg = cache().get(LF_CAUSALI, codcau, CAU_REG);
 | 
						|
  const long protiva    = movset.get(MOV_PROTIVA).as_int();
 | 
						|
  const TDate datareg   = movset.get(MOV_DATAREG).as_date();
 | 
						|
 | 
						|
  TIsamfile& mov = file(LF_MOV);
 | 
						|
  mov.setkey(4);
 | 
						|
  mov.zero();
 | 
						|
 | 
						|
  long numreg = 0L;
 | 
						|
  if (codreg.full() && protiva > 0 && datareg.ok())
 | 
						|
  {
 | 
						|
    mov.put(MOV_DATAREG, datareg);
 | 
						|
    mov.put(MOV_REG, codreg);
 | 
						|
    mov.put(MOV_PROTIVA, protiva);
 | 
						|
    if (mov.read(_isgteq) == NOERR && mov.get_int(MOV_PROTIVA) == protiva)
 | 
						|
      numreg = mov.get_long(MOV_NUMREG);
 | 
						|
  }
 | 
						|
  
 | 
						|
  return numreg;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::fill_mov_head(const TRecordset& movset, TRectype& mov, TLog_report& log) const
 | 
						|
{
 | 
						|
  TIsamfile& clifo = file(LF_CLIFO);
 | 
						|
  if (!find_or_create_clifo(movset, log))
 | 
						|
    return false;
 | 
						|
  
 | 
						|
  const TString4 codcau = movset.get(MOV_CODCAUS).as_string().left(3);
 | 
						|
  const TRectype& caus  = cache().get(LF_CAUSALI, codcau);
 | 
						|
  const TString4 codreg = caus.get(CAU_REG);
 | 
						|
 | 
						|
  if (codreg.blank())
 | 
						|
  {
 | 
						|
    TString msg; 
 | 
						|
    msg << "Impossibile determinare il registro del movimento con protocollo " << movset.get(MOV_PROTIVA);
 | 
						|
    log.log(2, msg);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
 | 
						|
  TDate datareg  = movset.get(MOV_DATAREG).as_date();
 | 
						|
  TDate datacomp = movset.get(MOV_DATACOMP).as_date();
 | 
						|
  if (!datacomp.ok())
 | 
						|
    datacomp = datareg;
 | 
						|
  TDate datadoc  = movset.get(MOV_DATADOC).as_date();
 | 
						|
  if (!datadoc.ok())
 | 
						|
    datadoc = datacomp;
 | 
						|
 | 
						|
  mov.put(MOV_DATAREG,  datareg);
 | 
						|
  mov.put(MOV_DATACOMP, datacomp);
 | 
						|
  mov.put(MOV_ANNOES,   datacomp.year());
 | 
						|
  mov.put(MOV_DATADOC,  datadoc);
 | 
						|
  mov.put(MOV_NUMDOC,   movset.get(MOV_PROTIVA).as_int());
 | 
						|
  mov.put(MOV_PROTIVA,  movset.get(MOV_PROTIVA).as_int());
 | 
						|
  mov.put(MOV_CODCAUS,  codcau);
 | 
						|
  mov.put(MOV_TIPODOC,  caus.get(CAU_TIPODOC));
 | 
						|
  mov.put(MOV_REG,      codreg);
 | 
						|
  mov.put(MOV_TIPO,     clifo.get(CLI_TIPOCF));
 | 
						|
  mov.put(MOV_CODCF,    clifo.get(CLI_CODCF));
 | 
						|
  if (clifo.get_bool(CLI_OCCAS))
 | 
						|
    mov.put(MOV_OCFPI, file(LF_OCCAS).get(OCC_CFPI));
 | 
						|
  else
 | 
						|
    mov.zero(MOV_OCFPI);
 | 
						|
 | 
						|
  TString descr = movset.get(MOV_DESCR).as_string();
 | 
						|
  if (descr.blank())
 | 
						|
    descr = movset.get("CauDescr").as_string();
 | 
						|
  mov.put(MOV_DESCR,  descr);
 | 
						|
 | 
						|
  mov.put(MOV_TOTDOC,   movset.get(MOV_TOTDOC).as_real());
 | 
						|
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::fill_iva_row(const TVisualGala_movset& movset, TRectype& rmi, TLog_report& log) const
 | 
						|
{
 | 
						|
  rmi.put(RMI_IMPONIBILE, movset.get(RMI_IMPONIBILE).as_real());
 | 
						|
  rmi.put(RMI_CODIVA,     movset.get(RMI_CODIVA).as_string());
 | 
						|
  rmi.put(RMI_IMPOSTA,    movset.get(RMI_IMPOSTA).as_real());
 | 
						|
 | 
						|
  TBill bill;
 | 
						|
  if (!movset.get_bill(RMV_SOTTOCONTO, bill))
 | 
						|
  {
 | 
						|
    TString msg;
 | 
						|
    msg << TR("Conto non valido ") << '"' << movset.get(RMV_SOTTOCONTO) << '"'
 | 
						|
        << TR(" nella riga ") << rmi.get(RMI_NUMRIG)
 | 
						|
        << TR(" nel movimento con protocollo ") << movset.get(MOV_PROTIVA);
 | 
						|
    log.log(2, msg);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
  bill.put(rmi);
 | 
						|
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::fill_cg_row(const TVisualGala_movset& movset, TRectype& rmv, TLog_report& log) const
 | 
						|
{
 | 
						|
  rmv.put(RMV_IMPORTO, movset.get(RMI_IMPONIBILE).as_real());
 | 
						|
  rmv.put(RMV_SEZIONE, movset.get(RMV_SEZIONE).as_int() == 2 ? 'A' : 'D');
 | 
						|
 | 
						|
  TBill bill;
 | 
						|
  if (!movset.get_bill(RMV_SOTTOCONTO, bill))
 | 
						|
  {
 | 
						|
    TString msg;
 | 
						|
    msg << TR("Conto non valido ") << '"' << movset.get(RMV_SOTTOCONTO) << '"'
 | 
						|
        << TR(" nella riga ") << rmv.get(RMV_NUMRIG)
 | 
						|
        << TR(" del movimento con protocollo ") << movset.get(MOV_PROTIVA);
 | 
						|
    log.log(2, msg);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
  bill.put(rmv);
 | 
						|
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
long TVisualGala_app::find_or_create_mov(const TRecordset& movset, TLog_report& log) const
 | 
						|
{
 | 
						|
  long numreg = find_mov(movset);
 | 
						|
  if (numreg <= 0)
 | 
						|
  {
 | 
						|
    TIsamfile& mov = file(LF_MOV);
 | 
						|
    mov.setkey(1);
 | 
						|
    mov.zero();
 | 
						|
    numreg = 1;
 | 
						|
    int err = mov.last();
 | 
						|
    if (err == NOERR)
 | 
						|
    {
 | 
						|
      numreg += mov.get_long(MOV_NUMREG);
 | 
						|
 | 
						|
      mov.zero();
 | 
						|
      mov.put(MOV_NUMREG,   numreg);
 | 
						|
      if (fill_mov_head(movset, mov.curr(), log))
 | 
						|
        err = mov.write();
 | 
						|
      else
 | 
						|
        err = _isnocurkey;
 | 
						|
    }
 | 
						|
    if (err != NOERR)
 | 
						|
    {
 | 
						|
      TString msg; 
 | 
						|
      msg << TR("Impossibile creare il movimento ") << numreg
 | 
						|
          << TR(" con protocollo ") << movset.get(MOV_PROTIVA);
 | 
						|
      log.log(2, msg);
 | 
						|
      numreg = 0;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return numreg;
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::import_mov(const TFilename& n, TLog_report& log) const
 | 
						|
{
 | 
						|
  TVisualGala_movset movset(n);
 | 
						|
  const TRecnotype r = movset.items();
 | 
						|
  TProgind pi(r, TR("Importazione movimenti"));
 | 
						|
  TRecnotype num = 0;
 | 
						|
  TString msg; 
 | 
						|
 | 
						|
  TMovimentoPN mov; 
 | 
						|
 | 
						|
  for (bool ok = movset.move_first(); ok; ok = movset.move_next())
 | 
						|
  {
 | 
						|
    if (!pi.addstatus(1))
 | 
						|
      break;
 | 
						|
 | 
						|
    const int nriga = movset.get(RMV_NUMRIG).as_int();
 | 
						|
    if (nriga == 1)
 | 
						|
    {
 | 
						|
      if (mov.cg_items() >= 3)
 | 
						|
      {
 | 
						|
        if (mov.rewrite() == NOERR)
 | 
						|
          num++;
 | 
						|
        else
 | 
						|
          break;
 | 
						|
      }
 | 
						|
 | 
						|
      const long numreg = find_or_create_mov(movset, log);
 | 
						|
      if (numreg <= 0)
 | 
						|
        break;
 | 
						|
      mov.curr() = file(LF_MOV).curr();
 | 
						|
      const int err = mov.read(_isequal, _lock);
 | 
						|
      if (err != NOERR)
 | 
						|
      {
 | 
						|
        msg << TR("Impossibile leggere il movimento ") << numreg;
 | 
						|
        log.log(2, msg);
 | 
						|
        break;
 | 
						|
      }
 | 
						|
      fill_mov_head(movset, mov.curr(), log);
 | 
						|
      mov.destroy_rows(numreg);
 | 
						|
      fill_iva_row(movset, mov.iva(0), log);
 | 
						|
    }
 | 
						|
    fill_cg_row(movset, mov.cg(nriga-1), log);
 | 
						|
 | 
						|
  }
 | 
						|
  if (mov.cg_items() >= 3)
 | 
						|
  {
 | 
						|
    if (mov.rewrite() == NOERR)
 | 
						|
      num++;
 | 
						|
  }
 | 
						|
 | 
						|
 | 
						|
  msg.format("%ld movimenti importati.", num);
 | 
						|
  log.log(0, "");
 | 
						|
  log.log(0, msg);
 | 
						|
  log.log(0, "");
 | 
						|
 | 
						|
  return r > 0;
 | 
						|
}
 | 
						|
 | 
						|
long TVisualGala_app::create_new_clifo() const
 | 
						|
{
 | 
						|
  TIsamfile& f = file(LF_CLIFO);
 | 
						|
 | 
						|
  long code = 0;
 | 
						|
  TISAM_recordset c("USE CLIFO\nTO TIPOCF=C");
 | 
						|
  if (c.move_last())
 | 
						|
  {
 | 
						|
    code = c.get(CLI_CODCF).as_int() + 1;
 | 
						|
    f.zero();
 | 
						|
    f.put(CLI_TIPOCF, 'C');
 | 
						|
    f.put(CLI_CODCF, code);
 | 
						|
    f.put(CLI_RAGSOC, TR("Nuovo Cliente"));
 | 
						|
    if (f.write() != NOERR)
 | 
						|
      code = 0;
 | 
						|
  }
 | 
						|
  
 | 
						|
  return code;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
bool TVisualGala_app::create()
 | 
						|
{
 | 
						|
  // Determina o crea l'anagrafica del cliente occasionale tipico
 | 
						|
  TISAM_recordset clifo("USE CLIFO SELECT OCCAS=\"X\"\nTO TIPOCF=C");
 | 
						|
  if (clifo.move_first())
 | 
						|
    _occas_code = clifo.get(CLI_CODCF).as_int();
 | 
						|
  else
 | 
						|
  {
 | 
						|
    _occas_code = create_new_clifo();
 | 
						|
    if (_occas_code > 0)
 | 
						|
    {
 | 
						|
      TIsamfile& f = file(LF_CLIFO);
 | 
						|
      f.put(CLI_RAGSOC, TR("Cliente Occasionale"));
 | 
						|
      f.put(CLI_OCCAS, true);
 | 
						|
      if (f.rewrite() != NOERR)
 | 
						|
        _occas_code = 0;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  if (_occas_code <= 0)
 | 
						|
    return error_box(TR("Impossibile determinare il codice del cliente occasionale"));
 | 
						|
 | 
						|
  return TSkeleton_application::create();
 | 
						|
}
 | 
						|
 | 
						|
void TVisualGala_app::close_isamfiles()
 | 
						|
{
 | 
						|
  _lf.destroy();
 | 
						|
  prefix().close_closeable_isamfiles();
 | 
						|
}
 | 
						|
 | 
						|
void TVisualGala_app::main_loop()
 | 
						|
{
 | 
						|
  open_files(LF_TAB, LF_TABCOM, LF_CLIFO, LF_MOV, LF_RMOV, 0);
 | 
						|
 | 
						|
  TVisualGala_mask mask;
 | 
						|
  while (mask.run() == K_ENTER)
 | 
						|
  {
 | 
						|
    const TFilename dir = mask.get(101);
 | 
						|
    TLog_report log;
 | 
						|
    if (!mask.field(102).empty())
 | 
						|
    {
 | 
						|
      TFilename f = dir; f.add(mask.get(102));
 | 
						|
      import_clifo(f, log);
 | 
						|
    }
 | 
						|
    if (!mask.field(103).empty())
 | 
						|
    {
 | 
						|
      TFilename f = dir; f.add(mask.get(103));
 | 
						|
      import_mov(f, log);
 | 
						|
    }
 | 
						|
    close_isamfiles();
 | 
						|
    log.preview();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
bool TVisualGala_app::destroy()
 | 
						|
{
 | 
						|
  close_isamfiles();
 | 
						|
  return TSkeleton_application::destroy();
 | 
						|
}
 | 
						|
 | 
						|
int tc8300 (int argc, char* argv[])
 | 
						|
{
 | 
						|
  TVisualGala_app main_app;
 | 
						|
  main_app.run(argc, argv, TR("Importazione VisualGala"));
 | 
						|
  return 0;
 | 
						|
}
 |