28 if (!defined(
'NOSESSION')) {
29 define(
'NOSESSION',
'1');
32 $sapi_type = php_sapi_name();
33 $script_file = basename(__FILE__);
37 $sapi_type = php_sapi_name();
38 if (substr($sapi_type, 0, 3) ==
'cgi') {
39 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
43 if (!isset($argv[1]) || !$argv[1] || !in_array($argv[1], array(
'test',
'confirm'))) {
44 print
"Usage: $script_file (test|confirm) [delay]\n";
46 print
"Send an email to users to remind all unpaid customer invoices user is sale representative for.\n";
47 print
"If you choose 'test' mode, no emails are sent.\n";
48 print
"If you add a delay (nb of days), only invoice with due date < today + delay are included.\n";
53 require $path.
"../../htdocs/master.inc.php";
54 require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
59 $version = DOL_VERSION;
67 print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
68 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
71 $duration_value = isset($argv[2]) ? $argv[2] :
'none';
73 print $script_file.
" launched with mode ".$mode.
" default lang=".$langs->defaultlang.(is_numeric($duration_value) ?
" delay=".$duration_value :
"").
"\n";
75 if ($mode !=
'confirm') {
76 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
79 if (!empty($dolibarr_main_db_readonly)) {
80 print
"Error: instance in read-onyl mode\n";
84 $sql =
"SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang,";
85 $sql .=
" u.rowid as uid, u.lastname, u.firstname, u.email, u.lang";
86 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
87 $sql .=
" , ".MAIN_DB_PREFIX.
"societe as s";
88 $sql .=
" , ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
89 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
90 $sql .=
" WHERE f.fk_statut = 1 AND f.paye = 0";
91 $sql .=
" AND f.fk_soc = s.rowid";
92 if (is_numeric($duration_value)) {
93 $sql .=
" AND f.date_lim_reglement < '".$db->idate(
dol_time_plus_duree($now, $duration_value,
"d")).
"'";
95 $sql .=
" AND sc.fk_soc = s.rowid";
96 $sql .=
" AND sc.fk_user = u.rowid";
97 $sql .=
" ORDER BY u.email ASC, s.rowid ASC, f.ref ASC";
100 $resql = $db->query($sql);
102 $num = $db->num_rows(
$resql);
109 print
"We found ".$num.
" couples (unpayed validated invoice - sale representative) qualified\n";
110 dol_syslog(
"We found ".$num.
" couples (unpayed validated invoice - sale representative) qualified");
115 $obj = $db->fetch_object(
$resql);
117 if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail ==
'none') {
119 if (
dol_strlen($oldemail) && $oldemail !=
'none') {
120 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
122 if ($oldemail !=
'none') {
123 print
"- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
126 $oldemail = $obj->email;
128 $oldlang = $obj->lang;
134 if (empty($obj->email)) {
135 print
"Warning: Sale representative ".$salerepresentative.
" has no email. Notice disabled.\n";
141 $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang);
144 $outputlangs->loadLangs(array(
"main",
"bills"));
147 $message .= $outputlangs->trans(
"Invoice").
" ".$obj->ref.
" : ".
price($obj->total_ttc, 0, $outputlangs, 0, 0, - 1, $conf->currency).
" : ".$obj->name.
"\n";
148 dol_syslog(
"email_unpaid_invoices_to_representatives.php: ".$obj->email);
151 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.
"): ";
155 print
"disqualified (no email).";
161 $total += $obj->total_ttc;
166 if ($foundtoprocess) {
167 if (
dol_strlen($oldemail) && $oldemail !=
'none') {
168 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
170 if ($oldemail !=
'none') {
171 print
"- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
176 print
"No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n";
182 dol_syslog(
"email_unpaid_invoices_to_representatives.php: Error");
198 function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldsalerepresentative)
200 global $conf, $langs;
202 if (getenv(
'DOL_FORCE_EMAIL_TO')) {
203 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
207 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ?
'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang);
208 $newlangs->load(
"main");
209 $newlangs->load(
"bills");
211 $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT) ? $newlangs->trans(
"ListOfYourUnpaidInvoices") : $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT);
213 $from = empty($conf->global->MAIN_MAIL_EMAIL_FROM) ?
'' : $conf->global->MAIN_MAIL_EMAIL_FROM;
214 $errorsto = empty($conf->global->MAIN_MAIL_ERRORS_TO) ?
'' : $conf->global->MAIN_MAIL_ERRORS_TO;
217 print
"- Send email for ".$oldsalerepresentative.
" (".$oldemail.
"), total: ".$total.
"\n";
218 dol_syslog(
"email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail);
221 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
224 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER) &&
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) {
229 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) {
230 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER;
232 $allmessage .= $newlangs->transnoentities(
"ListOfYourUnpaidInvoices").($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
233 $allmessage .= $newlangs->transnoentities(
"NoteListOfYourUnpaidInvoices").($usehtml ?
"<br>\n" :
"\n");
235 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
236 $allmessage .= $langs->trans(
"Total").
" = ".
price($total, 0, $newlangs, 0, 0, - 1, $conf->currency).($usehtml ?
"<br>\n" :
"\n");
237 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
238 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER;
239 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
244 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
246 $mail->errors_to = $errorsto;
249 if ($mode ==
'confirm') {
250 $result = $mail->sendfile();
252 print
"Error sending email ".$mail->error.
"\n";
253 dol_syslog(
"Error sending email ".$mail->error.
"\n");
256 print
"No email sent (test mode)\n";
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage translations.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldsalerepresentative)
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.