diff --git a/src/fp/fp0400.cpp b/src/fp/fp0400.cpp index de91a77d4..61fd0f9ae 100644 --- a/src/fp/fp0400.cpp +++ b/src/fp/fp0400.cpp @@ -17,7 +17,6 @@ #define FILTER_ALL "A" #define FILTER_CONT "C" #define PARA_FP "fp" -#define F1_INIREGCONT "regcont" enum { diff --git a/src/fp/fp0700.cpp b/src/fp/fp0700.cpp index efed94f48..4faddf95e 100644 --- a/src/fp/fp0700.cpp +++ b/src/fp/fp0700.cpp @@ -150,7 +150,7 @@ void TPAR_mask::fill() const TCli_for & cli = cached_clifor(mov.get_char(MOV_TIPO), mov.get_long(MOV_CODCF)); bool sent = false; - if (chiave_paf_mov(mov, tipodocsdi, hfatt, bfatt)) + if (chiave_paf_mov(mov, hfatt, bfatt)) { if (paf0100f.search(nullptr, hfatt, bfatt) && paf0100f.sq_get("P1_GESTIONE") != " " && paf0100f.sq_get("P1_ERRINT") != "*") { @@ -598,7 +598,7 @@ void TPAR_mask::set_err_paf() TString hfatt, bfatt; TPaf_record paf0100f("PAF0100F"); - if (chiave_paf_mov(mov, tipodocsdi, hfatt, bfatt) && paf0100f.search(nullptr, hfatt, bfatt)) + if (chiave_paf_mov(mov, hfatt, bfatt) && paf0100f.search(nullptr, hfatt, bfatt)) { TString query; query << diff --git a/src/fp/fplib.h b/src/fp/fplib.h index 2ffb7ba28..4a6f66631 100644 --- a/src/fp/fplib.h +++ b/src/fp/fplib.h @@ -7,6 +7,7 @@ #include "../ve/velib05.h" #include "../fe/felib.h" +#include #include #include #include @@ -35,13 +36,13 @@ TString& complete_num_fp(const TCodice_numerazione& codnum, const int numdoc); // Genera la chiave per i paf -bool chiave_paf_mov(const TRectype& mov, const TString & tipodoc, TString& hfatt, TString& bfatt); +bool chiave_paf_mov(const TRectype& mov, TString& hfatt, TString& bfatt); bool chiave_paf_doc(const TDocumento& doc, TString& hfatt, TString& bfatt); bool chiave_paf_doc(const TRectype& doc, TString& hfatt, TString& bfatt); // Ritorna cod sdi, pec o vuoto. Chiama get_coddest() TString get_dest_sdi(char tipocf, long codcf, const TString& cod_ind_sped); // Valorizza codice sdi e pec in base alle configurazioni del monitor -bool get_coddest(char tipocf, long codcf, TString& coddest, TString& pec, const TString& cod_ind_sped); +bool get_coddest(char tipocf, long codcf, TString& coddest, TString& pec, const TString& cod_ind_sped = EMPTY_STRING); const TString& tipo_doc_sdi(const TDocumento& doc); // Controlli comuni @@ -451,7 +452,17 @@ public: virtual ~TFP_expression() {}; }; -// Gestione PAF +/* + /$$$$$$$$ /$$$$$$$ +|__ $$__/| $$__ $$ + | $$ | $$ \ $$ /$$$$$$ /$$$$$$$ + | $$ | $$ | $$ /$$__ $$ /$$_____/ + | $$ | $$ | $$| $$ \ $$| $$ + | $$ | $$ | $$| $$ | $$| $$ + | $$ | $$$$$$$/| $$$$$$/| $$$$$$$ + |__/ |_______/ \______/ \_______/ +*/ + class TDoc_fp : public TObject { friend class TFP_custom; @@ -462,7 +473,7 @@ private: TAnagrafica _ditta; TString16 _cofi; TFilename _dbname; - TLog_report* _log; + TLog_report _log; TString _logpaf; bool _gestioneallegati; bool _allegafattura; @@ -542,7 +553,7 @@ protected: const TString& descrizione(const TRiga_documento& rdoc); const TRectype& cco(const TRectype& doc) const; // Contratto/Convenzione/Offerta - void log(int severity, const char* msg); + const TString & natura(const char* codiva) const; static const char* get_esigibilita_iva(const TDocumentoEsteso& doc); void set_IVA(TString codiva, TPaf_record& paf) const; @@ -581,4 +592,108 @@ public: TDoc_fp(); ~TDoc_fp(); }; + +/* + /$$$$$$$$ /$$$$$$$ +|__ $$__/| $$__ $$ + | $$ | $$ \ $$ /$$$$$$ /$$$$$$ + | $$ | $$$$$$$/ /$$__ $$ /$$__ $$ + | $$ | $$__ $$| $$$$$$$$| $$ \ $$ + | $$ | $$ \ $$| $$_____/| $$ | $$ + | $$ | $$ | $$| $$$$$$$| $$$$$$$ + |__/ |__/ |__/ \_______/ \____ $$ + /$$ \ $$ + | $$$$$$/ + \______/ +*/ + +class TReg_fp : public TObject +{ + friend class TFP_custom; + +private: + TAnagrafica _ditta; + TString16 _cofi; + TFilename _dbname; + TLog_report _log; + TString _logpaf; + bool _gestioneallegati; + bool _allegafattura; + TString _def_fld; + TString _def_usr_fld; + bool _to_commit; + bool _cache_insert; + vector _query; + TString8 _hfatt; // Codice univoco di 6 caratteri dell'ufficio P.A. o di 7 caratteri per un privato + TString _bfatt; // Codice univoco di 20 caratteri del Regumento + TString _paese; + bool _enapec; + bool _privato; + TString _coddest; + TString _pec; + bool _is_pa; + TString _codivadefault; + long _idx_cassa_previdenziale; + long _idx_adg_reg_row; // Indice per la tabella altri dati gestionali della riga + int _num_linea; + int _counter; + TPaf_container _paf_container; + int _count_r_conai; + bool _nascondi_sconti_righe_fatt; + TFP_nota_piede_f _riga_npf; + bool _has_cust; + + bool insert(TPaf_record& p); + bool remove(TPaf_record& p); + void reset(TPaf_record& p) const { TPaf_container::set_keys_paf(p, _hfatt, _bfatt); } + bool save_paf(); + + +protected: + + /**< Funzioni di controllo */ + /**< Controlli di testata, non const per il calcolo della codizione di pagamento */ + bool check_initial(const TMovimento_contabile& mov); + /**< Controllo di riga */ + bool check_row(const TMovimento_contabile& mov, int n_riga); + /**< Controllo riepilogo IVA */ + + + + bool initialize(const TMovimento_contabile& Reg); + //static bool get_bnp_iban(const TString& abi, const TString& cab, int prg, TString& iban); + //bool get_bank(TRegumento& Reg, TString& iban, TString& abi, TString& cab, TString& istituto) const; + //bool get_bank_presentazione(const TRegumento& Reg, TString& iban, TString& abi, TString& cab, TString& istituto) const; + //bool get_bank_appoggio(const TRegumento& Reg, TString& iban, TString& abi, TString& cab, TString& istituto) const; + const TString& descrizione(const TMovimento_contabile& mov, int n_riga) const { return mov.cg()[n_riga].get((RMV_DESCR));} + + + void save_log() { TReport_book b; b.add(_log); b.export_text(TFilename("fp_err.log"), false, 132); } + const TString & natura(const char* codiva) const; + static const char* get_esigibilita_iva(const TMovimento_contabile& Reg); + void set_IVA(TString codiva, TPaf_record& paf) const; + void set_IVA(const TMovimento_contabile& mov, int n_row, TPaf_record& paf) const; + bool add_row_art(long& riga_art, const TString& codice_tipo, const TString& codice_valore, TPaf_record& paf); + bool add_row_alleg(TFilename& file, long& nprogr, TPaf_record& paf); + void set_qta_prezzo(TPaf_record& paf1800f, const TMovimento_contabile& mov, int n_row) const; + void add_ritenuta(const TMovimento_contabile & mov, int n_row, TPaf_record& paf0700f) const; + bool add_riepilogo_iva(const TMovimento_contabile mov, int n_riga); + + const TFirm& TReg_fp::get_firm(){return prefix().firm();} + + bool export_paf0100f(); + bool export_paf3200f(); + +public: + bool reg_to_paf(const TMovimento_contabile& mov); + bool reg_to_paf(long n_mov); + // Mostra il log a fine esecuzione + bool show_log(); + int commit(); + int force_commit(); + void set_cache_insert(const bool v) { _cache_insert = v; } + + TReg_fp(); + ~TReg_fp(); +}; #endif // __FPLIB_H diff --git a/src/fp/fplib01.cpp b/src/fp/fplib01.cpp index 8516e0660..7ea2afe4a 100644 --- a/src/fp/fplib01.cpp +++ b/src/fp/fplib01.cpp @@ -21,6 +21,9 @@ #include #include #include "../li/letint.h" +#include +#include "../cg/cgsaldac.h" +#include bool set_connection(SSimple_query& s) { @@ -158,8 +161,8 @@ TString& complete_num_fp(const TCodice_numerazione& codnum, const int numdoc) * BFATT: datadoc(8) + tipodoc_SDI(4) + numdoc(7) */ -// Crea la coppia di chiavi per il db PAF a partire da un documento vero e proprio -bool chiave_paf_mov(const TRectype& mov, const TString & tipodoc, TString& hfatt, TString& bfatt) +// Crea la coppia di chiavi per il db PAF a partire da un movimento +bool chiave_paf_mov(const TRectype& mov, TString& hfatt, TString& bfatt) { TCli_for cli(mov.get_char(MOV_TIPO), mov.get_long(MOV_CODCF)); hfatt.cut(0); @@ -174,7 +177,7 @@ bool chiave_paf_mov(const TRectype& mov, const TString & tipodoc, TString& hfatt numdoc.lpad(mov.length(DOC_NDOC), '0'); bfatt = mov.get_date(MOV_DATAREG).date2ansi(); - bfatt << '_' << tipodoc << '_' << numdoc; + bfatt << '_' << numdoc; return hfatt.full() && bfatt.full(); } @@ -843,7 +846,7 @@ const TString& TDoc_fp::descrizione(const TRiga_documento& rdoc) { TString err; err << "Il documento " << rdoc.doc().anno() << " " << rdoc.doc().codice_numerazione().codice() << " " << rdoc.doc().numero() << " ha la riga numero " << rdoc.numero() << " pi lunga di quanto supportato dal formato dell'agenzia delle entrate, verr troncata a 900 caratteri"; - log(1, err); + _log.log(1, err); } return tmp.left(900); } @@ -904,8 +907,8 @@ bool TDoc_fp::insert(TPaf_record& p) ok = p.insert(); if (!ok) { - log(2, fp_db().sq_get_token_text_error(1)); - log(2, p.insert_string()); + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, p.insert_string()); } } return ok; @@ -923,8 +926,8 @@ bool TDoc_fp::remove(TPaf_record& p) ok = p.remove(); if (!ok) { - log(2, fp_db().sq_get_token_text_error(1)); - log(2, p.remove_string()); + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, p.remove_string()); } } return ok; @@ -940,8 +943,8 @@ bool TDoc_fp::save_paf() ok = fp_db().sq_set_exec(query); if (!ok) { - log(2, fp_db().sq_get_token_text_error(1)); - log(2, query.c_str()); + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, query.c_str()); } } return ok; @@ -952,7 +955,7 @@ bool TDoc_fp::check_initial(TDocumentoEsteso& doc) static TString msg; if (_coddest.len() != 6 && _coddest.len() != 7) { - log(1, "Il codice destinatario ha una lunghezza non conforme."); + _log.log(1, "Il codice destinatario ha una lunghezza non conforme."); ok = false; } @@ -961,7 +964,7 @@ bool TDoc_fp::check_initial(TDocumentoEsteso& doc) if (_rec_clifo.get(CLI_PAIV).empty() && _rec_clifo.get(CLI_COFI).empty() && !privato) { - log(1, "Sia la partita IVA che il codice fiscale del cessionario committente sono vuoti, almeno uno dei due deve essere valorizzato."); + _log.log(1, "Sia la partita IVA che il codice fiscale del cessionario committente sono vuoti, almeno uno dei due deve essere valorizzato."); ok = false; } @@ -969,7 +972,7 @@ bool TDoc_fp::check_initial(TDocumentoEsteso& doc) if (pag.cond_pag_sdi().empty()) { msg.cut(0) << "Non e' valorizzata la condizione di pagamento SDI (TP01, TP02, TP03) per la condizione di pagamento " << pag.code(); - log(1, msg); + _log.log(1, msg); ok = false; } @@ -981,7 +984,7 @@ bool TDoc_fp::check_initial(TDocumentoEsteso& doc) if (cache().get("%CLR", key_class, "S12").empty()) { msg.cut(0) << "Non e' valorizzata la tipologia di pagamento SDI (MPXX) per la condizione di pagamento " << pag.code(); - log(1, msg); + _log.log(1, msg); ok = false; } } @@ -997,14 +1000,14 @@ bool TDoc_fp::check_row(const TRiga_documento& rdoc) if (spesa.is_percentuale() && spesa.perc().is_zero()) { msg.cut(0) << "E' corretto che per la spesa " << spesa.codice() << " la percentuale sia zero?"; - log(1, msg); + _log.log(1, msg); } } const TCodiceIVA& codice_iva = rdoc.iva(); if (codice_iva.codice().full() && codice_iva.percentuale() == ZERO && natura(codice_iva.codice()).empty()) { msg.cut(0) << "Impossibile avere la natura non valorizzata a fronte di una aliquota con percentuale zero. Codice IVA: "; msg << codice_iva.codice(); - log(1, msg); + _log.log(1, msg); ok = false; } return ok; @@ -1016,7 +1019,7 @@ bool TDoc_fp::check_riepilogo(const TDocumentoEsteso& doc, const TRiepilogo_iva& if (*get_esigibilita_iva(doc) == 'S' && natura(riva.cod_iva().codice()) == "N6") { msg.cut(0) << "Impossibile avere un documento con scissione dei pagamenti e natura iva N6, codice: " << riva.cod_iva().codice(); - log(1, msg); + _log.log(1, msg); ok = false; } return ok; @@ -1038,17 +1041,17 @@ bool TDoc_fp::initialize(TDocumentoEsteso& doc) if (!chiave_paf_doc(doc, _hfatt, _bfatt)) return false; // Preparo il log - log(-1, _bfatt); + _log.log(-1, _bfatt); // Controllo se il documento almeno in stato di stampa if (doc.stato() < doc.tipo().stato_finale_stampa()) { - log(3, "Il documento non e' stato ancora stampato, verra' saltato"); + _log.log(3, "Il documento non e' stato ancora stampato, verra' saltato"); return false; } _is_pa = doc.clifor().get_int("ALLEG") == 7; if (!doc.get_coddest(_coddest, _pec)) { - log(1, "Impossibile trovare il codice destinatario per la fattura"); + _log.log(1, "Impossibile trovare il codice destinatario per la fattura"); return false; } _enapec = _coddest == "0000000" && _pec.full(); @@ -1098,47 +1101,17 @@ const TRectype& TDoc_fp::cco(const TRectype& doc) const } return tabmod.curr(); } -void TDoc_fp::log(int severity, const char* msg) -{ - if (_log == nullptr) - { - _log = new TLog_report; - // Tento l'eliminazione del file - std::remove("fp_err.log"); - } - if (severity < 0) - { - _logpaf = msg; - } - else - { - static TString txt; - txt.cut(0); - if (_logpaf.full()) - { - txt << _logpaf << ": " << msg; - } - else - txt << msg; - _log->log(severity, txt); - // Scrivo anche su file - std::filebuf fb; - fb.open("fp_err.log", std::ios::out | std::ios::app); - std::ostream os(&fb); - os << txt << std::endl; - fb.close(); - } -} + bool TDoc_fp::show_log() { - if (_log) - { - _log->preview(); - delete _log; - _log = NULL; - } - return true; + TReport_book b; + TFilename name = "fp_err.log"; + + b.add(_log); + b.export_text(name, false, 132); + return _log.preview(); } + int TDoc_fp::commit() { int r = 0; @@ -1150,17 +1123,17 @@ int TDoc_fp::commit() if (fp_db().sq_set_exec("UPDATE PAF0100F SET P1_GESTIONE = 'P' WHERE P1_GESTIONE = 'D'")) { r += 1; - log(2, "Le fatture sono state esportate correttamente in stato pronto"); + _log.log(2, "Le fatture sono state esportate correttamente in stato pronto"); } else { r = -1; - log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, fp_db().sq_get_token_text_error(1)); } } else { - log(2, "Le fatture sono state esportate in stato diagnosticato"); + _log.log(2, "Le fatture sono state esportate in stato diagnosticato"); } if (r >= 0 && fp_db().sq_commit()) { @@ -1169,8 +1142,8 @@ int TDoc_fp::commit() else { r = -1; - log(2, fp_db().sq_get_token_text_error(1)); - log(2, "UPDATE PAF0100F SET P1_GESTIONE = 'P' WHERE P1_GESTIONE = 'D'"); + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, "UPDATE PAF0100F SET P1_GESTIONE = 'P' WHERE P1_GESTIONE = 'D'"); } } _to_commit = false; @@ -1278,6 +1251,7 @@ void TDoc_fp::set_qta_prezzo(TPaf_record& paf1800f, TFPRiga_documento* rdoc) con paf1800f.set("PI_PREZZOUNIT", converti_prezzo(prezzo_unit)); paf1800f.set("PI_PRZTOTALE", converti_prezzo(prezzo_tot)); } + const real calc_ritenuta(const TDocumento& doc) { real imponibile_prestazioni = ZERO; @@ -1321,8 +1295,7 @@ void TDoc_fp::add_ritenuta(const TRiga_documento & rdoc, TPaf_record& paf0700f) paf0700f.set("P7_CAUSPAGAM", caus_la); // } -bool TDoc_fp::add_riepilogo_iva(TPaf_record& paf2200f, const TCodiceIVA& cod_iva, const char* eiva, const real& imponibile, - const real& imposta) +bool TDoc_fp::add_riepilogo_iva(TPaf_record& paf2200f, const TCodiceIVA& cod_iva, const char* eiva, const real& imponibile, const real& imposta) { reset(paf2200f); @@ -1351,6 +1324,7 @@ bool TDoc_fp::add_riepilogo_iva(TPaf_record& paf2200f, const TCodiceIVA& cod_iva // Inserisco return insert(paf2200f); } + bool TDoc_fp::add_cassa_previdenziale(TRiga_documento& rdoc) { TPaf_record& paf0800f = _paf_container.get_paf("PAF0800F"); @@ -1358,7 +1332,7 @@ bool TDoc_fp::add_cassa_previdenziale(TRiga_documento& rdoc) paf0800f.set("P8_RIFNUMLINEA", _idx_cassa_previdenziale++); // Resto const TSpesa_prest& sp = rdoc.spesa(); - const real imponibile_doc = rdoc.doc().prestazioni();//calc_ritenuta(rdoc.doc()); + const real imponibile_doc = rdoc.doc().prestazioni(); const real importo_cassa = imponibile_doc * sp.perc() / CENTO; paf0800f.set("P8_TIPOCASSA", sp.cassa_previdenziale()); // Aliquota della cassa @@ -1826,7 +1800,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) } } if (_is_pa && cup.blank() && cig.blank()) - log(1, "CIG e CUP assenti"); + _log.log(1, "CIG e CUP assenti"); // TPaf_record& paf1800f = _paf_container.get_paf("PAF1800F"); TPaf_record& paf2000f = _paf_container.get_paf("PAF2000F"); @@ -1932,7 +1906,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) { TString msg; msg.format("La riga merce %d ha quantit nulla", riga); - log(1, msg); + _log.log(1, msg); } set_qta_prezzo(paf1800f, rdoc); if (rdoc->iva().codice().empty()) @@ -2013,7 +1987,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) { TString msg; msg.format("La riga spese a quantit %d ha quantit nulla (campo %s)", riga, static_cast(rdoc->field_qta())); - log(1, msg); + _log.log(1, msg); qta = UNO; } if (qta < ZERO) @@ -2058,7 +2032,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) TString msg; msg << "Il documento " << doc.codice_numerazione().codice() << " " << doc.tipo().codice() << " " << doc.numero() << " presenta una o pi righe di tipo sconto o sconto percentuale.\n" \ "Esportazione impossibile"; - log(2, msg); + _log.log(2, msg); return false; } else if (rdoc->is_omaggio()) @@ -2067,7 +2041,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) { TString msg; msg.format("La riga omaggi %d ha quantit nulla", riga); - log(1, msg); + _log.log(1, msg); } paf1800f.set("PI_TIPOCESSPREST", "AB"); set_qta_prezzo(paf1800f, rdoc); @@ -2356,7 +2330,7 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) { TString msg = "Il vettore "; msg << vet.get("S0").mid(0, 50) << " non ha n codice fiscale n partita IVA, la fattura " << doc.anno() << " " << doc.codice_numerazione().codice() << " " << doc.numero() << " non pu essere trasmessa"; - log(3, msg); + _log.log(3, msg); } if (piva.full()) { @@ -2379,9 +2353,10 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) ok &= insert(paf0700f); // TPaf_record& paf2200f = _paf_container.get_paf("PAF2200F"); - const char* eiva = get_esigibilita_iva(doc); + const char * eiva = get_esigibilita_iva(doc); long num_riep = 0; TAssoc_array& tiva = doc.tabella_iva(false); + for (TObject* itm = tiva.first_item(); itm != nullptr; itm = tiva.succ_item()) { const TRiepilogo_iva& riva = *dynamic_cast(itm); @@ -2426,10 +2401,10 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) paf2500f.set("PO_CAB", cab); } if (tipo_pag == 3 && cab.blank()) // Ricevuta bancaria - log(2, TR("Non sono presenti ABI, CAB, IBAN per il pagamento")); + _log.log(2, TR("Non sono presenti ABI, CAB, IBAN per il pagamento")); if ((tipo_pag == 8 || tipo_pag == 9) && iban.blank()) // R.I.D. o Bonifico { - log(1, TR("Non presente il codice IBAN per il pagamento")); + _log.log(1, TR("Non presente il codice IBAN per il pagamento")); } for (int nr = 0; nr < scad.items(); nr++) { @@ -2455,11 +2430,11 @@ bool TDoc_fp::doc_to_paf(TDocumentoEsteso& doc) { if (!dongle().active(RSAUT)) { - log(1, "Impossibile generare la fattura, il modulo RS non abilitato!"); + _log.log(1, "Impossibile generare la fattura, il modulo RS non abilitato!"); } else if (!doc.tipo().main_print_profile(rep, 2)) { - log(1, "Impossibile generare la fattura, non disponibile un profilo di stampa per questo tipo documento!"); + _log.log(1, "Impossibile generare la fattura, non disponibile un profilo di stampa per questo tipo documento!"); } else { @@ -2614,6 +2589,7 @@ TDoc_fp::TDoc_fp() : _doc_rec(nullptr), _log(nullptr), _cache_insert(false) { _def_usr_fld << "\\"; } + // Mi preparo la stringa del CONAI _conai_str = ini_get_string(CONFIG_DITTA, "ve", "DESCCONAIASS"); if (_conai_str.empty()) @@ -2627,3 +2603,671 @@ TDoc_fp::~TDoc_fp() if (_doc_rec != nullptr) delete _doc_rec; } + +/*************************************************************************** + * TReg_fp + ***************************************************************************/ + +bool TReg_fp::insert(TPaf_record& p) +{ + bool ok; + + if (_cache_insert) + { + _query.push_back(p.insert_string()); + ok = true; + } + else + { + ok = p.insert(); + if (!ok) + { + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, p.insert_string()); + } + } + return ok; +} + +bool TReg_fp::remove(TPaf_record& p) +{ + bool ok; + + if (_cache_insert) + { + _query.push_back(p.remove_string()); + ok = true; + } + else + { + ok = p.remove(); + if (!ok) + { + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, p.remove_string()); + } + } + return ok; +} + +bool TReg_fp::save_paf() +{ + bool ok = true; + + if (_cache_insert) + { + string query; + for (auto i = _query.begin(); i != _query.end(); ++i) + query += *i; + ok = fp_db().sq_set_exec(query); + if (!ok) + { + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, query.c_str()); + } + } + return ok; +} + +bool TReg_fp::check_initial(const TMovimento_contabile& mov) +{ + const TCli_for & clifo = mov.clifo(); + + if (_coddest.len() != 6 && _coddest.len() != 7) + { + _log.log(1, "Il codice destinatario ha una lunghezza non conforme."); + return false; + } + + const int alleg = clifo.get_int(CLI_ALLEG); + bool privato = (alleg == 5 || alleg == 9) && clifo.get(CLI_STATOCF).full(); + + if (clifo.get(CLI_PAIV).empty() && clifo.get(CLI_COFI).empty() && !privato) + { + _log.log(1, "Sia la partita IVA che il codice fiscale del cessionario committente sono vuoti, almeno uno dei due deve essere valorizzato."); + return false; + } + + TPartite_array par; + + par.add_numreg(mov.get_long(MOV_NUMREG)); + for (TPartita* p = par.first() ; p!= nullptr ; p=par.next()) + { + int riga_p = p->prima_fattura(); + + if (riga_p >= 0) + { + const TRiga_partite& rp=p->riga(riga_p); + + for (int r = 0; r < rp.rate(); r++) + { + const TRiga_scadenze & rata = rp.rata(r); + TString key_class; + + key_class << rata.get(SCAD_TIPOPAG) << rata.get(SCAD_ULTCLASS); + + if (cache().get("%CLR", key_class, "S12").empty()) + { + TString msg; + msg.cut(0) << "Non e' valorizzata la tipologia di pagamento SDI (MPXX) per la condizione di pagamento " << mov.get(MOV_CODPAG); + _log.log(1, msg); + return false; + } + + } + } + } + return true; +} + +bool TReg_fp::check_row(const TMovimento_contabile& mov, int n_riga) +{ + TRectype & riga_iva = ((TMovimento_contabile &)mov).iva(n_riga); + const TCodiceIVA& codice_iva = cached_codIVA(riga_iva.get(RMI_CODIVA)); + + if (codice_iva.codice().full() && codice_iva.percentuale() == ZERO && natura(codice_iva.codice()).empty()) + { + TString msg; + + msg << "Impossibile avere la natura non valorizzata a fronte di una aliquota con percentuale zero. Codice IVA: "; msg << codice_iva.codice(); + _log.log(1, msg); + return false; + } + + + return true; +} + + +bool TReg_fp::initialize(const TMovimento_contabile& mov) +{ + // Azzero + _hfatt.cut(0); + _bfatt.cut(0); + + // Paese del documento + _paese = "IT"; + if (!chiave_paf_mov(mov, _hfatt, _bfatt)) + return false; + // Preparo il log + _log.log(-1, _bfatt); + // Controllo se il documento almeno in stato di stampa + _is_pa = mov.clifo().get_int("ALLEG") == 7; + if (!get_coddest(mov.get_char(MOV_TIPO), mov.get_long(MOV_CODCF), _coddest, _pec)) + { + _log.log(1, "Impossibile trovare il codice destinatario per la fattura"); + return false; + } + _enapec = _coddest == "0000000" && _pec.full(); + _privato = _coddest.len() != 6; + // Azzero indici + _idx_cassa_previdenziale = 1; + // Controllo custom +// _has_cust = cached_custom_fp().has_custom(doc); //DA VERIFICARE + if (check_initial(mov) || get_check_not_block()) + return _paf_container.clean_and_erase_paf(_hfatt, _bfatt); + return false; +} + +int TReg_fp::commit() +{ + int r = 0; + if (_to_commit) + { + // Controllo stato diagnosticato + if (!get_no_export_pronto()) + { + if (fp_db().sq_set_exec("UPDATE PAF0100F SET P1_GESTIONE = 'P' WHERE P1_GESTIONE = 'D'")) + { + r += 1; + _log.log(2, "Le fatture sono state esportate correttamente in stato pronto"); + } + else + { + r = -1; + _log.log(2, fp_db().sq_get_token_text_error(1)); + } + } + else + { + _log.log(2, "Le fatture sono state esportate in stato diagnosticato"); + } + if (r >= 0 && fp_db().sq_commit()) + { + r += 2; + } + else + { + r = -1; + _log.log(2, fp_db().sq_get_token_text_error(1)); + _log.log(2, "UPDATE PAF0100F SET P1_GESTIONE = 'P' WHERE P1_GESTIONE = 'D'"); + } + } + _to_commit = false; + return r; +} + +int TReg_fp::force_commit() +{ + _to_commit = true; + return commit(); +} + +const TString & TReg_fp::natura(const char* codiva) const +{ + const TCodiceIVA iva(codiva); + + return get_tmp_string(4) = iva.natura(); +} + +const char* TReg_fp::get_esigibilita_iva(const TMovimento_contabile& mov) +{ + // Esigibilit IVA: Immediata, Differita, Split payment + const char* eiva = "I"; + if (mov.is_split_payment()) + eiva = "S"; + else if (mov.get_bool(MOV_LIQDIFF) || mov.get_bool(MOV_IVAXCASSA)) + eiva = "D"; + return eiva; +} + +void TReg_fp::set_IVA(TString codiva, TPaf_record& paf) const +{ + if (codiva.empty()) + codiva = _codivadefault; + // necessario il cast a real? + paf.set("PI_ALIQUOTAIVA", static_cast(cache().get("%IVA", codiva, "R0"))); + paf.set("PI_NATURA", natura(codiva)); +} + +//???SERVE?????? +bool TReg_fp::add_row_art(long& riga_art, const TString& codice_tipo, const TString& codice_valore, TPaf_record& paf) +{ + paf.set("PY_KEYNLINAR", ++riga_art); + paf.set("PY_TIPOARTICOLO", codice_tipo); + paf.set("PY_VALOREARTICOLO", codice_valore); + return insert(paf); +} + +bool TReg_fp::add_row_alleg(TFilename& file, long& nprogr, TPaf_record& paf) +{ + static TString dest_path; + static TString dest_usr_path; + bool ok = false; + dest_path.cut(0) << _def_fld << file.name(); + dest_usr_path.cut(0) << _def_usr_fld << file.name(); + if (!fcopy(file, dest_usr_path)) + { + return yesno_box("Errore critico nel copiare il file %s, si desidera continuare?", file.name()); + } + + // Provo a copiare il file + paf.set("PP_NUMEROLINEA", ++nprogr); + paf.set("PP_NOMEATTACHMENT", file.name()); + paf.set("PP_ATTACHMENT", dest_path); + file.upper(); // serve estensione maiuscola + paf.set("PP_FMTATTACHMENT", file.ext()); + ok = insert(paf); + + return ok; +} + +bool TReg_fp::add_riepilogo_iva(const TMovimento_contabile mov, int n_riga) +{ + TPaf_record& paf2200f = _paf_container.get_paf("PAF2200F"); + const TRectype& rec_iva = mov.iva()[n_riga]; + const TString16 cod_aliquota = rec_iva.get(RMI_CODIVA); + const TCodiceIVA& cod_iva= cached_codIVA(cod_aliquota); + const real aliquota = cod_iva.percentuale(); + + reset(paf2200f); + // Aliquota + paf2200f.set("PL_ALIQUOTAIVA", aliquota); + // Natura + if (aliquota.is_zero()) + paf2200f.set("PL_NATURA", natura(cod_aliquota)); + // Imponibile + paf2200f.set("PL_IMPONIBILE", rec_iva.get(RMI_IMPONIBILE)); + // Imposta + paf2200f.set("PL_IMPOSTA", rec_iva.get(RMI_IMPOSTA)); + // Esigibilit IVA + paf2200f.set("PL_ESIGIVA", ""); // TODO va compilato ??? + + paf2200f.set("PL_RIFNORMATIVO", cod_iva.descrizione()); + paf2200f.set("PL_GESTIONE", "D"); + + // Inserisco + return insert(paf2200f); +} + +bool TReg_fp::export_paf0100f() +{ + // + TPaf_record& paf0100f = _paf_container.get_paf("PAF0100F"); + paf0100f.set("P1_TRASMITTPAESE", _paese); + paf0100f.set("P1_TRASMITTCOD", _cofi); + paf0100f.set("P1_FMTTRASMISS", _privato ? "FPR12" : "FPA12"); + paf0100f.set("P1_CODDEST", _coddest); + + TString80 tel; + + tel << get_firm().get(NDT_PTEL) << get_firm().get(NDT_TEL); + paf0100f.set("P1_TELEFONO", tel); + paf0100f.set("P1_MAIL", get_firm().get(NDT_MAIL)); + paf0100f.set("P1_GESTIONE", "D"); + paf0100f.set("P1_ERRINT", ""); + // + return insert(paf0100f); +} + +bool TReg_fp::export_paf3200f() +{ + if (_enapec) + { + // + TPaf_record& paf3200f = _paf_container.get_paf("PAF3200F"); + paf3200f.set("PU_PEC", _pec); + // + return insert(paf3200f); + } + return true; +} + +bool TReg_fp::reg_to_paf(const TMovimento_contabile& mov) +{ + if (!initialize(mov)) + return false; + bool ok = true; + + ok &= export_paf0100f(); + ok &= export_paf3200f(); + // + TPaf_record& paf0200f = _paf_container.get_paf("PAF0200F"); + TAnagrafica cliente(mov.clifo()); + if (!paf0200f.is_full()) + { + if (_ditta.partita_IVA().full()) + { + paf0200f.set("P2_FISCIVAPAESE", _ditta.stato_partita_IVA()); + paf0200f.set("P2_FISCIVACOD", _ditta.partita_IVA()); + } + paf0200f.set("P2_CODFISCALE", _ditta.codice_fiscale()); + if (_ditta.fisica()) + { + paf0200f.set("P2_ANANOME", _ditta.nome()); + paf0200f.set("P2_ANACOGNOME", _ditta.cognome()); + } + else + { + paf0200f.set("P2_ANADENOMIN", _ditta.ragione_sociale()); + } + paf0200f.set("P2_REGFISCALE", ""); //TODO DA RF01 A RF09 + // DatiSede + paf0200f.set("P2_SEDEIND", _ditta.via_residenza()); + paf0200f.set("P2_SEDENRCIVICO", _ditta.civico_residenza().left(8)); + paf0200f.set("P2_SEDECAP", _ditta.CAP_residenza()); + paf0200f.set("P2_SEDECOMUNE", _ditta.comune_residenza()); + paf0200f.set("P2_SEDEPROV", _ditta.provincia_residenza()); + paf0200f.set("P2_SEDENAZ", _paese); + paf0200f.set("P2_GESTIONE", "D"); + + paf0200f.set("P2_RIFAMMINISTR", mov.clifo().vendite().get(CFV_PARIFAMM)); + paf0200f.set("P2_ISCRREASOCIOU", _ditta.sociounico() == 'S' ? "SU" : "SM"); + TISAM_recordset unloc("USE UNLOC\nJOIN COMUNI INTO COM==COMCCIAA\nFROM CODDITTA=#DITTA\nTO CODDITTA=#DITTA"); + unloc.set_var("#DITTA", get_firm().get(NDT_CODDITTA)); + if (unloc.move_first()) + { + const TString& numrea = unloc.get(ULC_NUMCCIAA).as_string(); + if (numrea.full()) + { + paf0200f.set("P2_ISCRREANUM", numrea); + paf0200f.set("P2_ISCRREAUFF", unloc.get("13->" COM_PROVCOM)); + } + } + if (_ditta.giuridica()) + { + TISAM_recordset anagiu("USE ANAGIU\nFROM CODANAGR=#CODICE\nTO CODANAGR=#CODICE"); + + anagiu.set_var("#CODICE", get_firm().get(NDT_CODANAGR)); + if (anagiu.move_first()) + { + paf0200f.set("P2_ISCRREACAP", anagiu.get(ANG_CAPSOC)); + const int ss = anagiu.get(ANG_STATOSOC).as_int(); + paf0200f.set("P2_ISCRREASLIQUID", (ss == 2 || ss == 3) ? "LS" : "LN"); + } + } + else + paf0200f.set("P2_ISCRREASLIQUID", "LN"); + } + ok &= insert(paf0200f); + // + // + TPaf_record& paf0400f = _paf_container.get_paf("PAF0400F"); + + TString stato = cliente.stato_partita_IVA(); + TTable tab_codiso("%SCE"); + tab_codiso.tab(); + tab_codiso.put("CODTAB", stato); tab_codiso.read(); + TString piva = cliente.partita_IVA(), + fisc = cliente.codice_fiscale(); + if (!stato.full()) + stato = "IT"; + if (tab_codiso.get_bool("B0") || stato == "IT") // Cliente EU + { + if (piva.full()) + { + if (piva.len() == 11 && (piva[0] == '8' || piva[0] == '9')) + { + fisc = piva; + piva.cut(0); + } + } + else if (fisc.full() && fisc.len() == 11 && (fisc[0] == '8' || fisc[0] == '9')) + piva.cut(0); + + if (piva.full()) + { + paf0400f.set("P4_FISCIVAPAESE", stato); + paf0400f.set("P4_FISCIVACOD", piva); + } + if (fisc.full()) + paf0400f.set("P4_CODFISC", fisc); + } + else // Cliente EXTRA-EU sempre nel campo della partita iva sui paf + { + paf0400f.set("P4_FISCIVAPAESE", stato); + if (fisc.full()) // Guardo prima l'id fiscale, perche' e' qui che dovrebbero stare in Campo + paf0400f.set("P4_FISCIVACOD", fisc); + else if (piva.full()) // Altrimenti dovrebbe essere nella partita iva + paf0400f.set("P4_FISCIVACOD", piva); + } + + if (cliente.fisica() && cliente.nome().full()) + { + paf0400f.set("P4_ANANOME", cliente.nome()); + paf0400f.set("P4_ANACOGNOME", cliente.cognome()); + } + else + { + paf0400f.set("P4_ANADENOM", cliente.ragione_sociale()); + } + // DatiSede + paf0400f.set("P4_SEDEIND", cliente.via_residenza()); + paf0400f.set("P4_SEDENRCIVICO", cliente.civico_residenza().left(8)); + paf0400f.set("P4_SEDECOMUNE", cliente.comune_residenza()); + paf0400f.set("P4_SEDENAZ", cliente.stato_residenza_ISO()); + // I clienti esteri possono avere CAP alfanumerici, li tolgo + if (cliente.stato_residenza_ISO() != "IT") + { + paf0400f.set("P4_SEDECAP", "00000"); + } + else + { + paf0400f.set("P4_SEDECAP", cliente.CAP_residenza()); + paf0400f.set("P4_SEDEPROV", cliente.provincia_residenza()); + } + // Titolo onorifico! + const TString& titolo = (mov.clifo().vendite().get(CFV_TITOLO)); + + if (titolo.full()) + paf0400f.set("P4_ANATITOLO", cache().get("TIT", titolo, "S0")); + paf0400f.set("P4_GESTIONE", "D"); + ok &= insert(paf0400f); + // + // + TPaf_record& paf0700f = _paf_container.get_paf("PAF0700F"); + paf0700f.set("P7_TIPODOC", tipo_doc_sdi(mov)); + paf0700f.set("P7_DIVISA", "EUR"); // Fisso su euro in quanto effettuiamo il cambio + paf0700f.set("P7_DATA", mov.get_date(MOV_DATADOC)); + paf0700f.set("P7_NUMERO", ""); //da chiedere ad ilaria + paf0700f.set("P7_GESTIONE", "D"); + + TPaf_record& paf2700f = _paf_container.get_paf("PAF2700F"); + // Disabilitata la scrittura del totale del documento, questo causa problemi se presente uno sconto in testata e l'addebito del bollo. + // Campo calcola prima il totale, poi lo sconta e ci applica il bollo mentre lo SDI sconta a bollo gi applicato. + paf2700f.set("PQ_IMPTOTDOC", mov.get(MOV_TOTDOC)); + paf2700f.set("PQ_GESTIONE", "D"); + ok &= insert(paf2700f); + // + // + // Azzera DDT + TPaf_record& paf1600f = _paf_container.get_paf("PAF1600F"); + // SEMPRE +// + TPaf_record& paf1800f = _paf_container.get_paf("PAF1800F"); + TPaf_record& paf2000f = _paf_container.get_paf("PAF2000F"); + TPaf_record& paf2100f = _paf_container.get_paf("PAF2100F"); + TPaf_record& paf1900f = _paf_container.get_paf("PAF1900F"); + TPaf_record& paf3000f = _paf_container.get_paf("PAF3000F"); + int n_righe_iva = mov.iva().rows(); + + for (int i = 0; i < n_righe_iva; i++) + { + reset(paf1800f); + paf1800f.set("PI_NUMEROLINEA", (long)i); + + reset(paf3000f); + paf3000f.set("PT_RIFNUMLINEA", (long)i); + paf3000f.set("PT_COMMENTO", "");//TODO COSA DOBBIAMO METTERE NELLA DESCRIZIONE IVA CHE NON C'è + // + + reset(paf2100f); + paf2100f.set("PK_KEYNLINEA", (long)i); //TODO va bene??? + paf2100f.set("PK_KEYNLINAR", (long)i);// qui???? CHE DIFF C'è TRA KEYLINEA E KEYLINEAR + + + add_riepilogo_iva(mov, i); + + + /* Se ho una fattura che deriva dalla fatturazione differita delle lavanderie prendo + * dalla relativa riga descrizione tutti i riferimenti alle bolle che mancano */ + + ok &= insert(paf1800f); + if (ok) ok &= insert(paf3000f); + + if (ok) ok &= insert(paf0700f); + + // + // + /* + if (_gestioneallegati) + { + TPaf_record& paf2600f = _paf_container.get_paf("PAF2600F"); + long nprogr = 0; // Numero di file allegati + // Se abilitato stampo il documento e lo allego + TFilename rep; + + if (_allegafattura) + { + if (!dongle().active(RSAUT)) + { + _log.log(1, "Impossibile generare la fattura, il modulo RS non abilitato!"); + } + else if (!doc.tipo().main_print_profile(rep, 2)) + { + _log.log(1, "Impossibile generare la fattura, non disponibile un profilo di stampa per questo tipo documento!"); + } + else + { + //ve1 -2 {CODNUM} {ANNO} {PROVV} {NDOC}(-{ANDOC}) {TIPO_ELABORAZIONE} {TIPO_STAMPA} {NUM_COPIE} {ARCHIVIAZIONE} + // Costruisco la chiamata + static TString commandline; + commandline.cut(0) << "ve1 -2 " << doc.get(DOC_CODNUM) << ' ' << doc.get(DOC_ANNO) + << ' ' << doc.get(DOC_PROVV) << ' ' << doc.get(DOC_NDOC) << " X P 1 D"; // X: stampa su disco, P: provvisorio, 1: 1 copia, D: disabilita archiviazione + TExternal_app interattivo(commandline); + if (interattivo.run() != NOERR) + { + TString msgerr = "Fallita generazione PDF documento "; + msgerr << doc.get(DOC_CODNUM) << ' ' << doc.get(DOC_ANNO) + << ' ' << doc.get(DOC_PROVV) << ' ' << doc.get(DOC_NDOC); + error_box(msgerr); + } + else + { + TFilename pdf; pdf.tempdir(); + pdf << SLASH << doc.get(DOC_ANNO) << '_' << doc.get(DOC_CODNUM) << '_' << doc.get(DOC_NDOC) << ".pdf"; + if (!pdf.exist() && !yesno_box("Attenzione! Non stato possibile creare il pdf, continuare?")) + { + return false; + } + if (!add_row_alleg(pdf, nprogr, paf2600f)) + return false; + } + } + TToken_string allegati(doc.get("COLL_GOLEM"), '\n'); + bool load_allegati = true; + if (allegati.full()) + { + if (_def_fld.empty()) + { + TString msgerr; msgerr << "Errore: il documento " << _bfatt << " ha degli allegati ma nella configurazione non stato impostato come trametterli\nCaricare il documento senza allegati?"; + load_allegati = false; + if (!yesno_box(msgerr)) + return false; + } + else + { + TFilename fname; + FOR_EACH_TOKEN(allegati, row) + { + const TToken_string entry(row); + if (entry.get(0, fname) && fname.exist()) + { + if (!add_row_alleg(fname, nprogr, paf2600f)) + return false; + } + } + } + } + } + } + */ + + // Tabella di non invio XML + TPaf_record& pafw300f = _paf_container.get_paf("PAFW300F"); + pafw300f.set("PW_TIPODOC", ""); //TODO PASSARE IL TIPO DOCUMENTO DELLA MASK NEL COSTRUTTORE + pafw300f.set("PW_TIPONUM", ""); //TODO COSA CI METTIAMO NEL TIPO NUM + pafw300f.set("PW_NUMERO", mov.get(MOV_NUMDOC)); + pafw300f.set("PW_DATA", mov.get_date(MOV_DATADOC)); + /* + if (!cached_tipodoc(doc.get(DOC_TIPODOC)).invio_xml() || doc.noinvioxml()) //TODO COSA CI VA MESSO?? COSA VUOL DIRE INVIO XML + pafw300f.set("PW_CODSDI", "**********"); + */ + pafw300f.set("PW_CDEST", _coddest); + pafw300f.set("PW_RAGSOC", cliente.ragione_sociale().left(35)); + pafw300f.set("PW_PAESE", cliente.stato_residenza_ISO()); + pafw300f.set("PW_CODICE", cliente.codice_fiscale_estero()); + pafw300f.set("PW_CFISCA", cliente.codice_fiscale()); + pafw300f.set("PW_DENOM", cliente.ragione_sociale()); + if (cliente.fisica() && cliente.nome().full()) + { + pafw300f.set("PW_NOME", cliente.nome()); + pafw300f.set("PW_COGN", cliente.cognome()); + } + else + pafw300f.set("PW_RAGSOC", cliente.ragione_sociale().left(35)); + pafw300f.set("PW_IMPO", mov.get_real(MOV_TOTDOC)); //TODO controllare ????? + ok &= insert(pafw300f); + return _to_commit = (ok && save_paf()); + } + return ok; +} + +bool TReg_fp::reg_to_paf(long n_mov) +{ + const TMovimento_contabile mov(n_mov); + + return reg_to_paf(mov); +} + +//NON LE CAPISCO + +TReg_fp::TReg_fp() : _cache_insert(false) +{ + _ditta.init(LF_NDITTE, prefix().get_codditta()); + _cofi = get_cofi_tras(); + const TDate data_inizio = get_date_start_new_fatt(); + if (_cofi.blank()) + _cofi = _ditta.codice_fiscale(); + _gestioneallegati = get_gest_alleg(); + _allegafattura = get_allega_fat(); + _def_fld = get_fld_dest(); + if (!_def_fld.ends_with("\\")) + { + _def_fld << "\\"; + } + _def_usr_fld = get_fld_dest_usr(); + if (_def_usr_fld.empty()) + { + _def_usr_fld = _def_fld; + } + else if (!_def_usr_fld.ends_with("\\")) + { + _def_usr_fld << "\\"; + } +} + +TReg_fp::~TReg_fp() +{ + commit(); +}