campo-sirio/cg/cg3900.cpp

1361 lines
39 KiB
C++
Raw Normal View History

#include "cg3.h"
#include "cg2103.h"
#include "cg3900a.h"
#include <applicat.h>
#include <automask.h>
#include <defmask.h>
#include <progind.h>
#include <recarray.h>
#include <reprint.h>
#include <reputils.h>
#include <textset.h>
#include <validate.h>
#include <anafis.h>
#include <anagr.h>
#include <anagiu.h>
#include <causali.h>
#include <clifo.h>
#include <comuni.h>
#include <mov.h>
#include <nditte.h>
#include <occas.h>
#include <rmoviva.h>
///////////////////////////////////////////////////////////
// TAllegato_info
///////////////////////////////////////////////////////////
struct TAllegato_importi : public TObject
{
real _imp, _iva; // Imponibile ed imposta
real _impNI, _impES, _ivaNE; // Non imponibili, esenti, Non esposti
real _ind; // Valore indecifrabile valido solo per i fornitori fino al 2008
real _impNA; // Pattumiera per tutti valori NON in allegato
bool is_not_null(bool strict) const;
};
bool TAllegato_importi::is_not_null(bool strict) const
{
if (!strict && !_impNA.is_zero())
return true;
return !(_imp.is_zero() && _iva.is_zero() && _ind.is_zero() &&
_impNI.is_zero() && _impES.is_zero() && _ivaNE.is_zero());
}
class TAllegato_info : public TObject
{
char _tipo;
long _codice;
TString80 _occas; // Eventuale codice cliente occasionale
TAllegato_importi _curr, _prec;
public:
bool occasionale() const { return _tipo == 'C' && _occas.full(); }
char tipo() const { return _tipo; }
TVariant get(const TString& field) const;
void key(TString& str) const;
TAllegato_importi& importi(bool prec) { return prec ? _prec : _curr; }
const TAllegato_importi& importi(bool prec) const { return prec ? _prec : _curr; }
bool is_not_null(bool strict) const { return _curr.is_not_null(strict) || _prec.is_not_null(strict); }
TAllegato_info(const TRecordset& mov);
};
// Legge un campo dal record virtuale del cliente / fornitore
TVariant TAllegato_info::get(const TString& field) const
{
TVariant var;
if (field.starts_with("C_") || field.starts_with("P_"))
{
const bool anno_prec = field[0] == 'P';
const TAllegato_importi& allimp = importi(anno_prec);
if (allimp.is_not_null(false))
{
const TString& impfield = field.mid(2);
real euro;
if (impfield == "IMP")
euro = allimp._imp; else
if (impfield == "IVA")
euro = allimp._iva; else
if (impfield == "NI")
euro = allimp._impNI; else
if (impfield == "ES")
euro = allimp._impES; else
if (impfield == "NE")
euro = allimp._ivaNE; else
if (impfield == "IND")
euro = allimp._ind; else
if (impfield == "NA")
euro = allimp._impNA;
euro.round(0);
var = euro;
}
} else
if (field == CLI_TIPOCF)
{
const char t[2] = { _tipo, '\0' };
var = t;
} else
if (field == CLI_CODCF)
{
var = _codice;
} else
if (field == COM_DENCOM || field == COM_PROVCOM)
{
TString8 key;
key << get(CLI_STATOCF) << '|' << get(CLI_COMCF);
var = cache().get(LF_COMUNI, key, field);
if (var.is_empty() && key[0] == '|') // Ritento col CAP per gli Italiani
{
key = " |";
key << cap2comune(get(CLI_CAPCF).as_string(), get(CLI_LOCCF).as_string());
var = cache().get(LF_COMUNI, key, field);
}
}
else
{
if (occasionale())
{
const TRectype& occas = cache().get(LF_OCCAS, _occas);
// Tratto a parte COFI e PAIV degli occasionali
if (field == CLI_COFI || field == CLI_PAIV)
{
// Questi campi sono nel tracciato solo dalla versione 3.1 in poi
if (occas.type(field) != _nullfld)
var = occas.get(field);
/* Troppo astuto per "Sirio il Dragone"
// Se non compilato cerco di desumerlo da codice occasionale
if (var.is_empty())
{
if (field == CLI_COFI)
{
if (cf_check("", _occas))
var = _occas;
}
else
{
if (pi_check("", _occas))
var = _occas;
}
}
*/
}
else // Tutti gli altri campi a parte COFI e PAIV
{
// Converto opportunamente il nome del campo da CLIFO ad OCCAS
const char* const cli[] = { CLI_RAGSOC, CLI_STATOCF, CLI_COMCF, CLI_STATOPAIV,
CLI_INDCF, CLI_CAPCF, CLI_LOCCF, NULL, };
const char* const occ[] = { OCC_RAGSOC, OCC_STATO, OCC_COM, OCC_STATO,
OCC_INDIR, OCC_CAP, OCC_LOCALITA, NULL, };
for (int f = 0; cli[f]; f++)
{
if (field == cli[f])
{
var = occas.get(occ[f]);
break;
}
}
}
}
else // Clienti/Fornitori "normali" (non occasionali)
{
TString16 key; key.format("%c|%ld", _tipo, _codice);
const TRectype& clifo = cache().get(LF_CLIFO, key);
var = clifo.get(field);
}
}
return var;
}
void TAllegato_info::key(TString& tmp) const
{
tmp = get(CLI_PAIV).as_string();
if (tmp.blank())
{
tmp = get(CLI_COFI).as_string();
if (tmp.blank())
{
if (occasionale())
tmp = _occas;
else
tmp = _codice;
}
}
tmp.insert(_tipo == 'F' ? "F|" : "C|");
}
TAllegato_info::TAllegato_info(const TRecordset& mov)
: _tipo(mov.get(MOV_TIPO).as_string()[0]),
_codice(mov.get(MOV_CODCF).as_int()),
_occas(mov.get(MOV_OCFPI).as_string())
{
CHECK(_tipo == 'C' || _tipo == 'F', "Tipo C/F errato");
CHECKD(_codice > 0, "Codice C/F errato ", _codice);
}
///////////////////////////////////////////////////////////
// TAllegati_set
///////////////////////////////////////////////////////////
class TAllegati_set : public TAS400_recordset
{
protected:
const TString& comune(const TRectype& clifo, const char* field) const;
public:
void add(const TAllegato_info& info);
TAllegati_set();
};
void TAllegati_set::add(const TAllegato_info& info)
{
new_rec();
for (unsigned int i = 0; i < columns(); i++)
{
const TString& field = column_info(i)._name;
set(field, info.get(field));
}
}
TAllegati_set::TAllegati_set()
: TAS400_recordset("AS400(512)")
{
const char* const campi[] = { CLI_TIPOCF, CLI_CODCF, CLI_RAGSOC, CLI_COFI, CLI_PAIV,
CLI_INDCF, CLI_CIVCF, CLI_CAPCF, CLI_LOCCF, CLI_COMCF,
NULL };
TRectype clifo(LF_CLIFO);
for (int i = 0; campi[i]; i++)
{
const char* f = campi[i];
create_field(f, -1, clifo.length(f), clifo.type(f));
}
create_field(COM_DENCOM, -1, 50, _alfafld);
create_field(COM_PROVCOM, -1, 2, _alfafld);
for (int j = 0; j < 2; j++)
{
create_field(j ? "P_IMP" : "C_IMP", -1, 16, _longfld);
create_field(j ? "P_IVA" : "C_IVA", -1, 16, _longfld);
create_field(j ? "P_IND" : "C_IND", -1, 16, _longfld);
create_field(j ? "P_NI" : "C_NI", -1, 16, _longfld);
create_field(j ? "P_ES" : "C_ES", -1, 16, _longfld);
create_field(j ? "P_NE" : "C_NE", -1, 16, _longfld);
create_field(j ? "P_NA" : "C_NA", -1, 16, _longfld);
}
}
///////////////////////////////////////////////////////////
// TPadoaSchioppa_set
///////////////////////////////////////////////////////////
enum TPs_type { AN, CF, DT, NU, PI, PR };
enum TPs_campi_non_posizionali { npCode = 8, npValue = 16, npSize = 24, npCount = 70 };
class TPadoaSchioppa_set : public TAS400_recordset
{
void add_field(const int trc, int da, int a, int lung, const char* name,
TPs_type ft, bool required = false, const char* def = NULL);
void add_trc(int trc);
protected:
int primo_campo_libero();
public:
long new_rec(int tiporec); // 0, 1, 2, 3, 9, -1 (1 o 2 a seconda del tipo corrente)
const TString& curr_row() const { return row(-1); }
// Campi posizionali (record 0, 3 e 9)
bool set(const char* name, const TVariant& value);
bool set(const char* name, const TString& value);
bool set(const char* name, const TDate& value);
// Campi non posizionali (record 1 e 2)
bool set(int fldcode, const TString& val);
bool set(int fldcode, long val);
bool set(int fldcode, const TVariant& val);
TPadoaSchioppa_set();
};
long TPadoaSchioppa_set::new_rec(int tiporec)
{
if (tiporec < 0)
{
tiporec = atoi(rec_type());
CHECKD(tiporec == 1 || tiporec == 2, "Tipo record non duplicabile ", tiporec);
}
CHECKD(tiporec >= 0 && tiporec <= 3 || tiporec == 9, "Tipo record errato ", tiporec);
char tr[2] = { '0'+tiporec, '\0' };
return TAS400_recordset::new_rec(tr);
}
void TPadoaSchioppa_set::add_field(int trc, int da, int a, int lung, const char* name,
TPs_type tc, bool required, const char* def)
{
CHECKS(da > 0 && a >= da, "Posizioni incoerenti sul campo ", name);
CHECKS(lung == a-da+1, "Lunghezza incoerente sul campo ", name);
CHECKS(lung == a-da+1, "Lunghezza incoerente sul campo ", name);
TString80 field; field.format("%d.%s", trc, (const char*)name);
TFieldtypes ft = _alfafld;
switch (tc)
{
case DT:
CHECKS(lung == 8, "Lunghezza incoerente data ", name);
ft = _longzerofld;
break;
case NU:
ft = _longfld;
break;
case CF:
CHECKS(lung == 16, "Lunghezza incoerente codice fiscale ", name);
ft = _alfafld;
break;
case PI:
CHECKS(lung == 11, "Lunghezza incoerente partita IVA ", name);
ft = _longzerofld;
break;
case PR:
CHECKS(lung == 2, "Lunghezza incoerente provincia ", name);
ft = _alfafld;
break;
default:
ft = _alfafld;
break;
}
bool ok = false;
if (def && *def)
ok = create_field(field, da-1, lung, ft, required, TVariant(def));
else
ok = create_field(field, da-1, lung, ft, required);
CHECKS(ok, "Impossibile creare il campo ", (const char*)field);
}
void TPadoaSchioppa_set::add_trc(int trc)
{
TString4 str; str << trc;
add_field(trc, 1, 1, 1, "TipoRecord", NU, true, str);
switch (trc)
{
case 0: // Testata
case 9: // Coda
add_field(trc, 2, 4, 3, "IndentificativoFornitura", AN, true, "ECF");
add_field(trc, 5, 6, 2, "IdentificativoAnno" , AN, true, "00");
add_field(trc, 7, 8, 2, "CodiceFornitura" , NU, true, "38");
add_field(trc, 9, 24, 16, "CodiceFiscale" , CF, true);
add_field(trc, 25, 35, 11, "PartitaIVA" , PI, true);
add_field(trc, 36, 61, 26, "Cognome" , AN); // Persone fisiche
add_field(trc, 62, 86, 25, "Nome" , AN);
add_field(trc, 87, 87, 1, "Sesso" , AN);
add_field(trc, 88, 95, 8, "DataNascita" , DT);
add_field(trc, 96, 135, 40, "ComuneNascita" , AN);
add_field(trc, 136, 137, 2, "ProvinciaNascita" , PR);
add_field(trc, 138, 207, 70, "Denominazione" , AN); // Persone NON fisiche
add_field(trc, 208, 247, 40, "ComuneSedeLegale" , AN);
add_field(trc, 248, 249, 2, "ProvinciaSedeLegale" , AN);
add_field(trc, 250, 265, 16, "CodiceSoggettoObbligato" , CF);
add_field(trc, 266, 269, 4, "AnnoRiferimento" , NU, true);
add_field(trc, 270, 273, 4, "ProgressivoTelematico" , NU);
add_field(trc, 274, 277, 4, "NumeroTotaleInvii" , NU);
add_field(trc, 278, 293, 16, "CodiceIntermediario" , CF);
add_field(trc, 294, 298, 5, "NumeroIscrizioneAlboCAF" , NU);
add_field(trc, 299, 299, 1, "ImpegnoAllaTrasmissione" , NU);
add_field(trc, 300, 307, 8, "DataImpegno" , DT);
break;
case 1: // Clienti
case 2: // Fornitori
// NESSUN CAMPO POSIZIONALE!
break;
case 3:
add_field(trc, 2, 9, 8, "Clienti" , NU); // 2
add_field(trc, 10, 17, 8, "Fornitori" , NU);
add_field(trc, 18, 37, 20, "CL004001" , NU); // 4
add_field(trc, 38, 57, 20, "CL004002" , NU);
add_field(trc, 58, 77, 20, "CL005001" , NU);
add_field(trc, 78, 97, 20, "CL006001" , NU);
add_field(trc, 98, 117, 20, "CL007001" , NU);
add_field(trc, 118, 137, 20, "CL008001" , NU); // 9
add_field(trc, 138, 157, 20, "CL008002" , NU);
add_field(trc, 158, 177, 20, "CL009001" , NU);
add_field(trc, 178, 197, 20, "CL010001" , NU);
add_field(trc, 198, 217, 20, "CL011001" , NU); // 13
add_field(trc, 218, 237, 20, "FR004001" , NU); // 14
add_field(trc, 238, 257, 20, "FR004002" , NU);
add_field(trc, 258, 277, 20, "FR005001" , NU);
add_field(trc, 278, 297, 20, "FR006001" , NU);
add_field(trc, 298, 317, 20, "FR007001" , NU);
add_field(trc, 318, 337, 20, "FR008001" , NU);
add_field(trc, 338, 357, 20, "FR009001" , NU); // 20
add_field(trc, 358, 377, 20, "FR009002" , NU);
add_field(trc, 378, 397, 20, "FR010001" , NU);
add_field(trc, 398, 417, 20, "FR011001" , NU);
add_field(trc, 418, 437, 20, "FR012001" , NU);
add_field(trc, 438, 457, 20, "FR013001" , NU); // 25
break;
default:
CHECKD(false, "Tipo record non valido ", trc);
break;
}
add_field(trc,1798,1798, 1, "CarattereControllo" , AN, true, "A");
add_field(trc,1799,1800, 2, "FineRiga" , AN, true, "\r\n"); // 0D 0A
}
TPadoaSchioppa_set::TPadoaSchioppa_set()
: TAS400_recordset("AS400(1800,1)")
{
add_trc(0);
add_trc(1);
add_trc(2);
add_trc(3);
add_trc(9);
}
int TPadoaSchioppa_set::primo_campo_libero()
{
const TToken_string& curr = row(-1); // Record corrente
int i = 0, pos = 1;
for (i = 0; i < npCount; i++, pos += npSize) // Scandice i 70 campi non posizionali
{
// if (curr.mid(pos, fld_size).blank())
if (curr[pos] == ' ' && curr[pos+npSize-1] == ' ') // Campo vuoto!
break;
}
if (i >= npCount) // Non c'e' piu' posto!
{
new_rec(-1); // Creo un nuovo record del tipo corrente (1 o 2)
pos = 1; // Vado a colpo "sicuro"
}
return pos;
}
bool TPadoaSchioppa_set::set(const char* name, const TVariant& value)
{
bool ok = false;
const int tiporec = atoi(rec_type());
if (tiporec == 1 || tiporec == 2) // Gestione campi non posizionali
{
CHECKS(name!=NULL && strlen(name) == npCode,
"Lunghezza nome campo non posizionale errata ", name);
CHECKS((tiporec==1 && *name=='C') || (tiporec==2 && *name=='F'),
"Campo non posizionale incompatibile col tipo record (1/2=C/F) ", name);
const long codice = atol(name+2);
CHECKS(codice >= 1001 && codice <= 13001, "Campo non posizionale errato ", name);
if (value.is_empty())
{
ok = codice > 4000;
CHECKS(ok, "Campo obbligatorio non specificato ", name);
return ok;
}
const int pos = primo_campo_libero();
TString80 str = name;
if (codice < 4000) // Campi alfanumerici (1001,2001,3001) allineati a sinistra
{
str << value.as_string();
str.rpad(npSize);
}
else
{ // Importi interi allineati a destra
real r = value.as_real();
r.round(0); // Inutile in quanto gia' arrotondato all'origine: ma non si sa mai
str << r.string(npValue, 0);
}
CHECKS(str.len() == npSize, "Lunghezza campo non posizionale codice(8)+valore(16) <> 24 ", (const char*)str);
row(-1).overwrite(str, pos, npSize);
ok = true;
}
else
{
// Gestisco le date solo nei record 0,3,9
if (value.is_date() && !value.is_empty())
{
const TDate date = value.as_date();
TString8 str;
str.format("%02d%02d%04d", date.day(), date.month(), date.year());
ok = TAS400_recordset::set(name, TVariant(str));
}
else
{
if (value.is_string())
{
TString80 str = value.as_string();
str.upper();
ok = TAS400_recordset::set(name, TVariant(str));
}
else
ok = TAS400_recordset::set(name, value);
}
CHECKS(ok, "Campo non valido ", name);
}
return ok;
}
bool TPadoaSchioppa_set::set(const char* name, const TString& value)
{
return set(name, value.full() ? TVariant(value) : NULL_VARIANT);
}
bool TPadoaSchioppa_set::set(const char* name, const TDate& value)
{
return set(name, value.ok() ? TVariant(value) : NULL_VARIANT);
}
// Usata per 1001, 2001 e 3001
bool TPadoaSchioppa_set::set(int fldcode, const TString& val)
{
CHECKD(fldcode >= 1001 && fldcode <= 3001, "Codice campo non valido", fldcode);
CHECKD(val.full(), "Campo non posizionale vuoto ", fldcode);
const int tiporec = atoi(rec_type());
TString8 field;
field.format("%2s%06d", tiporec == 1 ? "CL" : "FR", fldcode);
return set(field, val);
}
// Usata solo per 1001
bool TPadoaSchioppa_set::set(int fldcode, long val)
{
CHECKD(fldcode == 1001, "Codice campo non valido", fldcode);
CHECKD(val > 0, "Campo non posizionale vuoto ", fldcode);
TString16 value;
value.format("%16ld", val);
return set(fldcode, value);
}
// Usata dal 4001 in poi
bool TPadoaSchioppa_set::set(int fldcode, const TVariant& val)
{
CHECKD(fldcode >= 4001 && fldcode <= 13001, "Codice campo non valido", fldcode);
if (val.is_zero())
return true;
const int tiporec = atoi(rec_type());
TString8 field;
field.format("%2s%06d", tiporec == 1 ? "CL" : "FR", fldcode);
return set(field, val);
}
///////////////////////////////////////////////////////////
// TCodiva_cache
///////////////////////////////////////////////////////////
class TCodiva_cache : public TCache
{
protected:
virtual TObject* key2obj(const char* key);
public:
const TCodiceIVA& codiva(const char* codice);
};
TObject* TCodiva_cache::key2obj(const char* key)
{
return new TCodiceIVA(key);
}
const TCodiceIVA& TCodiva_cache::codiva(const char* codice)
{
return *(TCodiceIVA*)objptr(codice);
}
///////////////////////////////////////////////////////////
// TCusali_cache
///////////////////////////////////////////////////////////
class TCausali_cache : public TCache
{
protected:
virtual TObject* key2obj(const char* key);
public:
const TCausale& causale(const char* codice, int annoiva);
};
TObject* TCausali_cache::key2obj(const char* key)
{
// Metodo bastardo per evitare TToken_string temporanee "a randa"
// sfrutto chiave a lunghezza fissa
const int anno = atoi(key);
const char* codice = key+5;
TCausale* pcaus = new TCausale(codice, anno);
return pcaus;
}
const TCausale& TCausali_cache::causale(const char* codice, int annoiva)
{
// Metodo bastardo per evitare TToken_string temporanee "a randa"
// creo chiave a lunghezza fissa anno+codice = 9999|AAA
TString8 key;
key.format("%04d|%s", annoiva, codice);
return *(const TCausale*)objptr(key);
}
///////////////////////////////////////////////////////////
// TAlleg_report
///////////////////////////////////////////////////////////
class TAlleg_report : public TReport
{
int _anno;
protected:
virtual bool use_mask() { return false; }
virtual bool get_usr_val(const TString& name, TVariant& var) const;
public:
TAlleg_report(TRecordset* rs, int anno);
};
bool TAlleg_report::get_usr_val(const TString& name, TVariant& var) const
{
if (name == "ANNO")
{
var.set(_anno);
return true;
}
if (name == "PREC")
{
var.set(_anno-1);
return true;
}
return TReport::get_usr_val(name, var);
}
TAlleg_report::TAlleg_report(TRecordset* rs, int anno) : _anno(anno)
{
load("cg3900a");
set_recordset(rs);
}
///////////////////////////////////////////////////////////
// TAlleg_mask
///////////////////////////////////////////////////////////
class TAlleg_mask : public TAutomask
{
TCodiva_cache _codiva;
TCausali_cache _causali;
bool _red_alert;
protected: // TAutomask
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
protected: // Elaborazione
const TRectype& contribuente() const;
bool documento_corrispettivi(const TString& tipodoc) const;
int imp_iva(char tipocf, bool non_esposti, const TRecordset& riga, real& imp, real& iva);
real indetraibile_al(const TRecordset& riga, bool prorata100) const;
bool scan_iva_rows(const TRecordset& mov, TAssoc_array& clifi, bool strict);
TRecordset* new_recordset(char tipocf = ' ', bool strict = false);
void log_error(const char* ragsoc, const char* msg1, const char* msg2,
TLog_report& errlog, int severity = 2);
void log_error(const TRecordset& clifo, const char* msg1, const char* msg2,
TLog_report& errlog, int severity = 2);
void copia_importo(const TRecordset& clifo, const char* fldname,
TPadoaSchioppa_set& pss, int fldcode) const;
void add_0_9(int trc, TPadoaSchioppa_set& pss, TLog_report& log);
void add_1_2(int trc, TRecordset& clifo, TPadoaSchioppa_set& pss, TLog_report& log);
void add_3(TPadoaSchioppa_set& pss);
void generazione();
public:
TAlleg_mask();
~TAlleg_mask();
};
bool TAlleg_mask::documento_corrispettivi(const TString& tipodoc) const
{
const TRectype& tpd = cache().get("%TPD", tipodoc);
bool corrisp = tpd.get_bool("B0");
if (corrisp)
{
const int natura_doc = tpd.get_int("I0");
corrisp = natura_doc == 1 || natura_doc == 9;
}
return corrisp;
}
int TAlleg_mask::imp_iva(char tipocf, bool non_esposti, const TRecordset& riga, real& imp, real& iva)
{
// Estraggo imponibile ed imposta dalla riga IVA ed
// inizialmente fingo che non ci sia una parte indetraibile
imp = riga.get(RMI_IMPONIBILE).as_real();
iva = riga.get(RMI_IMPOSTA).as_real();
int allegato = 0;
if (non_esposti)
{
allegato = 4;
}
else
{
const TString& codice = riga.get(RMI_CODIVA).as_string();
const TCodiceIVA& codiva = _codiva.codiva(codice);
allegato = codiva.allegato(tipocf);
if (allegato == 5 && get_int(F_ANNO) >= 2008) // FR005001 non esiste piu' nel 2008
allegato = 1;
}
if (allegato != 1 && !iva.is_zero()) // Succede col Reverse Charge
{
imp += iva;
iva = ZERO;
}
return allegato;
}
bool TAlleg_mask::scan_iva_rows(const TRecordset& mov, TAssoc_array& clifi, bool strict)
{
// Informazioni base sul cliente in testata: vengono ignorati gli importi
const TAllegato_info clifo(mov);
if (strict)
{
// Gli occasionali meritano solo di essere ignorati
if (clifo.occasionale())
return false;
const TVariant stato = clifo.get(CLI_STATOPAIV);
if (!stato.is_empty() && stato.as_string() != "IT") // Scarto gli esteri
return false;
// Ignoro da subito chi non va in allegato
const int tipalleg = clifo.get(CLI_ALLEG).as_int();
if (tipalleg != 0 && tipalleg != 4)
return false;
}
TString80 clifo_key; clifo.key(clifo_key);
TAllegato_info* ai = (TAllegato_info*)clifi.objptr(clifo_key);
if (ai == NULL)
{
ai = new TAllegato_info(mov);
clifi.add(clifo_key, ai);
}
const TDate datareg = mov.get(MOV_DATAREG).as_date();
const TDate datadoc = mov.get(MOV_DATADOC).as_date();
const int anno = get_int(F_ANNO);;
const int annoiva = datareg.year();
const TString& codcaus = mov.get(MOV_CODCAUS).as_string();
const TCausale& caus = _causali.causale(codcaus, annoiva);
const bool movintra = caus.intra();
const int reg_spec = caus.regime_speciale();
const bool reverse_charge = (reg_spec == 13) || (reg_spec == 50) || (reg_spec == 51);
const bool non_esposti = movintra || reverse_charge;
const bool prorata100 = caus.reg().prorata100(annoiva);
const bool anno_prec = datadoc.year() == anno-1;
TAllegato_importi& allimp = ai->importi(anno_prec);
TISAM_recordset righe_iva("USE RMOVIVA\nFROM NUMREG=#NR\nTO NUMREG=#NR");
righe_iva.set_var("#NR", mov.get(MOV_NUMREG));
real imponibile, imposta, indecifrabile;
for (bool ok = righe_iva.move_first(); ok; ok = righe_iva.move_next())
{
const int allegato = imp_iva(clifo.tipo(), non_esposti, righe_iva, imponibile, imposta);
switch (allegato)
{
case 1: allimp._imp += imponibile;
allimp._iva += imposta;
break;
case 2: allimp._impNI += imponibile; break;
case 3: allimp._impES += imponibile; break;
case 4: allimp._ivaNE += imponibile; break; // Importi con IVA non esposta
case 5: allimp._ind += imponibile; break; // Importi INDecifrabili
default: allimp._impNA += imponibile; break; // same as case 0
}
}
return true;
}
// algo: 0=AGA; 1=Sirio/Benetti
TRecordset* TAlleg_mask::new_recordset(char tipocf, bool strict)
{
const int algo_dick = get_int(F_ALGO);
// Compito: tradurre in ISAM la seguente query:
// SELECT * FROM MOV
// WHERE TIPO=#TYPE AND ANNOIVA=#YEAR
// ORDER BY TIPO,CODCF;
TString query = "USE MOV KEY 3"; // La chiave 3 e' TIPO+CODCF+DATAREG+NUMREG
query << "\nSELECT (ANNOIVA=#YEAR)"; // Seleziona solo l'anno desiderato
if (algo_dick)
query << "||(ANNOIVA=(#YEAR+1))";
query << "\nFROM TIPO=#TYPE CODCF=1"; // Salta tutti movimenti senza CODCF
query << "\nTO TIPO=#TYPE"; // Inutile dire CODCF=999999
TISAM_recordset mov(query);
if (tipocf <= ' ') tipocf = get(F_TIPO)[0];
const char tipo[2] = { tipocf, '\0' };
const int anno = get_int(F_ANNO);
mov.set_var("#YEAR", TVariant(long(anno)));
mov.set_var("#TYPE", TVariant(tipo));
const TRecnotype movs = mov.items();
TString msg;
msg << movs << ' ' << TR("Movimenti") << ' '
<< (tipocf == 'C' ? TR("Clienti") : TR("Fornitori"));
TProgind pi(movs, msg, true, true);
TAssoc_array clifi;
for (bool ok = mov.move_first(); ok; ok = mov.move_next())
{
if (!pi.addstatus(1))
break;
const int annoiva = mov.get(MOV_ANNOIVA).as_int();
const TString& codcaus = mov.get(MOV_CODCAUS).as_string();
const TCausale& caus = _causali.causale(codcaus, annoiva);
if (caus.esclusione_allegati()) // Controllo il flag di esclusione dagli allegati
continue;
// Scarta trasferimento movimenti intra prima del 2008
if (strict && anno < 2008 && caus.intra())
continue;
// Controllo inutilmente anche il tipo documento
const TString& tipodoc = caus.tipo_doc();
if (documento_corrispettivi(tipodoc))
continue;
// Controllo se registro e' compatibile
const TRegistro& reg = caus.reg();
if (reg.corrispettivi())
continue;
const TipoIVA tipoiva = reg.iva();
bool keep = tipoiva == iva_vendite || tipoiva == iva_acquisti; // Voglio solo registri IVA
if (keep)
keep = (tipocf == 'C') ^ (tipoiva == iva_acquisti); // compatibile
// Cavolata interpretativa tipicamente italiana
if (keep && algo_dick)
{
const int annodoc = mov.get(MOV_DATADOC).as_date().year();
const bool is_nota = (tipodoc=="NC") || (tipodoc=="ND");
keep = (annodoc == anno) || (is_nota && (annodoc == anno-1));
}
if (!keep)
continue;
scan_iva_rows(mov, clifi, strict);
}
TAllegati_set* hullygully = new TAllegati_set;
FOR_EACH_ASSOC_OBJECT(clifi, h, k, o)
{
const TAllegato_info& ai = *(TAllegato_info*)o;
if (ai.is_not_null(strict))
hullygully->add(ai);
}
hullygully->sort();
return hullygully;
}
const TRectype& TAlleg_mask::contribuente() const
{
const char tipoa = prefix().firm().get(NDT_TIPOA)[0];
const long codanagr = prefix().firm().get_long(NDT_CODANAGR);
TToken_string key;
key.add(tipoa, 0); key.add(codanagr, 1);
const TRectype& anag = cache().get(LF_ANAG, key);
return anag;
}
void TAlleg_mask::add_0_9(int trc, TPadoaSchioppa_set& pss, TLog_report& log)
{
pss.new_rec(trc);
const TRectype& anag = contribuente();
const char tipoa = anag.get_char(ANA_TIPOA);
const long codanagr = anag.get_long(ANA_CODANAGR);
TString16 pariva = anag.get(ANA_PAIV); pariva.right_just(11, '0');
if (!pi_check("", pariva))
log_error(anag.get(ANA_RAGSOC), TR("*** Ditta con partita IVA errata"), pariva, log, 2);
const TString16 codfis = anag.get(ANA_COFI);
if (!cf_check("", codfis))
log_error(anag.get(ANA_RAGSOC), TR("** Ditta con codice fiscale errato"), codfis, log, 2);
pss.set("CodiceFiscale", codfis.full() ? codfis : pariva);
pss.set("PartitaIVA", pariva.full() ? pariva : codfis);
if (tipoa == 'F')
{
pss.set("Cognome", anag.get(ANA_RAGSOC).left(30));
pss.set("Nome", anag.get(ANA_RAGSOC).mid(30));
const TRectype& anafis = cache().get(LF_ANAGFIS, codanagr);
pss.set("Sesso", anafis.get(ANF_SESSO));
pss.set("DataNascita", anafis.get_date(ANF_DATANASC));
const TString4 stato = anafis.get(ANF_STATONASC);
const TString4 comun = anafis.get(ANF_COMNASC);
TString8 key; key << stato << '|' << comun;
const TRectype& comnas = cache().get(LF_COMUNI, key);
pss.set("ComuneNascita", comnas.get(COM_DENCOM));
if (stato.full() || comun[0] >= 'Z') // Nato all'estero
pss.set("ProvinciaNascita", TVariant("EE"));
else
pss.set("ProvinciaNascita", comnas.get(COM_PROVCOM));
}
else
{
const TString4 stato = anag.get(ANA_STATORES);
const TString4 comun = anag.get(ANA_COMRES);
TString8 key; key << stato << '|' << comun;
const TRectype& comres = cache().get(LF_COMUNI, key);
pss.set("Denominazione", anag.get(ANA_RAGSOC));
pss.set("ComuneSedeLegale", comres.get(COM_DENCOM));
pss.set("ProvinciaSedeLegale", comres.get(COM_PROVCOM));
}
pss.set("AnnoRiferimento", get(F_ANNO));
// Compilare solo se diverso da dichiarante
pss.set("CodiceSoggettoObbligato", get(F_SOGG));
const TString& inter = get(F_INTR);
if (inter.full())
{
pss.set("CodiceIntermediario" , inter);
pss.set("NumeroIscrizioneAlboCAF", get(F_CAF));
pss.set("ImpegnoAllaTrasmissione", get(F_COMP));
pss.set("DataImpegno", get_date(F_DATA));
}
}
void TAlleg_mask::add_3(TPadoaSchioppa_set& pss)
{
long num_cli = 0;
long num_for = 0;
TAssoc_array totali;
TString8 code;
for (bool ok = pss.move_first(); ok; ok = pss.move_next())
{
const int tipo = atoi(pss.rec_type());
if (tipo == 1 || tipo == 2)
{
const TString& curr = pss.curr_row();
int pos = 1;
// Scansione dei 70 campi non posizionali
for (int i = 0; i < npCount; i++, pos += npSize)
{
code = curr.mid(pos, npCode); // Codice campo
if (code.blank()) // Campo nullo = Fine record, "certamente" ultimo di questo tipo
break;
const int subcode = atoi(code.mid(2)); // CL004001 -> 4001
if (subcode == 1001) // Numero progressivo ...
{
if (tipo == 1) // ... cliente
num_cli++;
else
num_for++; // ... fornitore
} else
if (subcode >= 4001 && subcode <= 13001) // Cosidera solo gli importi
{
real* tot = (real*)totali.objptr(code);
if (tot == NULL)
{
tot = new real;
totali.add(code, tot);
}
const real val(curr.mid(pos+npCode, npValue)); // Importo sicuramente NON nullo
*tot += val;
}
}
}
}
pss.new_rec(3);
pss.set("Clienti", TVariant(num_cli));
pss.set("Fornitori", TVariant(num_for));
FOR_EACH_ASSOC_OBJECT(totali, hash, key, v)
{
const real& val = *(real*)v;
pss.set(key, val);
}
}
void TAlleg_mask::copia_importo(const TRecordset& clifo, const char* fldname,
TPadoaSchioppa_set& pss, int fldcode) const
{
const TVariant& val = clifo.get(fldname);
if (!val.is_zero())
{
CHECKS(!val.is_string(), "Campo importo di tipo errato", fldname);
pss.set(fldcode, val);
}
}
void TAlleg_mask::log_error(const char* ragsoc, const char* msg1, const char* msg2,
TLog_report& errlog, int severity)
{
TString str;
str << ragsoc << " - " << msg1;
if (msg2 && *msg2)
str << " : " << msg2;
errlog.log(severity, str);
if (severity > 0)
_red_alert = true;
}
void TAlleg_mask::log_error(const TRecordset& clifo, const char* msg1, const char* msg2,
TLog_report& errlog, int severity)
{
TString str;
str << clifo.get(CLI_TIPOCF) << clifo.get(CLI_CODCF)
<< ' ' << clifo.get(CLI_RAGSOC);
str.strip_double_spaces();
log_error(str, msg1, msg2, errlog, severity);
}
void TAlleg_mask::add_1_2(int trc, TRecordset& clifo, TPadoaSchioppa_set& pss, TLog_report& log)
{
const int anno = get_int(F_ANNO);
const bool coffee = get_bool(F_COFI); // Codice fiscale voluto o obbligatorio
long np = 0; // Prossimo numero progressivo clifo
for (bool ok = clifo.move_first(); ok; ok = clifo.move_next())
{
if (np == 0) // Se sto per scrivere il primo elemento ...
pss.new_rec(trc); // ... creo un nuovo record per accoglierlo
pss.set(1001, ++np); // Incremento il numero progressivo
// Gestione automagica del codice fiscale e della partita IVA
TString16 paiv = clifo.get(CLI_PAIV).as_string(); paiv.trim();
if (paiv.full() && real::is_natural(paiv))
paiv.right_just(11, '0');
TString16 cofi = clifo.get(CLI_COFI).as_string(); cofi.trim();
/* Troppo astuto per "Sirio il Dragone"
// Se il codice fiscale e' vuoto prendo la partita IVA numerica
if (cofi.blank() && pi_check("", paiv))
cofi = paiv;
// Se la partita IVA e' vuota ed il codice fiscale e' numerico ...
if (paiv.blank() && pi_check("", cofi))
paiv = cofi; // ... allora copia nella partita IVA il codice fiscale
*/
// Voglio o devo salvare il codice fiscale del tizio
if (coffee)
{
if (cofi.blank())
{
log_error(clifo, TR("* Codice fiscale mancante"), cofi, log, 2);
}
else
{
if (!real::is_natural(cofi) && !cf_check("", cofi))
log_error(clifo, TR("** Codice fiscale errato"), cofi, log, 1);
pss.set(2001, cofi); // Codice fiscale (obbligatorio dal 2008)
}
}
if (real::is_null(paiv))
{
if (anno < 2008 || cofi.blank())
log_error(clifo, TR("** Partita IVA mancante"), NULL, log, 2);
}
else
{
if (!pi_check("", paiv))
log_error(clifo, TR("** Partita IVA errata"), paiv, log, 1);
pss.set(3001, paiv); // Partita IVA (obbligatoria dal 2006)
}
if (trc == 1) // Clienti
{
// Importi anno corrente
copia_importo(clifo, "C_IMP", pss, 4001);
copia_importo(clifo, "C_IVA", pss, 4002);
copia_importo(clifo, "C_NI" , pss, 5001);
copia_importo(clifo, "C_ES" , pss, 6001);
copia_importo(clifo, "C_NE" , pss, 7001);
// Importi anno precedente
copia_importo(clifo, "P_IMP", pss, 8001);
copia_importo(clifo, "P_IVA", pss, 8002);
copia_importo(clifo, "P_NI" , pss, 9001);
copia_importo(clifo, "P_ES" , pss,10001);
copia_importo(clifo, "P_NE" , pss,11001);
}
else // Fornitori
{
// Importi anno corrente
copia_importo(clifo, "C_IMP", pss, 4001);
copia_importo(clifo, "C_IVA", pss, 4002);
copia_importo(clifo, "C_IND", pss, 5001); // Indecifrabile
copia_importo(clifo, "C_NI" , pss, 6001);
copia_importo(clifo, "C_ES" , pss, 7001);
copia_importo(clifo, "C_NE" , pss, 8001);
// Importi anno precedente
copia_importo(clifo, "P_IMP", pss, 9001);
copia_importo(clifo, "P_IVA", pss, 9002);
copia_importo(clifo, "P_IND", pss,10001);
copia_importo(clifo, "P_NI" , pss,11001);
copia_importo(clifo, "P_ES" , pss,12001);
copia_importo(clifo, "P_NE" , pss,13001);
}
}
}
void TAlleg_mask::generazione()
{
TReport_book book;
_red_alert = false;
TLog_report log(TR("Errori riscontrati"));
TPadoaSchioppa_set pss;
add_0_9(0, pss, log); // Testa
TRecordset* cli = new_recordset('C', true);
if (cli != NULL)
{
log.log(0, TR("CLIENTI"));
add_1_2(1, *cli, pss, log); // Clienti
TAlleg_report rep(cli, get_int(F_ANNO));
book.add(rep);
}
TRecordset* ven = new_recordset('F', true);
if (ven != NULL)
{
log.log(0, TR("FORNITORI"));
add_1_2(2, *ven, pss, log); // Fornitori
TAlleg_report rep(ven, get_int(F_ANNO));
book.add(rep);
}
add_3(pss); // Totali Clienti e Fornitori
add_0_9(9, pss, log); // Piede (= Testa)
bool go_on = true;
if (_red_alert)
{
if (yesno_box(TR("Sono stati rilevati errori sulla fornitura:\nSi desidera visualizzarli?")))
{
TReport_book errbuc;
errbuc.add(log);
errbuc.preview();
}
go_on = yesno_box("Si desidera generare ugualmente la fornitura?");
}
if (go_on)
{
TFilename fname = get(F_DIR);
fname.add(get(F_NAME));
pss.save_as(fname);
book.preview();
}
}
bool TAlleg_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
switch (o.dlg())
{
case F_ANNO:
if (e == fe_init || e == fe_modify)
{
int anno = atoi(o.get());
if (anno < 2006)
{
const TDate oggi(TODAY);
anno = oggi.year()-1;
set(F_ANNO, anno);
}
if (anno >= 2008)
{
set(F_COFI, "X");
disable(F_COFI);
}
else
enable(F_COFI);
}
break;
case F_DIR:
if (e == fe_init || e == fe_modify)
{
if (o.empty())
{
TFilename fdir; fdir.tempdir();
o.set(fdir);
}
}
break;
case F_NAME:
if (e == fe_init || e == fe_modify)
{
if (o.empty())
{
const TRectype& anag = contribuente();
TFilename ecf = anag.get(ANA_PAIV);
ecf.right_just(11, '0');
ecf << "_ECF00.ecf";
set(F_NAME, ecf);
o.set(ecf);
}
}
break;
case F_CONT:
if (e == fe_init)
{
const TRectype& anag = contribuente();
o.set(anag.get(ANA_COFI));
o.disable();
}
break;
case F_INTR:
if (e == fe_init && o.empty())
{
TString16 key = prefix().firm().get(NDT_FIRMAT);
if (key.full())
{
key.insert("F|");
o.set(cache().get(LF_ANAG, key, ANA_COFI));
}
}
break;
case F_CAF:
if (o.empty())
{
if (e == fe_init)
o.set(prefix().firm().get(NDT_INTCAF));
}
break;
case DLG_SELECT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TRecordset_sheet sheet(*rs, TR("Elenco di controllo"), 0x8);
while (sheet.run() == K_ENTER)
{
const long sel = sheet.selected();
TToken_string& row = sheet.row(sel);
TRectype clifo(LF_CLIFO);
clifo.put(CLI_TIPOCF, row.get(0));
clifo.put(CLI_CODCF, row.get(1));
clifo.edit();
}
delete rs;
return false;
}
break;
case DLG_EDIT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TFilename fname = get(F_DIR);
fname.add("clifo.xls");
if (rs->save_as(fname, fmt_silk))
xvt_sys_goto_url(fname, "open");
delete rs;
return false;
}
break;
case DLG_PRINT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TAlleg_report rep(rs, get_int(F_ANNO));
TReport_book book;
book.add(rep);
book.print_or_preview();
return false;
}
break;
case DLG_ELABORA:
if (e == fe_button)
{
if (check_fields())
generazione();
return false;
}
break;
default:
break;
}
return true;
}
TAlleg_mask::TAlleg_mask() : TAutomask("cg3900a")
{ }
TAlleg_mask::~TAlleg_mask()
{
save_profile();
}
///////////////////////////////////////////////////////////
// TClifo_alleg_app
///////////////////////////////////////////////////////////
class TClifo_alleg_app : public TSkeleton_application
{
public:
virtual void main_loop();
};
void TClifo_alleg_app::main_loop()
{
// Roba anagrafica
open_files(LF_ANAG, LF_ANAGFIS, LF_COMUNI, LF_NDITTE, LF_OCCAS, LF_TAB, LF_TABCOM, 0);
// Roba contabile
open_files(LF_CAUSALI, LF_CLIFO, LF_MOV, LF_RMOVIVA, 0);
TAlleg_mask m;
m.run();
}
int cg3900(int argc, char* argv[])
{
TClifo_alleg_app app;
app.run(argc, argv, TR("Elenco clienti/fornitori"));
return 0;
}