Files correlati : Ricompilazione Demo : [ ] Commento : git-svn-id: svn://10.65.10.50/trunk@16904 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			249 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			249 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include <applicat.h>
 | 
						|
#include <automask.h>
 | 
						|
#include <progind.h>
 | 
						|
#include <utility.h>
 | 
						|
#include <relation.h>
 | 
						|
#include <textset.h>
 | 
						|
 | 
						|
#include "../mg/anamag.h"
 | 
						|
#include "../mg/umart.h"
 | 
						|
 | 
						|
#include "ps1104.h"
 | 
						|
#include "ps1104100a.h"
 | 
						|
 | 
						|
///////////////////////////////////////////////////////////
 | 
						|
// TAutomask
 | 
						|
///////////////////////////////////////////////////////////
 | 
						|
 | 
						|
class TImporta_penne_mask : public TAutomask
 | 
						|
{
 | 
						|
protected:
 | 
						|
  virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
 | 
						|
 | 
						|
public:
 | 
						|
  TImporta_penne_mask();
 | 
						|
};
 | 
						|
  
 | 
						|
TImporta_penne_mask::TImporta_penne_mask() :TAutomask ("ps1104100a")
 | 
						|
{
 | 
						|
}  
 | 
						|
 | 
						|
bool TImporta_penne_mask::on_field_event(TOperable_field& f, TField_event e, long jolly)
 | 
						|
{ 
 | 
						|
	switch (f.dlg())
 | 
						|
	{
 | 
						|
		//giochetto per avere la lista dei files validi nella directory di trasferimento!
 | 
						|
		case F_NAME:
 | 
						|
			if (e == fe_button)
 | 
						|
			{
 | 
						|
				TArray_sheet as(-1, -1, 72, 20, TR("Selezione file"), 
 | 
						|
                           "File@32");
 | 
						|
				TFilename path = get(F_PATH);
 | 
						|
				path.add("*.csv");	//files delle testate
 | 
						|
				list_files(path, as.rows_array());
 | 
						|
				TFilename name;
 | 
						|
				FOR_EACH_ARRAY_ROW(as.rows_array(), i, row)
 | 
						|
				{
 | 
						|
					name = *row;
 | 
						|
					*row = name.name();
 | 
						|
				}
 | 
						|
				if (as.run() == K_ENTER)
 | 
						|
				{
 | 
						|
					f.set(as.row(as.selected()));
 | 
						|
				}
 | 
						|
			}
 | 
						|
			break;
 | 
						|
		default:
 | 
						|
			break;
 | 
						|
	}
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
                                      ///////////////////////////////////////////////
 | 
						|
                                      //// CLASSI DERIVATE PER IMPORTAZIONE DATI ////
 | 
						|
                                      ///////////////////////////////////////////////  
 | 
						|
 | 
						|
/////////////////////////////////////////////////////////////
 | 
						|
//	Recordset specifici per i dati da trasferire
 | 
						|
/////////////////////////////////////////////////////////////
 | 
						|
class TImporta_penne_recordset : public TCSV_recordset
 | 
						|
{
 | 
						|
  char _sep;
 | 
						|
 | 
						|
  protected:
 | 
						|
    virtual TRecnotype new_rec(const char* buf = NULL);    
 | 
						|
  
 | 
						|
  public:
 | 
						|
    TImporta_penne_recordset(const char * fileName, char sep);
 | 
						|
};
 | 
						|
 | 
						|
TRecnotype TImporta_penne_recordset::new_rec(const char* buf)
 | 
						|
{
 | 
						|
  TToken_string str(256,'\t'); //nuovo record tab separator
 | 
						|
 | 
						|
  if(buf && *buf)
 | 
						|
  {
 | 
						|
    bool apici = false;
 | 
						|
 | 
						|
    for (const char* c = buf; *c ; c++)
 | 
						|
    {
 | 
						|
      if (*c == '"')
 | 
						|
      {
 | 
						|
        apici = !apici;
 | 
						|
      }
 | 
						|
      else
 | 
						|
      {
 | 
						|
        if (*c == _sep)  //tipo di separatore dei campi che si trova nel record di origine
 | 
						|
        {
 | 
						|
          if (!apici)
 | 
						|
            str << str.separator();
 | 
						|
          else
 | 
						|
            str << *c;
 | 
						|
        }
 | 
						|
        else
 | 
						|
          str << *c;
 | 
						|
      }
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  const TRecnotype n = TText_recordset::new_rec(str);
 | 
						|
 | 
						|
  if (n >= 0)
 | 
						|
    row(n).separator(str.separator());
 | 
						|
  
 | 
						|
  return n;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
TImporta_penne_recordset::TImporta_penne_recordset(const char * filename, char sep)
 | 
						|
                        : TCSV_recordset("CSV(;)"), _sep(sep)  //separatore
 | 
						|
{
 | 
						|
  TString query;
 | 
						|
  query << "CSV(" << sep << ")\n"
 | 
						|
        << "SELECT * FROM " << filename;
 | 
						|
 | 
						|
  TFilename n;
 | 
						|
  if (parse_query(query, n) == _qt_select && n.exist())
 | 
						|
    load_file(n);
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
///////////////////////////////////////
 | 
						|
// TSkeleton_application
 | 
						|
///////////////////////////////////////
 | 
						|
class TImporta_penne : public TSkeleton_application
 | 
						|
{
 | 
						|
	virtual bool check_autorization() const { return false; }
 | 
						|
  virtual const char * extra_modules() const { return "ve"; }
 | 
						|
 | 
						|
	TImporta_penne_mask* _msk;
 | 
						|
 | 
						|
protected:
 | 
						|
 | 
						|
public:           
 | 
						|
  virtual bool create();
 | 
						|
  virtual bool destroy();
 | 
						|
  virtual void main_loop();
 | 
						|
	bool transfer(const TFilename& file);
 | 
						|
 
 | 
						|
  TImporta_penne() {};
 | 
						|
};
 | 
						|
 | 
						|
bool TImporta_penne::transfer(const TFilename& file)
 | 
						|
{
 | 
						|
  //che tipo di separatore dei campi si e' scelto?
 | 
						|
  const char sep = _msk->get(F_SEP)[0];
 | 
						|
  //crea il recordset sul file di input utilizzando il separatore
 | 
						|
  TImporta_penne_recordset s(file, sep);
 | 
						|
 | 
						|
  TProgind pi(s.items(),"Importazione articoli in corso ...",true,true);
 | 
						|
 | 
						|
  //dichiarazione di files e record da usare;si fa qui per non doverla ripetere ad ogni record letto;
 | 
						|
  //il record rec_anamag e' generico e vuoto (anche rec_umart ovviamente)
 | 
						|
  TLocalisamfile anamag(LF_ANAMAG);
 | 
						|
  TRectype& rec_anamag = anamag.curr();
 | 
						|
  TLocalisamfile umart(LF_UMART);
 | 
						|
  TRectype& rec_umart = umart.curr();
 | 
						|
 | 
						|
  //giro su tutti i record del recordset per importarli
 | 
						|
  for (bool ok = s.move_first(); ok; ok = s.move_next())
 | 
						|
  {
 | 
						|
    if (!pi.addstatus(1)) 
 | 
						|
      break;
 | 
						|
 | 
						|
    //prende i dati dal record di input del file csv
 | 
						|
    //codart ci deve essere e va maiuscolizzato
 | 
						|
    TString80 codart = s.get(0).as_string();
 | 
						|
    if (codart.blank())
 | 
						|
      continue;
 | 
						|
    codart.upper();
 | 
						|
    //descrizione
 | 
						|
    const TString80 descr = s.get(1).as_string();
 | 
						|
    //prezzo (inizialmente come stringa x poter fare le replace)
 | 
						|
    TString80 str_prezzo = s.get(2).as_string();
 | 
						|
    str_prezzo.replace(',','.');
 | 
						|
    const real prezzo = str_prezzo;
 | 
						|
    //unita' di misura dalla maschera
 | 
						|
    const TString4 um = _msk->get(F_UMS);
 | 
						|
 | 
						|
    //aggiorna i files ANAMAG e UMART
 | 
						|
    //ANAMAG
 | 
						|
    rec_anamag.put(ANAMAG_CODART, codart);
 | 
						|
    rec_anamag.put(ANAMAG_DESCR, descr);
 | 
						|
    //tenta la rewrite nel caso l'articolo esista gia'; se non esiste fa la write di brutto
 | 
						|
    int err = anamag.rewrite_write();
 | 
						|
 | 
						|
    //UMART
 | 
						|
    //procede solo se la scrittura su anamag e' riuscita, in modo da evitare scritture incomplete
 | 
						|
    if (err == NOERR)
 | 
						|
    {
 | 
						|
      rec_umart.put(UMART_CODART, codart);
 | 
						|
      rec_umart.put(UMART_NRIGA, 1);
 | 
						|
      rec_umart.put(UMART_UM, um);
 | 
						|
      rec_umart.put(UMART_PREZZO, prezzo);
 | 
						|
 | 
						|
      err = umart.rewrite_write();
 | 
						|
    }
 | 
						|
  }
 | 
						|
	
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
bool TImporta_penne::create()
 | 
						|
{
 | 
						|
  _msk = new TImporta_penne_mask();        
 | 
						|
  return TSkeleton_application::create();
 | 
						|
}
 | 
						|
 | 
						|
bool TImporta_penne::destroy()
 | 
						|
{
 | 
						|
	delete _msk;
 | 
						|
  return TApplication::destroy();
 | 
						|
}
 | 
						|
 | 
						|
void TImporta_penne::main_loop()
 | 
						|
{
 | 
						|
  KEY	tasto;
 | 
						|
	tasto = _msk->run();
 | 
						|
  if (tasto == K_ENTER)
 | 
						|
  {
 | 
						|
    //genero il nome del file da caricare
 | 
						|
    TFilename name = _msk->get(F_PATH);
 | 
						|
    name.add(_msk->get(F_NAME));
 | 
						|
		if (transfer(name))
 | 
						|
		{
 | 
						|
      message_box(TR("Importazione articoli completata"));
 | 
						|
		}
 | 
						|
  }   
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
TImporta_penne& app() { return (TImporta_penne&) main_app(); }
 | 
						|
 | 
						|
 | 
						|
int ps1104100 (int argc, char* argv[])
 | 
						|
{
 | 
						|
  TImporta_penne main_app;
 | 
						|
  main_app.run(argc, argv, TR("Importazione articoli TENDER"));
 | 
						|
  return true;
 | 
						|
} |