v. 0.3.0 : customization form for each recurring invoice
This commit is contained in:
parent
3f14164994
commit
b5bc4bca93
12 changed files with 1470 additions and 524 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
516
class/sribmcustommailinfo.class.php
Normal file
516
class/sribmcustommailinfo.class.php
Normal file
|
@ -0,0 +1,516 @@
|
|||
<?php
|
||||
/* Cpoyright (C) 2021 chl-dev@bugness.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file class/sribmcustommailinfo.class.php
|
||||
* \brief File of class for objects storing custom mail informations (module SRIBM : SendRecurringInvoiceByMail)
|
||||
*/
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
|
||||
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php';
|
||||
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
|
||||
|
||||
/**
|
||||
* Class for objects storing custom mail informations.
|
||||
*
|
||||
* Part of module SRIBM : SendRecurringInvoiceByMail
|
||||
*/
|
||||
class SRIBMCustomMailInfo extends CommonObject
|
||||
{
|
||||
/**
|
||||
* @var string Id to identify managed objects
|
||||
*/
|
||||
public $element = 'sribmcustommailinfo';
|
||||
|
||||
/**
|
||||
* @var string Name of table without prefix where object is stored
|
||||
*/
|
||||
public $table_element = 'sribm_custom_mail_info';
|
||||
|
||||
// Database fields
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $fk_facture_rec;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $active = 1;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $addmaindocfile = 1;
|
||||
|
||||
/**
|
||||
* @var string Type of sender, could be 'robot', 'user' or 'company',
|
||||
* usually meaning the mail sender is determined by the
|
||||
* PHP/Dolibarr config (cf. admin/mails.php?mainmenu=home ),
|
||||
* this object's creator email or the company email.
|
||||
*/
|
||||
public $fromtype = 'robot';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $frommail;
|
||||
|
||||
/**
|
||||
* @var int 1: Send to societe main's email, 0: don't.
|
||||
*/
|
||||
public $sendto_thirdparty = 1;
|
||||
|
||||
/**
|
||||
* @var string Format expected: "Foo <foo@example.com>, bar@example.net"
|
||||
*/
|
||||
public $sendto_free;
|
||||
|
||||
/**
|
||||
* @var int 1: Send to societe main's email, 0: don't.
|
||||
*/
|
||||
public $sendcc_thirdparty = 0;
|
||||
|
||||
/**
|
||||
* @var string Format expected: "Foo <foo@example.com>, bar@example.net"
|
||||
*/
|
||||
public $sendcc_free;
|
||||
|
||||
/**
|
||||
* @var int 1: Send to societe main's email, 0: don't.
|
||||
*/
|
||||
public $sendbcc_thirdparty = 0;
|
||||
|
||||
/**
|
||||
* @var string Format expected: "Foo <foo@example.com>, bar@example.net"
|
||||
*/
|
||||
public $sendbcc_free;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $subject;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $body_plaintext;
|
||||
|
||||
/**
|
||||
* @var string (not used at the moment)
|
||||
*/
|
||||
public $body_html;
|
||||
|
||||
// End of database fields
|
||||
|
||||
/**
|
||||
* FactureRec object linked to this object
|
||||
*
|
||||
* Note : the class Facture use $fac_rec and fk_fac_rec_source which both
|
||||
* seem to be integer (I don't really see the distinction), so we suffix
|
||||
* with '_object' to avoid future conflict.
|
||||
*
|
||||
* @var FactureRec
|
||||
*/
|
||||
public $fac_rec_object;
|
||||
|
||||
/**
|
||||
* @var array SocPeoples linked via table llx_sribm_custom_mail_info_socpeople (FIXME: describe format)
|
||||
*/
|
||||
public $linkToSocPeoples = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param DoliDb $db Database handler
|
||||
*/
|
||||
public function __construct(DoliDB $db)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$this->db = $db;
|
||||
|
||||
// Fill default values
|
||||
$this->frommail = $conf->global->MAIN_MAIL_EMAIL_FROM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create object into database
|
||||
*
|
||||
* Since only one mail can exist for each invoice template, this method
|
||||
* only insert a row with the id of the template and delegate all the work
|
||||
* on the rest of the data to the update() method.
|
||||
*
|
||||
* @param User $user User that creates (at the moment, we don't use it)
|
||||
* @param int $notrigger 1=do not execute triggers, 0 otherwise
|
||||
* @return int >0 if OK, < 0 if KO
|
||||
*/
|
||||
public function create(User $user, $notrigger = 0)
|
||||
{
|
||||
$error=0;
|
||||
|
||||
$this->db->begin();
|
||||
|
||||
$sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element;
|
||||
$sql .= " (fk_facture_rec, fromtype, frommail)";
|
||||
$sql .= sprintf(
|
||||
" VALUES (%d, '%s', '%s')",
|
||||
(int)$this->fk_facture_rec,
|
||||
$this->db->escape($this->fromtype),
|
||||
$this->db->escape($this->frommail)
|
||||
);
|
||||
|
||||
dol_syslog("SRIBMCustomMailInfo::create", LOG_DEBUG);
|
||||
|
||||
$result = $this->db->query($sql);
|
||||
if ($result) {
|
||||
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
|
||||
$result = $this->update($user, 1);
|
||||
if ($result < 0) {
|
||||
$this->db->rollback();
|
||||
return -3;
|
||||
}
|
||||
|
||||
if (! $notrigger)
|
||||
{
|
||||
// Call trigger
|
||||
$result=$this->call_trigger('SRIBM_CUSTOM_MAIL_INFO_CREATE', $user);
|
||||
if ($result < 0) {
|
||||
$error++;
|
||||
}
|
||||
// End call triggers
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
|
||||
$this->db->rollback();
|
||||
return -2;
|
||||
}
|
||||
} else {
|
||||
$this->error = $this->db->lasterror();
|
||||
$this->db->rollback();
|
||||
return -1;
|
||||
}
|
||||
|
||||
$this->db->commit();
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update object in database
|
||||
*
|
||||
* @param User $user User that creates (at the moment, we don't use it)
|
||||
* @param int $notrigger 1=do not execute triggers, 0 otherwise
|
||||
* @return int >0 if OK, < 0 if KO
|
||||
*/
|
||||
public function update(User $user, $notrigger = 0)
|
||||
{
|
||||
$error=0;
|
||||
|
||||
//$this->db->begin();
|
||||
|
||||
$sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element;
|
||||
$sql .= " SET ";
|
||||
$sql .= " fk_facture_rec = " . (int)$this->fk_facture_rec;
|
||||
$sql .= ", active = " . (int)$this->active;
|
||||
$sql .= ", addmaindocfile = '" . (int)$this->addmaindocfile . "'";
|
||||
$sql .= ", fromtype = '" . $this->db->escape($this->fromtype) . "'";
|
||||
$sql .= ", frommail = '" . $this->db->escape($this->frommail) . "'";
|
||||
$sql .= ", sendto_thirdparty = " . (int)$this->db->escape($this->sendto_thirdparty);
|
||||
$sql .= ", sendto_free = '" . $this->db->escape($this->sendto_free) . "'";
|
||||
$sql .= ", sendcc_thirdparty = " . (int)$this->db->escape($this->sendcc_thirdparty);
|
||||
$sql .= ", sendcc_free = '" . $this->db->escape($this->sendcc_free) . "'";
|
||||
$sql .= ", sendbcc_thirdparty = " . (int)$this->db->escape($this->sendbcc_thirdparty);
|
||||
$sql .= ", sendbcc_free = '" . $this->db->escape($this->sendbcc_free) . "'";
|
||||
$sql .= ", subject = '" . $this->db->escape($this->subject) . "'";
|
||||
$sql .= ", body_plaintext = '" . $this->db->escape($this->body_plaintext) . "'";
|
||||
$sql .= ", body_html = '" . $this->db->escape($this->body_html) . "'";
|
||||
$sql .= " WHERE rowid = " . (int)$this->id;
|
||||
|
||||
$result = $this->db->query($sql);
|
||||
if ($result) {
|
||||
if (! $notrigger)
|
||||
{
|
||||
// Call trigger
|
||||
$result=$this->call_trigger('SRIBM_CUSTOM_MAIL_INFO_MODIFY', $user);
|
||||
if ($result < 0) {
|
||||
$error++;
|
||||
}
|
||||
// End call triggers
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
dol_syslog(get_class($this) . "::update " . $this->error, LOG_ERR);
|
||||
$this->db->rollback();
|
||||
return -$error;
|
||||
}
|
||||
} else {
|
||||
$this->error = $this->db->lasterror();
|
||||
$this->db->rollback();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//$this->db->commit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate object with data from DB
|
||||
*
|
||||
* @param int $rowid Id of the SRIBMCustomMailInfo to load
|
||||
* @param int $ref Id of the fac_rec_object
|
||||
* @param bool $fill_defaults_from_template If true and the model doesn't exist in DB, fill attributes using the template
|
||||
* @return int < 0 if KO, > 0 if OK
|
||||
*/
|
||||
public function fetch($rowid, $ref=null, $fill_defaults_from_template = false)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "SELECT rowid, fk_facture_rec, active, addmaindocfile, fromtype, frommail, sendto_thirdparty, sendto_free, sendcc_thirdparty, sendcc_free, sendbcc_thirdparty, sendbcc_free, subject, body_plaintext, body_html";
|
||||
$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
|
||||
$sql .= " WHERE " . (isset($ref) ? 'fk_facture_rec = ' . (int)$ref : "rowid = " . (int)$rowid);
|
||||
|
||||
dol_syslog("SRIBMCustomMailInfo::fetch", LOG_DEBUG);
|
||||
|
||||
$result = $this->db->query($sql);
|
||||
if (! $result) {
|
||||
$this->error=$this->db->lasterror();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ($this->db->num_rows($result)) {
|
||||
$obj = $this->db->fetch_object($result);
|
||||
|
||||
$this->id = $obj->rowid;
|
||||
$this->fk_facture_rec = $obj->fk_facture_rec;
|
||||
$this->active = $obj->active;
|
||||
$this->addmaindocfile = $obj->addmaindocfile;
|
||||
$this->fromtype = $obj->fromtype;
|
||||
$this->frommail = $obj->frommail;
|
||||
$this->sendto_thirdparty = $obj->sendto_thirdparty;
|
||||
$this->sendto_free = $obj->sendto_free;
|
||||
$this->sendcc_thirdparty = $obj->sendcc_thirdparty;
|
||||
$this->sendcc_free = $obj->sendcc_free;
|
||||
$this->sendbcc_thirdparty = $obj->sendbcc_thirdparty;
|
||||
$this->sendbcc_free = $obj->sendbcc_free;
|
||||
$this->subject = $obj->subject;
|
||||
$this->body_plaintext = $obj->body_plaintext;
|
||||
$this->body_html = $obj->body_html;
|
||||
$ref = $obj->fk_facture_rec;
|
||||
} elseif (!$fill_defaults_from_template) {
|
||||
$this->error = "SRIBMCustomMailInfo not found (id: " . var_export($rowid, true) . ", ref: " . var_export($ref, true);
|
||||
dol_syslog("SRIBMCustomMailInfo::fetch error " . $this->error, LOG_ERR);
|
||||
}
|
||||
|
||||
// It seems to be usual to fully fetch by default (cf. Facture source
|
||||
// code with fetch_optionals()) so we copy the Dolibarr behaviour
|
||||
// Link to the facture_rec
|
||||
$this->fac_rec_object = new FactureRec($this->db);
|
||||
if ($this->fac_rec_object->fetch($ref) <= 0) {
|
||||
$this->error = "Unable to fetch FactureRec $ref.";
|
||||
dol_syslog("FactureRec::Fetch error " . $this->error, LOG_ERR);
|
||||
return -2;
|
||||
}
|
||||
if ($this->fac_rec_object->fetch_thirdparty() <= 0) {
|
||||
$this->error = "Unable to fetch ThirdParty for FactureRec $ref.";
|
||||
dol_syslog("FactureRec::FetchThirdParty error " . $this->error, LOG_ERR);
|
||||
return -3;
|
||||
}
|
||||
$result = $this->fac_rec_object->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0); // This load $_facrec->linkedObjectsIds
|
||||
|
||||
// link to socpeople
|
||||
if ($this->id) {
|
||||
// FIXME: create a dedicated PHP class?
|
||||
$sql = "SELECT fk_socpeople, sendtype";
|
||||
$sql .= " FROM " . MAIN_DB_PREFIX . "sribm_custom_mail_info_socpeople";
|
||||
$sql .= " WHERE fk_sribm_cmi = " . (int)$this->id;
|
||||
$result = $this->db->query($sql);
|
||||
if (! $result) {
|
||||
$this->error=$this->db->lasterror();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// reset the attribute
|
||||
$this->linkToSocPeoples = array();
|
||||
while ($obj = $this->db->fetch_object($result)) {
|
||||
$tmp = new Contact($this->db);
|
||||
if ($tmp->fetch($obj->fk_socpeople) > 0) {
|
||||
$tmp->pivot = $obj;
|
||||
$this->linkToSocPeoples[] = $tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optionally, if the model hasn't been found (no id), we fill with the template's data
|
||||
if (!$this->id && $fill_defaults_from_template) {
|
||||
$result = $this->db->query("SELECT topic, content, joinfiles 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";
|
||||
dol_syslog("SRIBMCustomMailInfo::fetch error " . $this->error, LOG_ERR);
|
||||
return -4;
|
||||
}
|
||||
|
||||
$this->subject = $template->topic;
|
||||
$this->body_plaintext = $template->content;
|
||||
$this->addmaindocfile = $template->joinfiles;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete this object's record in database
|
||||
*
|
||||
* @return int <0 on error, >0 if OK
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$error=0;
|
||||
|
||||
//$this->db->begin();
|
||||
|
||||
$sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element;
|
||||
$sql .= " WHERE rowid = " . (int)$this->id;
|
||||
|
||||
$result = $this->db->query($sql);
|
||||
if (! $result) {
|
||||
//$this->db->rollback();
|
||||
$this->error = $this->db->lasterror();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Call trigger
|
||||
$result = $this->call_trigger('SRIBM_CUSTOM_MAIL_INFO_DELETE', $user);
|
||||
if ($result < 0) {
|
||||
$error++;
|
||||
}
|
||||
// End call triggers
|
||||
|
||||
if ($error) {
|
||||
dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
|
||||
//$this->db->rollback();
|
||||
return -$error;
|
||||
}
|
||||
|
||||
//$this->db->commit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quick function to synchronize the links between this object and its contacts
|
||||
*
|
||||
* Format of the data :
|
||||
* array(
|
||||
* array('id' => id_contact_1, 'sendtype' => 'to'),
|
||||
* array('id' => id_contact_2, 'sendtype' => 'cc'),
|
||||
* ...
|
||||
*
|
||||
* @param array $data Format : see above
|
||||
* @return int <0 on error, >0 if OK
|
||||
*/
|
||||
public function updateLinkSocPeople($data)
|
||||
{
|
||||
$this->db->begin();
|
||||
|
||||
$result = $this->db->query("DELETE FROM " . MAIN_DB_PREFIX . "sribm_custom_mail_info_socpeople where fk_sribm_cmi = " . (int)$this->id);
|
||||
if (! $result) {
|
||||
$this->error=$this->db->lasterror();
|
||||
$this->db->rollback();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ($data) {
|
||||
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "sribm_custom_mail_info_socpeople";
|
||||
$sql .= " (fk_sribm_cmi, fk_socpeople, sendtype) VALUES (%d, %d, '%s')";
|
||||
foreach ($data as $item) {
|
||||
// We insert one by one to maximize compatibility
|
||||
// (if there happens to be thousands of links, I'll rework it :)
|
||||
$result = $this->db->query(sprintf(
|
||||
$sql,
|
||||
$this->id,
|
||||
$item['id'],
|
||||
$this->db->escape($item['sendtype'])
|
||||
));
|
||||
if (! $result) {
|
||||
$this->error=$this->db->lasterror();
|
||||
$this->db->rollback();
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->commit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to compile the recipients in one string from sendto_thirdparty,
|
||||
* sendto_free and socpeople.
|
||||
*
|
||||
* @param string $sendtype One of 'to', 'cc', 'bcc'
|
||||
* @param bool $filterBadEmail If true, don't include recipients with empty/bad email address
|
||||
* @return string String compiling emails in the format 'Foo <foo@example.com>, bar@example.com'
|
||||
*/
|
||||
public function compileEmails($sendtype, $filterBadEmails = false)
|
||||
{
|
||||
$output = array();
|
||||
|
||||
$listContacts = $this->fac_rec_object->thirdparty->thirdparty_and_contact_email_array(1);
|
||||
|
||||
switch ($sendtype) {
|
||||
case 'to':
|
||||
if ($this->sendto_free) {
|
||||
$output[] = $this->sendto_free;
|
||||
}
|
||||
if ($this->sendto_thirdparty) {
|
||||
$output[] = $listContacts['thirdparty'];
|
||||
}
|
||||
break;
|
||||
|
||||
case 'cc':
|
||||
if ($this->sendcc_free) {
|
||||
$output[] = $this->sendcc_free;
|
||||
}
|
||||
if ($this->sendcc_thirdparty) {
|
||||
$output[] = $listContacts['thirdparty'];
|
||||
}
|
||||
break;
|
||||
|
||||
case 'bcc':
|
||||
if ($this->sendbcc_free) {
|
||||
$output[] = $this->sendbcc_free;
|
||||
}
|
||||
if ($this->sendbcc_thirdparty) {
|
||||
$output[] = $listContacts['thirdparty'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($this->linkToSocPeoples as $contact) {
|
||||
if ($contact->pivot->sendtype == $sendtype) {
|
||||
$output[] = $listContacts[$contact->id];
|
||||
}
|
||||
}
|
||||
|
||||
if ($filterBadEmails) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue