#include "defmask.h" #include "automask.h" #include "applicat.h" #include "tsdb.h" #include "fp0400a.h" #include "fplib.h" #include "execp.h" #include "dongle.h" #include #include "../f1/f1lib.h" #include "mov.h" #include "comuni.h" #define FILTER_NOASS "" #define FILTER_ASS "E" #define FILTER_ALL "A" #define FILTER_CONT "C" #define PARA_FP "fp" enum { no_cf = -14, // Non trovato con cod. fisc. (se non ho nemmeno la p.iva) no_match_cf = -15, // Trovato forn. ma il cod. fisc non corrisponde no_forn = -5, // Non trovato forn. con p.iva err_match_db = -3, // Salvato in db ma non corrisponde a un fornitore saved_db = 0, // Salvato già in db FPPRO found_piva = 2, // Lo trovo con p.iva found_cf = 4, // Non ho p.iva lo trovo con cod.fisc }; enum { filtri = 0, elenco_fe = 1, elenco_err = 2 }; // Enum per bottoni toolbar (numero pagina in cui mi trovo) ///////////////////////////////////////////////////////////////////////////////////// // TPassive_mask ///////////////////////////////////////////////////////////////////////////////////// class TPassive_mask : public TAutomask { TLog_report* _log; bool _f1; protected: bool _filter_changed; TFilename _tmp_dir; struct TForn_err { int forn_code; int fatt_err; }; std::map _list_fatt_err; void set_filter_changed(); void select_all(int sheet_field); // Tasto nuovo: apre nuovo fornitore valorizzando già i dati dalla fattura void new_forn(); // Tasto aggiorna: aggiorna cliente che non corrisponde. bool aggiorna_forn(); void salva_for() const; void clean_ini(const TString& filename) const; void run_cg0(const TString& filename) const; void elenco_prots_sel(TString& string) const; // Gestione F1 int prepara_contab() const; void log_contab(); void contabilizza(); TDate load_data() const; // Handlers static bool causfa_handler(TMask_field& f, KEY k); void fattsel(int dlg, TField_event& e); bool on_field_event(TOperable_field& o, TField_event e, long jolly) override; void check_buttons(int p); void next_page(int p) override; // Elenco dei protocolli selezionati void fill(); static int find_fornitore(TLocalisamfile& clifo); static void add_row_err_forn(int forn_code, TSheet_field& sf_err, TString& denom); void aggiungi_riga(TLocalisamfile& clifo, TSheet_field& sf, TSheet_field& sf_err); // Aggiunge i protocolli che sono già presenti per le fatture nel db alla tabellina per il filtro void aggiungi_prot() const; void init(); public: TString& get_fattsel() const; TPassive_mask(bool f1); }; void TPassive_mask::set_filter_changed() { _filter_changed = true; } void TPassive_mask::next_page(int p) { check_buttons(p); if (_filter_changed && p != 1000) { fill(); _filter_changed = false; } TAutomask::next_page(p); } void TPassive_mask::fill() { TSheet_field& sf = sfield(F_DOCS); TSheet_field& sf_err = sfield(F_ERR); TString query; TString prots_query; TLocalisamfile clifo(LF_CLIFO); TDate dal; TDate al; const TString& filter_elab = get_fattsel(); const TDate reg_dal(get(F_DATAINI)); const TDate reg_al(get(F_DATAEND)); const TString& ordin = get(F_FATTORD); const TString& verso_ord = get(F_VERSOORD); if(get_fattsel() == FILTER_CONT) { dal = "20010101"; al = TDate(TODAY); } else { dal = get(F_DATAINI); al = get(F_DATAEND); } sf.hide(); sf.destroy(); sf_err.hide(); sf_err.destroy(); elenco_prots_sel(prots_query); query << "SELECT PZ_TIPOPROT AS TIPO_PROT, YEAR(P7_DATA) AS ANNO, P7_TIPODOC AS [TIPO_SDI], P7_NUMERO AS [NUM_DOC], " << "P7_DATA AS [DATA_DOC], PZ_DATAORARIC AS [DATA_RIC], PQ_IMPTOTDOC AS [TOT_DOC], PZ_NUMREGCONT AS NUMREGCONT, PZ_DATAREGCONT AS DATAREGCONT, P2_ANADENOMIN AS [RAG_SOC], P2_ANANOME AS NOME, " << "P2_ANACOGNOME AS COGNOME, P2_FISCIVACOD AS [P_IVA], P2_CODFISCALE AS [COD_FISC], P2_FISCIVAPAESE AS [COD_PAESE],\n" << "COUNT(PP_ATTACHMENT) AS ATTACHMENT, P1_CODDEST AS [COD_SDI], PU_PEC AS PEC, P1_KEYPRGINVIO AS KEYPROG, P1_KEYHEADERFATT AS KEYHEAD, P1_KEYBODYFATT AS KEYFATT, " << "PZ_NUMPROT AS [NUM_PROT], PZ_ANNOPROT AS [ANNO_PROT], PZ_TIPOCF AS [TIPO_CF], PZ_CLIFOR AS [COD_CLIFOR]\n" << "FROM PAA0100F PAA01\n" << "JOIN PAA0200F PAA02 ON P1_KEYPRGINVIO = P2_KEYPRGINVIO and P1_KEYHEADERFATT = P2_KEYHEADERFATT and P1_KEYBODYFATT = P2_KEYBODYFATT\n" << "JOIN PAA0700F PAA07 ON P1_KEYPRGINVIO = P7_KEYPRGINVIO and P1_KEYHEADERFATT = P7_KEYHEADERFATT and P1_KEYBODYFATT = P7_KEYBODYFATT\n" << "JOIN PAA2700F PAA27 ON P1_KEYPRGINVIO = PQ_KEYPRGINVIO and P1_KEYHEADERFATT = PQ_KEYHEADERFATT and P1_KEYBODYFATT = PQ_KEYBODYFATT\n" << "LEFT JOIN PAA2600F PAA26 ON P1_KEYPRGINVIO = PP_KEYPRGINVIO and P1_KEYHEADERFATT = PP_KEYHEADERFATT and P1_KEYBODYFATT = PP_KEYBODYFATT\n" << "LEFT JOIN PAA3200F PAA32 ON P1_KEYPRGINVIO = PU_KEYPRGINVIO and P1_KEYHEADERFATT = PU_KEYHEADERFATT and P1_KEYBODYFATT = PU_KEYBODYFATT\n" << "LEFT JOIN FPPRO00F FPPRO ON P1_KEYPRGINVIO = PZ_KEYPRGINVIO and P1_KEYHEADERFATT = PZ_KEYHEADERFATT and P1_KEYBODYFATT = PZ_KEYBODYFATT\n" << "WHERE PZ_DATAORARIC >= \'" << dal.date2ansi() << "\' AND PZ_DATAORARIC < \'" << al.date2ansi()+1 << "\'" << prots_query << "\n" << "GROUP BY YEAR(P7_DATA), P7_TIPODOC, P7_NUMERO, P7_DATA, PZ_DATAORARIC, PQ_IMPTOTDOC, PZ_NUMREGCONT, PZ_DATAREGCONT, P2_ANADENOMIN, P2_ANANOME, P2_ANACOGNOME, P2_FISCIVACOD, P2_CODFISCALE, P2_FISCIVAPAESE,\n" << "P1_CODDEST, PU_PEC, P1_KEYPRGINVIO, P1_KEYHEADERFATT, P1_KEYBODYFATT, PZ_TIPOPROT, PZ_NUMPROT, PZ_ANNOPROT, PZ_TIPOCF, PZ_CLIFOR\n" << "ORDER BY " << (ordin == "D"? "P7_DATA" : "PZ_DATAORARIC") << " " << (verso_ord == "C"? "ASC" : "DESC"); fp_db().sq_set_exec(query, false); _list_fatt_err.clear(); while(fp_db().sq_next()) { aggiungi_riga(clifo, sf, sf_err); } sf.force_update(); sf.show(); sf_err.force_update(); sf_err.show(); } void TPassive_mask::elenco_prots_sel(TString& string) const { TString_array elenco; TSheet_field& sf = sfield(F_PROT); string << " AND PZ_TIPOPROT IN ("; FOR_EACH_SHEET_ROW(sf, nr, row) { if (*row->get(0) == 'X') elenco.add(row->get()); } // Creo una stringa da aggiungere alla query per la ricerca col codice protocollo, così fatta: // " AND PZ_TIPOPROT IN ('a', 'b', 'c')". (con elenco avente "a", "b" e "c" come stringhe) if (elenco.items() > 0) { for (int i = 0; i < elenco.items() - 1; i++) string << "\'" << elenco.row(i) << "\', "; string << "\'" << elenco.row(elenco.items() - 1) << "\')"; } else string = ""; } void TPassive_mask::aggiungi_riga(TLocalisamfile& clifo, TSheet_field& sf, TSheet_field& sf_err) { TString denom = fp_db().sq_get("RAG_SOC"); const TString& paa_codfisc = fp_db().sq_get("COD_FISC"); const TString& paa_piva = fp_db().sq_get("P_IVA"); const TString& paa_codpaese = fp_db().sq_get("COD_PAESE"); const TString& paa_codsdi = fp_db().sq_get("COD_SDI"); const TDate& dataregcont = fp_db().sq_get_date("DATAREGCONT"); const TString& filter_elab = get_fattsel(); const bool is_contab = dataregcont != TDate("20010101"); clifo.zero(); clifo.put(CLI_TIPOCF, 'F'); const int forn_code = find_fornitore(clifo); const bool data_range = dataregcont >= TDate(get(F_DATAINIREG)) && dataregcont <= TDate(get(F_DATAENDREG)); if( filter_elab.blank() && forn_code != 0 // Filtro sui non associati || filter_elab == FILTER_ASS && forn_code == 0 && !is_contab // Filtro sui associati e non contab. || filter_elab == FILTER_CONT && forn_code == 0 && is_contab && data_range // Filtro solo contabilizzati e data || filter_elab == FILTER_ALL) // Tutte { TToken_string& row = sf.row(-1); if (denom.blank()) { denom = fp_db().sq_get("NOME"); denom << " " << fp_db().sq_get("COGNOME"); } denom.cut(50); row.add(fp_db().sq_get_int("ANNO"), 1); row.add(fp_db().sq_get("TIPO_SDI")); row.add(fp_db().sq_get("NUM_DOC")); row.add(fp_db().sq_get_date("DATA_RIC")); row.add(fp_db().sq_get_date("DATA_DOC")); row.add(fp_db().sq_get("TOT_DOC")); // Decido se mettere o meno il flag if (forn_code == 0) row.add("X"); else row.add(""); row.add(fp_db().sq_get("NUMREGCONT")); if(dataregcont == TDate("20010101")) row.add(""); else row.add(dataregcont); // Valorizzo le colonne rimanenti o la tabella degli errori if (forn_code >= 0) { row.add(clifo.get(CLI_CODCF)); row.add(clifo.get(CLI_RAGSOC)); } else if (_list_fatt_err.find(denom) == _list_fatt_err.end()) { TForn_err& forn_err = _list_fatt_err[denom]; forn_err.forn_code = forn_code; forn_err.fatt_err = sf.items() - 1; add_row_err_forn(forn_code, sf_err, denom); } row.add(paa_codpaese, sf.cid2index(S_STATOPAIV)); row.add(paa_piva); row.add(paa_codfisc); row.add(fp_db().sq_get("ATTACHMENT") != "0" ? "" : "X"); row.add(denom); row.add(TProtocollo(fp_db().sq_get_int("ANNO_PROT"), fp_db().sq_get("TIPO_PROT"), fp_db().sq_get("NUM_PROT"))); TString key_prot; key_prot << fp_db().sq_get("KEYPROG") << ";" << fp_db().sq_get("KEYHEAD") << ";" << fp_db().sq_get("KEYFATT"); row.add(key_prot); } } void TPassive_mask::aggiungi_prot() const { TSheet_field& sf_prot = sfield(F_PROT); TString query = ""; TString prot = ""; query << "SELECT DISTINCT(PZ_TIPOPROT) AS TIPO_PROT \nFROM FPPRO00F"; fp_db().sq_set_exec(query, false); while (fp_db().sq_next()) { TToken_string& row = sf_prot.row(sf_prot.items()+1); prot = fp_db().sq_get("TIPO_PROT"); row.add("", 0); row.add(prot.empty() ? "no-prot." : prot ); } sf_prot.force_update(); } void TPassive_mask::add_row_err_forn(const int forn_code, TSheet_field& sf_err, TString& denom) { TToken_string& row_err = sf_err.row(-1); row_err.add(fp_db().sq_get("COD_PAESE"), 1); row_err.add(fp_db().sq_get("P_IVA")); row_err.add(fp_db().sq_get("COD_FISC")); row_err.add(denom); switch (forn_code){ case err_match_db: row_err.add("Salvato in database ma non corrisponde a un fornitore codificato. (Nuovo fornitore disabilitato)"); break; case no_forn: row_err.add("Non trovato fornitore per la P.IVA."); break; case no_match_cf: row_err.add("Trovato fornitore ma il codice fiscale non corrisponde. Aggiornare? (Solo se non e' gruppo IVA)"); break; case no_cf: row_err.add("Fornitore senza P. IVA, non trovato il codice fiscale"); break; default: row_err.add("Errore durante identificazione fornitore."); break; } } TString& TPassive_mask::get_fattsel() const { static TString fattsel; fattsel.cut(0); if (get_bool(F_CONTABIL)) return fattsel << FILTER_CONT; return fattsel << get(F_ASSOC); } int TPassive_mask::find_fornitore(TLocalisamfile& clifo) { TString paa_codfisc = fp_db().sq_get("COD_FISC"); const TString paa_piva = fp_db().sq_get("P_IVA"); TString paa_codpaese = fp_db().sq_get("COD_PAESE"); const TString fppro_tipocf = fp_db().sq_get("TIPO_CF"); TString fppro_codcf = fp_db().sq_get("COD_CLIFOR"); // Cerco se il fornitore è presente in Campo int found_clifo = -1; if (fppro_codcf == "17") bool simo = true; TString piva; // Leggo dall FPPRO se è già stato salvato il fornitore // Se è già salvato nell FPPRO ricerco in Campo col codice fornitore (chiave 1) if (fppro_tipocf == "F" && !fppro_codcf.blank()) { clifo.setkey(1); clifo.put(CLI_CODCF, fppro_codcf); // Se trovo dall FPPRO setto a 0 clifo.read() == NOERR ? found_clifo = 0 : found_clifo = -3; } else if (paa_codpaese.full() && (piva = paa_piva).full()) // Se non c'è nell FPPRO ricerco con chiave 5 { if (piva == "01903590154") bool simo = true; clifo.setkey(5); clifo.put(CLI_STATOPAIV, paa_codpaese); clifo.put(CLI_PAIV, paa_piva); clifo.read() == NOERR ? found_clifo = 2 : found_clifo = -1; // Se trovo con partita iva setto a 2 if (found_clifo != 2 && paa_codpaese == "IT") // Se non l'ho trovato potrebbe essere italiano e ha codice paese blank { clifo.put(CLI_PAIV, paa_piva); clifo.put(CLI_STATOPAIV, ""); clifo.read() == NOERR ? found_clifo = 2 : found_clifo = -5; // Se trovo con partita iva setto a 2 } // Se trovo con p.iva controllo il cod. fisc. e, se c'è da db e se c'è in clienti-fornitori, altrimenti vado avanti if (found_clifo == 2 && paa_codfisc.full() && clifo.get(CLI_COFI).full()) { if (clifo.get(CLI_COFI) == paa_codfisc) // Controllo che il cod fisc (se c'è) corrisponda found_clifo = 2; else found_clifo = -15; } } // Altrimenti lo cerco con chiave 4, se ho il codice fiscale o ho il cod fisc e l'ho trovato con p iva else if (paa_codfisc.full()) { clifo.setkey(4); clifo.put(CLI_COFI, paa_codfisc); clifo.read() == NOERR ? found_clifo = 4 : found_clifo = -14; // Se il cod fisc corrisponde setto a 2 } // Se lo trovo controllo che non abbia il flag di sospeso se no ricerco ancora bool noerr = true; if (found_clifo == 2) { while (clifo.get_bool(CLI_SOSPESO) && noerr && clifo.get(CLI_PAIV) == paa_piva) noerr = clifo.next() == NOERR; if (clifo.get_bool(CLI_SOSPESO) || !noerr || clifo.get(CLI_PAIV) != paa_piva) found_clifo = -5; } else if (found_clifo == 4) { while (clifo.get_bool(CLI_SOSPESO) && noerr && clifo.get(CLI_COFI) == paa_codfisc) noerr = clifo.next() == NOERR; if (clifo.get_bool(CLI_SOSPESO) || !noerr || clifo.get(CLI_COFI) != paa_codfisc) found_clifo = -14; } return found_clifo; } void TPassive_mask::init() { aggiungi_prot(); } bool TPassive_mask::causfa_handler(TMask_field& f, KEY k) { if (k == K_TAB && f.focusdirty()) { TMask& msk = f.mask(); const TString& codcaus = msk.get(F_CAUSFA); if (!check_causale(codcaus)) { warning_box("Impossibile selezionare questa causale registrazione.\nPrego inserire una causale di Fattura Acquisti \no Nota Credito Acquisto"); msk.field(F_CAUSFA).set(""); msk.field(F_DESCAUSFA).set(""); set_ini_codcaus(""); return false; } set_ini_codcaus(codcaus); } return true; } void TPassive_mask::select_all(const int sheet_field) { TSheet_field& docs = sfield((short)sheet_field); TString_array& sht = docs.rows_array(); const int items = sht.items(); if (items > 0) { const TString4 select = *sht.row(0).get(0) == 'X' ? "" : "X"; for (int i = 0; i < items; i++) sht.row(i).add(select, 0); docs.force_update(); } } void TPassive_mask::salva_for() const { if(fp_db().sq_is_connect()) { TSheet_field& sf = sfield(F_DOCS); TLocalisamfile clifo(LF_CLIFO); // Leggo in Campo per controllare che il codice cliente non sia errato sf.hide(); FOR_EACH_SHEET_ROW(sf, nr, row) { if (row->starts_with("X") && strcmp(clifo.get(CLI_CODCF), "17") == 0) bool simo = true; TString cod_forn = row->get(sf.cid2index(S_FORNITORE)); if(row->starts_with("X") && !cod_forn.empty()) { clifo.zero(); clifo.setkey(1); clifo.put(CLI_TIPOCF, "F"); clifo.put(CLI_CODCF, cod_forn); if(clifo.read() == NOERR) { TString q_update = ""; TToken_string key(row->get(sf.cid2index(S_PROKEY)), ';'); const TString& clifo_cofi = clifo.get(CLI_COFI); const TString& clifo_paiv = clifo.get(CLI_PAIV); if (!clifo_cofi.empty() && strcmp(row->get(sf.cid2index(S_CODFISC)), clifo_cofi) == 0 || !clifo_paiv.empty() && strcmp(row->get(sf.cid2index(S_PARIVA)), clifo_paiv) == 0) { q_update << "UPDATE FPPRO00F SET PZ_TIPOCF = 'F', PZ_CLIFOR = '" << row->get(sf.cid2index(S_FORNITORE)) << "' WHERE PZ_KEYPRGINVIO = '" << key.get(0) << "' AND PZ_KEYHEADERFATT = '" << key.get(1) << "' AND PZ_KEYBODYFATT = '" << key.get(2) << "'"; fp_db().sq_set_exec(q_update, false); row->add("X", sf.cid2index(S_FPPRO)); } } } row->add("", 0); } fp_db().sq_commit(); sf.force_update(); sf.show(); } } void TPassive_mask::clean_ini(const TString& filename) const { static TString remove_string; #ifdef DBG remove_string.cut(0) << filename << "*.ini"; #else remove_string.cut(0) << _tmp_dir << "\\" << filename << "*.ini"; #endif remove_files(remove_string, false); } void TPassive_mask::run_cg0(const TString& filename) const { static TString run_string; #ifdef DBG run_string.cut(0) << "cg0 -1 -i" << filename << "*.ini" << " /u" << user(); #else run_string.cut(0) << "cg0 -1 -i" << _tmp_dir << "\\" << filename << "*.ini" << " /u" << user(); #endif TExternal_app(run_string).run(); } int TPassive_mask::prepara_contab() const { int n_sel = 0; TSheet_field& sf = sfield(F_DOCS); sf.hide(); clean_ini(F1_INIREGCONT); FOR_EACH_SHEET_ROW(sf, n, row) { if (row->starts_with("X")) { if(!TString(row->get(sf.cid2index(S_NUMREGCONT))).empty()) return n+1000; TString codcaus(get_codcaus(row->get(2), row->get(10))); if (codcaus.empty()) return no_codcaus; TString tipodoc(row->get(sf.cid2index(S_TIPODOCSDI))); if(tipodoc == "TD01" && !check_causale(codcaus, "FA")) if(!yesno_box("Attenzione, per un documento di tipo TD01 e' stata selezionata \nuna causale diversa da Fattura d'Acquisto.\nContinuare?")) return cancel; if(tipodoc == "TD04" && !check_causale(codcaus, "NC")) if(!yesno_box("Attenzione, per un documento di tipo TD04 e' stata selezionata \nuna causale diversa da Nota Credito di Acquisto.\nContinuare?")) return cancel; n_sel++; TString num; num.format("%04d", n); TFilename newf_ini; #ifndef DBG newf_ini.tempdir() << "\\" << F1_INIREGCONT << num << ".ini"; #else newf_ini << F1_INIREGCONT << num << ".ini"; #endif TConfig contab_ini(newf_ini, "Transaction"); TString prokeys = row->get(sf.cid2index(S_PROKEY)); contab_ini.set("Action", "INSERT"); contab_ini.set_paragraph(LF_MOV); // [23] contab_ini.set("CODCAUS", codcaus); contab_ini.set("CODCF", row->get(sf.cid2index(S_FORNITORE))); contab_ini.set("DATACOMP", TDate(TODAY)); contab_ini.set("DATADOC", row->get(sf.cid2index(S_DATADOC))); contab_ini.set("NUMDOCEXT", row->get(sf.cid2index(S_NDOC))); contab_ini.set("NUMDOC", TString(row->get(sf.cid2index(S_NDOC))).right(7)); contab_ini.set("TOTDOC", abs(real(row->get(sf.cid2index(S_TOTDOC)))).string()); contab_ini.set("KEYFPPRO", prokeys); // Se ci sono le scadenze esporto anche quelle TToken_string keys(prokeys, ';'); TString where_q; where_q << "WHERE PO_KEYPRGINVIO = '" << keys.get() << "'"; where_q << " AND PO_KEYHEADERFATT = '" << keys.get() << "'"; where_q << " AND PO_KEYBODYFATT = '" << keys.get() << "'"; TString query; query << "SELECT PO_RIGA AS RIGA, PO_DATASCADENZA AS DATA, PO_IMPORTO AS IMPORTO \nFROM PAA2500F \n" << where_q; fp_db().sq_set_exec(query,false); for (bool ok = fp_db().sq_next(); ok; ok = fp_db().sq_next()) { const int riga = real(fp_db().sq_get("RIGA")).integer(); contab_ini.set_paragraph(LF_SCADENZE, riga); // Setto il paragrafo con riga scad contab_ini.set("DATASCAD", fp_db().sq_get_date("DATA")); contab_ini.set("IMPORTO", fp_db().sq_get("IMPORTO")); contab_ini.set("NRATA", riga); contab_ini.set("NRIGA", "1"); } where_q.cut(0) << "WHERE PL_KEYPRGINVIO = '" << keys.get(0); where_q << "' AND PL_KEYHEADERFATT = '" << keys.get(); where_q << "' AND PL_KEYBODYFATT = '" << keys.get() << "'"; query.cut(0) << "SELECT PL_IMPONIBILE AS IMPONIBILE, PL_IMPOSTA AS IMPOSTA \nFROM PAA2200F \n" << where_q; fp_db().sq_set_exec(query, false); int i = 1; for (bool ok = fp_db().sq_next(); ok; ok = fp_db().sq_next()) { TString imponibile = fp_db().sq_get("IMPONIBILE"); TString imposta = fp_db().sq_get("IMPOSTA"); if(imponibile.full() || imposta.full()) { contab_ini.set_paragraph(LF_RMOVIVA, i); contab_ini.set("IMPONIBILE", imponibile); contab_ini.set("IMPOSTA", imposta); contab_ini.set("NRIGA", i); i++; } } row->add("", 0); } } sf.force_update(); sf.show(); if (n_sel == 0) return no_selected; return is_ready; } void TPassive_mask::log_contab() { TFilename cg_ini; TString msg; TF1_log log; std::vector doc_saved, doc_canceled, save_numreg, save_protiv; FOR_EACH_SHEET_ROW(sfield(F_DOCS), n, row) { TString num; num.format("%04d", n); #ifdef DBG cg_ini = TString(F1_INIREGCONT) << num << ".ini"; #else cg_ini = TFilename().tempdir() << "\\" << TString(F1_INIREGCONT) << num << ".ini"; #endif if(cg_ini.exist()) { TConfig config(cg_ini, "Transaction"); TString numdoc; TString numreg; TString protiv; if (config.get("Result") == "OK") { numdoc = config.get(MOV_NUMDOCEXT, "23"); numreg = config.get(MOV_NUMREG, "23"); protiv = config.get(MOV_PROTIVA, "23"); doc_saved.insert(doc_saved.end(), numdoc); save_numreg.insert(save_numreg.end(), numreg); save_protiv.insert(save_protiv.end(), protiv); } else if(config.get("Result") == "CANCEL") { numdoc = config.get(MOV_NUMDOCEXT, "23"); doc_canceled.insert(doc_canceled.end(), numdoc); } } } const int elem_save = doc_saved.size(); const int elem_canc = doc_canceled.size(); if (elem_save > 0) { if (elem_save == 1) msg << "Il documento n. '" << doc_saved[0] << "' e' stato registrato con numero movimento contabile '" << save_numreg[0] << "' e protocollo iva '" << save_protiv[0] << "'."; else msg << "Sono stati registrati " << elem_save << " documenti."; if (elem_save > 1) for (int i = 0; i < elem_save; i++) msg << "\n - Documento n. '" << doc_saved[i] << "': movimento contabile '" << save_numreg[0] << "', protocollo iva '" << save_protiv[0] << "'"; TToken_string msg_tok(msg, '\n'); for(int i=0; i 0) { msg.cut(0); if (elem_canc == 1) msg << "La registrazione del documento n. '" << doc_canceled[0] << "' e' stata annullata."; else msg << "Sono state annullate " << elem_canc << " registrazioni."; if (elem_canc > 1) for (int i = 0; i < elem_canc; i++) msg << "\n - Documento n. '" << doc_canceled[i] << "'"; TToken_string msg_tok(msg, '\n'); for (int i = 0; i < msg_tok.items(); i++) log.log(LOG_WARN, msg_tok.get()); } if (elem_save > 0 || elem_canc > 0) log.show_log(); } void TPassive_mask::contabilizza() { const int stato = prepara_contab(); switch(stato) { case is_ready: run_cont_ini(get_bool(F_PERIODPREC)); // Eseguo gli ini log_contab(); // Mostro stato contabilizzate/annullate clean_ini(F1_INIREGCONT); fill(); // Ricarico sheet per togliere quelli contabilizzati break; case no_codcaus: message_box("Attenzione, il fornitore non ha associato nessun codice causale predefinito.\nPrego selezionare un codice causale per la contabilizzazione."); next_page(1000); break; case no_selected: message_box("Selezionare almeno un documento."); break; case cancel: next_page(1000); default: break; } if(stato >= 1000) { TSheet_field& sf = sfield(F_DOCS); TToken_string& row = sf.row(stato - 1000); const TString ndoc = row.get_int(sf.cid2index(S_NDOC)); const TDate datadoc = row.get(sf.cid2index(S_DATADOC)); const int nreg = row.get_int(sf.cid2index(S_NUMREGCONT)); TString msg; msg << "Attenzione il doc " << ndoc << " del " << datadoc << " e' gia' stato registrato con numero reg. " << nreg; warning_box(msg); fill(); } } TDate TPassive_mask::load_data() const { TString date; date << ini_get_string(CONFIG_DITTA, "fp", "dataini_fp04"); TDate start_date(TODAY); if(!date.empty()) start_date = date; else start_date.set_day(1); return start_date; } void TPassive_mask::new_forn() { TSheet_field& sf = sfield(F_ERR); static const TString newf = "fpnewf"; clean_ini(newf); FOR_EACH_SHEET_ROW(sf, nr, row) { const TString denom = row->get(sf.cid2index(S_RAGSERR)); TForn_err& forn_err = _list_fatt_err[denom]; const int& cod_forn = forn_err.forn_code; // Prendo codice di errore associazione fornitore const int& n_row_elenco = forn_err.fatt_err; // Prendo che numero di riga a cui mi riferisco in fatture TSheet_field& f_docs = sfield(F_DOCS); // Prendo SheetField da elenco fatture TToken_string& row_elenco_fatt = f_docs.row(n_row_elenco); // Prendo riga dall'elenco delle fatture TToken_string keys(row_elenco_fatt.get(f_docs.cid2index(S_PROKEY)), ';'); // Dalla riga leggo la chiave del db if (row->starts_with("X") && (cod_forn == no_forn || cod_forn == no_match_cf || cod_forn == no_cf)) { TString query; query << "SELECT PU_PEC AS PEC\n" << "FROM PAA3200F\n" << "WHERE PU_KEYPRGINVIO = '" << keys.get(); query << "' AND PU_KEYHEADERFATT = '" << keys.get(); query << "' AND PU_KEYBODYFATT = '" << keys.get() << "'"; fp_db().sq_set_exec(query); TString pec = fp_db().sq_get("PEC"); TString num; num.format("%04d", nr); TFilename newf_ini; #ifdef DBG newf_ini << newf << num << ".ini"; #else newf_ini.tempdir() << "\\" << newf << num << ".ini"; #endif TConfig forn_conf(newf_ini, "Transaction"); forn_conf.set("Action", "INSERT"); forn_conf.set_paragraph("20"); forn_conf.set("TIPOCF", "F"); forn_conf.set("COFI", row->get(sf.cid2index(S_CODFIERR))); forn_conf.set("PAIV", row->get(sf.cid2index(S_PIVAERR))); forn_conf.set("PEC", pec); forn_conf.set("RAGSOC", row->get(sf.cid2index(S_RAGSERR))); forn_conf.set("STATOPAIV", row_elenco_fatt.get(sf.cid2index(S_STATOPAIV))); query.cut(0) << "SELECT P2_SEDEIND, P2_SEDENRCIVICO, P2_SEDECAP, P2_SEDECOMUNE, P2_SEDEPROV, P2_SEDENAZ, " << "P2_CONTATTITELEF, P2_CONTATTIFAX, P2_CONTATTIMAIL\n" << "FROM PAA0200F \nWHERE P2_KEYPRGINVIO = '" << keys.get(0) << "' AND P2_KEYHEADERFATT = '" << keys.get(1) << "' AND P2_KEYBODYFATT = '" << keys.get(2) << "'"; fp_db().sq_set_exec(query); forn_conf.set("INDCF", fp_db().sq_get("P2_SEDEIND")); forn_conf.set("CIVCF", fp_db().sq_get("P2_SEDENRCIVICO")); forn_conf.set("CAPCF", fp_db().sq_get("P2_SEDECAP")); TLocalisamfile com(LF_COMUNI); com.setkey(2); com.put(COM_DENCOM, fp_db().sq_get("P2_SEDECOMUNE")); com.read(); forn_conf.set("COMCF", com.get(COM_COM)); forn_conf.set("TEL", fp_db().sq_get("P2_CONTATTITELEF")); forn_conf.set("TELEX", fp_db().sq_get("P2_CONTATTIFAX")); forn_conf.set("PEC", fp_db().sq_get("P2_CONTATTIMAIL")); forn_conf.set("DOCMAIL", fp_db().sq_get("P2_CONTATTIMAIL")); forn_conf.set_paragraph("17"); forn_conf.set("TIPOCF", "F"); forn_conf.set("RAGGOR", "O"); row->add("", 0); } } run_cg0(newf); sf.force_update(); } bool TPassive_mask::aggiorna_forn() { TSheet_field& sf = sfield(F_ERR); static const TString newf = "fpaggf"; clean_ini(newf); int count = 0; FOR_EACH_SHEET_ROW(sf, nr, row) { const TString denom = row->get(sf.cid2index(S_RAGSERR)); TForn_err& forn_err = _list_fatt_err[denom]; const int& cod_forn = forn_err.forn_code; // Prendo codice di errore associazione fornitore const int& n_row_elenco = forn_err.fatt_err; // Prendo che numero di riga a cui mi riferisco in fatture TSheet_field& f_docs = sfield(F_DOCS); // Prendo SheetField da elenco fatture TToken_string& row_elenco_fatt = f_docs.row(n_row_elenco); // Prendo riga dall'elenco delle fatture if (row->starts_with("X") && cod_forn == no_match_cf) { count++; TLocalisamfile clifo(LF_CLIFO); clifo.setkey(5); clifo.put(CLI_TIPOCF, 'F'); clifo.put(CLI_STATOPAIV, row->get(sf.cid2index(S_STATOERR))); clifo.put(CLI_PAIV, row->get()); if(clifo.read() != NOERR) { TString msg = "Impossibile trovare il fornitore scelto "; msg << row->get(sf.cid2index(S_STATOERR)) << " " << row->get(); warning_box(msg); continue; } if(clifo.get(CLI_ALLEG) != "G") // Controllo che non sia un gruppo IVA { TString num; num.format("%04d", nr); TFilename newf_ini; #ifdef DBG newf_ini << newf << num << ".ini"; #else newf_ini.tempdir() << "\\" << newf << num << ".ini"; #endif TConfig forn_conf(newf_ini, "Transaction"); forn_conf.set("Action", "MODIFY"); forn_conf.set_paragraph("20"); forn_conf.set("TIPOCF", "F"); forn_conf.set("COFI", row->get(sf.cid2index(S_CODFIERR))); forn_conf.set("CODCF", clifo.get(CLI_CODCF)); TString codsdi; codsdi << row_elenco_fatt.get(16); if (!codsdi.empty() && codsdi != "0000000") { forn_conf.set_paragraph("17"); forn_conf.set("PADESTIN", codsdi); } } } row->add("", 0); } if (count) run_cg0(newf); sf.force_update(); return count; } void TPassive_mask::fattsel(const int dlg, TField_event& e) { if (e == fe_init) set(F_CONTABIL, ""); else { if(dlg == F_CONTABIL) set(F_ASSOC, "A"); set(F_CONTABIL, dlg == F_CONTABIL && get_bool(F_CONTABIL)? "X" : " "); } field(F_DATAINI).enable(!get_bool(F_CONTABIL)); field(F_DATAEND).enable(!get_bool(F_CONTABIL)); field(F_DATAINIREG).enable(get_bool(F_CONTABIL)); field(F_DATAENDREG).enable(get_bool(F_CONTABIL)); } bool TPassive_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { switch (o.dlg()) { case S_SELCODPROT: if(e == fe_modify) set_filter_changed(); break; case F_PROT: if(e == fe_init) init(); break; case F_DOCS: if (e == se_query_modify) break; case DLG_USER: if (e == fe_button && jolly > 0 && get_fattsel() == FILTER_CONT) { TSheet_field& sf = sfield(F_DOCS); TToken_string& row = sf.row(sf.selected()); TRectype mov(LF_MOV); mov.put(MOV_NUMREG, row.get(sf.cid2index(S_NUMREGCONT))); if (mov.edit()) fill(); } break; case DLG_ALL: if (e == fe_button) switch (curr_page()) { case filtri: select_all(F_PROT); set_filter_changed(); break; case elenco_fe: select_all(F_DOCS); break; case elenco_err: select_all(F_ERR); break; default: break; } break; case DLG_SAVEREC: if (e == fe_button) if (curr_page() == elenco_fe) { salva_for(); fill(); } break; case DLG_NEWREC: if(e == fe_button) if(curr_page() == elenco_err) new_forn(); break; case DLG_RECALC: if (e == fe_button) if (curr_page() == elenco_err) if (aggiorna_forn()) fill(); break; case DLG_EXPORT: if (e == fe_button && curr_page() == elenco_fe) contabilizza(); break; case F_DATAINI: if (e == fe_init) o.set(load_data()); break; case F_DATAEND: if (e == fe_init) o.set(TDate(TODAY)); break; case F_PERIODPREC: if (e == fe_init) o.set(get_periodprec()? "X" : ""); break; default: break; } if (o.dlg() >= F_ASSOC && o.dlg() <= F_CONTABIL) if (e == fe_modify || e == fe_init) fattsel(o.dlg(), e); if ((e == fe_modify || e >= se_enter) && jolly == 0) if (o.dlg() >= START_MASK && o.dlg() <= END_MASK || o.dlg() == F_PROT) set_filter_changed(); return true; } void TPassive_mask::check_buttons(const int p) { const TString& fattsel = get_fattsel(); const bool err_forn = fattsel.empty() || fattsel == FILTER_ALL; field(DLG_EXPORT).enable((curr_page() + p == 1 || p == 1001) && _f1 && fattsel != FILTER_CONT); field(F_GROUPCAU).show((curr_page() + p == 1 || p == 1001) && _f1 && fattsel != FILTER_CONT); field(F_CAUSFA).show((curr_page() + p == 1 || p == 1001) && _f1 && fattsel != FILTER_CONT); field(F_DESCAUSFA).show((curr_page() + p == 1 || p == 1001) && _f1 && fattsel != FILTER_CONT); field(DLG_SAVEREC).enable((curr_page() + p == 1 || p == 1001) && (fattsel.empty() || fattsel == FILTER_ASS)); if (p >= 1000) { field(DLG_NEWREC).enable(p == 1002 && err_forn); field(DLG_RECALC).enable(p == 1002 && err_forn); } else { field(DLG_NEWREC).enable((curr_page() + p == 2 || curr_page() + p == 3) && err_forn); field(DLG_RECALC).enable((curr_page() + p == 2 || curr_page() + p == 3) && err_forn); } } TPassive_mask::TPassive_mask(bool f1) : TAutomask("fp0400a"), _f1(f1), _filter_changed(true) { _tmp_dir = _tmp_dir.tempdir(); TMask::set_handler(F_CAUSFA, causfa_handler); } ///////////////////////////////////////////////////////////////////////////////////// // TPassive_app ///////////////////////////////////////////////////////////////////////////////////// class TPassive_app : public TSkeleton_application { void load_mask(const TPassive_mask& mask) const; bool check_f1() const; void save_field(const TPassive_mask& mask); public: void main_loop() override; TPassive_app() = default; }; void TPassive_app::load_mask(const TPassive_mask& mask) const { if (check_f1()) { mask.field(F_CAUSFA).set(get_ini_codcaus()); mask.field(F_DATAINIREG).enable(mask.get_fattsel() == FILTER_CONT); mask.field(F_DATAINIREG).set(get_datainireg()); if(mask.field(F_DATAINIREG).get().empty()) { TDate start(TODAY); start.set_day(1); mask.field(F_DATAINIREG).set(start); } mask.field(F_DATAENDREG).enable(mask.get_fattsel() == FILTER_CONT); mask.field(F_DATAENDREG).set(TDate(TODAY)); } else { mask.field(F_GROUPCON).hide(); mask.field(F_GROUPCON2).hide(); mask.field(F_GROUPCAU).hide(); } } bool TPassive_app::check_f1() const { return has_module(F1AUT); } void TPassive_app::save_field(const TPassive_mask& mask) { if (check_f1()) { set_ini_codcaus(mask.get(F_CAUSFA)); set_datainireg(mask.get(F_DATAINIREG)); set_periodprec(mask.get_bool(F_PERIODPREC)); } ini_set_string(CONFIG_DITTA, PARA_FP, "dataini_fp04", mask.get(F_DATAINI)); } void TPassive_app::main_loop() { TPassive_mask mask(check_f1()); load_mask(mask); mask.first_focus(F_DATAINI); while(mask.run() == K_ENTER) { } save_field(mask); } int fp0400(int argc, char * argv[]) { TPassive_app fpas; fpas.run(argc, argv, TR("Fatturazione F.P.")); return 0; }