668 lines
18 KiB
C++
668 lines
18 KiB
C++
|
#include <isam.h>
|
||
|
#include <recarray.h>
|
||
|
#include <tabutil.h>
|
||
|
#include <utility.h>
|
||
|
|
||
|
#include "..\pr\agenti.h"
|
||
|
#include <clifo.h>
|
||
|
#include <cfven.h>
|
||
|
#include <comuni.h>
|
||
|
#include <doc.h>
|
||
|
#include <indsp.h>
|
||
|
#include <mov.h>
|
||
|
|
||
|
#include "tp0100.h"
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// Cache banche
|
||
|
///////////////////////////////////////////////////////////
|
||
|
|
||
|
class TCache_ban : public TCache_tp
|
||
|
{
|
||
|
protected:
|
||
|
virtual TObject* key2obj(const char* key);
|
||
|
|
||
|
public:
|
||
|
virtual const TString& decode(const TToken_string& tok);
|
||
|
TCache_ban(TPack_transfer* pt) : TCache_tp(pt) {}
|
||
|
};
|
||
|
|
||
|
TObject* TCache_ban::key2obj(const char* key)
|
||
|
{
|
||
|
TTable ban("%BAN");
|
||
|
TToken_string code(key);
|
||
|
TString8 abi; code.get(0, abi);
|
||
|
TString8 cab; code.get(1, cab);
|
||
|
ban.put("CODTAB", abi);
|
||
|
if (ban.read() != NOERR)
|
||
|
{
|
||
|
const TString& desc = get_str("BankName");
|
||
|
ban.put("CODTAB", abi);
|
||
|
ban.put("S0", desc);
|
||
|
test_write(ban);
|
||
|
}
|
||
|
TString16 str;
|
||
|
str << abi; str << cab;
|
||
|
ban.put("CODTAB", str);
|
||
|
if (ban.read() != NOERR)
|
||
|
{
|
||
|
const TString& desc = get_str("BankName");
|
||
|
ban.put("CODTAB", str);
|
||
|
ban.put("S0", desc);
|
||
|
test_write(ban);
|
||
|
}
|
||
|
return ban.get("S0").dup();
|
||
|
}
|
||
|
|
||
|
const TString& TCache_ban::decode(const TToken_string& tok)
|
||
|
{
|
||
|
return *(const TString*) objptr(tok);
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// TPack_conti
|
||
|
///////////////////////////////////////////////////////////
|
||
|
|
||
|
bool TPack_conti::trasferisci()
|
||
|
{
|
||
|
log(TR("Azzeramento piano dei conti PACK"));
|
||
|
odbc_exec("DELETE FROM AccountPlan");
|
||
|
|
||
|
TRecordset& pcon = create_recordset("USE PCON");
|
||
|
TPack_iterator pi(this);
|
||
|
while (++pi)
|
||
|
{
|
||
|
const int gruppo = pcon.get("GRUPPO").as_int();
|
||
|
const int conto = pcon.get("CONTO").as_int();
|
||
|
const long sottoc = pcon.get("SOTTOCONTO").as_int();
|
||
|
const long gruconto = gruppo*1000+conto;
|
||
|
|
||
|
TToken_string values;
|
||
|
values.add(gruconto);
|
||
|
values.add(sottoc);
|
||
|
values.add(pcon.get("DESCR").as_string());
|
||
|
odbc_exec(build_insert_query("AccountPlan", "AccountCode,AccountSubCode,DescAccount", values));
|
||
|
}
|
||
|
|
||
|
return write_enabled();
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// TPack_pag
|
||
|
///////////////////////////////////////////////////////////
|
||
|
|
||
|
bool TPack_pag::trasferisci()
|
||
|
{
|
||
|
log(TR("Azzeramento tabella pagamenti PACK"));
|
||
|
odbc_exec("DELETE FROM General_Data WHERE TablesType='P'");
|
||
|
|
||
|
TRecordset& pag = create_recordset("USE %CPG");
|
||
|
TPack_iterator pi(this);
|
||
|
while (++pi)
|
||
|
{
|
||
|
TToken_string values;
|
||
|
values.add("P");
|
||
|
values.add(pag.get("CODTAB").as_string());
|
||
|
values.add(pag.get("S0").as_string());
|
||
|
const TString& query = build_insert_query("General_Data", "TablesType,Code,Description1", values);
|
||
|
odbc_exec(query);
|
||
|
}
|
||
|
return write_enabled();
|
||
|
}
|
||
|
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// Cache agenti
|
||
|
///////////////////////////////////////////////////////////
|
||
|
|
||
|
class TCache_agenti : public TCache_tp
|
||
|
{
|
||
|
protected:
|
||
|
virtual TObject* key2obj(const char* key);
|
||
|
|
||
|
public:
|
||
|
const TString& decode(const TToken_string& tok);
|
||
|
TCache_agenti(TPack_transfer* pt) : TCache_tp(pt) {}
|
||
|
};
|
||
|
|
||
|
TObject* TCache_agenti::key2obj(const char* key)
|
||
|
{
|
||
|
TLocalisamfile agenti(LF_AGENTI);
|
||
|
agenti.setkey(3);
|
||
|
agenti.put(AGE_CODFORN, key);
|
||
|
if (agenti.read() == NOERR)
|
||
|
return agenti.get(AGE_CODAGE).dup();
|
||
|
|
||
|
long cod = 1;
|
||
|
int err = agenti.last();
|
||
|
if (err == NOERR)
|
||
|
{
|
||
|
const char* codage = agenti.get(AGE_CODAGE);
|
||
|
if (codage[0] == 'Z')
|
||
|
cod += atoi(codage+1);
|
||
|
}
|
||
|
|
||
|
TString8 codage; codage.format("Z%04d", cod);
|
||
|
TString msg; msg.format(FR("Agente %s"), (const char*)codage);
|
||
|
|
||
|
agenti.put(AGE_CODAGE, codage);
|
||
|
agenti.put(AGE_RAGSOC, msg);
|
||
|
agenti.put(AGE_CODFORN, key);
|
||
|
test_write(agenti);
|
||
|
return codage.dup();
|
||
|
}
|
||
|
|
||
|
const TString& TCache_agenti::decode(const TToken_string& tok)
|
||
|
{
|
||
|
return *(TString*)objptr(tok);
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// TPack_clifo
|
||
|
///////////////////////////////////////////////////////////
|
||
|
|
||
|
const TString& TPack_clifo::decode_agente(const long codforn)
|
||
|
{
|
||
|
if (_agenti == NULL)
|
||
|
_agenti = new TCache_agenti(this);
|
||
|
TToken_string key;
|
||
|
key << codforn;
|
||
|
return _agenti->decode(key);
|
||
|
}
|
||
|
|
||
|
const TString& TPack_clifo::get_agente()
|
||
|
{
|
||
|
const TString& code = get_str("AgentCode");
|
||
|
if (code.full())
|
||
|
{
|
||
|
TString query = query_header();
|
||
|
query << "SELECT CodContab FROM Customers_Suppliers WHERE (FlagCustSupp='A') AND (CustSuppCode=#CODCF)";
|
||
|
TODBC_recordset clifo(query);
|
||
|
clifo.set_var("#CODCF", TVariant(code));
|
||
|
if (clifo.move_first())
|
||
|
{
|
||
|
TToken_string codice(clifo.get((int)0).as_string());
|
||
|
return get_tmp_string() = codice.get(1);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
TString msg;
|
||
|
msg << TR("Impossibile determinare il codice dell'agente")
|
||
|
<< " '" << code << '\'';
|
||
|
log_error(msg);
|
||
|
}
|
||
|
}
|
||
|
return EMPTY_STRING;
|
||
|
}
|
||
|
|
||
|
int TPack_clifo::cancella_clifo(TLocalisamfile& clifo) const
|
||
|
{
|
||
|
// verificare prima che il cliente sia cancellabile (MOV e DOC)
|
||
|
TRectype& rec_clifo = clifo.curr();
|
||
|
const char tipocf = rec_clifo.get(CLI_TIPOCF)[0];
|
||
|
const long codcf = rec_clifo.get_long(CLI_CODCF);
|
||
|
// verifico MOV
|
||
|
TLocalisamfile mov(LF_MOV);
|
||
|
mov.setkey(3);
|
||
|
mov.curr().put(MOV_TIPO, tipocf);
|
||
|
mov.curr().put(MOV_CODCF, codcf);
|
||
|
mov.read();
|
||
|
bool noncancellare = (tipocf == mov.get(MOV_TIPO)[0] && codcf == atol(mov.get(MOV_CODCF)));
|
||
|
if (!noncancellare)
|
||
|
{
|
||
|
// verifico DOC
|
||
|
TLocalisamfile doc(LF_DOC);
|
||
|
doc.setkey(2);
|
||
|
doc.curr().put(DOC_TIPOCF, tipocf);
|
||
|
doc.curr().put(DOC_CODCF, codcf);
|
||
|
doc.read();
|
||
|
noncancellare = (tipocf == doc.get(DOC_TIPOCF)[0] && codcf == atol(doc.get(DOC_CODCF)));
|
||
|
}
|
||
|
if (!noncancellare)
|
||
|
{
|
||
|
if (clifo.remove() == NOERR)
|
||
|
{
|
||
|
TLocalisamfile cfven(LF_CFVEN);
|
||
|
cfven.put(CFV_TIPOCF, tipocf);
|
||
|
cfven.put(CFV_CODCF, codcf);
|
||
|
cfven.remove();
|
||
|
|
||
|
TString msg; msg << TR("Cancellato cliente") << ' ' << codcf;
|
||
|
log(msg, 1); // 0 = messaggio 1 = warning 2 = errore
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
TString msg; msg << TR("Errore di cancellazione del cliente") << ' ' << codcf;
|
||
|
log(msg, 1); // 0 = messaggio 1 = warning 2 = errore
|
||
|
}
|
||
|
}
|
||
|
return ((noncancellare) ? 3 : 9);
|
||
|
}
|
||
|
|
||
|
long TPack_clifo::get_next_key(const char tipocf) const
|
||
|
{
|
||
|
TLocalisamfile clifo(LF_CLIFO);
|
||
|
long codcf = 1L;
|
||
|
if (!clifo.empty())
|
||
|
{
|
||
|
if (tipocf == 'C')
|
||
|
{
|
||
|
clifo.put(CLI_TIPOCF, 'F');
|
||
|
clifo.read(_isgteq);
|
||
|
if (clifo.good())
|
||
|
clifo.prev();
|
||
|
clifo.setstatus(NOERR);
|
||
|
}
|
||
|
else
|
||
|
clifo.last();
|
||
|
if (clifo.good())
|
||
|
{
|
||
|
const char tipo = clifo.get(CLI_TIPOCF)[0];
|
||
|
if (tipocf == tipo)
|
||
|
codcf += clifo.get_long(CLI_CODCF);
|
||
|
}
|
||
|
}
|
||
|
return codcf;
|
||
|
}
|
||
|
|
||
|
void TPack_clifo::aggiorna_banca()
|
||
|
{
|
||
|
TToken_string abicab;
|
||
|
abicab = get_str("ABICode");
|
||
|
abicab.add(get_str("CABCode"));
|
||
|
if (!abicab.empty_items())
|
||
|
{
|
||
|
if (_banche == NULL)
|
||
|
_banche = new TCache_ban(this);
|
||
|
_banche->decode(abicab);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TPack_clifo::trasferisci_conai(TRectype& rec)
|
||
|
{
|
||
|
const long applyenvtax = get_long("ApplyEnvTax");
|
||
|
if (applyenvtax > 0)
|
||
|
{
|
||
|
rec.put(CFV_ADDCONAI,"X");
|
||
|
TString query = query_header();
|
||
|
query << "SELECT ClassCode, ApplyPercent FROM Customers_EnvironmentTax WHERE (CustCode=#CODCF)";
|
||
|
TODBC_recordset conai(query);
|
||
|
const TVariant& code = recordset().get("CustSuppCode");
|
||
|
conai.set_var("#CODCF", code);
|
||
|
for (bool ok = conai.move_first(); ok; ok = conai.move_next())
|
||
|
{
|
||
|
const TString& conai_class = conai.get("ClassCode").as_string();
|
||
|
if (conai_class.not_empty())
|
||
|
{
|
||
|
const TString& conai_field = config().get(conai_class, "CFV_CONAI");
|
||
|
if (conai_field.not_empty())
|
||
|
{
|
||
|
const real applypercent = conai.get("ApplyPercent").as_real();
|
||
|
rec.put(conai_field, applypercent);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
rec.zero(CFV_ADDCONAI);
|
||
|
}
|
||
|
|
||
|
bool TPack_clifo::trasferisci()
|
||
|
{
|
||
|
TString query =
|
||
|
"SELECT Customers_Suppliers.*, Unit_Measure.UMDesc "
|
||
|
"FROM Customers_Suppliers "
|
||
|
"LEFT JOIN Unit_Measure "
|
||
|
"ON (CurrencyCode = Unit_Measure.UMCode AND Unit_Measure.UMType='9') "
|
||
|
"WHERE (StatusFlag=1 OR StatusFlag=2 OR StatusFlag=3) AND ";
|
||
|
if (_only_agenti)
|
||
|
query << "(FlagCustSupp='A')";
|
||
|
else
|
||
|
query << "(FlagCustSupp='C' OR FlagCustSupp='S')";
|
||
|
TRecordset& recset = create_recordset(query);
|
||
|
|
||
|
TString str;
|
||
|
|
||
|
TConfig& ini = config();
|
||
|
TString_array lista_clifo, lista_cfven, lista_agenti;
|
||
|
ini.list_variables(lista_clifo, true, "CLIFO", true);
|
||
|
ini.list_variables(lista_cfven, true, "CFVEN", true);
|
||
|
ini.list_variables(lista_agenti, true, "AGENTI", true);
|
||
|
|
||
|
TLocalisamfile clifo(LF_CLIFO);
|
||
|
TLocalisamfile cfven(LF_CFVEN);
|
||
|
TRectype& rec_clifo = clifo.curr();
|
||
|
TRectype& rec_cfven = cfven.curr();
|
||
|
|
||
|
TRecord_cache cache_comuni(LF_COMUNI, 2);
|
||
|
|
||
|
TPack_iterator pi(this);
|
||
|
while (++pi)
|
||
|
{
|
||
|
const TString& custsuppcode = get_str("CustSuppCode");
|
||
|
const int statusflag = get_long("StatusFlag");
|
||
|
const char flagcustsupp = get_str("FlagCustSupp")[0];
|
||
|
const char tipocf = (flagcustsupp == 'C' ? 'C': 'F'); // C -> C (S,A) -> F
|
||
|
long codcf = get_long("CodContab");
|
||
|
|
||
|
bool needs_creation = codcf <= 0;
|
||
|
bool good = true;
|
||
|
if (!needs_creation)
|
||
|
{
|
||
|
// il cliente/fornitore va aggiornato
|
||
|
rec_cfven.zero();
|
||
|
rec_cfven.put(CFV_TIPOCF, tipocf);
|
||
|
rec_cfven.put(CFV_CODCF, codcf);
|
||
|
cfven.read();
|
||
|
|
||
|
rec_clifo.zero();
|
||
|
rec_clifo.put(CLI_TIPOCF, tipocf);
|
||
|
rec_clifo.put(CLI_CODCF, codcf);
|
||
|
good = clifo.read() == NOERR;
|
||
|
if (!good)
|
||
|
needs_creation = true;
|
||
|
}
|
||
|
|
||
|
if (needs_creation && statusflag != 3)
|
||
|
{
|
||
|
// il cliente/fornitore va inserito in campo previo controllo piva
|
||
|
if (codcf <= 0)
|
||
|
codcf = get_next_key(tipocf);
|
||
|
rec_cfven.zero();
|
||
|
rec_cfven.put(CFV_TIPOCF, tipocf);
|
||
|
rec_cfven.put(CFV_CODCF, codcf);
|
||
|
// dati standard solo se sono in inserimento
|
||
|
rec_cfven.put(CFV_RAGGDOC, config().get("RAGGDOC","STANDARD"));
|
||
|
rec_cfven.put(CFV_RAGGEFF, config().get("RAGGEFF","STANDARD"));
|
||
|
rec_cfven.put(CFV_ADDBOLLI, config().get("ADDBOLLI","STANDARD"));
|
||
|
rec_cfven.put(CFV_PERCSPINC, config().get("PERCSPINC","STANDARD"));
|
||
|
// scrivo
|
||
|
good = test_write(cfven);
|
||
|
rec_clifo.zero();
|
||
|
rec_clifo.put(CLI_TIPOCF, tipocf);
|
||
|
rec_clifo.put(CLI_CODCF, codcf);
|
||
|
good &= test_write(clifo);
|
||
|
}
|
||
|
|
||
|
if (good)
|
||
|
{
|
||
|
if (statusflag == 3)
|
||
|
{
|
||
|
const int flag = cancella_clifo(clifo);
|
||
|
str = "";
|
||
|
update_statusflag_codcontab(custsuppcode, flag, str);
|
||
|
// forse occorrera' cancellare anche l'agente se lo e'
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// aggiorna i file clifo, cfven, agenti se necessario
|
||
|
aggiorna_banca();
|
||
|
// aggiorna dati conai
|
||
|
trasferisci_conai(rec_cfven);
|
||
|
// aggiormento comune
|
||
|
TString80 dencom = get_str("Locality");
|
||
|
dencom.trim(); dencom.upper();
|
||
|
const TRectype& reccom = cache_comuni.get(dencom);
|
||
|
if (dencom == reccom.get(COM_DENCOM))
|
||
|
rec_clifo.put(CLI_COMCF, reccom.get(COM_COM));
|
||
|
else
|
||
|
{
|
||
|
rec_clifo.zero(CLI_COMCF);
|
||
|
rec_clifo.put(CLI_LOCCF, dencom);
|
||
|
|
||
|
log("");
|
||
|
str.format(FR("Cliente/Fornitore %c %ld: comune non trovato %s"), tipocf, codcf, (const char*)dencom);
|
||
|
log(str);
|
||
|
}
|
||
|
// iban
|
||
|
const bool ibanflag = get_long("IbanFlag") != 0;
|
||
|
if (ibanflag)
|
||
|
rec_clifo.put(CLI_IBAN, get_str("IbanCode"));
|
||
|
// agente
|
||
|
rec_cfven.put(CFV_CODAG, get_agente());
|
||
|
|
||
|
aggiorna_record(clifo, lista_clifo);
|
||
|
aggiorna_record(cfven, lista_cfven);
|
||
|
|
||
|
if (get_str("CodIvaNI") == "0") // Caso speciale: significa NON esenti
|
||
|
cfven.zero(CFV_ASSFIS); // Azzero il codice di assogettamento fiscale
|
||
|
|
||
|
// aggiorna anche record agenti se e' una spia
|
||
|
if (flagcustsupp == 'A')
|
||
|
{
|
||
|
const TString8 codage = decode_agente(codcf);
|
||
|
TLocalisamfile agenti(LF_AGENTI);
|
||
|
agenti.put(AGE_CODAGE, codage);
|
||
|
agenti.read();
|
||
|
agenti.put(AGE_CODFORN, codcf);
|
||
|
aggiorna_record(agenti, lista_agenti);
|
||
|
str.format("%ld|%s", codcf, (const char*) codage);
|
||
|
update_statusflag_codcontab(custsuppcode, 0, str);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// aggiorno codcontab
|
||
|
str.format("%ld", codcf);
|
||
|
update_statusflag_codcontab(custsuppcode, 0, str);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return write_enabled();
|
||
|
}
|
||
|
|
||
|
void TPack_clifo::update_statusflag_codcontab(const TString& custsuppcode, const int statusflag, const TString& codcontab)
|
||
|
{
|
||
|
TString query;
|
||
|
query.format("UPDATE Customers_Suppliers SET StatusFlag=%d, CodContab='%s' WHERE CustSuppCode='%s'", statusflag, (const char*) codcontab, (const char*) custsuppcode);
|
||
|
odbc_exec(query);
|
||
|
}
|
||
|
|
||
|
bool TPack_clifo::aggiorna_record(TLocalisamfile& file, const TString_array& lista_campi)
|
||
|
{
|
||
|
TRectype& rec = file.curr();
|
||
|
TString campo_dest, campo_orig, valore, str;
|
||
|
FOR_EACH_ARRAY_ROW(lista_campi,i,row)
|
||
|
{
|
||
|
row->get(0, campo_dest);
|
||
|
row->get(1, campo_orig);
|
||
|
if (!campo_orig.blank())
|
||
|
{
|
||
|
if (campo_orig[0] == '_')
|
||
|
{
|
||
|
TToken_string elabora(campo_orig.mid(1),',');
|
||
|
const TString& str = elabora.get();
|
||
|
if (str == "TAB") // formato _TAB, <tabella da leggere>,<valore CODTAB>, <campo da leggere>
|
||
|
{
|
||
|
const TString4 tab = elabora.get(); // tabella da leggere
|
||
|
const TString80 campo = elabora.get();
|
||
|
const TString16 codtab = get_str(campo);
|
||
|
const TString80 campotab = elabora.get();
|
||
|
valore = cache().get(tab, codtab, campotab);
|
||
|
}
|
||
|
else if (str == "FISSO")
|
||
|
valore = elabora.get(); // valore fisso indicato in configurazione
|
||
|
}
|
||
|
else
|
||
|
valore = get_str(campo_orig);
|
||
|
rec.put(campo_dest, valore);
|
||
|
}
|
||
|
}
|
||
|
return test_rewrite(file);
|
||
|
}
|
||
|
|
||
|
TPack_clifo::TPack_clifo(bool only_agenti) : _agenti(NULL), _banche(NULL), _only_agenti(only_agenti)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
TPack_clifo::~TPack_clifo()
|
||
|
{
|
||
|
if (_agenti != NULL)
|
||
|
delete _agenti;
|
||
|
if (_banche != NULL)
|
||
|
delete _banche;
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////
|
||
|
// TPack_indsped
|
||
|
///////////////////////////////////////////////////////////
|
||
|
bool TPack_indsped::get_clifo(char& tipocf, long& codcf)
|
||
|
{
|
||
|
TString query = query_header();
|
||
|
query << "SELECT CodContab FROM Customers_Suppliers WHERE (FlagCustSupp=#TIPOCF) AND (CustSuppCode=#CODCF)";
|
||
|
TODBC_recordset clifo(query);
|
||
|
|
||
|
const TVariant tipo(tipocf == 'C' ? "C": "S");
|
||
|
const TVariant& code = recordset().get("CustSuppRefCode");
|
||
|
|
||
|
clifo.set_var("#TIPOCF", tipo);
|
||
|
clifo.set_var("#CODCF", code);
|
||
|
bool ok = clifo.move_first();
|
||
|
if (ok)
|
||
|
{
|
||
|
tipocf = tipo.as_string()[0] == 'C' ? 'C' : 'F';
|
||
|
codcf = clifo.get((int)0).as_int();
|
||
|
}
|
||
|
|
||
|
if (!ok || codcf <= 0)
|
||
|
{
|
||
|
TString msg = TR("Impossibile determinare il codice contabile del cliente/fornitore");
|
||
|
msg << ' ' << tipo.as_string() << ' ' << code.as_string();
|
||
|
ok = log_error(msg);
|
||
|
}
|
||
|
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
int TPack_indsped::get_next_key(const char tipocf, const long codcf) const
|
||
|
{
|
||
|
TISAM_recordset indsped("USE INDSPED\nFROM TIPOCF=#TIPO CODCF=#CODE\nTO TIPOCF=#TIPO CODCF=#CODE");
|
||
|
TString4 str; str << tipocf;
|
||
|
indsped.set_var("#TIPO", TVariant(str));
|
||
|
indsped.set_var("#CODE", TVariant(codcf));
|
||
|
int codind = 1;
|
||
|
if (indsped.items() > 0) // La move_last da errore fatale su recordset vuoti!
|
||
|
{
|
||
|
indsped.move_last();
|
||
|
codind += indsped.get("CODIND").as_int();
|
||
|
}
|
||
|
return codind;
|
||
|
}
|
||
|
|
||
|
bool TPack_indsped::trasferisci()
|
||
|
{
|
||
|
const char* const query =
|
||
|
"SELECT * FROM Customers_Suppliers "
|
||
|
"WHERE (FlagCustSupp='D' OR FlagCustSupp='N') AND (StatusFlag=1 OR StatusFlag=2 OR StatusFlag=3)";
|
||
|
TRecordset& recset = create_recordset(query);
|
||
|
|
||
|
TConfig& ini = config();
|
||
|
TString_array lista_indsped;
|
||
|
ini.list_variables(lista_indsped, true, "INDSPED", true);
|
||
|
|
||
|
TLocalisamfile clifo(LF_CLIFO);
|
||
|
TLocalisamfile indsped(LF_INDSP);
|
||
|
TRectype& rec_clifo = clifo.curr();
|
||
|
TRectype& rec_indsped = indsped.curr();
|
||
|
TString str;
|
||
|
|
||
|
TRecord_cache cache_comuni(LF_COMUNI, 2);
|
||
|
|
||
|
TPack_iterator pi(this);
|
||
|
while (++pi)
|
||
|
{
|
||
|
const TString& custsuppcode = get_str("CustSuppCode");
|
||
|
const int statusflag = recset.get("StatusFlag").as_int();
|
||
|
const char flagcustsupp = get_str("FlagCustSupp")[0];
|
||
|
const char tipocf = flagcustsupp == 'D' ? 'C': 'F'; // D -> C N -> F
|
||
|
// in codcontab di una destinazione diversa ci mettiamo codcf|codind per ritrovarlo subito su indsped
|
||
|
TToken_string codcontab = get_str("CodContab");
|
||
|
codcontab.trim();
|
||
|
long codcf = -1;
|
||
|
int codind = 0;
|
||
|
if (codcontab.not_empty())
|
||
|
{
|
||
|
codcf = codcontab.get_long(0);
|
||
|
codind = codcontab.get_int();
|
||
|
}
|
||
|
bool needs_creation = codcf <= 0;
|
||
|
bool good = true;
|
||
|
if (!needs_creation)
|
||
|
{
|
||
|
rec_clifo.zero();
|
||
|
rec_clifo.put(CLI_TIPOCF, tipocf);
|
||
|
rec_clifo.put(CLI_CODCF, codcf);
|
||
|
good = clifo.read() == NOERR;
|
||
|
if (!good)
|
||
|
{
|
||
|
// custsupprefcode e' il cliente/fornitore a cui si riferisce l'indirizzo di spesione
|
||
|
const TString& custsupprefcode = get_str("CustSuppRefCode");
|
||
|
str.format(FR("Non esiste l'indirizzo di spedizione %ld/%d corrispondente a %s"),
|
||
|
codcf, codind, (const char*)custsupprefcode);
|
||
|
log(str, 1);
|
||
|
}
|
||
|
if (good)
|
||
|
{
|
||
|
rec_indsped.zero();
|
||
|
rec_indsped.put(IND_TIPOCF, tipocf);
|
||
|
rec_indsped.put(IND_CODCF, codcf);
|
||
|
rec_indsped.put(IND_CODIND, codind);
|
||
|
good = indsped.read() == NOERR;
|
||
|
if (!good)
|
||
|
needs_creation = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (needs_creation && statusflag != 3 && good)
|
||
|
{
|
||
|
// inserisco indirizzo di spedizione
|
||
|
char tipo = tipocf=='C' ? 'C':'S';
|
||
|
if (get_clifo(tipo, codcf))
|
||
|
{
|
||
|
codind = get_next_key(tipocf, codcf);
|
||
|
rec_indsped.zero();
|
||
|
rec_indsped.put(IND_TIPOCF, tipocf);
|
||
|
rec_indsped.put(IND_CODCF, codcf);
|
||
|
rec_indsped.put(IND_CODIND, codind);
|
||
|
codcontab.add(codcf);
|
||
|
codcontab.add(codind);
|
||
|
good = test_write(indsped);
|
||
|
}
|
||
|
else
|
||
|
good = false;
|
||
|
}
|
||
|
if (good)
|
||
|
{
|
||
|
if (statusflag == 3)
|
||
|
{
|
||
|
//const int flag = cancella_clifo(clifo);
|
||
|
//update_statusflag_codcontab(custsuppcode, flag, "0");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// aggiormento comune
|
||
|
TString80 dencom = get_str("Locality");
|
||
|
dencom.trim(); dencom.upper();
|
||
|
const TRectype& reccom = cache_comuni.get(dencom);
|
||
|
TString80 str = reccom.get(COM_DENCOM); str.trim();
|
||
|
if (str == dencom)
|
||
|
rec_indsped.put(IND_COM, reccom.get(COM_COM));
|
||
|
else
|
||
|
{
|
||
|
rec_clifo.put(IND_COM, " ");
|
||
|
rec_clifo.put(IND_LOCALITA, dencom);
|
||
|
|
||
|
log("");
|
||
|
str.format(FR("Destinazioni diverse %d: comune non trovato %s"), codcf, (const char*) dencom);
|
||
|
log(str);
|
||
|
}
|
||
|
aggiorna_record(indsped, lista_indsped);
|
||
|
// in codcontab ci metto codcf|indsped
|
||
|
update_statusflag_codcontab(custsuppcode, 0, codcontab);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return write_enabled();
|
||
|
}
|
||
|
|