#include #include #include #include #include #include #include "ps6215.h" #include "ps6215100a.h" #include "utility.h" #include "clifo.h" #include "comuni.h" #include "cfven.h" #include "sqlset.h" #include #define CLIFO_RECLEN 730 #define D_ANNO 4 #define D_NUMPART 7 #define D_NRIGA 4 #define D_TIPOC 1 // ['C' | 'F'] (Cliente/Fornitore) #define D_SOTTOCONTO 6 #define D_TIPOMOV 1 #define D_TIPOPAG 1 #define D_NREG 7 #define D_NUMRIG 3 #define D_DATAREG 8 // Formato data dd-MM-yyyy #define D_DATADOC 8 // Formato data dd-MM-yyyy #define D_DATAPAG 8 // Formato data dd-MM-yyyy #define D_NUMDOC 7 #define D_REG 3 #define D_PROTIVA 5 #define D_CODCAUS 3 #define D_SEZ 1 // ['D' | 'A'] (Dare/Avere) #define D_IMPORTO 18 // 18 con 3 decimali, segno, e virgola #define D_IMPOSTA 18 // 18 con 3 decimali, segno, e virgola #define D_SPESE 18 // 18 con 3 decimali, segno, e virgola #define D_IMPTOTDOC 18 // 18 con 3 decimali, segno, e virgola #define D_RITENUTE 18 // 18 con 3 decimali, segno, e virgola #define D_RITSOC 18 // 18 con 3 decimali, segno, e virgola #define D_SEZABB 1 #define D_ABBUONI 18 // 18 con 3 decimali, segno, e virgola #define D_SEZDIFCAM 1 #define D_DIFFCAM 18 // 18 con 3 decimali, segno, e virgola #define D_GRUPPOCL 3 #define D_CONTOCL 3 //const char* fields[] = { "ANNO", "NUMPART", "NRIGA", "TIPOC", "SOTTOCONTO", "TIPOMOV", "TIPOPAG", "NREG", "NUMRIG", "DATAREG", "DATADOC", "DATAPAG", "NUMDOC", "REG", "PROTIVA", "CODCAUS", "SEZ", "IMPORTO", "IMPOSTA", "SPESE", "IMPTOTDOC", "RITENUTE", "RITSOC", "SEZABB", "ABBUONI", "SEZDIFCAM", "DIFFCAM", "GRUPPOCL", "CONTOCL" }; const int dim_fields[] = { D_ANNO, D_NUMPART, D_NRIGA, D_TIPOC, D_SOTTOCONTO, D_TIPOMOV, D_TIPOPAG, D_NREG, D_NUMRIG, D_DATAREG, D_DATADOC, D_DATAPAG, D_NUMDOC, D_REG, D_PROTIVA, D_CODCAUS, D_SEZ, D_IMPORTO, D_IMPOSTA, D_SPESE, D_IMPTOTDOC, D_RITENUTE, D_RITSOC, D_SEZABB, D_ABBUONI, D_SEZDIFCAM, D_DIFFCAM, D_GRUPPOCL, D_CONTOCL }; class TFixed_record { int _items; int* _dims; int _tot_len; char* _str; int pos(int _Idx) const; public: void set_str(int _Idx, const char* _Str) const; const char* const get_line() const { return static_cast(_str); } TFixed_string operator[](int _Idx) const; TFixed_record(int items = 0, const int dims[] = nullptr); }; int TFixed_record::pos(int _Idx) const { int real_idx = 0; if (_Idx >= 0 && _Idx <= _items) { for (int i = 0; i < _Idx; ++i) real_idx += _dims[i]; } return real_idx; } void TFixed_record::set_str(int _Idx, const char* _Str) const { const int p = pos(_Idx); for (int i = p; i < p + _dims[_Idx] && i < (int)strlen(_Str) + p; ++i) _str[i] = _Str[i - p]; } TFixed_string TFixed_record::operator[](int _Idx) const { static TFixed_string* f_str = nullptr; delete f_str; if (_Idx >= 0 && _Idx < _items) { const int p = pos(_Idx); TString appo(_str); appo = appo.sub(p, p + _dims[_Idx]); const TFixed_string str(appo, _dims[_Idx] + 1); f_str = new TFixed_string(str); } else f_str = new TFixed_string(""); return *f_str; } TFixed_record::TFixed_record(const int items, const int dims[]): _items(items), _tot_len(0) { _dims = new int[items]; for (int i = 0; i < items; ++i) { _dims[i] = dims[i]; _tot_len += dims[i]; } _str = new char[_tot_len + 1]; memset(_str, ' ', _tot_len + 1); _str[_tot_len] = '\0'; } /////////////////////////////////////////////////////////// // Utilities /////////////////////////////////////////////////////////// void copy_wzero(char* dest, const int size, const char* source) { for(int i = 0; i < size; ++i) { const char c = source[i]; if (c == '\0') break; dest[i] = c; } } TString& trim_n(TString& str, const int max) { if(str.len() > max) str.rtrim(str.len() - max); return str; } TString to_escape(const TString& val) { TString& app = get_tmp_string(); for (int k = 0; k < val.len(); k++) { switch (val[k]) { case '\'': app << "''"; break; default: app << val[k]; break; } } return app; } /////////////////////////////////////////////////////////// // Main Mask /////////////////////////////////////////////////////////// class TComariExport_mask final : public TAutomask { protected: bool on_field_event(TOperable_field& o, TField_event e, long jolly) override; void load_all(); public: void save_all() const; TComariExport_mask(); ~TComariExport_mask() { save_all(); } }; bool TComariExport_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { return true; } TComariExport_mask::TComariExport_mask() : TAutomask("ps6215100a") { load_all(); } void TComariExport_mask::save_all() const { ini_set_string(CONFIG_DITTA, "ps6215", "flddest", get(F_FLDDEST)); ini_set_string(CONFIG_DITTA, "ps6215", "clifor_bool", get(F_CLIFOR)); } void TComariExport_mask::load_all() { TFilename temp; temp.tempdir(); set(F_FLDDEST, ini_get_string(CONFIG_DITTA, "ps6215", "flddest", temp)); set(F_CLIFOR, ini_get_string(CONFIG_DITTA, "ps6215", "clifor_bool", "X")); } /////////////////////////////////////////////////////////// // Main Program /////////////////////////////////////////////////////////// class TComariExport_app : public TSkeleton_application { TString _fld_dest; bool export_all() const; static void add_to_record(char* record, TString& str, int len, int& index); static void export_clifo(ofstream& fout); static void export_parti(ofstream& fout); bool export_table(short id) const; public: bool create() override; void main_loop() override; TComariExport_app() = default; }; bool TComariExport_app::export_all() const { return export_table(F_CLIFOR) && export_table(F_PARTOPEN) && export_table(F_PIANOCONTI) && export_table(F_MASTRI); } void TComariExport_app::add_to_record(char* record, TString& str, int len, int& index) { trim_n(str, len); copy_wzero(record + index, CLIFO_RECLEN - index, str); index += len; } void TComariExport_app::export_clifo(ofstream& fout) { int index = 0; TLocalisamfile clifo(LF_CLIFO); bool ok = clifo.read(_isfirst) == NOERR; int items = clifo.items(); TProgress_monitor bar(items, "Esportazione Tabella Clienti Fornitori"); for(; ok; ok = clifo.next() == NOERR) { if (!bar.add_status()) break; char record[CLIFO_RECLEN]; memset(record, ' ', CLIFO_RECLEN); index = 0; TLocalisamfile comuni(LF_COMUNI), cfven(LF_CFVEN); comuni.put(COM_STATO, clifo.get(CLI_STATOCF)); comuni.put(COM_COM, clifo.get(CLI_COMCF)); comuni.read(); cfven.put(CFV_CODCF, clifo.get(CLI_CODCF)); cfven.put(CFV_TIPOCF, clifo.get(CLI_TIPOCF)); cfven.read(); // Tipo: TString a = clifo.get(CLI_TIPOCF); if(a[0] != 'C' && a != 'F') a[0] = 'C'; add_to_record(record, a, 1, index); // Codcf: a = clifo.get(CLI_CODCF); add_to_record(record, a, 15, index); // Descrizione: a = clifo.get(CLI_RAGSOC); a.rpad(50); TString b = a.sleft(30); add_to_record(record, b, 40, index); // Descrizione2: b = a.sright(20); add_to_record(record, b, 40, index); // Indirizzo: a = clifo.get(CLI_INDCF); a << ", " << clifo.get(CLI_CIVCF); add_to_record(record, a, 35, index); // Indirizzo2: a = ""; add_to_record(record, a, 35, index); // CAP: a = clifo.get(CLI_CAPCF); add_to_record(record, a, 5, index); // Localita': a = clifo.get(CLI_LOCCF); add_to_record(record, a, 30, index); // Provincia: a = comuni.get(COM_PROVCOM); add_to_record(record, a, 2, index); // Telefono: a = clifo.get(CLI_PTEL); a << clifo.get(CLI_TEL); add_to_record(record, a, 18, index); // Cellulare: a = clifo.get(CLI_PTEL2); a << clifo.get(CLI_TEL2); add_to_record(record, a, 18, index); // Fax: a = clifo.get(CLI_PFAX); a << clifo.get(CLI_FAX); add_to_record(record, a, 18, index); // Persona fisica: a = clifo.get(CLI_TIPOPERS)[0] == 'F' ? "S" : "N"; add_to_record(record, a, 1, index); // Sesso: a = clifo.get(CLI_SESSO); add_to_record(record, a, 1, index); // Nascita: a = clifo.get_date(CLI_DATANASC).date2ansi(); add_to_record(record, a, 8, index); // Luogo di nascita: comuni.zero(); comuni.put(COM_STATO, clifo.get(CLI_STATONASC)); comuni.put(COM_COM, clifo.get(CLI_COMNASC)); comuni.read(); a = comuni.get(COM_DENCOM); add_to_record(record, a, 30, index); // Provincia Nascita: a = comuni.get(COM_PROVCOM); add_to_record(record, a, 2, index); // Codice fiscale: a = clifo.get(CLI_COFI); add_to_record(record, a, 16, index); // Partita IVA: a = clifo.get(CLI_PAIV); add_to_record(record, a, 12, index); // Mastro: a = clifo.get(CLI_GRUPPO); a << clifo.get(CLI_CONTO); add_to_record(record, a, 20, index); // Partite: a = " "; add_to_record(record, a, 1, index); // Codice pagamento: a = clifo.get(CLI_CODPAG); add_to_record(record, a, 5, index); // Codice zona: a = cfven.get(CFV_CODZONA); add_to_record(record, a, 5, index); // Codice agente1: a = cfven.get(CFV_CODAG1); add_to_record(record, a, 5, index); // Codice Iva: a = cfven.get(CFV_ASSFIS); add_to_record(record, a, 5, index); // Codice Valuta: a = clifo.get(CLI_CODVAL); add_to_record(record, a, 5, index); // Codice Banca: a = clifo.get(CLI_CODABI); a << clifo.get(CLI_CODCAB); add_to_record(record, a, 10, index); // Primo mese: a = " "; add_to_record(record, a, 2, index); // Secondo mese: a = " "; add_to_record(record, a, 2, index); // Primo gg scadenze: a = " "; add_to_record(record, a, 2, index); // Secondo gg: a = " "; add_to_record(record, a, 2, index); // Codice Lingua: a = clifo.get(CLI_CODLIN); add_to_record(record, a, 5, index); // Conto corrente: a = clifo.get(CLI_NUMCC); add_to_record(record, a, 15, index); // Indirizzo e-mail: a = clifo.get(CLI_DOCMAIL); add_to_record(record, a, 50, index); // Ritenute: a = "N"; add_to_record(record, a, 1, index); // Codice tributo: a = " "; add_to_record(record, a, 4, index); // Codice tributo: a = " "; add_to_record(record, a, 4, index); // Categoria contabile : a = " "; add_to_record(record, a, 5, index); // Annotazioni: a = " "; add_to_record(record, a, 255, index); fout << record << '\n'; } } void TComariExport_app::export_parti(ofstream& fout) { TString4 last_game = ini_get_string(CONFIG_DITTA, "sc", "last_year_open_game", "2019"); const string year = (const char*)last_game; const int y = stol(year); if (y < 2000 || y > 2029) last_game = "2019"; ini_set_string(CONFIG_DITTA, "sc", "last_game", last_game); TString sql; sql << "SELECT ANNO, NUMPART, NRIGA, TIPOC, SOTTOCONTO, TIPOMOV, TIPOPAG, NREG, "\ "NUMRIG, DATAREG, DATADOC, DATAPAG, NUMDOC, REG, PROTIVA, CODCAUS, SEZ, IMPORTO, IMPOSTA, "\ "SPESE, IMPTOTDOC, RITENUTE, RITSOC, SEZABB, ABBUONI, SEZDIFCAM, DIFFCAM, GRUPPOCL, CONTOCL \n" << "FROM part WHERE CHIUSA<>'X' AND ANNO <=" << last_game << ";"; TSQL_recordset openpart(sql); if (!openpart.items()) message_box("Non ci sono partite aperte al %s da esportare.", (const char*)last_game); else if(openpart.move_first()) { TProgress_monitor bar(openpart.items(), "Esportazione Partite Aperte"); for(bool ok = true; ok; ok = openpart.move_next()) { if (!bar.add_status()) break; TFixed_record rec((int)(sizeof dim_fields / sizeof *dim_fields), dim_fields); for (int i = 0; i < (int)(sizeof dim_fields / sizeof *dim_fields); ++i) { if (openpart.column_info(i)._type == _datefld) { TString ansi; ansi << TDate(openpart.get(i).as_date()).date2ansi(); if(ansi != "0") rec.set_str(i, ansi); } else if(i >= 17 && i <= 22 || i == 24 || i == 26) // reali (non so perche' ma becca come reali anche gli interi...) rec.set_str(i, TString(openpart.get(i).as_real().string(18, 3, ' '))); else rec.set_str(i, openpart.get(i).as_string()); } fout << rec.get_line() << endl; } } else warning_box(TR("Impossibile leggere file partite aperte.")); } bool TComariExport_app::export_table(const short id) const { bool ok = false; ofstream fout; TString path = _fld_dest; const char* name, *name_file; switch (id) { default: case F_CLIFOR: name_file = "clifo.txt"; path << "\\" << name_file; fout.open(path, ios_base::out); if ((ok = fout.is_open())) { export_clifo(fout); fout.close(); } name = "Clienti Fornitori"; break; case F_PARTOPEN: name_file = "partiteaperte.txt"; path << "\\" << name_file; fout.open(path, ios_base::out); if ((ok = fout.is_open())) { export_parti(fout); fout.close(); } name = "Clienti Fornitori"; break; case F_PIANOCONTI: name_file = "pianoconti.txt"; path << "\\" << name_file; ok = true; name = "Piano dei Conti"; break; case F_MASTRI: name_file = "mastri.txt"; path << "\\" << name_file; ok = true; name = "Mastri"; break; } TString msg; msg << "Esportazione " << name << (ok ? " " : " non ") << "completata.\nFile: " << (ok ? path : name_file); if (ok) message_box(msg); else warning_box(msg); return ok; } bool TComariExport_app::create() { open_files(LF_CLIFO, LF_CFVEN, LF_COMUNI, 0); return TSkeleton_application::create(); } void TComariExport_app::main_loop() { const TFixed_string arg = argv(2); if(arg.starts_with("-a") || arg.starts_with("-A")) { const WINDOW task = TASK_WIN; xvt_vobj_set_visible(task, FALSE); export_all(); } else { TComariExport_mask msk; while (msk.run() == K_ENTER) { bool ok = false; _fld_dest = msk.get(F_FLDDEST); for (short i = F_CLIFOR; i <= F_MASTRI; ++i) { if (msk.get_bool(i)) export_table(i); } } } } int ps6215100(const int argc, char* argv[]) { TComariExport_app pe; pe.run(argc, argv, TR("Esportazione CO.MA.RI.")); return 0; }