Files correlati : cg2.exe cg2100c.msk cg2100t.msk Commento: Nelle fatture intra l'identificativo SDI viene recuperato da FPPRO e scritto sulla fattura di acquisto e sulla regolarizzazione
501 lines
12 KiB
C++
501 lines
12 KiB
C++
#include "f1lib.h"
|
|
#include "config.h"
|
|
#include "../cg/cglib.h"
|
|
#include "execp.h"
|
|
#include "cfven.h"
|
|
#include "reputils.h"
|
|
#include "causali.h"
|
|
#include "clifo.h"
|
|
#include "../fp/fplib.h"
|
|
|
|
//////////////////////////////////////////////////////////
|
|
// TFppro
|
|
//////////////////////////////////////////////////////////
|
|
|
|
TFppro& fppro_db()
|
|
{
|
|
static TFppro* fppro = nullptr;
|
|
if (fppro == nullptr)
|
|
fppro = new TFppro();
|
|
return *fppro;
|
|
}
|
|
|
|
bool TFppro::sql_inj(TToken_string& keys)
|
|
{
|
|
TToken_string str("", ';');
|
|
|
|
str.add(keys.get(0));
|
|
str.add(keys.get(1));
|
|
str.add(keys.get(2));
|
|
return str.contains("--") || str.contains("SELECT") || str.contains("DELETE") || str.contains("DROP");
|
|
}
|
|
|
|
bool TFppro::check_reg(TToken_string& keys, int numreg)
|
|
{
|
|
return get_numreg(keys) == numreg;
|
|
}
|
|
|
|
bool TFppro::guess_the_doc(const TLocalisamfile& mov)
|
|
{
|
|
// Prendo il fornitore del mov per controllare la p.iva
|
|
TCli_for forn = cached_clifor('F', mov.get_long(MOV_CODCF));
|
|
|
|
const TString4 statopiva(forn.get(CLI_STATOPAIV));
|
|
|
|
const real tot = mov.get_real(MOV_TOTDOC) + mov.get_real(MOV_RITFIS) + mov.get_real(MOV_RITSOC);
|
|
TString numdoc = mov.get(MOV_NUMDOCEXT);
|
|
|
|
if (numdoc.empty())
|
|
numdoc = mov.get(MOV_NUMDOC);
|
|
// Controllo datadoc - numdoc - totdoc - p.iva
|
|
|
|
TString query;
|
|
|
|
query << "SELECT PQ_KEYPRGINVIO AS KEYPRGINVIO, PQ_KEYHEADERFATT AS KEYHEADERFATT, PQ_KEYBODYFATT AS KEYBODYFATT, P7_DATA AS DATA,\n" <<
|
|
"\tP7_NUMERO AS NUMDOC, PQ_IMPTOTDOC AS IMPTOTDOC, P2_FISCIVAPAESE AS STATOPIVA, P2_FISCIVACOD AS PIVA FROM PAA2700F\n" <<
|
|
"JOIN PAA0700F ON PQ_KEYPRGINVIO = P7_KEYPRGINVIO AND PQ_KEYHEADERFATT = P7_KEYHEADERFATT AND PQ_KEYBODYFATT = P7_KEYBODYFATT\n" <<
|
|
"JOIN PAA0200F ON PQ_KEYPRGINVIO = P2_KEYPRGINVIO AND PQ_KEYHEADERFATT = P2_KEYHEADERFATT AND PQ_KEYBODYFATT = P2_KEYBODYFATT\n" <<
|
|
"WHERE P7_DATA = '" << mov.get_date(MOV_DATADOC).date2ansi() << "' AND \n" <<
|
|
"(P7_NUMERO = '" << numdoc << "' OR P7_NUMERO LIKE '%" << numdoc << "%') \n";
|
|
if (statopiva.full())
|
|
query << "' AND \n" << "P2_FISCIVAPAESE = '" << statopiva;
|
|
query << "' AND \n" << "P2_FISCIVACOD = '" << forn.get(CLI_PAIV);
|
|
if (forn.gruppo_IVA())
|
|
query << "' AND \n" "P2_COCAZZO = '" << forn.get(CLI_COFI); // cazzo;
|
|
query << "PQ_IMPTOTDOC = " << tot;
|
|
|
|
if (_db->sq_set_exec(query) && _db->sq_items() == 1)
|
|
{
|
|
_keys = _db->sq_get("KEYPRGINVIO");
|
|
_keys.add(_db->sq_get("KEYHEADERFATT"));
|
|
_keys.add(_db->sq_get("KEYBODYFATT"));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const char* TFppro::get_keys_fppro()
|
|
{
|
|
TString& keys = get_tmp_string(64);
|
|
|
|
keys.cut(0) << _keys.get(0) << ";" << _keys.get(1) << ";" << _keys.get(2);
|
|
return keys;
|
|
}
|
|
|
|
long TFppro::get_numreg()
|
|
{
|
|
if (!_is_set)
|
|
{
|
|
if (set_query())
|
|
return _db->sq_get_long("PZ_NUMREGCONT");
|
|
return -1;
|
|
}
|
|
return _db->sq_get_long("PZ_NUMREGCONT");
|
|
}
|
|
|
|
long TFppro::get_numreg(TToken_string& keys)
|
|
{
|
|
set_keys(keys);
|
|
return get_numreg();
|
|
}
|
|
|
|
long TFppro::get_codforn()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_CLIFOR FROM FPPRO00F\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get_long("PZ_CLIFOR");
|
|
}
|
|
|
|
TDate TFppro::get_datareg()
|
|
{
|
|
if (!_is_set)
|
|
{
|
|
if (set_query())
|
|
return _db->sq_get_date("PZ_DATAREGCONT");
|
|
return TDate();
|
|
}
|
|
return _db->sq_get_date("PZ_DATAREGCONT");
|
|
}
|
|
|
|
TDate TFppro::get_datareg(TToken_string& keys)
|
|
{
|
|
set_keys(keys);
|
|
if (_is_set)
|
|
return get_datareg();
|
|
return TDate();
|
|
}
|
|
|
|
real TFppro::get_ritenute()
|
|
{
|
|
real imp;
|
|
TString query;
|
|
|
|
query << "SELECT P7_IMPORTORIT AS IMPORTO FROM PAA0700F\n" <<
|
|
"WHERE P7_KEYPRGINVIO = '" << _keys.get(0) << "' AND P7_KEYHEADERFATT = '" << _keys.get(1) << "' AND P7_KEYBODYFATT = '" << _keys.get(2) << "'";
|
|
_db->sq_set_exec(query, false);
|
|
for(bool ok = _db->sq_next(); ok; ok = _db->sq_next())
|
|
imp += _db->sq_get_real("IMPORTO");
|
|
return imp;
|
|
}
|
|
|
|
TFppro& TFppro::set_keys(TToken_string& keys)
|
|
{
|
|
if (sql_inj(keys))
|
|
{
|
|
TString msg; msg << "Database error: Possibile SQL INJECTION:\n" << keys;
|
|
;
|
|
error_box(msg);
|
|
_last_error.cut(0) << msg;
|
|
}
|
|
else
|
|
{
|
|
TString prginvio = keys.get(0);
|
|
TString headerfatt = keys.get(1);
|
|
TString bodyfatt = keys.get(2);
|
|
|
|
if (prginvio.full() && headerfatt.full() && bodyfatt.full())
|
|
{
|
|
_keys = keys;
|
|
_keys_setted = true;
|
|
}
|
|
else
|
|
{
|
|
_last_error = "Chiavi settate non valide:\n";
|
|
_last_error << keys;
|
|
_keys_setted = false;
|
|
}
|
|
}
|
|
_is_set = false;
|
|
return *this;
|
|
}
|
|
|
|
bool TFppro::associa_mov(const long numreg)
|
|
{
|
|
TRectype & mov = (TRectype &) cache().get(LF_MOV, numreg);
|
|
bool ok = mov.full();
|
|
|
|
if (ok)
|
|
{
|
|
TString query;
|
|
query << "UPDATE FPPRO00F \nSET " <<
|
|
"PZ_TIPOCF = 'F', PZ_CLIFOR = '" << mov.get(MOV_CODCF) << "', PZ_NUMREGCONT = '" <<
|
|
mov.get(MOV_NUMREG) << "', PZ_DATAREGCONT = '" << mov.get_date(MOV_DATAREG).date2ansi() << "'\n" <<
|
|
fppro_db().where_str() << ";";
|
|
ok = _db->sq_set_exec(query) && _db->sq_commit();
|
|
|
|
if (ok) // Scrivo sul movimento il riferimento al fppro
|
|
{
|
|
mov.put(MOV_IDDOCSDI, fppro_db().get_numdoc());
|
|
mov.put(MOV_DATADOCSDI, fppro_db().get_numdoc());
|
|
mov.put(MOV_KEYFPPRO, get_keys_fppro());
|
|
ok = mov.rewrite() == NOERR;
|
|
}
|
|
else // Se la scrittura sul db fallisce levo sul movimento il riferimento all'fppro
|
|
{
|
|
mov.zero(MOV_IDDOCSDI);
|
|
mov.zero(MOV_DATADOCSDI);
|
|
mov.zero(MOV_KEYFPPRO);
|
|
mov.rewrite();
|
|
}
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
const TString & TFppro::get_tipoprot()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_TIPOPROT AS TIPOPROT FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get("TIPOPROT");
|
|
}
|
|
|
|
const TString & TFppro::get_numprot()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_NUMPROT AS NUMPROT FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
|
|
return _db->sq_get("NUMPROT");
|
|
}
|
|
|
|
const TString & TFppro::get_annoprot()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_ANNOPROT AS ANNOPROT FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get("ANNOPROT");
|
|
}
|
|
|
|
const TString & TFppro::get_dataoraric()
|
|
{
|
|
TString query;
|
|
TString & data = get_tmp_string(16);
|
|
|
|
query << "SELECT PZ_DATAORARIC AS DATAORARIC FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
data << _db->sq_get_date("DATAORARIC").date2ansi();
|
|
return data;
|
|
}
|
|
|
|
const TString & TFppro::get_tipodoc()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_TIPODOC AS TIPODOC FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get("TIPODOC");
|
|
}
|
|
|
|
const TString & TFppro::get_numdoc()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_NUMERO AS NUMDOC FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get("NUMDOC");
|
|
}
|
|
|
|
TDate TFppro::get_data_first_doc() const
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT min(a.DATA) AS DATA \nFROM( \n\tSELECT P7_DATA as DATA \n" <<
|
|
"\tFROM PAA0700F \n\tUNION \n\tSELECT P7_DATA as DATA \n\tFROM PAF0700F \n) a";
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get_date("DATA");
|
|
}
|
|
|
|
TDate TFppro::get_data_doc()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_DATA FROM FPPRO00F\r\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get_date("PZ_DATA");
|
|
}
|
|
|
|
|
|
TDate TFppro::get_dataorarioric()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT P1_DATAORARIC \nFROM PAA0100F \n" <<
|
|
"WHERE P1_KEYPRGINVIO = '" << _keys.get(0) << "' AND P1_KEYHEADERFATT = '" << _keys.get(1) << "' AND P1_KEYBODYFATT = '" << _keys.get(2) << "'";
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get_date("P1_DATAORARIC");
|
|
}
|
|
|
|
long TFppro::get_numregcont()
|
|
{
|
|
TString query;
|
|
|
|
query << "SELECT PZ_NUMREGCONT FROM FPPRO00F\n" << where_str();
|
|
_db->sq_set_exec(query);
|
|
return _db->sq_get_long("PZ_NUMREGCONT");
|
|
}
|
|
|
|
TString TFppro::get_last_error(bool clear)
|
|
{
|
|
TString & l = get_tmp_string(128);
|
|
|
|
l = _last_error;
|
|
if(clear)
|
|
_last_error.cut(0);
|
|
return l;
|
|
}
|
|
|
|
bool TFppro::set_query()
|
|
{
|
|
if (_keys_setted)
|
|
{
|
|
_fppro_query.cut(0) << "SELECT * FROM FPPRO00F " << where_str();
|
|
return _is_set = _db->sq_set_exec(_fppro_query);
|
|
}
|
|
return _is_set = false;
|
|
}
|
|
|
|
const char* TFppro::where_str()
|
|
{
|
|
static TString str;
|
|
str.cut(0) << " WHERE PZ_KEYPRGINVIO = '" << _keys.get(0) << "' AND PZ_KEYHEADERFATT = '" << _keys.get(1) << "' AND PZ_KEYBODYFATT = '" << _keys.get(2) << "'";
|
|
return str;
|
|
}
|
|
|
|
TFppro::TFppro() : _keys("", ';'), _keys_setted(false), _is_set(false)
|
|
{
|
|
_db = new SSimple_query();
|
|
const bool ok = set_connection(*_db);
|
|
// Non utilizzo l'autocommit, viene gestito manualmente
|
|
if (ok) _db->sq_set_autocommit(false);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////
|
|
// TProtocollo
|
|
//////////////////////////////////////////////////////////
|
|
|
|
prot_s TProtocollo::get_prot()
|
|
{
|
|
const prot_s prot = { _tipoprot, _progres, _year };
|
|
return prot;
|
|
}
|
|
|
|
const TString& TProtocollo::prot_in(const int year, const char* tipoprot, const char* progres) const
|
|
{
|
|
TString & protocollo = get_tmp_string(128);
|
|
TString tipo(tipoprot);
|
|
|
|
if (tipo.empty())
|
|
tipo << "no_prot";
|
|
protocollo << year << "-" << tipo << "/" << progres;
|
|
return protocollo;
|
|
}
|
|
|
|
void TProtocollo::set(const TProtocollo& prot)
|
|
{
|
|
_year = prot._year;
|
|
_tipoprot = prot._tipoprot;
|
|
_progres = prot._progres;
|
|
}
|
|
|
|
void TProtocollo::sset(const char* prot)
|
|
{
|
|
TToken_string tok(prot, '-');
|
|
_year = tok.get_int();
|
|
TToken_string p(tok.get(), '/');
|
|
_tipoprot = p.get();
|
|
_progres = p.get();
|
|
}
|
|
|
|
const TString& TProtocollo::get_prot() const
|
|
{
|
|
return prot_in(_year, _tipoprot, _progres);
|
|
}
|
|
|
|
void set_ini_codcaus(const TString& codcaus)
|
|
{
|
|
ini_set_string(FILE_CONFIG, FILE_SECTION, F1_CAUS, codcaus);
|
|
}
|
|
|
|
const char* get_ini_codcaus()
|
|
{
|
|
return ini_get_string(FILE_CONFIG, FILE_SECTION, F1_CAUS);
|
|
}
|
|
|
|
const char* get_codcaus(const char * tipodoc, long codcf)
|
|
{
|
|
TCli_for f = cached_clifor('F', codcf);
|
|
const bool nc = TString(tipodoc) == "TD04";
|
|
|
|
if (nc)
|
|
{
|
|
const TString & codcaus = f.vendite().get(CFV_CODCAUSNC);
|
|
if (codcaus.full())
|
|
return codcaus;
|
|
}
|
|
else
|
|
{
|
|
const TString & codcaus = f.vendite().get(CFV_CODCAUS);
|
|
if (codcaus.full())
|
|
return codcaus;
|
|
}
|
|
return get_ini_codcaus();
|
|
}
|
|
|
|
bool get_endatareg()
|
|
{
|
|
return ini_get_bool(FILE_CONFIG, FILE_SECTION, "endatareg");
|
|
}
|
|
|
|
const TString & get_datainireg()
|
|
{
|
|
return ini_get_string(FILE_CONFIG, FILE_SECTION, "datainireg");
|
|
}
|
|
|
|
const TString & get_dataendreg()
|
|
{
|
|
return ini_get_string(FILE_CONFIG, FILE_SECTION, "dataendreg");
|
|
}
|
|
|
|
bool get_periodprec()
|
|
{
|
|
return ini_get_bool(CONFIG_DITTA, FILE_SECTION, "flag_periodprec");
|
|
}
|
|
|
|
void set_endatareg(bool enable)
|
|
{
|
|
ini_set_bool(FILE_CONFIG, FILE_SECTION, "endatareg", enable);
|
|
}
|
|
|
|
void set_datainireg(const TString& date)
|
|
{
|
|
ini_set_string(FILE_CONFIG, FILE_SECTION, "datainireg", date);
|
|
}
|
|
|
|
void set_dataendreg(const TString& date)
|
|
{
|
|
ini_set_string(FILE_CONFIG, FILE_SECTION, "dataendreg", date);
|
|
}
|
|
|
|
void set_periodprec(bool flag)
|
|
{
|
|
ini_set_bool(CONFIG_DITTA, FILE_SECTION, "flag_periodprec", flag);
|
|
}
|
|
|
|
bool check_causale(const TString& cod_caus, bool acq)
|
|
{
|
|
return cod_caus.full() && (
|
|
check_causale(cod_caus, "FA", acq) ||
|
|
check_causale(cod_caus, "BD", acq) ||
|
|
check_causale(cod_caus, "AF", acq) ||
|
|
check_causale(cod_caus, "FF", acq) ||
|
|
check_causale(cod_caus, "NC", acq) ||
|
|
check_causale(cod_caus, "ND", acq));
|
|
}
|
|
|
|
bool check_causale(const TString& cod_caus, const TString& tipo_doc, bool acq)
|
|
{
|
|
const TCausale& c = cached_causale(cod_caus);
|
|
|
|
if(tipo_doc == "FA" || tipo_doc == "BD" || tipo_doc == "AF" || tipo_doc == "FF")
|
|
return c.tipo_doc() == tipo_doc;
|
|
|
|
if (tipo_doc == "NC" || tipo_doc == "ND")
|
|
{
|
|
bool nota;
|
|
bool nota_iva = false;
|
|
|
|
if ((nota = c.tipo_doc() == tipo_doc))
|
|
{
|
|
if (acq)
|
|
nota_iva = c.reg().tipo() == iva_acquisti;
|
|
else
|
|
nota_iva = c.reg().tipo() == iva_vendite;
|
|
}
|
|
return nota && nota_iva;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool check_caus_has_rit(const TString& cod_caus, int year)
|
|
{
|
|
const TCausale & c = cached_causale(cod_caus, year);
|
|
|
|
return c.causale_770().full();
|
|
}
|
|
|
|
void run_cont_ini(bool liq)
|
|
{
|
|
TString cmd;
|
|
|
|
cmd.cut(0) << "cg2 -0 -i" << TFilename().temp("cg2CONTAB", "ini") << " -f1" << (liq ? " -liq" : " ") << " /u" << user();
|
|
|
|
TExternal_app app(cmd);
|
|
|
|
app.run();
|
|
}
|