From a626b252bd3a54f61d3c7a58d9da4166d007840b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 1 Sep 2020 15:33:02 +0200 Subject: [PATCH] Optimize code --- htdocs/comm/action/class/actioncomm.class.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/htdocs/comm/action/class/actioncomm.class.php b/htdocs/comm/action/class/actioncomm.class.php index aaf160b9ec1..fd41a9eadf4 100644 --- a/htdocs/comm/action/class/actioncomm.class.php +++ b/htdocs/comm/action/class/actioncomm.class.php @@ -1986,12 +1986,17 @@ class ActionComm extends CommonObject $this->db->begin(); //Select all action comm reminder - $sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."actioncomm_reminder WHERE typeremind = 'email' AND status = 0"; + $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 .= $this->db->order("dateremind", "ASC"); $resql = $this->db->query($sql); - if ($resql){ - while ($obj = $this->db->fetch_object($resql)){ - $actionCommReminder = new ActionCommReminder($this->db); + if ($resql) { + $formmail = new FormMail($this->db); + $actionCommReminder = new ActionCommReminder($this->db); + + while ($obj = $this->db->fetch_object($resql)){ $res = $actionCommReminder->fetch($obj->id); if ($res < 0) { $error++; @@ -2000,14 +2005,11 @@ class ActionComm extends CommonObject if (!$error) { - if ($actionCommReminder->dateremind <= dol_now()) - { //Select email template - $formmail = new FormMail($this->db); $arraymessage = $formmail->getEMailTemplate($this->db, 'actioncomm_send', $user, $langs, (!empty($actionCommReminder->fk_email_template)) ? $actionCommReminder->fk_email_template : -1, 1); + // Load event $res = $this->fetch($actionCommReminder->fk_actioncomm); - if ($res > 0) { // PREPARE EMAIL @@ -2065,7 +2067,6 @@ class ActionComm extends CommonObject else { $error++; } - } } } } else { @@ -2075,7 +2076,8 @@ class ActionComm extends CommonObject if (!$error) { // Delete also very old past events (we do not keep more than 1 month record in past) - $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder WHERE dateremind < '".$this->db->jdate($now - (3600 * 24 * 32))."'"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder"; + $sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'"; $resql = $this->db->query($sql); if (!$resql) {