v. 0.3.0 : customization form for each recurring invoice

This commit is contained in:
Chl 2021-03-23 15:27:33 +01:00
parent 3f14164994
commit b5bc4bca93
12 changed files with 1470 additions and 524 deletions

View file

@ -23,224 +23,174 @@
* Put detailed description here.
*/
// Load needed classes/lib
require_once 'sribmcustommailinfo.class.php';
/**
* Class Actionssendrecurringinvoicebymail
*/
class Actionssendrecurringinvoicebymail
{
/**
* @var DoliDB Database handler.
*/
public $db;
/**
* @var DoliDB Database handler.
*/
public $db;
/**
* @var string Error code (or message)
*/
public $error = '';
/**
* @var string Error code (or message)
*/
public $error = '';
/**
* @var array Errors
*/
public $errors = array();
/**
* @var array Errors
*/
public $errors = array();
/**
* @var array Hook results. Propagated to $hookmanager->resArray for later reuse
*/
public $results = array();
/**
* @var array Hook results. Propagated to $hookmanager->resArray for later reuse
*/
public $results = array();
/**
* @var string String displayed by executeHook() immediately after return
*/
public $resprints;
/**
* @var string String displayed by executeHook() immediately after return
*/
public $resprints;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Overloading the doActions function : replacing the parent's function with the one below
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function afterCreationOfRecurringInvoice($parameters, &$object, &$action, $hookmanager)
{
global $conf, $user, $langs;
$langs->load('mails');
/**
* Overloading the doActions function : replacing the parent's function with the one below
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function afterCreationOfRecurringInvoice($parameters, &$object, &$action, $hookmanager)
{
global $conf, $user, $langs;
$langs->load('mails');
$error = 0; // Error counter
$error = 0; // Error counter
$facturerec = $parameters['facturerec'];
$facturerec = $parameters['facturerec'];
$mailObject = new SRIBMCustomMailInfo($this->db);
if ($mailObject->fetch(null, $facturerec->id, true) != 1) {
dol_syslog("Error loading SRIBMCustomMailInfo for facture rec " . (isset($facturerec->id) ? $facturerec->id : "(facturerec->id not set ??)"));
return -1;
}
// We only send the mail when the invoice is not a draft
if ($object->brouillon) {
return 0;
}
// We only send the mail when the invoice is not a draft
// and the sending is enabled for the template.
if ($object->brouillon || !$mailObject->active) {
return 0;
}
// Fetch the mail template
// (pas très précise mais je commence à en avoir marre de creuser tout dolibarr pour trouver les bonnes fonctions...)
$result = $this->db->query("SELECT * FROM " . MAIN_DB_PREFIX . "c_email_templates WHERE module = 'sendrecurringinvoicebymail' AND active = 1 AND enabled = '1' ORDER BY tms DESC LIMIT 1");
if ( ! $result or ! ($template = $this->db->fetch_object($result))) {
$this->error = "Can't find mail template for sendrecurringinvoicebymail";
$this->errors[] = $this->error;
$error++;
return -1;
}
// Prepare the substitions for mail's subject and message (ex-body)
$substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
complete_substitutions_array($substitutionarray, $langs, $object); // lourd et n'a rien ajouté lors de mes tests
// Prepare the substitions for mail's subject and body
$substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
complete_substitutions_array($substitutionarray, $langs, $object); // lourd et n'a rien ajouté lors de mes tests
// Adding some useful substitions of our own...
if ( ! empty($object->linkedObjects['contrat'])) {
$contrat = reset($object->linkedObjects['contrat']); // no deep search, we take the first linked contract
$substitutionarray['__CONTRACT_REF__'] = $contrat->ref;
}
// Adding some useful substitions of our own...
if ( ! empty($object->linkedObjects['contrat'])) {
$contrat = reset($object->linkedObjects['contrat']); // no deep search, we take the first linked contract
$substitutionarray['__CONTRACT_REF__'] = $contrat->ref;
}
// Initialisations
$mail_data = array(
'from' => $mailObject->frommail,
'to' => implode(', ', $mailObject->compileEmails('to', true)),
'cc' => implode(', ', $mailObject->compileEmails('cc', true)),
'bcc' => implode(', ', $mailObject->compileEmails('bcc', true)),
'errorsTo' => $conf->global->MAIN_MAIL_ERRORS_TO,
'replyTo' => $conf->global->MAIN_MAIL_ERRORS_TO,
'subject' => $mailObject->subject,
'message' => $mailObject->body_plaintext,
);
// Initialisations
$mail_data = array(
'sendto' => $object->thirdparty->name . ' <' . $object->thirdparty->email . '>',
'from' => $conf->global->MAIN_MAIL_EMAIL_FROM,
'errorsTo' => $conf->global->MAIN_MAIL_ERRORS_TO,
'replyTo' => $conf->global->MAIN_MAIL_ERRORS_TO,
'subject' => $template->topic,
'body' => $template->content,
);
// Check that we have a recipient, to avoid some frequent error...
if (empty($mail_data['to'] . $mail_data['cc'] . $mail_data['bcc'])) {
dol_syslog("Empty recipient for thirdparty " . $object->thirdparty->id . ". Not sending facturerec " . $facturerec->ref . " (id:" . $facturerec->id . ").");
return 0;
}
// If the invoice has some custom parameters (subject, body, sendto, ...)
$mail_data = array_merge($mail_data, $this->getCustomFieldsMail($object));
// Make the substitutions
foreach (array('subject', 'message') as $key) {
$mail_data[$key] = make_substitutions($mail_data[$key], $substitutionarray, $langs);
if (method_exists($object, 'makeSubstitution')) {
$mail_data[$key] = $object->makeSubstitution($mail_data[$key]);
}
}
// Check that we have a recipient, to avoid some frequent error...
if (empty($mail_data['sendto'])) {
dol_syslog("Empty recipient for thirdparty " . $object->thirdparty->id . ". Not sending facturerec " . $facturerec->ref . " (id:" . $facturerec->id . ").");
return 0;
}
// Check if we have to attach the file
$filePath = array();
$fileMime = array();
$fileName = array();
if ($mailObject->addmaindocfile) {
$filePath = array(DOL_DATA_ROOT . '/' . $object->last_main_doc);
$fileMime = array('application/pdf'); // FIXME: à rendre dynamique, même si ce sera toujours du PDF ?
$fileName = array(basename($object->last_main_doc));
}
// Make the substitutions
foreach (array('subject', 'body') as $key) {
$mail_data[$key] = make_substitutions($mail_data[$key], $substitutionarray, $langs);
if (method_exists($object, 'makeSubstitution')) {
$mail_data[$key] = $object->makeSubstitution($mail_data[$key]);
}
}
// At last, send the mail
$mailfile = new CMailFile(
$mail_data['subject'],
$mail_data['to'],
$mail_data['from'],
$mail_data['message'],
$filePath,
$fileMime,
$fileName,
$mail_data['cc'], // CC
$mail_data['bcc'], // BCC
0, //deliveryreceipt
0, //msgishtml
$mail_data['errorsTo'],
'', // css
'', // trackid
'', // moreinheader
'standard', // sendcontext
$mail_data['replyTo']);
// Check if we have to attach the file
$filePath = array();
$fileMime = array();
$fileName = array();
if ($template->joinfiles) {
$filePath = array(DOL_DATA_ROOT . '/' . $object->last_main_doc);
$fileMime = array('application/pdf'); // FIXME: à rendre dynamique, même si ce sera toujours du PDF ?
$fileName = array(basename($object->last_main_doc));
}
if ($mailfile->sendfile()) {
dol_syslog("Success sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ").");
// At last, send the mail
$mailfile = new CMailFile(
$mail_data['subject'],
$mail_data['sendto'],
$mail_data['from'],
$mail_data['body'],
$filePath,
$fileMime,
$fileName,
'', // CC
'', // BCC
0, //deliveryreceipt
0, //msgishtml
$mail_data['errorsTo'],
'', // css
'', // trackid
'', // moreinheader
'standard', // sendcontext
$mail_data['replyTo']);
// Adds info to object for trigger
// (maybe make a copy of the object instead of modifying it directly ?)
$object->email_msgid = $mailfile->msgid;
$object->email_from = $mail_data['from'];
$object->email_subject = $mail_data['subject'];
$object->email_to = $mail_data['to'];
$object->email_tocc = $mail_data['cc'];
$object->email_tobcc = $mail_data['bcc'];
$object->actiontypecode = 'AC_OTH_AUTO';
$object->actionmsg2=$langs->transnoentities('MailSentBy').' '.CMailFile::getValidAddress($mail_data['from'],4,0,1).' '.$langs->transnoentities('To').' '.CMailFile::getValidAddress($mail_data['to'],4,0,1);
$object->actionmsg = $langs->transnoentities('MailFrom').': '.dol_escape_htmltag($mail_data['from']);
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($mail_data['to']));
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('MailTopic') . ": " . $mail_data['subject']);
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
$object->actionmsg = dol_concatdesc($object->actionmsg, $mail_data['message']);
if ($mailfile->sendfile()) {
dol_syslog("Success sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ").");
// Adds info to object for trigger
// (maybe make a copy of the object instead of modifying it directly ?)
$object->email_msgid = $mailfile->msgid;
$object->email_from = $mail_data['from'];
$object->email_subject = $mail_data['subject'];
$object->email_to = $mail_data['sendto'];
//$object->email_tocc = $sendtocc;
//$object->email_tobcc = $sendtobcc;
$object->actiontypecode = 'AC_OTH_AUTO';
$object->actionmsg2=$langs->transnoentities('MailSentBy').' '.CMailFile::getValidAddress($mail_data['from'],4,0,1).' '.$langs->transnoentities('To').' '.CMailFile::getValidAddress($mail_data['sendto'],4,0,1);
$object->actionmsg = $langs->transnoentities('MailFrom').': '.dol_escape_htmltag($mail_data['from']);
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($mail_data['sendto']));
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('MailTopic') . ": " . $mail_data['subject']);
$object->actionmsg = dol_concatdesc($object->actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
$object->actionmsg = dol_concatdesc($object->actionmsg, $mail_data['body']);
// Launch triggers
$interface = new Interfaces($this->db);
$resultTrigger = $interface->run_triggers('BILL_SENTBYMAIL', $object, $user, $langs, $conf);
} else {
$this->error = "Error sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ").";
$this->errors[] = $this->error;
dol_syslog($this->error);
$error++;
}
return ($error ? -1 : 0);
}
/**
* FIXME: For the time being, we abuse of note_private to store our customizations
*/
public function getCustomFieldsMail($object)
{
return $this->parseCustomFieldsMail($object->note_private);
}
/**
* FIXME: For the time being, we abuse of note_private to store our customizations
*
* This expect something like this in note_private:
* This is a good client... (other private infos)
* %%% sendrecurringinvoicebymail::subject
* New invoice __REF__
* %%%
* %%% sendrecurringinvoicebymail::sendto
* recipient1@example.org, recipient2@example.org
* %%%
* %%% sendrecurringinvoicebymail::body
* Hello dear client,
* Please find attached...
* %%%
*/
public function parseCustomFieldsMail($data)
{
$output = [];
// Remove eventual windows' "\r"
$data = str_replace("\r", "", $data);
$regexps = array(
'subject' => '/(^|\n)%%% sendrecurringinvoicebymail::subject\n(?<subject>.*)%%%(\n|$)/sU',
'body' => '/(^|\n)%%% sendrecurringinvoicebymail::body\n(?<body>.*)%%%(\n|$)/sU',
'sendto' => '/(^|\n)%%% sendrecurringinvoicebymail::sendto\n(?<sendto>.*)%%%(\n|$)/sU',
);
foreach ($regexps as $key => $r) {
$result_regexp = [];
if (preg_match_all($r, $data, $result_regexp)) {
$output[$key] = trim($result_regexp[$key][0]);
}
}
return $output;
}
// Launch triggers
$interface = new Interfaces($this->db);
$resultTrigger = $interface->run_triggers('BILL_SENTBYMAIL', $object, $user, $langs, $conf);
} else {
$this->error = "Error sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ").";
$this->errors[] = $this->error;
dol_syslog($this->error);
$error++;
}
return ($error ? -1 : 0);
}
}