1112ddfa73
git-svn-id: svn://10.65.10.50/branches/R_10_00@22776 c028cbd2-c16b-5b4b-a496-9718f37d4682
480 lines
12 KiB
C++
480 lines
12 KiB
C++
#include <automask.h>
|
||
#include <applicat.h>
|
||
#include <progind.h>
|
||
#include <reputils.h>
|
||
#include <textset.h>
|
||
#include <utility.h>
|
||
|
||
#include "../cg/cglib01.h"
|
||
#include "ps0430400a.h"
|
||
|
||
#include "../ve/velib.h"
|
||
|
||
#include "../cg/cfban.h"
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TCBA_recset
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TCBA_recset : public TCSV_recordset
|
||
{
|
||
public:
|
||
virtual const TVariant& get(unsigned int column) const;
|
||
TDate get_date(unsigned int column) const;
|
||
TCBA_recset() : TCSV_recordset("CSV(;)") {}
|
||
};
|
||
|
||
const TVariant& TCBA_recset::get(unsigned int column) const
|
||
{
|
||
const TVariant& var = TCSV_recordset::get(column);
|
||
if (var.is_string())
|
||
{
|
||
TString80 str = var.as_string();
|
||
if (str[0] == '"')
|
||
{
|
||
str.strip("\"");
|
||
return get_tmp_var() = str;
|
||
}
|
||
}
|
||
return var;
|
||
}
|
||
|
||
TDate TCBA_recset::get_date(unsigned int column) const
|
||
{
|
||
const TVariant& var = TCSV_recordset::get(column);
|
||
const long v = var.as_int();
|
||
const int d = v / 1000000;
|
||
const int m = (v / 10000) % 100;
|
||
const int y = v % 10000;
|
||
return TDate(d, m, y);
|
||
}
|
||
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TImport_mask
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TImport_mask : public TAutomask
|
||
{
|
||
protected:
|
||
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
|
||
|
||
public:
|
||
TImport_mask() : TAutomask("ps0430400a") {}
|
||
};
|
||
|
||
bool TImport_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
|
||
{
|
||
switch (o.dlg())
|
||
{
|
||
case F_CLI:
|
||
case F_FAT:
|
||
if (e == fe_button)
|
||
{
|
||
TFilename n = get(F_DIR);
|
||
n.add("*.csv");
|
||
if (input_filename(n))
|
||
o.set(n.name());
|
||
} else
|
||
if (e == fe_close)
|
||
{
|
||
TFilename n = get(F_DIR);
|
||
n.add(o.get());
|
||
if (!n.exist())
|
||
return cantread_box(n);
|
||
}
|
||
break;
|
||
default: break;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TImport_app
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TImport_app : public TSkeleton_application
|
||
{
|
||
protected:
|
||
virtual const char* extra_modules() const { return "ca|ve"; }
|
||
virtual void main_loop();
|
||
|
||
public:
|
||
long find_clifo(const TString& cf_pi, TLocalisamfile& clifo) const;
|
||
const TString& find_codart(const TString& codart) const;
|
||
bool find_or_create_clifo(const TCBA_recset& cli, TLocalisamfile& clifo, TLog_report& log) const;
|
||
bool importa_clienti(const TFilename& fname, TLog_report& log) const;
|
||
bool importa_fatture(const TFilename& fname, TLog_report& log) const;
|
||
};
|
||
|
||
long TImport_app::find_clifo(const TString& cf_pi, TLocalisamfile& clifo) const
|
||
{
|
||
if (cf_pi.blank() || cf_pi.len() < 10)
|
||
return 0;
|
||
|
||
TString cod = cf_pi;
|
||
cod.trim();
|
||
cod.upper();
|
||
|
||
// Tento prima il codice fiscale o la partita IVA?
|
||
const char* const cofi_paiv = cod.len() > 16 ? "PC" : "CP";
|
||
|
||
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& TImport_app::find_codart(const TString& codart) const
|
||
{
|
||
TString80 cod = codart;
|
||
cod.trim(); cod.upper();
|
||
const TRectype& anamag = cache().get(LF_ANAMAG, cod);
|
||
|
||
#ifdef DBG
|
||
if (anamag.empty())
|
||
cod = "45.01.06.052";
|
||
#endif
|
||
|
||
return get_tmp_string() = cod;
|
||
}
|
||
|
||
|
||
bool TImport_app::find_or_create_clifo(const TCBA_recset& cli, TLocalisamfile& clifo, TLog_report& log) const
|
||
{
|
||
TString cofi = cli.get(2).as_string();
|
||
if (cofi.full() && find_clifo(cofi, clifo))
|
||
return true;
|
||
|
||
TString piva = cli.get(1).as_string();
|
||
if (piva.full() && find_clifo(piva, clifo))
|
||
return true;
|
||
|
||
const TString ragsoc = cli.get(6).as_string();
|
||
|
||
if (piva.blank() && cofi.blank())
|
||
{
|
||
TString msg;
|
||
msg << ragsoc << TR(" non ha n<> codice fiscale n<> partita IVA")
|
||
<< TR(" alla riga ") << (cli.current_row()+1);
|
||
log.log(2, msg);
|
||
return false;
|
||
}
|
||
|
||
long next_clifo = 1;
|
||
TISAM_recordset recset("USE CLIFO\tTO TIPOCF=C");
|
||
if (recset.move_last())
|
||
next_clifo += recset.get(CLI_CODCF).as_int();
|
||
|
||
clifo.setkey(1);
|
||
clifo.zero();
|
||
clifo.put(CLI_TIPOCF, 'C');
|
||
clifo.put(CLI_CODCF, next_clifo);
|
||
clifo.put(CLI_RAGSOC, ragsoc);
|
||
|
||
cofi.trim(); cofi.upper();
|
||
clifo.put(CLI_COFI, cofi);
|
||
|
||
piva.trim(); piva.upper();
|
||
clifo.put(CLI_PAIV, piva);
|
||
|
||
const int err = clifo.write();
|
||
if (err != NOERR)
|
||
{
|
||
TString msg;
|
||
msg << TR("Impossibile creare l'anagrafica di ") << ragsoc << TR(" alla riga ") << (cli.current_row()+1);
|
||
log.log(2, msg);
|
||
}
|
||
|
||
return err == 0;
|
||
}
|
||
|
||
bool TImport_app::importa_clienti(const TFilename& fname, TLog_report& log) const
|
||
{
|
||
TString str = TR("Importazione clienti");
|
||
log.log(0, str);
|
||
|
||
TLocalisamfile clifo(LF_CLIFO);
|
||
|
||
TCBA_recset cli;
|
||
cli.load_file(fname);
|
||
|
||
long clifos = 0;
|
||
|
||
bool done = true;
|
||
|
||
TProgind pi(cli.items(), str);
|
||
for (bool ok = cli.move_first(); ok && done; ok = cli.move_next())
|
||
{
|
||
done = pi.addstatus(1);
|
||
if (!done)
|
||
break;
|
||
|
||
if (cli.get(0).is_empty()) // Salta righe vuote
|
||
continue;
|
||
|
||
done = find_or_create_clifo(cli, clifo, log);
|
||
if (!done)
|
||
break;
|
||
|
||
// Aggiorna dati cliente
|
||
|
||
const int privato = cli.get(27).as_int();
|
||
|
||
TRectype& curr = clifo.curr();
|
||
|
||
TString rs1 = cli.get(6).as_string(); rs1.trim();
|
||
TString rs2 = cli.get(7).as_string(); rs2.trim();
|
||
|
||
if (rs2.blank())
|
||
{
|
||
const int spc = rs1.find(' ');
|
||
if (spc > 0)
|
||
{
|
||
str = rs1.left(spc);
|
||
str.left_just(30);
|
||
str << rs1.mid(spc+1);
|
||
}
|
||
}
|
||
|
||
if (str.empty())
|
||
{
|
||
str << rs1 << ' ' << rs2;
|
||
str.strip_double_spaces();
|
||
}
|
||
TParagraph_string ragsoc(str, 50);
|
||
curr.put(CLI_RAGSOC, ragsoc.get());
|
||
|
||
curr.put(CLI_INDCF, cli.get(8).as_string());
|
||
|
||
const TString& loc = cli.get(10).as_string();
|
||
curr.put(CLI_LOCCF, loc);
|
||
|
||
const TString& cap = cli.get(9).as_string();
|
||
curr.put(CLI_CAPCF, cap);
|
||
|
||
const int naz = cli.get(16).as_int();
|
||
if (naz == 0)
|
||
{
|
||
const TString& com = cap2comune(cap, loc);
|
||
curr.put(CLI_COMCF, com);
|
||
}
|
||
|
||
int alleg = 0;
|
||
if (privato == 1)
|
||
alleg = 6; else
|
||
if (naz > 0)
|
||
alleg = naz == 1 ? 5 : 9;
|
||
|
||
const TString8 abi = cli.get(17).as_string();
|
||
const TString8 cab = cli.get(18).as_string();
|
||
if (abi.len() == 5 && cab.len() == 5)
|
||
{
|
||
TToken_string key;
|
||
key = "C";
|
||
key.add(curr.get(CLI_CODCF));
|
||
key.add("N");
|
||
TRecord_array nsban(key, LF_CFBAN);
|
||
TRectype& rec = nsban.row(1, true);
|
||
rec.put(CFBAN_ABI, abi);
|
||
rec.put(CFBAN_CAB, cab);
|
||
rec.put(CFBAN_NUMCC, cli.get(23).as_string());
|
||
|
||
key.cut(0) << abi << cab;
|
||
const TString& iban = cache().get("BNP", key, "S3");
|
||
rec.put(CFBAN_IBAN, iban);
|
||
|
||
const int err = nsban.rewrite();
|
||
if (err != NOERR)
|
||
{
|
||
ragsoc.strip_double_spaces();
|
||
str.cut(0) << TR("Impossibile aggiornare la banca di ") << ragsoc << TR(" alla riga ") << (cli.current_row()+1);
|
||
log.log(1, str);
|
||
}
|
||
}
|
||
|
||
curr.put(CLI_CODPAG, cli.get(20).as_string());
|
||
curr.put(CLI_MAIL, cli.get(28).as_string());
|
||
|
||
const int err = clifo.rewrite();
|
||
if (err == NOERR)
|
||
{
|
||
clifos++;
|
||
}
|
||
else
|
||
{
|
||
ragsoc.strip_double_spaces();
|
||
str.cut(0) << TR("Impossibile aggiornare l'anagrafica di ") << ragsoc << TR(" alla riga ") << (cli.current_row()+1);
|
||
log.log(2, str);
|
||
break;
|
||
}
|
||
}
|
||
|
||
log.log(0, str.cut(0));
|
||
str << TR("Sono state importate/aggiornate ") << clifos << TR(" anagrafiche");
|
||
log.log(0, str);
|
||
log.log(0, str.cut(0));
|
||
|
||
return done;
|
||
}
|
||
|
||
bool TImport_app::importa_fatture(const TFilename& fname, TLog_report& log) const
|
||
{
|
||
TString str = TR("Importazione documenti");
|
||
log.log(0, str);
|
||
|
||
TLocalisamfile clifo(LF_CLIFO);
|
||
|
||
TCBA_recset fat;
|
||
fat.load_file(fname);
|
||
|
||
long ndocs = 0;
|
||
|
||
bool done = true;
|
||
|
||
long codcf = 0;
|
||
TString4 codpag;
|
||
|
||
TProgind pi(fat.items(), str);
|
||
for (bool ok = fat.move_first(); ok && done; ok = fat.move_next())
|
||
{
|
||
done = pi.addstatus(1);
|
||
if (!done)
|
||
break;
|
||
|
||
const TString4 tipo = fat.get(8).as_string();
|
||
|
||
if (tipo == "T")
|
||
{
|
||
TString80 cofi_paiv = fat.get(14).as_string();
|
||
cofi_paiv.trim(); cofi_paiv.upper();
|
||
codcf = find_clifo(cofi_paiv, clifo);
|
||
if (codcf <= 0)
|
||
{
|
||
str.format("Impossibile associare un cliente al C.F./P.I '%s' alla riga %ld",
|
||
(const char*)cofi_paiv, fat.current_row()+1);
|
||
log.log(2, str);
|
||
}
|
||
codpag = fat.get(11).as_string();
|
||
continue;
|
||
}
|
||
|
||
if (tipo != "N" || codcf <= 0)
|
||
continue; // Considera solo le righe articolo di clienti validi
|
||
|
||
const TDate datadoc = fat.get_date(5);
|
||
const long ndoc = fat.get(6).as_int();
|
||
if (!datadoc.ok() || ndoc <= 0)
|
||
{
|
||
str.format("Data o numero documento nulli alla riga %ld", fat.current_row()+1);
|
||
log.log(2, str);
|
||
continue;
|
||
}
|
||
|
||
const TString codart = find_codart(fat.get(12).as_string());
|
||
TString4 um;
|
||
const TRectype& anamag = cache().get(LF_ANAMAG, codart);
|
||
if (anamag.empty())
|
||
{
|
||
str.format("Articolo non valido '%s' alla riga %ld", (const char*)codart, fat.current_row()+1);
|
||
log.log(1, str);
|
||
}
|
||
else
|
||
{
|
||
str = codart; str << "|1";
|
||
um = cache().get(LF_UMART, str, UMART_UM);
|
||
}
|
||
|
||
TDocumento doc('D', datadoc.year(), "F01", ndoc);
|
||
|
||
doc.put(DOC_TIPOCF, 'C');
|
||
doc.put(DOC_CODCF, codcf);
|
||
doc.put(DOC_DATADOC, datadoc);
|
||
doc.put(DOC_CODPAG, codpag);
|
||
|
||
str = fat.get(22).as_string();
|
||
str.right_just(20, '0');
|
||
doc.put(DOC_CODCMS, str);
|
||
|
||
if (doc.rows() == 0) // Nuovo documento
|
||
{
|
||
doc.put(DOC_TIPODOC, "F01");
|
||
doc.put(DOC_STATO, 2);
|
||
doc.new_row("01");
|
||
}
|
||
|
||
TRiga_documento& rdoc = doc[doc.physical_rows()];
|
||
rdoc.put(RDOC_CODART, codart);
|
||
rdoc.put(RDOC_CODARTMAG, codart);
|
||
rdoc.checked(true);
|
||
rdoc.put(RDOC_DESCR, anamag.get(ANAMAG_DESCR));
|
||
rdoc.put(RDOC_UMQTA, um);
|
||
rdoc.put(RDOC_QTA, UNO);
|
||
rdoc.put(RDOC_PREZZO, fat.get(19).as_real());
|
||
rdoc.put(RDOC_CODIVA, anamag.get(ANAMAG_CODIVA));
|
||
|
||
const int err = doc.write();
|
||
if (err == NOERR)
|
||
{
|
||
ndocs++;
|
||
}
|
||
else
|
||
{
|
||
str.format("Impossibile registrare il documento %ld alla riga %ld", ndoc, fat.current_row()+1);
|
||
log.log(2, str);
|
||
}
|
||
|
||
}
|
||
|
||
log.log(0, str.cut(0));
|
||
str << TR("Sono stati importati/aggiornati ") << ndocs << TR(" documenti");
|
||
log.log(0, str);
|
||
|
||
return done;
|
||
}
|
||
|
||
void TImport_app::main_loop()
|
||
{
|
||
open_files(LF_TAB, LF_TABCOM, LF_CLIFO, LF_DOC, LF_RIGHEDOC, LF_ANAMAG, LF_UMART, 0);
|
||
|
||
TImport_mask mask;
|
||
while (mask.run() == K_ENTER)
|
||
{
|
||
TLog_report log;
|
||
TFilename fname = mask.get(F_DIR);
|
||
fname.add(mask.get(F_CLI));
|
||
if (importa_clienti(fname, log))
|
||
{
|
||
fname = mask.get(F_DIR);
|
||
fname.add(mask.get(F_FAT));
|
||
importa_fatture(fname, log);
|
||
}
|
||
|
||
log.preview();
|
||
}
|
||
}
|
||
|
||
int ps0430400(int argc, char* argv[])
|
||
{
|
||
TImport_app a;
|
||
a.run(argc, argv, TR("Importazione CBA"));
|
||
return 0;
|
||
}
|