237 lines
5.6 KiB
C++
237 lines
5.6 KiB
C++
|
#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
|
||
|
{
|
||
|
protected:
|
||
|
virtual TRecnotype new_rec(const char* buf = NULL);
|
||
|
|
||
|
public:
|
||
|
TImporta_penne_recordset(const char * fileName);
|
||
|
};
|
||
|
|
||
|
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 == ';') //tipo di separatore dei campiche 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)
|
||
|
: TCSV_recordset("CSV(;)") //separatore con ";"
|
||
|
{
|
||
|
load_file(fileName);
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////
|
||
|
// 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)
|
||
|
{
|
||
|
TImporta_penne_recordset s(file);
|
||
|
|
||
|
TProgind pi(s.items(),"Importazione articoli in corso ...",true,true);
|
||
|
|
||
|
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.empty())
|
||
|
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
|
||
|
TLocalisamfile anamag(LF_ANAMAG);
|
||
|
TRectype rec_anamag = anamag.curr();
|
||
|
rec_anamag.put(ANAMAG_CODART, codart);
|
||
|
rec_anamag.put(ANAMAG_DESCR, descr);
|
||
|
//tenta la rewrite nel caso l'articolo esista gia'
|
||
|
int err = anamag.rewrite(rec_anamag);
|
||
|
//se non esiste fa la write di brutto
|
||
|
if (err != NOERR)
|
||
|
err = anamag.write(rec_anamag);
|
||
|
|
||
|
//UMART
|
||
|
//procede solo se la scrittura su anamag e' riuscita, in modo da evitare scritture incomplete
|
||
|
if (err == NOERR)
|
||
|
{
|
||
|
TLocalisamfile umart(LF_UMART);
|
||
|
TRectype rec_umart = umart.curr();
|
||
|
rec_umart.put(UMART_CODART, codart);
|
||
|
rec_umart.put(UMART_NRIGA, 1);
|
||
|
rec_umart.put(UMART_UM, um);
|
||
|
rec_umart.put(UMART_PREZZO, prezzo);
|
||
|
|
||
|
if (umart.rewrite(rec_umart) != NOERR)
|
||
|
umart.write(rec_umart);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|