diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 784d23498eb..492fd51e618 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -5623,16 +5623,17 @@ class Facture extends CommonInvoice /** - * Send reminders by emails for invoices that are due + * Send reminders by emails for invoices validated that are due. * CAN BE A CRON TASK * * @param int $nbdays Delay before due date (or after if delay is negative) * @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 string $forcerecipient Force email of recipient (for example to send the email to an accountant supervisor instead of the customer) + * @param string $datetouse 'duedate' (default) or 'invoicedate' * @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 = '', $forcerecipient = '') + public function sendEmailsRemindersOnInvoiceDueDate($nbdays = 0, $paymentmode = 'all', $template = '', $forcerecipient = '', $datetouse = 'duedate') { global $conf, $langs, $user; @@ -5648,6 +5649,10 @@ class Facture extends CommonInvoice $this->output .= $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Facture")); return 0; } + if (!in_array($datetouse, array('duedate', 'invoicedate'))) { + $this->output .= 'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"'; + return 0; + } /*if (empty($conf->global->FACTURE_REMINDER_EMAIL)) { $langs->load("bills"); $this->output .= $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Facture")); @@ -5672,15 +5677,23 @@ class Facture extends CommonInvoice if (!empty($paymentmode) && $paymentmode != 'all') { $sql .= ", ".MAIN_DB_PREFIX."c_paiement as cp"; } - $sql .= " WHERE f.paye = 0"; - $sql .= " AND f.fk_statut = ".self::STATUS_VALIDATED; - $sql .= " AND f.date_lim_reglement = '".$this->db->idate($tmpidate, 'gmt')."'"; + $sql .= " WHERE f.paye = 0"; // Only unpaid + $sql .= " AND f.fk_statut = ".self::STATUS_VALIDATED; // Only validated status + if ($datetouse == 'invoicedate') { + $sql .= " AND f.datef = '".$this->db->idate($tmpidate, 'gmt')."'"; + } else { + $sql .= " AND f.date_lim_reglement = '".$this->db->idate($tmpidate, 'gmt')."'"; + } $sql .= " AND f.entity IN (".getEntity('facture', 0).")"; // One batch process only one company (no sharing) if (!empty($paymentmode) && $paymentmode != 'all') { $sql .= " AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode)."'"; } // TODO Add a filter to check there is no payment started yet - $sql .= $this->db->order("date_lim_reglement", "ASC"); + if ($datetouse == 'invoicedate') { + $sql .= $this->db->order("datef", "ASC"); + } else { + $sql .= $this->db->order("date_lim_reglement", "ASC"); + } $resql = $this->db->query($sql); @@ -5707,7 +5720,7 @@ class Facture extends CommonInvoice $outputlangs = $langs; } - // Select email template + // Select email template according to language of recipient $arraymessage = $formmail->getEMailTemplate($this->db, 'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ? '' : $template)); if (is_numeric($arraymessage) && $arraymessage <= 0) { $langs->load("errors"); @@ -5812,7 +5825,7 @@ class Facture extends CommonInvoice $actioncomm->contact_id = 0; $actioncomm->code = 'AC_EMAIL'; - $actioncomm->label = 'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.' paymentmode='.$paymentmode.' template='.$template.' forcerecipient='.$forcerecipient.')'; + $actioncomm->label = 'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.' paymentmode='.$paymentmode.' template='.$template.' forcerecipient='.$forcerecipient.' datetouse='.$datetouse.')'; $actioncomm->note_private = $sendContent; $actioncomm->fk_project = $tmpinvoice->fk_project; $actioncomm->datep = dol_now(); diff --git a/htdocs/core/modules/modFacture.class.php b/htdocs/core/modules/modFacture.class.php index 64fca2dde60..f38be7f58ec 100644 --- a/htdocs/core/modules/modFacture.class.php +++ b/htdocs/core/modules/modFacture.class.php @@ -135,7 +135,7 @@ class modFacture extends DolibarrModules 'objectname'=>'Facture', 'method'=>'sendEmailsRemindersOnInvoiceDueDate', 'parameters'=>"10,all,EmailTemplateCode", - 'comment'=>'Send an emails when we reach the due date - n days of an invoice. First param is n, the number of days before due date to send the remind, second parameter is "all" or a payment mode code, last parameter is the code of email template to use (an email template with the EmailTemplateCode must exists. The version of the email template in the language of the thirdparty will be used in priority. Language of the thirdparty will be also used to update the PDF of the sent invoice).', + 'comment'=>'Send an email when we reach the invoice due date (or invoice date) - n days. First param is n, the number of days before due date (or invoice date) to send the remind (or after if value is negative), second parameter is "all" or a payment mode code, third parameter is the code of the email template to use (an email template with the EmailTemplateCode must exists. The version of the email template in the language of the thirdparty will be used in priority. Language of the thirdparty will be also used to update the PDF of the sent invoice). The last parameter is "duedate" or "invoicedate" to define which date of the invoice to use.', 'frequency'=>1, 'unitfrequency'=>3600 * 24, 'priority'=>50, diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 7e8177fdf1f..5a9312e77ee 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -625,7 +625,7 @@ SituationTotalProgress=Total progress %d %% SearchUnpaidInvoicesWithDueDate=Search unpaid invoices with a due date = %s NoPaymentAvailable=No payment available for %s PaymentRegisteredAndInvoiceSetToPaid=Payment registered and invoice %s set to paid -SendEmailsRemindersOnInvoiceDueDate=Send reminder by email for unpaid invoices +SendEmailsRemindersOnInvoiceDueDate=Send reminder by email for validated and unpaid invoices MakePaymentAndClassifyPayed=Record payment BulkPaymentNotPossibleForInvoice=Bulk payment is not possible for invoice %s (bad type or status) MentionVATDebitOptionIsOn=Option to pay tax based on debits