#include #include #include #include #include #include #include "ve4.h" #include "ve4100.h" /////////////////////////////////////////////////////////// // Ciclo principale di elaborazione /////////////////////////////////////////////////////////// bool elabora(TCursor& cur, TString_array& var) { TViswin vw("ve4100.txt", "Ricalcolo valori", FALSE, TRUE, FALSE, 3, 3, -3, -3, FALSE); for (cur = 0; cur.ok(); ++cur) { } return TRUE; } /////////////////////////////////////////////////////////// // Maschera ricalcolo di magazzino /////////////////////////////////////////////////////////// class TRicalcolo_mask : public TMask { TArray _rel, _des; int _cur_file; TToken_string _key_expr; protected: static bool file_handler (TMask_field& f, KEY k); static bool key_handler (TMask_field& f, KEY k); static bool filter_handler(TMask_field& f, KEY k); static bool field_handler (TMask_field& f, KEY k); static bool fromto_handler(TMask_field& f, KEY k); public: TRelation& get_rel() const; const TRectype& get_rec() const; const RecDes* get_rec_des() const; TToken_string& get_key_expr(int k); TRicalcolo_mask(); virtual ~TRicalcolo_mask() { } }; TRicalcolo_mask::TRicalcolo_mask() : TMask("ve4100"), _cur_file(0) { const int lf[] = { LF_ANAMAG, LF_CODCORR, LF_UMART, LF_DESLIN, LF_CONDV, LF_RCONDV, 0 }; for (int i = 0; lf[i]; i++) { TRelation* rel = new TRelation(lf[i]); _rel.add(rel); _des.add(new TRelation_description(*rel)); } set_handler(F_FILE, file_handler); set_handler(F_KEY, key_handler); set_handler(F_FILTER, filter_handler); TSheet_field& fields = (TSheet_field&)field(F_FIELDS); TMask& fsm = fields.sheet_mask(); fsm.set_handler(F_FIELD, field_handler); fsm.set_handler(F_FROM, fromto_handler); fsm.set_handler(F_TO, fromto_handler); TSheet_field& outputs = (TSheet_field&)field(F_OUTPUTS); TMask& osm = outputs.sheet_mask(); osm.set_handler(F_FIELD, field_handler); outputs.row(-1); // Aggiunge comunque una riga vuota; osm.force_update(); } TRelation& TRicalcolo_mask::get_rel() const { return (TRelation&)_rel[_cur_file]; } const TRectype& TRicalcolo_mask::get_rec() const { return get_rel().lfile().curr(); } const RecDes* TRicalcolo_mask::get_rec_des() const { return get_rec().rec_des(); } TToken_string& TRicalcolo_mask::get_key_expr(int key) { _key_expr.cut(0); if (key > 0) { const RecDes* rd = get_rec_des(); const KeyDes& kd = rd->Ky[key-1]; for (int i = 0; i < kd.NkFields; i++) { const int nf = kd.FieldSeq[i] % MaxFields; const RecFieldDes& rf = rd->Fd[nf]; _key_expr.add(rf.Name); } } return _key_expr; } bool TRicalcolo_mask::file_handler(TMask_field& f, KEY k) { TRicalcolo_mask& m = (TRicalcolo_mask&)f.mask(); if (k == K_SPACE) { const int n = atoi(f.get()); if (n != m._cur_file || !m.is_running()) { m._cur_file = n; const RecDes* rd = m.get_rec_des(); const int keys = rd->NKeys; TToken_string codes(16); TToken_string values(80); TString16 tmp; codes.add(0); values.add("Nessuna"); for (int k = 1; k <= keys; k++) { codes.add(k); tmp.format("Chiave %d", k); values.add(tmp); } TList_field& lk = (TList_field&)m.field(F_KEY); lk.replace_items(codes, values); if (atoi(lk.get()) != 0) lk.reset(); } } return TRUE; } bool TRicalcolo_mask::key_handler(TMask_field& f, KEY k) { if (k == K_SPACE) { TRicalcolo_mask& m = (TRicalcolo_mask&)f.mask(); const int cur_key = atoi(f.get()); TToken_string& key_expr = m.get_key_expr(cur_key); const bool empty = key_expr.empty_items(); TSheet_field& sheet = (TSheet_field&)m.field(F_FIELDS); TString_array& sa = sheet.rows_array(); sa.destroy(); if (empty) { for (int i = 4; i >= 0; i--) sa.add(""); } else { TRelation_description& des = (TRelation_description&)m._des[m._cur_file]; for (TString field = key_expr.get(0); field.not_empty(); field = key_expr.get()) { const int r = sa.add(field); sa.row(r).add(des.get_field_description(field), F_DESCR-F_FIELD); } } sheet.force_update(); } return TRUE; } bool TRicalcolo_mask::filter_handler(TMask_field& f, KEY k) { bool ok = TRUE; if (f.to_check(k)) { TString filter(f.get()); filter.strip("\n"); TExpression expr(_strexpr); ok = expr.set(filter, _strexpr); if (ok) { TRicalcolo_mask& m = (TRicalcolo_mask&)f.mask(); const TRectype& rec = m.get_rec(); TString var; for (int v = expr.numvar()-1; v >= 0; v--) { var = expr.varname(v); var.upper(); ok = rec.exist(var); if (!ok) { f.error_box("Il campo '%s' non esiste!", (const char*)var); break; } } } else f.error_box("Simbolo non riconosciuto: '%s'", expr.last_compiled_token()); } return ok; } bool TRicalcolo_mask::field_handler(TMask_field& f, KEY k) { bool ok = TRUE; switch (k) { case K_TAB: case K_ENTER: if (!f.empty() && f.to_check(k)) { TRicalcolo_mask& m = (TRicalcolo_mask&)f.mask().get_sheet()->mask(); const TRectype& rec = m.get_rec(); const char* field = f.get(); if (!rec.exist(field)) ok = f.error_box("Il campo %s non esiste!", field); } break; case K_F9: { TMask& m = f.mask(); TRicalcolo_mask& rm = (TRicalcolo_mask&)m.get_sheet()->mask(); TRelation_description& rd = (TRelation_description&)rm._des[rm._cur_file]; if (rd.choose_field(f.get())) { f.set(rd.field_name()); m.set(F_DESCR, rd.field_desc()); } } break; default: break; } return ok; } bool TRicalcolo_mask::fromto_handler(TMask_field& f, KEY k) { bool ok = TRUE; if (f.to_check(k)) { const TMask& m = f.mask(); const TString& field = m.get(F_FIELD); if (field.not_empty()) { TRicalcolo_mask& rm = (TRicalcolo_mask&)m.get_sheet()->mask(); const TRectype& rec = rm.get_rec(); const int max = rec.length(field); if (f.get().len() > max) { TString msg(32); msg = "Inserire al massimo "; if (max > 1) msg << max; else msg << "un"; msg << " caratter" << (max > 1 ? 'i' : 'e') << '.'; ok = f.error_box(msg); } } } return ok; } /////////////////////////////////////////////////////////// // Ricalcolo di magazzino /////////////////////////////////////////////////////////// class TRicalcola_application : public TApplication { virtual bool menu(MENU_TAG); virtual bool create(); public: TRicalcola_application() { } virtual ~TRicalcola_application() { } }; bool TRicalcola_application::create() { dispatch_e_menu(MENU_ITEM(1)); return TRUE; } bool TRicalcola_application::menu(MENU_TAG) { TRicalcolo_mask m; while (m.run() == K_ENTER) { TString filter = m.get(F_FILTER); filter.strip("\n"); int key = m.get_int(F_KEY); TRectype start(m.get_rec()), stop(m.get_rec()); start.zero(); stop.zero(); TToken_string& key_des = m.get_key_expr(key); TSheet_field& fs = (TSheet_field&)m.field(F_FIELDS); TString field, val; for (int r = 0; r < fs.items(); r++) { TToken_string& row = fs.row(r); field = row.get(0); if (field.not_empty()) { if (key > 0 && key_des.get_pos(field) >= 0) { val = row.get(); if (val.not_empty()) start.put(field, val); val = row.get(); if (val.not_empty()) stop.put(field, val); } else { val = row.get(); if (val.not_empty()) { if (filter.not_empty()) filter << "&&"; filter << '(' << field << ">=" << val << ')'; } val = row.get(); if (val.not_empty()) { if (filter.not_empty()) filter << "&&"; filter << '(' << field << "<=" << val << ')'; } } } } if (key <= 0) key = 1; TSheet_field& osf = (TSheet_field&)m.field(F_OUTPUTS); TCursor cur(&m.get_rel(), filter, key, start.empty() ? NULL : &start, stop.empty() ? NULL : &stop); elabora(cur, osf.rows_array()); } return FALSE; } int ve4100(int argc, char* argv[]) { TRicalcola_application r; r.run(argc, argv, "Ricalcolo valori"); return 0; }