email_unpaid_invoices_to_representatives.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/invoices/email_unpaid_invoices_to_representatives.php
  23. * \ingroup facture
  24. * \brief Script to send a mail to dolibarr users linked to companies with unpaid invoices
  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 users to remind all unpaid customer invoices user is 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 invoice with due 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->load('main');
  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. if (!empty($dolibarr_main_db_readonly)) {
  66. print "Error: instance in read-onyl mode\n";
  67. exit(-1);
  68. }
  69. $sql = "SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang,";
  70. $sql .= " u.rowid as uid, u.lastname, u.firstname, u.email, u.lang";
  71. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  72. $sql .= " , ".MAIN_DB_PREFIX."societe as s";
  73. $sql .= " , ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  74. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  75. $sql .= " WHERE f.fk_statut = 1 AND f.paye = 0";
  76. $sql .= " AND f.fk_soc = s.rowid";
  77. if (is_numeric($duration_value)) {
  78. $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'";
  79. }
  80. $sql .= " AND sc.fk_soc = s.rowid";
  81. $sql .= " AND sc.fk_user = u.rowid";
  82. $sql .= " ORDER BY u.email ASC, s.rowid ASC, f.ref ASC"; // Order by email to allow one message per email
  83. // print $sql;
  84. $resql = $db->query($sql);
  85. if ($resql) {
  86. $num = $db->num_rows($resql);
  87. $i = 0;
  88. $oldemail = 'none';
  89. $olduid = 0;
  90. $oldlang = '';
  91. $total = 0;
  92. $foundtoprocess = 0;
  93. print "We found ".$num." couples (unpayed validated invoice - sale representative) qualified\n";
  94. dol_syslog("We found ".$num." couples (unpayed validated invoice - sale representative) qualified");
  95. $message = '';
  96. $oldsalerepresentative = 0;
  97. if ($num) {
  98. while ($i < $num) {
  99. $obj = $db->fetch_object($resql);
  100. if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail == 'none') {
  101. // Break onto sales representative (new email or uid)
  102. if (dol_strlen($oldemail) && $oldemail != 'none') {
  103. envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
  104. } else {
  105. if ($oldemail != 'none') {
  106. print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
  107. }
  108. }
  109. $oldemail = $obj->email;
  110. $olduid = $obj->uid;
  111. $oldlang = $obj->lang;
  112. $oldsalerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname);
  113. $message = '';
  114. $total = 0;
  115. $foundtoprocess = 0;
  116. $salerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname);
  117. if (empty($obj->email)) {
  118. print "Warning: Sale representative ".$salerepresentative." has no email. Notice disabled.\n";
  119. }
  120. }
  121. // Define line content
  122. $outputlangs = new Translate('', $conf);
  123. $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang); // By default language of sale representative
  124. // Load translation files required by the page
  125. $outputlangs->loadLangs(array("main", "bills"));
  126. if (dol_strlen($obj->email)) {
  127. $message .= $outputlangs->trans("Invoice")." ".$obj->ref." : ".price($obj->total_ttc, 0, $outputlangs, 0, 0, -1, $conf->currency)." : ".$obj->name."\n";
  128. dol_syslog("email_unpaid_invoices_to_representatives.php: ".$obj->email);
  129. $foundtoprocess++;
  130. }
  131. print "Unpaid invoice ".$obj->ref.", price ".price2num($obj->total_ttc).", due date ".dol_print_date($db->jdate($obj->due_date), 'day')." (linked to company ".$obj->name.", sale representative ".dolGetFirstLastname($obj->firstname, $obj->lastname).", email ".$obj->email.", lang ".$outputlangs->defaultlang."): ";
  132. if (dol_strlen($obj->email)) {
  133. print "qualified.";
  134. } else {
  135. print "disqualified (no email).";
  136. }
  137. print "\n";
  138. unset($outputlangs);
  139. $total += $obj->total_ttc;
  140. $i++;
  141. }
  142. // Si il reste des envois en buffer
  143. if ($foundtoprocess) {
  144. if (dol_strlen($oldemail) && $oldemail != 'none') { // Break onto email (new email)
  145. envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative);
  146. } else {
  147. if ($oldemail != 'none') {
  148. print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
  149. }
  150. }
  151. }
  152. } else {
  153. print "No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n";
  154. }
  155. exit(0);
  156. } else {
  157. dol_print_error($db);
  158. dol_syslog("email_unpaid_invoices_to_representatives.php: Error");
  159. exit(-1);
  160. }
  161. /**
  162. * Send email
  163. *
  164. * @param string $mode Mode (test | confirm)
  165. * @param string $oldemail Target email
  166. * @param string $message Message to send
  167. * @param string $total Total amount of unpayed invoices
  168. * @param string $userlang Code lang to use for email output.
  169. * @param string $oldtarget Target name of sale representative
  170. * @return int Int <0 if KO, >0 if OK
  171. */
  172. function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldtarget)
  173. {
  174. global $conf, $langs;
  175. if (getenv('DOL_FORCE_EMAIL_TO')) {
  176. $oldemail = getenv('DOL_FORCE_EMAIL_TO');
  177. }
  178. $newlangs = new Translate('', $conf);
  179. $newlangs->setDefaultLang(empty($userlang) ? getDolGlobalString('MAIN_LANG_DEFAULT', 'auto') : $userlang);
  180. $newlangs->load("main");
  181. $newlangs->load("bills");
  182. $subject = getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT', $newlangs->trans("ListOfYourUnpaidInvoices"));
  183. $sendto = $oldemail;
  184. $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
  185. $errorsto = getDolGlobalString('MAIN_MAIL_ERRORS_TO');
  186. $msgishtml = -1;
  187. print "- Send email for ".$oldtarget." (".$oldemail."), total: ".$total."\n";
  188. dol_syslog("email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail);
  189. $usehtml = 0;
  190. if (getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER') && dol_textishtml(getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER'))) {
  191. $usehtml += 1;
  192. }
  193. if (getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER') && dol_textishtml(getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER'))) {
  194. $usehtml += 1;
  195. }
  196. $allmessage = '';
  197. if (getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER')) {
  198. $allmessage .= getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER');
  199. } else {
  200. $allmessage .= $newlangs->transnoentities("ListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n").($usehtml ? "<br>\n" : "\n");
  201. $allmessage .= $newlangs->transnoentities("NoteListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n");
  202. }
  203. $allmessage .= $message.($usehtml ? "<br>\n" : "\n");
  204. $allmessage .= $langs->trans("Total")." = ".price($total, 0, $newlangs, 0, 0, -1, $conf->currency).($usehtml ? "<br>\n" : "\n");
  205. if (getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER')) {
  206. $allmessage .= getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER');
  207. if (dol_textishtml(getDolGlobalString('SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER'))) {
  208. $usehtml += 1;
  209. }
  210. }
  211. $mail = new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(), '', '', 0, $msgishtml);
  212. $mail->errors_to = $errorsto;
  213. // Send or not email
  214. if ($mode == 'confirm') {
  215. $result = $mail->sendfile();
  216. if (!$result) {
  217. print "Error sending email ".$mail->error."\n";
  218. dol_syslog("Error sending email ".$mail->error."\n");
  219. }
  220. } else {
  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. return 1;
  228. } else {
  229. return -1;
  230. }
  231. }