email_unpaid_invoices_to_representatives.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/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. $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 users to remind all unpaid customer invoices user is sale representative for.\n";
  40. print "If you choose 'test' mode, no emails are sent.\n";
  41. print "If you add a delay (nb of days), only invoice with due 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. // Global variables
  49. $version=DOL_VERSION;
  50. $error=0;
  51. /*
  52. * Main
  53. */
  54. @set_time_limit(0);
  55. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  56. $now=dol_now('tzserver');
  57. $duration_value=isset($argv[2])?$argv[2]:'none';
  58. print $script_file." launched with mode ".$mode." default lang=".$langs->defaultlang.(is_numeric($duration_value)?" delay=".$duration_value:"")."\n";
  59. if ($mode != 'confirm') $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  60. $sql = "SELECT f.facnumber, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang,";
  61. $sql.= " u.rowid as uid, u.lastname, u.firstname, u.email, u.lang";
  62. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  63. $sql .= " , ".MAIN_DB_PREFIX."societe as s";
  64. $sql .= " , ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  65. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  66. $sql .= " WHERE f.fk_statut != 0 AND f.paye = 0";
  67. $sql .= " AND f.fk_soc = s.rowid";
  68. if (is_numeric($duration_value)) $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'";
  69. $sql .= " AND sc.fk_soc = s.rowid";
  70. $sql .= " AND sc.fk_user = u.rowid";
  71. $sql .= " ORDER BY u.email ASC, s.rowid ASC, f.facnumber ASC"; // Order by email to allow one message per email
  72. //print $sql;
  73. $resql=$db->query($sql);
  74. if ($resql)
  75. {
  76. $num = $db->num_rows($resql);
  77. $i = 0;
  78. $oldemail = 'none'; $olduid = 0; $oldlang='';
  79. $total = 0; $foundtoprocess = 0;
  80. print "We found ".$num." couples (unpayed validated invoice - sale representative) qualified\n";
  81. dol_syslog("We found ".$num." couples (unpayed validated invoice - sale representative) qualified");
  82. $message='';
  83. if ($num)
  84. {
  85. while ($i < $num)
  86. {
  87. $obj = $db->fetch_object($resql);
  88. if (($obj->email <> $oldemail || $obj->uid <> $olduid) || $oldemail == 'none')
  89. {
  90. // Break onto sales representative (new email or uid)
  91. if (dol_strlen($oldemail) && $oldemail != 'none')
  92. {
  93. envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldsalerepresentative);
  94. }
  95. else
  96. {
  97. if ($oldemail != 'none') print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
  98. }
  99. $oldemail = $obj->email;
  100. $olduid = $obj->uid;
  101. $oldlang = $obj->lang;
  102. $oldsalerepresentative=dolGetFirstLastname($obj->firstname, $obj->lastname);;
  103. $message = '';
  104. $total = 0;
  105. $foundtoprocess = 0;
  106. $salerepresentative=dolGetFirstLastname($obj->firstname, $obj->lastname);
  107. if (empty($obj->email)) print "Warning: Sal representative ".$salerepresentative." has no email. Notice disabled.\n";
  108. }
  109. // Define line content
  110. $outputlangs=new Translate('',$conf);
  111. $outputlangs->setDefaultLang(empty($obj->lang)?$langs->defaultlang:$obj->lang); // By default language of sale representative
  112. $outputlangs->load("bills");
  113. $outputlangs->load("main");
  114. if (dol_strlen($obj->email))
  115. {
  116. $message .= $outputlangs->trans("Invoice")." ".$obj->facnumber." : ".price($obj->total_ttc,0,$outputlangs)." : ".$obj->name."\n";
  117. dol_syslog("email_unpaid_invoices_to_representatives.php: ".$obj->email);
  118. $foundtoprocess++;
  119. }
  120. print "Unpaid invoice ".$obj->facnumber.", 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."): ";
  121. if (dol_strlen($obj->email)) print "qualified.";
  122. else print "disqualified (no email).";
  123. print "\n";
  124. unset($outputlangs);
  125. $total += $obj->total_ttc;
  126. $i++;
  127. }
  128. // Si il reste des envois en buffer
  129. if ($foundtoprocess)
  130. {
  131. if (dol_strlen($oldemail) && $oldemail != 'none') // Break onto email (new email)
  132. {
  133. envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldsalerepresentative);
  134. }
  135. else
  136. {
  137. if ($oldemail != 'none') print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n";
  138. }
  139. }
  140. }
  141. else
  142. {
  143. print "No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n";
  144. }
  145. exit(0);
  146. }
  147. else
  148. {
  149. dol_print_error($db);
  150. dol_syslog("email_unpaid_invoices_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. * @return int <0 if KO, >0 if OK
  163. */
  164. function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldsalerepresentative)
  165. {
  166. global $conf,$langs;
  167. if (getenv('DOL_FORCE_EMAIL_TO')) $oldemail=getenv('DOL_FORCE_EMAIL_TO');
  168. $newlangs=new Translate('',$conf);
  169. $newlangs->setDefaultLang(empty($userlang)?(empty($conf->global->MAIN_LANG_DEFAULT)?'auto':$conf->global->MAIN_LANG_DEFAULT):$userlang);
  170. $newlangs->load("main");
  171. $newlangs->load("bills");
  172. $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT)?$newlangs->trans("ListOfYourUnpaidInvoices"):$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT);
  173. $sendto = $oldemail;
  174. $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
  175. $errorsto = $conf->global->MAIN_MAIL_ERRORS_TO;
  176. $msgishtml = -1;
  177. print "- Send email for ".$oldsalerepresentative." (".$oldemail."), total: ".$total."\n";
  178. dol_syslog("email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail);
  179. $usehtml=0;
  180. if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) $usehtml+=1;
  181. if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) $usehtml+=1;
  182. $allmessage='';
  183. if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER))
  184. {
  185. $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER;
  186. }
  187. else
  188. {
  189. $allmessage.= $newlangs->transnoentities("ListOfYourUnpaidInvoices").($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
  190. $allmessage.= $newlangs->transnoentities("NoteListOfYourUnpaidInvoices").($usehtml?"<br>\n":"\n");
  191. }
  192. $allmessage.= $message.($usehtml?"<br>\n":"\n");
  193. $allmessage.= $langs->trans("Total")." = ".price($total).($usehtml?"<br>\n":"\n");
  194. if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER))
  195. {
  196. $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER;
  197. if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) $usehtml+=1;
  198. }
  199. $mail = new CMailFile(
  200. $subject,
  201. $sendto,
  202. $from,
  203. $allmessage,
  204. array(),
  205. array(),
  206. array(),
  207. '',
  208. '',
  209. 0,
  210. $msgishtml
  211. );
  212. $mail->errors_to = $errorsto;
  213. // Send or not email
  214. if ($mode == 'confirm')
  215. {
  216. $result=$mail->sendfile();
  217. if (! $result)
  218. {
  219. print "Error sending email ".$mail->error."\n";
  220. dol_syslog("Error sending email ".$mail->error."\n");
  221. }
  222. }
  223. else
  224. {
  225. print "No email sent (test mode)\n";
  226. dol_syslog("No email sent (test mode)");
  227. $mail->dump_mail();
  228. $result=1;
  229. }
  230. if ($result)
  231. {
  232. return 1;
  233. }
  234. else
  235. {
  236. return -1;
  237. }
  238. }
  239. ?>