#include #include #include #include #include //#include #include #include #include #include #include "../ca/pconana.h" #include "../ca/movana.h" #include "../ca/rmovana.h" #include "../ca/calib01.h" #include "../ca/calib02.h" #include "crpa1a.h" //--------------------------------- // TAutomask //--------------------------------- class TBudget_Import_mask : public TAutomask { protected: bool on_field_event(TOperable_field& o, TField_event e, long jolly); bool select_file_ext(const char* ext, TFilename& filename); public: TBudget_Import_mask(); virtual ~TBudget_Import_mask(){}; }; TBudget_Import_mask::TBudget_Import_mask() :TAutomask ("crpa1a") { } bool TBudget_Import_mask::select_file_ext(const char* ext, TFilename& filename) { TArray_sheet as(-1, -1, 72, 20, TR("Selezione file"), "File@48"); TFilename path = get(F_PATH); path.add("*"); path.ext(ext); list_files(path, as.rows_array()); TFilename name; FOR_EACH_ARRAY_ROW(as.rows_array(), i, row) { name = *row; *row = name.name(); } bool ok = as.run() == K_ENTER; if (ok) { filename = as.row(as.selected()); } return ok; } bool TBudget_Import_mask::on_field_event(TOperable_field& f, TField_event e, long jolly) { switch (f.dlg()) { //giochetto per avere la lista dei files validi nella directory di trasferimento! case F_NAMEFILE: if (e == fe_button) { TFilename name; if (select_file_ext("csv", name)) f.set(name); if (f.get() == get(F_LASTFILE)) return error_box(TR("File gia' importato!\nNON e' consigliabile proseguire!")); } break; default: break; } return true; } //-------------------------------- // TFile_text //-------------------------------- class TBudget_Import_file: public TFile_text { protected: virtual void validate(TCursor& cur,TRecord_text &rec, TToken_string &val, TString& str); public: TBudget_Import_file(const TString& file_name); virtual ~TBudget_Import_file() { } }; TBudget_Import_file::TBudget_Import_file(const TString& file_name) : TFile_text(file_name, "crpa1.ini") { } void TBudget_Import_file::validate(TCursor& cur,TRecord_text &rec, TToken_string &s, TString& str) { const TString code(s.get(0)); TString valore = str; if (code == "_UPPERCASE") { valore.upper(); } else NFCHECK("Macro non definita: %s", (const char *)code); str = valore; } //---------------------------------- // TSkeleton_application //---------------------------------- class TBudget_Import : public TSkeleton_application { TBudget_Import_mask* _msk; TBudget_Import_file* _trasfile; TConfig* _configfile; TRelation* _relmovana; TCursor* _cur; TProgind* _prog; TString _lastfile; virtual const char * extra_modules() const {return "ba";} protected: void mask2ini(); void ini2mask(); bool transfer(); void transfer_movimento(const TRecord_text& curr); //, TError_log& log); long get_next_key(); public: virtual bool create(); virtual bool destroy(); virtual void main_loop(); TBudget_Import() {} }; long TBudget_Import::get_next_key() { TLocalisamfile& lfile = _relmovana->lfile(); long numreg = 1L ; if (!lfile.empty()) { lfile.zero() ; lfile.setkey(1) ; lfile.read(_isgteq); if (lfile.good()) //se e' tutto ok,si posiziona sull'ultimo record lfile.last(); if (lfile.good()) numreg += lfile.get_long(MOVANA_NUMREG); } return numreg; } void TBudget_Import::transfer_movimento(const TRecord_text& curr) //, TError_log& log) { const TString codcms = curr.get(1); const TString codconto = curr.get(2); const real soldini = curr.get(3); const TString16 str_dataini = curr.get(4); const TString16 str_datafine = curr.get(5); TString4 gg = str_dataini.left(2); TString4 mm = str_dataini.mid(3,2); TString4 yy = str_dataini.right(2); yy.insert("20", 0); const TDate dataini(atoi(gg),atoi(mm),atoi(yy)); const int annoes = dataini.year(); gg = str_datafine.left(2); mm = str_datafine.mid(3,2); yy = str_datafine.right(2); yy.insert("20", 0); const TDate datafine(atoi(gg),atoi(mm),atoi(yy)); //cerca il primo posto libero in movana long numreg = get_next_key(); //TESTATA //crea il movimento di prima nota da aggiungere a movana TAnal_mov movana(numreg); movana.zero(); //azzera per sicurezza //sbatte i dati nei campi movana.put(MOVANA_NUMREG, numreg); movana.put(MOVANA_ANNOES, annoes); movana.put(MOVANA_DATAREG, dataini); movana.put(MOVANA_DATACOMP, dataini); movana.put(MOVANA_DESCR, "Movimento di budget importato"); movana.put(MOVANA_TIPOMOV, 'P'); movana.put(MOVANA_TOTDOC, soldini); TString query; TString tmp_codconto; tmp_codconto << "'" << codconto << "'"; query << "USE PCONANA KEY 1 \n"; query << "SELECT CODCONTO[10,15]=" << tmp_codconto; TISAM_recordset pconana(query); const TRecnotype items = pconana.items(); if (items <= 0) warning_box(TR("Il sottoconto %s NON esiste!"), codconto); if (items > 1) warning_box(TR("Esiste piu' di un sottoconto %s!"),codconto); pconana.move_last(); //si posiziona sul record corretto const TString contone = pconana.get(PCONANA_CODCONTO).as_string(); const TString4 sezione = pconana.get(PCONANA_SEZSALDI).as_string(); //mette quella bastarda di sezione nel record movana.put(MOVANA_SEZIONE, sezione); //RIGA //la riga sara' solo 1 per testata, almeno secondo l'attuale modalita' di import/export del CRPA TRectype& rec_rmovana = movana.new_row(); rec_rmovana.put(RMOVANA_ANNOES, annoes); rec_rmovana.put(RMOVANA_NUMREG ,numreg); rec_rmovana.put(RMOVANA_NUMRIG ,1); rec_rmovana.put(RMOVANA_SEZIONE ,sezione); rec_rmovana.put(RMOVANA_DATACOMP ,dataini); rec_rmovana.put(RMOVANA_CODCMS ,codcms); rec_rmovana.put(RMOVANA_CODCONTO, contone); rec_rmovana.put(RMOVANA_DESCR, "Riga movimento di budget importato"); rec_rmovana.put(RMOVANA_IMPORTO, soldini); //scrive testata e riga TLocalisamfile testate(LF_MOVANA); if (movana.write(testate) == NOERR) { TLocalisamfile righe(LF_RMOVANA); rec_rmovana.write(righe); } } bool TBudget_Import::transfer() { //file da trasferire //costruire il nome del file con path TFilename file = _msk->get(F_PATH); file.add(_msk->get(F_NAMEFILE)); file.ext("csv"); _trasfile = new TBudget_Import_file(file); _trasfile->open(file,'r'); //*** prima va inserito il log in libreria preparazione del log // TError_log log; // log.set_header(TR("Importazione movimenti")); const long dimension = fsize(file); TProgind pi(dimension,TR("Importazione bolle in corso...")); int err = NOERR; TRecord_text curr; while (_trasfile->read(curr) == NOERR && !pi.iscancelled() && err == NOERR) { pi.setstatus(_trasfile->read_file()->tellg()); transfer_movimento(curr); //, log); } _trasfile->close(); delete _trasfile; // log.show(); _lastfile = _msk->get(F_NAMEFILE); return true; } void TBudget_Import::mask2ini() { //aggiorna l'ultimo file inviato su msk e ini _msk->set(F_LASTFILE, _lastfile); _configfile->set_paragraph("BUDGET"); _configfile->set("PATH", _msk->get(F_PATH)); _configfile->set("LASTFILE", _lastfile); } void TBudget_Import::ini2mask() { //carica i parametri del file di configurazione _configfile->set_paragraph("BUDGET"); TString path = _configfile->get("PATH"); _lastfile = _configfile->get("LASTFILE"); _msk->set(F_PATH, path); _msk->set(F_LASTFILE, _lastfile); } bool TBudget_Import::create() { _configfile = new TConfig("crpa1conf.ini"); _msk = new TBudget_Import_mask(); open_files(LF_MOVANA, LF_RMOVANA, LF_PCONANA, 0); _relmovana = new TRelation(LF_MOVANA); return TSkeleton_application::create(); } bool TBudget_Import::destroy() { delete _msk; delete _configfile; return TApplication::destroy(); } void TBudget_Import::main_loop() { KEY tasto; ini2mask(); tasto = _msk->run(); if (tasto == K_ENTER) { if (transfer()) message_box(TR("Importazione movimenti completata")); } mask2ini(); } int main(int argc, char** argv) { TBudget_Import a; a.run(argc, argv, TR("Importazione movimenti di budget per CRPA")); return 0; }