28 if (!defined(
'NOSESSION')) define(
'NOSESSION',
'1');
30 $sapi_type = php_sapi_name();
31 $script_file = basename(__FILE__);
35 $sapi_type = php_sapi_name();
36 if (substr($sapi_type, 0, 3) ==
'cgi') {
37 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
41 if (!isset($argv[2]) || !$argv[2] || !in_array($argv[1], array(
'test',
'confirm')) || !in_array($argv[2], array(
'thirdparties',
'contacts'))) {
42 print "Usage: $script_file (test|confirm) (thirdparties|contacts) [delay] [after]\n";
44 print "Send an email to customers to remind all unpaid customer invoices.\n";
45 print "If you choose 'test' mode, no emails are sent.\n";
46 print "If you add param delay (nb of days), only invoice with due date < today + delay are included.\n";
47 print "If you add param after (nb of days), only invoice with due date >= today + delay are included.\n";
51 $targettype = $argv[2];
53 require $path.
"../../htdocs/master.inc.php";
54 require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
59 $version = DOL_VERSION;
68 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
71 $duration_value = isset($argv[3]) ? $argv[3] :
'none';
72 $duration_value2 = isset($argv[4]) ? $argv[4] :
'none';
75 print $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";
77 if ($mode !=
'confirm') {
78 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
81 $sql =
"SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date,";
82 $sql .=
" s.rowid as sid, s.nom as name, s.email, s.default_lang";
83 if ($targettype ==
'contacts')
84 $sql .=
", sp.rowid as cid, sp.firstname as cfirstname, sp.lastname as clastname, sp.email as cemail";
85 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f, ".MAIN_DB_PREFIX.
"societe as s";
86 if ($targettype ==
'contacts')
87 $sql .=
", ".MAIN_DB_PREFIX.
"socpeople as sp";
88 $sql .=
" WHERE f.fk_statut = 1 AND f.paye = 0";
89 $sql .=
" AND f.fk_soc = s.rowid";
90 if (is_numeric($duration_value2))
91 $sql .=
" AND f.date_lim_reglement >= '".$db->idate(
dol_time_plus_duree($now, $duration_value2,
"d")).
"'";
92 if (is_numeric($duration_value))
93 $sql .=
" AND f.date_lim_reglement < '".$db->idate(
dol_time_plus_duree($now, $duration_value,
"d")).
"'";
94 if ($targettype ==
'contacts')
95 $sql .=
" AND s.rowid = sp.fk_soc";
97 if ($targettype ==
'contacts')
98 $sql .=
" sp.email, sp.rowid,";
99 $sql .=
" s.email ASC, s.rowid ASC, f.ref ASC";
102 $resql = $db->query($sql);
104 $num = $db->num_rows(
$resql);
112 $trackthirdpartiessent = array();
114 print "We found ".$num.
" couples (unpayed validated invoices-".$targettype.
") qualified\n";
115 dol_syslog(
"We found ".$num.
" couples (unpayed validated invoices-".$targettype.
") qualified");
121 $obj = $db->fetch_object(
$resql);
123 $newemail = empty($obj->cemail) ? $obj->email : $obj->cemail;
127 if ($newemail != $oldemail || $oldemail ==
'none') {
130 if ($obj->sid && $obj->sid != $oldsid) {
133 if ($targettype ==
'contacts') {
134 if ($obj->cid && $obj->cid != $oldcid) {
141 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
142 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldtarget);
143 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
145 if ($oldemail !=
'none') {
146 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
147 print "- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
148 else print "- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
151 $oldemail = $newemail;
153 if ($targettype ==
'contacts') {
156 $oldlang = $obj->default_lang;
157 $oldtarget = (empty($obj->cfirstname) && empty($obj->clastname)) ? $obj->name : ($obj->clastname.
" ".$obj->cfirstname);
166 $outputlangs->setDefaultLang(empty($obj->default_lang) ? $langs->defaultlang : $obj->default_lang);
169 $outputlangs->loadLangs(array(
"main",
"bills"));
172 $message .= $outputlangs->trans(
"Invoice").
" ".$obj->ref.
" : ".
price($obj->total_ttc, 0, $outputlangs, 0, 0, - 1, $conf->currency).
"\n";
173 dol_syslog(
"email_unpaid_invoices_to_customers.php: ".$newemail.
" ".$message);
176 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.
": ";
179 else print "disqualified (no email).";
184 $total += $obj->total_ttc;
190 if ($foundtoprocess) {
191 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
193 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldtarget);
194 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
196 if ($oldemail !=
'none') {
197 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
198 print "- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
199 else print "- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
204 print "No unpaid invoices found\n";
210 dol_syslog(
"email_unpaid_invoices_to_customers.php: Error");
226 function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget)
228 global $conf, $langs;
230 if (getenv(
'DOL_FORCE_EMAIL_TO'))
231 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
234 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ?
'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang);
235 $newlangs->load(
"main");
236 $newlangs->load(
"bills");
238 $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_SUBJECT) ? $newlangs->trans(
"ListOfYourUnpaidInvoices") : $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_SUBJECT);
240 $from = empty($conf->global->MAIN_MAIL_EMAIL_FROM) ?
'' : $conf->global->MAIN_MAIL_EMAIL_FROM;
241 $errorsto = empty($conf->global->MAIN_MAIL_ERRORS_TO) ?
'' : $conf->global->MAIN_MAIL_ERRORS_TO;
244 print "- Send email to '".$oldtarget.
"' (".$oldemail.
"), total: ".$total.
"\n";
245 dol_syslog(
"email_unpaid_invoices_to_customers.php: send mail to ".$oldemail);
248 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER))
250 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER))
254 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER)) {
255 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER;
257 $allmessage .=
"Dear customer".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
258 $allmessage .=
"Please, find a summary of the bills with pending payments from you.".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
259 $allmessage .=
"Note: This list contains only unpaid invoices.".($usehtml ?
"<br>\n" :
"\n");
261 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
262 $allmessage .= $langs->trans(
"Total").
" = ".
price($total, 0, $userlang, 0, 0, - 1, $conf->currency).($usehtml ?
"<br>\n" :
"\n");
263 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER)) {
264 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER;
265 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER))
269 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
271 $mail->errors_to = $errorsto;
274 if ($mode ==
'confirm') {
275 $result = $mail->sendfile();
277 print "Error sending email ".$mail->error.
"\n";
278 dol_syslog(
"Error sending email ".$mail->error.
"\n");
281 print "No email sent (test mode)\n";
dol_now($mode= 'auto')
Return date for now.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget, $duration_value)
Send email.
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_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage translations.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
dol_textishtml($msg, $option=0)
Return if a text is a html content.