#include "tf0500a.h" #include #include #include #include #include // cache() #include // TCSV_recordset #include // goto_url() #include // get_iva_sirio() #define TIPO_FILE "DF" #define TFBASE "ModuliSirio" #define CODICE_FORNITURA "IVP" class TIva_send_msk : public TAutomask { bool _mese; virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly); public: bool check_variables(); bool is_mese() const { return _mese; } TIva_send_msk() : TAutomask("tf0500a"){} }; // Definizione per chiamare metodo send dalla maschera class TIva_send_app : public TSkeleton_application { void generate_n_send(TIva_send_msk& msk) { generate(msk) && send(); } bool generate(TIva_send_msk& msk); TToken_string get_testata(TIva_send_msk& msk); public: bool send(); virtual void main_loop(); }; TIva_send_app& app() { return (TIva_send_app&)main_app(); } const TRectype get_anag_ditta() { TString codanagr = cache().get(LF_NDITTE, prefix().firm().codice(), "TIPOA"); codanagr << "|" << cache().get(LF_NDITTE, prefix().firm().codice(), "CODANAGR"); return cache().get(LF_ANAG, codanagr); } // Ritorna il record richiesto di tabcom.LIM const TRectype get_lim(int anno, int mese) { TString key; key << anno; if(mese < 10) key << "0" << mese; else key << mese; return cache().get("LIM", key); } // Ritorna il record richiesto di ivaliq const TRectype get_liq(int anno, int mese) { TString key; key << anno << "|"; if(mese < 10) key << "0"; key << mese << "|U"; return cache().get(LF_IVALIQ, key); } // Ritorna il record richiesto di tabcom.LIA const TRectype get_lia(int anno) { TString key; key << format("%05d", prefix().firm().codice()) << anno; // %05d Crea un numero di 5 cifre partendo dal codice e mettendoci 0 davanti return cache().get("%LIA", key); } TString format_currency(const TString& value) { const real app(value); if(app == ZERO) return ",00"; else return TCurrency(app).string(); } bool TIva_send_msk::on_field_event(TOperable_field& o, TField_event e, long jolly) { switch(o.dlg()) { case F_PATH: if(e == fe_init) { set(F_PATH, ini_get_string(CONFIG_STUDIO, "tf", "path_invio")); } case F_ANNO: if(e == fe_modify) { TString cod = get(F_CODDITTA); cod << get_int(F_ANNO); if(cache().get("%LIA", cod).empty()) { warning_box("Attenzione anno di liquidazione non presente"); set(F_ANNO, ""); break; } if(cache().get("%LIA", cod, "S7") == "M") { _mese = true; enable(F_DAMESE); enable(F_AMESE); disable(F_DATRIMESTRE); disable(F_ATRIMESTRE); } else { _mese = false; disable(F_DAMESE); disable(F_AMESE); enable(F_DATRIMESTRE); enable(F_ATRIMESTRE); } } else if(e == fe_init) { // Per trovare un valore tento per 5 anni di vedere se c'è una liquidazione aperta, così da valorizzare tutta la roba int year = TDate(TODAY).year(); for(int i = year; i >= year - 5; i--) { TString cod = get(F_CODDITTA); cod << i; if(cache().get("%LIA", cod).empty()) continue; if(cache().get("%LIA", cod, "S7") == "M") { set(F_ANNO, i); _mese = true; enable(F_DAMESE); enable(F_AMESE); disable(F_DATRIMESTRE); disable(F_ATRIMESTRE); break; } else { set(F_ANNO, i); _mese = false; disable(F_DAMESE); disable(F_AMESE); enable(F_DATRIMESTRE); enable(F_ATRIMESTRE); break; } } } break; case F_DAMESE: if(e == fe_modify) set(F_AMESE, get_int(F_DAMESE)+2); break; case F_DATRIMESTRE: if(e == fe_modify) set(F_ATRIMESTRE, get_int(F_DATRIMESTRE)); break; case DLG_EMAIL: if(e == fe_button) app().send(); break; } return true; } bool TIva_send_msk::check_variables() { if(get(F_ANNO).empty()) { error_box("Inserire un anno di liquidazione"); return false; } if((_mese && get(F_DAMESE).empty() && get(F_AMESE).empty()) || (!_mese && get(F_DATRIMESTRE).empty() && get(F_ATRIMESTRE).empty())) { error_box("Inserisci un periodo"); return false; } if((_mese && (get_int(F_DAMESE) > get_int(F_AMESE))) || (!_mese &&(get_int(F_DATRIMESTRE) > get_int(F_ATRIMESTRE)))) { error_box("Date immesse non corrette"); return false; } return true; } bool TIva_send_app::generate(TIva_send_msk& msk) { if(!msk.check_variables()) return false; ofstream file; TFilename path(msk.get(F_PATH)); TRectype row_lia = get_lia(msk.get_int(F_ANNO)); // Decido di utilizzare I10 come numero progressivo di trasmissione int prog = row_lia.get_int("I14"); if(prog == 0) { prog = get_lia(msk.get_int(F_ANNO) - 1).get_int("I14"); } prog++; TString nome_file; nome_file << "IT" << get_anag_ditta().get("PAIV") << TIPO_FILE << prog << ".csv"; path.add(nome_file); file.open(path, ios::out); if(!file.is_open()) error_box(TR("Impossibile aprire il file alla posizione %s"), path); else ini_set_string(CONFIG_STUDIO, "tf", "path_invio", msk.get(F_PATH)); const TToken_string testata(get_testata(msk)); int start = msk.is_mese() ? msk.get_int(F_DAMESE) : (((msk.get_int(F_DATRIMESTRE)) * 3)); const int end = msk.is_mese() ? msk.get_int(F_AMESE) : (((msk.get_int(F_ATRIMESTRE)) * 3)); for(; start <= end; start++) { TRectype row_lim = get_lim(msk.get_int(F_ANNO), start); // row_lim viene già usata sopra TRectype row_liq = get_liq(msk.get_int(F_ANNO), start); TToken_string riga(testata); if(msk.is_mese()) { riga.add(start); // Mese riga.add(""); // Trimestre } else { riga.add(""); // Mese riga.add(row_liq.get_int("TRIMESTRE") == 4 ? "5" : row_liq.get("TRIMESTRE")); // Trimestre } const char subfor = row_liq.get_bool("SUBFOR") ? 'X' : '\0'; riga.add(subfor); // Flag Subfornitura riga.add(""); // Eventi eccezionali riga.add(format_currency(row_liq.get("TOTOPATT"))); // Totale operazioni attive (al netto dell'IVA) riga.add(format_currency(row_liq.get("TOTOPPAS"))); // Totale operazioni passive (al netto dell'IVA) riga.add(format_currency(row_liq.get("IVAES"))); // IVA esigibile riga.add(format_currency(row_liq.get("IVADET"))); // IVA detratta riga.add(format_currency(row_liq.get("IVADOV"))); // IVA dovuta riga.add(format_currency(row_liq.get("IVADOVC"))); // IVA a credito VP6 col 2 riga.add(format_currency(row_liq.get("DEBPREC"))); // Debito periodo precedente riga.add(format_currency(row_liq.get("CREPREC"))); // Credito periodo precedente riga.add(format_currency(row_liq.get("CREAPREC"))); // Credito anno precedente riga.add(format_currency(row_liq.get("VEAUE"))); // Versamenti auto UE riga.add(format_currency(row_liq.get("CREIMP"))); // Crediti di imposta riga.add(format_currency(row_liq.get("INTLIQTRI"))); // Interessi dovuti per liquidazioni trimestrali riga.add(format_currency(row_liq.get("ACCDOV"))); // Acconto dovuto riga.add(format_currency(row_liq.get("IVAVER"))); // IVA da versare riga.add(format_currency(row_liq.get("IVAVERC"))); // IVA a credito VP14 col 2 riga.add(prog); // Numero interno trasmissione riga.add(""); // Flag trasmesso riga.add(""); // Flag accettato // Appendo al file file << riga << "\n"; } file.close(); // Aggiorno l'univoco row_lia.put("I14", prog); if(row_lia.rewrite(TLocalisamfile(LF_TABCOM)) != NOERR) { error_box("Errore aggiornamento progressivo"); return false; } return true; } TToken_string TIva_send_app::get_testata(TIva_send_msk& msk) { TToken_string testata; // Token string di virgole TString cod_forn = CODICE_FORNITURA; cod_forn << msk.get(F_ANNO).right(2); testata.add(cod_forn); // Codice Fornitura testata.add(""); // Cod.Fisc. Sost. Dich. testata.add(""); // Cod. Carica Sost. Dich. testata.add(msk.get_int(F_ANNO)); // Anno comunicazione testata.add(""); // Mese comunicazione TRectype ana_ditta = get_anag_ditta(); testata.add(ana_ditta.get("COFI")); // Codice fiscale testata.add(ana_ditta.get("PAIV")); // Partita IVA testata.add(""); // P.IVA Controllante testata.add(""); // Flag Liq. gruppo testata.add(""); // Flag firma comunicazione const int codice_carica = atoi(prefix().firm().get("CARRAPP")); if(codice_carica > 0) { TString key = "F|"; key << prefix().firm().get("RAPPR"); const TString cod_fisc = cache().get(LF_ANAG, key, "COFI"); testata.add(cod_fisc); // Cod.Fisc. Dichiarante testata.add(codice_carica); // Cod.Carica Dichiarante } else { testata.add(""); // Cod.Fisc. Dichiarante testata.add(""); // Cod.Carica Dichiarante } testata.add(""); // Cod.Fisc. Soc. Dichiarante testata.add(""); // Cod.Fisc. Intermediario testata.add(""); // Flag Impegno testata.add(""); // Data Impegno testata.add(""); // Flag Firma Intermediario testata.add(""); // Flag Conferma testata.add(get_iva_sirio()); // Ricorda! Il programma di Leo vuole true = 'S', false = '' ! return testata; } bool TIva_send_app::send() { TFilename tmp = TFBASE"\\ModuliSirio.jar"; tmp.make_absolute_path(); DIRECTORY old_dir; xvt_fsys_get_dir(&old_dir); DIRECTORY new_dir; xvt_fsys_convert_str_to_dir(tmp.path(), &new_dir); xvt_fsys_set_dir(&new_dir); const bool good = goto_url(tmp); if (good) { xvt_sys_sleep(3000); } else { error_box(FR("Impossibile eseguire Java -jar %s"), static_cast(tmp)); } xvt_fsys_set_dir(&old_dir); return true; } void TIva_send_app::main_loop() { TIva_send_msk msk; while(msk.run() == K_ENTER) { generate_n_send(msk); } } int tf0500(int argc, char* argv[]) { TIva_send_app app; app.run(argc, argv, TR("Invio dati liquidazione IVA")); return 0; }