#include "sc2.h" #include "sc2402.h" #include "sc2102.h" #include "sc2400a.h" #include #include #include #include #include /////////////////////////////////////////////////////////// // Stampa solleciti /////////////////////////////////////////////////////////// class TStampaSol_application: public TSkeleton_application { TString _lingua_ditta; TSol_mask *_msk; bool _gesval; TSol_form *_form; protected: virtual bool create(); virtual bool destroy(); virtual void main_loop(); virtual void on_firm_change(); bool get_mail_address(TToken_string& to, TToken_string& cc) const; virtual bool get_next_mail(TToken_string& to, TToken_string& cc, TToken_string& ccn, TString& subj, TString& text, TToken_string& attach, short& ui) const; int print_sol(); // stampa l'elemento corrente public: TSol_mask &mask() const { return *_msk; } TSol_form &form() const { return *_form; } TCursor_sheet &sheet() { return _msk->cur_sheet(); } bool print_selected(); // cicla la stampa sugli elementi selezionati bool mail_selected(); // manda email agli elementi selezionati TStampaSol_application(); virtual ~TStampaSol_application() {} }; bool TStampaSol_application::print_selected() { TCursor_sheet &s = sheet(); TCursor &c = *s.cursor(); const char who = mask().get_who(); const int key = mask().get_key(); // Attiva la stampa del saldo partita form().stampa_saldo(mask().stampa_saldo()); // filtra il cursore del form in modo che diventi uguale al cursor_sheet corrente // Qui sarebbe bello copiarsi l'indice dell'altro cursore TCursor &fc = *form().cursor(); fc.setkey(key); TRectype filter(LF_CLIFO); filter.put(CLI_TIPOCF, who); fc.setregion(filter, filter); const long print_all = !s.one_checked(); // se non ho selezionato nulla allora li stampo tutti long analfabeti = 0; // persone non stampate in quanto aventi lingua errata printer().open(); const long items = c.items(); for (long i=0; i < items; i++) { if (print_all || s.checked(i)) { fc = i; // muove il cursore alla posizione corrente const int ret = print_sol(); if (ret < 0) analfabeti++; } if (printer().frozen()) break; } printer().close(); if (analfabeti > 0) warning_box(FR("%ld clienti non sono stati stampati in quanto " "il codice lingua non corrispondeva al profilo di stampa"), analfabeti); return TRUE; } int TStampaSol_application::print_sol() { TSol_form& f = form(); // preparazione variabili per controllo lingua const TRectype &clf= f.cursor()->file().curr(); const TString lincf(clf.get(CLI_CODLIN)); bool ok= TRUE; // controllo lingua ditta corrente if ((f.lingua() == _lingua_ditta && !lincf.empty()) || f.lingua() != _lingua_ditta) ok= (lincf == f.lingua()); if (!ok) return -1; // cliente analfabeta f.azzera_totali(); // filtra solo le partite del cliente selezionato TLocalisamfile partite(LF_PARTITE); partite.zero(); partite.put(PART_TIPOCF, clf.get(CLI_TIPOCF)); partite.put(PART_SOTTOCONTO, clf.get(CLI_CODCF)); const TRectype filter(partite.curr()); bool one_printed = FALSE; // booleano di controllo di riuscita della stampa const bool sel_tot_saldo = f.get_sel_tot_saldo(); // selezione sul saldo totale cliente const real sel_importo(f.get_sel_importo()); // importo di selezione const TDate data_limite_soll(f.data_limite_operazione()); const TDate data_limite_scad(f.data_limite_scaduto()); if (sel_tot_saldo && sel_importo > ZERO) { real saldo; for (int err = partite.read(_isgteq); err == NOERR && partite.curr() == filter; err = partite.read(_isgreat)) { TPartita game(partite.curr()); saldo += game.calcola_scaduto_al(FALSE, data_limite_soll); // Era data_limite_scad! partite.put(PART_NRIGA, 9999); } if (saldo < sel_importo) return 0; partite.curr() = filter; } for (int err = partite.read(_isgteq); err == NOERR && partite.curr() == filter; err = partite.read(_isgreat)) { TPartita game(partite.curr()); const real saldo = game.calcola_scaduto_al(FALSE, data_limite_scad); TImporto unreferenced; //Totale non assegnati per questa partita. { // E' giusto calcolare il saldo, comprendente i non assegnati. // se il saldo della partita chiude in avere va sommato ad unreferenced const TRiga_partite& sum = game.riga(game.first()); unreferenced = game.calcola_saldo_al(game.in_valuta() && f.in_valuta(),data_limite_soll, data_limite_scad, data_limite_scad); if (unreferenced.valore() > ZERO && unreferenced.sezione() == 'A') { unreferenced.normalize('A'); //per i non assegnati/anticipi c'e' solo la colonna AVERE const TString8 valuta = sum.get(PART_CODVAL); form().totali().add(unreferenced,valuta); } } if (sel_tot_saldo || (saldo > ZERO && saldo >= sel_importo )) { const bool printed = form().print_game(game); one_printed |= printed; } partite.put(PART_NRIGA, 9999); if (printer().frozen()) break; } if (one_printed) { f.ultima_pagina(); printer().formfeed(); } return one_printed ? 1 : 0; } bool TStampaSol_application::get_mail_address(TToken_string& to, TToken_string& cc) const { const TRectype& fc = form().cursor()->curr(); const long codcf = fc.get_long(CLI_CODCF); TString8 clifo; clifo.format("C%06ld", codcf); TISAM_recordset contacts("USE MULTIREL\nFROM COD=BACON FIRST=#CLIFO\nTO COD=BACON FIRST=#CLIFO"); contacts.set_var("#CLIFO", clifo); TToken_string data; for (bool ok = contacts.move_first(); ok; ok = contacts.move_next()) { data = contacts.get("DATA").as_string(); FOR_EACH_TOKEN(data, tok) { const TFixed_string doc(tok); if (doc.starts_with("sc2400", true) || doc.starts_with("sollec", true)) { const TRectype& rub = cache().get(LF_CONTACT, contacts.get("SECOND").as_int()); TString80 mail = rub.get("MAIL"); if (mail.blank()) mail = rub.get("MAIL2"); if (mail.full()) { if (to.blank()) to = mail; else cc.add(mail); break; } } } } if (to.blank()) { TString8 key; key << "C|" << codcf; to = cache().get(LF_CLIFO, key, CLI_DOCMAIL); } return to.full(); } bool TStampaSol_application::get_next_mail(TToken_string& to, TToken_string& cc, TToken_string& ccn, TString& subj, TString& text, TToken_string& attach, short& ui) const { bool ok = TApplication::get_next_mail(to, cc, ccn, subj, text, attach, ui) && get_mail_address(to, cc); if (ok) { const TRectype& clifo = form().cursor()->curr(); const TDate oggi = mask().get(F_DATASEND); subj << TR("Sollecito ") << prefix().firm().ragione_sociale(); text << "Spett. " << clifo.get(CLI_RAGSOC) << '\n'; text << TR("Si sollecita il rispetto delle scadenze aperte al ") << oggi << TR(" riepilogate nel file ") << attach << "\n" << prefix().firm().ragione_sociale(); if (to.full()) ui &= ~0x1; // No user interface ui |= 0x2; // Query receipt const long codcf = clifo.get_long(CLI_CODCF); TFilename pdf; ok = get_next_pdf(oggi.year(), -1, "SOLL", oggi.date2ansi(), codcf, pdf); attach = pdf; } return ok; } struct TMail_message : public TObject { TToken_string _to, _cc, _ccn; TString _subj, _text; TToken_string _attach; short _ui; }; bool TStampaSol_application::mail_selected() { TCursor_sheet &s = sheet(); TCursor &c = *s.cursor(); const char who = mask().get_who(); const int key = mask().get_key(); // Attiva la stampa del saldo partita form().stampa_saldo(mask().stampa_saldo()); // filtra il cursore del form in modo che diventi uguale al cursor_sheet corrente // Qui sarebbe bello copiarsi l'indice dell'altro cursore TCursor &fc = *form().cursor(); fc.setkey(key); TRectype filter(LF_CLIFO); filter.put(CLI_TIPOCF, who); fc.setregion(filter, filter); const long print_all = !s.one_checked(); // se non ho selezionato nulla allora li stampo tutti TLog_report log; TArray mail; const long items = c.items(); if (items > 0) { const TDate oggi(TODAY); TProgind pi(items); for (long i=0; i < items; i++) { if (!pi.addstatus(1)) break; if (print_all || s.checked(i)) { fc = i; // muove il cursore alla posizione corrente printer().set_printtype(exportprinter); printer().open(); const int ret = print_sol(); printer().close(); if (ret > 0) { TString msg; msg << fc.curr().get(CLI_RAGSOC) << ": "; msg.strip_double_spaces(); TMail_message* m = new TMail_message; bool done = false; if (get_next_mail(m->_to, m->_cc, m->_ccn, m->_subj, m->_text, m->_attach, m->_ui)) { const TFilename fn = m->_attach; done = printer().print_pdf(printer().get_txt(), fn); if (done) { msg << TR("invio ") << fn.name() << TR(" a ") << m->_to; log.log(0, msg); mail.add(m); } else { msg << TR("Impossibile genereare ") << fn; log.log(2, msg); } } else { msg << TR("Impossibile trovare un indirizzo e-mail valido"); log.log(2, msg); } if (!done) delete m; } } } printer().read_configuration(); } log.preview(); if (!mail.empty()) { TArray_sheet sheet(-1, -1, 78, 20, TR("Mail"), HR("@1|Destinatario@32|Messaggio@50"), 0, 1); FOR_EACH_ARRAY_ITEM(mail, r, obj) { const TMail_message& m = *(TMail_message*)obj; TToken_string* row = new TToken_string; *row = "X"; row->add(m._to); row->add(m._text.before('\n')); sheet.add(row); } if (sheet.run() == K_ENTER) { const long m = sheet.checked(); if (m > 0 && yesno_box(FR("Confermare l'invio di %ld mail?"), m)) { FOR_EACH_CHECKED_ROW(sheet, r, row) { const TMail_message& m = (const TMail_message&)mail[r]; xvt_mail_send(m._to, m._cc, m._ccn, m._subj, m._text, m._attach, m._ui); } } } } return true; } bool TStampaSol_application::create() { TApplication::create(); open_files(LF_TABCOM, LF_TAB, LF_CAUSALI, LF_MOV, LF_RMOV, 0); open_files(LF_NDITTE, LF_ANAG, LF_COMUNI, LF_RFORM, 0); open_files(LF_CLIFO, LF_PARTITE, LF_SCADENZE, LF_PAGSCA ,0); _msk = new TSol_mask("sc2400a"); return TSkeleton_application::create(); } bool TStampaSol_application::destroy() { delete _msk; return TSkeleton_application::destroy(); } void TStampaSol_application::on_firm_change() { mask().reset(); mask().set(F_STAMPSALDO, "X"); TApplication::on_firm_change(); TConfig c(CONFIG_DITTA, "cg"); _lingua_ditta= c.get("CodLin"); _gesval= c.get_bool("GesVal"); } void TStampaSol_application::main_loop() { TSol_mask& m = mask(); for(;;) { const KEY key = m.run(); if (key == K_QUIT) break; const TDate dlimsol(m.get(F_DATALIMSOL)); const TDate dsped(m.get(F_DATASEND)); if (dlimsol >= dsped) { _form= new TSol_form(m, _gesval, F_DATALIMOP, F_DATALIMSOL); if (key == 'M') mail_selected(); else print_selected(); delete _form; _form= NULL; } else error_box(TR("La data limite sollecito deve essere specificata e non puo' essere superiore alla data di invio")); } } TStampaSol_application::TStampaSol_application(): _lingua_ditta(1), _msk(NULL), _form(NULL) {} int sc2400(int argc, char** argv) { TStampaSol_application app; app.run(argc, argv, TR("Stampa solleciti")); return 0; }