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[1]) || !$argv[1] || !in_array($argv[1], array(
'test',
'confirm'))) {
42 print "Usage: $script_file (test|confirm) [delay]\n";
44 print "Send an email to users to remind all unpaid customer invoices user is sale representative for.\n";
45 print "If you choose 'test' mode, no emails are sent.\n";
46 print "If you add a delay (nb of days), only invoice with due date < today + delay are included.\n";
51 require $path.
"../../htdocs/master.inc.php";
52 require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
57 $version = DOL_VERSION;
66 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
69 $duration_value = isset($argv[2]) ? $argv[2] :
'none';
71 print $script_file.
" launched with mode ".$mode.
" default lang=".$langs->defaultlang.(is_numeric($duration_value) ?
" delay=".$duration_value :
"").
"\n";
73 if ($mode !=
'confirm') {
74 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
77 $sql =
"SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang,";
78 $sql .=
" u.rowid as uid, u.lastname, u.firstname, u.email, u.lang";
79 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
80 $sql .=
" , ".MAIN_DB_PREFIX.
"societe as s";
81 $sql .=
" , ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
82 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
83 $sql .=
" WHERE f.fk_statut = 1 AND f.paye = 0";
84 $sql .=
" AND f.fk_soc = s.rowid";
85 if (is_numeric($duration_value))
86 $sql .=
" AND f.date_lim_reglement < '".$db->idate(
dol_time_plus_duree($now, $duration_value,
"d")).
"'";
87 $sql .=
" AND sc.fk_soc = s.rowid";
88 $sql .=
" AND sc.fk_user = u.rowid";
89 $sql .=
" ORDER BY u.email ASC, s.rowid ASC, f.ref ASC";
94 $num = $db->num_rows(
$resql);
101 print "We found ".$num.
" couples (unpayed validated invoice - sale representative) qualified\n";
102 dol_syslog(
"We found ".$num.
" couples (unpayed validated invoice - sale representative) qualified");
107 $obj = $db->fetch_object(
$resql);
109 if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail ==
'none') {
111 if (
dol_strlen($oldemail) && $oldemail !=
'none') {
112 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
114 if ($oldemail !=
'none')
115 print "- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
117 $oldemail = $obj->email;
119 $oldlang = $obj->lang;
125 if (empty($obj->email))
126 print "Warning: Sale representative ".$salerepresentative.
" has no email. Notice disabled.\n";
131 $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang);
134 $outputlangs->loadLangs(array(
"main",
"bills"));
137 $message .= $outputlangs->trans(
"Invoice").
" ".$obj->ref.
" : ".
price($obj->total_ttc, 0, $outputlangs, 0, 0, - 1, $conf->currency).
" : ".$obj->name.
"\n";
138 dol_syslog(
"email_unpaid_invoices_to_representatives.php: ".$obj->email);
141 print "Unpaid invoice ".$obj->ref.
", price ".
price2num($obj->total_ttc).
", due date ".
dol_print_date($db->jdate($obj->due_date),
'day').
" (linked to company ".$obj->name.
", sale representative ".
dolGetFirstLastname($obj->firstname, $obj->lastname).
", email ".$obj->email.
", lang ".$outputlangs->defaultlang.
"): ";
144 else print "disqualified (no email).";
149 $total += $obj->total_ttc;
154 if ($foundtoprocess) {
155 if (
dol_strlen($oldemail) && $oldemail !=
'none')
157 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
159 if ($oldemail !=
'none')
160 print "- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
164 print "No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n";
170 dol_syslog(
"email_unpaid_invoices_to_representatives.php: Error");
186 function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldsalerepresentative)
188 global $conf, $langs;
190 if (getenv(
'DOL_FORCE_EMAIL_TO'))
191 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
194 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ?
'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang);
195 $newlangs->load(
"main");
196 $newlangs->load(
"bills");
198 $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT) ? $newlangs->trans(
"ListOfYourUnpaidInvoices") : $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT);
200 $from = empty($conf->global->MAIN_MAIL_EMAIL_FROM) ?
'' : $conf->global->MAIN_MAIL_EMAIL_FROM;
201 $errorsto = empty($conf->global->MAIN_MAIL_ERRORS_TO) ?
'' : $conf->global->MAIN_MAIL_ERRORS_TO;
204 print "- Send email for ".$oldsalerepresentative.
" (".$oldemail.
"), total: ".$total.
"\n";
205 dol_syslog(
"email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail);
208 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER))
210 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER))
214 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) {
215 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER;
217 $allmessage .= $newlangs->transnoentities(
"ListOfYourUnpaidInvoices").($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
218 $allmessage .= $newlangs->transnoentities(
"NoteListOfYourUnpaidInvoices").($usehtml ?
"<br>\n" :
"\n");
220 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
221 $allmessage .= $langs->trans(
"Total").
" = ".
price($total, 0, $newlangs, 0, 0, - 1, $conf->currency).($usehtml ?
"<br>\n" :
"\n");
222 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
223 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER;
224 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER))
228 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
230 $mail->errors_to = $errorsto;
233 if ($mode ==
'confirm') {
234 $result = $mail->sendfile();
236 print "Error sending email ".$mail->error.
"\n";
237 dol_syslog(
"Error sending email ".$mail->error.
"\n");
240 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.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_textishtml($msg, $option=0)
Return if a text is a html content.