email_expire_services_to_representatives.php 10 KB

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