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')) || !in_array($argv[2], array(
'thirdparties',
'contacts'))) {
46 print
"Usage: $script_file (test|confirm) (thirdparties|contacts) [delay] [after]\n";
48 print
"Send an email to customers to remind all contracts services to expire or expired.\n";
49 print
"If you choose 'test' mode, no emails are sent.\n";
50 print
"If you add param delay (nb of days), only services with expired date < today + delay are included.\n";
51 print
"If you add param after (nb of days), only services with expired date >= today + delay are included.\n";
55$targettype = $argv[2];
57require $path.
"../../htdocs/master.inc.php";
58require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
59require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
61$langs->loadLangs(array(
'main',
'contracts'));
64$version = DOL_VERSION;
67$hookmanager->initHooks(array(
'cli'));
75print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
76dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
79$duration_value = isset($argv[3]) ? $argv[3] :
'none';
80$duration_value2 = isset($argv[4]) ? $argv[4] :
'none';
83print $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";
85if ($mode !=
'confirm') {
86 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
89$sql =
"SELECT c.ref, cd.date_fin_validite, cd.total_ttc, cd.description as description, p.label as plabel,";
90$sql .=
" s.rowid as sid, s.nom as name, s.email, s.default_lang";
91if ($targettype ==
'contacts') {
92 $sql .=
", sp.rowid as cid, sp.firstname as cfirstname, sp.lastname as clastname, sp.email as cemail";
94$sql .=
" FROM ".MAIN_DB_PREFIX.
"societe AS s";
95if ($targettype ==
'contacts') {
96 $sql .=
", ".MAIN_DB_PREFIX.
"socpeople as sp";
98$sql .=
", ".MAIN_DB_PREFIX.
"contrat AS c";
99$sql .=
", ".MAIN_DB_PREFIX.
"contratdet AS cd";
100$sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = cd.fk_product";
101$sql .=
" WHERE s.rowid = c.fk_soc AND c.rowid = cd.fk_contrat AND c.statut > 0 AND cd.statut < 5";
102if (is_numeric($duration_value2)) {
103 $sql .=
" AND cd.date_fin_validite >= '".$db->idate(
dol_time_plus_duree($now, (
int) $duration_value2,
"d")).
"'";
105if (is_numeric($duration_value)) {
106 $sql .=
" AND cd.date_fin_validite < '".$db->idate(
dol_time_plus_duree($now, (
int) $duration_value,
"d")).
"'";
108if ($targettype ==
'contacts') {
109 $sql .=
" AND s.rowid = sp.fk_soc";
112if ($targettype ==
'contacts') {
113 $sql .=
" sp.email, sp.rowid,";
115$sql .=
" s.email ASC, s.rowid ASC, cd.date_fin_validite ASC";
118$resql = $db->query($sql);
120 $num = $db->num_rows($resql);
128 $trackthirdpartiessent = array();
130 print
"We found ".$num.
" couples (services to expire-".$targettype.
") qualified\n";
131 dol_syslog(
"We found ".$num.
" couples (services to expire-".$targettype.
") qualified");
137 $obj = $db->fetch_object($resql);
139 $newemail = empty($obj->cemail) ? $obj->email : $obj->cemail;
143 if ($newemail != $oldemail || $oldemail ==
'none') {
146 if ($obj->sid && $obj->sid != $oldsid) {
149 if ($targettype ==
'contacts') {
150 if ($obj->cid && $obj->cid != $oldcid) {
157 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
158 sendEmailTo($mode, $oldemail, $message,
price2num($total), $oldlang, $oldtarget, (
int) $duration_value);
159 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
161 if ($oldemail !=
'none') {
162 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
163 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
165 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
169 $oldemail = $newemail;
171 if ($targettype ==
'contacts') {
174 $oldlang = $obj->default_lang;
175 $oldtarget = (empty($obj->cfirstname) && empty($obj->clastname)) ? $obj->name : ($obj->clastname.
" ".$obj->cfirstname);
184 $outputlangs->setDefaultLang(empty($obj->default_lang) ? $langs->defaultlang : $obj->default_lang);
187 $outputlangs->loadLangs(array(
"main",
"contracts",
"bills",
"products"));
190 $message .= $outputlangs->trans(
"Contract").
" ".$obj->ref.
": ".$outputlangs->trans(
"Service").
" ".
dol_concatdesc($obj->plabel, $obj->description).
" (".
price($obj->total_ttc, 0, $outputlangs, 0, 0, -1, $conf->currency).
"), ".$outputlangs->trans(
"DateEndPlannedShort").
" ".
dol_print_date($db->jdate($obj->date_fin_validite),
'day').
"\n\n";
191 dol_syslog(
"email_expire_services_to_customers.php: ".$newemail.
" ".$message);
194 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').
", customer id ".$obj->sid.
" ".$obj->name.
", ".(isset($obj->cid) ?
"contact id ".$obj->cid.
" ".$obj->clastname.
" ".$obj->cfirstname.
", " :
"").
"email ".$newemail.
", lang ".$outputlangs->defaultlang.
": ";
198 print
"disqualified (no email).";
204 $total += $obj->total_ttc;
210 if ($foundtoprocess) {
211 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
212 sendEmailTo($mode, $oldemail, $message,
price2num($total), $oldlang, $oldtarget, (
int) $duration_value);
213 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
215 if ($oldemail !=
'none') {
216 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
217 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
219 print
"- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
225 print
"No services to expire found\n";
231 dol_syslog(
"email_expire_services_to_customers.php: Error");
248function sendEmailTo($mode, $oldemail, $message, $total, $userlang, $oldtarget, $duration_value)
250 global $conf, $langs;
252 if (getenv(
'DOL_FORCE_EMAIL_TO')) {
253 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
257 $newlangs->setDefaultLang(empty($userlang) ?
getDolGlobalString(
'MAIN_LANG_DEFAULT',
'auto') : $userlang);
258 $newlangs->load(
"main");
259 $newlangs->load(
"contracts");
261 if ($duration_value) {
262 if ($duration_value > 0) {
263 $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDuration", $duration_value);
265 $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDurationNeg", $duration_value);
268 $title = $newlangs->transnoentities(
"ListOfServicesToExpire");
271 $subject =
getDolGlobalString(
'SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_SUBJECT', $title);
277 print
"- Send email to '".$oldtarget.
"' (".$oldemail.
"), total: ".$total.
"\n";
278 dol_syslog(
"email_expire_services_to_customers.php: send mail to ".$oldemail);
292 $allmessage .=
"Dear customer".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
293 $allmessage .=
"Please, find a summary of the services contracted by you that are about to expire.".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
295 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
304 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
306 $mail->errors_to = $errorsto;
309 if ($mode ==
'confirm') {
310 $result = $mail->sendfile();
312 print
"Error sending email ".$mail->error.
"\n";
313 dol_syslog(
"Error sending email ".$mail->error.
"\n");
316 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).
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.