dolibarr 18.0.6
email_unpaid_invoices_to_representatives.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
3/*
4 * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5 * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
6 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28if (!defined('NOSESSION')) {
29 define('NOSESSION', '1');
30}
31
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
34$path = __DIR__.'/';
35
36// Test si mode batch
37$sapi_type = php_sapi_name();
38if (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";
40 exit(-1);
41}
42
43if (!isset($argv[1]) || !$argv[1] || !in_array($argv[1], array('test', 'confirm'))) {
44 print "Usage: $script_file (test|confirm) [delay]\n";
45 print "\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";
49 exit(-1);
50}
51$mode = $argv[1];
52
53require $path."../../htdocs/master.inc.php";
54require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
55
56$langs->load('main');
57
58// Global variables
59$version = DOL_VERSION;
60$error = 0;
61
62/*
63 * Main
64 */
65
66@set_time_limit(0);
67print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
68dol_syslog($script_file." launched with arg ".join(',', $argv));
69
70$now = dol_now('tzserver');
71$duration_value = isset($argv[2]) ? $argv[2] : 'none';
72
73print $script_file." launched with mode ".$mode." default lang=".$langs->defaultlang.(is_numeric($duration_value) ? " delay=".$duration_value : "")."\n";
74
75if ($mode != 'confirm') {
76 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
77}
78
79if (!empty($dolibarr_main_db_readonly)) {
80 print "Error: instance in read-onyl mode\n";
81 exit(-1);
82}
83
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";
92if (is_numeric($duration_value)) {
93 $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'";
94}
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"; // Order by email to allow one message per email
98
99// print $sql;
100$resql = $db->query($sql);
101if ($resql) {
102 $num = $db->num_rows($resql);
103 $i = 0;
104 $oldemail = 'none';
105 $olduid = 0;
106 $oldlang = '';
107 $total = 0;
108 $foundtoprocess = 0;
109
110 print "We found ".$num." couples (unpayed validated invoice - sale representative) qualified\n";
111 dol_syslog("We found ".$num." couples (unpayed validated invoice - sale representative) qualified");
112 $message = '';
113 $oldsalerepresentative = 0;
114
115 if ($num) {
116 while ($i < $num) {
117 $obj = $db->fetch_object($resql);
118
119 if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail == 'none') {
120 // Break onto sales representative (new email or uid)
121 if (dol_strlen($oldemail) && $oldemail != 'none') {
122 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
123 } else {
124 if ($oldemail != 'none') {
125 print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
126 }
127 }
128 $oldemail = $obj->email;
129 $olduid = $obj->uid;
130 $oldlang = $obj->lang;
131 $oldsalerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname);
132 $message = '';
133 $total = 0;
134 $foundtoprocess = 0;
135 $salerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname);
136 if (empty($obj->email)) {
137 print "Warning: Sale representative ".$salerepresentative." has no email. Notice disabled.\n";
138 }
139 }
140
141 // Define line content
142 $outputlangs = new Translate('', $conf);
143 $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang); // By default language of sale representative
144
145 // Load translation files required by the page
146 $outputlangs->loadLangs(array("main", "bills"));
147
148 if (dol_strlen($obj->email)) {
149 $message .= $outputlangs->trans("Invoice")." ".$obj->ref." : ".price($obj->total_ttc, 0, $outputlangs, 0, 0, - 1, $conf->currency)." : ".$obj->name."\n";
150 dol_syslog("email_unpaid_invoices_to_representatives.php: ".$obj->email);
151 $foundtoprocess++;
152 }
153 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."): ";
154 if (dol_strlen($obj->email)) {
155 print "qualified.";
156 } else {
157 print "disqualified (no email).";
158 }
159 print "\n";
160
161 unset($outputlangs);
162
163 $total += $obj->total_ttc;
164 $i++;
165 }
166
167 // Si il reste des envois en buffer
168 if ($foundtoprocess) {
169 if (dol_strlen($oldemail) && $oldemail != 'none') { // Break onto email (new email)
170 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
171 } else {
172 if ($oldemail != 'none') {
173 print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
174 }
175 }
176 }
177 } else {
178 print "No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n";
179 }
180
181 exit(0);
182} else {
183 dol_print_error($db);
184 dol_syslog("email_unpaid_invoices_to_representatives.php: Error");
185
186 exit(-1);
187}
188
200function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget)
201{
202 global $conf, $langs;
203
204 if (getenv('DOL_FORCE_EMAIL_TO')) {
205 $oldemail = getenv('DOL_FORCE_EMAIL_TO');
206 }
207
208 $newlangs = new Translate('', $conf);
209 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang);
210 $newlangs->load("main");
211 $newlangs->load("bills");
212
213 $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT) ? $newlangs->trans("ListOfYourUnpaidInvoices") : $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT);
214 $sendto = $oldemail;
215 $from = empty($conf->global->MAIN_MAIL_EMAIL_FROM) ? '' : $conf->global->MAIN_MAIL_EMAIL_FROM;
216 $errorsto = empty($conf->global->MAIN_MAIL_ERRORS_TO) ? '' : $conf->global->MAIN_MAIL_ERRORS_TO;
217 $msgishtml = - 1;
218
219 print "- Send email for ".$oldtarget." (".$oldemail."), total: ".$total."\n";
220 dol_syslog("email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail);
221
222 $usehtml = 0;
223 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER) && dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
224 $usehtml += 1;
225 }
226 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER) && dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) {
227 $usehtml += 1;
228 }
229
230 $allmessage = '';
231 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) {
232 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER;
233 } else {
234 $allmessage .= $newlangs->transnoentities("ListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n").($usehtml ? "<br>\n" : "\n");
235 $allmessage .= $newlangs->transnoentities("NoteListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n");
236 }
237 $allmessage .= $message.($usehtml ? "<br>\n" : "\n");
238 $allmessage .= $langs->trans("Total")." = ".price($total, 0, $newlangs, 0, 0, - 1, $conf->currency).($usehtml ? "<br>\n" : "\n");
239 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
240 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER;
241 if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) {
242 $usehtml += 1;
243 }
244 }
245
246 $mail = new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(), '', '', 0, $msgishtml);
247
248 $mail->errors_to = $errorsto;
249
250 // Send or not email
251 if ($mode == 'confirm') {
252 $result = $mail->sendfile();
253 if (!$result) {
254 print "Error sending email ".$mail->error."\n";
255 dol_syslog("Error sending email ".$mail->error."\n");
256 }
257 } else {
258 print "No email sent (test mode)\n";
259 dol_syslog("No email sent (test mode)");
260 $mail->dump_mail();
261 $result = 1;
262 }
263
264 if ($result) {
265 return 1;
266 } else {
267 return -1;
268 }
269}
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.
Definition date.lib.php:122
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_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.