campo-sirio/src/fp/fplib03.cpp

181 lines
5.6 KiB
C++
Raw Normal View History

#include "fplib.h"
#include "execp.h"
class TExternal_app;
void TFp_mail_sender::set_doc(const int anno, const long ndoc, const TFixed_string& codnum, const TFixed_string& tipodoc, const long codcf, TString mail, bool accord, TString ragsoc, bool sent)
{
_anno = anno;
_codnum = codnum;
_tipodoc = &cached_tipodoc(tipodoc);
_ndoc = ndoc;
_codcf = codcf;
_mail = mail;
_accord = accord;
_ragsoc = ragsoc;
_sent = sent;
_pdf_name.cut(0) << SLASH << _anno << '_' << _codnum << '_' << _ndoc << ".pdf";
}
bool TFp_mail_sender::genera_pdf()
{
static TString commandline;
commandline.cut(0) << "ve1 -2 " << _codnum << ' ' << _anno
<< " D " << _ndoc << " X P 1 D"; // X: stampa su disco, P: provvisorio, 1: 1 copia, D: disabilita archiviazione
TExternal_app interattivo(commandline);
if (interattivo.run() != NOERR)
{
TString msgerr = "Fallita generazione PDF documento ";
msgerr << _codnum << ' ' << _anno << " D " << _ndoc;
error_box(msgerr);
return false;
}
TFilename pdf;
_pdf_path.cut(0) << pdf.tempdir();
pdf.cut(0) << _pdf_path << _pdf_name;
if (!pdf.exist()) {
error_box("Attenzione! Non <20> stato possibile generare il PDF");
return false;
}
return true;
}
// Creo se non c'<27> il pdf e lo allego alla mail per l'invio
bool TFp_mail_sender::send(const TString& msg)
{
// Controllo per sicurezza quando si prova a rifare l'invio di una fattura gi<67> spedita
if (_sent)
{
if(_error == -1)
_error = custom_box("ATTENZIONE fattura gi<67> spedita. Reinviare?", "S<EFBFBD> a tutti", "No", "Annulla");
if (_error == K_NO)
_error = -1;
if (_error == -1 || _error == K_SPACE)
return false; // Gestione decisione se inviare tutte quelle gia inviate o no (singola) o annullare (tutte)
}
bool ok = false;
if (_alleg && !_mail.blank() && _accord)
{
_pdf_path = fp_settings().get_fld_dest_usr(); // Cartella dove ci sono i pdf generati
TFilename pdf; pdf << _pdf_path << _pdf_name;
const int pdf_exist = pdf.exist();
if (pdf_exist)
ok = spotlite_send_mail(pdf, msg); // -> Invio mail con PDF gi<67> esistente
else if (genera_pdf()) // Genero pdf se non esiste gi<67> (in tempdir)
{ // Manda gi<67> un messaggio di errore generazione se fallisce la generazione
TFilename newf_pdf; newf_pdf << _pdf_path << _pdf_name;
ok = spotlite_send_mail(newf_pdf, msg);
}
else {
TString msg_err; msg_err << "Inviare lo stesso la mail senza il pdf allegato?";
if (noyes_box(msg_err)) // Se la generazione fallisce chiedo se procedere lo stesso
ok = spotlite_send_mail(msg); // Se s<>, procedo con mail senza pdf
} // Se no, esco senza inviare
}
else if (!_alleg && _accord)
ok = spotlite_send_mail(msg);
if(ok)
{
TDocumento fdoc('D', _anno, _codnum, _ndoc);
TString hfatt, bfatt;
TPaf_record paf0100f("PAF0100F");
TString query;
if (chiave_paf(fdoc, hfatt, bfatt) && paf0100f.search(nullptr, hfatt, bfatt))
{
query << "UPDATE PAF0100F SET P1_ERRINT = 'S' WHERE P1_KEYHEADERFATT = '" << hfatt << "' AND P1_KEYBODYFATT = '" << bfatt << "'";
fp_db().sq_set_exec(query);
fp_db().sq_commit();
}
#ifdef DBG
else
error_box("Attenzione non <20> stato possibile trovare il documento");
#endif
}
return ok;
}
bool TFp_mail_sender::get_mail(TToken_string& to, TToken_string& cc, TToken_string& ccn,
TString& subj, TString& text, TToken_string& attach, short& ui)
{
to.cut(0) << _mail;
// to.cut(0) << "spalacino@sirio-is.it";
const TDocumento doc('D', _anno, _codnum, _ndoc);
doc.riferimento(subj);
subj << "Notifica Mancata Consegna Fattura n. " << _ndoc
<< ' ' << prefix().firm().ragione_sociale();
TString saluti = esc(ini_get_string(CONFIG_USER, "Mail", "Signature"));
if (saluti.full())
{
if (saluti.find('\n') < 0 && fexist(saluti))
{
TScanner s(saluti);
while (!s.eof())
text << s.line() << '\n';
}
else
text << saluti << '\n';
}
else
text << "Cordiali Saluti " << prefix().firm().ragione_sociale();
text.trim();
if (to.full())
ui &= ~0x1; // No user interface
ui |= 0x2; // Query receipt
return true;
}
// Funzione per spedire un pdf via mail
bool TFp_mail_sender::spotlite_send_mail(const TFilename& pdf, const TString& msg)
{
TToken_string to(15, ';'), cc(15, ';'), ccn(15, ';'), attach(pdf, ';');
TString subj;
TString text; text << msg << "\n\n";
short flags = 0x1; // UI
bool ok = get_mail(to, cc, ccn, subj, text, attach, flags);
if (subj.blank())
subj.cut(0) << "Invio di " << pdf.name();
if (!(flags & 0x1) && to.empty_items() && cc.empty_items() && ccn.empty_items())
flags |= 0x1; // Forza UI in assenza di destinatari
static bool usePower = ini_get_bool(CONFIG_USER, "Mail", "Powershell");
ok = (usePower ? xvt_powermail_send(to, cc, ccn, subj, text, attach, flags, user()) :
xvt_mail_send(to, cc, ccn, subj, text, attach, flags)) != 0;
return ok;
}
bool TFp_mail_sender::spotlite_send_mail(const TString& msg)
{
TToken_string to(15, ';'), cc(15, ';'), ccn(15, ';'), attach(15, ';');
TString subj;
TString text; text << msg << "\n\n";
short flags = 0x1; // UI
bool ok = get_mail(to, cc, ccn, subj, text, attach, flags);
if (subj.blank())
subj.cut(0) << "Notifica Mancata Consegna Fattura ";
if (!(flags & 0x1) && to.empty_items() && cc.empty_items() && ccn.empty_items())
flags |= 0x1; // Forza UI in assenza di destinatari
static bool usePower = ini_get_bool(CONFIG_USER, "Mail", "Powershell");
ok = (usePower ? xvt_powermail_send(to, cc, ccn, subj, text, attach, flags, user()) :
xvt_mail_send(to, cc, ccn, subj, text, attach, flags)) != 0;
return ok;
}
bool TFp_mail_sender::set_alleg(const bool allega_fat)
{
_alleg = allega_fat;
return _alleg;
}