Add a schedule dtask to send reminder by email on date due.

This commit is contained in:
Laurent Destailleur 2021-04-19 10:35:19 +02:00
parent 1988703a60
commit a74b237a7b
2 changed files with 137 additions and 2 deletions

View File

@ -2290,10 +2290,11 @@ class ActionComm extends CommonObject
$this->db->begin();
//Select all action comm reminder
//Select all action comm reminders
$sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
$sql .= " WHERE typeremind = 'email' AND status = 0";
$sql .= " AND dateremind <= '".$this->db->idate(dol_now())."'";
$sql .= " AND dateremind <= '".$this->db->idate($now)."'";
$sql .= " AND entity IN (".getEntity('actioncomm').")";
$sql .= $this->db->order("dateremind", "ASC");
$resql = $this->db->query($sql);

View File

@ -5604,4 +5604,138 @@ class FactureLigne extends CommonInvoiceLine
}
}
}
/**
* Send reminders by emails for ivoices that are due
* CAN BE A CRON TASK
*
* @param int $nbdays Delay after due date (or before if delay is negative)
* @param int $idtemplate Id or name of of email template (Must be a template of type 'invoice')
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
*/
public function sendEmailsReminderOnDueDate($nbdays = 0, $idtemplate = 0)
{
global $conf, $langs, $user;
$error = 0;
$this->output = '';
$this->error = '';
$nbMailSend = 0;
$errorsMsg = array();
if (empty($conf->facture->enabled)) { // Should not happen. If module disabled, cron job should not be visible.
$langs->load("bills");
$this->output = $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Facture"));
return 0;
}
if (empty($conf->global->FACTURE_REMINDER_EMAIL)) {
$langs->load("bills");
$this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Facture"));
return 0;
}
$now = dol_now();
$tmpinvoice = new Facture($this->db);
dol_syslog(__METHOD__, LOG_DEBUG);
$this->db->begin();
//Select all action comm reminder
$sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."facture";
$sql .= " WHERE paye = 0";
$sql .= " AND date_lim_reglement = '".$this->db->idate(dol_time_plus_duree($now, -1 * $nbdays, 'd'))."'"; // TODO Remove hours into filter
$sql .= " AND entity IN (".getEntity('facture').")";
// TODO Add filter to check there is no payment started
$sql .= $this->db->order("date_lim_reglement", "ASC");
$resql = $this->db->query($sql);
if ($resql) {
$formmail = new FormMail($this->db);
while ($obj = $this->db->fetch_object($resql)) {
if (!$error) {
//Select email template
$arraymessage = $formmail->getEMailTemplate($this->db, 'facture', $user, $langs, $idtemplate, 1);
// Load event
$res = $tmpinvoice->fetch($obj->id);
if ($res > 0) {
// PREPARE EMAIL
$errormesg = '';
// Make substitution in email content
$substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this);
complete_substitutions_array($substitutionarray, $langs, $this);
// Content
$sendContent = make_substitutions($langs->trans($arraymessage->content), $substitutionarray);
//Topic
$sendTopic = (!empty($arraymessage->topic)) ? $arraymessage->topic : html_entity_decode($langs->trans('EventReminder'));
// Recipient
$res = $tmpinvoice->fetch_thirdparty();
$recipient = $tmpinvoice->thirdparty;
if ($res > 0) {
if (!empty($recipient->email)) {
$to = $recipient->email;
} else {
$errormesg = "Failed to send remind to thirdparty id=".$tmpinvoice->fk_soc.". No email defined for user.";
$error++;
}
} else {
$errormesg = "Failed to load recipient with thirdparty id=".$tmpinvoice->fk_soc;
$error++;
}
// Sender
$from = $conf->global->MAIN_MAIL_EMAIL_FROM;
if (empty($from)) {
$errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
$error++;
}
if (!$error) {
// Errors Recipient
$errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
$trackid = 'inv'.$tmpinvoice->id;
// Mail Creation
$cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', $trackid, '', '', '');
// Sending Mail
if ($cMailFile->sendfile()) {
$nbMailSend++;
} else {
$errormesg = $cMailFile->error.' : '.$to;
$error++;
}
}
if ($errormesg) {
$errorsMsg[] = $errormesg;
}
} else {
$errorsMsg[] = 'Failed to fetch record invoice with ID = '.$obj->id;
$error++;
}
}
}
} else {
$error++;
}
if (!$error) {
$this->output = 'Nb of emails sent : '.$nbMailSend;
$this->db->commit();
return 0;
} else {
$this->db->commit(); // We commit also on error, to have the error message recorded.
$this->error = 'Nb of emails sent : '.$nbMailSend.', '.(!empty($errorsMsg)) ? join(', ', $errorsMsg) : $error;
return $error;
}
}
}