#include #include #include #include #include #include #include "velib.h" #include "vereplib.h" #include "../ba/ba8400.h" /////////////////////////////////////////////////////////// // Utility /////////////////////////////////////////////////////////// static bool cod2app(const char* tok, TString& app) { if (isalpha(tok[0]) && isalpha(tok[1]) && atoi(tok+2) > 1000) { app.strncpy(tok, 3); app << " -" << char(tok[3]-1); return true; } return false; } static bool get_xml_attr(const TString& line, const char* attr, TString& value) { TString str; str << ' ' << attr << "=\""; const int pos = line.find(str); if (pos >= 0) { const int apicia = line.find('"', pos)+1; const int apicic = line.find('"', apicia); if (apicic > apicia) { value = line.sub(apicia, apicic); return true; } } return false; } static bool get_xml_child(const TString& line, const char* tag, TString& value) { TString str; str << '<' << tag << '>'; const int pos = line.find(str); if (pos >= 0) { const int apicia = line.find('>', pos)+1; const int apicic = line.find('<', apicia); if (apicic > apicia) { value = line.sub(apicia, apicic); return true; } } return false; } bool rep2app(const char* name, TString& app, TString& desc) { app = desc = ""; TFilename report_name(name); report_name.ext("rep"); if (!report_name.custom_path()) return false; TToken_string libraries(50, ','); TString stringona; TScanner scan(report_name); for (int i = 0; i < 3 && scan.good(); i++) // Leggo solo le prime righe stringona << scan.line(); get_xml_attr(stringona, "libraries", libraries); get_xml_child(stringona, "description", desc); FOR_EACH_TOKEN(libraries, tok) if (strlen(tok) == 6) { if (cod2app(tok, app)) return true; } cod2app(report_name.name(), app); return true; } /////////////////////////////////////////////////////////// // TDocument_cache /////////////////////////////////////////////////////////// class TDocument_cache : TCache { protected: virtual TObject* key2obj(const char* key); public: TDocumento& doc(const TRectype& rec); TDocument_cache() : TCache(23) { } virtual ~TDocument_cache() { } }; TObject* TDocument_cache::key2obj(const char* key) { TToken_string k(key); const char provv = *k.get(0); const int anno = k.get_int(); const TString4 codnum= k.get(); const long ndoc = k.get_long(); return new TDocumento(provv, anno, codnum, ndoc); } TDocumento& TDocument_cache::doc(const TRectype& rec) { TToken_string key; key = rec.get(DOC_PROVV); key.add(rec.get(DOC_ANNO)); key.add(rec.get(DOC_CODNUM)); key.add(rec.get(DOC_NDOC)); TDocumento& d = *(TDocumento*)objptr(key); return d; } /////////////////////////////////////////////////////////// // TDocument_recordset /////////////////////////////////////////////////////////// class TDocument_recordset : public TISAM_recordset { TDocument_cache _cache; protected: virtual const TVariant& get(int logic, const char* field) const; public: TDocument_recordset(const char* use) : TISAM_recordset(use) { } virtual ~TDocument_recordset() { } }; const TVariant& TDocument_recordset::get(int num, const char* field) const { const int idx = relation()->log2ind(num); if (idx < 0) return NULL_VARIANT; const TRectype& rec = relation()->file(idx).curr(); const int logic = rec.num(); if (logic == LF_DOC || logic == LF_RIGHEDOC) { // Se non e' un campo standard, ma e' calcolato da una formula... if (rec.type(field) == _nullfld) { const TDocumento& doc = ((TDocument_cache&)_cache).doc(rec); TVariant& var = get_tmp_var(); const TFieldref ref(field, logic); if (logic == LF_DOC) { var = ref.read(doc); } else { const int nriga = rec.get_int(RDOC_NRIGA); const TRiga_documento& rdoc = doc[nriga]; var = ref.read(rdoc); } return var; } } return TISAM_recordset::get(num, field); } /////////////////////////////////////////////////////////// // TDocument_report /////////////////////////////////////////////////////////// class TDocument_report : public TReport { protected: virtual bool set_recordset(const TString& query); public: bool load(const char* name); }; bool TDocument_report::set_recordset(const TString& query) { return TReport::set_recordset(new TDocument_recordset(query)); } bool TDocument_report::load(const char* name) { const bool ok = TReport::load(name); if (ok) { // Purtroppo il recordset delle sottosezioni deve essere reimpostato a mano for (int i = 11; i <= 999; i++) { TReport_section* sec = find_section('B', i); if (sec != NULL) { TRecordset* recset = sec->recordset(); if (recset != NULL) { const TString use = recset->query_text(); recset = new TDocument_recordset(use); sec->set_recordset(recset); } } } } return ok; } /////////////////////////////////////////////////////////// // TKlarkKent_mask /////////////////////////////////////////////////////////// class TKlarkKent_mask : public TAutomask { protected: virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly); public: TKlarkKent_mask() : TAutomask("ba8500a") { } }; bool TKlarkKent_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { switch (o.dlg()) { case F_REPORT: if (e == fe_button) { TArray_sheet a(-1, -1, 75, 20, "Report", "Nome@16|Progr.|Descrizione@50"); TString_array& arr = a.rows_array(); list_files("*.rep", arr); TFilename dir = firm2dir(-1); dir.add("custom"); dir.add("*.rep"); list_files(dir, arr); TString app, desc; FOR_EACH_ARRAY_ROW(arr, i, row) { rep2app(*row, app, desc); TFilename n = *row; n = n.name(); n.ext(""); *row = n; row->add(app); row->add(desc); } if (a.run() == K_ENTER) { o.set(a.row(-1).get(0)); e = fe_modify; } } if (e == fe_init || e == fe_modify) { TFilename name = o.get(); TString app, desc; const bool ok = rep2app(name, app, desc); set(F_FORM, desc); set(F_DOC, app); enable(DLG_PRINT, ok); } break; case DLG_EDIT: if (e == fe_button && !field(F_REPORT).empty()) { TString str; str << "ba8 -2 " << get(F_REPORT); TExternal_app app(str); app.run(true); } break; default: break; } return true; } /////////////////////////////////////////////////////////// // TKlarkKent_app /////////////////////////////////////////////////////////// class TKlarkKent_app : public TSkeleton_application { protected: virtual void main_loop(); }; void TKlarkKent_app::main_loop() { TString_array arr; for (int i = 2; i < argc(); i++) { TString name = argv(i); if (name[0] == '%') name.ltrim(1); if (name.len() == 3) //il file in questione e' la stampa di una tabella? { TFilename filename(name); filename.insert("vest"); filename.ext("rep"); if (filename.custom_path()) name = filename.name(); } name.lower(); arr.add(name); } if (arr.items() == 0) arr.add(EMPTY_STRING); bool can_repeat = false; do { TReport_book book; FOR_EACH_ARRAY_ROW(arr, r, row) { TFilename report_name = *row; report_name.ext("rep"); if (row->blank() || !report_name.custom_path()) { TKlarkKent_mask m; m.set(F_REPORT, report_name); if (m.run() == K_ENTER) { report_name = m.get(F_REPORT); report_name.ext("rep"); report_name.custom_path(); *row = report_name; } } TString appname, desc; rep2app(report_name, appname, desc); if (appname.not_empty()) { appname << ' ' << report_name; TExternal_app app(appname); app.run(true); } else { TDocument_report rep; if (rep.load(report_name)) { const bool ok = book.add(rep); if (ok && arr.items() == 1) // Controlla se e' pensabile ripetere la stampa { TFilename msk = report_name; msk.ext("msk"); can_repeat = msk.exist(); // Posso ripetere se ho una maschera collegata } else can_repeat = false; } } } if (book.pages() > 0) book.print_or_preview(); } while (can_repeat); } int ve1400(int argc, char* argv[]) { TKlarkKent_app app; app.run(argc, argv, TR("Stampa Report Vendite")); return 0; }