campo-sirio/ve/velib04f.cpp

476 lines
14 KiB
C++
Raw Normal View History

#include <diction.h>
#include <viswin.h>
#include "velib04.h"
#include "../ca/movana.h"
#include "../ca/rmovana.h"
#include "../ca/rrip.h"
#include "../mg/anamag.h"
class TCache_causali : public TCache
{
protected:
TObject* key2obj(const char* key);
public:
const TCausale& causale(const TDocumento& doc);
TCache_causali() : TCache(17) { }
};
TObject* TCache_causali::key2obj(const char* key)
{
return new TCausale(key);
}
const TCausale& TCache_causali::causale(const TDocumento& doc)
{
// Cerca il codice causale sul cliente
TToken_string key;
key.add(doc.get(DOC_TIPOCF));
key.add(doc.get(DOC_CODCF));
const TRectype& cfven = cache().get(LF_CFVEN, key);
const char* fld = doc.is_nota_credito() ? CFV_CODCAUSNC : CFV_CODCAUS;
TString4 codcaus = cfven.get(fld);
// Se non lo trova guarda sul tipo documento
if (codcaus.empty())
{
const TTipo_documento& tipo = doc.tipo();
codcaus = tipo.causale();
}
// Trasforma il codice in causale vera e propria
return (const TCausale&)*objptr(codcaus);
}
///////////////////////////////////////////////////////////
// TContabilizzazione_analitica
///////////////////////////////////////////////////////////
static TCache_ripartizioni _rip;
static TToken_string _search_seq;
const TCausale& TContabilizzazione_analitica::doc2caus(const TDocumento& doc)
{
static TCache_causali _caus;
return _caus.causale(doc);
}
const TCausale& TContabilizzazione_analitica::rdoc2caus(const TRiga_documento& rdoc)
{
return doc2caus(rdoc.doc());
}
// Copiata dalla contabilizzazione ed eliminati riferimenti a tutte le variabili globali del cazzo
error_type TContabilizzazione_analitica::search_costo_ricavo(const TRiga_documento& r, TBill& conto)
{
if (_search_seq.blank())
{
TConfig conf(CONFIG_DITTA, "ve");
_search_seq = conf.get("RICERCACR");
}
const int items = _search_seq.items();
TToken_string clifo_key;
clifo_key.format("%c|%ld", r.doc().get_char(DOC_TIPOCF), r.doc().get_long(DOC_CODCF));
const TRectype& cli_file = cache().get(LF_CLIFO, clifo_key);
const bool is_cli = cli_file.get(CLI_TIPOCF) != 'F';
bool skip_art_related = false;
bool skip_clifo = !cli_file.empty();
const TCodiceIVA codiva(r.get(RDOC_CODIVA));
const char t = r.tipo().tipo();
switch (t)
{
case 'O': // righe omaggio come articoli spiaccicato identico (avranno imponibile 0)
case 'M': // righe di merce
{
// posiziona l'anagrafica sull'articolo specificato sulla ..iga
const TRectype& anamag = cache().get(LF_ANAMAG, r.get(RDOC_CODARTMAG));
if (anamag.empty()) // se non trova l'articolo saltera' anche gmc,smc,rfa.
skip_art_related = true;
TString16 tok;
// Scorre la stringa di ricerca
for (int i=0; _error == no_error && i<items;i++)
{
_search_seq.get(i, tok);
if (tok == "CF")
{
if (skip_clifo) continue;
const int gr = cli_file.get_int(CLI_GRUPPORIC);
const int co = cli_file.get_int(CLI_CONTORIC);
const long so = cli_file.get_long(CLI_SOTTOCRIC);
conto.set(gr,co,so);
if (conto.ok()) break; // se lo trova esce (tutti != 0)
}
else
if (tok == "CA")
{
const TCausale& caus = rdoc2caus(r);
if (caus.IVA2bill(codiva,conto))
break; // se lo trova esce
}
else
if (tok == "AR")
{
if (skip_art_related)
continue;
int gr = anamag.get_int(is_cli ? ANAMAG_GRUPPOV : ANAMAG_GRUPPOA);
int co = anamag.get_int(is_cli ? ANAMAG_CONTOV : ANAMAG_CONTOA);
long so = anamag.get_long(is_cli ? ANAMAG_SOTTOCV : ANAMAG_SOTTOCA);
conto.set(gr,co,so);
if (!conto.ok()) // se il conto non c'e' guarda la categoria acquisti/vendite
{
const TString16 codtab = anamag.get(is_cli ? ANAMAG_CATCONV : ANAMAG_CATCONA);
const TRectype& t = cache().get(is_cli ? "CRA" : "CAA", codtab);
if (!t.empty())
{
gr = t.get_int("I0");
co = t.get_int("I1");
so = t.get_long("I2");
conto.set(gr,co,so);
}
}
if (conto.ok()) break;
}
else
if (tok == "GM" || tok == "SM" || tok == "RF")
{
if (skip_art_related)
continue;
const bool is_fis = tok == "RF";
TString16 codtab = anamag.get(is_fis ? ANAMAG_RAGGFIS : ANAMAG_GRMERC);
if (tok == "GM" && codtab.len() > 3)
codtab.cut(3); // gli ultimi 2 si riferiscono al sottogruppo.
const TRectype& tab = cache().get(is_fis ? "RFA" : "GMC", codtab);
if (!tab.empty())
{
const int gr = tab.get_int(is_cli ? "I3" : "I0");
const int co = tab.get_int(is_cli ? "I4" : "I1");
const long so = tab.get_long(is_cli ? "I5" : "I2");
conto.set(gr,co,so);
}
if (conto.ok())
break;
}
else
if (tok == "CV" || tok == "CC")
{
const bool is_cve = tok == "CV";
if (is_cve && !is_cli)
continue; // se e' un fornitore salta questa condizione
TString16 cod = is_cve ? r.doc().get(DOC_CATVEN) : EMPTY_STRING;
if (cod.empty())
{
if (skip_clifo)
continue; // se non aveva trovato il cliente salta al prossimo
const TRectype& cfven = cache().get(LF_CFVEN, clifo_key);
cod = cfven.get(is_cve ? CFV_CATVEN : CFV_CODCATC);
}
const TRectype& t = cache().get(is_cve ? "CVE" : "CCO", cod);
if (!t.empty())
{
const bool x =(is_cve || is_cli);
const int gr = t.get_int(x ? "I3" : "I0");
const int co = t.get_int(x ? "I4" : "I1");
const long so = t.get_long(x ? "I5": "I2");
conto.set(gr,co,so);
}
if (conto.ok())
break;
}
}
break; // case 'M'
}
case 'P': // righe prestazione
case 'S': // righe spese
{
const TRectype& tab = cache().get('P' ? "PRS" : "SPP", r.get(RDOC_CODART));
if (!tab.empty())
{
int gr = tab.get_int(is_cli ? "I0" : "I3");
int co = tab.get_int(is_cli ? "I1" : "I4");
long so = tab.get_long(is_cli ? "I2" : "I5");
conto.set(gr,co,so);
if (!is_cli && !conto.ok())
{
gr = r.get_int("QTAGG1");
co = r.get_int("QTAGG2");
so = r.get_long("QTAGG3");
conto.set(gr,co,so);
}
}
if (t == 'P' && !conto.find()) // Cerca il conto nella stringa di ricerca (solo per prestazioni)
{
TString16 tok;
// Scorre la stringa di ricerca ma solo per causale o CLI/FO
for (int i=0; _error == no_error && i<items;i++)
{
_search_seq.get(i, tok);
if (tok == "CF")
{
if (skip_clifo)
continue;
const int gr = cli_file.get_int(CLI_GRUPPORIC);
const int co = cli_file.get_int(CLI_CONTORIC);
const long so = cli_file.get_long(CLI_SOTTOCRIC);
conto.set(gr,co,so);
if (conto.ok())
break;
}
else
if (tok == "CA")
{
const TCausale& caus = rdoc2caus(r);
if (caus.IVA2bill(codiva,conto))
break;
}
}
}
break; // case 'P','S'
}
case 'C': // righe sconti
case 'D': // righe descrizioni (saltare)
default :
break;
} // end of switch
if (_error == no_error)
{
if (!conto.ok() || !conto.find())
_error = conto_error;
}
return _error;
}
bool TContabilizzazione_analitica::find_conti(const TRiga_documento& riga, TString_array& conti)
{
const char tipocf = riga.doc().get_char(DOC_TIPOCF);
bool ok = false;
TString80 contanal;
if (riga.is_articolo())
{
const TRectype& anamag = cache().get(LF_ANAMAG, riga.get(RDOC_CODARTMAG));
ok = !anamag.empty();
contanal = anamag.get(tipocf == 'F' ? ANAMAG_CONTOINDA : ANAMAG_CONTOINDV);
}
else
{
const char* tabanal = NULL;
if (riga.is_spese())
{ tabanal = "SPP"; } else
if (riga.is_prestazione())
{ tabanal = "PRS"; } else
if (riga.is_risorsa())
{ tabanal = "RIS"; } else
if (riga.is_attrezzatura())
{ tabanal = "ATR"; }
if (tabanal != NULL)
{
const TRectype& spp = cache().get(tabanal, riga.get(RDOC_CODART));
ok = !spp.empty();
contanal = spp.get(tipocf == 'F' ? "S2" : "S1");
}
}
if (ok)
{
const TString riga_cms = riga.codice_commessa();
const TString riga_fsc = riga.fase_commessa();
const TString riga_cos = riga.codice_costo();
const bool riga_any = riga_cms.not_empty() || riga_fsc.not_empty() || riga_cos.not_empty();
if (contanal.blank()) // Non ho trovato il conto in anagrafica ...
{
TBill bill; // Cerco il conto contabile
if (search_costo_ricavo(riga, bill) != no_error)
{
const TRecord_array& rip = _rip.righe(bill.gruppo(), bill.conto(), bill.sottoconto());
TToken_string conto;
for (int i = 1; i <= rip.rows(); i++)
{
const TRectype& rigarip = rip.row(i);
conto = rigarip.get(RRIP_CODCONTO);
if (riga_any)
{
conto.add(riga_cms, 1);
conto.add(riga_fsc, 2);
conto.add(riga_cos, 3);
}
else
{
conto.add(rigarip.get(RRIP_CODCMS), 1);
conto.add(rigarip.get(RRIP_CODFASE), 2);
conto.add(rigarip.get(RRIP_CODCOSTO), 3);
}
conto.add(rigarip.get(RRIP_RIPARTO), 4);
conti.add(conto);
}
}
}
else
{
TToken_string conto = contanal;
if (riga_any)
{
conto.add(riga_cms, 1);
conto.add(riga_fsc, 2);
conto.add(riga_cos, 3);
}
conto.add(100, 4);
conti.add(conto);
}
}
return ok;
}
void TContabilizzazione_analitica::init_distrib(TString_array& conti, TGeneric_distrib& distrib)
{
FOR_EACH_ARRAY_ROW(conti, i, row)
{
real slice = row->get(4);
if (slice <= ZERO)
slice = UNO;
distrib.add(slice);
}
}
bool TContabilizzazione_analitica::elabora(const TDocumento& doc, long numreg_cg, TViswin* viswin, bool can_write)
{
TAnal_mov* mov = NULL;
TDate datareg, datacomp;
int annoes = 0;
TString descr;
if (numreg_cg > 0)
{
const TRectype& mov_cg = cache().get(LF_MOV, numreg_cg);
datareg = mov_cg.get_date(MOV_DATAREG);
datacomp = mov_cg.get_date(MOV_DATACOMP);
annoes = mov_cg.get_int(MOV_ANNOES);
descr = mov_cg.get(MOV_DESCR);
}
else
{
datareg = datacomp = TDate(TODAY);
annoes = datareg.year();
}
const int decimals = TCurrency::get_firm_dec();
// Scandisco le righe del documento,
for (int i = 1; i <= doc.physical_rows(); i++)
{
const TRiga_documento& riga = doc[i];
// salto descrizioni, sconti e omaggi
if (riga.is_descrizione() || riga.is_sconto() || riga.is_omaggio())
continue;
// salto valori nulli
const real valore = riga.valore(true, decimals);
if (valore.is_zero())
continue;
TString_array conti;
bool ok = find_conti(riga, conti);
if (conti.empty())
{
if (viswin != NULL)
{
TString msg;
msg.format(FR("*** Impossibile determinare il conto analitico per l'articolo: '%s'"), (const char*)riga.get(RDOC_CODART));
viswin->add_line(msg);
}
continue;
}
if (can_write)
{
bool dare = doc.get_char(DOC_TIPOCF) == 'F';
if (doc.is_nota_credito())
dare= !dare;
const char sezione = dare ? 'D' : 'A';
TImporto totdoc;
TGeneric_distrib esso(valore, decimals);
init_distrib(conti, esso);
FOR_EACH_ARRAY_ROW(conti, i, row)
{
if (mov == NULL)
{
mov = new TAnal_mov;
mov->put(MOVANA_DATAREG, datareg);
mov->put(MOVANA_DATACOMP, datacomp);
mov->put(MOVANA_ANNOES, annoes);
mov->put(MOVANA_NUMREGCG, numreg_cg);
mov->put(MOVANA_DESCR, descr);
}
TRectype& rmov = mov->new_row();
rmov.put(RMOVANA_ANNOES, annoes);
rmov.put(RMOVANA_CODCONTO, row->get(0));
rmov.put(RMOVANA_CODCMS, row->get());
rmov.put(RMOVANA_CODFASE, row->get());
rmov.put(RMOVANA_CODCCOSTO,row->get());
TImporto imp(sezione, real(esso.get()));
imp.normalize();
rmov.put(RMOVANA_SEZIONE, imp.sezione());
rmov.put(RMOVANA_IMPORTO, imp.valore());
totdoc += imp;
}
if (mov != NULL)
{
totdoc.normalize();
mov->put(MOVANA_SEZIONE, totdoc.sezione());
mov->put(MOVANA_TOTDOC, totdoc.valore());
TLocalisamfile movana(LF_MOVANA);
mov->write(movana);
delete mov;
}
}
}
return can_write;
}
bool TContabilizzazione_analitica::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out,
const TDate& data_elab, bool interattivo)
{
const TImporto zero;
for (int i = 0; doc_in.items(); i++)
{
if (!elabora(doc_in[i], 0, NULL, true))
break;
}
return true;
}
TContabilizzazione_analitica::TContabilizzazione_analitica() : TElaborazione(NULL)
{
}
TContabilizzazione_analitica::TContabilizzazione_analitica(const TRectype& rec)
: TElaborazione(rec)
{
}
TContabilizzazione_analitica::~TContabilizzazione_analitica()
{ }