Files correlati : tc3.exe tc3100a.msk Importazione da Ericsoft 1 - possibilità di selezionare più file da importare) 2 - importare le anagrafiche partendo "dal basso" del file .txt 3 - attualmente il programma si chiude una volta eseguito il trasferimento. Deve invece rimanere sulla maschera di trasferimento git-svn-id: svn://10.65.10.50/branches/R_10_00@23758 c028cbd2-c16b-5b4b-a496-9718f37d4682
1159 lines
29 KiB
C++
1159 lines
29 KiB
C++
#include <automask.h>
|
||
#include <applicat.h>
|
||
#include <config.h>
|
||
#include <diction.h>
|
||
#include <execp.h>
|
||
#include <progind.h>
|
||
#include <relapp.h>
|
||
#include <reputils.h>
|
||
#include <sheet.h>
|
||
#include <tabutil.h>
|
||
#include <utility.h>
|
||
#include <textset.h>
|
||
#include <reprint.h>
|
||
|
||
#include "tc3.h"
|
||
#include "tc3100a.h"
|
||
|
||
#include "../ve/velib.h"
|
||
#include "../cg/cg2103.h"
|
||
|
||
#include "comuni.h"
|
||
#include <mov.h>
|
||
#include <rmov.h>
|
||
#include <rmoviva.h>
|
||
#include <riclpdc.h>
|
||
|
||
#define TAB_REGISTRI "&RRE"
|
||
#define TAB_CAUSALI "&RCA"
|
||
#define TAB_CODIVA "&RIV"
|
||
#define TAB_PAGAMENTI "&RPG"
|
||
#define TAB_VALUTE "&RVL"
|
||
#define TAB_CONTI "&RCO"
|
||
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TClient_textset
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TClifo_recset : public TCSV_recordset
|
||
{
|
||
TAssoc_array _index;
|
||
|
||
protected:
|
||
virtual TRecnotype new_rec(const char* buf = NULL);
|
||
|
||
public:
|
||
const TString& rag_sociale() const;
|
||
TToken_string& indirizzo();
|
||
const TString& add_stato(TLog_report& log) const;
|
||
TClifo_recset(const char * query);
|
||
};
|
||
|
||
TRecnotype TClifo_recset::new_rec(const char* buf)
|
||
{
|
||
TToken_string str(256,'|'); //nuovo record tab separator
|
||
|
||
if(buf && *buf)
|
||
{
|
||
bool apici=false;
|
||
|
||
for (const unsigned char* c = (const unsigned char*) buf; *c ; c++)
|
||
{
|
||
if (*c == '"')
|
||
{
|
||
apici = !apici;
|
||
}
|
||
else
|
||
{
|
||
if (*c == '|')
|
||
{
|
||
str.trim();
|
||
if (!apici)
|
||
{
|
||
str << str.separator();
|
||
while (isspace(*++c));
|
||
--c;
|
||
}
|
||
else
|
||
str << *((const char *) c);
|
||
}
|
||
else
|
||
str << *((const char *) c);
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
const TRecnotype n = TText_recordset::new_rec(str);
|
||
|
||
if (n >= 0)
|
||
row(n).separator(str.separator());
|
||
|
||
return n;
|
||
}
|
||
|
||
//funzione che crea il campo ragione sociale dai campi opportuni del file csv
|
||
const TString& TClifo_recset::rag_sociale() const
|
||
{
|
||
const TString& nome = get(2).as_string();
|
||
TString ragsoc = get(1).as_string();
|
||
|
||
ragsoc << " " << nome;
|
||
ragsoc.cut(50);
|
||
return get_tmp_string() = ragsoc;
|
||
}
|
||
|
||
//funzione che cerca o aggiunge lo stato alla tabella stati e ne ritorna il codice
|
||
const TString& TClifo_recset::add_stato(TLog_report& log) const
|
||
{
|
||
TString80 stato = get(7).as_string(); // stato
|
||
TTable stati("%STA");
|
||
|
||
if (stato.full() && stato != "Italia")
|
||
{
|
||
stati.setkey(2);
|
||
stati.put("S0", stato);
|
||
|
||
if (stati.read(_isequal) != NOERR)
|
||
{
|
||
stati.setkey(1);
|
||
stati.last();
|
||
|
||
const int codice = stati.get_int("CODTAB") + 1;
|
||
|
||
stati.zero();
|
||
stati.put("CODTAB", format("%03d", codice));
|
||
stati.put("S0", stato);
|
||
|
||
const int err = stati.write();
|
||
|
||
if (err != NOERR)
|
||
log.log(2, format(FR("Impossibile creare lo stato %s - errore %d"), (const char *) stato, codice));
|
||
}
|
||
return get_tmp_string() = stati.get("CODTAB");
|
||
}
|
||
return EMPTY_STRING;
|
||
}
|
||
|
||
//funzione che costruisce l'indirizzo dai campi opportuni del file csv
|
||
TToken_string & TClifo_recset::indirizzo()
|
||
{
|
||
TToken_string & via = get_tmp_string();
|
||
|
||
via.separator(',');
|
||
via = get(3).as_string();
|
||
|
||
return via;
|
||
}
|
||
|
||
TClifo_recset::TClifo_recset(const char * fileName)
|
||
: TCSV_recordset("CSV(|)\n")
|
||
{
|
||
load_file(fileName);
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TPNota_textset
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TPNota_recset : public TCSV_recordset
|
||
{
|
||
TAssoc_array _index;
|
||
|
||
protected:
|
||
virtual TRecnotype new_rec(const char* buf = NULL);
|
||
|
||
public:
|
||
TPNota_recset(const char * query);
|
||
};
|
||
|
||
TRecnotype TPNota_recset::new_rec(const char* buf)
|
||
{
|
||
TToken_string str(2560,'|'); //nuovo record tab separator
|
||
|
||
if(buf && *buf)
|
||
{
|
||
bool apici=false;
|
||
|
||
for (const char* c = buf; *c ; c++)
|
||
{
|
||
if (*c == '"')
|
||
{
|
||
apici = !apici;
|
||
}
|
||
else
|
||
{
|
||
if (*c == '|')
|
||
{
|
||
str.trim();
|
||
if (!apici)
|
||
{
|
||
str << str.separator();
|
||
while (isspace(*++c));
|
||
--c;
|
||
}
|
||
else
|
||
str << *c;
|
||
}
|
||
else
|
||
str << *c;
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
const TRecnotype n = TText_recordset::new_rec(str);
|
||
|
||
if (n >= 0)
|
||
row(n).separator(str.separator());
|
||
|
||
return n;
|
||
}
|
||
|
||
TPNota_recset::TPNota_recset(const char * fileName)
|
||
: TCSV_recordset("CSV(|)\n")
|
||
{
|
||
load_file(fileName);
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TAutomask
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TImportaEricsoft_mask : public TAutomask
|
||
{
|
||
protected:
|
||
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
|
||
|
||
public:
|
||
void load_filenames();
|
||
TImportaEricsoft_mask();
|
||
};
|
||
|
||
TImportaEricsoft_mask::TImportaEricsoft_mask() :TAutomask ("tc3100a")
|
||
{
|
||
}
|
||
|
||
bool TImportaEricsoft_mask::on_field_event(TOperable_field& f, TField_event e, long jolly)
|
||
{
|
||
switch (f.dlg()) // qui 00
|
||
{
|
||
//giochetto per avere la lista dei files validi nella directory di trasferimento!
|
||
case F_CLIFOR:
|
||
if (e == fe_button)
|
||
{
|
||
TArray_sheet as(-1, -1, 72, 20, TR("Selezione file"),
|
||
"File@32");
|
||
TFilename path = get(F_PATH);
|
||
path.add("*.txt"); //files delle anagrafiche
|
||
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()));
|
||
}
|
||
}
|
||
if (e == fe_button || e == fe_modify)
|
||
load_filenames();
|
||
break;
|
||
case F_PATH:
|
||
if (e == fe_init || e == fe_button || e == fe_modify)
|
||
load_filenames();
|
||
break;
|
||
case F_MOV:
|
||
if (e == fe_button)
|
||
{
|
||
TArray_sheet as(-1, -1, 72, 20, TR("Selezione file"),
|
||
"File@32");
|
||
TFilename path = get(F_PATH);
|
||
path.add("*.txt"); //files dei movimenti
|
||
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;
|
||
}
|
||
|
||
void TImportaEricsoft_mask::load_filenames()
|
||
{
|
||
TFilename path = get(F_PATH);
|
||
|
||
if (path.full())
|
||
{
|
||
//eseguo tutte le importazioni (una alla volta)
|
||
TFilename clifoname = get(F_CLIFOR);
|
||
TSheet_field & sh = sfield(F_MOVSH);
|
||
TString_array filenames;
|
||
|
||
path.add("*.*");
|
||
|
||
int items = list_files(path, filenames);
|
||
|
||
sh.destroy();
|
||
for (int i = 0; i < items; i++)
|
||
{
|
||
TFilename file = filenames.row(i);
|
||
|
||
file = file.name();
|
||
if (file != clifoname)
|
||
{
|
||
TToken_string &r = sh.row(i);
|
||
|
||
r.add(file);
|
||
}
|
||
}
|
||
sh.force_update();
|
||
}
|
||
}
|
||
|
||
///////////////////////////////////////
|
||
// TSkeleton_application
|
||
///////////////////////////////////////
|
||
class TImportaEricsoft : public TSkeleton_application
|
||
{
|
||
virtual bool check_autorization() const {return false;}
|
||
|
||
TImportaEricsoft_mask * _msk;
|
||
TEsercizi_contabili _esc;
|
||
TAssoc_array _indetr;
|
||
TString4 _last;
|
||
|
||
protected:
|
||
//metodi per la ricerca dei clienti / fornitori
|
||
bool find_cli(const long cod) const;
|
||
|
||
//metodi per la gestione dei dati dell'anagrafica
|
||
void pulisci_cap(TString& cap) const;
|
||
void estrai_numero(TString& telefono, TString& pref, TString& num) const;
|
||
const TString comune2cap(TString& comune) const;
|
||
void gest_localita(TString& comune, TString& cap, TString& codcom, const TString& prov, const TString& stato = EMPTY_STRING) const;
|
||
const TString & find_indetr(const real & imponibile, const real & imponibile_tot);
|
||
|
||
//metodi per l'inserimento dei dati sulle tabelle corrette
|
||
void save_mov(TLog_report& log, TFilename& ini_file, bool skip);
|
||
void upload_cli(const TFilename& file, TLog_report& log);
|
||
void upload_mov(const TFilename& file, TLog_report& log);
|
||
|
||
const TString & decode_codreg(int tiporeg, int numreg);
|
||
const TString & decode_codcaus(int tipocaus, const TString & codcaus);
|
||
const TString & decode_codiva(const TString & codiva);
|
||
const TString & decode_codpag(const TString & codpag);
|
||
const TString & decode_codval(const TString & codval);
|
||
void decode_conto(const TString & cod, int & gruppo, int & conto, long & sottoconto);
|
||
|
||
public:
|
||
virtual bool create();
|
||
virtual bool destroy();
|
||
virtual void main_loop();
|
||
bool transfer();
|
||
|
||
TImportaEricsoft() {};
|
||
};
|
||
|
||
TImportaEricsoft& app() { return (TImportaEricsoft&) main_app(); }
|
||
|
||
|
||
/////////////////////////////////
|
||
// GESTIONE ANAGRAFICHE //
|
||
/////////////////////////////////
|
||
|
||
/////////////////////////////////
|
||
// ricerca clifo
|
||
/////////////////////////////////
|
||
|
||
//funzione che ricerca un cliente o un fornitore nel file clifo di campo in base al CODCF
|
||
bool TImportaEricsoft::find_cli(const long cod) const
|
||
{
|
||
TString16 key; key.format("C|E%ld", cod);
|
||
return !cache().get(LF_CLIFO, key).empty();
|
||
}
|
||
|
||
|
||
/////////////////////////////////
|
||
// gestione dati anagrafici
|
||
/////////////////////////////////
|
||
|
||
//funzione che elimina eventuali caratteri non numerici dalla stringa del CAP
|
||
void TImportaEricsoft::pulisci_cap(TString& cap) const
|
||
{
|
||
TString8 str;
|
||
for (int i = 0; cap[i] && str.len() < 5; i++)
|
||
if (isdigit(cap[i]))
|
||
str << cap[i];
|
||
cap = str;
|
||
}
|
||
|
||
//funzione che separa il prefisso dal numero di telefono
|
||
void TImportaEricsoft::estrai_numero(TString& telefono, TString& pref, TString& num) const
|
||
{
|
||
bool pre = true;
|
||
|
||
TString str1;
|
||
TString str2;
|
||
|
||
telefono.trim();
|
||
|
||
for (int i = 0; telefono[i]; i++)
|
||
{
|
||
if(isdigit(telefono[i])) //se il carattere letto <20> una cifra numerica, allora concatenalo alla stringa corretta
|
||
{
|
||
if (pre)
|
||
str1 << telefono[i];
|
||
else
|
||
str2 << telefono[i];
|
||
}
|
||
else
|
||
{
|
||
if (pre)
|
||
pre = i<=1; //considero il prefisso finito se <20> composto da almeno 2 caratteri
|
||
else
|
||
{
|
||
if (str2.len() >= 6) //considero il numero finito se <20> composto da alemno 6 cifre
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (str2.full())
|
||
{
|
||
pref = str1;
|
||
num = str2;
|
||
}
|
||
else
|
||
{
|
||
pref.cut(0);
|
||
num = str1;
|
||
}
|
||
}
|
||
|
||
//funzione che estrae il cap dalla tabella comuni dato il nome del comune
|
||
const TString TImportaEricsoft::comune2cap(TString& comune) const
|
||
{
|
||
TLocalisamfile comuni(LF_COMUNI);
|
||
TRectype& rec = comuni.curr();
|
||
TString up_comune = comune;
|
||
up_comune.upper();
|
||
|
||
TString cap;
|
||
|
||
comuni.setkey(2);
|
||
|
||
rec.put(COM_DENCOM,up_comune);
|
||
comuni.read();
|
||
|
||
cap = rec.get(COM_CAPCOM);
|
||
|
||
return get_tmp_string() = cap;
|
||
}
|
||
|
||
//funzione che gestisce le localit<69>
|
||
void TImportaEricsoft::gest_localita(TString& comune, TString& cap, TString& codcom, const TString& prov, const TString& stato) const
|
||
{
|
||
comune.trim();
|
||
|
||
//se stato <20> settato vuol dire stato estero, e lo scrivo cos<6F> com'<27> nella localit<69>
|
||
if (stato.full())
|
||
{
|
||
comune << " - " << stato;
|
||
return;
|
||
}
|
||
|
||
//se sto inserendo una citt<74> italiana, cerco di riempire il CAP se <20> vuoto
|
||
if (cap.empty())
|
||
cap = comune2cap(comune);
|
||
|
||
//se sono riuscito a riempirlo, o era gi<67> pieno, ricerca il codice comune
|
||
if (cap.full())
|
||
{
|
||
pulisci_cap(cap);
|
||
codcom = cap2comune(cap,comune);
|
||
}
|
||
|
||
//se non sono riuscito a trovare il codice comune, prepara aggiungi al comune la provincia (dove esiste)
|
||
if (codcom.blank())
|
||
{
|
||
if (prov.full())
|
||
comune << " (" << prov << ")";
|
||
}
|
||
return;
|
||
}
|
||
|
||
/////////////////////////////////
|
||
// inserimento clifo
|
||
/////////////////////////////////
|
||
|
||
//funzione che effettua l'inserimento delle persone fisiche sulla tabella CLIFO
|
||
void TImportaEricsoft::upload_cli(const TFilename& file, TLog_report& log)
|
||
{
|
||
char tipo = 'C';
|
||
TClifo_recset s(file);
|
||
TLocalisamfile clifo(LF_CLIFO);
|
||
TProgind pi(s.items(),"Importazione Clienti in corso...",true,true);
|
||
const bool aggclifor = !_msk->get_bool( F_NOAGGCLIFOR);
|
||
const bool reverseload = _msk->get_bool(F_REVLOAD);
|
||
|
||
for (bool ok= reverseload ? s.move_last() : s.move_first(); ok; reverseload ? ok = s.move_prev() : ok=s.move_next())
|
||
{
|
||
if (!pi.addstatus(1))
|
||
break;
|
||
|
||
const long cod = s.get(0).as_int();
|
||
const TString ragsoc = s.rag_sociale();
|
||
|
||
//salto la prima riga del file, che contiene le intestazioni delle colonne
|
||
//salto tutte le righe dei fornitori (inserisco solo clienti)
|
||
//eseguo tutto SOLO se riesco a riempire la ragione sociale
|
||
if (ragsoc.full())
|
||
{
|
||
TString16 key; key.format("E%ld", cod);
|
||
TString40 paiv = s.get(11).as_string();
|
||
TString40 cofi = s.get(14).as_string();
|
||
|
||
paiv.strip_spaces();
|
||
cofi.strip_spaces();
|
||
clifo.setkey(6);
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, "C");
|
||
clifo.put(CLI_RICALT, key);
|
||
|
||
bool found = clifo.read() == NOERR;
|
||
|
||
if (!found && paiv.full())
|
||
{
|
||
clifo.setkey(5);
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, "C");
|
||
clifo.put(CLI_PAIV, paiv);
|
||
|
||
found = clifo.read() == NOERR;
|
||
}
|
||
|
||
if (!found && cofi.full())
|
||
{
|
||
clifo.setkey(4);
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, "C");
|
||
clifo.put(CLI_COFI, cofi);
|
||
|
||
found = clifo.read() == NOERR;
|
||
}
|
||
clifo.setkey(1);
|
||
|
||
if (!found || aggclifor)
|
||
{
|
||
clifo.put(CLI_TIPOCF, "F");
|
||
clifo.put(CLI_CODCF, cofi);
|
||
clifo.read();
|
||
if (clifo.get(CLI_TIPOCF) == "F")
|
||
clifo.prev();
|
||
const long newcod = clifo.get_long(CLI_CODCF) + 1L;
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, "C");
|
||
clifo.put(CLI_CODCF, newcod);
|
||
clifo.put(CLI_RAGSOC, ragsoc); //ragsoc
|
||
TToken_string & indirizzo = s.indirizzo();
|
||
clifo.put(CLI_INDCF, indirizzo.get()); //indirizzo
|
||
clifo.put(CLI_CIVCF, indirizzo.get()); //indirizzo
|
||
|
||
//inserisci di dati della residenza
|
||
|
||
TString cap = s.get(4).as_string();
|
||
TString80 comune = s.get(5).as_string(); //comune o localita'
|
||
TString prov = s.get(6).as_string();
|
||
TString codcom;
|
||
|
||
gest_localita(comune, cap, codcom, prov);
|
||
|
||
//se cap <20> pieno, allora inseriscilo
|
||
if (cap.full())
|
||
clifo.put(CLI_CAPCF, cap); //cap
|
||
|
||
//se codcom <20> vuoto, allora o il cap <20> rimasto vuoto, oppure <20> una frazione;
|
||
//in entrambi i casi bisogna inserire tutto in localit<69>
|
||
if (codcom.blank())
|
||
clifo.put(CLI_LOCCF, comune);
|
||
else
|
||
{
|
||
clifo.put(CLI_COMCF, codcom); //inserisci il codice del comune, recuperato tramite il cap
|
||
clifo.put(CLI_LOCCF, s.get(20).as_string()); //inserisci la frazione se <20> indicata
|
||
}
|
||
clifo.put(CLI_STATOCF, s.add_stato(log));
|
||
clifo.put(CLI_REFERENTE, s.get(8).as_string());
|
||
//inserisci i numeri di telefono (telefono, altro telefono, fax)
|
||
for (int i = 9; i < 10; i++)
|
||
{
|
||
TString telefono = s.get(i).as_string();
|
||
|
||
//esegui le operazioni sul numero solo se <20> presente sul file da importare
|
||
if (telefono.full())
|
||
{
|
||
TString pref;
|
||
TString num;
|
||
|
||
estrai_numero(telefono, pref, num);
|
||
|
||
switch (i)
|
||
{
|
||
case 8: clifo.put(CLI_PTEL,pref); clifo.put(CLI_TEL,num); break; //inserisci il numero di telefono
|
||
case 9: clifo.put(CLI_PTEL2,pref); clifo.put(CLI_TEL2,num); break; //inserisci il secondo numero di telefono
|
||
default: break;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
//inserisci l'indrizzo e-mail
|
||
clifo.put(CLI_PAIV, paiv); //p.iva
|
||
const TString codpag = s.get(12).as_string(); // clifo.put(CLI_, riferimento);
|
||
/* const TString ricavo = s.get(13).as_string();
|
||
int gruppo;
|
||
int conto;
|
||
long sottoconto;
|
||
|
||
decode_conto(ricavo, gruppo, conto, sottoconto);
|
||
clifo.put(CLI_GRUPPORIC, gruppo);
|
||
clifo.put(CLI_CONTORIC, conto);
|
||
clifo.put(CLI_SOTTOCRIC, sottoconto);
|
||
*/
|
||
clifo.put(CLI_COFI, cofi); //cod. fisc.
|
||
}
|
||
|
||
const TString & ricalt = clifo.get(CLI_RICALT);
|
||
|
||
clifo.put(CLI_RICALT, key); //ragsoc
|
||
|
||
TString str;
|
||
str << "Il Cliente codice (" << clifo.get(CLI_CODCF) <<") " << ragsoc << " ";
|
||
|
||
if (found)
|
||
{
|
||
const int err = clifo.rewrite();
|
||
if (err == NOERR)
|
||
{
|
||
str << "<EFBFBD> stato aggiornato";
|
||
log.log(0, str);
|
||
}
|
||
else
|
||
{
|
||
str << "NON <20> stato aggiornato. Errore " << err;
|
||
log.log(2, str);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
const int err = clifo.write();
|
||
if (err == NOERR)
|
||
{
|
||
str << "<EFBFBD> stato inserito";
|
||
log.log(0, str);
|
||
}
|
||
else
|
||
{
|
||
str << "NON <20> stato inserito. Errore " << err;
|
||
log.log(2, str);
|
||
}
|
||
}
|
||
|
||
if (cofi.empty() && paiv.empty())
|
||
{
|
||
TString str;
|
||
str << "Il " << cod << ' ' << ragsoc << " non ha n<> CODICE FISCALE n<> PARTITA IVA";
|
||
log.log(1, str);
|
||
}
|
||
}
|
||
|
||
}
|
||
return;
|
||
}
|
||
|
||
const TString & TImportaEricsoft::decode_codreg(int tiporeg, int numreg)
|
||
{
|
||
const TString8 cod = format("%1d%02d", tiporeg, numreg);
|
||
TString8 codreg = cache().get(TAB_REGISTRI, cod, "S1");
|
||
|
||
if (codreg.blank() && tiporeg > 0)
|
||
codreg = cod;
|
||
return get_tmp_string() = codreg;
|
||
}
|
||
|
||
const TString & TImportaEricsoft::decode_codcaus(int tipocaus, const TString & codcaus)
|
||
{
|
||
const TString8 cod = format("%1d%s", tipocaus, (const char *) codcaus);
|
||
TString8 codc = cache().get(TAB_CAUSALI, cod, "S1");
|
||
|
||
if (codc.blank())
|
||
codc = cod;
|
||
return get_tmp_string() = codc;
|
||
}
|
||
|
||
const TString & TImportaEricsoft::decode_codiva(const TString & codiva)
|
||
{
|
||
TString8 codi = cache().get(TAB_CODIVA, (const char *) codiva, "S1");
|
||
|
||
if (codi.blank())
|
||
codi = codiva;
|
||
return get_tmp_string() = codi;
|
||
}
|
||
|
||
const TString & TImportaEricsoft::decode_codpag(const TString & codpag)
|
||
{
|
||
TString8 codp = cache().get(TAB_PAGAMENTI, (const char *) codpag, "S1");
|
||
|
||
if (codp.blank())
|
||
codp = codpag;
|
||
return get_tmp_string() = codp;
|
||
}
|
||
|
||
const TString & TImportaEricsoft::decode_codval(const TString & codval)
|
||
{
|
||
TString8 codv = cache().get(TAB_VALUTE, (const char *) codval, "S1");
|
||
|
||
if (codv.blank())
|
||
codv = codval;
|
||
return get_tmp_string() = codv;
|
||
}
|
||
|
||
void TImportaEricsoft::decode_conto(const TString & cod, int & gruppo, int & conto, long & sottoconto)
|
||
{
|
||
gruppo = atoi(cod.left(2));
|
||
conto = atoi(cod.mid(2,2));
|
||
sottoconto = atol(cod.right(3));
|
||
const TRectype & rec = cache().get(TAB_CONTI, (const char *) cod);
|
||
|
||
if (rec.full())
|
||
{
|
||
gruppo = rec.get_int("I0");
|
||
conto = rec.get_int("I1");
|
||
sottoconto = rec.get_int("I2");
|
||
}
|
||
}
|
||
|
||
void TImportaEricsoft::save_mov(TLog_report& log, TFilename& ini_file, bool skip)
|
||
{
|
||
TString command = format(FR("cg2 -0 -i%s -u%s"), (const char *)ini_file, (const char *) user());
|
||
TExternal_app a(command) ;
|
||
a.run();
|
||
|
||
{
|
||
TConfig ini(ini_file);
|
||
int errs, warns;
|
||
TString s;
|
||
TString_array vars;
|
||
const long nreg = ini.get_long(MOV_NUMREG, TOSTRING(LF_MOV));
|
||
|
||
if (nreg > 0)
|
||
{
|
||
TLocalisamfile movf(LF_MOV);
|
||
const long tnreg = ini.get_long(MOV_TNUMREG, TOSTRING(LF_MOV));
|
||
TRectype & mov = (TRectype &) cache().get(LF_MOV, nreg);
|
||
|
||
mov.put(MOV_TNUMREG, tnreg);
|
||
mov.rewrite(movf);
|
||
}
|
||
ini.list_variables(vars, false, "Transaction");
|
||
log.log(0, TR("[Transaction]"));
|
||
FOR_EACH_ARRAY_ROW(vars, j, var)
|
||
log.log(0, format(FR("%s = %s"), (const char *) *var, (const char *) ini.get((const char *) *var)));
|
||
if (_msk->get_bool(F_VERBOSE))
|
||
{
|
||
TString_array pars;
|
||
|
||
ini.list_paragraphs(pars);
|
||
FOR_EACH_ARRAY_ROW(pars, i, parname)
|
||
{
|
||
if (*parname != "Transaction" && *parname != "Main")
|
||
{
|
||
ini.list_variables(vars, false, (const char *) *parname);
|
||
log.log(0, format(TR("[%s]"), (const char *) *parname));
|
||
FOR_EACH_ARRAY_ROW(vars, j, var)
|
||
log.log(0, format(FR("%s = %s"), (const char *) *var, (const char *) ini.get((const char *) *var)));
|
||
}
|
||
}
|
||
|
||
}
|
||
for (errs = 0; (s = ini.get("ErrMsg", "Main", errs, "")).full(); errs++)
|
||
log.log(2, s);
|
||
for (warns = 0; (s = ini.get("ErrMsg", "Main", warns, "")).full(); warns++)
|
||
log.log(1, s);
|
||
if (errs > 0)
|
||
log.log(0, TR("Non elaborato"));
|
||
}
|
||
#ifndef _DBG
|
||
remove(ini_file);
|
||
#endif
|
||
}
|
||
|
||
const TString & TImportaEricsoft::find_indetr(const real & imponibile, const real & imponibile_tot)
|
||
{
|
||
real perc_ind = imponibile * CENTO / imponibile_tot;
|
||
|
||
perc_ind.round(0);
|
||
|
||
if (_indetr.items() == 0)
|
||
{
|
||
TISAM_recordset ind("USE %IND");
|
||
|
||
_last = "@";
|
||
for (bool ok = ind.move_first(); ok; ok= ind.move_next())
|
||
{
|
||
const real perc = ind.get("R0").as_real();
|
||
const TString4 cod = ind.get("CODTAB").as_string();
|
||
|
||
_indetr.add(perc.string(), cod);
|
||
if (cod > _last)
|
||
_last = cod;
|
||
}
|
||
}
|
||
if (_indetr.objptr(perc_ind.string()) == NULL)
|
||
{
|
||
TTable ind("%IND");
|
||
|
||
_last[0]++;
|
||
ind.put("CODTAB", _last);
|
||
ind.put("I0", 9);
|
||
ind.put("R0", perc_ind);
|
||
ind.write();
|
||
_indetr.add(perc_ind.string(), _last);
|
||
}
|
||
|
||
return get_tmp_string() = (const TString &)_indetr[perc_ind.string()];
|
||
}
|
||
|
||
void TImportaEricsoft::upload_mov(const TFilename& file, TLog_report& log)
|
||
{
|
||
TPNota_recset s(file);
|
||
long numreg = -1L;
|
||
int gruppo;
|
||
int conto;
|
||
int nrigaIVA = 0;
|
||
int nriga = 0;
|
||
bool skip = false;
|
||
long sottoconto;
|
||
TFilename ini_file;
|
||
TConfig * ini;
|
||
TArray cods_iva;
|
||
TISAM_recordset daily_movs("USE " TOSTRING(LF_MOV) " KEY 2\nFROM " MOV_DATAREG "=#DATE");
|
||
TProgind pi(s.items(),"Importazione Movimenti in corso...",true,true);
|
||
TLocalisamfile clifo(LF_CLIFO);
|
||
|
||
for (bool ok=s.move_first();ok;ok=s.move_next())
|
||
{
|
||
if (!pi.addstatus(1))
|
||
break;
|
||
|
||
if (s.get(1).as_int() == 0L)
|
||
continue;
|
||
|
||
const TDate datareg = s.get(0).as_date();
|
||
const long numreg_eric = s.get(1).as_int();
|
||
const TString4 sezione = s.get(2).as_int() == 0 ? "D" : "A";
|
||
decode_conto(s.get(3).as_string(), gruppo, conto, sottoconto);
|
||
long codcf = s.get(5).as_int();
|
||
TString4 tipocf;
|
||
|
||
switch (s.get(4).as_int())
|
||
{
|
||
case 1:
|
||
tipocf = "C";
|
||
break;
|
||
case 2:
|
||
tipocf = "F";
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
if (codcf != 0L)
|
||
{
|
||
TString16 key; key.format("E%ld", codcf);
|
||
|
||
clifo.setkey(6);
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, tipocf);
|
||
clifo.put(CLI_RICALT, key);
|
||
if (clifo.read() == NOERR)
|
||
codcf = clifo.get_long(CLI_CODCF);
|
||
else
|
||
log.log(3, format(FR("Codice cliente Ericsoft %ld assente"), codcf));
|
||
}
|
||
if (tipocf.full())
|
||
sottoconto = codcf;
|
||
|
||
const TDate datacomp = s.get(6).as_date();
|
||
const int tiporeg = s.get(7).as_int();
|
||
const int numregs = s.get(8).as_int();
|
||
const TString8 codreg = decode_codreg(tiporeg, numregs);
|
||
const long numprot = s.get(9).as_int();
|
||
const TDate datadoc = s.get(10).as_date();
|
||
TString20 numdoc = s.get(12).as_string();
|
||
TString8 codcaus = s.get(13).as_string();
|
||
const int tipocau = s.get(14).as_int();
|
||
|
||
codcaus = decode_codcaus(tipocau, codcaus);
|
||
const TCausale & caus = cached_causale(codcaus, datacomp.year());
|
||
bool mov_iva = caus.iva() > nessuna_iva;
|
||
|
||
const int oper = s.get(15).as_int();
|
||
|
||
TString80 descr = s.get(16).as_string();
|
||
|
||
if (descr.full())
|
||
descr << " ";
|
||
descr << caus.descrizione();
|
||
if (tipocf.full())
|
||
{
|
||
const TString16 key = format("%s|%ld", (const char *) tipocf, codcf);
|
||
descr << " " << cache().get(LF_CLIFO, key, CLI_RAGSOC);
|
||
}
|
||
descr = descr.left(50);
|
||
|
||
const real importo = s.get(17).as_real(); // valuta conto ???
|
||
const real imponibile = s.get(18).as_real(); // valuta conto ???
|
||
const real imposta = s.get(17).as_real(); // valuta conto ???
|
||
const real totdoc = s.get(24).as_real(); // valuta conto ???
|
||
const TString8 codiva = decode_codiva(s.get(19).as_string()); //
|
||
const bool annullato = s.get(30).as_int() == 1;
|
||
const TString8 codpag = decode_codpag(s.get(33).as_string());
|
||
const TDate datainsca = s.get(34).as_date();
|
||
const TString8 codval = decode_codval(s.get(36).as_string());
|
||
const real impval = s.get(37).as_real();
|
||
const real cambio = s.get(38).as_real(); // valuta conto ???
|
||
const TDate datacam = s.get(39).as_date();
|
||
const bool riga_iva = s.get(40).as_int() == 1;
|
||
const bool riga_cont = !riga_iva;
|
||
const bool riga_storno_iva = riga_iva && s.get(20).as_int() == 1;
|
||
const int nriga_eric = s.get(41).as_int();
|
||
|
||
if (numreg_eric != numreg)
|
||
{
|
||
if (numreg > 0L)
|
||
{
|
||
delete ini;
|
||
ini = NULL;
|
||
skip |= (nriga == 0) && (nrigaIVA == 0);
|
||
save_mov(log, ini_file, skip);
|
||
}
|
||
if (!datareg.ok() || numreg_eric == 0)
|
||
continue;
|
||
log.log(0, format("Movimento Ericsoft n.ro %ld", numreg_eric));
|
||
cods_iva.destroy();
|
||
numreg = numreg_eric;
|
||
nriga = 0;
|
||
nrigaIVA = 0L;
|
||
ini_file.temp("eric", "ini");
|
||
ini = new TConfig(ini_file, "Transaction");
|
||
|
||
char mode[2] = { TM_AUTOMATIC, '\0' };
|
||
long nreg = 0;
|
||
|
||
daily_movs.set_var("#DATE", datareg.stringa());
|
||
for (bool ok = daily_movs.move_first(); ok && nreg == 0L; ok = daily_movs.move_next())
|
||
if (numreg == daily_movs.get(MOV_TNUMREG).as_int())
|
||
nreg = daily_movs.get(MOV_NUMREG).as_int();
|
||
|
||
if (nreg == 0L)
|
||
ini->set("Action", TRANSACTION_INSERT);
|
||
else
|
||
ini->set("Action", TRANSACTION_MODIFY);
|
||
ini->set("Mode", mode);
|
||
ini->set_paragraph(LF_MOV);
|
||
if (nreg != 0L)
|
||
ini->set(MOV_NUMREG, nreg);
|
||
ini->set(MOV_DATAREG, datareg);
|
||
ini->set(MOV_ANNOIVA, datareg.year());
|
||
ini->set(MOV_ANNOES, _esc.esercizio(datacomp).codice());
|
||
ini->set(MOV_TIPO, tipocf);
|
||
ini->set(MOV_CODCF, codcf);
|
||
ini->set(MOV_DATADOC, datadoc);
|
||
ini->set(MOV_NUMDOC, numdoc);
|
||
ini->set(MOV_TNUMREG, numreg);
|
||
|
||
ini->set(MOV_TOTDOC, totdoc.string());
|
||
ini->set(MOV_TOTDOCVAL, impval.string());
|
||
ini->set(MOV_CAMBIO, cambio.string());
|
||
ini->set(MOV_DATACAM, datacam);
|
||
ini->set(MOV_CODVAL, codval);
|
||
ini->set(MOV_CODCAUS, codcaus);
|
||
ini->set(MOV_DESCR, descr);
|
||
ini->set(MOV_REG, codreg);
|
||
// ini->set(MOV_PROTIVA, numprot);
|
||
ini->set(MOV_CODPAG, codpag);
|
||
ini->set(MOV_IVAXCASSA, tipocau == 8 || tipocau == 9 ? "X" : "" );
|
||
ini->set(MOV_LIQDIFF, tipocau == 8 || tipocau == 9 ? "X" : "" );
|
||
ini->set(MOV_DATACOMP, datacomp);
|
||
}
|
||
|
||
if (!mov_iva && riga_cont)
|
||
{
|
||
ini->set_paragraph(format("%d,%d",LF_RMOV, ++nriga));
|
||
|
||
ini->set(RMV_ANNOES, _esc.esercizio(datacomp).codice());
|
||
ini->set(RMV_DATAREG, datareg);
|
||
ini->set(RMV_NUMRIG, nriga);
|
||
ini->set(RMV_SEZIONE, sezione);
|
||
ini->set(RMV_DESCR, descr);
|
||
ini->set(RMV_IMPORTO, importo.string());
|
||
ini->set(RMV_TIPOC, tipocf);
|
||
ini->set(RMV_GRUPPO, gruppo);
|
||
ini->set(RMV_CONTO, conto);
|
||
ini->set(RMV_SOTTOCONTO, sottoconto);
|
||
}
|
||
else
|
||
if (riga_iva)
|
||
{
|
||
if (riga_storno_iva)
|
||
{
|
||
bool found = false;
|
||
for (int riga = 1 ; !found && cods_iva.objptr(riga) != NULL; riga++)
|
||
if (codiva == (TString &) cods_iva[riga] && ini->get(RMI_TIPODET).blank())
|
||
{
|
||
found = true;
|
||
ini->set_paragraph(format("%d,%d",LF_RMOVIVA, ++nriga));
|
||
real imponibile_tot = ini->get(RMI_IMPONIBILE);
|
||
ini->set(RMI_TIPODET, find_indetr(imponibile, imponibile_tot));
|
||
}
|
||
if (!found)
|
||
log.log(2, format(FR("Codice IVA %s di storno indetraibilit<69> non trovato"), (const char *) codiva));
|
||
|
||
}
|
||
else
|
||
{
|
||
ini->set_paragraph(format("%d,%d",LF_RMOVIVA, ++nrigaIVA));
|
||
ini->set(RMI_ANNOES, _esc.esercizio(datacomp).codice());
|
||
ini->set(RMI_NUMRIG, nrigaIVA);
|
||
ini->set(RMI_CODIVA, codiva);
|
||
ini->set(RMI_IMPONIBILE, imponibile.string());
|
||
ini->set(RMI_IMPOSTA, imposta.string());
|
||
ini->set(RMI_TIPOC, tipocf);
|
||
ini->set(RMI_GRUPPO, gruppo);
|
||
ini->set(RMI_CONTO, conto);
|
||
ini->set(RMI_SOTTOCONTO, sottoconto);
|
||
cods_iva.add(codiva, nrigaIVA);
|
||
ini->set_paragraph(LF_MOV);
|
||
ini->set(MOV_TOTDOC, totdoc.string());
|
||
ini->set(MOV_TOTDOCVAL, impval.string());
|
||
ini->set(MOV_CAMBIO, cambio.string());
|
||
}
|
||
}
|
||
else
|
||
if (nriga == 0 && tipocf.full())
|
||
{
|
||
ini->set_paragraph(format("%d,%d",LF_RMOV, ++nriga));
|
||
ini->set(RMV_ANNOES, _esc.esercizio(datacomp).codice());
|
||
ini->set(RMV_DATAREG, datareg);
|
||
ini->set(RMV_NUMRIG, nriga);
|
||
// ini->set(RMV_SEZIONE, sezione);
|
||
ini->set(RMV_DESCR, descr);
|
||
// ini->set(RMV_IMPORTO, importo.string());
|
||
ini->set(RMV_TIPOC, tipocf);
|
||
ini->set(RMV_GRUPPO, gruppo);
|
||
ini->set(RMV_CONTO, conto);
|
||
ini->set(RMV_SOTTOCONTO, sottoconto);
|
||
ini->set(RMV_ROWTYPE, "T");
|
||
}
|
||
}
|
||
if (numreg > 0L)
|
||
{
|
||
delete ini;
|
||
ini = NULL;
|
||
skip |= (nriga == 0) && (nrigaIVA == 0);
|
||
save_mov(log, ini_file, skip);
|
||
}
|
||
}
|
||
|
||
bool TImportaEricsoft::transfer()
|
||
{
|
||
TLog_report log("Trasferimento Anagrafiche");
|
||
TLog_report log1("Trasferimento Prima Nota");
|
||
|
||
//prelevo il path dei file da caricare dalla maschera
|
||
TFilename path = _msk->get(F_PATH);
|
||
//eseguo tutte le importazioni (una alla volta)
|
||
TFilename name = _msk->get(F_CLIFOR);
|
||
|
||
if (name.full())
|
||
{
|
||
//costruisco il nome del file
|
||
TFilename full = path;
|
||
full.add(name);
|
||
|
||
//se eiste il file, esegui l'elaborazione corretta
|
||
if (full.exist())
|
||
upload_cli(full, log);
|
||
}
|
||
|
||
TSheet_field & sh = _msk->sfield(F_MOVSH);
|
||
const int items = sh.items();
|
||
|
||
for(int f = 0; f <items; f++)
|
||
{
|
||
name = sh.row(f).get(0);
|
||
if (name.full())
|
||
{
|
||
//costruisco il nome del file
|
||
TFilename full = path;
|
||
full.add(name);
|
||
|
||
//se eiste il file, esegui l'elaborazione corretta
|
||
if (full.exist())
|
||
upload_mov(full, log1);
|
||
}
|
||
}
|
||
|
||
TReport_book buc;
|
||
TReport_book buc1;
|
||
|
||
buc.add(log);
|
||
if (buc.pages()>0)
|
||
buc.preview(); //visualizzo il log di importazione delle anagrafiche
|
||
|
||
buc1.add(log1);
|
||
if (buc1.pages()>0)
|
||
buc1.preview(); //visualizzo il log di importazione dei documenti
|
||
|
||
return true;
|
||
}
|
||
|
||
bool TImportaEricsoft::create()
|
||
{
|
||
_msk = new TImportaEricsoft_mask();
|
||
return TSkeleton_application::create();
|
||
}
|
||
|
||
bool TImportaEricsoft::destroy()
|
||
{
|
||
delete _msk;
|
||
return TApplication::destroy();
|
||
}
|
||
|
||
void TImportaEricsoft::main_loop()
|
||
{
|
||
while (_msk->run() == K_ENTER)
|
||
if (transfer())
|
||
message_box(TR("Importazione completata"));
|
||
}
|
||
|
||
int tc3100 (int argc, char* argv[])
|
||
{
|
||
TImportaEricsoft main_app;
|
||
main_app.run(argc, argv, TR("Importazione da ericsoft"));
|
||
return true;
|
||
}
|