email_expire_services_to_customers.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/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 <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file scripts/contracts/email_expire_services_to_customers.php
  23. * \ingroup facture
  24. * \brief Script to send a mail to customers with services to expire
  25. */
  26. $sapi_type = php_sapi_name();
  27. $script_file = basename(__FILE__);
  28. $path=dirname(__FILE__).'/';
  29. // Test si mode batch
  30. $sapi_type = php_sapi_name();
  31. if (substr($sapi_type, 0, 3) == 'cgi') {
  32. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  33. exit(-1);
  34. }
  35. if (! isset($argv[1]) || ! $argv[1] || ! in_array($argv[1],array('test','confirm')))
  36. {
  37. print "Usage: $script_file [test|confirm] [delay]\n";
  38. print "\n";
  39. print "Send an email to customers to remind all all contracts services to expire.\n";
  40. print "If you choose 'test' mode, no emails are sent.\n";
  41. print "If you add a delay (nb of days), only services with expired date < today + delay are included.\n";
  42. exit(-1);
  43. }
  44. $mode=$argv[1];
  45. require($path."../../htdocs/master.inc.php");
  46. require_once (DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php");
  47. $langs->load('main');
  48. $langs->load('contracts');
  49. // Global variables
  50. $version=DOL_VERSION;
  51. $error=0;
  52. /*
  53. * Main
  54. */
  55. @set_time_limit(0);
  56. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  57. $now=dol_now('tzserver');
  58. $duration_value=isset($argv[2])?$argv[2]:'none';
  59. print $script_file." launched with mode ".$mode.(is_numeric($duration_value)?" delay=".$duration_value:"")."\n";
  60. if ($mode != 'confirm') $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  61. $sql = "SELECT DISTINCT s.nom as name, c.ref, cd.date_fin_validite, cd.total_ttc, p.label label, s.email, s.default_lang";
  62. $sql .= " FROM ".MAIN_DB_PREFIX."societe AS s";
  63. $sql .= ", ".MAIN_DB_PREFIX."contrat AS c";
  64. $sql .= ", ".MAIN_DB_PREFIX."contratdet AS cd";
  65. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product AS p ON p.rowid = cd.fk_product";
  66. $sql .= " WHERE s.rowid = c.fk_soc AND c.rowid = cd.fk_contrat AND c.statut > 0 AND cd.statut<5";
  67. if (is_numeric($duration_value)) $sql .= " AND cd.date_fin_validite < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'";
  68. $sql .= " ORDER BY cd.date_fin_validite ASC, s.rowid ASC";
  69. print $sql;
  70. $resql=$db->query($sql);
  71. if ($resql)
  72. {
  73. $num = $db->num_rows($resql);
  74. $i = 0;
  75. $oldemail = 'none'; $oldlang='';
  76. $total = 0; $foundtoprocess = 0;
  77. print "We found ".$num." couples (services to expire - customer) qualified\n";
  78. dol_syslog("We found ".$num." couples (services to expire - customer) qualified");
  79. $message='';
  80. if ($num)
  81. {
  82. while ($i < $num)
  83. {
  84. $obj = $db->fetch_object($resql);
  85. if (($obj->email <> $oldemail) || $oldemail == 'none')
  86. {
  87. // Break onto sales representative (new email or uid)
  88. if (dol_strlen($oldemail) && $oldemail != 'none')
  89. {
  90. envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer,$duration_value);
  91. }
  92. else
  93. {
  94. if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n";
  95. }
  96. $oldemail = $obj->email;
  97. $oldlang = $obj->lang;
  98. $oldcustomer=$obj->name;
  99. $message = '';
  100. $total = 0;
  101. $foundtoprocess = 0;
  102. $customer=$obj->name;
  103. if (empty($obj->email)) print "Warning: Customer ".$customer." has no email. Notice disabled.\n";
  104. }
  105. if (dol_strlen($oldemail))
  106. {
  107. $message .= $langs->trans("Contract")." ".$obj->ref.": ".$langs->trans("Service")." ".$obj->label." (".price($obj->total_ttc)."), ".$langs->trans("DateEndPlannedShort")." ".dol_print_date($db->jdate($obj->date_fin_validite),'day')."\n\n";
  108. dol_syslog("email_expire_services_to_customers.php: ".$obj->email);
  109. $foundtoprocess++;
  110. }
  111. print "Service to expire ".$obj->ref.", label ".$obj->label.", due date ".dol_print_date($db->jdate($obj->date_fin_validite),'day')." (linked to company ".$obj->nom.", sale representative ".dolGetFirstLastname($obj->firstname, $obj->lastname).", email ".$obj->email."): ";
  112. if (dol_strlen($obj->email)) print "qualified.";
  113. else print "disqualified (no email).";
  114. print "\n";
  115. $total += $obj->total_ttc;
  116. $i++;
  117. }
  118. // Si il reste des envois en buffer
  119. if ($foundtoprocess)
  120. {
  121. if (dol_strlen($oldemail) && $oldemail != 'none') // Break onto email (new email)
  122. {
  123. envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer,$duration_value);
  124. }
  125. else
  126. {
  127. if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n";
  128. }
  129. }
  130. }
  131. else
  132. {
  133. print "No unpaid invoices found\n";
  134. }
  135. exit(0);
  136. }
  137. else
  138. {
  139. dol_print_error($db);
  140. dol_syslog("email_expire_services_to_customers.php: Error");
  141. exit(-1);
  142. }
  143. /**
  144. * Send email
  145. *
  146. * @param string $mode Mode (test | confirm)
  147. * @param string $oldemail Old email
  148. * @param string $message Message to send
  149. * @param string $total Total amount of unpayed invoices
  150. * @param string $userlang Code lang to use for email output.
  151. * @param string $oldcustomer Old customer
  152. * @param int $duration_value duration value
  153. * @return int <0 if KO, >0 if OK
  154. */
  155. function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldcustomer,$duration_value)
  156. {
  157. global $conf,$langs;
  158. if (getenv('DOL_FORCE_EMAIL_TO')) $oldemail=getenv('DOL_FORCE_EMAIL_TO');
  159. $newlangs=new Translate('',$conf);
  160. $newlangs->setDefaultLang(empty($userlang)?(empty($conf->global->MAIN_LANG_DEFAULT)?'auto':$conf->global->MAIN_LANG_DEFAULT):$userlang);
  161. $newlangs->load("main");
  162. $newlangs->load("contracts");
  163. if ($duration_value)
  164. $title=$newlangs->transnoentities("ListOfServicesToExpireWithDuration",$duration_value);
  165. else
  166. $title= $newlangs->transnoentities("ListOfServicesToExpire");
  167. $subject = "[".(empty($conf->global->MAIN_APPLICATION_TITLE)?'Dolibarr':$conf->global->MAIN_APPLICATION_TITLE)."] ".$title;
  168. $sendto = $oldemail;
  169. $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
  170. $errorsto = $conf->global->MAIN_MAIL_ERRORS_TO;
  171. $msgishtml = -1;
  172. print "- Send email for ".$oldcustomer."(".$oldemail."), total: ".$total."\n";
  173. dol_syslog("email_expire_services_to_customers.php: send mail to ".$oldemail);
  174. $usehtml=0;
  175. if (dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER)) $usehtml+=1;
  176. if (dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER)) $usehtml+=1;
  177. $allmessage='';
  178. if (! empty($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER))
  179. {
  180. $allmessage.=$conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_HEADER;
  181. }
  182. else
  183. {
  184. $allmessage.= "Dear customer".($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
  185. $allmessage.= "Please, find a summary of the services contracted by you that are about to expire.".($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
  186. $allmessage.= "Note: This list contains only services to expire.".($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
  187. }
  188. $allmessage.= $message.($usehtml?"<br>\n":"\n");
  189. $allmessage.= $langs->trans("Total")." = ".price($total).($usehtml?"<br>\n":"\n");
  190. if (! empty($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER))
  191. {
  192. $allmessage.=$conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER;
  193. if (dol_textishtml($conf->global->SCRIPT_EMAIL_EXPIRE_SERVICES_CUSTOMERS_FOOTER)) $usehtml+=1;
  194. }
  195. $mail = new CMailFile(
  196. $subject,
  197. $sendto,
  198. $from,
  199. $allmessage,
  200. array(),
  201. array(),
  202. array(),
  203. '',
  204. '',
  205. 0,
  206. $msgishtml
  207. );
  208. $mail->errors_to = $errorsto;
  209. // Send or not email
  210. if ($mode == 'confirm')
  211. {
  212. $result=$mail->sendfile();
  213. if (! $result)
  214. {
  215. print "Error sending email ".$mail->error."\n";
  216. dol_syslog("Error sending email ".$mail->error."\n");
  217. }
  218. }
  219. else
  220. {
  221. print "No email sent (test mode)\n";
  222. dol_syslog("No email sent (test mode)");
  223. $mail->dump_mail();
  224. $result=1;
  225. }
  226. if ($result)
  227. {
  228. return 1;
  229. }
  230. else
  231. {
  232. return -1;
  233. }
  234. }
  235. ?>