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')) || !in_array($argv[2], array(
'thirdparties',
'contacts'))) {
42 print "Usage: $script_file (test|confirm) (thirdparties|contacts) [delay] [after]\n";
44 print "Send an email to customers to remind all contracts services to expire or expired.\n";
45 print "If you choose 'test' mode, no emails are sent.\n";
46 print "If you add param delay (nb of days), only services with expired date < today + delay are included.\n";
47 print "If you add param after (nb of days), only services with expired date >= today + delay are included.\n";
51 $targettype = $argv[2];
53 require $path.
"../../htdocs/master.inc.php";
54 require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
56 $langs->loadLangs(array(
'main',
'contracts'));
59 $version = DOL_VERSION;
68 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
71 $duration_value = isset($argv[3]) ? $argv[3] :
'none';
72 $duration_value2 = isset($argv[4]) ? $argv[4] :
'none';
75 print $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";
77 if ($mode !=
'confirm') {
78 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
81 $sql =
"SELECT c.ref, cd.date_fin_validite, cd.total_ttc, cd.description as description, p.label as plabel,";
82 $sql .=
" s.rowid as sid, s.nom as name, s.email, s.default_lang";
83 if ($targettype ==
'contacts')
84 $sql .=
", sp.rowid as cid, sp.firstname as cfirstname, sp.lastname as clastname, sp.email as cemail";
85 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe AS s";
86 if ($targettype ==
'contacts')
87 $sql .=
", ".MAIN_DB_PREFIX.
"socpeople as sp";
88 $sql .=
", ".MAIN_DB_PREFIX.
"contrat AS c";
89 $sql .=
", ".MAIN_DB_PREFIX.
"contratdet AS cd";
90 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = cd.fk_product";
91 $sql .=
" WHERE s.rowid = c.fk_soc AND c.rowid = cd.fk_contrat AND c.statut > 0 AND cd.statut < 5";
92 if (is_numeric($duration_value2))
93 $sql .=
" AND cd.date_fin_validite >= '".$db->idate(
dol_time_plus_duree($now, $duration_value2,
"d")).
"'";
94 if (is_numeric($duration_value))
95 $sql .=
" AND cd.date_fin_validite < '".$db->idate(
dol_time_plus_duree($now, $duration_value,
"d")).
"'";
96 if ($targettype ==
'contacts')
97 $sql .=
" AND s.rowid = sp.fk_soc";
99 if ($targettype ==
'contacts')
100 $sql .=
" sp.email, sp.rowid,";
101 $sql .=
" s.email ASC, s.rowid ASC, cd.date_fin_validite ASC";
104 $resql = $db->query($sql);
106 $num = $db->num_rows(
$resql);
114 $trackthirdpartiessent = array();
116 print "We found ".$num.
" couples (services to expire-".$targettype.
") qualified\n";
117 dol_syslog(
"We found ".$num.
" couples (services to expire-".$targettype.
") qualified");
123 $obj = $db->fetch_object(
$resql);
125 $newemail = empty($obj->cemail) ? $obj->email : $obj->cemail;
129 if ($newemail != $oldemail || $oldemail ==
'none') {
132 if ($obj->sid && $obj->sid != $oldsid) {
135 if ($targettype ==
'contacts') {
136 if ($obj->cid && $obj->cid != $oldcid) {
143 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail])) {
144 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldtarget, $duration_value);
145 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
147 if ($oldemail !=
'none') {
148 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
149 print "- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
150 else print "- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
153 $oldemail = $newemail;
155 if ($targettype ==
'contacts') {
158 $oldlang = $obj->default_lang;
159 $oldtarget = (empty($obj->cfirstname) && empty($obj->clastname)) ? $obj->name : ($obj->clastname.
" ".$obj->cfirstname);
168 $outputlangs->setDefaultLang(empty($obj->default_lang) ? $langs->defaultlang : $obj->default_lang);
171 $outputlangs->loadLangs(array(
"main",
"contracts",
"bills",
"products"));
174 $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";
175 dol_syslog(
"email_expire_services_to_customers.php: ".$newemail.
" ".$message);
178 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.
": ";
181 else print "disqualified (no email).";
186 $total += $obj->total_ttc;
192 if ($foundtoprocess) {
193 if (
dol_strlen($oldemail) && $oldemail !=
'none' && empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
195 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldtarget, $duration_value);
196 $trackthirdpartiessent[$oldsid.
'|'.$oldemail] =
'contact id '.$oldcid;
198 if ($oldemail !=
'none') {
199 if (empty($trackthirdpartiessent[$oldsid.
'|'.$oldemail]))
200 print "- No email sent for '".$oldtarget.
"', total: ".$total.
"\n";
201 else print "- No email sent for '".$oldtarget.
"', total: ".$total.
" (already sent to ".$trackthirdpartiessent[$oldsid.
'|'.$oldemail].
")\n";
206 print "No services to expire found\n";
212 dol_syslog(
"email_expire_services_to_customers.php: Error");
229 function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget, $duration_value)
231 global $conf, $langs;
233 if (getenv(
'DOL_FORCE_EMAIL_TO'))
234 $oldemail = getenv(
'DOL_FORCE_EMAIL_TO');
237 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ?
'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang);
238 $newlangs->load(
"main");
239 $newlangs->load(
"contracts");
241 if ($duration_value) {
242 if ($duration_value > 0)
243 $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDuration", $duration_value);
244 else $title = $newlangs->transnoentities(
"ListOfServicesToExpireWithDurationNeg", $duration_value);
245 }
else $title = $newlangs->transnoentities(
"ListOfServicesToExpire");
247 $subject = (empty($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_SUBJECT) ? $title : $conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_SUBJECT);
249 $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
250 $errorsto = $conf->global->MAIN_MAIL_ERRORS_TO;
253 print "- Send email to '".$oldtarget.
"' (".$oldemail.
"), total: ".$total.
"\n";
254 dol_syslog(
"email_expire_services_to_customers.php: send mail to ".$oldemail);
257 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER))
259 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER))
263 if (!empty($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER)) {
264 $allmessage .= $conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER;
266 $allmessage .=
"Dear customer".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
267 $allmessage .=
"Please, find a summary of the services contracted by you that are about to expire.".($usehtml ?
"<br>\n" :
"\n").($usehtml ?
"<br>\n" :
"\n");
269 $allmessage .= $message.($usehtml ?
"<br>\n" :
"\n");
271 if (!empty($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER)) {
272 $allmessage .= $conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER;
273 if (
dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER))
277 $mail =
new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(),
'',
'', 0, $msgishtml);
279 $mail->errors_to = $errorsto;
282 if ($mode ==
'confirm') {
283 $result = $mail->sendfile();
285 print "Error sending email ".$mail->error.
"\n";
286 dol_syslog(
"Error sending email ".$mail->error.
"\n");
289 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.
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...
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.
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.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
dol_textishtml($msg, $option=0)
Return if a text is a html content.