Can force recipient of unpaid invoice email reminder

This commit is contained in:
Laurent Destailleur 2022-11-07 14:22:43 +01:00
parent 055627e248
commit 7fae4d1b1c

View File

@ -5421,12 +5421,13 @@ class Facture extends CommonInvoice
* Send reminders by emails for ivoices that are due * Send reminders by emails for ivoices that are due
* CAN BE A CRON TASK * CAN BE A CRON TASK
* *
* @param int $nbdays Delay after due date (or before if delay is negative) * @param int $nbdays Delay after due date (or before if delay is negative)
* @param string $paymentmode '' or 'all' by default (no filter), or 'LIQ', 'CHQ', CB', ... * @param string $paymentmode '' or 'all' by default (no filter), or 'LIQ', 'CHQ', CB', ...
* @param int|string $template Name (or id) of email template (Must be a template of type 'facture_send') * @param int|string $template Name (or id) of email template (Must be a template of type 'facture_send')
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK) * @param string $forcerecipient Force email of recipient (for example to send the email to an accountant supervisor instead of the customer)
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
*/ */
public function sendEmailsRemindersOnInvoiceDueDate($nbdays = 0, $paymentmode = 'all', $template = '') public function sendEmailsRemindersOnInvoiceDueDate($nbdays = 0, $paymentmode = 'all', $template = '', $forcerecipient = '')
{ {
global $conf, $langs, $user; global $conf, $langs, $user;
@ -5527,26 +5528,30 @@ class Facture extends CommonInvoice
// Recipient // Recipient
$to = array(); $to = array();
$res = $tmpinvoice->fetch_thirdparty(); if ($forcerecipient) { // If a recipient was forced
$recipient = $tmpinvoice->thirdparty; $to = array($forcerecipient);
if ($res > 0) { } else {
$tmparraycontact = $tmpinvoice->liste_contact(-1, 'external', 0, 'BILLING'); $res = $tmpinvoice->fetch_thirdparty();
if (is_array($tmparraycontact) && count($tmparraycontact) > 0) { $recipient = $tmpinvoice->thirdparty;
foreach ($tmparraycontact as $data_email) { if ($res > 0) {
if (!empty($data_email['email'])) { $tmparraycontact = $tmpinvoice->liste_contact(-1, 'external', 0, 'BILLING');
$to[] = $tmpinvoice->thirdparty->contact_get_property($data_email['id'], 'email'); if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
foreach ($tmparraycontact as $data_email) {
if (!empty($data_email['email'])) {
$to[] = $tmpinvoice->thirdparty->contact_get_property($data_email['id'], 'email');
}
} }
} }
} if (empty($to) && !empty($recipient->email)) {
if (empty($to) && !empty($recipient->email)) { $to[] = $recipient->email;
$to[] = $recipient->email; } else {
$errormesg = "Failed to send remind to thirdparty id=".$tmpinvoice->socid.". No email defined for user.";
$error++;
}
} else { } else {
$errormesg = "Failed to send remind to thirdparty id=".$tmpinvoice->socid.". No email defined for user."; $errormesg = "Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
$error++; $error++;
} }
} else {
$errormesg = "Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
$error++;
} }
// Sender // Sender