30if (!defined(
'NOSESSION')) {
31 define(
'NOSESSION',
'1');
34$sapi_type = php_sapi_name();
35$script_file = basename(__FILE__);
39$sapi_type = php_sapi_name();
40if (substr($sapi_type, 0, 3) ==
'cgi') {
41 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
45if (!isset($argv[2]) || !$argv[2] || !in_array($argv[1], array(
'test',
'confirm')) || !in_array($argv[2], array(
'thirdparties',
'contacts'))) {
46 print
"Usage: $script_file (test|confirm) (thirdparties|contacts) [delay] [after]\n";
48 print
"Send an email to customers to remind all unpaid customer invoices.\n";
49 print
"If you choose 'test' mode, no emails are sent.\n";
50 print
"If you add param delay (nb of days), only invoice with due date < today + delay are included.\n";
51 print
"If you add param after (nb of days), only invoice with due date >= today + delay are included.\n";
55$targettype = $argv[2];
57require $path.
"../../htdocs/master.inc.php";
58require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
63$version = DOL_VERSION;
66$hookmanager->initHooks(array(
'cli'));
74print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
75dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
78$duration_value = isset($argv[3]) ? $argv[3] :
'none';
79$duration_value2 = isset($argv[4]) ? $argv[4] :
'none';
82print $script_file.
" launched with mode ".$mode.
" default lang=".$langs->defaultlang.(is_numeric($duration_value) ?
" delay=".$duration_value :
"").(is_numeric($duration_value2) ?
" after=".$duration_value2 :
"").
"\n";
84if ($mode !=
'confirm') {
85 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
88if (!empty($dolibarr_main_db_readonly)) {
89 print
"Error: instance in read-onyl mode\n";
95$sql =
"SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date,";
96$sql .=
" s.rowid as sid, s.nom as name, s.email, s.default_lang";
97if ($targettype ==
'contacts') {
98 $sql .=
", sp.rowid as cid, sp.firstname as cfirstname, sp.lastname as clastname, sp.email as cemail";
100$sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f, ".MAIN_DB_PREFIX.
"societe as s";
101if ($targettype ==
'contacts') {
102 $sql .=
", ".MAIN_DB_PREFIX.
"socpeople as sp";
104$sql .=
" WHERE f.fk_statut = 1 AND f.paye = 0";
105$sql .=
" AND f.fk_soc = s.rowid";
106if (is_numeric($duration_value2)) {
107 $sql .=
" AND f.date_lim_reglement >= '".$db->idate(
dol_time_plus_duree($now, (
int) $duration_value2,
"d")).
"'";
109if (is_numeric($duration_value)) {
110 $sql .=
" AND f.date_lim_reglement < '".$db->idate(
dol_time_plus_duree($now, (
int) $duration_value,
"d")).
"'";
112if ($targettype ==
'contacts') {
113 $sql .=
" AND s.rowid = sp.fk_soc";
116if ($targettype ==
'contacts') {
117 $sql .=
" sp.email, sp.rowid,";
119$sql .=
" s.email ASC, s.rowid ASC, f.ref ASC";
122$resql = $db->query($sql);
124 $num = $db->num_rows($resql);
132 $trackthirdpartiessent = array();
134 print
"We found ".$num.
" couples (unpayed validated invoices-".$targettype.
") qualified\n";
135 dol_syslog(
"We found ".$num.
" couples (unpayed validated invoices-".$targettype.
") qualified");
141 $obj = $db->fetch_object($resql);
143 $newemail = empty($obj->cemail) ? $obj->email : $obj->cemail;
147 if ($newemail != $oldemail || $oldemail ==
'none') {
150 if ($obj->sid && $obj->sid != $oldsid) {
153 if ($targettype ==
'contacts') {
154 if ($obj->cid && $obj->cid != $oldcid) {
161 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
163 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
165 if ($oldemail !=
'none') {
166 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
167 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
169 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
173 $oldemail = $newemail;
175 if ($targettype ==
'contacts') {
178 $oldlang = $obj->default_lang;
179 $oldtarget = (empty($obj->cfirstname) && empty($obj->clastname)) ? $obj->name : ($obj->clastname.
" ".$obj->cfirstname);
188 $outputlangs->setDefaultLang(empty($obj->default_lang) ? $langs->defaultlang : $obj->default_lang);
191 $outputlangs->loadLangs(array(
"main",
"bills"));
194 $message .= $outputlangs->trans(
"Invoice").
" ".$obj->ref.
" : ".
price($obj->total_ttc, 0, $outputlangs, 0, 0, -1, $conf->currency).
"\n";
195 dol_syslog(
"email_unpaid_invoices_to_customers.php: ".$newemail.
" ".$message);
198 print
"Unpaid invoice ".$obj->ref.
", price ".
price2num($obj->total_ttc).
", due date ".
dol_print_date($db->jdate($obj->due_date),
'day').
", customer id ".$obj->sid.
" ".$obj->name.
", ".(isset($obj->cid) ?
"contact id ".$obj->cid.
" ".$obj->clastname.
" ".$obj->cfirstname.
", " :
"").
"email ".$newemail.
", lang ".$outputlangs->defaultlang.
": ";
202 print
"disqualified (no email).";
208 $total += $obj->total_ttc;
214 if ($foundtoprocess) {
215 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
217 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
219 if ($oldemail !=
'none') {
220 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
221 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
223 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
229 print
"No unpaid invoices found\n";
235 dol_syslog(
"email_unpaid_invoices_to_customers.php: Error");
251function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget)
253 global $conf, $langs;
255 if (getenv(
'DOL_FORCE_EMAIL_TO')) {
256 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
260 $newlangs->setDefaultLang(empty($userlang) ?
getDolGlobalString(
'MAIN_LANG_DEFAULT',
'auto') : $userlang);
261 $newlangs->load(
"main");
262 $newlangs->load(
"bills");
264 $subject =
getDolGlobalString(
'SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_SUBJECT', $newlangs->trans(
"ListOfYourUnpaidInvoices"));
270 print
"- Send email to '".$oldtarget.
"' (".$oldemail.
"), total: ".$total.
"\n";
271 dol_syslog(
"email_unpaid_invoices_to_customers.php: send mail to ".$oldemail);
283 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER;
285 $allmessage .=
"Dear customer".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
286 $allmessage .=
"Please, find a summary of the bills with pending payments from you.".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
287 $allmessage .=
"Note: This list contains only unpaid invoices.".($usehtml ?
"<br>\n" :
"\n");
289 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
290 $allmessage .= $langs->trans(
"Total").
" = ".
price($total, 0, $userlang, 0, 0, -1, $conf->currency).($usehtml ?
"<br>\n" :
"\n");
298 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
300 $mail->errors_to = $errorsto;
303 if ($mode ==
'confirm') {
304 $result = $mail->sendfile();
306 print
"Error sending email ".$mail->error.
"\n";
307 dol_syslog(
"Error sending email ".$mail->error.
"\n");
310 print
"No email sent (test mode)\n";
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage translations.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget)
Send email.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_textishtml($msg, $option=0)
Return if a text is a html content.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.