#include #include #include #include #include #include #include #include #include #include #include #include #include #include "at0.h" // nomi campi maschera #include "at0200a.h" // nomi dei campi #include "soggetti.h" #include "donaz.h" #include "contsan.h" #include "rconvoc.h" #include "atlib.h" #define ALIAS_CTD 100 class TGiornalieroDC : public TApplication { TMask* _msk; TRelation* _rel; TLocalisamfile* _soggetti; TLocalisamfile* _donaz; TLocalisamfile* _contsan; TLocalisamfile* _rconvoc; TRecord_array* _sdonazioni; TRecord_array* _scontrolli; TAssoc_array* _asoggetti; // array dei soggetti inseriti per controllare doppio ins. bool _modified; long _progins; // parametri di sezione int _numdon1, _numdon2; TString16 _catini1, _catfin1, _catini2, _catfin2; bool _sttess2, _dataisc; protected: virtual bool create(); virtual bool destroy(); virtual bool menu(MENU_TAG m); virtual TMask& get_mask() { return *_msk; } virtual TRelation* get_relation() const { return _rel; } TAssoc_array& get_array_sogg() { return *_asoggetti; } int write(TSheet_field& s); bool check_sog_sheet(const char* codsog); static bool nome_handler(TMask_field& f, KEY k); static bool codice_handler(TMask_field& f, KEY k); static bool soggetti_notify(TSheet_field& s, int r, KEY k); void add_rows_soggetti(TSheet_field& s, int count = 20, int start = 0); static bool ins_controlli_handler(TMask_field& f, KEY k); public: TGiornalieroDC() {} }; HIDDEN inline TGiornalieroDC& app() { return (TGiornalieroDC&) main_app(); } bool TGiornalieroDC::ins_controlli_handler(TMask_field& f, KEY k) { if (k == K_SPACE) { TMask& m = f.mask(); TSheet_field& s = (TSheet_field&)m.field(F_SOGGETTI); const bool ins_controlli = m.get_bool(F_INS_CONTROLLI); s.enable_column(3,ins_controlli); } return TRUE; } void TGiornalieroDC::add_rows_soggetti(TSheet_field& s, int count, int start) { if (start == 0) s.destroy(); for (int r=start; r<=start+count-1; r++) s.row(r); } bool TGiornalieroDC::create() { TApplication::create(); _modified = FALSE; _msk = new TMask("at0200a"); _rel = new TRelation(LF_SOGGETTI); _rel->add("CTD", "CODTAB==CATDON",1,0,ALIAS_CTD); _donaz = new TLocalisamfile(LF_DONAZ); _contsan = new TLocalisamfile(LF_CONTSAN); _rconvoc = new TLocalisamfile(LF_RCONVOC); _sdonazioni = new TRecord_array(LF_DONAZ,DON_PROGDON); _scontrolli = new TRecord_array(LF_CONTSAN,CON_PROGCON); _asoggetti = new TAssoc_array(); _msk->set_handler(F_INS_CONTROLLI,ins_controlli_handler); TSheet_field& ss = (TSheet_field&)_msk->field(F_SOGGETTI); ss.set_notify(soggetti_notify); ss.sheet_mask().set_handler(F_S_NOME,nome_handler); ss.sheet_mask().set_handler(F_S_CODICE,codice_handler); TConfig config(CONFIG_STUDIO); _numdon1 = config.get_int("NumDon1"); _numdon2 = config.get_int("NumDon2"); _catini1 = config.get("CatIni1"); _catfin1 = config.get("CatFin1"); _catini2 = config.get("CatIni2"); _catfin2 = config.get("CatFin2"); _sttess2 = config.get_bool("StTess2"); _dataisc = config.get_bool("DataIsc"); _donaz->setkey(3); _donaz->last(); _progins = _donaz->get_long(DON_PROGINS); _donaz->setkey(1); dispatch_e_menu(BAR_ITEM(1)); return TRUE; } bool TGiornalieroDC::destroy() { delete _rel; delete _msk; delete _donaz; delete _contsan; delete _rconvoc; delete _sdonazioni; delete _scontrolli; delete _asoggetti; return TApplication::destroy(); } bool TGiornalieroDC::menu(MENU_TAG m) { TMask& msk = get_mask(); const TDate oggi(TODAY); KEY tasto; do { TSheet_field& s = (TSheet_field&)msk.field(F_SOGGETTI); //s.reset(); if (s.items() == 0) add_rows_soggetti(s); _asoggetti->destroy(); msk.set(F_DATADON,oggi); tasto = msk.run(); switch (tasto) { case K_ESC: // annulla { if (_modified) if (yesno_box("Registrare le modifiche?")) TGiornalieroDC::write(s); _modified = FALSE; msk.reset(); } break; case K_SAVE: // registra { TGiornalieroDC::write(s); _modified = FALSE; msk.reset(); //verificare se va tolta } break; case K_INS: // nuovo { if (_modified) if (yesno_box("Registrare le modifiche?")) TGiornalieroDC::write(s); _modified = FALSE; msk.reset(); } break; case K_QUIT: // fine { if (_modified) if (yesno_box("Registrare le modifiche?")) TGiornalieroDC::write(s); msk.reset(); _modified = FALSE; } break; } } while (tasto != K_QUIT); return FALSE; } int TGiornalieroDC::write(TSheet_field& s) { const TMask& m = s.mask(); const TDate data = m.get(F_DATADON); int items = s.items(); TProgind *pi; pi = new TProgind(items,"Registrazione donazioni e controlli", FALSE, TRUE, 10); pi->setstatus(1); TLocalisamfile& sog = get_relation()->lfile(); TLocalisamfile rconv(LF_RCONVOC); for (int r=1; r<=s.items(); r++) { pi->addstatus(1); TToken_string& row = s.row(r-1); const long codsog = row.get_long(0); if (codsog != 0) { sog.setkey(1); sog.zero(); sog.put(SOG_CODICE, codsog); int err = sog.read(); if (err == NOERR) { const TString16 tipocon = row.get(3); if (!tipocon.blank()) { TRectype* recc = new TRectype(LF_CONTSAN); recc->put(CON_CODICE, row.get(0)); recc->put(CON_DATACON, m.get(F_DATADON)); recc->put(CON_TIPOCON, row.get(3)); bool insert = FALSE; bool exist = FALSE; TRectype* keyc = new TRectype(LF_CONTSAN); keyc->put(CON_CODICE, row.get(0)); int err = _scontrolli->read(keyc); if (err == NOERR) { int r=_scontrolli->rows(); while (r>=1 && !insert && !exist) { const TRectype& riga = _scontrolli->row(r); const TDate d = riga.get(CON_DATACON); if (data==d) // esiste già un controllo in questa data { exist=TRUE; r=0; } else if (data > d) { recc->put(CON_PROGCON,r+1); _scontrolli->insert_row(recc); insert=TRUE; } r--; } if (!exist && !insert) { recc->put(CON_PROGCON,1); _scontrolli->insert_row(recc); } } else if (err == _iseof || err == _isemptyfile) { recc->put(CON_PROGCON,1); _scontrolli->insert_row(recc); } if (!exist) _scontrolli->write(TRUE); } _progins++; TRectype* rec = new TRectype(LF_DONAZ); rec->put(DON_CODICE, row.get(0)); rec->put(DON_DATADON, m.get(F_DATADON)); rec->put(DON_TIPODON, m.get(F_TIPODON)); rec->put(DON_LUOGODON, m.get(F_LUOGODON)); rec->put(DON_ETICHETTA, row.get(4)); rec->put(DON_CODSEZ,sog.get(SOG_CODSEZ)); rec->put(DON_CODSOT,sog.get(SOG_CODSOT)); rec->put(DON_PROGINS,_progins); bool insert = FALSE; bool exist = FALSE; TRectype* key = new TRectype(LF_DONAZ); key->put(DON_CODICE, row.get(0)); err = _sdonazioni->read(key); if (err == NOERR) { int r=_sdonazioni->rows(); while (r>=1 && !insert && !exist) { const TRectype& riga = _sdonazioni->row(r); const TDate d = riga.get(DON_DATADON); if (data==d) // esiste gia' una donazione in questa data { exist=TRUE; r=0; } else if (data > d) { rec->put(DON_PROGDON,r+1); _sdonazioni->insert_row(rec); insert=TRUE; } r--; } if (!exist && !insert) { rec->put(DON_PROGDON,1); _sdonazioni->insert_row(rec); } } else if (err == _iseof || err == _isemptyfile) { rec->put(DON_PROGDON,1); _sdonazioni->insert_row(rec); } if (!exist) { _sdonazioni->write(TRUE); // se ha una convocazione successiva alla donazione, // va cancellata, anche dall'archivio convocazioni TDate dataultdon = sog.get_date(SOG_DATAULTDON); TDate dataconv = sog.get_date(SOG_DATACONV); if (dataultdon >= dataconv) { rconv.setkey(2); rconv.zero(); rconv.put(RCV_DATACONV, dataconv); rconv.put(RCV_CODICE, codsog); int err = rconv.read(); if (err == NOERR) { rconv.put(RCV_ANNULLATO,TRUE); rconv.rewrite(); } sog.put(SOG_DATACONV, NULLDATE); sog.put(SOG_DATAULTSOL, NULLDATE); const int zeroconv = 0; sog.put(SOG_NUMCONV, zeroconv); } // controllo se è idoneo const TString16 tipo = m.get(F_TIPODON); int totdon = sog.get_int(SOG_TOTDON); sog.put(SOG_TOTDON,totdon+1); if (data > dataultdon) { sog.put(SOG_DATAULTDON,data); sog.put(SOG_TIPOULTDON,tipo); } if (is_donaz(tipo,IDON_SI)) { totdon = sog.get_int(SOG_TOTDONSI); sog.put(SOG_TOTDONSI,totdon+1); dataultdon = sog.get_date(SOG_DATAULTSI); if (data>dataultdon) sog.put(SOG_DATAULTSI,data); } if (is_donaz(tipo,IDON_AF)) { totdon = sog.get_int(SOG_TOTDONAF); sog.put(SOG_TOTDONAF,totdon+1); dataultdon = sog.get_date(SOG_DATAULTAF); if (data>dataultdon) sog.put(SOG_DATAULTAF,data); } char modstato = ' '; const TString16 stato = sog.get(SOG_STATO); // stato attuale const TString16 id1 = sog.get(SOG_IDON1); // idon. 1 const TString16 id2 = sog.get(SOG_IDON2); // idon. 2 const TString16 id3 = sog.get(SOG_IDON3); // idon. 3 const TString16 id4 = sog.get(SOG_IDON4); // idon. 4 const int intsi = sog.get_int(SOG_INTSI); // intervallo per SI const int intaf = sog.get_int(SOG_INTAF); // intervallo per AF dataultdon = sog.get_date(SOG_DATAULTDON); // data ultima donazione const TString16 tipoultdon(sog.get(SOG_TIPOULTDON)); // tipo ultima donazione TDate datasi(NULLDATE); // data prossima si calcolata TDate dataaf(NULLDATE); // data prossima af calcolata TDate dataultsi(NULLDATE); // data ultima donazione si bool id_si = FALSE; // il soggetto è idoneo per si? bool id_af = FALSE; // il soggetto è idoneo per af? modstato = modstato_tcs(stato); if (modstato == 'I' || modstato == 'F') // il soggetto è idoneo { id_si = (is_idon(id1,id2,id3,id4,IDON_SI) && intsi != 0); // il soggetto è idoneo SI id_af = (is_idon(id1,id2,id3,id4,IDON_AF) && intaf != 0); // il soggetto è idoneo AF if (dataultdon.ok()) // se ha fatto almeno una donazione { if (is_donaz(tipoultdon,IDON_SI)) // se l'ultima donazione è una SI { if (id_si) { datasi=dataultdon; datasi+=intsi; } if (id_af) { dataaf=dataultdon; dataaf+=intaf; } } if (is_donaz(tipoultdon,IDON_AF)) // se l'ultima donazione è una AF { dataultsi = sog.get_date(SOG_DATAULTSI); if (id_si) { if (intaf != 0) { datasi=dataultdon; datasi+=intaf; } else { datasi=dataultdon; datasi+=intsi; } if (dataultsi.ok()) dataultsi+=intsi; if (dataultsi > datasi) datasi = dataultsi; } if (id_af) { dataaf=dataultdon; dataaf+=intaf; } } } else { if (id_si) datasi=sog.get_date(SOG_DATAULTID); if (id_af) dataaf=sog.get_date(SOG_DATAULTID); } } sog.put(SOG_DATAPROSSI,datasi); sog.put(SOG_DATAPROSAF,dataaf); // controllo la sua categoria // se dimesso diventa della categoria collegata // se non c'e' la categoria collegata ??? TTable ctd("CTD"); TString16 catdon = sog.get(SOG_CATDON); ctd.put("CODTAB",catdon); if (ctd.read() == NOERR) { bool dimissione = ctd.get_bool("B0"); if (dimissione) sog.put(SOG_CATDON,ctd.get("S6")); else { if ((catdon == _catini1 || _catini1.empty()) && (totdon+1>=_numdon1) && _catfin1.not_empty()) sog.put(SOG_CATDON, _catfin1); if ((catdon == _catini2 || _catini2.empty()) && (totdon+1>=_numdon2) && _catfin2.not_empty() && !_sttess2) sog.put(SOG_CATDON, _catfin2); } // per bologna // if (catdon == "06") // sog.put(SOG_CATDON,"04"); } TDate dataisc = sog.get_date(SOG_DATAISC); if (dataisc.empty() && _dataisc) { const TRectype& riga = _sdonazioni->row(1); sog.put(SOG_DATAISC,riga.get(DON_DATADON)); } // aggiorno data e utente ultimo aggiornamento sog.put(SOG_DATAULTAGG,TODAY); sog.put(SOG_UTENULTAGG,user()); sog.rewrite(); } } } } delete pi; return NOERR; } bool TGiornalieroDC::check_sog_sheet(const char* codsog) { TAssoc_array& array_sogg = app().get_array_sogg(); if (array_sogg.is_key(codsog)) return FALSE; else return TRUE; } bool TGiornalieroDC::soggetti_notify(TSheet_field& s, int r, KEY k) { bool ok = TRUE; switch (k) { case K_CTRL+K_DEL: // avvenuta cancellazione riga break; case K_SPACE: // inizio modifica riga break; case K_TAB: // entrata riga { static bool entering = TRUE; if (entering) { entering = FALSE; if ((r == s.items()-1) && (r == s.first_empty())) app().add_rows_soggetti(s,10,r+1); TToken_string& row = s.row(r); if (row.empty_items()) s.select(r); entering = TRUE; } } break; case K_ENTER: // uscita da riga modificata case K_CTRL+K_TAB: // uscita riga { TToken_string& row = s.row(r); if (!row.empty_items()) app()._modified = TRUE; const long codsog = row.get_long(0); if (codsog != 0) for (int i=s.items()-1; i>=0; i--) { if (i != r) { TToken_string& row = s.row(i); if (codsog == row.get_long(0)) return s.sheet_mask().field(F_S_CODICE).error_box("Soggetto gia' inserito"); } } else { const char* cognome = row.get(1); if ((cognome != NULL) && (cognome != "\0")) s.sheet_mask().field(F_S_NOME).set_focusdirty(); //s.set_focus_cell(r,2); } } break; } return ok; } bool TGiornalieroDC::nome_handler(TMask_field& f, KEY k) { bool ok = TRUE; if (f.to_check(k)) { TMask& m = f.mask(); long codsog = m.get_long(F_S_CODICE); if (codsog == 0) f.on_key(K_F9); } return ok; } bool TGiornalieroDC::codice_handler(TMask_field& f, KEY k) { bool ok = TRUE; if (f.to_check(k)) { TMask& m = f.mask(); long codsog = m.get_long(F_S_CODICE); if (codsog != 0) { TLocalisamfile& sog = app().get_relation()->lfile(); sog.setkey(1); sog.zero(); sog.put(SOG_CODICE, codsog); int err = sog.read(); if (err == NOERR) { m.set(F_S_COGNOME, sog.get(SOG_COGNOME)); m.set(F_S_NOME, sog.get(SOG_NOME)); m.set(F_S_DATANASC, sog.get(SOG_DATANASC)); m.set(F_S_TESSAVIS, sog.get(SOG_TESSAVIS)); m.set(F_S_CODSEZ, sog.get(SOG_CODSEZ)); m.set(F_S_CODSOT, sog.get(SOG_CODSOT)); m.set(F_S_CATDON, sog.get(SOG_CATDON)); } else ok = FALSE; // codice non esistente } } return ok; } int at0200(int argc, char* argv[]) { TGiornalieroDC a; a.run(argc, argv, "Giornaliero donazioni/controlli"); return 0; }