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[1]) || !$argv[1] || !in_array($argv[1], array(
'test',
'confirm'))) {
46 print
"Usage: $script_file (test|confirm) [delay]\n";
48 print
"Send an email to remind all contracts services to expire, to users that are sale representative for.\n";
49 print
"If you choose 'test' mode, no emails are sent.\n";
50 print
"If you add a delay (nb of days), only services with expired date < today + delay are included.\n";
55require $path.
"../../htdocs/master.inc.php";
56require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
57require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
66$langs->loadLangs(array(
'main',
'contracts'));
69$version = DOL_VERSION;
72$hookmanager->initHooks(array(
'cli'));
80print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
81dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
84$duration_value = isset($argv[2]) ? $argv[2] :
'none';
86print $script_file.
" launched with mode ".$mode.
" default lang=".$langs->defaultlang.(is_numeric($duration_value) ?
" delay=".$duration_value :
"").
"\n";
88if ($mode !=
'confirm') {
89 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
94$sql =
"SELECT DISTINCT c.ref, c.fk_soc, cd.date_fin_validite, cd.total_ttc, cd.description as description, p.label as plabel, s.rowid, s.nom as name, s.email, s.default_lang,";
95$sql .=
" u.rowid as uid, u.lastname, u.firstname, u.email, u.lang";
96$sql .=
" FROM ".MAIN_DB_PREFIX.
"societe AS s, ".MAIN_DB_PREFIX.
"contrat AS c, ".MAIN_DB_PREFIX.
"contratdet AS cd";
97$sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = cd.fk_product, ".MAIN_DB_PREFIX.
"societe_commerciaux AS sc, ".MAIN_DB_PREFIX.
"user AS u";
98$sql .=
" WHERE s.rowid = c.fk_soc AND c.rowid = cd.fk_contrat AND c.statut > 0 AND cd.statut<5";
99if (is_numeric($duration_value)) {
100 $sql .=
" AND cd.date_fin_validite < '".$db->idate(
dol_time_plus_duree($now, (
int) $duration_value,
"d")).
"'";
102$sql .=
" AND sc.fk_soc = s.rowid AND sc.fk_user = u.rowid";
103$sql .=
" ORDER BY u.email ASC, s.rowid ASC, c.ref ASC";
105$resql = $db->query($sql);
107 $num = $db->num_rows($resql);
110 $oldsalerepresentative =
'';
115 print
"We found ".$num.
" couples (services to expire - sale representative) qualified\n";
116 dol_syslog(
"We found ".$num.
" couples (services to expire - sale representative) qualified");
121 $obj = $db->fetch_object($resql);
123 if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail ==
'none') {
125 if (
dol_strlen($oldemail) && $oldemail !=
'none') {
126 sendEmailTo($mode, $oldemail, $message,
price2num($total), $oldlang, $oldsalerepresentative, (
int) $duration_value);
128 if ($oldemail !=
'none') {
129 print
"- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
132 $oldemail = $obj->email;
134 $oldlang = $obj->lang;
140 if (empty($obj->email)) {
141 print
"Warning: Sale representative ".$salerepresentative.
" has no email. Notice disabled.\n";
147 $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang);
150 $outputlangs->loadLangs(array(
"main",
"contracts",
"bills",
"products"));
153 $message .= $outputlangs->trans(
"Contract").
" ".$obj->ref.
": ".$langs->trans(
"Service").
" ".
dol_concatdesc($obj->plabel, $obj->description).
" (".
price($obj->total_ttc, 0, $outputlangs, 0, 0, -1,
$conf->currency).
") ".$obj->name.
", ".$outputlangs->trans(
"DateEndPlannedShort").
" ".
dol_print_date($db->jdate($obj->date_fin_validite),
'day').
"\n\n";
154 dol_syslog(
"email_expire_services_to_representatives.php: ".$obj->email);
157 print
"Service to expire ".$obj->ref.
", label ".
dol_concatdesc($obj->plabel, $obj->description).
", due date ".
dol_print_date($db->jdate($obj->date_fin_validite),
'day').
" (linked to company ".$obj->name.
", sale representative ".
dolGetFirstLastname($obj->firstname, $obj->lastname).
", email ".$obj->email.
"): ";
161 print
"disqualified (no email).";
167 $total += $obj->total_ttc;
172 if ($foundtoprocess) {
173 if (
dol_strlen($oldemail) && $oldemail !=
'none') {
174 sendEmailTo($mode, $oldemail, $message,
price2num($total), $oldlang, $oldsalerepresentative, (
int) $duration_value);
176 if ($oldemail !=
'none') {
177 print
"- No email sent for ".$oldsalerepresentative.
", total: ".$total.
"\n";
182 print
"No services to expire (for companies linked to a particular commercial dolibarr user) found\n";
188 dol_syslog(
"email_expire_services_to_representatives.php: Error");
205function sendEmailTo($mode, $oldemail, $message, $total, $userlang, $oldtarget, $duration_value)
207 global
$conf, $langs;
209 if (getenv(
'DOL_FORCE_EMAIL_TO')) {
210 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
214 $newlangs->setDefaultLang(empty($userlang) ?
getDolGlobalString(
'MAIN_LANG_DEFAULT',
'auto') : $userlang);
215 $newlangs->load(
"main");
216 $newlangs->load(
"contracts");
218 if ($duration_value) {
219 if ($duration_value > 0) {
220 $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDuration", $duration_value);
222 $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDurationNeg", $duration_value);
225 $title = $newlangs->transnoentities(
"ListOfServicesToExpire");
228 $subject =
getDolGlobalString(
'SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_SUBJECT', $title);
234 print
"- Send email for ".$oldtarget.
" (".$oldemail.
"), total: ".$total.
"\n";
235 dol_syslog(
"email_expire_services_to_representatives.php: send mail to ".$oldemail);
247 $allmessage .=
getDolGlobalString(
'SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_HEADER');
249 $allmessage .= $title.($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
250 $allmessage .= $newlangs->transnoentities(
"NoteListOfYourExpiredServices").($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
252 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
253 $allmessage .= $langs->trans(
"Total").
" = ".
price($total, 0, $userlang, 0, 0, -1,
$conf->currency).($usehtml ?
"<br>\n" :
"\n");
255 $allmessage .=
getDolGlobalString(
'SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER');
261 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
263 $mail->errors_to = $errorsto;
266 if ($mode ==
'confirm') {
267 $result = $mail->sendfile();
269 print
"Error sending email ".$mail->error.
"\n";
270 dol_syslog(
"Error sending email ".$mail->error.
"\n");
273 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.
sendEmailTo($mode, $oldemail, $message, $total, $userlang, $oldtarget, $duration_value)
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).
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...