570 lines
14 KiB
C++
570 lines
14 KiB
C++
#include <diction.h>
|
||
#include <recarray.h>
|
||
#include <mask.h>
|
||
#include <validate.h>
|
||
|
||
#include "fe0100a.h"
|
||
#include "felib.h"
|
||
|
||
#include "../cg/cglib.h"
|
||
|
||
#include <alleg.h>
|
||
#include <anafis.h>
|
||
#include <anagr.h>
|
||
#include <attiv.h>
|
||
#include <clifo.h>
|
||
#include <comuni.h>
|
||
#include <doc.h>
|
||
#include <effetti.h>
|
||
#include <mov.h>
|
||
#include <nditte.h>
|
||
#include <occas.h>
|
||
#include <variant.h>
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// Utility
|
||
///////////////////////////////////////////////////////////
|
||
|
||
bool fe_is_nota_variazione(const TRectype& mov)
|
||
{
|
||
const int logicnum = mov.num();
|
||
|
||
if (logicnum == LF_MOV)
|
||
{
|
||
const real totdoc = mov.get_real(MOV_TOTDOC);
|
||
if (totdoc < ZERO)
|
||
return true;
|
||
|
||
const int tipomov = mov.get_int(MOV_TIPOMOV);
|
||
if (tipomov == 2) // Nota di credito/debito per saldaconto
|
||
return true;
|
||
|
||
if (tipomov <= 1)
|
||
{
|
||
const TString& tipodoc = mov.get(MOV_TIPODOC);
|
||
if (tipodoc == "NC" || tipodoc == "ND") // Nota di credito/debito senza saldaconto
|
||
return true;
|
||
}
|
||
} else
|
||
if (logicnum == LF_ALLEG)
|
||
{
|
||
const TString& tipo = mov.get(ALL_TIPOPE);
|
||
if (tipo.len() == 2 && tipo != "BL")
|
||
return tipo == "NE" || tipo == "NR";
|
||
|
||
const real importo = mov.get_real(ALL_IMPORTO);
|
||
const real imposta = mov.get_real(ALL_IMPOSTA);
|
||
if (importo < ZERO || (importo.is_zero() && imposta < ZERO))
|
||
return true;
|
||
|
||
const long numreg = mov.get_long(ALL_PROGR);
|
||
if (numreg < 900000L)
|
||
{
|
||
const TRectype& pn = cache().get(LF_MOV, numreg);
|
||
if (!pn.empty())
|
||
return fe_is_nota_variazione(pn);
|
||
}
|
||
} else
|
||
if (logicnum == LF_DOC)
|
||
{
|
||
const TString& tipodoc = mov.get(DOC_TIPODOC);
|
||
const TRectype& td = cache().get("%TIP", tipodoc);
|
||
|
||
bool yes = td.get_bool("B7");
|
||
if (!yes)
|
||
{
|
||
const TString4 codcaus(td.get("S6"));
|
||
if (codcaus.full())
|
||
{
|
||
const TCausale c(codcaus, mov.get_int(DOC_ANNO));
|
||
const char sez = c.sezione_clifo();
|
||
//controllo ulteriore sull'iva
|
||
TipoIVA tiva = c.reg().iva();
|
||
const char tcf = mov.get_char(DOC_TIPOCF);
|
||
if (tiva == nessuna_iva && tcf > ' ')
|
||
tiva = tcf == 'C' ? iva_vendite : iva_acquisti;
|
||
if (tiva != nessuna_iva)
|
||
yes = ((tiva == iva_vendite) ^ (sez == 'D'));
|
||
}
|
||
}
|
||
return yes;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
bool fe_decode_cofi(const TString& cofi, char& sex_nasc, TDate& dt_nasc, TString& com_nasc)
|
||
{
|
||
if (cofi.len() != 16 || !cf_check("", cofi))
|
||
return false;
|
||
|
||
const TFixed_string wm = "ABCDEHLMPRST"; // Controllo data di nascita
|
||
|
||
int anno = atoi(cofi.mid(6,2));
|
||
anno += anno < 25? 2000 : 1900;
|
||
const int mese = wm.find(cofi[8])+1;
|
||
int giorno = atoi(cofi.mid(9,2));
|
||
if (giorno > 40)
|
||
{
|
||
giorno -= 40;
|
||
sex_nasc = 'F';
|
||
}
|
||
else
|
||
sex_nasc = 'M';
|
||
|
||
com_nasc = cofi.mid(11,4);
|
||
|
||
return true;
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TSpesometro_set
|
||
///////////////////////////////////////////////////////////
|
||
|
||
const TVariant& TSpesometro_set::get(unsigned int column) const
|
||
{
|
||
const TRecordset_column_info& info = column_info(column);
|
||
if (info._type == _realfld || info._type == _datefld)
|
||
{
|
||
TString80 val = row().get(column);
|
||
TVariant& var = get_tmp_var();
|
||
if (info._type == _realfld)
|
||
{
|
||
val.replace(',', '.');
|
||
var = real(val);
|
||
}
|
||
else
|
||
{
|
||
if (val.len() == 8)
|
||
{
|
||
val.insert("-", 2);
|
||
val.insert("-", 5);
|
||
}
|
||
var = TDate(val);
|
||
}
|
||
return var;
|
||
}
|
||
return TCSV_recordset::get(column);
|
||
}
|
||
|
||
bool TSpesometro_set::set(unsigned int fld, const TVariant& var)
|
||
{
|
||
const TRecordset_column_info& fi = column_info(fld);
|
||
|
||
// Salva le date in formato GGMMAAAA invece dello standard ANSI AAAAMMGG
|
||
if (fi._type == _datefld)
|
||
{
|
||
const TDate d = var.as_date();
|
||
if (d.ok())
|
||
{
|
||
TFixed_string str = d.string(full, '\0', full, full, gma_date);
|
||
row().add(str, fld);
|
||
}
|
||
else
|
||
row().add("", fld);
|
||
return true;
|
||
} else
|
||
// Salva gli importi in italiano (non possono essere negativi)
|
||
if (fi._type == _realfld)
|
||
{
|
||
real r = var.as_real();
|
||
row().add(r.stringa(0, 2), fld);
|
||
} else
|
||
if (fi._type == _boolfld)
|
||
{
|
||
row().add(var.as_bool() ? "S" : " ", fld);
|
||
}
|
||
else
|
||
{
|
||
TString256 str = var.as_string();
|
||
str.upper();
|
||
str.replace('<EFBFBD>', 'A');
|
||
str.replace('<EFBFBD>', 'N');
|
||
str.replace('<EFBFBD>', 'O');
|
||
str.replace('<EFBFBD>', 'U');
|
||
row().add(str, fld);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
void TSpesometro_set::add_header_data(int n, const TString& value)
|
||
{
|
||
CHECKD(n > 0 && n <= 16, "Bad SO record type ", n);
|
||
TString8 fld; fld.format("SO%03d", n);
|
||
new_rec(fld);
|
||
set(1, value);
|
||
}
|
||
|
||
const TString& TSpesometro_set::get_recapito(const TRectype& ndt, const char* numero, const char* prefisso) const
|
||
{
|
||
if (ndt.exist(numero))
|
||
{
|
||
TString& tel = get_tmp_string();
|
||
if (prefisso && *prefisso)
|
||
tel = ndt.get(prefisso);
|
||
tel << ndt.get(numero);
|
||
if (tel.full())
|
||
return tel;
|
||
}
|
||
|
||
if (ndt.num() == LF_NDITTE)
|
||
{
|
||
TString16 key;
|
||
key.format("%c|%ld", ndt.get_char(NDT_TIPOA), ndt.get_long(NDT_CODANAGR));
|
||
const TRectype& anag = cache().get(LF_ANAG, key);
|
||
if (prefisso)
|
||
{
|
||
if (xvt_str_same(numero, "TEL"))
|
||
{
|
||
numero = "TELRF";
|
||
prefisso = "PTELRF";
|
||
} else
|
||
if (xvt_str_same(numero, "FAX"))
|
||
{
|
||
numero = "FAXRF";
|
||
prefisso = "PFAXRF";
|
||
}
|
||
}
|
||
return get_recapito(anag, numero, prefisso);
|
||
}
|
||
|
||
return EMPTY_STRING;
|
||
}
|
||
|
||
bool TSpesometro_set::add_header(const TMask& msk)
|
||
{
|
||
const long codditta = prefix().get_codditta();
|
||
const TAnagrafica ditta(LF_NDITTE, codditta);
|
||
|
||
add_header_data(1, ditta.codice_fiscale());
|
||
add_header_data(2, ditta.partita_IVA());
|
||
|
||
TString16 key;
|
||
key.format("%ld", codditta);
|
||
const TRectype& ndt = cache().get(LF_NDITTE, key);
|
||
const char* codatt = ndt.get(NDT_CODATTPREV);
|
||
key.format("%ld|%s", codditta, codatt);
|
||
const TRectype& att = cache().get(LF_ATTIV, key);
|
||
const TString8 ateco = att.get(ATT_CODATECO);
|
||
add_header_data(3, ateco);
|
||
|
||
add_header_data(4, get_recapito(ndt, NDT_TEL, NDT_PTEL));
|
||
add_header_data(5, get_recapito(ndt, NDT_FAX, NDT_PFAX));
|
||
add_header_data(6, get_recapito(ndt, "MAIL", NULL));
|
||
|
||
if (ditta.fisica())
|
||
{
|
||
add_header_data( 7, ditta.cognome());
|
||
add_header_data( 8, ditta.nome());
|
||
add_header_data( 9, ditta.sesso() == 'F' ? "F" : "M");
|
||
add_header_data(10, ditta.data_nascita().string(full, '\0', full, full, gma_date));
|
||
add_header_data(11, ditta.comune_nascita());
|
||
add_header_data(12, ditta.provincia_nascita());
|
||
add_header_data(13, EMPTY_STRING);
|
||
}
|
||
else
|
||
{
|
||
add_header_data( 7, EMPTY_STRING);
|
||
add_header_data( 8, EMPTY_STRING);
|
||
add_header_data( 9, EMPTY_STRING);
|
||
add_header_data(10, EMPTY_STRING);
|
||
add_header_data(11, EMPTY_STRING);
|
||
add_header_data(12, EMPTY_STRING);
|
||
add_header_data(13, ditta.ragione_sociale());
|
||
}
|
||
|
||
add_header_data(14, msk.get(F_ANNO));
|
||
add_header_data(15, EMPTY_STRING);
|
||
add_header_data(16, TFixed_string("04879210963"));
|
||
|
||
if (ateco.blank())
|
||
return error_box("Manca il codice attivit<69> ATECO nell'anagrafica della ditta %ld", codditta);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool TSpesometro_set::add_footer(const TMask& /*msk*/)
|
||
{
|
||
// Elimina elementi composti da un solo spazio
|
||
for (TRecnotype i = items()-1; i >= 0; i--)
|
||
{
|
||
TToken_string& r = row(i);
|
||
if (r.empty_items())
|
||
{
|
||
destroy(i);
|
||
continue;
|
||
}
|
||
|
||
for (;;)
|
||
{
|
||
const int p = r.find("| |");
|
||
if (p > 0)
|
||
{
|
||
r.overwrite(r.mid(p+2), p+1);
|
||
r.rtrim(1);
|
||
}
|
||
else
|
||
break;
|
||
}
|
||
r.rtrim();
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
void TSpesometro_set::init()
|
||
{
|
||
create_column("Tipo", _alfafld); // 0
|
||
create_column("PartitaIVA", _alfafld);
|
||
create_column("CodiceFiscale", _alfafld);
|
||
create_column("IVANonEsposta", _boolfld);
|
||
create_column("Noleggio", _alfafld);
|
||
create_column("Autofattura", _boolfld); // 5
|
||
create_column("ReverseCharge", _boolfld);
|
||
create_column("DataDoc", _datefld);
|
||
create_column("DataReg", _datefld);
|
||
create_column("NumDoc", _alfafld);
|
||
create_column("TipoOperazione", _alfafld); // 10
|
||
create_column("TipoDocumento", _alfafld);
|
||
create_column("Imponibile", _realfld); // 12
|
||
create_column("Imposta", _realfld);
|
||
|
||
create_column("Cognome", _alfafld); // 14
|
||
create_column("Nome", _alfafld);
|
||
create_column("DataNasc", _datefld);
|
||
create_column("ComNasc", _alfafld);
|
||
create_column("Provincia", _alfafld);
|
||
create_column("StatoEstero", _alfafld); // 19
|
||
create_column("CittaEstera", _alfafld);
|
||
create_column("IndirEstero", _alfafld);
|
||
|
||
create_column("RagSoc", _alfafld); // 22
|
||
create_column("CittaEsteraSede", _alfafld);
|
||
create_column("StatoEsteroSede", _alfafld);
|
||
create_column("IndirEsteroSede", _alfafld);
|
||
create_column("CodiceIVAEstero", _alfafld);
|
||
|
||
create_column("BlackList", _alfafld); // 27 S o M o blank
|
||
create_column("NonResidente", _boolfld);
|
||
create_column("AcquistoDaNonRes", _boolfld);
|
||
create_column("TipoImponibile", _alfafld);
|
||
|
||
create_column("OperazAggAttive", _intfld); // 31
|
||
create_column("OperazAggPassive", _intfld);
|
||
|
||
create_column("FatturaRiepilogativa", _boolfld);
|
||
create_column("SchedaCarburante", _boolfld); // 34
|
||
}
|
||
|
||
TSpesometro_set::TSpesometro_set(const TFilename& file) : TCSV_recordset("CSV(|)")
|
||
{
|
||
init();
|
||
load_file(file);
|
||
move_first();
|
||
}
|
||
|
||
TSpesometro_set::TSpesometro_set() : TCSV_recordset("CSV(|)")
|
||
{
|
||
init();
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TCofi_cache
|
||
///////////////////////////////////////////////////////////
|
||
|
||
class TCofi_cache : public TCache
|
||
{
|
||
TLocalisamfile _clifo, _occas;
|
||
|
||
protected:
|
||
virtual TObject* key2obj(const char* key);
|
||
TObject* find_ragsoc(bool piva, char tipocf, const TString& code);
|
||
|
||
public:
|
||
const TString& cofi2ragsoc(char tipocf, const TString& cofi);
|
||
const TString& paiv2ragsoc(char tipocf, const TString& paiv);
|
||
TCofi_cache() : _clifo(LF_CLIFO), _occas(LF_OCCAS) { }
|
||
};
|
||
|
||
TObject* TCofi_cache::find_ragsoc(bool piva, char tipocf, const TString& code)
|
||
{
|
||
const int fkey = piva ? 5 : 4;
|
||
const char* kfield = piva ? CLI_PAIV : CLI_COFI;
|
||
_clifo.setkey(fkey);
|
||
_clifo.zero();
|
||
_clifo.put(CLI_TIPOCF, tipocf);
|
||
_clifo.put(kfield, code);
|
||
|
||
int err = _clifo.read();
|
||
|
||
if (err == NOERR && _clifo.get_int(CLI_ALLEG) == 4) // Codice Fiscale o Partita IVA doppia
|
||
{
|
||
const long codall = _clifo.get_long(CLI_CODALLEG);
|
||
if (codall > 0)
|
||
{
|
||
const TRecnotype recno = _clifo.recno();
|
||
_clifo.setkey(1);
|
||
_clifo.put(CLI_TIPOCF, tipocf);
|
||
_clifo.put(CLI_CODCF, codall);
|
||
err = _clifo.read();
|
||
if (err != NOERR || _clifo.get(kfield) != code)
|
||
err = _clifo.readat(recno);
|
||
_clifo.setkey(fkey);
|
||
}
|
||
}
|
||
|
||
if (err != NOERR && piva && pi_check("IT", code))
|
||
{
|
||
_clifo.put(CLI_TIPOCF, tipocf);
|
||
_clifo.put(CLI_STATOPAIV, "IT");
|
||
_clifo.put(CLI_PAIV, code);
|
||
err = _clifo.read();
|
||
}
|
||
|
||
if (err == NOERR)
|
||
return new TString80(_clifo.get(CLI_RAGSOC));
|
||
|
||
if (piva && !pi_check("IT", code)) // cerco partite IVA estere
|
||
{
|
||
TString query;
|
||
query << "USE CLIFO SELECT PAIV=\"" << code << '"'
|
||
<< "\nFROM TIPOCF=" << tipocf
|
||
<< "\nTO TIPOCF=" << tipocf;
|
||
TISAM_recordset clifo(query);
|
||
if (clifo.move_first())
|
||
return new TString80(clifo.get(CLI_RAGSOC).as_string());
|
||
}
|
||
|
||
_occas.put(OCC_CFPI, code);
|
||
if (_occas.read() == NOERR)
|
||
{
|
||
const TString& cfpi = _occas.get(piva ? OCC_PAIV : OCC_COFI);
|
||
if (code == cfpi || cfpi.empty())
|
||
return new TString80(_occas.get(OCC_RAGSOC));
|
||
}
|
||
|
||
TString query;
|
||
query << "USE OCCAS SELECT ";
|
||
if (piva)
|
||
query << "PAIV=\"" << code << '"';
|
||
else
|
||
query << "COFI=\"" << code << '"';
|
||
TISAM_recordset occas(query);
|
||
if (occas.move_first())
|
||
return new TString80(occas.get(OCC_RAGSOC).as_string());
|
||
|
||
return NULL;
|
||
}
|
||
|
||
TObject* TCofi_cache::key2obj(const char* key)
|
||
{
|
||
TToken_string chiave(key);
|
||
const bool paiv = chiave.get_char(0)=='P';
|
||
const char tipocf = chiave.get_char(1);
|
||
TString16 code; chiave.get(2, code);
|
||
|
||
TObject* ragsoc = NULL;
|
||
if (code.full())
|
||
{
|
||
if (tipocf != 'C' && tipocf != 'F')
|
||
{
|
||
ragsoc = find_ragsoc(paiv, 'C', code);
|
||
if (ragsoc == NULL)
|
||
ragsoc = find_ragsoc(paiv, 'F', code);
|
||
}
|
||
else
|
||
ragsoc = find_ragsoc(paiv, tipocf, code);
|
||
}
|
||
return ragsoc;
|
||
}
|
||
|
||
const TString& TCofi_cache::cofi2ragsoc(char tipocf, const TString& cofi)
|
||
{
|
||
const TString* ragsoc = NULL;
|
||
if (cofi.full())
|
||
{
|
||
TString80 key;
|
||
key.format("CF|%c|%s", tipocf, (const char*)cofi);
|
||
ragsoc = (const TString*)objptr(key);
|
||
}
|
||
if (ragsoc == NULL)
|
||
{
|
||
TString& str = get_tmp_string();
|
||
str << TR("C.F. sconosciuto ") << cofi;
|
||
ragsoc = &str;
|
||
}
|
||
return *ragsoc;
|
||
}
|
||
|
||
const TString& TCofi_cache::paiv2ragsoc(char tipocf, const TString& paiv)
|
||
{
|
||
const TString* ragsoc = NULL;
|
||
if (paiv.full())
|
||
{
|
||
TString80 key;
|
||
key.format("PI|%c|%s", tipocf, (const char*)paiv);
|
||
ragsoc = (const TString*)objptr(key);
|
||
}
|
||
if (ragsoc == NULL)
|
||
{
|
||
TString& str = get_tmp_string();
|
||
str << TR("P.I. sconosciuta ") << paiv;
|
||
ragsoc = &str;
|
||
}
|
||
return *ragsoc;
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TSpesometro_rep
|
||
///////////////////////////////////////////////////////////
|
||
|
||
const TString& TSpesometro_rep::cofi2ragsoc(char tipocf, const TString& cofi) const
|
||
{ return ((TCofi_cache*)_cofi)->cofi2ragsoc(tipocf, cofi); }
|
||
|
||
const TString& TSpesometro_rep::paiv2ragsoc(char tipocf, const TString& cofi) const
|
||
{ return ((TCofi_cache*)_cofi)->paiv2ragsoc(tipocf, cofi); }
|
||
|
||
bool TSpesometro_rep::get_usr_val(const TString& name, TVariant& var) const
|
||
{
|
||
if (name == "ANNO")
|
||
{
|
||
const TFixed_string d("DataReg");
|
||
TVariant var;
|
||
if (!get_record_field(d, var))
|
||
var = "2015";
|
||
else
|
||
var = var.as_string().right(4);
|
||
return true;
|
||
}
|
||
|
||
return TReport::get_usr_val(name, var);
|
||
}
|
||
|
||
TSpesometro_rep::TSpesometro_rep(const TFilename& file)
|
||
{
|
||
_cofi = new TCofi_cache;
|
||
load("fe0100");
|
||
|
||
TSpesometro_set* set = new TSpesometro_set(file);
|
||
|
||
// Elimina dati di testata e coda
|
||
for (TRecnotype i = set->items()-1; i >= 0; i--)
|
||
{
|
||
if (set->move_to(i))
|
||
{
|
||
const TString& t = set->get(0).as_string();
|
||
if (t.blank() || t.starts_with("SO"))
|
||
set->destroy(i);
|
||
}
|
||
else
|
||
break;
|
||
}
|
||
|
||
set_recordset(set);
|
||
}
|
||
|
||
TSpesometro_rep::~TSpesometro_rep()
|
||
{ delete _cofi; }
|
||
|